forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledger_mock.go
117 lines (92 loc) · 3.55 KB
/
ledger_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// +build ledger,test_ledger_mock
package crypto
import (
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/pkg/errors"
secp256k1 "github.com/tendermint/btcd/btcec"
"github.com/tendermint/tendermint/crypto"
tmsecp256k1 "github.com/tendermint/tendermint/crypto/secp256k1"
bip39 "github.com/cosmos/go-bip39"
"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// If ledger support (build tag) has been enabled, which implies a CGO dependency,
// set the discoverLedger function which is responsible for loading the Ledger
// device at runtime or returning an error.
func init() {
discoverLedger = func() (LedgerSECP256K1, error) {
return LedgerSECP256K1Mock{}, nil
}
}
type LedgerSECP256K1Mock struct {
}
func (mock LedgerSECP256K1Mock) Close() error {
return nil
}
// GetPublicKeySECP256K1 mocks a ledger device
// 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")
}
if derivationPath[1] != sdk.GetConfig().GetCoinType() {
return nil, errors.New("Invalid derivation path")
}
seed, err := bip39.NewSeedWithErrorChecking(tests.TestMnemonic, "")
if err != nil {
return nil, err
}
path := hd.NewParams(derivationPath[0], derivationPath[1], derivationPath[2], derivationPath[3] != 0, derivationPath[4])
masterPriv, ch := hd.ComputeMastersFromSeed(seed)
derivedPriv, err := hd.DerivePrivateKeyForPath(masterPriv, ch, path.String())
if err != nil {
return nil, err
}
_, pubkeyObject := secp256k1.PrivKeyFromBytes(secp256k1.S256(), derivedPriv[:])
return pubkeyObject.SerializeUncompressed(), nil
}
// GetAddressPubKeySECP256K1 mocks a ledger device
// as per the original API, it returns a compressed key and a bech32 address
func (mock LedgerSECP256K1Mock) GetAddressPubKeySECP256K1(derivationPath []uint32, hrp string) ([]byte, string, error) {
pk, err := mock.GetPublicKeySECP256K1(derivationPath)
if err != nil {
return nil, "", err
}
// re-serialize in the 33-byte compressed format
cmp, err := btcec.ParsePubKey(pk[:], btcec.S256())
if err != nil {
return nil, "", fmt.Errorf("error parsing public key: %v", err)
}
var compressedPublicKey tmsecp256k1.PubKeySecp256k1
copy(compressedPublicKey[:], cmp.SerializeCompressed())
// Generate the bech32 addr using existing tmcrypto/etc.
addr := sdk.AccAddress(compressedPublicKey.Address()).String()
return pk, addr, err
}
func (mock LedgerSECP256K1Mock) SignSECP256K1(derivationPath []uint32, message []byte) ([]byte, error) {
path := hd.NewParams(derivationPath[0], derivationPath[1], derivationPath[2], derivationPath[3] != 0, derivationPath[4])
seed, err := bip39.NewSeedWithErrorChecking(tests.TestMnemonic, "")
if err != nil {
return nil, err
}
masterPriv, ch := hd.ComputeMastersFromSeed(seed)
derivedPriv, err := hd.DerivePrivateKeyForPath(masterPriv, ch, path.String())
if err != nil {
return nil, err
}
priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), derivedPriv[:])
sig, err := priv.Sign(crypto.Sha256(message))
if err != nil {
return nil, err
}
// Need to return DER as the ledger does
sig2 := btcec.Signature{R: sig.R, S: sig.S}
return sig2.Serialize(), nil
}
// ShowAddressSECP256K1 shows the address for the corresponding bip32 derivation path
func (mock LedgerSECP256K1Mock) ShowAddressSECP256K1(bip32Path []uint32, hrp string) error {
fmt.Printf("Request to show address for %v at %v", hrp, bip32Path)
return nil
}