-
Notifications
You must be signed in to change notification settings - Fork 12
/
mixinnet_signedtransaction.go
67 lines (57 loc) · 1.39 KB
/
mixinnet_signedtransaction.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
package mixin
import (
"bytes"
"encoding/hex"
"errors"
"github.com/fox-one/msgpack"
)
type (
SignedTransactionV1 struct {
Transaction
Signatures [][]*Signature `json:"signatures,omitempty" msgpack:",omitempty"`
}
SignedTransactionV2 struct {
Transaction
Signatures []map[uint16]*Signature `json:"signatures,omitempty" msgpack:",omitempty"`
}
)
func (t *SignedTransactionV1) DumpTransaction() (string, error) {
t.Transaction.Signatures = t.Signatures
return t.Transaction.DumpTransaction()
}
func (t *SignedTransactionV2) DumpTransaction() (string, error) {
t.Transaction.Signatures = t.Signatures
return t.Transaction.DumpTransaction()
}
func TransactionFromRaw(raw string) (*Transaction, error) {
bts, err := hex.DecodeString(raw)
if err != nil {
return nil, err
}
if len(bts) > 4 {
switch bts[3] {
case 0, 1:
var tx SignedTransactionV1
if err := msgpack.Unmarshal(bts, &tx); err != nil {
return nil, err
}
tx.Transaction.Signatures = tx.Signatures
return &tx.Transaction, nil
case 2:
tx, err := NewDecoder(bts).DecodeTransaction()
if err != nil {
return nil, err
}
tx.Transaction.Signatures = tx.Signatures
return &tx.Transaction, nil
}
}
return nil, errors.New("invalid transaction data")
}
func checkTxVersion(val []byte) bool {
if len(val) < 4 {
return false
}
v := append(magic, 0, TxVersion)
return bytes.Equal(v, val[:4])
}