forked from yangmiok/thorchain-tss
/
conversion.go
185 lines (166 loc) · 5.18 KB
/
conversion.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package conversion
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"math"
"math/big"
"sort"
"strconv"
"strings"
"github.com/binance-chain/tss-lib/crypto"
btss "github.com/binance-chain/tss-lib/tss"
"github.com/btcsuite/btcd/btcec"
coskey "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
crypto2 "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"gitlab.com/thorchain/binance-sdk/common/types"
"github.com/joltgeorge/tss/messages"
)
// GetPeerIDFromSecp256PubKey convert the given pubkey into a peer.ID
func GetPeerIDFromSecp256PubKey(pk []byte) (peer.ID, error) {
if len(pk) == 0 {
return "", errors.New("empty public key raw bytes")
}
ppk, err := crypto2.UnmarshalSecp256k1PublicKey(pk)
if err != nil {
return "", fmt.Errorf("fail to convert pubkey to the crypto pubkey used in libp2p: %w", err)
}
return peer.IDFromPublicKey(ppk)
}
func GetPeerIDFromPartyID(partyID *btss.PartyID) (peer.ID, error) {
if partyID == nil || !partyID.ValidateBasic() {
return "", errors.New("invalid partyID")
}
pkBytes := partyID.KeyInt().Bytes()
return GetPeerIDFromSecp256PubKey(pkBytes)
}
func PartyIDtoPubKey(party *btss.PartyID) (string, error) {
if party == nil || !party.ValidateBasic() {
return "", errors.New("invalid party")
}
partyKeyBytes := party.GetKey()
pk := coskey.PubKey{
Key: partyKeyBytes,
}
pubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, &pk)
if err != nil {
return "", err
}
return pubKey, nil
}
func AccPubKeysFromPartyIDs(partyIDs []string, partyIDMap map[string]*btss.PartyID) ([]string, error) {
pubKeys := make([]string, 0)
for _, partyID := range partyIDs {
blameParty, ok := partyIDMap[partyID]
if !ok {
return nil, errors.New("cannot find the blame party")
}
blamedPubKey, err := PartyIDtoPubKey(blameParty)
if err != nil {
return nil, err
}
pubKeys = append(pubKeys, blamedPubKey)
}
return pubKeys, nil
}
func SetupPartyIDMap(partiesID []*btss.PartyID) map[string]*btss.PartyID {
partyIDMap := make(map[string]*btss.PartyID)
for _, id := range partiesID {
partyIDMap[id.Id] = id
}
return partyIDMap
}
func GetPeersID(partyIDtoP2PID map[string]peer.ID, localPeerID string) []peer.ID {
if partyIDtoP2PID == nil {
return nil
}
peerIDs := make([]peer.ID, 0, len(partyIDtoP2PID)-1)
for _, value := range partyIDtoP2PID {
if value.String() == localPeerID {
continue
}
peerIDs = append(peerIDs, value)
}
return peerIDs
}
func SetupIDMaps(parties map[string]*btss.PartyID, partyIDtoP2PID map[string]peer.ID) error {
for id, party := range parties {
peerID, err := GetPeerIDFromPartyID(party)
if err != nil {
return err
}
partyIDtoP2PID[id] = peerID
}
return nil
}
func GetParties(keys []string, localPartyKey string) ([]*btss.PartyID, *btss.PartyID, error) {
var localPartyID *btss.PartyID
var unSortedPartiesID []*btss.PartyID
sort.Strings(keys)
for idx, item := range keys {
pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, item)
if err != nil {
return nil, nil, fmt.Errorf("fail to get account pub key address(%s): %w", item, err)
}
key := new(big.Int).SetBytes(pk.Bytes())
// Set up the parameters
// Note: The `id` and `moniker` fields are for convenience to allow you to easily track participants.
// The `id` should be a unique string representing this party in the network and `moniker` can be anything (even left blank).
// The `uniqueKey` is a unique identifying key for this peer (such as its p2p public key) as a big.Int.
partyID := btss.NewPartyID(strconv.Itoa(idx), "", key)
if item == localPartyKey {
localPartyID = partyID
}
unSortedPartiesID = append(unSortedPartiesID, partyID)
}
if localPartyID == nil {
return nil, nil, errors.New("local party is not in the list")
}
partiesID := btss.SortPartyIDs(unSortedPartiesID)
return partiesID, localPartyID, nil
}
func GetPreviousKeySignUicast(current string) string {
if strings.HasSuffix(current, messages.KEYSIGN1b) {
return messages.KEYSIGN1aUnicast
}
return messages.KEYSIGN2Unicast
}
func isOnCurve(x, y *big.Int) bool {
curve := btcec.S256()
return curve.IsOnCurve(x, y)
}
func GetTssPubKey(pubKeyPoint *crypto.ECPoint) (string, types.AccAddress, error) {
// we check whether the point is on curve according to Kudelski report
if pubKeyPoint == nil || !isOnCurve(pubKeyPoint.X(), pubKeyPoint.Y()) {
return "", types.AccAddress{}, errors.New("invalid points")
}
tssPubKey := btcec.PublicKey{
Curve: btcec.S256(),
X: pubKeyPoint.X(),
Y: pubKeyPoint.Y(),
}
compressedPubkey := coskey.PubKey{
Key: tssPubKey.SerializeCompressed(),
}
pubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, &compressedPubkey)
addr := types.AccAddress(compressedPubkey.Address().Bytes())
return pubKey, addr, err
}
func BytesToHashString(msg []byte) (string, error) {
h := sha256.New()
_, err := h.Write(msg)
if err != nil {
return "", fmt.Errorf("fail to caculate sha256 hash: %w", err)
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func GetThreshold(value int) (int, error) {
if value < 0 {
return 0, errors.New("negative input")
}
threshold := int(math.Ceil(float64(value)*2.0/3.0)) - 1
return threshold, nil
}