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

feat(SPV-936): upgrade to go-sdk #250

Merged
merged 15 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
64 changes: 34 additions & 30 deletions authentication.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package walletclient

import (
"encoding/hex"
"fmt"
"net/http"
"time"

bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32"
bsm "github.com/bitcoin-sv/go-sdk/compat/bsm"
ec "github.com/bitcoin-sv/go-sdk/primitives/ec"
script "github.com/bitcoin-sv/go-sdk/script"
trx "github.com/bitcoin-sv/go-sdk/transaction"
sighash "github.com/bitcoin-sv/go-sdk/transaction/sighash"

"github.com/bitcoin-sv/spv-wallet-go-client/utils"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoinschema/go-bitcoin/v2"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bk/bip32"
"github.com/libsv/go-bt/v2"
"github.com/libsv/go-bt/v2/bscript"
"github.com/libsv/go-bt/v2/sighash"
)

// TODO: NewTxFromString - NewTransactionFromHex ?
// TODO: NewFromHexString - no replacement in v2
// TODO: NewP2PKHUnlockingScript - not found
wregulski marked this conversation as resolved.
Show resolved Hide resolved

// SetSignature will set the signature on the header for the request
func setSignature(header *http.Header, xPriv *bip32.ExtendedKey, bodyString string) error {
// Create the signature
Expand All @@ -34,14 +38,14 @@

// GetSignedHex will sign all the inputs using the given xPriv key
func GetSignedHex(dt *models.DraftTransaction, xPriv *bip32.ExtendedKey) (signedHex string, err error) {
var tx *bt.Tx
if tx, err = bt.NewTxFromString(dt.Hex); err != nil {
var tx *trx.Transaction
if tx, err = trx.NewTransactionFromHex(dt.Hex); err != nil {
return
}

// Enrich inputs
for index, draftInput := range dt.Configuration.Inputs {
tx.Inputs[index].PreviousTxSatoshis = draftInput.Satoshis

Check failure on line 48 in authentication.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

tx.Inputs[index].PreviousTxSatoshis undefined (type *"github.com/bitcoin-sv/go-sdk/transaction".TransactionInput has no field or method PreviousTxSatoshis)

dst := draftInput.Destination
if err = setPreviousTxScript(tx, uint32(index), &dst); err != nil {
Expand All @@ -58,23 +62,23 @@
return
}

func setPreviousTxScript(tx *bt.Tx, inputIndex uint32, dst *models.Destination) (err error) {
var ls *bscript.Script
if ls, err = bscript.NewFromHexString(dst.LockingScript); err != nil {
func setPreviousTxScript(tx *trx.Transaction, inputIndex uint32, dst *models.Destination) (err error) {
var ls *script.Script
if ls, err = script.NewFromHexString(dst.LockingScript); err != nil {

Check failure on line 67 in authentication.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

undefined: script.NewFromHexString
return
}

tx.Inputs[inputIndex].PreviousTxScript = ls

Check failure on line 71 in authentication.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

tx.Inputs[inputIndex].PreviousTxScript undefined (type *"github.com/bitcoin-sv/go-sdk/transaction".TransactionInput has no field or method PreviousTxScript)
return
}

func setUnlockingScript(tx *bt.Tx, inputIndex uint32, xPriv *bip32.ExtendedKey, dst *models.Destination) (err error) {
var key *bec.PrivateKey
func setUnlockingScript(tx *trx.Transaction, inputIndex uint32, xPriv *bip32.ExtendedKey, dst *models.Destination) (err error) {
var key *ec.PrivateKey
if key, err = getDerivedKeyForDestination(xPriv, dst); err != nil {
return
}

var s *bscript.Script
var s *script.Script
if s, err = getUnlockingScript(tx, inputIndex, key); err != nil {
return
}
Expand All @@ -83,10 +87,10 @@
return
}

func getDerivedKeyForDestination(xPriv *bip32.ExtendedKey, dst *models.Destination) (key *bec.PrivateKey, err error) {
func getDerivedKeyForDestination(xPriv *bip32.ExtendedKey, dst *models.Destination) (key *ec.PrivateKey, err error) {
// Derive the child key (m/chain/num)
var derivedKey *bip32.ExtendedKey
if derivedKey, err = bitcoin.GetHDKeyByPath(xPriv, dst.Chain, dst.Num); err != nil {
if derivedKey, err = bip32.GetHDKeyByPath(xPriv, dst.Chain, dst.Num); err != nil {
return
}

Expand All @@ -99,32 +103,32 @@
}
}

if key, err = bitcoin.GetPrivateKeyFromHDKey(derivedKey); err != nil {
if key, err = bip32.GetPrivateKeyFromHDKey(derivedKey); err != nil {
return
}

return
}

// GetUnlockingScript will generate an unlocking script
func getUnlockingScript(tx *bt.Tx, inputIndex uint32, privateKey *bec.PrivateKey) (*bscript.Script, error) {
func getUnlockingScript(tx *trx.Transaction, inputIndex uint32, privateKey *ec.PrivateKey) (*script.Script, error) {
sigHashFlags := sighash.AllForkID

sigHash, err := tx.CalcInputSignatureHash(inputIndex, sigHashFlags)
if err != nil {
return nil, err
}

var sig *bec.Signature
var sig *ec.Signature
if sig, err = privateKey.Sign(sigHash); err != nil {
return nil, err
}

pubKey := privateKey.PubKey().SerialiseCompressed()
signature := sig.Serialise()
pubKey := privateKey.PubKey().SerializeCompressed()
signature := sig.Serialize()

var script *bscript.Script
if script, err = bscript.NewP2PKHUnlockingScript(pubKey, signature, sigHashFlags); err != nil {
var script *script.Script
if script, err = script.NewP2PKHUnlockingScript(pubKey, signature, sigHashFlags); err != nil {

Check failure on line 131 in authentication.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

script.NewP2PKHUnlockingScript undefined (type *script.Script has no field or method NewP2PKHUnlockingScript)
return nil, err
}

Expand All @@ -141,7 +145,7 @@

// Get the xPub
payload = new(models.AuthPayload)
if payload.XPub, err = bitcoin.GetExtendedPublicKey(
if payload.XPub, err = bip32.GetExtendedPublicKey(
xPriv,
); err != nil { // Should never error if key is correct
return
Expand All @@ -161,16 +165,16 @@
return
}

var privateKey *bec.PrivateKey
if privateKey, err = bitcoin.GetPrivateKeyFromHDKey(key); err != nil {
var privateKey *ec.PrivateKey
if privateKey, err = bip32.GetPrivateKeyFromHDKey(key); err != nil {
return // Should never error if key is correct
}

return createSignatureCommon(payload, bodyString, privateKey)
}

// createSignatureCommon will create a signature
func createSignatureCommon(payload *models.AuthPayload, bodyString string, privateKey *bec.PrivateKey) (*models.AuthPayload, error) {
func createSignatureCommon(payload *models.AuthPayload, bodyString string, privateKey *ec.PrivateKey) (*models.AuthPayload, error) {
// Create the auth header hash
payload.AuthHash = utils.Hash(bodyString)

Expand All @@ -184,8 +188,8 @@

// Signature, using bitcoin signMessage
var err error
if payload.Signature, err = bitcoin.SignMessage(
hex.EncodeToString(privateKey.Serialise()),
if payload.Signature, err = bsm.SignMessage(
privateKey,
getSigningMessage(key, payload),
true,
); err != nil {
Expand Down
26 changes: 11 additions & 15 deletions client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"net/http"
"net/url"

"github.com/bitcoinschema/go-bitcoin/v2"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bk/wif"
bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32"
ec "github.com/bitcoin-sv/go-sdk/primitives/ec"

"github.com/pkg/errors"
)

Expand All @@ -23,7 +23,7 @@ type xPrivConf struct {

func (w *xPrivConf) Configure(c *WalletClient) {
var err error
if c.xPriv, err = bitcoin.GenerateHDKeyFromString(w.XPrivString); err != nil {
if c.xPriv, err = bip32.GenerateHDKeyFromString(w.XPrivString); err != nil {
c.xPriv = nil
}
}
Expand All @@ -35,7 +35,7 @@ type xPubConf struct {

func (w *xPubConf) Configure(c *WalletClient) {
var err error
if c.xPub, err = bitcoin.GetHDKeyFromExtendedPublicKey(w.XPubString); err != nil {
if c.xPub, err = bip32.GetHDKeyFromExtendedPublicKey(w.XPubString); err != nil {
c.xPub = nil
}

Expand All @@ -60,7 +60,7 @@ type adminKeyConf struct {

func (w *adminKeyConf) Configure(c *WalletClient) {
var err error
c.adminXPriv, err = bitcoin.GenerateHDKeyFromString(w.AdminKeyString)
c.adminXPriv, err = bip32.GenerateHDKeyFromString(w.AdminKeyString)
if err != nil {
c.adminXPriv = nil
}
Expand Down Expand Up @@ -102,17 +102,13 @@ func (w *signRequest) Configure(c *WalletClient) {
}

// initializeAccessKey handles the specific initialization of the access key.
func (w *accessKeyConf) initializeAccessKey() (*bec.PrivateKey, error) {
func (w *accessKeyConf) initializeAccessKey() (*ec.PrivateKey, error) {
var err error
var privateKey *bec.PrivateKey
var decodedWIF *wif.WIF
var privateKey *ec.PrivateKey

if decodedWIF, err = wif.DecodeWIF(w.AccessKeyString); err != nil {
if privateKey, err = bitcoin.PrivateKeyFromString(w.AccessKeyString); err != nil {
return nil, errors.Wrap(err, "failed to decode access key")
}
} else {
privateKey = decodedWIF.PrivKey
privateKey, err = ec.PrivateKeyFromWif(w.AccessKeyString)
chris-4chain marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, errors.Wrap(err, "failed to decode access key")
}

return privateKey, nil
Expand Down
7 changes: 2 additions & 5 deletions examples/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions examples/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 10 additions & 14 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading