-
Notifications
You must be signed in to change notification settings - Fork 127
/
version.go
142 lines (126 loc) · 2.94 KB
/
version.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
package common
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/crypto"
)
type VersionedTransaction struct {
SignedTransaction
pmbytes []byte
hash crypto.Hash
}
func (tx *SignedTransaction) AsVersioned() *VersionedTransaction {
if tx.Version < TxVersionHashSignature {
panic(tx.Version)
}
return &VersionedTransaction{
SignedTransaction: *tx,
}
}
func (tx *Transaction) AsVersioned() *VersionedTransaction {
if tx.Version < TxVersionHashSignature {
panic(tx.Version)
}
return &VersionedTransaction{
SignedTransaction: SignedTransaction{Transaction: *tx},
}
}
func UnmarshalVersionedTransaction(val []byte) (*VersionedTransaction, error) {
return unmarshalVersionedTransaction(val)
}
func (ver *VersionedTransaction) Marshal() []byte {
val := ver.marshal()
if config.Debug {
ret, err := unmarshalVersionedTransaction(val)
if err != nil {
panic(err)
}
retv := ret.marshal()
if !bytes.Equal(retv, val) {
panic(fmt.Errorf("malformed %s %s", hex.EncodeToString(val), hex.EncodeToString(retv)))
}
}
return val
}
func (ver *VersionedTransaction) PayloadMarshal() []byte {
if len(ver.pmbytes) > 0 {
return ver.pmbytes
}
val := ver.payloadMarshal()
if config.Debug {
ret, err := unmarshalVersionedTransaction(val)
if err != nil {
panic(err)
}
retv := ret.payloadMarshal()
if !bytes.Equal(retv, val) {
panic(fmt.Errorf("malformed %s %s", hex.EncodeToString(val), hex.EncodeToString(retv)))
}
}
ver.pmbytes = val
return ver.pmbytes
}
func (ver *VersionedTransaction) PayloadHash() crypto.Hash {
if !ver.hash.HasValue() {
ver.hash = crypto.Blake3Hash(ver.PayloadMarshal())
}
return ver.hash
}
func checkTxVersion(val []byte) uint8 {
if len(val) < 4 {
return 0
}
for _, i := range []byte{
TxVersionHashSignature,
} {
v := append(magic, 0, i)
if bytes.Equal(v, val[:4]) {
return i
}
}
return 0
}
func checkSnapVersion(val []byte) uint8 {
if len(val) < 4 {
return 0
}
for _, i := range []byte{
SnapshotVersionCommonEncoding,
} {
v := append(magic, 0, i)
if bytes.Equal(v, val[:4]) {
return i
}
}
return 0
}
func unmarshalVersionedTransaction(val []byte) (*VersionedTransaction, error) {
if len(val) > config.TransactionMaximumSize {
return nil, fmt.Errorf("transaction too large %d", len(val))
}
signed, err := NewDecoder(val).DecodeTransaction()
if err != nil {
return nil, err
}
ver := &VersionedTransaction{SignedTransaction: *signed}
return ver, nil
}
func (ver *VersionedTransaction) marshal() []byte {
switch ver.Version {
case TxVersionHashSignature:
return NewEncoder().EncodeTransaction(&ver.SignedTransaction)
default:
panic(ver.Version)
}
}
func (ver *VersionedTransaction) payloadMarshal() []byte {
switch ver.Version {
case TxVersionHashSignature:
signed := &SignedTransaction{Transaction: ver.Transaction}
return NewEncoder().EncodeTransaction(signed)
default:
panic(ver.Version)
}
}