-
Notifications
You must be signed in to change notification settings - Fork 170
/
account_encoder.go
207 lines (169 loc) · 4.95 KB
/
account_encoder.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
package flow
// TODO: these functions will be moved to a separate `encoding` package in a future PR
import (
"github.com/ethereum/go-ethereum/rlp"
"github.com/onflow/cadence"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/crypto/hash"
)
// accountPublicKeyWrapper is used for encoding and decoding.
type accountPublicKeyWrapper struct {
PublicKey []byte
SignAlgo uint
HashAlgo uint
Weight uint
SeqNumber uint64
Revoked bool
}
// legacyAccountPublicKeyWrapper is an RLP wrapper for the old public key format that does
// not contain a Revoked field.
type legacyAccountPublicKeyWrapper struct {
PublicKey []byte
SignAlgo uint
HashAlgo uint
Weight uint
SeqNumber uint64
}
// runtimeAccountPublicKeyWrapper must match format used in Transaction
// currently should match SDK AccountKey encoded format
type runtimeAccountPublicKeyWrapper struct {
PublicKey []byte
SignAlgo uint
HashAlgo uint
Weight uint
}
// accountPrivateKeyWrapper is used for encoding and decoding.
type accountPrivateKeyWrapper struct {
PrivateKey []byte
SignAlgo uint
HashAlgo uint
}
func EncodeAccountPublicKey(a AccountPublicKey) ([]byte, error) {
w := accountPublicKeyWrapper{
PublicKey: a.PublicKey.Encode(),
SignAlgo: uint(a.SignAlgo),
HashAlgo: uint(a.HashAlgo),
Weight: uint(a.Weight),
SeqNumber: a.SeqNumber,
Revoked: a.Revoked,
}
return rlp.EncodeToBytes(&w)
}
func EncodeRuntimeAccountPublicKeys(keys []AccountPublicKey) ([]cadence.Value, error) {
encodedKeys := make([]cadence.Value, len(keys))
for i, key := range keys {
k, err := EncodeRuntimeAccountPublicKey(key)
if err != nil {
return nil, err
}
values := make([]cadence.Value, len(k))
for j, v := range k {
values[j] = cadence.NewUInt8(v)
}
encodedKeys[i] = cadence.NewArray(values)
}
return encodedKeys, nil
}
func EncodeRuntimeAccountPublicKey(a AccountPublicKey) ([]byte, error) {
publicKey := a.PublicKey.Encode()
w := runtimeAccountPublicKeyWrapper{
PublicKey: publicKey,
SignAlgo: uint(a.SignAlgo),
HashAlgo: uint(a.HashAlgo),
Weight: uint(a.Weight),
}
return rlp.EncodeToBytes(&w)
}
func decodeAccountPublicKeyWrapper(b []byte) (accountPublicKeyWrapper, error) {
var wrapper accountPublicKeyWrapper
err := rlp.DecodeBytes(b, &wrapper)
if err != nil {
// public key data may be stored in legacy format, so convert
var legacyWrapper legacyAccountPublicKeyWrapper
err := rlp.DecodeBytes(b, &legacyWrapper)
if err != nil {
return accountPublicKeyWrapper{}, err
}
return accountPublicKeyWrapper{
PublicKey: legacyWrapper.PublicKey,
SignAlgo: legacyWrapper.SignAlgo,
HashAlgo: legacyWrapper.HashAlgo,
Weight: legacyWrapper.Weight,
SeqNumber: legacyWrapper.SeqNumber,
Revoked: false, // all legacy keys are not revoked
}, nil
}
return wrapper, nil
}
func DecodeAccountPublicKey(b []byte, index uint64) (AccountPublicKey, error) {
w, err := decodeAccountPublicKeyWrapper(b)
if err != nil {
return AccountPublicKey{}, err
}
signAlgo := crypto.SigningAlgorithm(w.SignAlgo)
hashAlgo := hash.HashingAlgorithm(w.HashAlgo)
publicKey, err := crypto.DecodePublicKey(signAlgo, w.PublicKey)
if err != nil {
return AccountPublicKey{}, err
}
return AccountPublicKey{
Index: int(index),
PublicKey: publicKey,
SignAlgo: signAlgo,
HashAlgo: hashAlgo,
Weight: int(w.Weight),
SeqNumber: w.SeqNumber,
Revoked: w.Revoked,
}, nil
}
// DecodeRuntimeAccountPublicKey decode bytes into AccountPublicKey
// but it is designed to accept byte-format used by Cadence runtime
// (currently same as SDK, but we don't want to keep explicit dependency
// on SDK)
func DecodeRuntimeAccountPublicKey(b []byte, seqNumber uint64) (AccountPublicKey, error) {
var w runtimeAccountPublicKeyWrapper
err := rlp.DecodeBytes(b, &w)
if err != nil {
return AccountPublicKey{}, err
}
signAlgo := crypto.SigningAlgorithm(w.SignAlgo)
hashAlgo := hash.HashingAlgorithm(w.HashAlgo)
publicKey, err := crypto.DecodePublicKey(signAlgo, w.PublicKey)
if err != nil {
return AccountPublicKey{}, err
}
return AccountPublicKey{
PublicKey: publicKey,
SignAlgo: signAlgo,
HashAlgo: hashAlgo,
Weight: int(w.Weight),
SeqNumber: seqNumber,
}, nil
}
func EncodeAccountPrivateKey(a AccountPrivateKey) ([]byte, error) {
privateKey := a.PrivateKey.Encode()
w := accountPrivateKeyWrapper{
PrivateKey: privateKey,
SignAlgo: uint(a.SignAlgo),
HashAlgo: uint(a.HashAlgo),
}
return rlp.EncodeToBytes(&w)
}
func DecodeAccountPrivateKey(b []byte) (AccountPrivateKey, error) {
var w accountPrivateKeyWrapper
err := rlp.DecodeBytes(b, &w)
if err != nil {
return AccountPrivateKey{}, err
}
signAlgo := crypto.SigningAlgorithm(w.SignAlgo)
hashAlgo := hash.HashingAlgorithm(w.HashAlgo)
privateKey, err := crypto.DecodePrivateKey(signAlgo, w.PrivateKey)
if err != nil {
return AccountPrivateKey{}, err
}
return AccountPrivateKey{
PrivateKey: privateKey,
SignAlgo: signAlgo,
HashAlgo: hashAlgo,
}, nil
}