Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go sdk #104

Merged
merged 12 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: Bug report
about: Create a report to help us improve this project
labels: bug-p3
assignees: mrz1836
assignees: rohenaz

---

Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: Feature request
about: Suggest an idea for this project
labels: idea
assignees: mrz1836
assignees: rohenaz

---

Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/question.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: Question
about: 'General template for a question '
labels: question
assignees: mrz1836
assignees: rohenaz

---

Expand Down
8 changes: 4 additions & 4 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ updates:
# Check for npm updates at 9am UTC (5am EST)
time: "10:00"
reviewers:
- "mrz1836"
- "rohenaz"
assignees:
- "mrz1836"
- "rohenaz"
# Labels must be created first
labels:
- "update"
Expand All @@ -25,9 +25,9 @@ updates:
interval: "weekly"
day: "monday"
reviewers:
- "mrz1836"
- "rohenaz"
assignees:
- "mrz1836"
- "rohenaz"
labels:
- "chore"
open-pull-requests-limit: 10
15 changes: 6 additions & 9 deletions .github/mergify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ pull_request_rules:
conditions:
- -draft
- author~=^dependabot(|-preview)\[bot\]$
- check-success='test (1.18.x, ubuntu-latest)'
- check-success='test (1.19.x, ubuntu-latest)'
- check-success='test (1.23.x, ubuntu-latest)'
- check-success='Analyze (go)'
- title~=^Bump [^\s]+ from ([\d]+)\..+ to \1\.
actions:
Expand All @@ -21,13 +20,12 @@ pull_request_rules:
- name: Alert on major version detection
conditions:
- author~=^dependabot(|-preview)\[bot\]$
- check-success='test (1.18.x, ubuntu-latest)'
- check-success='test (1.19.x, ubuntu-latest)'
- check-success='test (1.23.x, ubuntu-latest)'
- check-success='Analyze (go)'
- -title~=^Bump [^\s]+ from ([\d]+)\..+ to \1\.
actions:
comment:
message: "⚠️ @mrz1836: this is a major version bump and requires your attention"
message: "⚠️ @rohenaz: this is a major version bump and requires your attention"

# ===============================================================================
# AUTOMATIC MERGE (APPROVALS)
Expand All @@ -38,8 +36,7 @@ pull_request_rules:
- "#approved-reviews-by>=1"
- "#review-requested=0"
- "#changes-requested-reviews-by=0"
- check-success='test (1.18.x, ubuntu-latest)'
- check-success='test (1.19.x, ubuntu-latest)'
- check-success='test (1.23.x, ubuntu-latest)'
- check-success='Analyze (go)'
- -title~=(?i)wip
- label!=work-in-progress
Expand All @@ -57,7 +54,7 @@ pull_request_rules:
- "#assignee=0"
actions:
assign:
users: [ "mrz1836" ]
users: [ "rohenaz" ]

# ===============================================================================
# ALERTS
Expand Down Expand Up @@ -170,7 +167,7 @@ pull_request_rules:
- and:
- author!=dependabot[bot]
- author!=mergify[bot]
- author!=mrz1836
- author!=rohenaz
actions:
comment:
message: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.19
go-version: 1.23
- name: Run GoReleaser
uses: goreleaser/[email protected]
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
test:
strategy:
matrix:
go-version: [ 1.18.x, 1.19.x ]
go-version: [ 1.23.x ]
os: [ ubuntu-latest ]
runs-on: ${{ matrix.os }}
steps:
Expand Down
44 changes: 14 additions & 30 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Make sure to check the documentation at http://goreleaser.com
# .goreleaser.yaml

version: 2

# ---------------------------
# General
# ---------------------------
Expand All @@ -16,31 +19,22 @@ changelog:
- '^test:'

# ---------------------------
# Publishers
# Builders
# ---------------------------
# publishers:
# - name: "Publish GoDocs"
# cmd: make godocs
builds:
- skip: true

# ---------------------------
# Builder
# ---------------------------
build:
skip: true

# ---------------------------
# Github Release
# GitHub Release
# ---------------------------
release:
prerelease: true
name_template: "Release v{{.Version}}"
prerelease: false
name_template: "Release v{{ .Version }}"

# ---------------------------
# Announce
# ---------------------------
announce:

# See more at: https://goreleaser.com/customization/announce/#slack
slack:
enabled: false
message_template: '{{ .ProjectName }} {{ .Tag }} is out! Changelog: https://github.com/BitcoinSchema/{{ .ProjectName }}/releases/tag/{{ .Tag }}'
Expand All @@ -49,30 +43,20 @@ announce:
# icon_emoji: ''
# icon_url: ''

# See more at: https://goreleaser.com/customization/announce/#twitter
twitter:
enabled: false
message_template: '{{ .ProjectName }} {{ .Tag }} is out!'

# See more at: https://goreleaser.com/customization/announce/#discord
discord:
enabled: false
message_template: '{{ .ProjectName }} {{ .Tag }} is out!'
# Defaults to `GoReleaser`
author: ''
# Defaults to `3888754` - the grey-ish from goreleaser
color: ''
# Defaults to `https://goreleaser.com/static/avatar.png`
icon_url: ''
# author: ''
# color: ''
# icon_url: ''

# See more at: https://goreleaser.com/customization/announce/#reddit
reddit:
enabled: false
# Application ID for Reddit Application
application_id: ""
# Username for your Reddit account
username: ""
# Defaults to `{{ .GitURL }}/releases/tag/{{ .Tag }}`
# url_template: 'https://github.com/BitcoinSchema/{{ .ProjectName }}/releases/tag/{{ .Tag }}'
# Defaults to `{{ .ProjectName }} {{ .Tag }} is out!`
title_template: '{{ .ProjectName }} {{ .Tag }} is out!'
# url_template: 'https://github.com/BitcoinSchema/{{ .ProjectName }}/releases/tag/{{ .Tag }}'
8 changes: 4 additions & 4 deletions .make/go.mk
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ install-go: ## Install the application (Using Native Go)
lint: ## Run the golangci-lint application (install if not found)
@echo "installing golangci-lint..."
@#Travis (has sudo)
@if [ "$(shell command -v golangci-lint)" = "" ] && [ $(TRAVIS) ]; then curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.53.3 && sudo cp ./bin/golangci-lint $(go env GOPATH)/bin/; fi;
@if [ "$(shell command -v golangci-lint)" = "" ] && [ $(TRAVIS) ]; then curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.61.0 && sudo cp ./bin/golangci-lint $(go env GOPATH)/bin/; fi;
@#AWS CodePipeline
@if [ "$(shell command -v golangci-lint)" = "" ] && [ "$(CODEBUILD_BUILD_ID)" != "" ]; then curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.53.3; fi;
@if [ "$(shell command -v golangci-lint)" = "" ] && [ "$(CODEBUILD_BUILD_ID)" != "" ]; then curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.61.0; fi;
@#GitHub Actions
@if [ "$(shell command -v golangci-lint)" = "" ] && [ "$(GITHUB_WORKFLOW)" != "" ]; then curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sudo sh -s -- -b $(go env GOPATH)/bin v1.53.3; fi;
@if [ "$(shell command -v golangci-lint)" = "" ] && [ "$(GITHUB_WORKFLOW)" != "" ]; then curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sudo sh -s -- -b $(go env GOPATH)/bin v1.61.0; fi;
@#Brew - MacOS
@if [ "$(shell command -v golangci-lint)" = "" ] && [ "$(shell command -v brew)" != "" ]; then brew install golangci-lint; fi;
@#MacOS Vanilla
@if [ "$(shell command -v golangci-lint)" = "" ] && [ "$(shell command -v brew)" != "" ]; then curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- v1.53.3; fi;
@if [ "$(shell command -v golangci-lint)" = "" ] && [ "$(shell command -v brew)" != "" ]; then curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- v1.61.0; fi;
@echo "running golangci-lint..."
@golangci-lint run --verbose

Expand Down
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
<br/>

## Table of Contents
- [Installation](#installation)
- [Documentation](#documentation)
- [Examples & Tests](#examples--tests)
- [Benchmarks](#benchmarks)
- [Code Standards](#code-standards)
- [Usage](#usage)
- [Maintainers](#maintainers)
- [Contributing](#contributing)
- [License](#license)
- [go-aip](#go-aip)
- [Table of Contents](#table-of-contents)
- [Installation](#installation)
- [Documentation](#documentation)
- [Features](#features)
- [Examples \& Tests](#examples--tests)
- [Benchmarks](#benchmarks)
- [Code Standards](#code-standards)
- [Usage](#usage)
- [Maintainers](#maintainers)
- [Contributing](#contributing)
- [How can I help?](#how-can-i-help)
- [License](#license)

<br/>

Expand Down Expand Up @@ -51,9 +55,8 @@ View the generated [documentation](https://pkg.go.dev/github.com/bitcoinschema/g
<summary><strong><code>Package Dependencies</code></strong></summary>
<br/>

- [bitcoinschema/go-bitcoin](https://github.com/bitcoinschema/go-bitcoin)
- [bitcoin-sv/go-sdk](https://github.com/bitcoin-sv/go-sdk)
- [bitcoinschema/go-bob](https://github.com/bitcoinschema/go-bob)
- [libsv/go-bt](https://github.com/libsv/go-bt)
</details>

<details>
Expand Down Expand Up @@ -115,7 +118,7 @@ vet Run the Go vet application

## Examples & Tests
All unit tests and [examples](examples) run via [GitHub Actions](https://github.com/BitcoinSchema/go-aip/actions) and
uses [Go version 1.18.x](https://golang.org/doc/go1.18). View the [configuration file](.github/workflows/run-tests.yml).
uses [Go version 1.23.x](https://golang.org/doc/go1.23). View the [configuration file](.github/workflows/run-tests.yml).

Run all tests (including integration tests)
```shell script
Expand Down
57 changes: 37 additions & 20 deletions aip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ package aip

import (
"bytes"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"strings"

"github.com/bitcoinschema/go-bitcoin"
"github.com/bitcoinsv/bsvd/txscript"
"github.com/bitcoinsv/bsvutil"
bsm "github.com/bitcoin-sv/go-sdk/compat/bsm"
ec "github.com/bitcoin-sv/go-sdk/primitives/ec"
"github.com/bitcoin-sv/go-sdk/script"
)

// Prefix is the Bitcom prefix used by AIP
Expand All @@ -24,7 +25,7 @@ var Prefix = "15PciHG22SNLQJXMoSUaWVi7WSqc7hCfva"
var hexPrefix = hex.EncodeToString([]byte(Prefix))

const pipe = "|"
const opReturn = string(rune(txscript.OP_RETURN)) // creates: j
const opReturn = string(rune(script.OpRETURN)) // creates: j

// Algorithm is an enum for the different possible signature algorithms
type Algorithm string
Expand Down Expand Up @@ -58,30 +59,40 @@ func (a *Aip) Validate() (bool, error) {
return false, fmt.Errorf("the first item in payload is always OP_RETURN, got: %s", a.Data[0])
}

sig, err := base64.StdEncoding.DecodeString(a.Signature)
if err != nil {
return false, err
}
// Convert pubkey to address
if a.Algorithm == Paymail {
// Detect whether this key was compressed when sig was made
_, wasCompressed, err := bitcoin.PubKeyFromSignature(a.Signature, strings.Join(a.Data, ""))
_, wasCompressed, err := bsm.PubKeyFromSignature(sig, []byte(strings.Join(a.Data, "")))
if err != nil {
return false, err
}

// Get the public address for this paymail from pki
var addr *bsvutil.LegacyAddressPubKeyHash
if addr, err = bitcoin.GetAddressFromPubKeyString(a.AlgorithmSigningComponent, wasCompressed); err != nil {
var pubKey *ec.PublicKey
var addr *script.Address
if pubKey, err = ec.PublicKeyFromString(a.AlgorithmSigningComponent); err != nil {
return false, err
}
if addr, err = script.NewAddressFromPublicKeyWithCompression(
pubKey,
true,
wasCompressed); err != nil {
return false, err
}
a.AlgorithmSigningComponent = addr.String()
a.AlgorithmSigningComponent = addr.AddressString

}

// You get the address associated with the pki instead of the current address
err := bitcoin.VerifyMessage(a.AlgorithmSigningComponent, a.Signature, strings.Join(a.Data, ""))
err = bsm.VerifyMessage(a.AlgorithmSigningComponent, sig, []byte(strings.Join(a.Data, "")))
return err == nil, err
}

// Sign will provide an AIP signature for a given private key and message using
// the provided algorithm. It prepends an OP_RETURN to the payload
func Sign(privateKey string, algorithm Algorithm, message string) (a *Aip, err error) {
func Sign(privateKey *ec.PrivateKey, algorithm Algorithm, message string) (a *Aip, err error) {

// Prepend the OP_RETURN to keep consistent with BitcoinFiles SDK
// data = append(data, []byte{byte(txscript.OP_RETURN)})
Expand All @@ -91,31 +102,37 @@ func Sign(privateKey string, algorithm Algorithm, message string) (a *Aip, err e
a = &Aip{Algorithm: algorithm, Data: prependedData}

// Sign using the private key and the message
if a.Signature, err = bitcoin.SignMessage(privateKey, strings.Join(prependedData, ""), false); err != nil {
return
var sig []byte
if sig, err = bsm.SignMessage(privateKey, []byte(strings.Join(prependedData, ""))); err != nil {
return nil, err
}

a.Signature = base64.StdEncoding.EncodeToString(sig)

// Store address vs pubkey
switch algorithm {
case BitcoinECDSA, BitcoinSignedMessage:
// Signing component = bitcoin address
// Get the address of the private key
if a.AlgorithmSigningComponent, err = bitcoin.GetAddressFromPrivateKeyString(privateKey, false); err != nil {
return
if add, err := script.NewAddressFromPublicKey(privateKey.PubKey(), true); err != nil {
return nil, err
} else {
a.AlgorithmSigningComponent = add.AddressString
}
case Paymail:
// Signing component = paymail identity key
// Get pubKey from private key and overload the address field in AIP
if a.AlgorithmSigningComponent, err = bitcoin.PubKeyFromPrivateKeyString(privateKey, false); err != nil {
return
}
// if pubkey, err := bitcoin.PubKeyFromPrivateKeyString(privateKey, false); err != nil {
// return
// }
a.AlgorithmSigningComponent = hex.EncodeToString(privateKey.PubKey().SerializeCompressed())
}

return
}

// SignOpReturnData will append the given data and return a bt.Output
func SignOpReturnData(privateKey string, algorithm Algorithm,
func SignOpReturnData(privateKey *ec.PrivateKey, algorithm Algorithm,
data [][]byte) (outData [][]byte, a *Aip, err error) {

// Sign with AIP
Expand Down
Loading
Loading