/
signer.go
332 lines (276 loc) · 7.45 KB
/
signer.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package util
import (
"encoding/hex"
"strconv"
"strings"
"github.com/dan13ram/wpokt-validator/app"
"github.com/dan13ram/wpokt-validator/models"
pokt "github.com/pokt-network/pocket-core/app"
"github.com/pokt-network/pocket-core/crypto"
sdk "github.com/pokt-network/pocket-core/types"
"github.com/pokt-network/pocket-core/x/auth"
authTypes "github.com/pokt-network/pocket-core/x/auth/types"
nodeTypes "github.com/pokt-network/pocket-core/x/nodes/types"
"github.com/tendermint/tendermint/libs/rand"
)
var txEncoder sdk.TxEncoder = auth.DefaultTxEncoder(pokt.Codec())
var txDecoder sdk.TxDecoder = auth.DefaultTxDecoder(pokt.Codec())
func buildMultiSigTxAndSign(
toAddr string,
memo string,
chainID string,
amount int64,
fees int64,
signerKey crypto.PrivateKey,
multisigKey crypto.PublicKeyMultiSig,
) ([]byte, error) {
fa, err := sdk.AddressFromHex(multisigKey.Address().String())
if err != nil {
return nil, err
}
ta, err := sdk.AddressFromHex(toAddr)
if err != nil {
return nil, err
}
m := &nodeTypes.MsgSend{
FromAddress: fa,
ToAddress: ta,
Amount: sdk.NewInt(amount),
}
entropy := rand.Int64()
fee := sdk.NewCoins(sdk.NewCoin(sdk.DefaultStakeDenom, sdk.NewInt(fees)))
signBz, err := authTypes.StdSignBytes(chainID, entropy, fee, m, memo)
if err != nil {
return nil, err
}
sigBytes, err := signerKey.Sign(signBz)
if err != nil {
return nil, err
}
// sign using multisignature structure
var ms = crypto.MultiSig(crypto.MultiSignature{})
ms = ms.NewMultiSignature()
// loop through all the keys and add signatures
for i := 0; i < len(multisigKey.Keys()); i++ {
ms = ms.AddSignatureByIndex(sigBytes, i)
// when new signatures are added they will replace the old ones
}
sig := authTypes.StdSignature{
PublicKey: multisigKey,
Signature: ms.Marshal(),
}
// create a new standard transaction object
tx := authTypes.NewTx(m, fee, sig, memo, entropy)
// encode it using the default encoder
return txEncoder(tx, -1)
}
func decodeTx(txHex string, chainID string) (authTypes.StdTx, []byte, error) {
bz, err := hex.DecodeString(txHex)
if err != nil {
return authTypes.StdTx{}, nil, err
}
t, err := txDecoder(bz, -1)
if err != nil {
return authTypes.StdTx{}, nil, err
}
tx := t.(authTypes.StdTx)
bytesToSign, err := authTypes.StdSignBytes(chainID, tx.GetEntropy(), tx.GetFee(), tx.GetMsg(), tx.GetMemo())
if err != nil {
return authTypes.StdTx{}, nil, err
}
return tx, bytesToSign, nil
}
func signMultisigTx(
txHex string,
chainID string,
signerKey crypto.PrivateKey,
multisigKey crypto.PublicKeyMultiSig,
) ([]byte, error) {
tx, bytesToSign, err := decodeTx(txHex, chainID)
if err != nil {
return nil, err
}
sigBytes, err := signerKey.Sign(bytesToSign)
if err != nil {
return nil, err
}
var ms = crypto.MultiSig(crypto.MultiSignature{})
if tx.GetSignature().GetSignature() == nil || len(tx.GetSignature().GetSignature()) == 0 {
ms = ms.NewMultiSignature()
} else {
ms = ms.Unmarshal(tx.GetSignature().GetSignature())
}
ms, err = ms.AddSignature(
sigBytes,
signerKey.PublicKey(),
multisigKey.Keys(),
)
if err != nil {
return nil, err
}
sig := authTypes.StdSignature{
PublicKey: tx.Signature.PublicKey,
Signature: ms.Marshal(),
}
// replace the old multi-signature with the new multi-signature (containing the additional signature)
tx, err = tx.WithSignature(sig)
if err != nil {
return nil, err
}
// encode using the standard encoder
return txEncoder(tx, -1)
}
func UpdateStatusAndConfirmationsForInvalidMint(doc *models.InvalidMint, currentHeight int64) (*models.InvalidMint, error) {
status := doc.Status
confirmations, err := strconv.ParseInt(doc.Confirmations, 10, 64)
if err != nil || confirmations < 0 {
confirmations = 0
}
if status == models.StatusPending || confirmations == 0 {
status = models.StatusPending
if app.Config.Pocket.Confirmations == 0 {
status = models.StatusConfirmed
} else {
mintHeight, err := strconv.ParseInt(doc.Height, 10, 64)
if err != nil {
return doc, err
}
confirmations = currentHeight - mintHeight
if confirmations >= app.Config.Pocket.Confirmations {
status = models.StatusConfirmed
}
}
}
doc.Status = status
doc.Confirmations = strconv.FormatInt(confirmations, 10)
return doc, nil
}
func SignInvalidMint(
doc *models.InvalidMint,
privateKey crypto.PrivateKey,
multisigPubKey crypto.PublicKeyMultiSig,
numSigners int,
) (*models.InvalidMint, error) {
returnTx := doc.ReturnTx
signers := doc.Signers
if signers == nil || returnTx == "" || len(signers) == 0 {
signers = []string{}
returnTx = ""
}
if returnTx == "" {
amountWithTxFee, err := strconv.ParseInt(doc.Amount, 10, 64)
if err != nil {
return doc, err
}
amount := amountWithTxFee - app.Config.Pocket.TxFee
memo := doc.TransactionHash
returnTxBytes, err := buildMultiSigTxAndSign(
doc.SenderAddress,
memo,
app.Config.Pocket.ChainId,
amount,
app.Config.Pocket.TxFee,
privateKey,
multisigPubKey,
)
if err != nil {
return doc, err
}
returnTx = hex.EncodeToString(returnTxBytes)
} else {
returnTxBytes, err := signMultisigTx(
returnTx,
app.Config.Pocket.ChainId,
privateKey,
multisigPubKey,
)
if err != nil {
return doc, err
}
returnTx = hex.EncodeToString(returnTxBytes)
}
signers = append(signers, strings.ToLower(privateKey.PublicKey().RawString()))
if len(signers) == numSigners {
doc.Status = models.StatusSigned
}
doc.ReturnTx = returnTx
doc.Signers = signers
return doc, nil
}
func UpdateStatusAndConfirmationsForBurn(doc *models.Burn, blockNumber int64) (*models.Burn, error) {
status := doc.Status
confirmations, err := strconv.ParseInt(doc.Confirmations, 10, 64)
if err != nil || confirmations < 0 {
confirmations = 0
}
if status == models.StatusPending || confirmations == 0 {
status = models.StatusPending
if app.Config.Ethereum.Confirmations == 0 {
status = models.StatusConfirmed
} else {
burnBlockNumber, err := strconv.ParseInt(doc.BlockNumber, 10, 64)
if err != nil {
return doc, err
}
confirmations = blockNumber - burnBlockNumber
if confirmations >= app.Config.Ethereum.Confirmations {
status = models.StatusConfirmed
}
}
}
doc.Status = status
doc.Confirmations = strconv.FormatInt(confirmations, 10)
return doc, nil
}
func SignBurn(
doc *models.Burn,
privateKey crypto.PrivateKey,
multisigPubKey crypto.PublicKeyMultiSig,
numSigners int,
) (*models.Burn, error) {
signers := doc.Signers
returnTx := doc.ReturnTx
if signers == nil || returnTx == "" || len(signers) == 0 {
signers = []string{}
returnTx = ""
}
if returnTx == "" {
amountWithTxFee, err := strconv.ParseInt(doc.Amount, 10, 64)
if err != nil {
return doc, err
}
amount := amountWithTxFee - app.Config.Pocket.TxFee
memo := doc.TransactionHash
returnTxBytes, err := buildMultiSigTxAndSign(
doc.RecipientAddress,
memo,
app.Config.Pocket.ChainId,
amount,
app.Config.Pocket.TxFee,
privateKey,
multisigPubKey,
)
if err != nil {
return doc, err
}
returnTx = hex.EncodeToString(returnTxBytes)
} else {
returnTxBytes, err := signMultisigTx(
returnTx,
app.Config.Pocket.ChainId,
privateKey,
multisigPubKey,
)
if err != nil {
return doc, err
}
returnTx = hex.EncodeToString(returnTxBytes)
}
signers = append(signers, strings.ToLower(privateKey.PublicKey().RawString()))
if len(signers) == numSigners {
doc.Status = models.StatusSigned
}
doc.Signers = signers
doc.ReturnTx = returnTx
return doc, nil
}