-
Notifications
You must be signed in to change notification settings - Fork 12
/
output.go
470 lines (428 loc) · 11.9 KB
/
output.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package psetv2
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/vulpemventures/go-elements/internal/bufferutil"
)
const (
//Per output types: BIP 174, 370, 371
OutputRedeemScript = 0x00 //BIP 174
OutputWitnessScript = 0x01 //BIP 174
OutputBip32Derivation = 0x02 //BIP 174
OutputAmount = 0x03 //BIP 370
OutputScript = 0x04 //BIP 370
OutputTapInternalKey = 0x05 //BIP 371
OutputTapTree = 0x06 //BIP 371
OutputTapLeafScript = 0x06 //BIP 371 //TODO is duplicate key type allowed?
OutputTapBip32Derivation = 0x07 //BIP 371
//Elements Proprietary types
OutputValueCommitment = 0x01
OutputAsset = 0x02
OutputAssetCommitment = 0x03
OutputValueRangeproof = 0x04
OutputAssetSurjectionProof = 0x05
OutputBlindingPubkey = 0x06
OutputEcdhPubkey = 0x07
OutputBlinderIndex = 0x08
OutputBlindValueProof = 0x09
OutputBlindAssetProof = 0x0a
)
var (
ErrOutInvalidBlinding = fmt.Errorf(
"output is partially blinded while it must be either unblinded or " +
"fully blinded",
)
ErrOutInvalidBlinderIndexState = fmt.Errorf(
"blinder index must be unset for fully blinded output",
)
ErrOutInvalidValue = fmt.Errorf("invalid output value length")
ErrOutInvalidPubKey = fmt.Errorf("invalid output pubkey length")
ErrOutInvalidBlindingPubKey = fmt.Errorf(
"invalid output blinding pubkey length",
)
ErrOutInvalidBlinderIndex = fmt.Errorf("invalid output blinder index length")
ErrOutDuplicatedField = func(field string) error {
return fmt.Errorf("duplicated output %s", field)
}
)
type Output struct {
RedeemScript []byte
WitnessScript []byte
Bip32Derivation []DerivationPathWithPubKey
Value uint64
Script []byte
ValueCommitment []byte
Asset []byte
AssetCommitment []byte
ValueRangeproof []byte
AssetSurjectionProof []byte
BlindingPubkey []byte
EcdhPubkey []byte
BlinderIndex uint32
BlindValueProof []byte
BlindAssetProof []byte
ProprietaryData []ProprietaryData
Unknowns []KeyPair
}
func (o *Output) SanityCheck() error {
valueCommitSet := len(o.ValueCommitment) > 0
blindValueProofSet := len(o.BlindValueProof) > 0
if o.Value > 0 && valueCommitSet != blindValueProofSet {
return ErrOutMissingValueBlindProof
}
assetCommitSet := len(o.AssetCommitment) > 0
blindAssetProofSet := len(o.BlindAssetProof) > 0
if !assetCommitSet && len(o.Asset) == 0 {
return ErrOutMissingAsset
}
if len(o.Asset) > 0 && assetCommitSet != blindAssetProofSet {
return ErrOutMissingAssetBlindProof
}
if o.IsPartiallyBlinded() && !o.IsFullyBlinded() {
return ErrOutInvalidBlinding
}
if o.IsFullyBlinded() && o.BlinderIndex != 0 {
return ErrOutInvalidBlinderIndexState
}
return nil
}
func (o *Output) NeedsBlinding() bool {
return len(o.BlindingPubkey) > 0
}
func (o *Output) IsPartiallyBlinded() bool {
return (len(o.ValueCommitment) > 0 ||
len(o.AssetCommitment) > 0 ||
len(o.ValueRangeproof) > 0 ||
len(o.AssetSurjectionProof) > 0 ||
len(o.EcdhPubkey) > 0)
}
func (o *Output) IsFullyBlinded() bool {
return len(o.ValueCommitment) > 0 &&
len(o.AssetCommitment) > 0 &&
len(o.ValueRangeproof) > 0 &&
len(o.AssetSurjectionProof) > 0 &&
len(o.EcdhPubkey) > 0
}
func (o *Output) getKeyPairs() ([]KeyPair, error) {
keyPairs := make([]KeyPair, 0)
if len(o.RedeemScript) > 0 {
redeemScriptKeyPair := KeyPair{
Key: Key{
KeyType: OutputRedeemScript,
KeyData: nil,
},
Value: o.RedeemScript,
}
keyPairs = append(keyPairs, redeemScriptKeyPair)
}
if len(o.WitnessScript) > 0 {
witnessScriptKeyPair := KeyPair{
Key: Key{
KeyType: OutputWitnessScript,
KeyData: nil,
},
Value: o.WitnessScript,
}
keyPairs = append(keyPairs, witnessScriptKeyPair)
}
if len(o.Bip32Derivation) > 0 {
for _, v := range o.Bip32Derivation {
bip32DerivationPathKeyPair := KeyPair{
Key: Key{
KeyType: OutputBip32Derivation,
KeyData: v.PubKey,
},
Value: SerializeBIP32Derivation(v.MasterKeyFingerprint, v.Bip32Path),
}
keyPairs = append(keyPairs, bip32DerivationPathKeyPair)
}
}
outputAmountBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(outputAmountBytes, o.Value)
outputAmountKeyPair := KeyPair{
Key: Key{
KeyType: OutputAmount,
KeyData: nil,
},
Value: outputAmountBytes,
}
keyPairs = append(keyPairs, outputAmountKeyPair)
outputScriptKeyPair := KeyPair{
Key: Key{
KeyType: OutputScript,
KeyData: nil,
},
Value: o.Script,
}
keyPairs = append(keyPairs, outputScriptKeyPair)
if len(o.ValueCommitment) > 0 {
outputValueCommitmentKeyPair := KeyPair{
Key: Key{
KeyType: PsetProprietary,
KeyData: proprietaryKey(OutputValueCommitment, nil),
},
Value: o.ValueCommitment,
}
keyPairs = append(keyPairs, outputValueCommitmentKeyPair)
}
if len(o.AssetCommitment) > 0 {
outputAssetCommitmentKeyPair := KeyPair{
Key: Key{
KeyType: PsetProprietary,
KeyData: proprietaryKey(OutputAssetCommitment, nil),
},
Value: o.AssetCommitment,
}
keyPairs = append(keyPairs, outputAssetCommitmentKeyPair)
}
if len(o.Asset) > 0 {
outputAssetKeyPair := KeyPair{
Key: Key{
KeyType: PsetProprietary,
KeyData: proprietaryKey(OutputAsset, nil),
},
Value: o.Asset,
}
keyPairs = append(keyPairs, outputAssetKeyPair)
}
if len(o.ValueRangeproof) > 0 {
outputValueRangeproofKeyPair := KeyPair{
Key: Key{
KeyType: PsetProprietary,
KeyData: proprietaryKey(OutputValueRangeproof, nil),
},
Value: o.ValueRangeproof,
}
keyPairs = append(keyPairs, outputValueRangeproofKeyPair)
}
if len(o.AssetSurjectionProof) > 0 {
outputAssetSurjectionProofKeyPair := KeyPair{
Key: Key{
KeyType: PsetProprietary,
KeyData: proprietaryKey(OutputAssetSurjectionProof, nil),
},
Value: o.AssetSurjectionProof,
}
keyPairs = append(keyPairs, outputAssetSurjectionProofKeyPair)
}
if len(o.BlindingPubkey) > 0 {
outputBlindingPubkeyKeyPair := KeyPair{
Key: Key{
KeyType: PsetProprietary,
KeyData: proprietaryKey(OutputBlindingPubkey, nil),
},
Value: o.BlindingPubkey,
}
keyPairs = append(keyPairs, outputBlindingPubkeyKeyPair)
}
if len(o.EcdhPubkey) > 0 {
outputEcdhPubkeyKeyPair := KeyPair{
Key: Key{
KeyType: PsetProprietary,
KeyData: proprietaryKey(OutputEcdhPubkey, nil),
},
Value: o.EcdhPubkey,
}
keyPairs = append(keyPairs, outputEcdhPubkeyKeyPair)
}
outputBlinderIndexBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(outputBlinderIndexBytes, o.BlinderIndex)
outputBlinderIndexKeyPair := KeyPair{
Key: Key{
KeyType: PsetProprietary,
KeyData: proprietaryKey(OutputBlinderIndex, nil),
},
Value: outputBlinderIndexBytes,
}
keyPairs = append(keyPairs, outputBlinderIndexKeyPair)
if len(o.BlindValueProof) > 0 {
outputBlindValueProofKeyPair := KeyPair{
Key: Key{
KeyType: PsetProprietary,
KeyData: proprietaryKey(OutputBlindValueProof, nil),
},
Value: o.BlindValueProof,
}
keyPairs = append(keyPairs, outputBlindValueProofKeyPair)
}
if len(o.BlindAssetProof) > 0 {
outputBlindAssetProofKeyPair := KeyPair{
Key: Key{
KeyType: PsetProprietary,
KeyData: proprietaryKey(OutputBlindAssetProof, nil),
},
Value: o.BlindAssetProof,
}
keyPairs = append(keyPairs, outputBlindAssetProofKeyPair)
}
for _, v := range o.ProprietaryData {
kp := KeyPair{
Key: Key{
KeyType: PsetProprietary,
KeyData: proprietaryKey(v.Subtype, v.KeyData),
},
Value: v.Value,
}
keyPairs = append(keyPairs, kp)
}
keyPairs = append(keyPairs, o.Unknowns...)
return keyPairs, nil
}
func (o *Output) serialize(s *bufferutil.Serializer) error {
outputKeyPairs, err := o.getKeyPairs()
if err != nil {
return err
}
for _, v := range outputKeyPairs {
if err := v.serialize(s); err != nil {
return err
}
}
if err := s.WriteUint8(separator); err != nil {
return err
}
return nil
}
func (o *Output) deserialize(buf *bytes.Buffer) error {
kp := KeyPair{}
//read bytes and do the deserialization until separator is found at the
//end of global map
for {
if err := kp.deserialize(buf); err != nil {
if err == ErrNoMoreKeyPairs {
break
}
return err
}
switch kp.Key.KeyType {
case OutputRedeemScript:
if len(o.RedeemScript) > 0 {
return ErrOutDuplicatedField("redeem script")
}
o.RedeemScript = kp.Value
case OutputWitnessScript:
if len(o.WitnessScript) > 0 {
return ErrOutDuplicatedField("witness script")
}
o.WitnessScript = kp.Value
case OutputBip32Derivation:
if !validatePubkey(kp.Key.KeyData) {
return ErrOutInvalidPubKey
}
master, derivationPath, err := readBip32Derivation(kp.Value)
if err != nil {
return fmt.Errorf("invalid output bip32 derivation: %s", err)
}
// Duplicate keys are not allowed
for _, x := range o.Bip32Derivation {
if bytes.Equal(x.PubKey, kp.Key.KeyData) {
return ErrOutDuplicatedField("bip32 derivation")
}
}
o.Bip32Derivation = append(
o.Bip32Derivation,
DerivationPathWithPubKey{
PubKey: kp.Key.KeyData,
MasterKeyFingerprint: master,
Bip32Path: derivationPath,
},
)
case OutputAmount:
if o.Value != 0 {
return ErrOutDuplicatedField("value")
}
if len(kp.Value) != 8 {
return ErrOutInvalidValue
}
o.Value = binary.LittleEndian.Uint64(kp.Value)
case OutputScript:
if len(o.Script) > 0 {
return ErrOutDuplicatedField("script")
}
o.Script = kp.Value
case PsetProprietary:
pd := ProprietaryData{}
if err := pd.fromKeyPair(kp); err != nil {
return err
}
if bytes.Equal(pd.Identifier, magicPrefix) {
switch pd.Subtype {
case OutputValueCommitment:
if len(o.ValueCommitment) > 0 {
return ErrOutDuplicatedField("value commitment")
}
if len(kp.Value) != 33 {
return ErrOutInvalidValueCommitment
}
o.ValueCommitment = kp.Value
case OutputAsset:
if len(o.Asset) > 0 {
return ErrOutDuplicatedField("asset")
}
if len(kp.Value) != 32 {
return ErrOutInvalidAsset
}
o.Asset = kp.Value
case OutputAssetCommitment:
if len(o.AssetCommitment) > 0 {
return ErrOutDuplicatedField("asset commitment")
}
if len(kp.Value) != 33 {
return ErrOutInvalidAssetCommitment
}
o.AssetCommitment = kp.Value
case OutputValueRangeproof:
if len(o.ValueRangeproof) > 0 {
return ErrOutDuplicatedField("value range proof")
}
o.ValueRangeproof = kp.Value
case OutputAssetSurjectionProof:
if len(o.AssetSurjectionProof) > 0 {
return ErrOutDuplicatedField("asset surjection proof")
}
o.AssetSurjectionProof = kp.Value
case OutputBlindingPubkey:
if len(o.BlindingPubkey) > 0 {
return ErrOutDuplicatedField("blinding pubkey")
}
if !validatePubkey(kp.Value) {
return ErrOutInvalidBlindingPubKey
}
o.BlindingPubkey = kp.Value
case OutputEcdhPubkey:
if len(o.EcdhPubkey) > 0 {
return ErrOutDuplicatedField("ecdh pubkey")
}
if !validatePubkey(kp.Value) {
return ErrOutInvalidNonce
}
o.EcdhPubkey = kp.Value
case OutputBlinderIndex:
if o.BlinderIndex != 0 {
return ErrOutDuplicatedField("blinder index")
}
if len(kp.Value) != 4 {
return ErrOutInvalidBlinderIndex
}
o.BlinderIndex = binary.LittleEndian.Uint32(kp.Value)
case OutputBlindValueProof:
if len(o.BlindValueProof) > 0 {
return ErrOutDuplicatedField("blind value proof")
}
o.BlindValueProof = kp.Value
case OutputBlindAssetProof:
if len(o.BlindAssetProof) > 0 {
return ErrOutDuplicatedField("blind asset proof")
}
o.BlindAssetProof = kp.Value
default:
o.ProprietaryData = append(o.ProprietaryData, pd)
}
}
default:
o.Unknowns = append(o.Unknowns, kp)
}
}
return o.SanityCheck()
}