refactor: reduce tendermint deps (#14616)
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
||||
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"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||
@@ -40,11 +39,11 @@ func (mock LedgerSECP256K1Mock) Close() error {
|
||||
// as per the original API, it returns an uncompressed key
|
||||
func (mock LedgerSECP256K1Mock) GetPublicKeySECP256K1(derivationPath []uint32) ([]byte, error) {
|
||||
if derivationPath[0] != 44 {
|
||||
return nil, errors.New("Invalid derivation path")
|
||||
return nil, errors.New("invalid derivation path")
|
||||
}
|
||||
|
||||
if derivationPath[1] != sdk.GetConfig().GetCoinType() {
|
||||
return nil, errors.New("Invalid derivation path")
|
||||
return nil, errors.New("invalid derivation path")
|
||||
}
|
||||
|
||||
seed, err := bip39.NewSeedWithErrorChecking(testdata.TestMnemonic, "")
|
||||
@@ -59,7 +58,7 @@ func (mock LedgerSECP256K1Mock) GetPublicKeySECP256K1(derivationPath []uint32) (
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, pubkeyObject := secp256k1.PrivKeyFromBytes(secp256k1.S256(), derivedPriv[:])
|
||||
_, pubkeyObject := btcec.PrivKeyFromBytes(derivedPriv)
|
||||
|
||||
return pubkeyObject.SerializeUncompressed(), nil
|
||||
}
|
||||
@@ -73,7 +72,7 @@ func (mock LedgerSECP256K1Mock) GetAddressPubKeySECP256K1(derivationPath []uint3
|
||||
}
|
||||
|
||||
// re-serialize in the 33-byte compressed format
|
||||
cmp, err := btcec.ParsePubKey(pk[:])
|
||||
cmp, err := btcec.ParsePubKey(pk)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("error parsing public key: %v", err)
|
||||
}
|
||||
@@ -100,18 +99,10 @@ func (mock LedgerSECP256K1Mock) SignSECP256K1(derivationPath []uint32, message [
|
||||
return nil, err
|
||||
}
|
||||
|
||||
priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), derivedPriv[:])
|
||||
priv, _ := btcec.PrivKeyFromBytes(derivedPriv)
|
||||
sig := ecdsa.Sign(priv, crypto.Sha256(message))
|
||||
|
||||
sig, err := priv.Sign(crypto.Sha256(message))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Need to return DER as the ledger does
|
||||
var r, s btcec.ModNScalar
|
||||
r.SetByteSlice(sig.R.Bytes())
|
||||
s.SetByteSlice(sig.S.Bytes())
|
||||
return ecdsa.NewSignature(&r, &s).Serialize(), nil
|
||||
return sig.Serialize(), nil
|
||||
}
|
||||
|
||||
// ShowAddressSECP256K1 shows the address for the corresponding bip32 derivation path
|
||||
|
||||
@@ -9,8 +9,6 @@ import (
|
||||
btcec "github.com/btcsuite/btcd/btcec/v2"
|
||||
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
|
||||
|
||||
tmbtcec "github.com/tendermint/btcd/btcec"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/types"
|
||||
@@ -218,14 +216,25 @@ 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 <total length> 0x02 <length of R> <R> 0x02 <length of S> <S>
|
||||
r, s := new(big.Int), new(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
|
||||
sModNScalar := new(btcec.ModNScalar)
|
||||
sModNScalar.SetByteSlice(s.Bytes())
|
||||
// based on https://github.com/tendermint/btcd/blob/ec996c5/btcec/signature.go#L33-L50
|
||||
if sModNScalar.IsOverHalfOrder() {
|
||||
s = new(big.Int).Sub(btcec.S256().N, s)
|
||||
}
|
||||
|
||||
sigBytes := make([]byte, 64)
|
||||
// 0 pad the byte arrays from the left if they aren't big enough.
|
||||
copy(sigBytes[32-len(r.Bytes()):32], r.Bytes())
|
||||
copy(sigBytes[64-len(s.Bytes()):64], s.Bytes())
|
||||
|
||||
return sigBytes, nil
|
||||
}
|
||||
|
||||
func getDevice() (SECP256K1, error) {
|
||||
|
||||
Reference in New Issue
Block a user