dedot 0.0.1-alpha.22

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 (100) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +245 -0
  3. package/cjs/client/Dedot.js +444 -0
  4. package/cjs/client/index.js +17 -0
  5. package/cjs/executor/ConstantExecutor.js +22 -0
  6. package/cjs/executor/ErrorExecutor.js +47 -0
  7. package/cjs/executor/EventExecutor.js +53 -0
  8. package/cjs/executor/Executor.js +39 -0
  9. package/cjs/executor/RpcExecutor.js +109 -0
  10. package/cjs/executor/RuntimeApiExecutor.js +93 -0
  11. package/cjs/executor/StorageQueryExecutor.js +33 -0
  12. package/cjs/executor/TxExecutor.js +153 -0
  13. package/cjs/executor/index.js +24 -0
  14. package/cjs/extrinsic/SubmittableResult.js +33 -0
  15. package/cjs/extrinsic/extensions/ExtraSignedExtension.js +65 -0
  16. package/cjs/extrinsic/extensions/SignedExtension.js +41 -0
  17. package/cjs/extrinsic/extensions/index.js +19 -0
  18. package/cjs/extrinsic/extensions/known/ChargeAssetTxPayment.js +44 -0
  19. package/cjs/extrinsic/extensions/known/ChargeTransactionPayment.js +16 -0
  20. package/cjs/extrinsic/extensions/known/CheckGenesis.js +19 -0
  21. package/cjs/extrinsic/extensions/known/CheckMortality.js +71 -0
  22. package/cjs/extrinsic/extensions/known/CheckNonZeroSender.js +10 -0
  23. package/cjs/extrinsic/extensions/known/CheckNonce.js +34 -0
  24. package/cjs/extrinsic/extensions/known/CheckSpecVersion.js +19 -0
  25. package/cjs/extrinsic/extensions/known/CheckTxVersion.js +19 -0
  26. package/cjs/extrinsic/extensions/known/CheckWeight.js +10 -0
  27. package/cjs/extrinsic/extensions/known/PrevalidateAttests.js +11 -0
  28. package/cjs/extrinsic/extensions/known/index.js +25 -0
  29. package/cjs/extrinsic/index.js +18 -0
  30. package/cjs/index.js +25 -0
  31. package/cjs/package.json +1 -0
  32. package/cjs/packageInfo.js +5 -0
  33. package/cjs/proxychain.js +20 -0
  34. package/cjs/storage/QueryableStorage.js +99 -0
  35. package/cjs/types.js +2 -0
  36. package/client/Dedot.d.ts +181 -0
  37. package/client/Dedot.js +440 -0
  38. package/client/index.d.ts +1 -0
  39. package/client/index.js +1 -0
  40. package/executor/ConstantExecutor.d.ts +11 -0
  41. package/executor/ConstantExecutor.js +18 -0
  42. package/executor/ErrorExecutor.d.ts +11 -0
  43. package/executor/ErrorExecutor.js +43 -0
  44. package/executor/EventExecutor.d.ts +11 -0
  45. package/executor/EventExecutor.js +49 -0
  46. package/executor/Executor.d.ts +150 -0
  47. package/executor/Executor.js +35 -0
  48. package/executor/RpcExecutor.d.ts +14 -0
  49. package/executor/RpcExecutor.js +105 -0
  50. package/executor/RuntimeApiExecutor.d.ts +9 -0
  51. package/executor/RuntimeApiExecutor.js +89 -0
  52. package/executor/StorageQueryExecutor.d.ts +10 -0
  53. package/executor/StorageQueryExecutor.js +29 -0
  54. package/executor/TxExecutor.d.ts +47 -0
  55. package/executor/TxExecutor.js +147 -0
  56. package/executor/index.d.ts +8 -0
  57. package/executor/index.js +8 -0
  58. package/extrinsic/SubmittableResult.d.ts +19 -0
  59. package/extrinsic/SubmittableResult.js +29 -0
  60. package/extrinsic/extensions/ExtraSignedExtension.d.ts +14 -0
  61. package/extrinsic/extensions/ExtraSignedExtension.js +61 -0
  62. package/extrinsic/extensions/SignedExtension.d.ts +40 -0
  63. package/extrinsic/extensions/SignedExtension.js +37 -0
  64. package/extrinsic/extensions/index.d.ts +3 -0
  65. package/extrinsic/extensions/index.js +3 -0
  66. package/extrinsic/extensions/known/ChargeAssetTxPayment.d.ts +14 -0
  67. package/extrinsic/extensions/known/ChargeAssetTxPayment.js +40 -0
  68. package/extrinsic/extensions/known/ChargeTransactionPayment.d.ts +6 -0
  69. package/extrinsic/extensions/known/ChargeTransactionPayment.js +12 -0
  70. package/extrinsic/extensions/known/CheckGenesis.d.ts +10 -0
  71. package/extrinsic/extensions/known/CheckGenesis.js +15 -0
  72. package/extrinsic/extensions/known/CheckMortality.d.ts +15 -0
  73. package/extrinsic/extensions/known/CheckMortality.js +67 -0
  74. package/extrinsic/extensions/known/CheckNonZeroSender.d.ts +6 -0
  75. package/extrinsic/extensions/known/CheckNonZeroSender.js +6 -0
  76. package/extrinsic/extensions/known/CheckNonce.d.ts +10 -0
  77. package/extrinsic/extensions/known/CheckNonce.js +30 -0
  78. package/extrinsic/extensions/known/CheckSpecVersion.d.ts +9 -0
  79. package/extrinsic/extensions/known/CheckSpecVersion.js +15 -0
  80. package/extrinsic/extensions/known/CheckTxVersion.d.ts +9 -0
  81. package/extrinsic/extensions/known/CheckTxVersion.js +15 -0
  82. package/extrinsic/extensions/known/CheckWeight.d.ts +6 -0
  83. package/extrinsic/extensions/known/CheckWeight.js +6 -0
  84. package/extrinsic/extensions/known/PrevalidateAttests.d.ts +7 -0
  85. package/extrinsic/extensions/known/PrevalidateAttests.js +7 -0
  86. package/extrinsic/extensions/known/index.d.ts +3 -0
  87. package/extrinsic/extensions/known/index.js +22 -0
  88. package/extrinsic/index.d.ts +2 -0
  89. package/extrinsic/index.js +2 -0
  90. package/index.d.ts +6 -0
  91. package/index.js +5 -0
  92. package/package.json +54 -0
  93. package/packageInfo.d.ts +4 -0
  94. package/packageInfo.js +3 -0
  95. package/proxychain.d.ts +7 -0
  96. package/proxychain.js +16 -0
  97. package/storage/QueryableStorage.d.ts +16 -0
  98. package/storage/QueryableStorage.js +95 -0
  99. package/types.d.ts +44 -0
  100. package/types.js +1 -0
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "dedot",
3
+ "version": "0.0.1-alpha.22",
4
+ "description": "A fast & lightweight JavaScript/TypeScript client for Polkadot & Substrate",
5
+ "author": "Thang X. Vu <thang@coongcrafts.io>",
6
+ "homepage": "https://github.com/dedotdev/dedot",
7
+ "repository": {
8
+ "directory": "packages/api",
9
+ "type": "git",
10
+ "url": "https://github.com/dedotdev/dedot.git"
11
+ },
12
+ "main": "./cjs/index.js",
13
+ "type": "module",
14
+ "dependencies": {
15
+ "@dedot/chaintypes": "0.0.1-alpha.22",
16
+ "@dedot/codecs": "0.0.1-alpha.22",
17
+ "@dedot/shape": "0.0.1-alpha.22",
18
+ "@dedot/specs": "0.0.1-alpha.22",
19
+ "@dedot/utils": "0.0.1-alpha.22",
20
+ "@polkadot/rpc-provider": "^10.11.2",
21
+ "@polkadot/util": "^12.6.2",
22
+ "@polkadot/util-crypto": "^12.6.2",
23
+ "localforage": "^1.10.0"
24
+ },
25
+ "scripts": {
26
+ "build": "tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json",
27
+ "clean": "rm -rf ./dist && rm -rf ./tsconfig.tsbuildinfo ./tsconfig.build.tsbuildinfo",
28
+ "test": "vitest --watch=false"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public",
32
+ "directory": "dist"
33
+ },
34
+ "license": "Apache-2.0",
35
+ "devDependencies": {
36
+ "@polkadot/keyring": "^12.6.2",
37
+ "@polkadot/types": "^10.11.2"
38
+ },
39
+ "gitHead": "535f3ade0fa48d6d8aeabb1a1c9ed26b82e9e836",
40
+ "module": "./index.js",
41
+ "types": "./index.d.ts",
42
+ "exports": {
43
+ ".": {
44
+ "import": {
45
+ "types": "./index.d.ts",
46
+ "default": "./index.js"
47
+ },
48
+ "require": {
49
+ "types": "./index.d.ts",
50
+ "default": "./cjs/index.js"
51
+ }
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,4 @@
1
+ export declare const packageInfo: {
2
+ name: string;
3
+ version: string;
4
+ };
package/packageInfo.js ADDED
@@ -0,0 +1,3 @@
1
+ // THIS FILE IS AUTO-GENERATED, DO NOT EDIT!
2
+
3
+ export const packageInfo = { name: 'dedot', version: '0.0.1-alpha.22' };
@@ -0,0 +1,7 @@
1
+ import { GenericSubstrateApi } from '@dedot/types';
2
+ import { Executor } from './executor';
3
+ export interface Carrier<ChainApi extends GenericSubstrateApi> {
4
+ executor: Executor<ChainApi>;
5
+ chain?: string[];
6
+ }
7
+ export declare const newProxyChain: <ChainApi extends GenericSubstrateApi>(carrier: Carrier<ChainApi>, currentLevel?: number, maxLevel?: number) => unknown;
package/proxychain.js ADDED
@@ -0,0 +1,16 @@
1
+ export const newProxyChain = (carrier, currentLevel = 1, maxLevel = 3) => {
2
+ const { executor, chain = [] } = carrier;
3
+ if (currentLevel === maxLevel) {
4
+ return executor.execute(...chain);
5
+ }
6
+ return new Proxy(carrier, {
7
+ get(target, property, receiver) {
8
+ if (!target.chain) {
9
+ target.chain = [];
10
+ }
11
+ const { chain } = target;
12
+ chain.push(property.toString());
13
+ return newProxyChain(target, currentLevel + 1);
14
+ },
15
+ });
16
+ };
@@ -0,0 +1,16 @@
1
+ import { CodecRegistry, PalletDefLatest, StorageDataLike, StorageEntryLatest, StorageKey } from '@dedot/codecs';
2
+ /**
3
+ * @name QueryableStorage
4
+ * @description A helper to encode key & decode value for a storage entry
5
+ */
6
+ export declare class QueryableStorage {
7
+ #private;
8
+ readonly registry: CodecRegistry;
9
+ readonly palletName: string;
10
+ readonly storageItem: string;
11
+ readonly pallet: PalletDefLatest;
12
+ readonly storageEntry: StorageEntryLatest;
13
+ constructor(registry: CodecRegistry, palletName: string, storageItem: string);
14
+ encodeKey(keyInput?: any): StorageKey;
15
+ decodeValue(raw?: StorageDataLike | null): any;
16
+ }
@@ -0,0 +1,95 @@
1
+ import { assert, HASHERS } from '@dedot/utils';
2
+ import { $StorageData, } from '@dedot/codecs';
3
+ import { hexToU8a, stringCamelCase, u8aConcat, u8aToHex } from '@polkadot/util';
4
+ import { xxhashAsU8a } from '@polkadot/util-crypto';
5
+ /**
6
+ * @name QueryableStorage
7
+ * @description A helper to encode key & decode value for a storage entry
8
+ */
9
+ export class QueryableStorage {
10
+ registry;
11
+ palletName;
12
+ storageItem;
13
+ pallet;
14
+ storageEntry;
15
+ constructor(registry, palletName, storageItem) {
16
+ this.registry = registry;
17
+ this.palletName = palletName;
18
+ this.storageItem = storageItem;
19
+ this.pallet = this.#getPallet();
20
+ this.storageEntry = this.#getStorageEntry();
21
+ }
22
+ encodeKey(keyInput) {
23
+ const palletNameHash = xxhashAsU8a(this.pallet.name, 128);
24
+ const storageItemHash = xxhashAsU8a(this.storageEntry.name, 128);
25
+ const prefixHash = u8aConcat(palletNameHash, storageItemHash);
26
+ const { type } = this.storageEntry;
27
+ if (type.tag === 'Plain') {
28
+ return u8aToHex(prefixHash);
29
+ }
30
+ else if (type.tag === 'Map') {
31
+ const { hashers, keyTypeId } = type.value;
32
+ const extractedInputs = this.#extractRequiredKeyInputs(keyInput, hashers.length);
33
+ let keyTypeIds = [keyTypeId];
34
+ if (hashers.length > 1) {
35
+ const { type } = this.registry.findPortableType(keyTypeId);
36
+ assert(type.tag === 'Tuple', 'Key type should be a tuple!');
37
+ keyTypeIds = type.value.fields;
38
+ }
39
+ const keyParts = keyTypeIds.map((keyId, index) => {
40
+ const input = extractedInputs[index];
41
+ const hasher = HASHERS[hashers[index]];
42
+ const $keyCodec = this.registry.findPortableCodec(keyId);
43
+ return hasher($keyCodec.tryEncode(input));
44
+ });
45
+ return u8aToHex(u8aConcat(prefixHash, ...keyParts));
46
+ }
47
+ throw Error(`Invalid storage entry type: ${type}`);
48
+ }
49
+ decodeValue(raw) {
50
+ const { modifier, type: { value: { valueTypeId }, }, default: defaultValue, } = this.storageEntry;
51
+ if (raw === null || raw === undefined) {
52
+ if (modifier === 'Optional') {
53
+ return undefined;
54
+ }
55
+ else if (modifier === 'Default') {
56
+ return this.registry.findPortableCodec(valueTypeId).tryDecode(hexToU8a(defaultValue));
57
+ }
58
+ }
59
+ else {
60
+ return this.registry.findPortableCodec(valueTypeId).tryDecode($StorageData.tryEncode(raw));
61
+ }
62
+ }
63
+ #extractRequiredKeyInputs(keyInput, numberOfValue) {
64
+ if (numberOfValue === 0) {
65
+ return [];
66
+ }
67
+ else {
68
+ if (keyInput === undefined) {
69
+ throw new Error(`Invalid key inputs, required ${numberOfValue} input(s)`);
70
+ }
71
+ if (numberOfValue === 1) {
72
+ return [keyInput];
73
+ }
74
+ else {
75
+ if (!Array.isArray(keyInput)) {
76
+ throw new Error(`Input should be an array with ${numberOfValue} value(s)`);
77
+ }
78
+ if (keyInput.length !== numberOfValue) {
79
+ throw new Error(`Mismatch key inputs length, required an array of ${numberOfValue} value(s)`);
80
+ }
81
+ return keyInput.slice(0, numberOfValue);
82
+ }
83
+ }
84
+ }
85
+ #getPallet() {
86
+ const targetPallet = this.registry.metadata.pallets.find((p) => stringCamelCase(p.name) === stringCamelCase(this.palletName));
87
+ assert(targetPallet, `Pallet not found: ${this.palletName}`);
88
+ return targetPallet;
89
+ }
90
+ #getStorageEntry() {
91
+ const targetEntry = this.pallet.storage?.entries?.find((entry) => stringCamelCase(entry.name) === stringCamelCase(this.storageItem));
92
+ assert(targetEntry, `Storage item not found: ${this.storageItem}`);
93
+ return targetEntry;
94
+ }
95
+ }
package/types.d.ts ADDED
@@ -0,0 +1,44 @@
1
+ import { ProviderInterface } from '@polkadot/rpc-provider/types';
2
+ import { HexString } from '@dedot/utils';
3
+ import { AnySignedExtension } from './extrinsic';
4
+ export type NetworkEndpoint = string;
5
+ export type MetadataKey = `RAW_META/${string}`;
6
+ export interface ApiOptions {
7
+ provider?: ProviderInterface;
8
+ /**
9
+ * @description A `ProviderInterface` will be created based on the supplied endpoint.
10
+ * If both `provider` and `endpoint` is provided, the `provider` will be used for connection.
11
+ *
12
+ * A valid endpoint should start with `wss://`, `ws://`, `https://` or `http://`
13
+ */
14
+ endpoint?: NetworkEndpoint;
15
+ /**
16
+ * @description Cache metadata in local storage for next time usage
17
+ * For now this only supports browser environments
18
+ *
19
+ * @default: false
20
+ */
21
+ cacheMetadata?: boolean;
22
+ /**
23
+ * @description Metadata is usually downloaded from chain via RPC upon API initialization,
24
+ * We can supply the metadata directly via this option to skip the download metadata step.
25
+ *
26
+ * Metadata Key format: `RAW_META/{genesisHash}/{runtimeSpecVersion}`
27
+ *
28
+ * If the `genesisHash` & `runtimeSpecVersion` of the supplied metadata key match with connected chain,
29
+ * then use the provided metadata, else we fetch it anew from chain.
30
+ *
31
+ * Catch-all Metadata Key (`RAW_META/ALL`) will be used
32
+ * regardless of the `genesisHash` or `runtimeSpecVersion` of connected chain.
33
+ *
34
+ * If we supplied a raw-hex metadata to this option, it's a catch-all metadata.
35
+ */
36
+ metadata?: HexString | Record<MetadataKey, HexString>;
37
+ /**
38
+ * @description User-defined chain-specific signed extensions
39
+ */
40
+ signedExtensions?: Record<string, AnySignedExtension>;
41
+ }
42
+ export interface NormalizedApiOptions extends ApiOptions {
43
+ metadata?: Record<string, HexString>;
44
+ }
package/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};