UNPKG

3.21 kBJavaScriptView Raw
1import { Enum, Struct } from '@polkadot/types-codec';
2import { objectSpread } from '@polkadot/util';
3import { sign } from '../util.js';
4/**
5 * @name GenericExtrinsicPayloadV4
6 * @description
7 * A signing payload for an [[Extrinsic]]. For the final encoding, it is
8 * variable length based on the contents included
9 */
10export class GenericExtrinsicPayloadV4 extends Struct {
11 __internal__signOptions;
12 constructor(registry, value) {
13 super(registry, objectSpread({ method: 'Bytes' }, registry.getSignedExtensionTypes(), registry.getSignedExtensionExtra()), value);
14 // Do detection for the type of extrinsic, in the case of MultiSignature
15 // this is an enum, in the case of AnySignature, this is a Hash only
16 // (which may be 64 or 65 bytes)
17 this.__internal__signOptions = {
18 withType: registry.createTypeUnsafe('ExtrinsicSignature', []) instanceof Enum
19 };
20 }
21 /**
22 * @description Returns a breakdown of the hex encoding for this Codec
23 */
24 inspect() {
25 return super.inspect({ method: true });
26 }
27 /**
28 * @description The block [[BlockHash]] the signature applies to (mortal/immortal)
29 */
30 get blockHash() {
31 return this.getT('blockHash');
32 }
33 /**
34 * @description The [[ExtrinsicEra]]
35 */
36 get era() {
37 return this.getT('era');
38 }
39 /**
40 * @description The genesis [[BlockHash]] the signature applies to (mortal/immortal)
41 */
42 get genesisHash() {
43 return this.getT('genesisHash');
44 }
45 /**
46 * @description The [[Bytes]] contained in the payload
47 */
48 get method() {
49 return this.getT('method');
50 }
51 /**
52 * @description The [[Index]]
53 */
54 get nonce() {
55 return this.getT('nonce');
56 }
57 /**
58 * @description The specVersion for this signature
59 */
60 get specVersion() {
61 return this.getT('specVersion');
62 }
63 /**
64 * @description The tip [[Balance]]
65 */
66 get tip() {
67 return this.getT('tip');
68 }
69 /**
70 * @description The transactionVersion for this signature
71 */
72 get transactionVersion() {
73 return this.getT('transactionVersion');
74 }
75 /**
76 * @description The (optional) asset id for this signature for chains that support transaction fees in assets
77 */
78 get assetId() {
79 return this.getT('assetId');
80 }
81 /**
82 * @description The (optional) asset id for this signature for chains that support transaction fees in assets
83 */
84 get metadataHash() {
85 return this.getT('metadataHash');
86 }
87 /**
88 * @description Sign the payload with the keypair
89 */
90 sign(signerPair) {
91 // NOTE The `toU8a({ method: true })` argument is absolutely critical, we
92 // don't want the method (Bytes) to have the length prefix included. This
93 // means that the data-as-signed is un-decodable, but is also doesn't need
94 // the extra information, only the pure data (and is not decoded) ...
95 // The same applies to V1..V3, if we have a V5, carry this comment
96 return sign(this.registry, signerPair, this.toU8a({ method: true }), this.__internal__signOptions);
97 }
98}