refactor: update crypto/ledger to btcec/v2 (#14123)

* chore(crypto): update crypto/ledger to v2

* go mod tidy

* updates

* add comment
This commit is contained in:
Julien Robert
2022-12-05 14:15:44 +00:00
committed by GitHub
parent 3f08dffc20
commit 417ce2511c
8 changed files with 27 additions and 19 deletions
+8 -6
View File
@@ -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,8 +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}
return sig2.Serialize(), nil
var r, s btcec.ModNScalar
r.SetByteSlice(sig.R.Bytes())
s.SetByteSlice(sig.S.Bytes())
return ecdsa.NewSignature(&r, &s).Serialize(), nil
}
// ShowAddressSECP256K1 shows the address for the corresponding bip32 derivation path
+1 -1
View File
@@ -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,
+17 -7
View File
@@ -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,19 @@ 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
// The format of a DER encoded signature is as follows:
// 0x30 <total length> 0x02 <length of R> <R> 0x02 <length of S> <S>
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
}
@@ -225,7 +235,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 +293,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 +317,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)
}