diff --git a/authentication.go b/authentication.go index 7e226e03..1a777196 100644 --- a/authentication.go +++ b/authentication.go @@ -1,19 +1,21 @@ package walletclient import ( - "encoding/hex" + "encoding/base64" "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/go-sdk/transaction/template/p2pkh" + "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" ) // SetSignature will set the signature on the header for the request @@ -33,102 +35,77 @@ func setSignature(header *http.Header, xPriv *bip32.ExtendedKey, bodyString stri } // 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 { - return +func GetSignedHex(dt *models.DraftTransaction, xPriv *bip32.ExtendedKey) (string, error) { + // Create transaction from hex + tx, err := trx.NewTransactionFromHex(dt.Hex) + + // we need to reset the inputs as we are going to add them via tx.AddInputFrom (ts-sdk method) and then sign + tx.Inputs = make([]*trx.TransactionInput, 0) + if err != nil { + return "", err } // Enrich inputs - for index, draftInput := range dt.Configuration.Inputs { - tx.Inputs[index].PreviousTxSatoshis = draftInput.Satoshis - - dst := draftInput.Destination - if err = setPreviousTxScript(tx, uint32(index), &dst); err != nil { - return + for _, draftInput := range dt.Configuration.Inputs { + lockingScript, err := prepareLockingScript(&draftInput.Destination) + if err != nil { + return "", err } - if err = setUnlockingScript(tx, uint32(index), xPriv, &dst); err != nil { - return + unlockScript, err := prepareUnlockingScript(xPriv, &draftInput.Destination) + if err != nil { + return "", err } + + tx.AddInputFrom(draftInput.TransactionID, draftInput.OutputIndex, lockingScript.String(), draftInput.Satoshis, unlockScript) } - // Return the signed hex - signedHex = tx.String() - return + tx.Sign() + + return tx.String(), nil } -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 { - return +func prepareLockingScript(dst *models.Destination) (*script.Script, error) { + lockingScript, err := script.NewFromHex(dst.LockingScript) + if err != nil { + return nil, fmt.Errorf("failed to create locking script from hex for destination: %w", err) } - tx.Inputs[inputIndex].PreviousTxScript = ls - return + return lockingScript, nil } -func setUnlockingScript(tx *bt.Tx, inputIndex uint32, xPriv *bip32.ExtendedKey, dst *models.Destination) (err error) { - var key *bec.PrivateKey - if key, err = getDerivedKeyForDestination(xPriv, dst); err != nil { - return - } - - var s *bscript.Script - if s, err = getUnlockingScript(tx, inputIndex, key); err != nil { - return +func prepareUnlockingScript(xPriv *bip32.ExtendedKey, dst *models.Destination) (*p2pkh.P2PKH, error) { + key, err := getDerivedKeyForDestination(xPriv, dst) + if err != nil { + return nil, err } - tx.Inputs[inputIndex].UnlockingScript = s - return + return getUnlockingScript(key) } -func getDerivedKeyForDestination(xPriv *bip32.ExtendedKey, dst *models.Destination) (key *bec.PrivateKey, err error) { +func getDerivedKeyForDestination(xPriv *bip32.ExtendedKey, dst *models.Destination) (*ec.PrivateKey, error) { // Derive the child key (m/chain/num) - var derivedKey *bip32.ExtendedKey - if derivedKey, err = bitcoin.GetHDKeyByPath(xPriv, dst.Chain, dst.Num); err != nil { - return + derivedKey, err := bip32.GetHDKeyByPath(xPriv, dst.Chain, dst.Num) + if err != nil { + return nil, err } - // Derive key for paymail destination (m/chain/num/paymailNum) + // Handle paymail destination derivation if applicable if dst.PaymailExternalDerivationNum != nil { - if derivedKey, err = derivedKey.Child( - *dst.PaymailExternalDerivationNum, - ); err != nil { - return + derivedKey, err = derivedKey.Child(*dst.PaymailExternalDerivationNum) + if err != nil { + return nil, err } } - if key, err = bitcoin.GetPrivateKeyFromHDKey(derivedKey); err != nil { - return - } - - return + // Get the private key from the derived key + return bip32.GetPrivateKeyFromHDKey(derivedKey) } -// GetUnlockingScript will generate an unlocking script -func getUnlockingScript(tx *bt.Tx, inputIndex uint32, privateKey *bec.PrivateKey) (*bscript.Script, error) { +// Generate unlocking script using private key +func getUnlockingScript(privateKey *ec.PrivateKey) (*p2pkh.P2PKH, error) { sigHashFlags := sighash.AllForkID - - sigHash, err := tx.CalcInputSignatureHash(inputIndex, sigHashFlags) - if err != nil { - return nil, err - } - - var sig *bec.Signature - if sig, err = privateKey.Sign(sigHash); err != nil { - return nil, err - } - - pubKey := privateKey.PubKey().SerialiseCompressed() - signature := sig.Serialise() - - var script *bscript.Script - if script, err = bscript.NewP2PKHUnlockingScript(pubKey, signature, sigHashFlags); err != nil { - return nil, err - } - - return script, nil + return p2pkh.Unlock(privateKey, &sigHashFlags) } // createSignature will create a signature for the given key & body contents @@ -141,7 +118,7 @@ func createSignature(xPriv *bip32.ExtendedKey, bodyString string) (payload *mode // 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 @@ -161,8 +138,8 @@ func createSignature(xPriv *bip32.ExtendedKey, bodyString string) (payload *mode 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 } @@ -170,7 +147,7 @@ func createSignature(xPriv *bip32.ExtendedKey, bodyString string) (payload *mode } // 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) @@ -183,21 +160,23 @@ func createSignatureCommon(payload *models.AuthPayload, bodyString string, priva } // Signature, using bitcoin signMessage - var err error - if payload.Signature, err = bitcoin.SignMessage( - hex.EncodeToString(privateKey.Serialise()), + sigBytes, err := bsm.SignMessage( + privateKey, getSigningMessage(key, payload), - true, - ); err != nil { + ) + if err != nil { return nil, err } + payload.Signature = base64.StdEncoding.EncodeToString(sigBytes) + return payload, nil } -// getSigningMessage will build the signing message string -func getSigningMessage(xPub string, auth *models.AuthPayload) string { - return fmt.Sprintf("%s%s%s%d", xPub, auth.AuthHash, auth.AuthNonce, auth.AuthTime) +// getSigningMessage will build the signing message byte array +func getSigningMessage(xPub string, auth *models.AuthPayload) []byte { + message := fmt.Sprintf("%s%s%s%d", xPub, auth.AuthHash, auth.AuthNonce, auth.AuthTime) + return []byte(message) } func setSignatureHeaders(header *http.Header, authData *models.AuthPayload) { diff --git a/client_options.go b/client_options.go index a5d06fa9..fcee3385 100644 --- a/client_options.go +++ b/client_options.go @@ -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" ) @@ -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 } } @@ -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 } @@ -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 } @@ -101,18 +101,13 @@ func (w *signRequest) Configure(c *WalletClient) { c.signRequest = w.Sign } -// initializeAccessKey handles the specific initialization of the access key. -func (w *accessKeyConf) initializeAccessKey() (*bec.PrivateKey, error) { - var err error - var privateKey *bec.PrivateKey - var decodedWIF *wif.WIF - - 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") +func (w *accessKeyConf) initializeAccessKey() (*ec.PrivateKey, error) { + privateKey, err := ec.PrivateKeyFromWif(w.AccessKeyString) + if err != nil { + privateKey, _ = ec.PrivateKeyFromHex(w.AccessKeyString) + if privateKey == nil { + return nil, errors.New("failed to decode access key") } - } else { - privateKey = decodedWIF.PrivKey } return privateKey, nil diff --git a/examples/go.mod b/examples/go.mod index 1676b643..472de8fb 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -10,12 +10,9 @@ require ( ) require ( - github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 // indirect - github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 // indirect + github.com/bitcoin-sv/go-sdk v1.1.7 // indirect github.com/boombuler/barcode v1.0.2 // indirect - github.com/libsv/go-bk v0.1.6 // indirect - github.com/libsv/go-bt/v2 v2.2.5 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pquerna/otp v1.4.0 // indirect - golang.org/x/crypto v0.25.0 // indirect + golang.org/x/crypto v0.26.0 // indirect ) diff --git a/examples/go.sum b/examples/go.sum index f6d8a655..db4d1ba5 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -1,19 +1,13 @@ +github.com/bitcoin-sv/go-sdk v1.1.7 h1:JbtaYCGUsHM7HTelLKSJp1NuYiG/xSNOdexjJ6w577A= +github.com/bitcoin-sv/go-sdk v1.1.7/go.mod h1:NOAkJLbjqKOLuxJmb9ABG86ExTZp4HS8+iygiDIUps4= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.25 h1:OygyRn44GhQJQGhvp2vqTjXQRABXK+EA5ZvL6WE84nU= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.25/go.mod h1:PEJdH9ZWKOiKHyOZkzYsRbKuZjzlRaEJy3GsM75Icdo= -github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 h1:Sgh5Eb746Zck/46rFDrZZEXZWyO53fMuWYhNoZa1tck= -github.com/bitcoinschema/go-bitcoin/v2 v2.0.5/go.mod h1:JjO1ivfZv6vhK0uAXzyH08AAHlzNMAfnyK1Fiv9r4ZA= -github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 h1:2yTIV9u7H0BhRDGXH5xrAwAz7XibWJtX2dNezMeNsUo= -github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173/go.mod h1:BZ1UcC9+tmcDEcdVXgpt13hMczwJxWzpAn68wNs7zRA= github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/boombuler/barcode v1.0.2 h1:79yrbttoZrLGkL/oOI8hBrUKucwOL0oOjUgEguGMcJ4= github.com/boombuler/barcode v1.0.2/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/libsv/go-bk v0.1.6 h1:c9CiT5+64HRDbzxPl1v/oiFmbvWZTuUYqywCf+MBs/c= -github.com/libsv/go-bk v0.1.6/go.mod h1:khJboDoH18FPUaZlzRFKzlVN84d4YfdmlDtdX4LAjQA= -github.com/libsv/go-bt/v2 v2.2.5 h1:VoggBLMRW9NYoFujqe5bSYKqnw5y+fYfufgERSoubog= -github.com/libsv/go-bt/v2 v2.2.5/go.mod h1:cV45+jDlPOLfhJLfpLmpQoWzrIvVth9Ao2ZO1f6CcqU= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -24,7 +18,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= -golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= +golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= +golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/handle_exceptions/handle_exceptions.go b/examples/handle_exceptions/handle_exceptions.go index 7bb7b31b..39558fea 100644 --- a/examples/handle_exceptions/handle_exceptions.go +++ b/examples/handle_exceptions/handle_exceptions.go @@ -23,7 +23,7 @@ func main() { const server = "http://localhost:3003/v1" - client := walletclient.NewWithXPub(server, examples.ExampleXPub) + client := walletclient.NewWithXPub(server, examples.ExampleAdminKey) ctx := context.Background() fmt.Println("Client created") diff --git a/go.mod b/go.mod index c7bd8e32..755bb33c 100644 --- a/go.mod +++ b/go.mod @@ -3,20 +3,20 @@ module github.com/bitcoin-sv/spv-wallet-go-client go 1.22.5 require ( + github.com/bitcoin-sv/go-sdk v1.1.7 github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.25 - github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 - github.com/libsv/go-bk v0.1.6 - github.com/libsv/go-bt/v2 v2.2.5 github.com/pkg/errors v0.9.1 github.com/pquerna/otp v1.4.0 github.com/stretchr/testify v1.9.0 ) require ( - github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 // indirect github.com/boombuler/barcode v1.0.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/kr/pretty v0.3.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/crypto v0.25.0 // indirect + github.com/rogpeppe/go-internal v1.11.0 // indirect + golang.org/x/crypto v0.26.0 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 5758c51e..5d541015 100644 --- a/go.sum +++ b/go.sum @@ -1,37 +1,37 @@ +github.com/bitcoin-sv/go-sdk v1.1.7 h1:JbtaYCGUsHM7HTelLKSJp1NuYiG/xSNOdexjJ6w577A= +github.com/bitcoin-sv/go-sdk v1.1.7/go.mod h1:NOAkJLbjqKOLuxJmb9ABG86ExTZp4HS8+iygiDIUps4= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.25 h1:OygyRn44GhQJQGhvp2vqTjXQRABXK+EA5ZvL6WE84nU= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.25/go.mod h1:PEJdH9ZWKOiKHyOZkzYsRbKuZjzlRaEJy3GsM75Icdo= -github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 h1:Sgh5Eb746Zck/46rFDrZZEXZWyO53fMuWYhNoZa1tck= -github.com/bitcoinschema/go-bitcoin/v2 v2.0.5/go.mod h1:JjO1ivfZv6vhK0uAXzyH08AAHlzNMAfnyK1Fiv9r4ZA= -github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 h1:2yTIV9u7H0BhRDGXH5xrAwAz7XibWJtX2dNezMeNsUo= -github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173/go.mod h1:BZ1UcC9+tmcDEcdVXgpt13hMczwJxWzpAn68wNs7zRA= github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/boombuler/barcode v1.0.2 h1:79yrbttoZrLGkL/oOI8hBrUKucwOL0oOjUgEguGMcJ4= github.com/boombuler/barcode v1.0.2/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/libsv/go-bk v0.1.6 h1:c9CiT5+64HRDbzxPl1v/oiFmbvWZTuUYqywCf+MBs/c= -github.com/libsv/go-bk v0.1.6/go.mod h1:khJboDoH18FPUaZlzRFKzlVN84d4YfdmlDtdX4LAjQA= -github.com/libsv/go-bt/v2 v2.2.5 h1:VoggBLMRW9NYoFujqe5bSYKqnw5y+fYfufgERSoubog= -github.com/libsv/go-bt/v2 v2.2.5/go.mod h1:cV45+jDlPOLfhJLfpLmpQoWzrIvVth9Ao2ZO1f6CcqU= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pquerna/otp v1.4.0 h1:wZvl1TIVxKRThZIBiwOOHOGP/1+nZyWBil9Y2XNEDzg= github.com/pquerna/otp v1.4.0/go.mod h1:dkJfzwRKNiegxyNb54X/3fLwhCynbMspSyWKnvi1AEg= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= -golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= +golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= +golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/http.go b/http.go index 0999c59a..a8960264 100644 --- a/http.go +++ b/http.go @@ -9,12 +9,11 @@ import ( "net/http" "strconv" + bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" + ec "github.com/bitcoin-sv/go-sdk/primitives/ec" "github.com/bitcoin-sv/spv-wallet-go-client/utils" "github.com/bitcoin-sv/spv-wallet/models" "github.com/bitcoin-sv/spv-wallet/models/filter" - "github.com/bitcoinschema/go-bitcoin/v2" - "github.com/libsv/go-bk/bec" - "github.com/libsv/go-bk/bip32" ) // SetSignRequest turn the signing of the http request on or off @@ -478,8 +477,8 @@ func createSignatureAccessKey(privateKeyHex, bodyString string) (payload *models return } - var privateKey *bec.PrivateKey - if privateKey, err = bitcoin.PrivateKeyFromString( + var privateKey *ec.PrivateKey + if privateKey, err = ec.PrivateKeyFromHex( privateKeyHex, ); err != nil { return @@ -488,7 +487,7 @@ func createSignatureAccessKey(privateKeyHex, bodyString string) (payload *models // Get the AccessKey payload = new(models.AuthPayload) - payload.AccessKey = hex.EncodeToString(publicKey.SerialiseCompressed()) + payload.AccessKey = hex.EncodeToString(publicKey.SerializeCompressed()) // auth_nonce is a random unique string to seed the signing message // this can be checked server side to make sure the request is not being replayed @@ -553,7 +552,7 @@ func (wc *WalletClient) authenticateWithXpriv(sign bool, req *http.Request, xPri } } else { var xPub string - xPub, err := bitcoin.GetExtendedPublicKey(xPriv) + xPub, err := bip32.GetExtendedPublicKey(xPriv) if err != nil { return WrapError(err) } @@ -567,7 +566,7 @@ func (wc *WalletClient) authenticateWithAccessKey(req *http.Request, rawJSON []b if wc.accessKey == nil { return ErrMissingAccessKey } - return SetSignatureFromAccessKey(&req.Header, hex.EncodeToString(wc.accessKey.Serialise()), string(rawJSON)) + return SetSignatureFromAccessKey(&req.Header, hex.EncodeToString(wc.accessKey.Serialize()), string(rawJSON)) } // AcceptContact will accept the contact associated with the paymail diff --git a/search.go b/search.go index e2f93623..aa23dcb0 100644 --- a/search.go +++ b/search.go @@ -3,8 +3,9 @@ package walletclient import ( "context" "encoding/json" + + bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" "github.com/bitcoin-sv/spv-wallet/models/filter" - "github.com/libsv/go-bk/bip32" ) // SearchRequester is a function that sends a request to the server and returns the response. diff --git a/totp.go b/totp.go index e202e964..babeb08a 100644 --- a/totp.go +++ b/totp.go @@ -5,11 +5,10 @@ import ( "encoding/hex" "time" + bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" + ec "github.com/bitcoin-sv/go-sdk/primitives/ec" "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/pquerna/otp" "github.com/pquerna/otp/totp" ) @@ -58,7 +57,7 @@ func makeSharedSecret(b *WalletClient, c *models.Contact) ([]byte, error) { return nil, err } - x, _ := bec.S256().ScalarMult(pubKey.X, pubKey.Y, privKey.D.Bytes()) + x, _ := ec.S256().ScalarMult(pubKey.X, pubKey.Y, privKey.D.Bytes()) return x.Bytes(), nil } @@ -77,7 +76,7 @@ func getTotpOpts(period, digits uint) *totp.ValidateOpts { } } -func getSharedSecretFactors(b *WalletClient, c *models.Contact) (*bec.PrivateKey, *bec.PublicKey, error) { +func getSharedSecretFactors(b *WalletClient, c *models.Contact) (*ec.PrivateKey, *ec.PublicKey, error) { if b.xPriv == nil { return nil, nil, ErrMissingXpriv } @@ -104,7 +103,7 @@ func deriveXprivForPki(xpriv *bip32.ExtendedKey) (*bip32.ExtendedKey, error) { // PKI derivation path: m/0/0/0 // NOTICE: we currently do not support PKI rotation; however, adjustments will be made if and when we decide to implement it - pkiXpriv, err := bitcoin.GetHDKeyByPath(xpriv, utils.ChainExternal, 0) + pkiXpriv, err := bip32.GetHDKeyByPath(xpriv, utils.ChainExternal, 0) if err != nil { return nil, err } @@ -112,13 +111,13 @@ func deriveXprivForPki(xpriv *bip32.ExtendedKey) (*bip32.ExtendedKey, error) { return pkiXpriv.Child(0) } -func convertPubKey(pubKey string) (*bec.PublicKey, error) { +func convertPubKey(pubKey string) (*ec.PublicKey, error) { hex, err := hex.DecodeString(pubKey) if err != nil { return nil, err } - return bec.ParsePubKey(hex, bec.S256()) + return ec.ParsePubKey(hex) } // directedSecret appends a paymail to the secret and encodes it into base32 string diff --git a/totp_test.go b/totp_test.go index 5ad10600..a35de430 100644 --- a/totp_test.go +++ b/totp_test.go @@ -7,10 +7,10 @@ import ( "net/http/httptest" "testing" + bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" "github.com/bitcoin-sv/spv-wallet-go-client/fixtures" "github.com/bitcoin-sv/spv-wallet-go-client/xpriv" "github.com/bitcoin-sv/spv-wallet/models" - "github.com/libsv/go-bk/bip32" "github.com/stretchr/testify/require" ) @@ -128,5 +128,5 @@ func makeMockPKI(xpub string) string { panic(err) } - return hex.EncodeToString(pubKey.SerialiseCompressed()) + return hex.EncodeToString(pubKey.SerializeCompressed()) } diff --git a/utils/utils.go b/utils/utils.go index e322ec73..84649d50 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -8,7 +8,7 @@ import ( "math" "strconv" - "github.com/libsv/go-bk/bip32" + bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" ) const ( diff --git a/walletclient.go b/walletclient.go index aae8d23f..f6bff95c 100644 --- a/walletclient.go +++ b/walletclient.go @@ -3,8 +3,8 @@ package walletclient import ( "net/http" - "github.com/libsv/go-bk/bec" - "github.com/libsv/go-bk/bip32" + bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" + ec "github.com/bitcoin-sv/go-sdk/primitives/ec" ) // WalletClient is the spv wallet Go client representation. @@ -12,7 +12,7 @@ type WalletClient struct { signRequest bool server string httpClient *http.Client - accessKey *bec.PrivateKey + accessKey *ec.PrivateKey adminXPriv *bip32.ExtendedKey xPriv *bip32.ExtendedKey xPub *bip32.ExtendedKey diff --git a/xpriv/xpriv.go b/xpriv/xpriv.go index 549f5916..f87ca3d3 100644 --- a/xpriv/xpriv.go +++ b/xpriv/xpriv.go @@ -1,12 +1,14 @@ // Package xpriv manges keys package xpriv +// "github.com/libsv/go-bk/bip39" - no replacements + import ( "fmt" - "github.com/libsv/go-bk/bip32" - "github.com/libsv/go-bk/bip39" - "github.com/libsv/go-bk/chaincfg" + bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" + bip39 "github.com/bitcoin-sv/go-sdk/compat/bip39" + chaincfg "github.com/bitcoin-sv/go-sdk/transaction/chaincfg" ) // Keys is a struct containing the xpriv, xpub and mnemonic @@ -58,25 +60,31 @@ func (k PublicKey) String() string { // Generate generates a random set of keys - xpriv, xpb and mnemonic func Generate() (KeyWithMnemonic, error) { - entropy, err := bip39.GenerateEntropy(160) + entropy, err := bip39.NewEntropy(160) if err != nil { return nil, fmt.Errorf("generate method: key generation error when creating entropy: %w", err) } - mnemonic, seed, err := bip39.Mnemonic(entropy, "") + mnemonic, err := bip39.NewMnemonic(entropy) if err != nil { return nil, fmt.Errorf("generate method: key generation error when creating mnemonic: %w", err) } - hdXpriv, hdXpub, err := createXPrivAndXPub(seed) + hdKey, err := bip32.GenerateHDKeyFromMnemonic(mnemonic, "", &chaincfg.MainNet) + if err != nil { + return nil, err + } + + hdXpriv := hdKey.String() + hdXpub, err := bip32.GetExtendedPublicKey(hdKey) if err != nil { return nil, err } keys := &Keys{ - xpriv: hdXpriv.String(), - xpub: PublicKey(hdXpub.String()), + xpriv: hdXpriv, + xpub: PublicKey(hdXpub), mnemonic: mnemonic, } @@ -85,7 +93,7 @@ func Generate() (KeyWithMnemonic, error) { // FromMnemonic generates Keys based on given mnemonic func FromMnemonic(mnemonic string) (KeyWithMnemonic, error) { - seed, err := bip39.MnemonicToSeed(mnemonic, "") + seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "") if err != nil { return nil, fmt.Errorf("FromMnemonic method: error when creating seed: %w", err) }