/
transaction.go
363 lines (318 loc) 路 9.18 KB
/
transaction.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package common
import (
"crypto/sha512"
"encoding/binary"
"fmt"
"filippo.io/edwards25519"
"github.com/MixinNetwork/mixin/crypto"
)
const (
TxVersionHashSignature = 0x05
ExtraSizeGeneralLimit = 256
ExtraSizeStorageStep = 1024
ExtraSizeStorageCapacity = 1024 * 1024 * 4
ExtraStoragePriceStep = "0.0001"
SliceCountLimit = 256
ReferencesCountLimit = 2
OutputTypeScript = 0x00
OutputTypeWithdrawalSubmit = 0xa1
OutputTypeNodePledge = 0xa3
OutputTypeNodeAccept = 0xa4
outputTypeNodeResign = 0xa5
OutputTypeNodeRemove = 0xa6
OutputTypeWithdrawalClaim = 0xa9
OutputTypeNodeCancel = 0xaa
OutputTypeCustodianUpdateNodes = 0xb1
OutputTypeCustodianSlashNodes = 0xb2
TransactionTypeScript = 0x00
TransactionTypeMint = 0x01
TransactionTypeDeposit = 0x02
TransactionTypeWithdrawalSubmit = 0x03
TransactionTypeWithdrawalClaim = 0x05
TransactionTypeNodePledge = 0x06
TransactionTypeNodeAccept = 0x07
transactionTypeNodeResign = 0x08
TransactionTypeNodeRemove = 0x09
TransactionTypeNodeCancel = 0x12
TransactionTypeCustodianUpdateNodes = 0x13
TransactionTypeCustodianSlashNodes = 0x14
TransactionTypeUnknown = 0xff
)
type Input struct {
Hash crypto.Hash
Index uint
Genesis []byte
Deposit *DepositData
Mint *MintData
}
type Output struct {
Type uint8
Amount Integer
Keys []*crypto.Key
Mask crypto.Key
Script Script
Withdrawal *WithdrawalData
}
type Transaction struct {
Version uint8
Asset crypto.Hash
Inputs []*Input
Outputs []*Output
References []crypto.Hash
Extra []byte
}
type SignedTransaction struct {
Transaction
AggregatedSignature *AggregatedSignature
SignaturesMap []map[uint16]*crypto.Signature
}
func (tx *Transaction) ViewGhostKey(a *crypto.Key) []*Output {
outputs := make([]*Output, 0)
for i, o := range tx.Outputs {
if o.Type != OutputTypeScript {
continue
}
out := &Output{
Type: o.Type,
Amount: o.Amount,
Script: o.Script,
Mask: o.Mask,
}
for _, k := range o.Keys {
key := crypto.ViewGhostOutputKey(k, a, &o.Mask, uint64(i))
out.Keys = append(out.Keys, key)
}
outputs = append(outputs, out)
}
return outputs
}
func (tx *SignedTransaction) TransactionType() uint8 {
for _, in := range tx.Inputs {
if in.Mint != nil {
return TransactionTypeMint
}
if in.Deposit != nil {
return TransactionTypeDeposit
}
if in.Genesis != nil {
return TransactionTypeUnknown
}
}
isScript := true
for _, out := range tx.Outputs {
switch out.Type {
case OutputTypeWithdrawalSubmit:
return TransactionTypeWithdrawalSubmit
case OutputTypeWithdrawalClaim:
return TransactionTypeWithdrawalClaim
case OutputTypeNodePledge:
return TransactionTypeNodePledge
case OutputTypeNodeCancel:
return TransactionTypeNodeCancel
case OutputTypeNodeAccept:
return TransactionTypeNodeAccept
case OutputTypeNodeRemove:
return TransactionTypeNodeRemove
case OutputTypeCustodianUpdateNodes:
return TransactionTypeCustodianUpdateNodes
case OutputTypeCustodianSlashNodes:
return TransactionTypeCustodianSlashNodes
}
isScript = isScript && out.Type == OutputTypeScript
}
if isScript {
return TransactionTypeScript
}
return TransactionTypeUnknown
}
func (signed *SignedTransaction) SignUTXO(utxo *UTXO, accounts []*Address) error {
msg := signed.AsVersioned().PayloadHash()
if len(accounts) == 0 {
return nil
}
keysFilter := make(map[string]uint16)
for i, k := range utxo.Keys {
keysFilter[k.String()] = uint16(i)
}
sigs := make(map[uint16]*crypto.Signature)
for _, acc := range accounts {
priv := crypto.DeriveGhostPrivateKey(&utxo.Mask, &acc.PrivateViewKey, &acc.PrivateSpendKey, uint64(utxo.Index))
i, found := keysFilter[priv.Public().String()]
if !found {
return fmt.Errorf("invalid key for the input %s", acc.String())
}
sig := priv.Sign(msg)
sigs[i] = &sig
}
signed.SignaturesMap = append(signed.SignaturesMap, sigs)
return nil
}
func (signed *SignedTransaction) SignInput(reader UTXOKeysReader, index int, accounts []*Address) error {
if len(accounts) == 0 {
return nil
}
if index >= len(signed.Inputs) {
return fmt.Errorf("invalid input index %d/%d", index, len(signed.Inputs))
}
in := signed.Inputs[index]
if in.Deposit != nil || in.Mint != nil {
return signed.SignRaw(accounts[0].PrivateSpendKey)
}
utxo, err := reader.ReadUTXOKeys(in.Hash, in.Index)
if err != nil {
return err
}
if utxo == nil {
return fmt.Errorf("input not found %s:%d", in.Hash.String(), in.Index)
}
keysFilter := make(map[string]uint16)
for i, k := range utxo.Keys {
keysFilter[k.String()] = uint16(i)
}
sigs := make(map[uint16]*crypto.Signature)
msg := signed.AsVersioned().PayloadHash()
for _, acc := range accounts {
priv := crypto.DeriveGhostPrivateKey(&utxo.Mask, &acc.PrivateViewKey, &acc.PrivateSpendKey, uint64(in.Index))
i, found := keysFilter[priv.Public().String()]
if !found {
return fmt.Errorf("invalid key for the input %s", acc.String())
}
sig := priv.Sign(msg)
sigs[i] = &sig
}
signed.SignaturesMap = append(signed.SignaturesMap, sigs)
return nil
}
func (signed *SignedTransaction) SignRaw(key crypto.Key) error {
msg := signed.AsVersioned().PayloadHash()
if len(signed.Inputs) != 1 {
return fmt.Errorf("invalid inputs count %d", len(signed.Inputs))
}
in := signed.Inputs[0]
if in.Deposit == nil && in.Mint == nil {
return fmt.Errorf("invalid input format")
}
sig := key.Sign(msg)
sigs := map[uint16]*crypto.Signature{0: &sig}
signed.SignaturesMap = append(signed.SignaturesMap, sigs)
return nil
}
func (signed *SignedTransaction) AggregateSign(reader UTXOKeysReader, accounts [][]*Address, seed []byte) error {
var signers []int
var randoms []*crypto.Key
var pubKeys, privKeys []*crypto.Key
for index, in := range signed.Inputs {
utxo, err := reader.ReadUTXOKeys(in.Hash, in.Index)
if err != nil {
return err
}
if utxo == nil {
return fmt.Errorf("input not found %s:%d", in.Hash.String(), in.Index)
}
keysFilter := make(map[string]int)
for i, k := range utxo.Keys {
keysFilter[k.String()] = i
}
for _, acc := range accounts[index] {
priv := crypto.DeriveGhostPrivateKey(&utxo.Mask, &acc.PrivateViewKey, &acc.PrivateSpendKey, uint64(in.Index))
i, found := keysFilter[priv.Public().String()]
if !found {
return fmt.Errorf("invalid key for the input %s", acc.String())
}
m := len(pubKeys) + i
if sl := len(signers); sl > 0 && m <= signers[sl-1] {
return fmt.Errorf("invalid signers order %d %d", signers[sl-1], m)
}
signers = append(signers, m)
privKeys = append(privKeys, priv)
}
pubKeys = append(pubKeys, utxo.Keys...)
}
P := edwards25519.NewIdentityPoint()
A := edwards25519.NewIdentityPoint()
for _, m := range signers {
buf := binary.BigEndian.AppendUint16(seed, uint16(m))
s := crypto.Blake3Hash(buf)
r := crypto.NewKeyFromSeed(append(s[:], s[:]...))
randoms = append(randoms, &r)
R := r.Public()
p, err := edwards25519.NewIdentityPoint().SetBytes(R[:])
if err != nil {
return err
}
P = P.Add(P, p)
pub := pubKeys[m]
a, err := edwards25519.NewIdentityPoint().SetBytes(pub[:])
if err != nil {
return err
}
A = A.Add(A, a)
}
var hramDigest [64]byte
msg := signed.AsVersioned().PayloadHash()
h := sha512.New()
h.Write(P.Bytes())
h.Write(A.Bytes())
h.Write(msg[:])
h.Sum(hramDigest[:0])
x, err := edwards25519.NewScalar().SetUniformBytes(hramDigest[:])
if err != nil {
return err
}
S := edwards25519.NewScalar()
for i, k := range privKeys {
y, err := edwards25519.NewScalar().SetCanonicalBytes(k[:])
if err != nil {
panic(k.String())
}
z, err := edwards25519.NewScalar().SetCanonicalBytes(randoms[i][:])
if err != nil {
panic(randoms[i].String())
}
s := edwards25519.NewScalar().MultiplyAdd(x, y, z)
S = S.Add(S, s)
}
as := &AggregatedSignature{Signers: signers}
copy(as.Signature[:32], P.Bytes())
copy(as.Signature[32:], S.Bytes())
signed.AggregatedSignature = as
return nil
}
func NewTransactionV5(asset crypto.Hash) *Transaction {
return &Transaction{
Version: TxVersionHashSignature,
Asset: asset,
}
}
func (tx *Transaction) AddInput(hash crypto.Hash, index uint) {
in := &Input{
Hash: hash,
Index: index,
}
tx.Inputs = append(tx.Inputs, in)
}
func (tx *Transaction) AddOutputWithType(ot uint8, accounts []*Address, s Script, amount Integer, seed []byte) {
out := &Output{
Type: ot,
Amount: amount,
Script: s,
Keys: make([]*crypto.Key, 0),
}
if len(accounts) > 0 {
r := crypto.NewKeyFromSeed(seed)
out.Mask = r.Public()
for _, a := range accounts {
k := crypto.DeriveGhostPublicKey(&r, &a.PublicViewKey, &a.PublicSpendKey, uint64(len(tx.Outputs)))
out.Keys = append(out.Keys, k)
}
}
tx.Outputs = append(tx.Outputs, out)
}
func (tx *Transaction) AddScriptOutput(accounts []*Address, s Script, amount Integer, seed []byte) {
tx.AddOutputWithType(OutputTypeScript, accounts, s, amount, seed)
}
func (tx *Transaction) AddRandomScriptOutput(accounts []*Address, s Script, amount Integer) {
seed := make([]byte, 64)
crypto.ReadRand(seed)
tx.AddScriptOutput(accounts, s, amount, seed)
}