UNPKG

2.49 kBJavaScriptView Raw
1import { Struct } from '@polkadot/types-codec';
2import { isU8a } from '@polkadot/util';
3export const EXTRINSIC_VERSION = 4;
4/**
5 * @name GenericExtrinsicV4
6 * @description
7 * The third generation of compact extrinsics
8 */
9export class GenericExtrinsicV4 extends Struct {
10 constructor(registry, value, { isSigned } = {}) {
11 super(registry, {
12 signature: 'ExtrinsicSignatureV4',
13 // eslint-disable-next-line sort-keys
14 method: 'Call'
15 }, GenericExtrinsicV4.decodeExtrinsic(registry, value, isSigned));
16 }
17 /** @internal */
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 // here we decode manually since we need to pull through the version information
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 * @description The length of the value when encoded as a Uint8Array
38 */
39 get encodedLength() {
40 return this.toU8a().length;
41 }
42 /**
43 * @description The [[Call]] this extrinsic wraps
44 */
45 get method() {
46 return this.getT('method');
47 }
48 /**
49 * @description The [[ExtrinsicSignatureV4]]
50 */
51 get signature() {
52 return this.getT('signature');
53 }
54 /**
55 * @description The version for the signature
56 */
57 get version() {
58 return EXTRINSIC_VERSION;
59 }
60 /**
61 * @description Add an [[ExtrinsicSignatureV4]] to the extrinsic (already generated)
62 */
63 addSignature(signer, signature, payload) {
64 this.signature.addSignature(signer, signature, payload);
65 return this;
66 }
67 /**
68 * @description Sign the extrinsic with a specific keypair
69 */
70 sign(account, options) {
71 this.signature.sign(this.method, account, options);
72 return this;
73 }
74 /**
75 * @describe Adds a fake signature to the extrinsic
76 */
77 signFake(signer, options) {
78 this.signature.signFake(this.method, signer, options);
79 return this;
80 }
81}