From cd713e62c7e65b0b9af60c2db8b5b1feff216fd8 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 1 Dec 2022 19:13:14 +0100 Subject: [PATCH 1/4] chore(crypto): update crypto/ledger to v2 --- crypto/ledger/ledger_mock.go | 13 ++++++++----- crypto/ledger/ledger_notavail.go | 2 +- crypto/ledger/ledger_secp256k1.go | 22 +++++++++++++++------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/crypto/ledger/ledger_mock.go b/crypto/ledger/ledger_mock.go index 7a618f40301..ab5fcc18495 100644 --- a/crypto/ledger/ledger_mock.go +++ b/crypto/ledger/ledger_mock.go @@ -4,11 +4,11 @@ package ledger import ( + "errors" "fmt" - "github.com/btcsuite/btcd/btcec" - "github.com/pkg/errors" - + btcec "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec/v2/ecdsa" "github.com/cosmos/go-bip39" secp256k1 "github.com/tendermint/btcd/btcec" "github.com/tendermint/tendermint/crypto" @@ -73,7 +73,7 @@ func (mock LedgerSECP256K1Mock) GetAddressPubKeySECP256K1(derivationPath []uint3 } // re-serialize in the 33-byte compressed format - cmp, err := btcec.ParsePubKey(pk[:], btcec.S256()) + cmp, err := btcec.ParsePubKey(pk[:]) if err != nil { return nil, "", fmt.Errorf("error parsing public key: %v", err) } @@ -108,7 +108,10 @@ func (mock LedgerSECP256K1Mock) SignSECP256K1(derivationPath []uint32, message [ } // Need to return DER as the ledger does - sig2 := btcec.Signature{R: sig.R, S: sig.S} + var r, s *btcec.ModNScalar + r.SetByteSlice(sig.R.Bytes()) + s.SetByteSlice(sig.S.Bytes()) + sig2 := ecdsa.NewSignature(r, s) return sig2.Serialize(), nil } diff --git a/crypto/ledger/ledger_notavail.go b/crypto/ledger/ledger_notavail.go index a183166faac..35a60cf356f 100644 --- a/crypto/ledger/ledger_notavail.go +++ b/crypto/ledger/ledger_notavail.go @@ -6,7 +6,7 @@ package ledger import ( - "github.com/pkg/errors" + "errors" ) // If ledger support (build tag) has been enabled, which implies a CGO dependency, diff --git a/crypto/ledger/ledger_secp256k1.go b/crypto/ledger/ledger_secp256k1.go index ed6d80a4ad4..d34a7a5b7ed 100644 --- a/crypto/ledger/ledger_secp256k1.go +++ b/crypto/ledger/ledger_secp256k1.go @@ -1,11 +1,13 @@ package ledger import ( + "errors" "fmt" + "math/big" "os" - "github.com/btcsuite/btcd/btcec" - "github.com/pkg/errors" + btcec "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec/v2/ecdsa" tmbtcec "github.com/tendermint/btcd/btcec" @@ -210,11 +212,17 @@ func warnIfErrors(f func() error) { } func convertDERtoBER(signatureDER []byte) ([]byte, error) { - sigDER, err := btcec.ParseDERSignature(signatureDER, btcec.S256()) + sigDER, err := ecdsa.ParseDERSignature(signatureDER) if err != nil { return nil, err } - sigBER := tmbtcec.Signature{R: sigDER.R, S: sigDER.S} + + sigStr := sigDER.Serialize() + var r, s *big.Int + r.SetBytes(sigStr[:32]) + s.SetBytes(sigStr[32:64]) + + sigBER := tmbtcec.Signature{R: r, S: s} return sigBER.Serialize(), nil } @@ -225,7 +233,7 @@ func getDevice() (SECP256K1, error) { device, err := options.discoverLedger() if err != nil { - return nil, errors.Wrap(err, "ledger nano S") + return nil, fmt.Errorf("ledger nano S: %w", err) } return device, nil @@ -283,7 +291,7 @@ func getPubKeyUnsafe(device SECP256K1, path hd.BIP44Params) (types.PubKey, error } // re-serialize in the 33-byte compressed format - cmp, err := btcec.ParsePubKey(publicKey, btcec.S256()) + cmp, err := btcec.ParsePubKey(publicKey) if err != nil { return nil, fmt.Errorf("error parsing public key: %v", err) } @@ -307,7 +315,7 @@ func getPubKeyAddrSafe(device SECP256K1, path hd.BIP44Params, hrp string) (types } // re-serialize in the 33-byte compressed format - cmp, err := btcec.ParsePubKey(publicKey, btcec.S256()) + cmp, err := btcec.ParsePubKey(publicKey) if err != nil { return nil, "", fmt.Errorf("error parsing public key: %v", err) } From 2a072c04d9134917d5028e25db9883a8a8f03528 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 1 Dec 2022 19:16:02 +0100 Subject: [PATCH 2/4] go mod tidy --- go.mod | 2 +- simapp/go.mod | 1 - simapp/go.sum | 1 - tests/go.mod | 1 - tests/go.sum | 1 - 5 files changed, 1 insertion(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 5f4598f378d..02dca1707f4 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,6 @@ require ( github.com/99designs/keyring v1.2.1 github.com/armon/go-metrics v0.4.1 github.com/bgentry/speakeasy v0.1.0 - github.com/btcsuite/btcd v0.22.3 github.com/btcsuite/btcd/btcec/v2 v2.3.2 github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e github.com/cockroachdb/apd/v2 v2.0.2 @@ -74,6 +73,7 @@ require ( github.com/aws/aws-sdk-go v1.40.45 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect + github.com/btcsuite/btcd v0.22.3 // indirect github.com/cenkalti/backoff/v4 v4.1.3 // indirect github.com/cespare/xxhash v1.1.0 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect diff --git a/simapp/go.mod b/simapp/go.mod index 5dc0a81939f..263340f1cb2 100644 --- a/simapp/go.mod +++ b/simapp/go.mod @@ -35,7 +35,6 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect github.com/bgentry/speakeasy v0.1.0 // indirect - github.com/btcsuite/btcd v0.22.3 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect github.com/cenkalti/backoff/v4 v4.1.3 // indirect github.com/cespare/xxhash v1.1.0 // indirect diff --git a/simapp/go.sum b/simapp/go.sum index 621d56680f5..ecadca52ed3 100644 --- a/simapp/go.sum +++ b/simapp/go.sum @@ -113,7 +113,6 @@ github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= github.com/btcsuite/btcd v0.22.3 h1:kYNaWFvOw6xvqP0vR20RP1Zq1DVMBxEO8QN5d1/EfNg= -github.com/btcsuite/btcd v0.22.3/go.mod h1:wqgTSL29+50LRkmOVknEdmt8ZojIzhuWvgu/iptuN7Y= github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U= github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= github.com/btcsuite/btcd/btcutil v1.1.2 h1:XLMbX8JQEiwMcYft2EGi8zPUkoa0abKIU6/BJSRsjzQ= diff --git a/tests/go.mod b/tests/go.mod index a4e0c5ec613..69d6b33e09f 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -37,7 +37,6 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect github.com/bgentry/speakeasy v0.1.0 // indirect - github.com/btcsuite/btcd v0.22.3 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect github.com/cenkalti/backoff/v4 v4.1.3 // indirect github.com/cespare/xxhash v1.1.0 // indirect diff --git a/tests/go.sum b/tests/go.sum index 47ad311f7d7..fda54117eef 100644 --- a/tests/go.sum +++ b/tests/go.sum @@ -111,7 +111,6 @@ github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= github.com/btcsuite/btcd v0.22.3 h1:kYNaWFvOw6xvqP0vR20RP1Zq1DVMBxEO8QN5d1/EfNg= -github.com/btcsuite/btcd v0.22.3/go.mod h1:wqgTSL29+50LRkmOVknEdmt8ZojIzhuWvgu/iptuN7Y= github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U= github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= github.com/btcsuite/btcd/btcutil v1.1.2 h1:XLMbX8JQEiwMcYft2EGi8zPUkoa0abKIU6/BJSRsjzQ= From ecab39c578a8782d7e9eb5c2f32ca89696dbe95d Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 1 Dec 2022 22:28:20 +0100 Subject: [PATCH 3/4] updates --- crypto/ledger/ledger_mock.go | 5 ++--- crypto/ledger/ledger_secp256k1.go | 9 ++++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/crypto/ledger/ledger_mock.go b/crypto/ledger/ledger_mock.go index ab5fcc18495..ec9ad15076b 100644 --- a/crypto/ledger/ledger_mock.go +++ b/crypto/ledger/ledger_mock.go @@ -108,11 +108,10 @@ func (mock LedgerSECP256K1Mock) SignSECP256K1(derivationPath []uint32, message [ } // Need to return DER as the ledger does - var r, s *btcec.ModNScalar + var r, s btcec.ModNScalar r.SetByteSlice(sig.R.Bytes()) s.SetByteSlice(sig.S.Bytes()) - sig2 := ecdsa.NewSignature(r, s) - return sig2.Serialize(), nil + return ecdsa.NewSignature(&r, &s).Serialize(), nil } // ShowAddressSECP256K1 shows the address for the corresponding bip32 derivation path diff --git a/crypto/ledger/ledger_secp256k1.go b/crypto/ledger/ledger_secp256k1.go index d34a7a5b7ed..ec835d4475d 100644 --- a/crypto/ledger/ledger_secp256k1.go +++ b/crypto/ledger/ledger_secp256k1.go @@ -218,11 +218,10 @@ func convertDERtoBER(signatureDER []byte) ([]byte, error) { } sigStr := sigDER.Serialize() - var r, s *big.Int - r.SetBytes(sigStr[:32]) - s.SetBytes(sigStr[32:64]) - - sigBER := tmbtcec.Signature{R: r, S: s} + var r, s big.Int + r.SetBytes(sigStr[4 : 4+sigStr[3]]) + s.SetBytes(sigStr[4+sigStr[3]+2:]) + sigBER := tmbtcec.Signature{R: &r, S: &s} return sigBER.Serialize(), nil } From d2a00a9942692fb3f95ee67117719581bde71102 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Fri, 2 Dec 2022 00:47:00 +0100 Subject: [PATCH 4/4] add comment --- crypto/ledger/ledger_secp256k1.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crypto/ledger/ledger_secp256k1.go b/crypto/ledger/ledger_secp256k1.go index ec835d4475d..58e1e7d4a30 100644 --- a/crypto/ledger/ledger_secp256k1.go +++ b/crypto/ledger/ledger_secp256k1.go @@ -219,9 +219,12 @@ func convertDERtoBER(signatureDER []byte) ([]byte, error) { sigStr := sigDER.Serialize() var r, s big.Int + // The format of a DER encoded signature is as follows: + // 0x30 0x02 0x02 r.SetBytes(sigStr[4 : 4+sigStr[3]]) s.SetBytes(sigStr[4+sigStr[3]+2:]) sigBER := tmbtcec.Signature{R: &r, S: &s} + return sigBER.Serialize(), nil }