1 | import { Struct } from '@polkadot/types-codec';
|
2 | import { isU8a } from '@polkadot/util';
|
3 | export const EXTRINSIC_VERSION = 4;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | export class GenericExtrinsicV4 extends Struct {
|
10 | constructor(registry, value, { isSigned } = {}) {
|
11 | super(registry, {
|
12 | signature: 'ExtrinsicSignatureV4',
|
13 |
|
14 | method: 'Call'
|
15 | }, GenericExtrinsicV4.decodeExtrinsic(registry, value, isSigned));
|
16 | }
|
17 |
|
18 | static decodeExtrinsic(registry, value, isSigned = false) {
|
19 | if (value instanceof GenericExtrinsicV4) {
|
20 | return value;
|
21 | }
|
22 | else if (value instanceof registry.createClassUnsafe('Call')) {
|
23 | return { method: value };
|
24 | }
|
25 | else if (isU8a(value)) {
|
26 |
|
27 | const signature = registry.createTypeUnsafe('ExtrinsicSignatureV4', [value, { isSigned }]);
|
28 | const method = registry.createTypeUnsafe('Call', [value.subarray(signature.encodedLength)]);
|
29 | return {
|
30 | method,
|
31 | signature
|
32 | };
|
33 | }
|
34 | return value || {};
|
35 | }
|
36 | |
37 |
|
38 |
|
39 | get encodedLength() {
|
40 | return this.toU8a().length;
|
41 | }
|
42 | |
43 |
|
44 |
|
45 | get method() {
|
46 | return this.getT('method');
|
47 | }
|
48 | |
49 |
|
50 |
|
51 | get signature() {
|
52 | return this.getT('signature');
|
53 | }
|
54 | |
55 |
|
56 |
|
57 | get version() {
|
58 | return EXTRINSIC_VERSION;
|
59 | }
|
60 | |
61 |
|
62 |
|
63 | addSignature(signer, signature, payload) {
|
64 | this.signature.addSignature(signer, signature, payload);
|
65 | return this;
|
66 | }
|
67 | |
68 |
|
69 |
|
70 | sign(account, options) {
|
71 | this.signature.sign(this.method, account, options);
|
72 | return this;
|
73 | }
|
74 | |
75 |
|
76 |
|
77 | signFake(signer, options) {
|
78 | this.signature.signFake(this.method, signer, options);
|
79 | return this;
|
80 | }
|
81 | }
|