UNPKG

3.66 kBJavaScriptView Raw
1// Copyright 2017-2023 @polkadot/types authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3
4import { AbstractBase } from '@polkadot/types-codec';
5import { u8aToHex } from '@polkadot/util';
6import { DEFAULT_VERSION } from "./constants.js";
7const VERSIONS = ['ExtrinsicPayloadUnknown',
8// v0 is unknown
9'ExtrinsicPayloadUnknown', 'ExtrinsicPayloadUnknown', 'ExtrinsicPayloadUnknown', 'ExtrinsicPayloadV4'];
10
11/** @internal */
12function decodeExtrinsicPayload(registry, value, version = DEFAULT_VERSION) {
13 if (value instanceof GenericExtrinsicPayload) {
14 return value.unwrap();
15 }
16 return registry.createTypeUnsafe(VERSIONS[version] || VERSIONS[0], [value, {
17 version
18 }]);
19}
20
21/**
22 * @name GenericExtrinsicPayload
23 * @description
24 * A signing payload for an [[Extrinsic]]. For the final encoding, it is variable length based
25 * on the contents included
26 */
27export class GenericExtrinsicPayload extends AbstractBase {
28 constructor(registry, value, {
29 version
30 } = {}) {
31 super(registry, decodeExtrinsicPayload(registry, value, version));
32 }
33
34 /**
35 * @description The block [[BlockHash]] the signature applies to (mortal/immortal)
36 */
37 get blockHash() {
38 return this.inner.blockHash;
39 }
40
41 /**
42 * @description The [[ExtrinsicEra]]
43 */
44 get era() {
45 return this.inner.era;
46 }
47
48 /**
49 * @description The genesis block [[BlockHash]] the signature applies to
50 */
51 get genesisHash() {
52 // NOTE only v3+
53 return this.inner.genesisHash || this.registry.createTypeUnsafe('Hash', []);
54 }
55
56 /**
57 * @description The [[Bytes]] contained in the payload
58 */
59 get method() {
60 return this.inner.method;
61 }
62
63 /**
64 * @description The [[Index]]
65 */
66 get nonce() {
67 return this.inner.nonce;
68 }
69
70 /**
71 * @description The specVersion as a [[u32]] for this payload
72 */
73 get specVersion() {
74 // NOTE only v3+
75 return this.inner.specVersion || this.registry.createTypeUnsafe('u32', []);
76 }
77
78 /**
79 * @description The [[Balance]]
80 */
81 get tip() {
82 // NOTE from v2+
83 return this.inner.tip || this.registry.createTypeUnsafe('Compact<Balance>', []);
84 }
85
86 /**
87 * @description The transaction version as a [[u32]] for this payload
88 */
89 get transactionVersion() {
90 // NOTE only v4+
91 return this.inner.transactionVersion || this.registry.createTypeUnsafe('u32', []);
92 }
93
94 /**
95 * @description Compares the value of the input to see if there is a match
96 */
97 eq(other) {
98 return this.inner.eq(other);
99 }
100
101 /**
102 * @description Sign the payload with the keypair
103 */
104 sign(signerPair) {
105 const signature = this.inner.sign(signerPair);
106
107 // This is extensible, so we could quite readily extend to send back extra
108 // information, such as for instance the payload, i.e. `payload: this.toHex()`
109 // For the case here we sign via the extrinsic, we ignore the return, so generally
110 // this is applicable for external signing
111 return {
112 signature: u8aToHex(signature)
113 };
114 }
115
116 /**
117 * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
118 */
119 toHuman(isExtended) {
120 return this.inner.toHuman(isExtended);
121 }
122
123 /**
124 * @description Converts the Object to JSON, typically used for RPC transfers
125 */
126 toJSON() {
127 return this.toHex();
128 }
129
130 /**
131 * @description Returns the string representation of the value
132 */
133 toString() {
134 return this.toHex();
135 }
136
137 /**
138 * @description Returns a serialized u8a form
139 */
140 toU8a(isBare) {
141 // call our parent, with only the method stripped
142 return super.toU8a(isBare ? {
143 method: true
144 } : false);
145 }
146}
\No newline at end of file