forked from joltify-finance/tss
/
key_provider.go
124 lines (114 loc) · 3.59 KB
/
key_provider.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
118
119
120
121
122
123
124
package conversion
import (
"encoding/base64"
"errors"
"fmt"
"github.com/btcsuite/btcd/btcec"
coskey "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/types/bech32/legacybech32"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
tcrypto "github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/secp256k1"
)
// GetPeerIDFromPubKey get the peer.ID from bech32 format node pub key
func GetPeerIDFromPubKey(pubkey string) (peer.ID, error) {
pk, err := legacybech32.UnmarshalPubKey(legacybech32.AccPK, pubkey)
if err != nil {
return "", fmt.Errorf("fail to parse account pub key(%s): %w", pubkey, err)
}
ppk, err := crypto.UnmarshalEd25519PublicKey(pk.Bytes())
if err != nil {
return "", fmt.Errorf("fail to convert pubkey to the crypto pubkey used in libp2p: %w", err)
}
return peer.IDFromPublicKey(ppk)
}
// GetPeerIDsFromPubKeys convert a list of node pub key to their peer.ID
func GetPeerIDsFromPubKeys(pubkeys []string) ([]peer.ID, error) {
var peerIDs []peer.ID
for _, item := range pubkeys {
peerID, err := GetPeerIDFromPubKey(item)
if err != nil {
return nil, err
}
peerIDs = append(peerIDs, peerID)
}
return peerIDs, nil
}
// GetPeerIDs return a slice of peer id
func GetPeerIDs(pubkeys []string) ([]peer.ID, error) {
var peerIDs []peer.ID
for _, item := range pubkeys {
pID, err := GetPeerIDFromPubKey(item)
if err != nil {
return nil, fmt.Errorf("fail to get peer id from pubkey(%s):%w", item, err)
}
peerIDs = append(peerIDs, pID)
}
return peerIDs, nil
}
// GetPubKeysFromPeerIDs given a list of peer ids, and get a list og pub keys.
func GetPubKeysFromPeerIDs(peers []string) ([]string, error) {
var result []string
for _, item := range peers {
pKey, err := GetPubKeyFromPeerID(item)
if err != nil {
return nil, fmt.Errorf("fail to get pubkey from peerID: %w", err)
}
result = append(result, pKey)
}
return result, nil
}
// GetPubKeyFromPeerID extract the pub key from PeerID
func GetPubKeyFromPeerID(pID string) (string, error) {
peerID, err := peer.Decode(pID)
if err != nil {
return "", fmt.Errorf("fail to decode peer id: %w", err)
}
pk, err := peerID.ExtractPublicKey()
if err != nil {
return "", fmt.Errorf("fail to extract pub key from peer id: %w", err)
}
rawBytes, err := pk.Raw()
if err != nil {
return "", fmt.Errorf("faail to get pub key raw bytes: %w", err)
}
pubKey := coskey.PubKey{
Key: rawBytes,
}
return legacybech32.MarshalPubKey(legacybech32.AccPK, &pubKey)
}
func GetPriKey(priKeyString string) (tcrypto.PrivKey, error) {
priBytes, err := base64.StdEncoding.DecodeString(priKeyString)
if err != nil {
return nil, fmt.Errorf("fail to decode private key: %w", err)
}
//rawBytes, err := hex.DecodeString(string(priHexBytes))
//if err != nil {
// return nil, fmt.Errorf("fail to hex decode private key: %w", err)
//}
var priKey ed25519.PrivKey
priKey = priBytes
return priKey, nil
}
func GetPriKeyRawBytes(priKey tcrypto.PrivKey) ([]byte, error) {
var keyBytesArray [32]byte
pk, ok := priKey.(secp256k1.PrivKey)
if !ok {
return nil, errors.New("private key is not secp256p1.PrivKey")
}
copy(keyBytesArray[:], pk[:])
return keyBytesArray[:], nil
}
func CheckKeyOnCurve(pk string) (bool, error) {
pubKey, err := legacybech32.UnmarshalPubKey(legacybech32.AccPK, pk)
if err != nil {
return false, fmt.Errorf("fail to parse pub key(%s): %w", pk, err)
}
bPk, err := btcec.ParsePubKey(pubKey.Bytes(), btcec.S256())
if err != nil {
return false, err
}
return isOnCurve(bPk.X, bPk.Y), nil
}