-
Notifications
You must be signed in to change notification settings - Fork 26
/
mswallet_base.go
132 lines (103 loc) · 3.13 KB
/
mswallet_base.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
//go:build !mobile
// +build !mobile
package zcncore
import (
"encoding/hex"
"fmt"
"time"
"github.com/0chain/errors"
"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/core/zcncrypto"
)
// MSVoteCallback callback definition multisig Vote function
type MSVoteCallback interface {
OnVoteComplete(status int, proposal string, err string)
}
// CreateMSWallet returns multisig wallet information
func CreateMSWallet(t, n int) (string, string, []string, error) {
if t < 1 || t > n {
return "", "", nil, errors.New("bls0_generate_threshold_key_shares", fmt.Sprintf("Given threshold (%d) is less than 1 or greater than numsigners (%d)", t, n))
}
id := 0
if _config.chain.SignatureScheme != "bls0chain" {
return "", "", nil, errors.New("", "encryption scheme for this blockchain is not bls0chain")
}
groupKey := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme)
wallet, err := groupKey.GenerateKeys()
if err != nil {
return "", "", nil, err
}
logging.Info(fmt.Sprintf("Wallet id: %s", wallet.ClientKey))
groupClientID := GetClientID(groupKey.GetPublicKey())
//Code modified to directly use BLS0ChainThresholdScheme
signerKeys, err := zcncrypto.GenerateThresholdKeyShares(t, n, groupKey)
if err != nil {
return "", "", nil, errors.Wrap(err, "Err in generateThresholdKeyShares")
}
var signerClientIDs []string
for _, key := range signerKeys {
signerClientIDs = append(signerClientIDs, GetClientID(key.GetPublicKey()))
}
msw := MSWallet{
Id: id,
SignatureScheme: _config.chain.SignatureScheme,
GroupClientID: groupClientID,
GroupKey: groupKey,
SignerClientIDs: signerClientIDs,
SignerKeys: signerKeys,
T: t,
N: n,
}
wallets, errw := getWallets(msw)
if errw != nil {
return "", "", nil, errw
}
smsw, er := msw.Marshal()
if er != nil {
return "", "", nil, er
}
return smsw, groupClientID, wallets, nil
}
func getWallets(msw MSWallet) ([]string, error) {
wallets := make([]string, 0, msw.N+1)
b0ss := msw.GroupKey
grw, err := makeWallet(b0ss.GetPrivateKey(), b0ss.GetPublicKey(), b0ss.GetMnemonic())
if err != nil {
return nil, err
}
wallets = append(wallets, grw)
for _, signer := range msw.SignerKeys {
w, err := makeWallet(signer.GetPrivateKey(), signer.GetPublicKey(), "")
if err != nil {
return nil, err
}
wallets = append(wallets, w)
}
return wallets, nil
}
func makeWallet(privateKey, publicKey, mnemonic string) (string, error) {
w := &zcncrypto.Wallet{}
w.Keys = make([]zcncrypto.KeyPair, 1)
w.Keys[0].PrivateKey = privateKey
w.Keys[0].PublicKey = publicKey
w.ClientID = GetClientID(publicKey) //VerifyThis
w.ClientKey = publicKey
w.Mnemonic = mnemonic
w.Version = zcncrypto.CryptoVersion
w.DateCreated = time.Now().Format(time.RFC3339)
return w.Marshal()
}
// GetClientID -- computes Client ID from publickey
func GetClientID(pkey string) string {
publicKeyBytes, err := hex.DecodeString(pkey)
if err != nil {
panic(err)
}
return encryption.Hash(publicKeyBytes)
}
func GetClientWalletKey() string {
return _config.wallet.ClientKey
}
func GetClientWalletID() string {
return _config.wallet.ClientID
}