This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
/
decoder.go
199 lines (184 loc) · 4.55 KB
/
decoder.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package chain
import (
"fmt"
"strconv"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/spacesvm/tdata"
)
const (
Claim = "claim"
Lifeline = "lifeline"
Set = "set"
Delete = "delete"
Move = "move"
Transfer = "transfer"
// Non-user created event
Reward = "reward"
)
type Input struct {
Typ string `json:"type"`
Space string `json:"space"`
Key string `json:"key"`
Value []byte `json:"value"`
To common.Address `json:"to"`
Units uint64 `json:"units"`
}
func (i *Input) Decode() (UnsignedTransaction, error) {
switch i.Typ {
case Claim:
return &ClaimTx{
BaseTx: &BaseTx{},
Space: i.Space,
}, nil
case Lifeline:
return &LifelineTx{
BaseTx: &BaseTx{},
Space: i.Space,
Units: i.Units,
}, nil
case Set:
return &SetTx{
BaseTx: &BaseTx{},
Space: i.Space,
Key: i.Key,
Value: i.Value,
}, nil
case Delete:
return &DeleteTx{
BaseTx: &BaseTx{},
Space: i.Space,
Key: i.Key,
}, nil
case Move:
return &MoveTx{
BaseTx: &BaseTx{},
Space: i.Space,
To: i.To,
}, nil
case Transfer:
return &TransferTx{
BaseTx: &BaseTx{},
To: i.To,
Units: i.Units,
}, nil
default:
return nil, ErrInvalidType
}
}
const (
tdString = "string"
tdUint64 = "uint64"
tdBytes = "bytes"
tdAddress = "address"
tdBlockID = "blockID"
tdPrice = "price"
tdSpace = "space"
tdKey = "key"
tdValue = "value"
tdUnits = "units"
tdTo = "to"
)
func parseUint64Message(td *tdata.TypedData, k string) (uint64, error) {
r, ok := td.Message[k].(string)
if !ok {
return 0, fmt.Errorf("%w: %s", ErrTypedDataKeyMissing, k)
}
return strconv.ParseUint(r, 10, 64)
}
func parseBaseTx(td *tdata.TypedData) (*BaseTx, error) {
rblockID, ok := td.Message[tdBlockID].(string)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrTypedDataKeyMissing, tdBlockID)
}
blockID, err := ids.FromString(rblockID)
if err != nil {
return nil, err
}
magic, err := strconv.ParseUint(td.Domain.Magic, 10, 64)
if err != nil {
return nil, err
}
price, err := parseUint64Message(td, tdPrice)
if err != nil {
return nil, err
}
return &BaseTx{BlockID: blockID, Magic: magic, Price: price}, nil
}
func ParseTypedData(td *tdata.TypedData) (UnsignedTransaction, error) {
bTx, err := parseBaseTx(td)
if err != nil {
return nil, err
}
switch td.PrimaryType {
case Claim:
space, ok := td.Message[tdSpace].(string)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrTypedDataKeyMissing, tdSpace)
}
return &ClaimTx{BaseTx: bTx, Space: space}, nil
case Lifeline:
space, ok := td.Message[tdSpace].(string)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrTypedDataKeyMissing, tdSpace)
}
units, err := parseUint64Message(td, tdUnits)
if err != nil {
return nil, err
}
return &LifelineTx{BaseTx: bTx, Space: space, Units: units}, nil
case Set:
space, ok := td.Message[tdSpace].(string)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrTypedDataKeyMissing, tdSpace)
}
key, ok := td.Message[tdKey].(string)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrTypedDataKeyMissing, tdKey)
}
rvalue, ok := td.Message[tdValue].(string)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrTypedDataKeyMissing, tdValue)
}
value, err := hexutil.Decode(rvalue)
if err != nil {
return nil, err
}
return &SetTx{BaseTx: bTx, Space: space, Key: key, Value: value}, nil
case Delete:
space, ok := td.Message[tdSpace].(string)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrTypedDataKeyMissing, tdSpace)
}
key, ok := td.Message[tdKey].(string)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrTypedDataKeyMissing, tdKey)
}
return &DeleteTx{BaseTx: bTx, Space: space, Key: key}, nil
case Move:
space, ok := td.Message[tdSpace].(string)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrTypedDataKeyMissing, tdSpace)
}
to, ok := td.Message[tdTo].(string)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrTypedDataKeyMissing, tdTo)
}
return &MoveTx{BaseTx: bTx, Space: space, To: common.HexToAddress(to)}, nil
case Transfer:
to, ok := td.Message[tdTo].(string)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrTypedDataKeyMissing, tdTo)
}
units, err := parseUint64Message(td, tdUnits)
if err != nil {
return nil, err
}
return &TransferTx{BaseTx: bTx, To: common.HexToAddress(to), Units: units}, nil
default:
return nil, ErrInvalidType
}
}