dedot 0.0.1-next.fecbe32d.13 → 0.1.0
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.
- package/README.md +80 -86
- package/bin/dedot +3 -0
- package/chaintypes/index.d.ts +1 -1
- package/chaintypes/index.js +1 -1
- package/cjs/chaintypes/index.js +1 -1
- package/cjs/{json-rpc → codecs}/index.js +1 -1
- package/cjs/{storage → contracts}/index.js +1 -1
- package/cjs/index.js +6 -15
- package/cjs/runtime-specs/index.js +17 -0
- package/cjs/{chaintypes/substrate → shape}/index.js +1 -1
- package/cjs/{client → types}/index.js +1 -1
- package/cjs/types/json-rpc/index.js +17 -0
- package/cjs/utils/index.js +17 -0
- package/codecs/index.d.ts +1 -0
- package/codecs/index.js +1 -0
- package/contracts/index.d.ts +1 -0
- package/contracts/index.js +1 -0
- package/index.d.ts +2 -9
- package/index.js +2 -9
- package/package.json +76 -36
- package/runtime-specs/index.d.ts +1 -0
- package/runtime-specs/index.js +1 -0
- package/shape/index.d.ts +1 -0
- package/shape/index.js +1 -0
- package/types/index.d.ts +1 -0
- package/types/index.js +1 -0
- package/types/json-rpc/index.d.ts +1 -0
- package/types/json-rpc/index.js +1 -0
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/chaintypes/substrate/consts.d.ts +0 -1971
- package/chaintypes/substrate/consts.js +0 -1
- package/chaintypes/substrate/errors.d.ts +0 -3337
- package/chaintypes/substrate/errors.js +0 -1
- package/chaintypes/substrate/events.d.ts +0 -4777
- package/chaintypes/substrate/events.js +0 -1
- package/chaintypes/substrate/index.d.ts +0 -18
- package/chaintypes/substrate/index.js +0 -1
- package/chaintypes/substrate/json-rpc.d.ts +0 -3
- package/chaintypes/substrate/json-rpc.js +0 -1
- package/chaintypes/substrate/query.d.ts +0 -3447
- package/chaintypes/substrate/query.js +0 -1
- package/chaintypes/substrate/runtime.d.ts +0 -708
- package/chaintypes/substrate/runtime.js +0 -1
- package/chaintypes/substrate/tx.d.ts +0 -8800
- package/chaintypes/substrate/tx.js +0 -1
- package/chaintypes/substrate/types.d.ts +0 -20332
- package/chaintypes/substrate/types.js +0 -1
- package/cjs/chaintypes/substrate/consts.js +0 -2
- package/cjs/chaintypes/substrate/errors.js +0 -2
- package/cjs/chaintypes/substrate/events.js +0 -2
- package/cjs/chaintypes/substrate/json-rpc.js +0 -2
- package/cjs/chaintypes/substrate/query.js +0 -2
- package/cjs/chaintypes/substrate/runtime.js +0 -2
- package/cjs/chaintypes/substrate/tx.js +0 -2
- package/cjs/chaintypes/substrate/types.js +0 -2
- package/cjs/client/BaseSubstrateClient.js +0 -247
- package/cjs/client/Dedot.js +0 -257
- package/cjs/executor/ConstantExecutor.js +0 -20
- package/cjs/executor/ErrorExecutor.js +0 -46
- package/cjs/executor/EventExecutor.js +0 -52
- package/cjs/executor/Executor.js +0 -45
- package/cjs/executor/RuntimeApiExecutor.js +0 -113
- package/cjs/executor/StorageQueryExecutor.js +0 -109
- package/cjs/executor/TxExecutor.js +0 -158
- package/cjs/executor/index.js +0 -23
- package/cjs/extrinsic/SubmittableResult.js +0 -33
- package/cjs/extrinsic/extensions/ExtraSignedExtension.js +0 -87
- package/cjs/extrinsic/extensions/SignedExtension.js +0 -41
- package/cjs/extrinsic/extensions/index.js +0 -19
- package/cjs/extrinsic/extensions/known/ChargeAssetTxPayment.js +0 -43
- package/cjs/extrinsic/extensions/known/ChargeTransactionPayment.js +0 -16
- package/cjs/extrinsic/extensions/known/CheckGenesis.js +0 -19
- package/cjs/extrinsic/extensions/known/CheckMortality.js +0 -70
- package/cjs/extrinsic/extensions/known/CheckNonZeroSender.js +0 -10
- package/cjs/extrinsic/extensions/known/CheckNonce.js +0 -33
- package/cjs/extrinsic/extensions/known/CheckSpecVersion.js +0 -19
- package/cjs/extrinsic/extensions/known/CheckTxVersion.js +0 -19
- package/cjs/extrinsic/extensions/known/CheckWeight.js +0 -10
- package/cjs/extrinsic/extensions/known/PrevalidateAttests.js +0 -11
- package/cjs/extrinsic/extensions/known/index.js +0 -25
- package/cjs/extrinsic/index.js +0 -18
- package/cjs/json-rpc/JsonRpcClient.js +0 -157
- package/cjs/proxychain.js +0 -27
- package/cjs/storage/QueryableStorage.js +0 -161
- package/cjs/types.js +0 -2
- package/client/BaseSubstrateClient.d.ts +0 -65
- package/client/BaseSubstrateClient.js +0 -242
- package/client/Dedot.d.ts +0 -130
- package/client/Dedot.js +0 -253
- package/client/index.d.ts +0 -1
- package/client/index.js +0 -1
- package/executor/ConstantExecutor.d.ts +0 -11
- package/executor/ConstantExecutor.js +0 -16
- package/executor/ErrorExecutor.d.ts +0 -11
- package/executor/ErrorExecutor.js +0 -42
- package/executor/EventExecutor.d.ts +0 -11
- package/executor/EventExecutor.js +0 -48
- package/executor/Executor.d.ts +0 -150
- package/executor/Executor.js +0 -41
- package/executor/RuntimeApiExecutor.d.ts +0 -19
- package/executor/RuntimeApiExecutor.js +0 -109
- package/executor/StorageQueryExecutor.d.ts +0 -13
- package/executor/StorageQueryExecutor.js +0 -105
- package/executor/TxExecutor.d.ts +0 -48
- package/executor/TxExecutor.js +0 -152
- package/executor/index.d.ts +0 -7
- package/executor/index.js +0 -7
- package/extrinsic/SubmittableResult.d.ts +0 -19
- package/extrinsic/SubmittableResult.js +0 -29
- package/extrinsic/extensions/ExtraSignedExtension.d.ts +0 -14
- package/extrinsic/extensions/ExtraSignedExtension.js +0 -60
- package/extrinsic/extensions/SignedExtension.d.ts +0 -40
- package/extrinsic/extensions/SignedExtension.js +0 -37
- package/extrinsic/extensions/index.d.ts +0 -3
- package/extrinsic/extensions/index.js +0 -3
- package/extrinsic/extensions/known/ChargeAssetTxPayment.d.ts +0 -14
- package/extrinsic/extensions/known/ChargeAssetTxPayment.js +0 -39
- package/extrinsic/extensions/known/ChargeTransactionPayment.d.ts +0 -6
- package/extrinsic/extensions/known/ChargeTransactionPayment.js +0 -12
- package/extrinsic/extensions/known/CheckGenesis.d.ts +0 -10
- package/extrinsic/extensions/known/CheckGenesis.js +0 -15
- package/extrinsic/extensions/known/CheckMortality.d.ts +0 -15
- package/extrinsic/extensions/known/CheckMortality.js +0 -66
- package/extrinsic/extensions/known/CheckNonZeroSender.d.ts +0 -6
- package/extrinsic/extensions/known/CheckNonZeroSender.js +0 -6
- package/extrinsic/extensions/known/CheckNonce.d.ts +0 -10
- package/extrinsic/extensions/known/CheckNonce.js +0 -29
- package/extrinsic/extensions/known/CheckSpecVersion.d.ts +0 -9
- package/extrinsic/extensions/known/CheckSpecVersion.js +0 -15
- package/extrinsic/extensions/known/CheckTxVersion.d.ts +0 -9
- package/extrinsic/extensions/known/CheckTxVersion.js +0 -15
- package/extrinsic/extensions/known/CheckWeight.d.ts +0 -6
- package/extrinsic/extensions/known/CheckWeight.js +0 -6
- package/extrinsic/extensions/known/PrevalidateAttests.d.ts +0 -7
- package/extrinsic/extensions/known/PrevalidateAttests.js +0 -7
- package/extrinsic/extensions/known/index.d.ts +0 -3
- package/extrinsic/extensions/known/index.js +0 -22
- package/extrinsic/index.d.ts +0 -2
- package/extrinsic/index.js +0 -2
- package/json-rpc/JsonRpcClient.d.ts +0 -49
- package/json-rpc/JsonRpcClient.js +0 -153
- package/json-rpc/index.d.ts +0 -1
- package/json-rpc/index.js +0 -1
- package/proxychain.d.ts +0 -14
- package/proxychain.js +0 -23
- package/storage/QueryableStorage.d.ts +0 -35
- package/storage/QueryableStorage.js +0 -157
- package/storage/index.d.ts +0 -1
- package/storage/index.js +0 -1
- package/types.d.ts +0 -103
- package/types.js +0 -1
package/executor/TxExecutor.js
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import { Extrinsic } from '@dedot/codecs';
|
|
2
|
-
import { assert, blake2AsU8a, hexToU8a, isFunction, isHex, stringCamelCase, stringPascalCase, u8aToHex, UnknownApiError, } from '@dedot/utils';
|
|
3
|
-
import { ExtraSignedExtension, SubmittableResult } from '../extrinsic/index.js';
|
|
4
|
-
import { Executor } from './Executor.js';
|
|
5
|
-
import { StorageQueryExecutor } from './StorageQueryExecutor.js';
|
|
6
|
-
export function isKeyringPair(account) {
|
|
7
|
-
return isFunction(account.sign);
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Sign a raw message
|
|
11
|
-
* @param signerPair
|
|
12
|
-
* @param raw
|
|
13
|
-
* @param options
|
|
14
|
-
*/
|
|
15
|
-
export function signRaw(signerPair, raw, options) {
|
|
16
|
-
const u8a = hexToU8a(raw);
|
|
17
|
-
// Ref: https://github.com/paritytech/polkadot-sdk/blob/943697fa693a4da6ef481ef93df522accb7d0583/substrate/primitives/runtime/src/generic/unchecked_extrinsic.rs#L234-L238
|
|
18
|
-
const toSignRaw = u8a.length > 256 ? blake2AsU8a(u8a, 256) : u8a;
|
|
19
|
-
return signerPair.sign(toSignRaw, options);
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* @name TxExecutor
|
|
23
|
-
* @description Execute a transaction instruction, returns a submittable extrinsic
|
|
24
|
-
*/
|
|
25
|
-
export class TxExecutor extends Executor {
|
|
26
|
-
doExecute(pallet, functionName) {
|
|
27
|
-
const targetPallet = this.getPallet(pallet);
|
|
28
|
-
assert(targetPallet.calls, new UnknownApiError(`Tx calls are not available for pallet ${targetPallet.name}`));
|
|
29
|
-
const txType = this.metadata.types[targetPallet.calls];
|
|
30
|
-
assert(txType.type.tag === 'Enum', new UnknownApiError('Tx type defs should be enum'));
|
|
31
|
-
const isFlatEnum = txType.type.value.members.every((m) => m.fields.length === 0);
|
|
32
|
-
const txCallDef = txType.type.value.members.find((m) => stringCamelCase(m.name) === functionName);
|
|
33
|
-
assert(txCallDef, new UnknownApiError(`Tx call spec not found for ${pallet}.${functionName}`));
|
|
34
|
-
const txCallFn = (...args) => {
|
|
35
|
-
let call;
|
|
36
|
-
if (isFlatEnum) {
|
|
37
|
-
call = {
|
|
38
|
-
pallet: stringPascalCase(targetPallet.name),
|
|
39
|
-
palletCall: stringPascalCase(txCallDef.name),
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
const callParams = txCallDef.fields.reduce((o, { name }, idx) => {
|
|
44
|
-
o[stringCamelCase(name)] = args[idx];
|
|
45
|
-
return o;
|
|
46
|
-
}, {});
|
|
47
|
-
call = {
|
|
48
|
-
pallet: stringPascalCase(targetPallet.name),
|
|
49
|
-
palletCall: {
|
|
50
|
-
name: stringPascalCase(txCallDef.name),
|
|
51
|
-
params: callParams,
|
|
52
|
-
},
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
return this.createExtrinsic(call);
|
|
56
|
-
};
|
|
57
|
-
txCallFn.meta = {
|
|
58
|
-
...txCallDef,
|
|
59
|
-
fieldCodecs: txCallDef.fields.map(({ typeId }) => this.registry.findCodec(typeId)),
|
|
60
|
-
pallet: targetPallet.name,
|
|
61
|
-
palletIndex: targetPallet.index,
|
|
62
|
-
};
|
|
63
|
-
return txCallFn;
|
|
64
|
-
}
|
|
65
|
-
createExtrinsic(call) {
|
|
66
|
-
const txExecutor = this;
|
|
67
|
-
const api = this.api;
|
|
68
|
-
class SubmittableExtrinsic extends Extrinsic {
|
|
69
|
-
async sign(fromAccount, options) {
|
|
70
|
-
const address = isKeyringPair(fromAccount) ? fromAccount.address : fromAccount.toString();
|
|
71
|
-
const extra = new ExtraSignedExtension(api, {
|
|
72
|
-
signerAddress: address,
|
|
73
|
-
payloadOptions: options,
|
|
74
|
-
});
|
|
75
|
-
await extra.init();
|
|
76
|
-
const { signer } = options || {};
|
|
77
|
-
let signature;
|
|
78
|
-
if (isKeyringPair(fromAccount)) {
|
|
79
|
-
signature = u8aToHex(signRaw(fromAccount, extra.toRawPayload(this.callHex).data, { withType: true }));
|
|
80
|
-
}
|
|
81
|
-
else if (signer?.signPayload) {
|
|
82
|
-
const result = await signer.signPayload(extra.toPayload(this.callHex));
|
|
83
|
-
signature = result.signature;
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
throw new Error('Signer not found. Cannot sign the extrinsic!');
|
|
87
|
-
}
|
|
88
|
-
const { signatureTypeId } = this.registry.metadata.extrinsic;
|
|
89
|
-
const $Signature = this.registry.findCodec(signatureTypeId);
|
|
90
|
-
this.attachSignature({
|
|
91
|
-
address: address,
|
|
92
|
-
signature: $Signature.tryDecode(signature),
|
|
93
|
-
extra: extra.data,
|
|
94
|
-
});
|
|
95
|
-
return this;
|
|
96
|
-
}
|
|
97
|
-
async signAndSend(fromAccount, partialOptions, maybeCallback) {
|
|
98
|
-
const [options, callback] = this.#normalizeOptions(partialOptions, maybeCallback);
|
|
99
|
-
await this.sign(fromAccount, options);
|
|
100
|
-
return this.send(callback);
|
|
101
|
-
}
|
|
102
|
-
#normalizeOptions(partialOptions, callback) {
|
|
103
|
-
if (isFunction(partialOptions)) {
|
|
104
|
-
return [{}, partialOptions];
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
return [Object.assign({}, partialOptions), callback];
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
async dryRun(account, optionsOrHash) {
|
|
111
|
-
// TODO check if system_dryRun is available
|
|
112
|
-
const dryRunFn = api.rpc.system_dryRun;
|
|
113
|
-
if (isHex(optionsOrHash)) {
|
|
114
|
-
return dryRunFn(this.toHex(), optionsOrHash);
|
|
115
|
-
}
|
|
116
|
-
await this.sign(account, optionsOrHash);
|
|
117
|
-
return dryRunFn(this.toHex());
|
|
118
|
-
}
|
|
119
|
-
async send(callback) {
|
|
120
|
-
const isSubscription = !!callback;
|
|
121
|
-
const txHash = this.hash;
|
|
122
|
-
if (isSubscription) {
|
|
123
|
-
return api.rpc.author_submitAndWatchExtrinsic(this.toHex(), async (status) => {
|
|
124
|
-
if (status.tag === 'InBlock' || status.tag === 'Finalized') {
|
|
125
|
-
const blockHash = status.value;
|
|
126
|
-
const [signedBlock, blockEvents] = await Promise.all([
|
|
127
|
-
api.rpc.chain_getBlock(blockHash),
|
|
128
|
-
txExecutor.#getSystemEventsAt(blockHash),
|
|
129
|
-
]);
|
|
130
|
-
const txIndex = signedBlock.block.extrinsics.findIndex((tx) => this.registry.hashAsHex(tx) === txHash);
|
|
131
|
-
assert(txIndex >= 0, 'Extrinsic not found!');
|
|
132
|
-
const events = blockEvents.filter(({ phase }) => phase.tag === 'ApplyExtrinsic' && phase.value === txIndex);
|
|
133
|
-
return callback(new SubmittableResult({ status, txHash, events, txIndex }));
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
return callback(new SubmittableResult({ status, txHash }));
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
return api.rpc.author_submitExtrinsic(this.toHex());
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return new SubmittableExtrinsic(api.registry, call);
|
|
146
|
-
}
|
|
147
|
-
async #getSystemEventsAt(hash) {
|
|
148
|
-
const executor = new StorageQueryExecutor(this.api, hash);
|
|
149
|
-
const eventsQueryFn = executor.execute('system', 'events');
|
|
150
|
-
return eventsQueryFn();
|
|
151
|
-
}
|
|
152
|
-
}
|
package/executor/index.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export * from './Executor.js';
|
|
2
|
-
export * from './ConstantExecutor.js';
|
|
3
|
-
export * from './StorageQueryExecutor.js';
|
|
4
|
-
export * from './ErrorExecutor.js';
|
|
5
|
-
export * from './EventExecutor.js';
|
|
6
|
-
export * from './RuntimeApiExecutor.js';
|
|
7
|
-
export * from './TxExecutor.js';
|
package/executor/index.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export * from './Executor.js';
|
|
2
|
-
export * from './ConstantExecutor.js';
|
|
3
|
-
export * from './StorageQueryExecutor.js';
|
|
4
|
-
export * from './ErrorExecutor.js';
|
|
5
|
-
export * from './EventExecutor.js';
|
|
6
|
-
export * from './RuntimeApiExecutor.js';
|
|
7
|
-
export * from './TxExecutor.js';
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { DispatchError, DispatchInfo, Hash, TransactionStatus } from '@dedot/codecs';
|
|
2
|
-
import type { IEventRecord, ISubmittableResult } from '@dedot/types';
|
|
3
|
-
import type { FrameSystemEventRecord } from '../chaintypes/index.js';
|
|
4
|
-
export interface SubmittableResultInputs<E extends IEventRecord = FrameSystemEventRecord> {
|
|
5
|
-
events?: E[];
|
|
6
|
-
status: TransactionStatus;
|
|
7
|
-
txHash: Hash;
|
|
8
|
-
txIndex?: number;
|
|
9
|
-
}
|
|
10
|
-
export declare class SubmittableResult<E extends IEventRecord = FrameSystemEventRecord> implements ISubmittableResult<E> {
|
|
11
|
-
#private;
|
|
12
|
-
dispatchInfo?: DispatchInfo;
|
|
13
|
-
dispatchError?: DispatchError;
|
|
14
|
-
events: E[];
|
|
15
|
-
status: TransactionStatus;
|
|
16
|
-
txHash: Hash;
|
|
17
|
-
txIndex?: number;
|
|
18
|
-
constructor({ events, status, txHash, txIndex }: SubmittableResultInputs<E>);
|
|
19
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
export class SubmittableResult {
|
|
2
|
-
dispatchInfo;
|
|
3
|
-
dispatchError;
|
|
4
|
-
events;
|
|
5
|
-
status;
|
|
6
|
-
txHash;
|
|
7
|
-
txIndex;
|
|
8
|
-
constructor({ events, status, txHash, txIndex }) {
|
|
9
|
-
this.events = events || [];
|
|
10
|
-
this.status = status;
|
|
11
|
-
this.txHash = txHash;
|
|
12
|
-
this.txIndex = txIndex;
|
|
13
|
-
[this.dispatchInfo, this.dispatchError] = this.#extractInfo();
|
|
14
|
-
}
|
|
15
|
-
#extractInfo() {
|
|
16
|
-
for (const { event } of this.events) {
|
|
17
|
-
const { pallet, palletEvent } = event;
|
|
18
|
-
if (pallet === 'System' && palletEvent.name === 'ExtrinsicFailed') {
|
|
19
|
-
const { dispatchInfo, dispatchError } = palletEvent.data;
|
|
20
|
-
return [dispatchInfo, dispatchError];
|
|
21
|
-
}
|
|
22
|
-
else if (pallet === 'System' && palletEvent.name === 'ExtrinsicSuccess') {
|
|
23
|
-
const { dispatchInfo } = palletEvent.data;
|
|
24
|
-
return [dispatchInfo, undefined];
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return [undefined, undefined];
|
|
28
|
-
}
|
|
29
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { SignerPayloadJSON, SignerPayloadRaw } from '@polkadot/types/types';
|
|
2
|
-
import * as $ from '@dedot/shape';
|
|
3
|
-
import { HexString } from '@dedot/utils';
|
|
4
|
-
import { SignedExtension } from './SignedExtension.js';
|
|
5
|
-
export declare class ExtraSignedExtension extends SignedExtension<any[], any[]> {
|
|
6
|
-
#private;
|
|
7
|
-
init(): Promise<void>;
|
|
8
|
-
get identifier(): string;
|
|
9
|
-
get dataCodec(): $.AnyShape;
|
|
10
|
-
get additionalSignedCodec(): $.AnyShape;
|
|
11
|
-
get payloadCodec(): $.AnyShape;
|
|
12
|
-
toPayload(call?: HexString): SignerPayloadJSON;
|
|
13
|
-
toRawPayload(call?: HexString): SignerPayloadRaw;
|
|
14
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import * as $ from '@dedot/shape';
|
|
2
|
-
import { assert, ensurePresence, u8aToHex } from '@dedot/utils';
|
|
3
|
-
import { SignedExtension } from './SignedExtension.js';
|
|
4
|
-
import { knownSignedExtensions } from './known/index.js';
|
|
5
|
-
export class ExtraSignedExtension extends SignedExtension {
|
|
6
|
-
#signedExtensions;
|
|
7
|
-
async init() {
|
|
8
|
-
this.#signedExtensions = this.#getSignedExtensions();
|
|
9
|
-
await Promise.all(this.#signedExtensions.map((se) => se.init()));
|
|
10
|
-
this.data = this.#signedExtensions.map((se) => se.data);
|
|
11
|
-
this.additionalSigned = this.#signedExtensions.map((se) => se.additionalSigned);
|
|
12
|
-
}
|
|
13
|
-
get identifier() {
|
|
14
|
-
return 'ExtraSignedExtension';
|
|
15
|
-
}
|
|
16
|
-
get dataCodec() {
|
|
17
|
-
const { extraTypeId } = this.registry.metadata.extrinsic;
|
|
18
|
-
return ensurePresence(this.registry.findCodec(extraTypeId));
|
|
19
|
-
}
|
|
20
|
-
get additionalSignedCodec() {
|
|
21
|
-
const $AdditionalSignedCodecs = this.#signedExtensionDefs.map((se) => this.registry.findCodec(se.additionalSigned));
|
|
22
|
-
return $.Tuple(...$AdditionalSignedCodecs);
|
|
23
|
-
}
|
|
24
|
-
get payloadCodec() {
|
|
25
|
-
const { callTypeId } = this.registry.metadata.extrinsic;
|
|
26
|
-
const $Call = this.registry.findCodec(callTypeId);
|
|
27
|
-
return $.Tuple($Call, this.dataCodec, this.additionalSignedCodec);
|
|
28
|
-
}
|
|
29
|
-
get #signedExtensionDefs() {
|
|
30
|
-
return this.registry.metadata.extrinsic.signedExtensions;
|
|
31
|
-
}
|
|
32
|
-
#getSignedExtensions() {
|
|
33
|
-
return this.#signedExtensionDefs.map((extDef) => {
|
|
34
|
-
const { signedExtensions: userSignedExtensions = {} } = this.api.options;
|
|
35
|
-
const Extension = userSignedExtensions[extDef.ident] ||
|
|
36
|
-
knownSignedExtensions[extDef.ident];
|
|
37
|
-
assert(Extension, `SignedExtension for ${extDef.ident} not found`);
|
|
38
|
-
return new Extension(this.api, {
|
|
39
|
-
...ensurePresence(this.options),
|
|
40
|
-
def: extDef,
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
toPayload(call = '0x') {
|
|
45
|
-
const signedExtensions = this.#signedExtensions.map((se) => se.identifier);
|
|
46
|
-
const { version } = this.registry.metadata.extrinsic;
|
|
47
|
-
return Object.assign({ address: this.options.signerAddress, signedExtensions, version, method: call }, ...this.#signedExtensions.map((se) => se.toPayload()));
|
|
48
|
-
}
|
|
49
|
-
toRawPayload(call = '0x') {
|
|
50
|
-
const payload = this.toPayload(call);
|
|
51
|
-
const $ToSignPayload = $.Tuple($.RawHex, this.dataCodec, this.additionalSignedCodec);
|
|
52
|
-
const toSignPayload = [call, this.data, this.additionalSigned];
|
|
53
|
-
const rawPayload = $ToSignPayload.tryEncode(toSignPayload);
|
|
54
|
-
return {
|
|
55
|
-
address: payload.address,
|
|
56
|
-
data: u8aToHex(rawPayload),
|
|
57
|
-
type: 'payload',
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { SignerPayloadJSON } from '@polkadot/types/types';
|
|
2
|
-
import { PortableRegistry, SignedExtensionDefLatest } from '@dedot/codecs';
|
|
3
|
-
import * as $ from '@dedot/shape';
|
|
4
|
-
import { PayloadOptions } from '@dedot/types';
|
|
5
|
-
import { Dedot } from '../../client/index.js';
|
|
6
|
-
export interface ISignedExtension {
|
|
7
|
-
identifier: string;
|
|
8
|
-
dataCodec: $.AnyShape;
|
|
9
|
-
additionalSignedCodec: $.AnyShape;
|
|
10
|
-
data: any;
|
|
11
|
-
additionalSigned: any;
|
|
12
|
-
init(): Promise<void>;
|
|
13
|
-
registry: PortableRegistry;
|
|
14
|
-
toPayload(...additional: any[]): Partial<SignerPayloadJSON>;
|
|
15
|
-
}
|
|
16
|
-
interface SignedExtensionOptions {
|
|
17
|
-
def?: SignedExtensionDefLatest;
|
|
18
|
-
signerAddress?: string;
|
|
19
|
-
payloadOptions?: Partial<PayloadOptions>;
|
|
20
|
-
}
|
|
21
|
-
export declare abstract class SignedExtension<Data extends any = {}, AdditionalSigned extends any = []> implements ISignedExtension {
|
|
22
|
-
api: Dedot;
|
|
23
|
-
options?: SignedExtensionOptions | undefined;
|
|
24
|
-
data: Data;
|
|
25
|
-
additionalSigned: AdditionalSigned;
|
|
26
|
-
constructor(api: Dedot, options?: SignedExtensionOptions | undefined);
|
|
27
|
-
init(): Promise<void>;
|
|
28
|
-
get identifier(): string;
|
|
29
|
-
get dataCodec(): $.AnyShape;
|
|
30
|
-
get additionalSignedCodec(): $.AnyShape;
|
|
31
|
-
get registry(): PortableRegistry;
|
|
32
|
-
get signedExtensionDef(): {
|
|
33
|
-
ident: string;
|
|
34
|
-
typeId: number;
|
|
35
|
-
additionalSigned: number;
|
|
36
|
-
};
|
|
37
|
-
get payloadOptions(): Partial<PayloadOptions>;
|
|
38
|
-
toPayload(...args: any[]): Partial<SignerPayloadJSON>;
|
|
39
|
-
}
|
|
40
|
-
export {};
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { ensurePresence } from '@dedot/utils';
|
|
2
|
-
export class SignedExtension {
|
|
3
|
-
api;
|
|
4
|
-
options;
|
|
5
|
-
data;
|
|
6
|
-
additionalSigned;
|
|
7
|
-
constructor(api, options) {
|
|
8
|
-
this.api = api;
|
|
9
|
-
this.options = options;
|
|
10
|
-
this.data = {};
|
|
11
|
-
this.additionalSigned = [];
|
|
12
|
-
}
|
|
13
|
-
async init() {
|
|
14
|
-
// TODO implement this method
|
|
15
|
-
}
|
|
16
|
-
get identifier() {
|
|
17
|
-
return this.signedExtensionDef.ident;
|
|
18
|
-
}
|
|
19
|
-
get dataCodec() {
|
|
20
|
-
return ensurePresence(this.registry.findCodec(this.signedExtensionDef.typeId));
|
|
21
|
-
}
|
|
22
|
-
get additionalSignedCodec() {
|
|
23
|
-
return ensurePresence(this.registry.findCodec(this.signedExtensionDef.additionalSigned));
|
|
24
|
-
}
|
|
25
|
-
get registry() {
|
|
26
|
-
return this.api.registry;
|
|
27
|
-
}
|
|
28
|
-
get signedExtensionDef() {
|
|
29
|
-
return ensurePresence(this.options.def);
|
|
30
|
-
}
|
|
31
|
-
get payloadOptions() {
|
|
32
|
-
return this.options?.payloadOptions || {};
|
|
33
|
-
}
|
|
34
|
-
toPayload(...args) {
|
|
35
|
-
return {};
|
|
36
|
-
}
|
|
37
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { SignerPayloadJSON } from '@polkadot/types/types';
|
|
2
|
-
import { SignedExtension } from '../SignedExtension.js';
|
|
3
|
-
/**
|
|
4
|
-
* @name ChargeAssetTxPayment
|
|
5
|
-
*/
|
|
6
|
-
export declare class ChargeAssetTxPayment extends SignedExtension<{
|
|
7
|
-
tip: bigint;
|
|
8
|
-
assetId?: number | object;
|
|
9
|
-
}> {
|
|
10
|
-
#private;
|
|
11
|
-
init(): Promise<void>;
|
|
12
|
-
toPayload(): Partial<SignerPayloadJSON>;
|
|
13
|
-
$AssetId(): any;
|
|
14
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { assert, bnToHex, u8aToHex } from '@dedot/utils';
|
|
2
|
-
import { SignedExtension } from '../SignedExtension.js';
|
|
3
|
-
/**
|
|
4
|
-
* @name ChargeAssetTxPayment
|
|
5
|
-
*/
|
|
6
|
-
export class ChargeAssetTxPayment extends SignedExtension {
|
|
7
|
-
async init() {
|
|
8
|
-
const { tip, assetId } = this.payloadOptions;
|
|
9
|
-
this.data = {
|
|
10
|
-
tip: tip || 0n,
|
|
11
|
-
assetId,
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
toPayload() {
|
|
15
|
-
const { tip, assetId } = this.data;
|
|
16
|
-
return {
|
|
17
|
-
tip: bnToHex(tip),
|
|
18
|
-
// @ts-ignore
|
|
19
|
-
assetId: this.#encodeAssetId(assetId),
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
#encodeAssetId(assetId) {
|
|
23
|
-
if (assetId === null || assetId === undefined) {
|
|
24
|
-
return undefined;
|
|
25
|
-
}
|
|
26
|
-
return u8aToHex(this.$AssetId().tryEncode(assetId));
|
|
27
|
-
}
|
|
28
|
-
$AssetId() {
|
|
29
|
-
const extensionTypeDef = this.registry.findType(this.signedExtensionDef.typeId);
|
|
30
|
-
assert(extensionTypeDef.type.tag === 'Struct');
|
|
31
|
-
const assetIdTypeDef = extensionTypeDef.type.value.fields.find((f) => f.name === 'asset_id');
|
|
32
|
-
const $codec = this.registry.findCodec(assetIdTypeDef.typeId);
|
|
33
|
-
const codecMetadata = $codec.metadata[0];
|
|
34
|
-
if (codecMetadata.name === '$.option') {
|
|
35
|
-
return codecMetadata.args[0]; // inner shape
|
|
36
|
-
}
|
|
37
|
-
return $codec;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { SignerPayloadJSON } from '@polkadot/types/types';
|
|
2
|
-
import { SignedExtension } from '../SignedExtension.js';
|
|
3
|
-
export declare class ChargeTransactionPayment extends SignedExtension<bigint> {
|
|
4
|
-
init(): Promise<void>;
|
|
5
|
-
toPayload(): Partial<SignerPayloadJSON>;
|
|
6
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { bnToHex } from '@dedot/utils';
|
|
2
|
-
import { SignedExtension } from '../SignedExtension.js';
|
|
3
|
-
export class ChargeTransactionPayment extends SignedExtension {
|
|
4
|
-
async init() {
|
|
5
|
-
this.data = this.payloadOptions.tip || 0n;
|
|
6
|
-
}
|
|
7
|
-
toPayload() {
|
|
8
|
-
return {
|
|
9
|
-
tip: bnToHex(this.data),
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { SignerPayloadJSON } from '@polkadot/types/types';
|
|
2
|
-
import { Hash } from '@dedot/codecs';
|
|
3
|
-
import { SignedExtension } from '../SignedExtension.js';
|
|
4
|
-
/**
|
|
5
|
-
* @description Genesis hash check to provide replay protection between different networks.
|
|
6
|
-
*/
|
|
7
|
-
export declare class CheckGenesis extends SignedExtension<{}, Hash> {
|
|
8
|
-
init(): Promise<void>;
|
|
9
|
-
toPayload(): Partial<SignerPayloadJSON>;
|
|
10
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { ensurePresence } from '@dedot/utils';
|
|
2
|
-
import { SignedExtension } from '../SignedExtension.js';
|
|
3
|
-
/**
|
|
4
|
-
* @description Genesis hash check to provide replay protection between different networks.
|
|
5
|
-
*/
|
|
6
|
-
export class CheckGenesis extends SignedExtension {
|
|
7
|
-
async init() {
|
|
8
|
-
this.additionalSigned = ensurePresence(this.api.genesisHash);
|
|
9
|
-
}
|
|
10
|
-
toPayload() {
|
|
11
|
-
return {
|
|
12
|
-
genesisHash: this.additionalSigned,
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { SignerPayloadJSON } from '@polkadot/types/types';
|
|
2
|
-
import { EraLike, Hash } from '@dedot/codecs';
|
|
3
|
-
import { SignedExtension } from '../SignedExtension.js';
|
|
4
|
-
export declare const MAX_FINALITY_LAG: number;
|
|
5
|
-
export declare const FALLBACK_MAX_HASH_COUNT: number;
|
|
6
|
-
export declare const FALLBACK_PERIOD: number;
|
|
7
|
-
export declare const MORTAL_PERIOD: number;
|
|
8
|
-
/**
|
|
9
|
-
* @description Check for transaction mortality.
|
|
10
|
-
*/
|
|
11
|
-
export declare class CheckMortality extends SignedExtension<EraLike, Hash> {
|
|
12
|
-
#private;
|
|
13
|
-
init(): Promise<void>;
|
|
14
|
-
toPayload(): Partial<SignerPayloadJSON>;
|
|
15
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { assert, bnMin, isZeroHex, numberToHex, u8aToHex } from '@dedot/utils';
|
|
2
|
-
import { SignedExtension } from '../SignedExtension.js';
|
|
3
|
-
export const MAX_FINALITY_LAG = 5;
|
|
4
|
-
export const FALLBACK_MAX_HASH_COUNT = 250;
|
|
5
|
-
export const FALLBACK_PERIOD = 6 * 1000;
|
|
6
|
-
export const MORTAL_PERIOD = 5 * 60 * 1000;
|
|
7
|
-
/**
|
|
8
|
-
* @description Check for transaction mortality.
|
|
9
|
-
*/
|
|
10
|
-
export class CheckMortality extends SignedExtension {
|
|
11
|
-
#signingHeader;
|
|
12
|
-
async init() {
|
|
13
|
-
this.#signingHeader = await this.#determineSigningHeader();
|
|
14
|
-
this.data = { period: this.#calculateMortalLength(), current: BigInt(this.#signingHeader.number) };
|
|
15
|
-
this.additionalSigned = (await this.api.rpc.chain_getBlockHash(this.#signingHeader.number));
|
|
16
|
-
}
|
|
17
|
-
// Ref: https://github.com/polkadot-js/api/blob/3bdf49b0428a62f16b3222b9a31bfefa43c1ca55/packages/api-derive/src/tx/signingInfo.ts#L34-L64
|
|
18
|
-
async #determineSigningHeader() {
|
|
19
|
-
const [header, finalizedHash] = await Promise.all([
|
|
20
|
-
this.api.rpc.chain_getHeader(),
|
|
21
|
-
this.api.rpc.chain_getFinalizedHead(),
|
|
22
|
-
]);
|
|
23
|
-
assert(header, 'Current header not found');
|
|
24
|
-
const [currentHeader, finalizedHeader] = await Promise.all([
|
|
25
|
-
Promise.resolve(header).then((header) => {
|
|
26
|
-
const { parentHash } = header;
|
|
27
|
-
if (parentHash.length === 0 || isZeroHex(parentHash)) {
|
|
28
|
-
return header;
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
return this.api.rpc.chain_getHeader(parentHash);
|
|
32
|
-
}
|
|
33
|
-
}),
|
|
34
|
-
this.api.rpc.chain_getHeader(finalizedHash),
|
|
35
|
-
]);
|
|
36
|
-
assert(currentHeader, 'Cannot determine current header');
|
|
37
|
-
if (!finalizedHeader || currentHeader.number - finalizedHeader.number > MAX_FINALITY_LAG) {
|
|
38
|
-
return currentHeader;
|
|
39
|
-
}
|
|
40
|
-
return finalizedHeader;
|
|
41
|
-
}
|
|
42
|
-
// Ref: https://github.com/polkadot-js/api/blob/3bdf49b0428a62f16b3222b9a31bfefa43c1ca55/packages/api-derive/src/tx/signingInfo.ts#L83-L93
|
|
43
|
-
#calculateMortalLength() {
|
|
44
|
-
return bnMin(BigInt(this.#getConst('system', 'blockHashCount') || FALLBACK_MAX_HASH_COUNT), BigInt(MORTAL_PERIOD) /
|
|
45
|
-
BigInt(this.#getConst('babe', 'expectedBlockTime') ||
|
|
46
|
-
BigInt(this.#getConst('timestamp', 'minimumPeriod') || 0) * 2n ||
|
|
47
|
-
FALLBACK_PERIOD) +
|
|
48
|
-
BigInt(MAX_FINALITY_LAG));
|
|
49
|
-
}
|
|
50
|
-
#getConst(pallet, name) {
|
|
51
|
-
try {
|
|
52
|
-
return this.api.consts[pallet][name];
|
|
53
|
-
}
|
|
54
|
-
catch {
|
|
55
|
-
// ignore this on purpose
|
|
56
|
-
}
|
|
57
|
-
return undefined;
|
|
58
|
-
}
|
|
59
|
-
toPayload() {
|
|
60
|
-
return {
|
|
61
|
-
era: u8aToHex(this.dataCodec.tryEncode(this.data)),
|
|
62
|
-
blockHash: this.additionalSigned,
|
|
63
|
-
blockNumber: numberToHex(this.#signingHeader.number),
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { SignerPayloadJSON } from '@polkadot/types/types';
|
|
2
|
-
import { SignedExtension } from '../SignedExtension.js';
|
|
3
|
-
/**
|
|
4
|
-
* @description Nonce check and increment to give replay protection for transactions.
|
|
5
|
-
*/
|
|
6
|
-
export declare class CheckNonce extends SignedExtension<number> {
|
|
7
|
-
#private;
|
|
8
|
-
init(): Promise<void>;
|
|
9
|
-
toPayload(): Partial<SignerPayloadJSON>;
|
|
10
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { assert, numberToHex } from '@dedot/utils';
|
|
2
|
-
import { SignedExtension } from '../SignedExtension.js';
|
|
3
|
-
/**
|
|
4
|
-
* @description Nonce check and increment to give replay protection for transactions.
|
|
5
|
-
*/
|
|
6
|
-
export class CheckNonce extends SignedExtension {
|
|
7
|
-
async init() {
|
|
8
|
-
this.data = this.payloadOptions.nonce || (await this.#getNonce());
|
|
9
|
-
}
|
|
10
|
-
async #getNonce() {
|
|
11
|
-
const { signerAddress } = this.options || {};
|
|
12
|
-
assert(signerAddress, 'Signer address not found');
|
|
13
|
-
try {
|
|
14
|
-
return (await this.api.query.system.account(signerAddress)).nonce;
|
|
15
|
-
}
|
|
16
|
-
catch { }
|
|
17
|
-
try {
|
|
18
|
-
return await this.api.call.accountNonceApi.accountNonce(signerAddress);
|
|
19
|
-
}
|
|
20
|
-
catch { }
|
|
21
|
-
// TODO fallback to api.rpc.system_accountNextIndex if needed
|
|
22
|
-
return 0;
|
|
23
|
-
}
|
|
24
|
-
toPayload() {
|
|
25
|
-
return {
|
|
26
|
-
nonce: numberToHex(this.data),
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { SignerPayloadJSON } from '@polkadot/types/types';
|
|
2
|
-
import { SignedExtension } from '../SignedExtension.js';
|
|
3
|
-
/**
|
|
4
|
-
* @description Ensure the runtime version registered in the transaction is the same as at present.
|
|
5
|
-
*/
|
|
6
|
-
export declare class CheckSpecVersion extends SignedExtension<{}, number> {
|
|
7
|
-
init(): Promise<void>;
|
|
8
|
-
toPayload(): Partial<SignerPayloadJSON>;
|
|
9
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { numberToHex } from '@dedot/utils';
|
|
2
|
-
import { SignedExtension } from '../SignedExtension.js';
|
|
3
|
-
/**
|
|
4
|
-
* @description Ensure the runtime version registered in the transaction is the same as at present.
|
|
5
|
-
*/
|
|
6
|
-
export class CheckSpecVersion extends SignedExtension {
|
|
7
|
-
async init() {
|
|
8
|
-
this.additionalSigned = this.api.runtimeVersion.specVersion;
|
|
9
|
-
}
|
|
10
|
-
toPayload() {
|
|
11
|
-
return {
|
|
12
|
-
specVersion: numberToHex(this.additionalSigned),
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
}
|