-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext.go
148 lines (120 loc) · 2.98 KB
/
ext.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
package client
import (
"fmt"
"github.com/centrifuge/go-substrate-rpc-client/v4/scale"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)
func NewExtrinsic(c types.Call) Extrinsic {
return Extrinsic{
types.Extrinsic{
Version: types.ExtrinsicVersion4,
Method: c,
},
}
}
type Extrinsic struct {
types.Extrinsic
}
func (e *Extrinsic) Sign(signer *Signer, o types.SignatureOptions) error {
if e.Type() != types.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 = types.ExtrinsicEra{IsImmortalEra: true}
}
payload := ExtrinsicPayloadV4{
ExtrinsicPayloadV3: types.ExtrinsicPayloadV3{
Method: mb,
Era: era,
Nonce: o.Nonce,
Tip: o.Tip,
SpecVersion: o.SpecVersion,
GenesisHash: o.GenesisHash,
BlockHash: o.BlockHash,
},
TransactionVersion: o.TransactionVersion,
}
signerPubKey, err := types.NewMultiAddressFromAccountID(signer.Public())
if err != nil {
return err
}
sig, err := payload.Sign(signer)
if err != nil {
return err
}
var signature types.MultiSignature
if signer.KeyType == 0 {
signature = types.MultiSignature{IsSr25519: true, AsSr25519: sig}
} else if signer.KeyType == 1 {
signature = types.MultiSignature{IsEd25519: true, AsEd25519: sig}
}
extSig := types.ExtrinsicSignatureV4{
Signer: signerPubKey,
Signature: signature,
Era: era,
Nonce: o.Nonce,
Tip: o.Tip,
}
e.Signature = extSig
// mark the extrinsic as signed
e.Version |= types.ExtrinsicBitSigned
return nil
}
type ExtrinsicPayloadV4 struct {
types.ExtrinsicPayloadV3
TransactionVersion types.U32
}
// Sign the extrinsic payload with the given derivation path
func (e ExtrinsicPayloadV4) Sign(signer *Signer) (types.Signature, error) {
b, err := codec.Encode(e)
if err != nil {
return types.Signature{}, err
}
sig, err := signer.Sign(b)
return types.NewSignature(sig), err
}
func (e ExtrinsicPayloadV4) Encode(encoder scale.Encoder) error {
err := encoder.Encode(e.Method)
if err != nil {
return err
}
err = encoder.Encode(e.Era)
if err != nil {
return err
}
err = encoder.Encode(e.Nonce)
if err != nil {
return err
}
err = encoder.Encode(e.Tip)
if err != nil {
return err
}
err = encoder.Encode(e.SpecVersion)
if err != nil {
return err
}
err = encoder.Encode(e.TransactionVersion)
if err != nil {
return err
}
err = encoder.Encode(e.GenesisHash)
if err != nil {
return err
}
err = encoder.Encode(e.BlockHash)
if err != nil {
return err
}
return nil
}
// Decode does nothing and always returns an error. ExtrinsicPayloadV4 is only used for encoding, not for decoding
func (e *ExtrinsicPayloadV4) Decode(decoder scale.Decoder) error {
return fmt.Errorf("decoding of ExtrinsicPayloadV4 is not supported")
}