/
tool.go
92 lines (83 loc) · 2.52 KB
/
tool.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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"os"
"github.com/btcsuite/btcd/btcec"
coskey "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
bech32 "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32"
"github.com/zeta-chain/tss-lib/ecdsa/keygen"
)
type (
KeygenLocalState struct {
PubKey string `json:"pub_key"`
LocalData keygen.LocalPartySaveData `json:"local_data"`
ParticipantKeys []string `json:"participant_keys"` // the paticipant of last key gen
LocalPartyKey string `json:"local_party_key"`
}
)
func getTssSecretFile(file string) (KeygenLocalState, error) {
_, err := os.Stat(file)
if err != nil {
return KeygenLocalState{}, err
}
buf, err := ioutil.ReadFile(file)
if err != nil {
return KeygenLocalState{}, fmt.Errorf("file to read from file(%s): %w", file, err)
}
var localState KeygenLocalState
if err := json.Unmarshal(buf, &localState); nil != err {
return KeygenLocalState{}, fmt.Errorf("fail to unmarshal KeygenLocalState: %w", err)
}
return localState, nil
}
func setupBech32Prefix() {
config := sdk.GetConfig()
// thorchain will import go-tss as a library , thus this is not needed, we copy the prefix here to avoid go-tss to import thorchain
config.SetBech32PrefixForAccount("thor", "thorpub")
config.SetBech32PrefixForValidator("thorv", "thorvpub")
config.SetBech32PrefixForConsensusNode("thorc", "thorcpub")
}
func getTssPubKey(x, y *big.Int) (string, sdk.AccAddress, error) {
if x == nil || y == nil {
return "", sdk.AccAddress{}, errors.New("invalid points")
}
tssPubKey := btcec.PublicKey{
Curve: btcec.S256(),
X: x,
Y: y,
}
pubKeyCompressed := coskey.PubKey{
Key: tssPubKey.SerializeCompressed(),
}
pubKey, err := bech32.MarshalPubKey(bech32.AccPK, &pubKeyCompressed)
addr := sdk.AccAddress(pubKeyCompressed.Address().Bytes())
return pubKey, addr, err
}
func aesCTRXOR(key, inText, iv []byte) ([]byte, error) {
// AES-128 is selected due to size of encryptKey.
aesBlock, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
stream := cipher.NewCTR(aesBlock, iv)
outText := make([]byte, len(inText))
stream.XORKeyStream(outText, inText)
return outText, err
}
func generateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return nil, err
}
return b, nil
}