-
Notifications
You must be signed in to change notification settings - Fork 2
/
extrinsic.go
262 lines (223 loc) · 6.52 KB
/
extrinsic.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package extrinsic
import (
"bytes"
"encoding/json"
"fmt"
"math/big"
"strings"
"github.com/centrifuge/go-substrate-rpc-client/v4/scale"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)
type SignatureOptions struct {
Era ExtrinsicEra
Nonce types.UCompact
Tip types.UCompact
SpecVersion types.U32
GenesisHash types.Hash
BlockHash types.Hash
AppID types.UCompact
TransactionVersion types.U32
}
type ExtrinsicSignatureV4 struct {
Signer types.MultiAddress
Signature types.MultiSignature
Era ExtrinsicEra // extra via system::CheckEra
Nonce types.UCompact // extra via system::CheckNonce (Compact<Index> where Index is u32))
Tip types.UCompact // extra via balances::TakeFees (Compact<Balance> where Balance is u128))
AppID types.UCompact // Avail specific AppID
}
const (
ExtrinsicBitSigned = 0x80
ExtrinsicBitUnsigned = 0
ExtrinsicUnmaskVersion = 0x7f
ExtrinsicDefaultVersion = 1
ExtrinsicVersionUnknown = 0 // v0 is unknown
ExtrinsicVersion1 = 1
ExtrinsicVersion2 = 2
ExtrinsicVersion3 = 3
ExtrinsicVersion4 = 4
)
// Extrinsic is a piece of Args bundled into a block that expresses something from the "external" (i.e. off-chain)
// world. There are, broadly speaking, two types of extrinsic: transactions (which tend to be signed) and
// inherents (which don't).
type Extrinsic struct {
// Version is the encoded version flag (which encodes the raw transaction version and signing information in one byte)
Version byte
// Signature is the ExtrinsicSignatureV4, it's presence depends on the Version flag
Signature ExtrinsicSignatureV4
// Method is the call this extrinsic wraps
Method types.Call
}
// NewExtrinsic creates a new Extrinsic from the provided Call
func NewExtrinsic(c types.Call) Extrinsic {
return Extrinsic{
Version: ExtrinsicVersion4,
Method: c,
}
}
// UnmarshalJSON fills Extrinsic with the JSON encoded byte array given by bz
func (e *Extrinsic) UnmarshalJSON(bz []byte) error {
var tmp string
if err := json.Unmarshal(bz, &tmp); err != nil {
return err
}
// HACK 11 Jan 2019 - before https://github.com/paritytech/substrate/pull/1388
// extrinsics didn't have the length, cater for both approaches. This is very
// inconsistent with any other `Vec<u8>` implementation
var l types.UCompact
err := codec.DecodeFromHex(tmp, &l)
if err != nil {
return err
}
prefix, err := codec.EncodeToHex(l)
if err != nil {
return err
}
// determine whether length prefix is there
if strings.HasPrefix(tmp, prefix) {
return codec.DecodeFromHex(tmp, e)
}
// not there, prepend with compact encoded length prefix
dec, err := codec.HexDecodeString(tmp)
if err != nil {
return err
}
length := types.NewUCompactFromUInt(uint64(len(dec)))
bprefix, err := codec.Encode(length)
if err != nil {
return err
}
bprefix = append(bprefix, dec...)
return codec.Decode(bprefix, e)
}
// MarshalJSON returns a JSON encoded byte array of Extrinsic
func (e Extrinsic) MarshalJSON() ([]byte, error) {
s, err := codec.EncodeToHex(e)
if err != nil {
return nil, err
}
return json.Marshal(s)
}
// IsSigned returns true if the extrinsic is signed
func (e Extrinsic) IsSigned() bool {
return e.Version&ExtrinsicBitSigned == ExtrinsicBitSigned
}
// Type returns the raw transaction version (not flagged with signing information)
func (e Extrinsic) Type() uint8 {
return e.Version & ExtrinsicUnmaskVersion
}
// Sign adds a signature to the extrinsic
func (e *Extrinsic) Sign(signer signature.KeyringPair, o SignatureOptions) error {
if e.Type() != ExtrinsicVersion4 {
return fmt.Errorf("unsupported extrinsic version: %v (isSigned: %v, type: %v)", e.Version, e.IsSigned(), e.Type())
}
mb, err := codec.Encode(e.Method)
if err != nil {
return err
}
era := o.Era
if !o.Era.IsMortalEra {
era = ExtrinsicEra{IsImmortalEra: true}
}
payload := ExtrinsicPayloadV3{
Method: mb,
Era: era,
Nonce: o.Nonce,
Tip: o.Tip,
AppID: o.AppID,
SpecVersion: o.SpecVersion,
TransactionVersion: o.TransactionVersion,
GenesisHash: o.GenesisHash,
BlockHash: o.BlockHash,
}
signerPubKey, err := types.NewMultiAddressFromAccountID(signer.PublicKey)
if err != nil {
return err
}
sig, err := payload.Sign(signer)
if err != nil {
return err
}
extSig := ExtrinsicSignatureV4{
Signer: signerPubKey,
Signature: types.MultiSignature{IsSr25519: true, AsSr25519: sig},
Era: era,
Nonce: o.Nonce,
Tip: o.Tip,
AppID: o.AppID,
}
e.Signature = extSig
// mark the extrinsic as signed
e.Version |= ExtrinsicBitSigned
return nil
}
func (e *Extrinsic) Decode(decoder scale.Decoder) error {
// compact length encoding (1, 2, or 4 bytes) (may not be there for Extrinsics older than Jan 11 2019)
_, err := decoder.DecodeUintCompact()
if err != nil {
return err
}
// version, signature bitmask (1 byte)
err = decoder.Decode(&e.Version)
if err != nil {
return err
}
// signature
if e.IsSigned() {
if e.Type() != ExtrinsicVersion4 {
return fmt.Errorf("unsupported extrinsic version: %v (isSigned: %v, type: %v)", e.Version, e.IsSigned(),
e.Type())
}
err = decoder.Decode(&e.Signature)
if err != nil {
return err
}
}
// call
err = decoder.Decode(&e.Method)
if err != nil {
return err
}
return nil
}
func (e Extrinsic) Encode(encoder scale.Encoder) error {
if e.Type() != ExtrinsicVersion4 {
return fmt.Errorf("unsupported extrinsic version: %v (isSigned: %v, type: %v)", e.Version, e.IsSigned(),
e.Type())
}
// create a temporary buffer that will receive the plain encoded transaction (version, signature (optional),
// method/call)
var bb = bytes.Buffer{}
tempEnc := scale.NewEncoder(&bb)
// encode the version of the extrinsic
err := tempEnc.Encode(e.Version)
if err != nil {
return err
}
// encode the signature if signed
if e.IsSigned() {
err = tempEnc.Encode(e.Signature)
if err != nil {
return err
}
}
// encode the method
err = tempEnc.Encode(e.Method)
if err != nil {
return err
}
// take the temporary buffer to determine length, write that as prefix
eb := bb.Bytes()
err = encoder.EncodeUintCompact(*big.NewInt(0).SetUint64(uint64(len(eb))))
if err != nil {
return err
}
// write the actual encoded transaction
err = encoder.Write(eb)
if err != nil {
return err
}
return nil
}