@dedot/api 0.0.1-alpha.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (202) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +1 -0
  3. package/chaintypes/index.d.ts +1 -0
  4. package/chaintypes/index.js +1 -0
  5. package/chaintypes/substrate/consts.d.ts +2132 -0
  6. package/chaintypes/substrate/consts.js +2 -0
  7. package/chaintypes/substrate/errors.d.ts +3490 -0
  8. package/chaintypes/substrate/errors.js +2 -0
  9. package/chaintypes/substrate/events.d.ts +5187 -0
  10. package/chaintypes/substrate/events.js +2 -0
  11. package/chaintypes/substrate/index.d.ts +22 -0
  12. package/chaintypes/substrate/index.js +2 -0
  13. package/chaintypes/substrate/json-rpc.d.ts +3 -0
  14. package/chaintypes/substrate/json-rpc.js +2 -0
  15. package/chaintypes/substrate/query.d.ts +3829 -0
  16. package/chaintypes/substrate/query.js +2 -0
  17. package/chaintypes/substrate/runtime.d.ts +850 -0
  18. package/chaintypes/substrate/runtime.js +2 -0
  19. package/chaintypes/substrate/tx.d.ts +14269 -0
  20. package/chaintypes/substrate/tx.js +2 -0
  21. package/chaintypes/substrate/types.d.ts +30457 -0
  22. package/chaintypes/substrate/types.js +2 -0
  23. package/cjs/chaintypes/index.js +17 -0
  24. package/cjs/chaintypes/substrate/consts.js +3 -0
  25. package/cjs/chaintypes/substrate/errors.js +3 -0
  26. package/cjs/chaintypes/substrate/events.js +3 -0
  27. package/cjs/chaintypes/substrate/index.js +18 -0
  28. package/cjs/chaintypes/substrate/json-rpc.js +3 -0
  29. package/cjs/chaintypes/substrate/query.js +3 -0
  30. package/cjs/chaintypes/substrate/runtime.js +3 -0
  31. package/cjs/chaintypes/substrate/tx.js +3 -0
  32. package/cjs/chaintypes/substrate/types.js +3 -0
  33. package/cjs/client/BaseSubstrateClient.js +257 -0
  34. package/cjs/client/Dedot.js +250 -0
  35. package/cjs/client/DedotClient.js +186 -0
  36. package/cjs/client/index.js +18 -0
  37. package/cjs/executor/ConstantExecutor.js +20 -0
  38. package/cjs/executor/ErrorExecutor.js +46 -0
  39. package/cjs/executor/EventExecutor.js +52 -0
  40. package/cjs/executor/Executor.js +42 -0
  41. package/cjs/executor/RuntimeApiExecutor.js +121 -0
  42. package/cjs/executor/StorageQueryExecutor.js +115 -0
  43. package/cjs/executor/TxExecutor.js +55 -0
  44. package/cjs/executor/index.js +26 -0
  45. package/cjs/executor/v2/RuntimeApiExecutorV2.js +21 -0
  46. package/cjs/executor/v2/StorageQueryExecutorV2.js +65 -0
  47. package/cjs/executor/v2/TxExecutorV2.js +20 -0
  48. package/cjs/extrinsic/extensions/ExtraSignedExtension.js +87 -0
  49. package/cjs/extrinsic/extensions/SignedExtension.js +41 -0
  50. package/cjs/extrinsic/extensions/index.js +19 -0
  51. package/cjs/extrinsic/extensions/known/ChargeAssetTxPayment.js +43 -0
  52. package/cjs/extrinsic/extensions/known/ChargeTransactionPayment.js +16 -0
  53. package/cjs/extrinsic/extensions/known/CheckGenesis.js +19 -0
  54. package/cjs/extrinsic/extensions/known/CheckMortality.js +90 -0
  55. package/cjs/extrinsic/extensions/known/CheckNonZeroSender.js +10 -0
  56. package/cjs/extrinsic/extensions/known/CheckNonce.js +33 -0
  57. package/cjs/extrinsic/extensions/known/CheckSpecVersion.js +19 -0
  58. package/cjs/extrinsic/extensions/known/CheckTxVersion.js +19 -0
  59. package/cjs/extrinsic/extensions/known/CheckWeight.js +10 -0
  60. package/cjs/extrinsic/extensions/known/PrevalidateAttests.js +11 -0
  61. package/cjs/extrinsic/extensions/known/index.js +25 -0
  62. package/cjs/extrinsic/index.js +18 -0
  63. package/cjs/extrinsic/submittable/BaseSubmittableExtrinsic.js +70 -0
  64. package/cjs/extrinsic/submittable/SubmittableExtrinsic.js +47 -0
  65. package/cjs/extrinsic/submittable/SubmittableExtrinsicV2.js +204 -0
  66. package/cjs/extrinsic/submittable/SubmittableResult.js +33 -0
  67. package/cjs/extrinsic/submittable/errors.js +16 -0
  68. package/cjs/extrinsic/submittable/fakeSigner.js +13 -0
  69. package/cjs/extrinsic/submittable/index.js +20 -0
  70. package/cjs/extrinsic/submittable/utils.js +20 -0
  71. package/cjs/index.js +21 -0
  72. package/cjs/json-rpc/JsonRpcClient.js +165 -0
  73. package/cjs/json-rpc/group/ChainHead/BlockUsage.js +24 -0
  74. package/cjs/json-rpc/group/ChainHead/ChainHead.js +603 -0
  75. package/cjs/json-rpc/group/ChainHead/error.js +59 -0
  76. package/cjs/json-rpc/group/ChainHead/index.js +18 -0
  77. package/cjs/json-rpc/group/ChainSpec.js +19 -0
  78. package/cjs/json-rpc/group/JsonRpcGroup.js +89 -0
  79. package/cjs/json-rpc/group/Transaction.js +25 -0
  80. package/cjs/json-rpc/group/TransactionWatch.js +17 -0
  81. package/cjs/json-rpc/group/index.js +21 -0
  82. package/cjs/json-rpc/index.js +20 -0
  83. package/cjs/json-rpc/scaledResponses.js +15 -0
  84. package/cjs/json-rpc/subscriptionsInfo.js +20 -0
  85. package/cjs/package.json +1 -0
  86. package/cjs/proxychain.js +27 -0
  87. package/cjs/storage/QueryableStorage.js +161 -0
  88. package/cjs/storage/index.js +17 -0
  89. package/cjs/types.js +2 -0
  90. package/client/BaseSubstrateClient.d.ts +65 -0
  91. package/client/BaseSubstrateClient.js +252 -0
  92. package/client/Dedot.d.ts +131 -0
  93. package/client/Dedot.js +246 -0
  94. package/client/DedotClient.d.ts +61 -0
  95. package/client/DedotClient.js +182 -0
  96. package/client/index.d.ts +2 -0
  97. package/client/index.js +2 -0
  98. package/executor/ConstantExecutor.d.ts +10 -0
  99. package/executor/ConstantExecutor.js +16 -0
  100. package/executor/ErrorExecutor.d.ts +10 -0
  101. package/executor/ErrorExecutor.js +42 -0
  102. package/executor/EventExecutor.d.ts +10 -0
  103. package/executor/EventExecutor.js +48 -0
  104. package/executor/Executor.d.ts +149 -0
  105. package/executor/Executor.js +38 -0
  106. package/executor/RuntimeApiExecutor.d.ts +27 -0
  107. package/executor/RuntimeApiExecutor.js +117 -0
  108. package/executor/StorageQueryExecutor.d.ts +14 -0
  109. package/executor/StorageQueryExecutor.js +111 -0
  110. package/executor/TxExecutor.d.ts +10 -0
  111. package/executor/TxExecutor.js +51 -0
  112. package/executor/index.d.ts +10 -0
  113. package/executor/index.js +10 -0
  114. package/executor/v2/RuntimeApiExecutorV2.d.ts +14 -0
  115. package/executor/v2/RuntimeApiExecutorV2.js +17 -0
  116. package/executor/v2/StorageQueryExecutorV2.d.ts +17 -0
  117. package/executor/v2/StorageQueryExecutorV2.js +61 -0
  118. package/executor/v2/TxExecutorV2.d.ts +11 -0
  119. package/executor/v2/TxExecutorV2.js +16 -0
  120. package/extrinsic/extensions/ExtraSignedExtension.d.ts +14 -0
  121. package/extrinsic/extensions/ExtraSignedExtension.js +60 -0
  122. package/extrinsic/extensions/SignedExtension.d.ts +40 -0
  123. package/extrinsic/extensions/SignedExtension.js +37 -0
  124. package/extrinsic/extensions/index.d.ts +3 -0
  125. package/extrinsic/extensions/index.js +3 -0
  126. package/extrinsic/extensions/known/ChargeAssetTxPayment.d.ts +14 -0
  127. package/extrinsic/extensions/known/ChargeAssetTxPayment.js +39 -0
  128. package/extrinsic/extensions/known/ChargeTransactionPayment.d.ts +6 -0
  129. package/extrinsic/extensions/known/ChargeTransactionPayment.js +12 -0
  130. package/extrinsic/extensions/known/CheckGenesis.d.ts +10 -0
  131. package/extrinsic/extensions/known/CheckGenesis.js +15 -0
  132. package/extrinsic/extensions/known/CheckMortality.d.ts +15 -0
  133. package/extrinsic/extensions/known/CheckMortality.js +86 -0
  134. package/extrinsic/extensions/known/CheckNonZeroSender.d.ts +6 -0
  135. package/extrinsic/extensions/known/CheckNonZeroSender.js +6 -0
  136. package/extrinsic/extensions/known/CheckNonce.d.ts +10 -0
  137. package/extrinsic/extensions/known/CheckNonce.js +29 -0
  138. package/extrinsic/extensions/known/CheckSpecVersion.d.ts +9 -0
  139. package/extrinsic/extensions/known/CheckSpecVersion.js +15 -0
  140. package/extrinsic/extensions/known/CheckTxVersion.d.ts +9 -0
  141. package/extrinsic/extensions/known/CheckTxVersion.js +15 -0
  142. package/extrinsic/extensions/known/CheckWeight.d.ts +6 -0
  143. package/extrinsic/extensions/known/CheckWeight.js +6 -0
  144. package/extrinsic/extensions/known/PrevalidateAttests.d.ts +7 -0
  145. package/extrinsic/extensions/known/PrevalidateAttests.js +7 -0
  146. package/extrinsic/extensions/known/index.d.ts +3 -0
  147. package/extrinsic/extensions/known/index.js +22 -0
  148. package/extrinsic/index.d.ts +2 -0
  149. package/extrinsic/index.js +2 -0
  150. package/extrinsic/submittable/BaseSubmittableExtrinsic.d.ts +17 -0
  151. package/extrinsic/submittable/BaseSubmittableExtrinsic.js +66 -0
  152. package/extrinsic/submittable/SubmittableExtrinsic.d.ts +12 -0
  153. package/extrinsic/submittable/SubmittableExtrinsic.js +43 -0
  154. package/extrinsic/submittable/SubmittableExtrinsicV2.d.ts +15 -0
  155. package/extrinsic/submittable/SubmittableExtrinsicV2.js +200 -0
  156. package/extrinsic/submittable/SubmittableResult.d.ts +19 -0
  157. package/extrinsic/submittable/SubmittableResult.js +29 -0
  158. package/extrinsic/submittable/errors.d.ts +11 -0
  159. package/extrinsic/submittable/errors.js +12 -0
  160. package/extrinsic/submittable/fakeSigner.d.ts +2 -0
  161. package/extrinsic/submittable/fakeSigner.js +10 -0
  162. package/extrinsic/submittable/index.d.ts +4 -0
  163. package/extrinsic/submittable/index.js +4 -0
  164. package/extrinsic/submittable/utils.d.ts +10 -0
  165. package/extrinsic/submittable/utils.js +15 -0
  166. package/index.d.ts +6 -0
  167. package/index.js +5 -0
  168. package/json-rpc/JsonRpcClient.d.ts +50 -0
  169. package/json-rpc/JsonRpcClient.js +160 -0
  170. package/json-rpc/group/ChainHead/BlockUsage.d.ts +11 -0
  171. package/json-rpc/group/ChainHead/BlockUsage.js +20 -0
  172. package/json-rpc/group/ChainHead/ChainHead.d.ts +70 -0
  173. package/json-rpc/group/ChainHead/ChainHead.js +599 -0
  174. package/json-rpc/group/ChainHead/error.d.ts +47 -0
  175. package/json-rpc/group/ChainHead/error.js +48 -0
  176. package/json-rpc/group/ChainHead/index.d.ts +2 -0
  177. package/json-rpc/group/ChainHead/index.js +2 -0
  178. package/json-rpc/group/ChainSpec.d.ts +10 -0
  179. package/json-rpc/group/ChainSpec.js +15 -0
  180. package/json-rpc/group/JsonRpcGroup.d.ts +74 -0
  181. package/json-rpc/group/JsonRpcGroup.js +85 -0
  182. package/json-rpc/group/Transaction.d.ts +11 -0
  183. package/json-rpc/group/Transaction.js +21 -0
  184. package/json-rpc/group/TransactionWatch.d.ts +10 -0
  185. package/json-rpc/group/TransactionWatch.js +13 -0
  186. package/json-rpc/group/index.d.ts +5 -0
  187. package/json-rpc/group/index.js +5 -0
  188. package/json-rpc/index.d.ts +4 -0
  189. package/json-rpc/index.js +4 -0
  190. package/json-rpc/scaledResponses.d.ts +2 -0
  191. package/json-rpc/scaledResponses.js +12 -0
  192. package/json-rpc/subscriptionsInfo.d.ts +2 -0
  193. package/json-rpc/subscriptionsInfo.js +17 -0
  194. package/package.json +54 -0
  195. package/proxychain.d.ts +14 -0
  196. package/proxychain.js +23 -0
  197. package/storage/QueryableStorage.d.ts +35 -0
  198. package/storage/QueryableStorage.js +157 -0
  199. package/storage/index.d.ts +1 -0
  200. package/storage/index.js +1 -0
  201. package/types.d.ts +120 -0
  202. package/types.js +1 -0
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Executor.js"), exports);
18
+ __exportStar(require("./ConstantExecutor.js"), exports);
19
+ __exportStar(require("./StorageQueryExecutor.js"), exports);
20
+ __exportStar(require("./ErrorExecutor.js"), exports);
21
+ __exportStar(require("./EventExecutor.js"), exports);
22
+ __exportStar(require("./RuntimeApiExecutor.js"), exports);
23
+ __exportStar(require("./TxExecutor.js"), exports);
24
+ __exportStar(require("./v2/StorageQueryExecutorV2.js"), exports);
25
+ __exportStar(require("./v2/RuntimeApiExecutorV2.js"), exports);
26
+ __exportStar(require("./v2/TxExecutorV2.js"), exports);
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RuntimeApiExecutorV2 = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const RuntimeApiExecutor_js_1 = require("../RuntimeApiExecutor.js");
6
+ /**
7
+ * @name RuntimeApiExecutorV2
8
+ */
9
+ class RuntimeApiExecutorV2 extends RuntimeApiExecutor_js_1.RuntimeApiExecutor {
10
+ chainHead;
11
+ constructor(api, chainHead, atBlockHash) {
12
+ (0, utils_1.assert)(api.rpcVersion === 'v2', 'Only supports JSON-RPC v2');
13
+ super(api, atBlockHash);
14
+ this.chainHead = chainHead;
15
+ }
16
+ stateCall(callParams) {
17
+ const { func, params, at } = callParams;
18
+ return this.chainHead.call(func, params, at);
19
+ }
20
+ }
21
+ exports.RuntimeApiExecutorV2 = RuntimeApiExecutorV2;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StorageQueryExecutorV2 = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const StorageQueryExecutor_js_1 = require("../StorageQueryExecutor.js");
6
+ /**
7
+ * @name StorageQueryExecutorV2
8
+ */
9
+ class StorageQueryExecutorV2 extends StorageQueryExecutor_js_1.StorageQueryExecutor {
10
+ chainHead;
11
+ constructor(api, chainHead, atBlockHash) {
12
+ (0, utils_1.assert)(api.rpcVersion === 'v2', 'Only supports JSON-RPC v2');
13
+ super(api, atBlockHash);
14
+ this.chainHead = chainHead;
15
+ }
16
+ exposeStorageMapMethods(entry) {
17
+ // chainHead_storage does not support pagination
18
+ // so for now we're trying to pull all entries from storage
19
+ // this might take a while for large storage
20
+ // TODO improve this, fallback to use `archive`-prefixed if available?
21
+ const entries = async () => {
22
+ const results = await this.chainHead.storage([{ type: 'descendantsValues', key: entry.prefixKey }]);
23
+ return results.map(({ key, value }) => [
24
+ entry.decodeKey(key),
25
+ entry.decodeValue(value),
26
+ ]);
27
+ };
28
+ return { entries };
29
+ }
30
+ async queryStorage(keys, at) {
31
+ const results = await this.chainHead.storage(keys.map((key) => ({ type: 'value', key })), undefined, at);
32
+ return results.reduce((o, r) => {
33
+ o[r.key] = (r.value ?? undefined);
34
+ return o;
35
+ }, {});
36
+ }
37
+ async subscribeStorage(keys, callback) {
38
+ let initialHash = await this.chainHead.bestHash();
39
+ let eventToListen = 'bestBlock';
40
+ // TODO subscribe to finalized data source
41
+ // initialHash = this.chainHead.finalizedHash;
42
+ // eventToListen = 'finalizedBlock';
43
+ const latestChanges = {};
44
+ const pull = async (hash) => {
45
+ const results = await this.queryStorage(keys, hash);
46
+ let changed = false;
47
+ keys.forEach((key) => {
48
+ const newValue = results[key];
49
+ if (latestChanges[key] === newValue)
50
+ return;
51
+ changed = true;
52
+ latestChanges[key] = newValue;
53
+ });
54
+ if (!changed)
55
+ return;
56
+ callback(keys.map((key) => latestChanges[key]));
57
+ };
58
+ await pull(initialHash);
59
+ const unsub = this.chainHead.on(eventToListen, pull);
60
+ return async () => {
61
+ unsub();
62
+ };
63
+ }
64
+ }
65
+ exports.StorageQueryExecutorV2 = StorageQueryExecutorV2;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TxExecutorV2 = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const index_js_1 = require("../../extrinsic/index.js");
6
+ const TxExecutor_js_1 = require("../TxExecutor.js");
7
+ /**
8
+ * @name TxExecutor
9
+ * @description Execute a transaction instruction, returns a submittable extrinsic
10
+ */
11
+ class TxExecutorV2 extends TxExecutor_js_1.TxExecutor {
12
+ constructor(api) {
13
+ (0, utils_1.assert)(api.rpcVersion === 'v2', 'Only supports JSON-RPC v2');
14
+ super(api);
15
+ }
16
+ createExtrinsic(call) {
17
+ return new index_js_1.SubmittableExtrinsicV2(this.api, call);
18
+ }
19
+ }
20
+ exports.TxExecutorV2 = TxExecutorV2;
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.ExtraSignedExtension = void 0;
27
+ const $ = __importStar(require("@dedot/shape"));
28
+ const utils_1 = require("@dedot/utils");
29
+ const SignedExtension_js_1 = require("./SignedExtension.js");
30
+ const index_js_1 = require("./known/index.js");
31
+ class ExtraSignedExtension extends SignedExtension_js_1.SignedExtension {
32
+ #signedExtensions;
33
+ async init() {
34
+ this.#signedExtensions = this.#getSignedExtensions();
35
+ await Promise.all(this.#signedExtensions.map((se) => se.init()));
36
+ this.data = this.#signedExtensions.map((se) => se.data);
37
+ this.additionalSigned = this.#signedExtensions.map((se) => se.additionalSigned);
38
+ }
39
+ get identifier() {
40
+ return 'ExtraSignedExtension';
41
+ }
42
+ get $Data() {
43
+ const { extraTypeId } = this.registry.metadata.extrinsic;
44
+ return (0, utils_1.ensurePresence)(this.registry.findCodec(extraTypeId));
45
+ }
46
+ get $AdditionalSigned() {
47
+ const $AdditionalSignedCodecs = this.#signedExtensionDefs.map((se) => this.registry.findCodec(se.additionalSigned));
48
+ return $.Tuple(...$AdditionalSignedCodecs);
49
+ }
50
+ get $Payload() {
51
+ const { callTypeId } = this.registry.metadata.extrinsic;
52
+ const $Call = this.registry.findCodec(callTypeId);
53
+ return $.Tuple($Call, this.$Data, this.$AdditionalSigned);
54
+ }
55
+ get #signedExtensionDefs() {
56
+ return this.registry.metadata.extrinsic.signedExtensions;
57
+ }
58
+ #getSignedExtensions() {
59
+ return this.#signedExtensionDefs.map((extDef) => {
60
+ const { signedExtensions: userSignedExtensions = {} } = this.api.options;
61
+ const Extension = userSignedExtensions[extDef.ident] ||
62
+ index_js_1.knownSignedExtensions[extDef.ident];
63
+ (0, utils_1.assert)(Extension, `SignedExtension for ${extDef.ident} not found`);
64
+ return new Extension(this.api, {
65
+ ...(0, utils_1.ensurePresence)(this.options),
66
+ def: extDef,
67
+ });
68
+ });
69
+ }
70
+ toPayload(call = '0x') {
71
+ const signedExtensions = this.#signedExtensions.map((se) => se.identifier);
72
+ const { version } = this.registry.metadata.extrinsic;
73
+ return Object.assign({ address: this.options.signerAddress, signedExtensions, version, method: call }, ...this.#signedExtensions.map((se) => se.toPayload()));
74
+ }
75
+ toRawPayload(call = '0x') {
76
+ const payload = this.toPayload(call);
77
+ const $ToSignPayload = $.Tuple($.RawHex, this.$Data, this.$AdditionalSigned);
78
+ const toSignPayload = [call, this.data, this.additionalSigned];
79
+ const rawPayload = $ToSignPayload.tryEncode(toSignPayload);
80
+ return {
81
+ address: payload.address,
82
+ data: (0, utils_1.u8aToHex)(rawPayload),
83
+ type: 'payload',
84
+ };
85
+ }
86
+ }
87
+ exports.ExtraSignedExtension = ExtraSignedExtension;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SignedExtension = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ class SignedExtension {
6
+ api;
7
+ options;
8
+ data;
9
+ additionalSigned;
10
+ constructor(api, options) {
11
+ this.api = api;
12
+ this.options = options;
13
+ this.data = {};
14
+ this.additionalSigned = [];
15
+ }
16
+ async init() {
17
+ // TODO implement this method
18
+ }
19
+ get identifier() {
20
+ return this.signedExtensionDef.ident;
21
+ }
22
+ get $Data() {
23
+ return (0, utils_1.ensurePresence)(this.registry.findCodec(this.signedExtensionDef.typeId));
24
+ }
25
+ get $AdditionalSigned() {
26
+ return (0, utils_1.ensurePresence)(this.registry.findCodec(this.signedExtensionDef.additionalSigned));
27
+ }
28
+ get registry() {
29
+ return this.api.registry;
30
+ }
31
+ get signedExtensionDef() {
32
+ return (0, utils_1.ensurePresence)(this.options.def);
33
+ }
34
+ get payloadOptions() {
35
+ return this.options?.payloadOptions || {};
36
+ }
37
+ toPayload(...args) {
38
+ return {};
39
+ }
40
+ }
41
+ exports.SignedExtension = SignedExtension;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./SignedExtension.js"), exports);
18
+ __exportStar(require("./known/index.js"), exports);
19
+ __exportStar(require("./ExtraSignedExtension.js"), exports);
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChargeAssetTxPayment = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const SignedExtension_js_1 = require("../SignedExtension.js");
6
+ /**
7
+ * @name ChargeAssetTxPayment
8
+ */
9
+ class ChargeAssetTxPayment extends SignedExtension_js_1.SignedExtension {
10
+ async init() {
11
+ const { tip, assetId } = this.payloadOptions;
12
+ this.data = {
13
+ tip: tip || 0n,
14
+ assetId,
15
+ };
16
+ }
17
+ toPayload() {
18
+ const { tip, assetId } = this.data;
19
+ return {
20
+ tip: (0, utils_1.bnToHex)(tip),
21
+ // @ts-ignore
22
+ assetId: this.#encodeAssetId(assetId),
23
+ };
24
+ }
25
+ #encodeAssetId(assetId) {
26
+ if (assetId === null || assetId === undefined) {
27
+ return undefined;
28
+ }
29
+ return (0, utils_1.u8aToHex)(this.$AssetId().tryEncode(assetId));
30
+ }
31
+ $AssetId() {
32
+ const extensionTypeDef = this.registry.findType(this.signedExtensionDef.typeId);
33
+ (0, utils_1.assert)(extensionTypeDef.type.tag === 'Struct');
34
+ const assetIdTypeDef = extensionTypeDef.type.value.fields.find((f) => f.name === 'asset_id');
35
+ const $codec = this.registry.findCodec(assetIdTypeDef.typeId);
36
+ const codecMetadata = $codec.metadata[0];
37
+ if (codecMetadata.name === '$.option') {
38
+ return codecMetadata.args[0]; // inner shape
39
+ }
40
+ return $codec;
41
+ }
42
+ }
43
+ exports.ChargeAssetTxPayment = ChargeAssetTxPayment;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChargeTransactionPayment = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const SignedExtension_js_1 = require("../SignedExtension.js");
6
+ class ChargeTransactionPayment extends SignedExtension_js_1.SignedExtension {
7
+ async init() {
8
+ this.data = this.payloadOptions.tip || 0n;
9
+ }
10
+ toPayload() {
11
+ return {
12
+ tip: (0, utils_1.bnToHex)(this.data),
13
+ };
14
+ }
15
+ }
16
+ exports.ChargeTransactionPayment = ChargeTransactionPayment;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CheckGenesis = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const SignedExtension_js_1 = require("../SignedExtension.js");
6
+ /**
7
+ * @description Genesis hash check to provide replay protection between different networks.
8
+ */
9
+ class CheckGenesis extends SignedExtension_js_1.SignedExtension {
10
+ async init() {
11
+ this.additionalSigned = (0, utils_1.ensurePresence)(this.api.genesisHash);
12
+ }
13
+ toPayload() {
14
+ return {
15
+ genesisHash: this.additionalSigned,
16
+ };
17
+ }
18
+ }
19
+ exports.CheckGenesis = CheckGenesis;
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CheckMortality = exports.MORTAL_PERIOD = exports.FALLBACK_PERIOD = exports.FALLBACK_MAX_HASH_COUNT = exports.MAX_FINALITY_LAG = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const SignedExtension_js_1 = require("../SignedExtension.js");
6
+ exports.MAX_FINALITY_LAG = 5;
7
+ exports.FALLBACK_MAX_HASH_COUNT = 250;
8
+ exports.FALLBACK_PERIOD = 6 * 1000;
9
+ exports.MORTAL_PERIOD = 5 * 60 * 1000;
10
+ /**
11
+ * @description Check for transaction mortality.
12
+ */
13
+ class CheckMortality extends SignedExtension_js_1.SignedExtension {
14
+ #signingHeader;
15
+ async init() {
16
+ this.#signingHeader = await this.#getSigningHeader();
17
+ this.data = { period: this.#calculateMortalLength(), current: BigInt(this.#signingHeader.number) };
18
+ this.additionalSigned = this.#signingHeader.hash;
19
+ }
20
+ async #getSigningHeader() {
21
+ if (this.api.rpcVersion === 'v2') {
22
+ return this.#getSigningHeaderRpcV2();
23
+ }
24
+ return this.#getSigningHeaderViaLegacyRpc();
25
+ }
26
+ async #getSigningHeaderRpcV2() {
27
+ const api = this.api;
28
+ // TODO similar to the legacy, we should account for the case finality is lagging behind
29
+ const finalizedBlock = await api.chainHead.finalizedBlock();
30
+ return {
31
+ hash: finalizedBlock.hash,
32
+ number: finalizedBlock.number,
33
+ };
34
+ }
35
+ // Ref: https://github.com/polkadot-js/api/blob/3bdf49b0428a62f16b3222b9a31bfefa43c1ca55/packages/api-derive/src/tx/signingInfo.ts#L34-L64
36
+ async #getSigningHeaderViaLegacyRpc() {
37
+ const [header, finalizedHash] = await Promise.all([
38
+ this.api.rpc.chain_getHeader(),
39
+ this.api.rpc.chain_getFinalizedHead(),
40
+ ]);
41
+ (0, utils_1.assert)(header, 'Current header not found');
42
+ const [currentHeader, finalizedHeader] = await Promise.all([
43
+ Promise.resolve(header).then((header) => {
44
+ const { parentHash } = header;
45
+ if (parentHash.length === 0 || (0, utils_1.isZeroHex)(parentHash)) {
46
+ return header;
47
+ }
48
+ else {
49
+ return this.api.rpc.chain_getHeader(parentHash);
50
+ }
51
+ }),
52
+ this.api.rpc.chain_getHeader(finalizedHash),
53
+ ]);
54
+ (0, utils_1.assert)(currentHeader, 'Cannot determine current header');
55
+ if (!finalizedHeader || currentHeader.number - finalizedHeader.number > exports.MAX_FINALITY_LAG) {
56
+ return this.#toSigningHeader(currentHeader);
57
+ }
58
+ return this.#toSigningHeader(finalizedHeader);
59
+ }
60
+ async #toSigningHeader(header) {
61
+ const { number } = header;
62
+ const hash = (await this.api.rpc.chain_getBlockHash(header.number));
63
+ return { hash, number };
64
+ }
65
+ // Ref: https://github.com/polkadot-js/api/blob/3bdf49b0428a62f16b3222b9a31bfefa43c1ca55/packages/api-derive/src/tx/signingInfo.ts#L83-L93
66
+ #calculateMortalLength() {
67
+ return (0, utils_1.bnMin)(BigInt(this.#getConst('system', 'blockHashCount') || exports.FALLBACK_MAX_HASH_COUNT), BigInt(exports.MORTAL_PERIOD) /
68
+ BigInt(this.#getConst('babe', 'expectedBlockTime') ||
69
+ BigInt(this.#getConst('timestamp', 'minimumPeriod') || 0) * 2n ||
70
+ exports.FALLBACK_PERIOD) +
71
+ BigInt(exports.MAX_FINALITY_LAG));
72
+ }
73
+ #getConst(pallet, name) {
74
+ try {
75
+ return this.api.consts[pallet][name];
76
+ }
77
+ catch {
78
+ // ignore this on purpose
79
+ }
80
+ return undefined;
81
+ }
82
+ toPayload() {
83
+ return {
84
+ era: (0, utils_1.u8aToHex)(this.$Data.tryEncode(this.data)),
85
+ blockHash: this.additionalSigned,
86
+ blockNumber: (0, utils_1.numberToHex)(this.#signingHeader.number),
87
+ };
88
+ }
89
+ }
90
+ exports.CheckMortality = CheckMortality;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CheckNonZeroSender = void 0;
4
+ const SignedExtension_js_1 = require("../SignedExtension.js");
5
+ /**
6
+ * @description Check to ensure that the sender is not the zero address
7
+ */
8
+ class CheckNonZeroSender extends SignedExtension_js_1.SignedExtension {
9
+ }
10
+ exports.CheckNonZeroSender = CheckNonZeroSender;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CheckNonce = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const SignedExtension_js_1 = require("../SignedExtension.js");
6
+ /**
7
+ * @description Nonce check and increment to give replay protection for transactions.
8
+ */
9
+ class CheckNonce extends SignedExtension_js_1.SignedExtension {
10
+ async init() {
11
+ this.data = this.payloadOptions.nonce || (await this.#getNonce());
12
+ }
13
+ async #getNonce() {
14
+ const { signerAddress } = this.options || {};
15
+ (0, utils_1.assert)(signerAddress, 'Signer address not found');
16
+ try {
17
+ return (await this.api.query.system.account(signerAddress)).nonce;
18
+ }
19
+ catch { }
20
+ try {
21
+ return await this.api.call.accountNonceApi.accountNonce(signerAddress);
22
+ }
23
+ catch { }
24
+ // TODO fallback to api.rpc.system_accountNextIndex if needed
25
+ return 0;
26
+ }
27
+ toPayload() {
28
+ return {
29
+ nonce: (0, utils_1.numberToHex)(this.data),
30
+ };
31
+ }
32
+ }
33
+ exports.CheckNonce = CheckNonce;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CheckSpecVersion = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const SignedExtension_js_1 = require("../SignedExtension.js");
6
+ /**
7
+ * @description Ensure the runtime version registered in the transaction is the same as at present.
8
+ */
9
+ class CheckSpecVersion extends SignedExtension_js_1.SignedExtension {
10
+ async init() {
11
+ this.additionalSigned = this.api.runtimeVersion.specVersion;
12
+ }
13
+ toPayload() {
14
+ return {
15
+ specVersion: (0, utils_1.numberToHex)(this.additionalSigned),
16
+ };
17
+ }
18
+ }
19
+ exports.CheckSpecVersion = CheckSpecVersion;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CheckTxVersion = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const SignedExtension_js_1 = require("../SignedExtension.js");
6
+ /**
7
+ * @description Ensure the transaction version registered in the transaction is the same as at present.
8
+ */
9
+ class CheckTxVersion extends SignedExtension_js_1.SignedExtension {
10
+ async init() {
11
+ this.additionalSigned = this.api.runtimeVersion.transactionVersion;
12
+ }
13
+ toPayload() {
14
+ return {
15
+ transactionVersion: (0, utils_1.numberToHex)(this.additionalSigned),
16
+ };
17
+ }
18
+ }
19
+ exports.CheckTxVersion = CheckTxVersion;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CheckWeight = void 0;
4
+ const SignedExtension_js_1 = require("../SignedExtension.js");
5
+ /**
6
+ * @description Block resource (weight) limit check.
7
+ */
8
+ class CheckWeight extends SignedExtension_js_1.SignedExtension {
9
+ }
10
+ exports.CheckWeight = CheckWeight;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PrevalidateAttests = void 0;
4
+ const SignedExtension_js_1 = require("../SignedExtension.js");
5
+ /**
6
+ * @description Validate `attest` calls prior to execution.
7
+ * Needed to avoid a DoS attack since they are otherwise free to place on chain.
8
+ */
9
+ class PrevalidateAttests extends SignedExtension_js_1.SignedExtension {
10
+ }
11
+ exports.PrevalidateAttests = PrevalidateAttests;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.knownSignedExtensions = void 0;
4
+ const ChargeAssetTxPayment_js_1 = require("./ChargeAssetTxPayment.js");
5
+ const ChargeTransactionPayment_js_1 = require("./ChargeTransactionPayment.js");
6
+ const CheckGenesis_js_1 = require("./CheckGenesis.js");
7
+ const CheckMortality_js_1 = require("./CheckMortality.js");
8
+ const CheckNonZeroSender_js_1 = require("./CheckNonZeroSender.js");
9
+ const CheckNonce_js_1 = require("./CheckNonce.js");
10
+ const CheckSpecVersion_js_1 = require("./CheckSpecVersion.js");
11
+ const CheckTxVersion_js_1 = require("./CheckTxVersion.js");
12
+ const CheckWeight_js_1 = require("./CheckWeight.js");
13
+ const PrevalidateAttests_js_1 = require("./PrevalidateAttests.js");
14
+ exports.knownSignedExtensions = {
15
+ CheckNonZeroSender: CheckNonZeroSender_js_1.CheckNonZeroSender,
16
+ CheckSpecVersion: CheckSpecVersion_js_1.CheckSpecVersion,
17
+ CheckTxVersion: CheckTxVersion_js_1.CheckTxVersion,
18
+ CheckGenesis: CheckGenesis_js_1.CheckGenesis,
19
+ CheckMortality: CheckMortality_js_1.CheckMortality,
20
+ CheckNonce: CheckNonce_js_1.CheckNonce,
21
+ CheckWeight: CheckWeight_js_1.CheckWeight,
22
+ ChargeTransactionPayment: ChargeTransactionPayment_js_1.ChargeTransactionPayment,
23
+ PrevalidateAttests: PrevalidateAttests_js_1.PrevalidateAttests,
24
+ ChargeAssetTxPayment: ChargeAssetTxPayment_js_1.ChargeAssetTxPayment,
25
+ };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./extensions/index.js"), exports);
18
+ __exportStar(require("./submittable/index.js"), exports);