From b9fea948441bf683cd072b94e4a193c66174f7e9 Mon Sep 17 00:00:00 2001 From: yarex-4chain Date: Fri, 19 Jul 2024 10:01:37 +0200 Subject: [PATCH 01/13] feat/SPV-936: temp commit --- authentication.go | 16 ++++++--- client_options.go | 9 +++-- go.mod | 28 +-------------- go.sum | 89 ++--------------------------------------------- http.go | 6 ++-- totp.go | 8 +++-- totp_test.go | 2 +- utils/utils.go | 2 +- walletclient.go | 4 +-- xpriv/xpriv.go | 9 +++-- 10 files changed, 42 insertions(+), 131 deletions(-) diff --git a/authentication.go b/authentication.go index 7e226e0..6053990 100644 --- a/authentication.go +++ b/authentication.go @@ -6,16 +6,24 @@ import ( "net/http" "time" + bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" + bec "github.com/bitcoin-sv/go-sdk/primitives/ec" + bscript "github.com/bitcoin-sv/go-sdk/script" + 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: Issues with "github.com/bitcoinschema/go-bitcoin/v2" + +// TODO: Tx - go-sdk txJSON ? +// TODO: NewTxFromString - no replacement in v2 +// TODO: NewFromHexString - no replacement in v2 +// TODO: NewP2PKHUnlockingScript - not found + // 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 diff --git a/client_options.go b/client_options.go index a5d06fa..f9fa470 100644 --- a/client_options.go +++ b/client_options.go @@ -5,12 +5,17 @@ import ( "net/http" "net/url" - "github.com/bitcoinschema/go-bitcoin/v2" - "github.com/libsv/go-bk/bec" + bec "github.com/bitcoin-sv/go-sdk/primitives/ec" "github.com/libsv/go-bk/wif" + + "github.com/bitcoinschema/go-bitcoin/v2" + "github.com/pkg/errors" ) +// TODO: Issues with "github.com/bitcoinschema/go-bitcoin/v2" +// TODO: wif "github.com/bitcoin-sv/go-sdk/compat/wif" - is not found although used in the go-sdk repo? + // configurator is the interface for configuring WalletClient type configurator interface { Configure(c *WalletClient) diff --git a/go.mod b/go.mod index b7ae30a..8384e92 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/bitcoin-sv/spv-wallet-go-client go 1.22.4 require ( + github.com/bitcoin-sv/go-sdk v0.0.0-20240709134414-8719c587e9dd github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.16 github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 github.com/libsv/go-bk v0.1.6 @@ -15,35 +16,8 @@ require ( require ( github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 // indirect github.com/boombuler/barcode v1.0.1 // indirect - github.com/bytedance/sonic v1.11.6 // indirect - github.com/bytedance/sonic/loader v0.1.1 // indirect - github.com/cloudwego/base64x v0.1.4 // indirect - github.com/cloudwego/iasm v0.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/gabriel-vasile/mimetype v1.4.3 // indirect - github.com/gin-contrib/sse v0.1.0 // indirect - github.com/gin-gonic/gin v1.10.0 // indirect - github.com/go-playground/locales v0.14.1 // indirect - github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.20.0 // indirect - github.com/goccy/go-json v0.10.2 // indirect - github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/cpuid/v2 v2.2.7 // indirect - github.com/leodido/go-urn v1.4.0 // indirect - github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/rs/zerolog v1.33.0 // indirect - github.com/twitchyliquid64/golang-asm v0.15.1 // indirect - github.com/ugorji/go/codec v1.2.12 // indirect - golang.org/x/arch v0.8.0 // indirect golang.org/x/crypto v0.23.0 // indirect - golang.org/x/net v0.25.0 // indirect - golang.org/x/sys v0.20.0 // indirect - golang.org/x/text v0.15.0 // indirect - google.golang.org/protobuf v1.34.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index cfe05a0..3b49daf 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,5 @@ -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.13 h1:rBscs3Gbz0RWY03eI3Z9AwD7/MxajdJF54oy3xMqKRQ= -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.13/go.mod h1:i3txysriHpprqYd3u97wEQsC4/jn+KHcyFOmuFYMw8M= -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.14.0.20240626082725-2c073c5330a6 h1:ZTEHuSNbXszs+5TKN0uiW6DY7JdWIM5m6NQpBJZyre4= -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.14.0.20240626082725-2c073c5330a6/go.mod h1:u3gnRDS3uHWZNM2qbYATTpN+mAphyozCJrYIKGwBX7k= -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.15 h1:Qjp9gSe1XlBwADgDlkaIGuzqNoQwktu1DuB6tzurdQI= -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.15/go.mod h1:Ni6SFkmMjV39Bg4FtlgPAsnsiJUfRDVEPlbzTZa8z40= -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.15.0.20240704130751-f3156fd52a0e h1:Bw8bq7YUvMSNwRNQUm8gFKakICyNk8ScBqhJ9LFr54o= -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.15.0.20240704130751-f3156fd52a0e/go.mod h1:Ni6SFkmMjV39Bg4FtlgPAsnsiJUfRDVEPlbzTZa8z40= +github.com/bitcoin-sv/go-sdk v0.0.0-20240709134414-8719c587e9dd h1:juRDF9giUvWorD83ZyPSLAoaRjduha7Vkc1LinM+uiY= +github.com/bitcoin-sv/go-sdk v0.0.0-20240709134414-8719c587e9dd/go.mod h1:NOAkJLbjqKOLuxJmb9ABG86ExTZp4HS8+iygiDIUps4= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.16 h1:iHLUofGb40sQ31KpVwtdjuKVs3W/vW1w8exF8Vidvfc= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.16/go.mod h1:Ni6SFkmMjV39Bg4FtlgPAsnsiJUfRDVEPlbzTZa8z40= github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 h1:Sgh5Eb746Zck/46rFDrZZEXZWyO53fMuWYhNoZa1tck= @@ -15,63 +9,17 @@ github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173/go.mod h1:BZ1UcC9+t github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/boombuler/barcode v1.0.1 h1:NDBbPmhS+EqABEs5Kg3n/5ZNjy73Pz7SIV+KCeqyXcs= github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= -github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= -github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= -github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= -github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= -github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= -github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= -github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= -github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= -github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= 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/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= -github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= -github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= -github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= -github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= -github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= -github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= -github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= -github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= -github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= -github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= -github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= -github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= -github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= -github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= -github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= -github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= -github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= 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/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= -github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= -github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= -github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= -github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= -github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= -github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= 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= @@ -80,47 +28,14 @@ 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.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= -github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= -github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= -github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 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= -github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= -github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= -github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= -github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= -golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= -golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= -golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= -golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= -golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= -golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= -google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= 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= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= -rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/http.go b/http.go index 8bf6144..69e8efa 100644 --- a/http.go +++ b/http.go @@ -10,14 +10,16 @@ import ( "net/http" "strconv" + bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" + bec "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" ) +// TODO: Issues with "github.com/bitcoinschema/go-bitcoin/v2" + // SetSignRequest turn the signing of the http request on or off func (wc *WalletClient) SetSignRequest(signRequest bool) { wc.signRequest = signRequest diff --git a/totp.go b/totp.go index 336f6d6..ce4b88d 100644 --- a/totp.go +++ b/totp.go @@ -7,15 +7,17 @@ import ( "fmt" "time" + bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" + bec "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" ) +// TODO: Issues with "github.com/bitcoinschema/go-bitcoin/v2" + // ErrClientInitNoXpriv error per init client with first xpriv key var ErrClientInitNoXpriv = errors.New("init client with xPriv first") @@ -123,7 +125,7 @@ func convertPubKey(pubKey string) (*bec.PublicKey, error) { return nil, err } - return bec.ParsePubKey(hex, bec.S256()) + return bec.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 e6be2e4..3cef2d0 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" ) diff --git a/utils/utils.go b/utils/utils.go index e322ec7..84649d5 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 aae8d23..1508935 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" + bec "github.com/bitcoin-sv/go-sdk/primitives/ec" ) // WalletClient is the spv wallet Go client representation. diff --git a/xpriv/xpriv.go b/xpriv/xpriv.go index 549f591..511e597 100644 --- a/xpriv/xpriv.go +++ b/xpriv/xpriv.go @@ -1,14 +1,19 @@ // Package xpriv manges keys package xpriv +// "github.com/libsv/go-bk/bip39" - no replacements + import ( "fmt" - "github.com/libsv/go-bk/bip32" + bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" + chaincfg "github.com/bitcoin-sv/go-sdk/transaction/chaincfg" + "github.com/libsv/go-bk/bip39" - "github.com/libsv/go-bk/chaincfg" ) +// TODO: "github.com/libsv/go-bk/bip39" - no replacement (GenerateEntropy, Mnemonic, MnemonicToSeed) + // Keys is a struct containing the xpriv, xpub and mnemonic type Keys struct { xpriv string From 824dc575c63235ca4710e0b2b735550b682d41ab Mon Sep 17 00:00:00 2001 From: yarex-4chain Date: Thu, 25 Jul 2024 02:47:20 +0200 Subject: [PATCH 02/13] feat/SPV-936: temp commit 2 --- authentication.go | 51 ++++++++++++++++++++++------------------------- client_options.go | 18 ++++++++--------- http.go | 11 ++++------ search.go | 3 ++- totp.go | 15 ++++++-------- walletclient.go | 4 ++-- 6 files changed, 46 insertions(+), 56 deletions(-) diff --git a/authentication.go b/authentication.go index 6053990..44215f6 100644 --- a/authentication.go +++ b/authentication.go @@ -7,19 +7,16 @@ import ( "time" bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" - bec "github.com/bitcoin-sv/go-sdk/primitives/ec" - bscript "github.com/bitcoin-sv/go-sdk/script" + 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-bt/v2" ) -// TODO: Issues with "github.com/bitcoinschema/go-bitcoin/v2" - -// TODO: Tx - go-sdk txJSON ? // TODO: NewTxFromString - no replacement in v2 // TODO: NewFromHexString - no replacement in v2 // TODO: NewP2PKHUnlockingScript - not found @@ -42,8 +39,8 @@ 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 { + var tx *trx.Transaction + if tx, err = trx.NewTransactionFromHex(dt.Hex); err != nil { return } @@ -66,9 +63,9 @@ func GetSignedHex(dt *models.DraftTransaction, xPriv *bip32.ExtendedKey) (signed 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 { return } @@ -76,13 +73,13 @@ func setPreviousTxScript(tx *bt.Tx, inputIndex uint32, dst *models.Destination) 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 } @@ -91,10 +88,10 @@ func setUnlockingScript(tx *bt.Tx, inputIndex uint32, xPriv *bip32.ExtendedKey, 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 } @@ -107,7 +104,7 @@ func getDerivedKeyForDestination(xPriv *bip32.ExtendedKey, dst *models.Destinati } } - if key, err = bitcoin.GetPrivateKeyFromHDKey(derivedKey); err != nil { + if key, err = bip32.GetPrivateKeyFromHDKey(derivedKey); err != nil { return } @@ -115,7 +112,7 @@ func getDerivedKeyForDestination(xPriv *bip32.ExtendedKey, dst *models.Destinati } // 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) @@ -123,7 +120,7 @@ func getUnlockingScript(tx *bt.Tx, inputIndex uint32, privateKey *bec.PrivateKey return nil, err } - var sig *bec.Signature + var sig *ec.Signature if sig, err = privateKey.Sign(sigHash); err != nil { return nil, err } @@ -131,8 +128,8 @@ func getUnlockingScript(tx *bt.Tx, inputIndex uint32, privateKey *bec.PrivateKey pubKey := privateKey.PubKey().SerialiseCompressed() signature := sig.Serialise() - 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 { return nil, err } @@ -149,7 +146,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 @@ -169,8 +166,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 } @@ -178,7 +175,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) @@ -192,7 +189,7 @@ func createSignatureCommon(payload *models.AuthPayload, bodyString string, priva // Signature, using bitcoin signMessage var err error - if payload.Signature, err = bitcoin.SignMessage( + if payload.Signature, err = bsm.SignMessage( hex.EncodeToString(privateKey.Serialise()), getSigningMessage(key, payload), true, diff --git a/client_options.go b/client_options.go index f9fa470..405e43d 100644 --- a/client_options.go +++ b/client_options.go @@ -5,15 +5,13 @@ import ( "net/http" "net/url" - bec "github.com/bitcoin-sv/go-sdk/primitives/ec" + bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" + ec "github.com/bitcoin-sv/go-sdk/primitives/ec" "github.com/libsv/go-bk/wif" - "github.com/bitcoinschema/go-bitcoin/v2" - "github.com/pkg/errors" ) -// TODO: Issues with "github.com/bitcoinschema/go-bitcoin/v2" // TODO: wif "github.com/bitcoin-sv/go-sdk/compat/wif" - is not found although used in the go-sdk repo? // configurator is the interface for configuring WalletClient @@ -28,7 +26,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 } } @@ -40,7 +38,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 } @@ -65,7 +63,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 } @@ -107,13 +105,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 privateKey *ec.PrivateKey var decodedWIF *wif.WIF if decodedWIF, err = wif.DecodeWIF(w.AccessKeyString); err != nil { - if privateKey, err = bitcoin.PrivateKeyFromString(w.AccessKeyString); err != nil { + if privateKey, err = ec.PrivateKeyFromHex(w.AccessKeyString); err != nil { return nil, errors.Wrap(err, "failed to decode access key") } } else { diff --git a/http.go b/http.go index 69e8efa..2be7545 100644 --- a/http.go +++ b/http.go @@ -11,15 +11,12 @@ import ( "strconv" bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" - bec "github.com/bitcoin-sv/go-sdk/primitives/ec" + 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" ) -// TODO: Issues with "github.com/bitcoinschema/go-bitcoin/v2" - // SetSignRequest turn the signing of the http request on or off func (wc *WalletClient) SetSignRequest(signRequest bool) { wc.signRequest = signRequest @@ -481,8 +478,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 @@ -556,7 +553,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) } diff --git a/search.go b/search.go index e2f9362..aa23dcb 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 ce4b88d..2a47d36 100644 --- a/totp.go +++ b/totp.go @@ -8,16 +8,13 @@ import ( "time" bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" - bec "github.com/bitcoin-sv/go-sdk/primitives/ec" + 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/pquerna/otp" "github.com/pquerna/otp/totp" ) -// TODO: Issues with "github.com/bitcoinschema/go-bitcoin/v2" - // ErrClientInitNoXpriv error per init client with first xpriv key var ErrClientInitNoXpriv = errors.New("init client with xPriv first") @@ -65,7 +62,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 } @@ -84,7 +81,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, ErrClientInitNoXpriv } @@ -111,7 +108,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 } @@ -119,13 +116,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) + return ec.ParsePubKey(hex) } // directedSecret appends a paymail to the secret and encodes it into base32 string diff --git a/walletclient.go b/walletclient.go index 1508935..f6bff95 100644 --- a/walletclient.go +++ b/walletclient.go @@ -4,7 +4,7 @@ import ( "net/http" bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" - bec "github.com/bitcoin-sv/go-sdk/primitives/ec" + 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 From f0f45b557831464dc3cdd20abac21dfa5843b669 Mon Sep 17 00:00:00 2001 From: yarex-4chain Date: Thu, 25 Jul 2024 08:35:20 +0200 Subject: [PATCH 03/13] feat/SPV-936: temp commit 3 --- authentication.go | 5 ++--- client_options.go | 13 +++---------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/authentication.go b/authentication.go index 44215f6..f3456f3 100644 --- a/authentication.go +++ b/authentication.go @@ -1,7 +1,6 @@ package walletclient import ( - "encoding/hex" "fmt" "net/http" "time" @@ -17,7 +16,7 @@ import ( "github.com/bitcoin-sv/spv-wallet/models" ) -// TODO: NewTxFromString - no replacement in v2 +// TODO: NewTxFromString - NewTransactionFromHex ? // TODO: NewFromHexString - no replacement in v2 // TODO: NewP2PKHUnlockingScript - not found @@ -190,7 +189,7 @@ func createSignatureCommon(payload *models.AuthPayload, bodyString string, priva // Signature, using bitcoin signMessage var err error if payload.Signature, err = bsm.SignMessage( - hex.EncodeToString(privateKey.Serialise()), + privateKey, getSigningMessage(key, payload), true, ); err != nil { diff --git a/client_options.go b/client_options.go index 405e43d..8f9c5d0 100644 --- a/client_options.go +++ b/client_options.go @@ -7,13 +7,10 @@ import ( bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32" ec "github.com/bitcoin-sv/go-sdk/primitives/ec" - "github.com/libsv/go-bk/wif" "github.com/pkg/errors" ) -// TODO: wif "github.com/bitcoin-sv/go-sdk/compat/wif" - is not found although used in the go-sdk repo? - // configurator is the interface for configuring WalletClient type configurator interface { Configure(c *WalletClient) @@ -108,14 +105,10 @@ func (w *signRequest) Configure(c *WalletClient) { func (w *accessKeyConf) initializeAccessKey() (*ec.PrivateKey, error) { var err error var privateKey *ec.PrivateKey - var decodedWIF *wif.WIF - if decodedWIF, err = wif.DecodeWIF(w.AccessKeyString); err != nil { - if privateKey, err = ec.PrivateKeyFromHex(w.AccessKeyString); err != nil { - return nil, errors.Wrap(err, "failed to decode access key") - } - } else { - privateKey = decodedWIF.PrivKey + privateKey, err = ec.PrivateKeyFromWif(w.AccessKeyString) + if err != nil { + return nil, errors.Wrap(err, "failed to decode access key") } return privateKey, nil From 338904e4c986b0eb4c8d922187a7728055885694 Mon Sep 17 00:00:00 2001 From: yarex-4chain Date: Thu, 25 Jul 2024 09:58:14 +0200 Subject: [PATCH 04/13] feat/SPV-936: bip39 from go-sdk --- go.mod | 2 +- go.sum | 2 ++ xpriv/xpriv.go | 23 +++++++++++++---------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 8384e92..ad7b037 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/bitcoin-sv/spv-wallet-go-client go 1.22.4 require ( - github.com/bitcoin-sv/go-sdk v0.0.0-20240709134414-8719c587e9dd + github.com/bitcoin-sv/go-sdk v0.0.0-20240723152403-6fa5c8714bb9 github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.16 github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 github.com/libsv/go-bk v0.1.6 diff --git a/go.sum b/go.sum index 3b49daf..6377f2f 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/bitcoin-sv/go-sdk v0.0.0-20240709134414-8719c587e9dd h1:juRDF9giUvWorD83ZyPSLAoaRjduha7Vkc1LinM+uiY= github.com/bitcoin-sv/go-sdk v0.0.0-20240709134414-8719c587e9dd/go.mod h1:NOAkJLbjqKOLuxJmb9ABG86ExTZp4HS8+iygiDIUps4= +github.com/bitcoin-sv/go-sdk v0.0.0-20240723152403-6fa5c8714bb9 h1:0FzZum72lt0J+u936VdT+pUVZQ4kLdnnVFEhOMO1+Rk= +github.com/bitcoin-sv/go-sdk v0.0.0-20240723152403-6fa5c8714bb9/go.mod h1:NOAkJLbjqKOLuxJmb9ABG86ExTZp4HS8+iygiDIUps4= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.16 h1:iHLUofGb40sQ31KpVwtdjuKVs3W/vW1w8exF8Vidvfc= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.16/go.mod h1:Ni6SFkmMjV39Bg4FtlgPAsnsiJUfRDVEPlbzTZa8z40= github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 h1:Sgh5Eb746Zck/46rFDrZZEXZWyO53fMuWYhNoZa1tck= diff --git a/xpriv/xpriv.go b/xpriv/xpriv.go index 511e597..f87ca3d 100644 --- a/xpriv/xpriv.go +++ b/xpriv/xpriv.go @@ -7,13 +7,10 @@ import ( "fmt" 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" - - "github.com/libsv/go-bk/bip39" ) -// TODO: "github.com/libsv/go-bk/bip39" - no replacement (GenerateEntropy, Mnemonic, MnemonicToSeed) - // Keys is a struct containing the xpriv, xpub and mnemonic type Keys struct { xpriv string @@ -63,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, } @@ -90,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) } From 1df370cfe152b0aaa01fbc664279a32af26790dd Mon Sep 17 00:00:00 2001 From: yarex-4chain Date: Thu, 25 Jul 2024 10:13:17 +0200 Subject: [PATCH 05/13] feat/SPV-936: Serialise -> Serialize --- authentication.go | 4 ++-- http.go | 4 ++-- totp_test.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/authentication.go b/authentication.go index f3456f3..96813c9 100644 --- a/authentication.go +++ b/authentication.go @@ -124,8 +124,8 @@ func getUnlockingScript(tx *trx.Transaction, inputIndex uint32, privateKey *ec.P return nil, err } - pubKey := privateKey.PubKey().SerialiseCompressed() - signature := sig.Serialise() + pubKey := privateKey.PubKey().SerializeCompressed() + signature := sig.Serialize() var script *script.Script if script, err = script.NewP2PKHUnlockingScript(pubKey, signature, sigHashFlags); err != nil { diff --git a/http.go b/http.go index 2be7545..32d27a0 100644 --- a/http.go +++ b/http.go @@ -488,7 +488,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 @@ -567,7 +567,7 @@ func (wc *WalletClient) authenticateWithAccessKey(req *http.Request, rawJSON []b if wc.accessKey == nil { return WrapError(errors.New("access key is missing")) } - 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/totp_test.go b/totp_test.go index 3cef2d0..a914004 100644 --- a/totp_test.go +++ b/totp_test.go @@ -128,5 +128,5 @@ func makeMockPKI(xpub string) string { panic(err) } - return hex.EncodeToString(pubKey.SerialiseCompressed()) + return hex.EncodeToString(pubKey.SerializeCompressed()) } From fc817cb0f4736da0d25c84975afdc75802ca98ea Mon Sep 17 00:00:00 2001 From: wregulski Date: Mon, 9 Sep 2024 16:03:37 +0200 Subject: [PATCH 06/13] feat(SPV-936): upgrade to go-sdk --- examples/go.mod | 7 ++----- examples/go.sum | 16 ++++------------ 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/examples/go.mod b/examples/go.mod index 9e17759..8bf3a50 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.5 // 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 ae2fa9f..cfcffc7 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -1,21 +1,13 @@ -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.23.0.20240823064107-bcc29881132b h1:NCjFanr8Z0Xvdbof6gHwGnfW4Anf5tL3XDqwdB6S7NY= -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.23.0.20240823064107-bcc29881132b/go.mod h1:PEJdH9ZWKOiKHyOZkzYsRbKuZjzlRaEJy3GsM75Icdo= +github.com/bitcoin-sv/go-sdk v1.1.5 h1:AO2l33F4Ql61qfW3lBSjHsAmKhkziNhGdeiiM6DkaEI= +github.com/bitcoin-sv/go-sdk v1.1.5/go.mod h1:NOAkJLbjqKOLuxJmb9ABG86ExTZp4HS8+iygiDIUps4= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.24 h1:A5uhGBRo5eC1Nu30inL9X0QsFOd0wXUruLuwV9+HyLo= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.24/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= @@ -26,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= From 77a5834b913c4c712426c1f39827e849c5ca9b05 Mon Sep 17 00:00:00 2001 From: wregulski Date: Tue, 10 Sep 2024 09:07:13 +0200 Subject: [PATCH 07/13] fix(SPV-936): skip redundant predeclaration --- authentication.go | 128 +++++++++++++++++----------------------------- client_options.go | 5 +- go.mod | 2 +- go.sum | 2 + 4 files changed, 51 insertions(+), 86 deletions(-) diff --git a/authentication.go b/authentication.go index 96813c9..f831619 100644 --- a/authentication.go +++ b/authentication.go @@ -11,15 +11,12 @@ import ( 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" ) -// TODO: NewTxFromString - NewTransactionFromHex ? -// TODO: NewFromHexString - no replacement in v2 -// TODO: NewP2PKHUnlockingScript - not found - // 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 @@ -37,102 +34,69 @@ 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 *trx.Transaction - if tx, err = trx.NewTransactionFromHex(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) + 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 } - } - // Return the signed hex - signedHex = tx.String() - return -} - -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 { - return + tx.AddInputFrom(draftInput.TransactionID, draftInput.OutputIndex, lockingScript.String(), draftInput.Satoshis, unlockScript) } - tx.Inputs[inputIndex].PreviousTxScript = ls - return + tx.Sign() + + return tx.String(), nil } -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 - } +func prepareLockingScript(dst *models.Destination) (*script.Script, error) { + return script.NewFromHex(dst.LockingScript) +} - var s *script.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 *ec.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 = bip32.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 = bip32.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 *trx.Transaction, inputIndex uint32, privateKey *ec.PrivateKey) (*script.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 *ec.Signature - if sig, err = privateKey.Sign(sigHash); err != nil { - return nil, err - } - - pubKey := privateKey.PubKey().SerializeCompressed() - signature := sig.Serialize() - - var script *script.Script - if script, err = script.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 @@ -187,21 +151,23 @@ func createSignatureCommon(payload *models.AuthPayload, bodyString string, priva } // Signature, using bitcoin signMessage - var err error - if payload.Signature, err = bsm.SignMessage( + sigBytes, err := bsm.SignMessage( privateKey, getSigningMessage(key, payload), - true, - ); err != nil { + ) + if err != nil { return nil, err } + payload.Signature = string(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) // Convert string to byte array } func setSignatureHeaders(header *http.Header, authData *models.AuthPayload) { diff --git a/client_options.go b/client_options.go index 8f9c5d0..2f55854 100644 --- a/client_options.go +++ b/client_options.go @@ -103,10 +103,7 @@ func (w *signRequest) Configure(c *WalletClient) { // initializeAccessKey handles the specific initialization of the access key. func (w *accessKeyConf) initializeAccessKey() (*ec.PrivateKey, error) { - var err error - var privateKey *ec.PrivateKey - - privateKey, err = ec.PrivateKeyFromWif(w.AccessKeyString) + privateKey, err := ec.PrivateKeyFromWif(w.AccessKeyString) if err != nil { return nil, errors.Wrap(err, "failed to decode access key") } diff --git a/go.mod b/go.mod index 1341f4e..2143f3b 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/bitcoin-sv/spv-wallet-go-client go 1.22.5 require ( - github.com/bitcoin-sv/go-sdk v1.1.3 + github.com/bitcoin-sv/go-sdk v1.1.5 github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.24 github.com/pkg/errors v0.9.1 github.com/pquerna/otp v1.4.0 diff --git a/go.sum b/go.sum index 3df03cb..b2ad93e 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/bitcoin-sv/go-sdk v1.1.3 h1:kZZp++bgZwXHxNQuRWqywA8WY0luEq33LGPPGfSKBdo= github.com/bitcoin-sv/go-sdk v1.1.3/go.mod h1:NOAkJLbjqKOLuxJmb9ABG86ExTZp4HS8+iygiDIUps4= +github.com/bitcoin-sv/go-sdk v1.1.5 h1:AO2l33F4Ql61qfW3lBSjHsAmKhkziNhGdeiiM6DkaEI= +github.com/bitcoin-sv/go-sdk v1.1.5/go.mod h1:NOAkJLbjqKOLuxJmb9ABG86ExTZp4HS8+iygiDIUps4= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.24 h1:A5uhGBRo5eC1Nu30inL9X0QsFOd0wXUruLuwV9+HyLo= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.24/go.mod h1:PEJdH9ZWKOiKHyOZkzYsRbKuZjzlRaEJy3GsM75Icdo= github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= From 989820fc4281e2da626dd7b861c9cea7dcf0890b Mon Sep 17 00:00:00 2001 From: wregulski Date: Tue, 10 Sep 2024 10:59:36 +0200 Subject: [PATCH 08/13] fix(SPV-936): add proper string encoding to a computed signature --- authentication.go | 3 ++- client_options.go | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/authentication.go b/authentication.go index f831619..7ba021d 100644 --- a/authentication.go +++ b/authentication.go @@ -1,6 +1,7 @@ package walletclient import ( + "encoding/base64" "fmt" "net/http" "time" @@ -159,7 +160,7 @@ func createSignatureCommon(payload *models.AuthPayload, bodyString string, priva return nil, err } - payload.Signature = string(sigBytes) + payload.Signature = base64.StdEncoding.EncodeToString(sigBytes) return payload, nil } diff --git a/client_options.go b/client_options.go index 2f55854..b5ef1b7 100644 --- a/client_options.go +++ b/client_options.go @@ -1,6 +1,7 @@ package walletclient import ( + "encoding/hex" "fmt" "net/http" "net/url" @@ -101,11 +102,18 @@ func (w *signRequest) Configure(c *WalletClient) { c.signRequest = w.Sign } -// initializeAccessKey handles the specific initialization of the access key. func (w *accessKeyConf) initializeAccessKey() (*ec.PrivateKey, error) { privateKey, err := ec.PrivateKeyFromWif(w.AccessKeyString) if err != nil { - return nil, errors.Wrap(err, "failed to decode access key") + keyBytes, err := hex.DecodeString(w.AccessKeyString) + if err != nil { + return nil, errors.Wrap(err, "failed to decode access key") + } + + privateKey, _ = ec.PrivateKeyFromBytes(keyBytes) + if privateKey == nil { + return nil, errors.New("failed to decode access key") + } } return privateKey, nil From 105e1eb8f473f64ef9d276b2d7f049b68efd2589 Mon Sep 17 00:00:00 2001 From: wregulski Date: Tue, 10 Sep 2024 11:07:18 +0200 Subject: [PATCH 09/13] fix(SPV-936): simplify redundant conversion with predefined go-sdk method --- client_options.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/client_options.go b/client_options.go index b5ef1b7..fcee338 100644 --- a/client_options.go +++ b/client_options.go @@ -1,7 +1,6 @@ package walletclient import ( - "encoding/hex" "fmt" "net/http" "net/url" @@ -105,12 +104,7 @@ func (w *signRequest) Configure(c *WalletClient) { func (w *accessKeyConf) initializeAccessKey() (*ec.PrivateKey, error) { privateKey, err := ec.PrivateKeyFromWif(w.AccessKeyString) if err != nil { - keyBytes, err := hex.DecodeString(w.AccessKeyString) - if err != nil { - return nil, errors.Wrap(err, "failed to decode access key") - } - - privateKey, _ = ec.PrivateKeyFromBytes(keyBytes) + privateKey, _ = ec.PrivateKeyFromHex(w.AccessKeyString) if privateKey == nil { return nil, errors.New("failed to decode access key") } From 33d1b4dc207bb902e59930a28d3a70bcaf96e3c1 Mon Sep 17 00:00:00 2001 From: wregulski Date: Tue, 10 Sep 2024 16:11:10 +0200 Subject: [PATCH 10/13] fix(SPV-936): clear inputs before signing them as we add them again --- authentication.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/authentication.go b/authentication.go index 7ba021d..245f04f 100644 --- a/authentication.go +++ b/authentication.go @@ -38,6 +38,9 @@ func setSignature(header *http.Header, xPriv *bip32.ExtendedKey, bodyString stri 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 } From 80fa3dd8d7958d08492a72108f7399efc4d58ff3 Mon Sep 17 00:00:00 2001 From: wregulski Date: Wed, 11 Sep 2024 09:09:58 +0200 Subject: [PATCH 11/13] fix(SPV-936): add wrapcheck for lockingscript error and remove unnecessary comment --- authentication.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/authentication.go b/authentication.go index 245f04f..1a77719 100644 --- a/authentication.go +++ b/authentication.go @@ -66,7 +66,12 @@ func GetSignedHex(dt *models.DraftTransaction, xPriv *bip32.ExtendedKey) (string } func prepareLockingScript(dst *models.Destination) (*script.Script, error) { - return script.NewFromHex(dst.LockingScript) + lockingScript, err := script.NewFromHex(dst.LockingScript) + if err != nil { + return nil, fmt.Errorf("failed to create locking script from hex for destination: %w", err) + } + + return lockingScript, nil } func prepareUnlockingScript(xPriv *bip32.ExtendedKey, dst *models.Destination) (*p2pkh.P2PKH, error) { @@ -171,7 +176,7 @@ func createSignatureCommon(payload *models.AuthPayload, bodyString string, priva // 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) // Convert string to byte array + return []byte(message) } func setSignatureHeaders(header *http.Header, authData *models.AuthPayload) { From 8148f2107d71b55dad3f0d2dee01635ab64f47ed Mon Sep 17 00:00:00 2001 From: wregulski Date: Wed, 11 Sep 2024 10:54:13 +0200 Subject: [PATCH 12/13] feat(SPV-936): update go mod in examples --- examples/go.mod | 2 +- examples/go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/go.mod b/examples/go.mod index b01f222..472de8f 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -10,7 +10,7 @@ require ( ) require ( - github.com/bitcoin-sv/go-sdk v1.1.5 // indirect + github.com/bitcoin-sv/go-sdk v1.1.7 // indirect github.com/boombuler/barcode v1.0.2 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pquerna/otp v1.4.0 // indirect diff --git a/examples/go.sum b/examples/go.sum index 987357a..db4d1ba 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -1,5 +1,5 @@ -github.com/bitcoin-sv/go-sdk v1.1.5 h1:AO2l33F4Ql61qfW3lBSjHsAmKhkziNhGdeiiM6DkaEI= -github.com/bitcoin-sv/go-sdk v1.1.5/go.mod h1:NOAkJLbjqKOLuxJmb9ABG86ExTZp4HS8+iygiDIUps4= +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/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= From 7e501600dd4893b651208cbf0e01e12ac31fb314 Mon Sep 17 00:00:00 2001 From: wregulski Date: Wed, 11 Sep 2024 13:46:20 +0200 Subject: [PATCH 13/13] fix(SPV-936): proper key in examples --- examples/handle_exceptions/handle_exceptions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/handle_exceptions/handle_exceptions.go b/examples/handle_exceptions/handle_exceptions.go index 7bb7b31..39558fe 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")