-
Notifications
You must be signed in to change notification settings - Fork 13
/
mixinnet_signedtransaction.go
60 lines (51 loc) · 1.28 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
package mixin
import (
"bytes"
"encoding/hex"
"github.com/fox-one/msgpack"
)
type (
TransactionV1 struct {
Transaction
Signatures [][]*Signature `json:"signatures,omitempty" msgpack:",omitempty"`
}
)
func (t *TransactionV1) DumpTransaction() (string, error) {
return t.Transaction.DumpTransaction()
}
func TransactionFromRaw(raw string) (*Transaction, error) {
bts, err := hex.DecodeString(raw)
if err != nil {
return nil, err
}
if !checkTxVersion(bts) {
return transactionV1FromRaw(bts)
}
return transactionV2FromRaw(bts)
}
func transactionV1FromRaw(bts []byte) (*Transaction, error) {
var tx TransactionV1
if err := msgpack.Unmarshal(bts, &tx); err != nil {
return nil, err
}
if len(tx.Signatures) > 0 {
tx.Transaction.Signatures = make([]map[uint16]*Signature, len(tx.Signatures))
for i, sigs := range tx.Signatures {
tx.Transaction.Signatures[i] = make(map[uint16]*Signature, len(sigs))
for k, sig := range sigs {
tx.Transaction.Signatures[i][uint16(k)] = sig
}
}
}
return &tx.Transaction, nil
}
func transactionV2FromRaw(bts []byte) (*Transaction, error) {
return NewDecoder(bts).DecodeTransaction()
}
func checkTxVersion(val []byte) bool {
if len(val) < 4 {
return false
}
v := append(magic, 0, TxVersion)
return bytes.Equal(v, val[:4])
}