Ensure signAsync and dryRun is fully compatible with signedTransaction, and add withSignedTransaction option (#5920)

* Ensure signAsync is compatible with signedTransaction

* Fix payload construction

* Update sig

* add withSignedTransaction option

* remove unused type

* Add withSignedTransaction to other type

* Throw error when signTransaction is there but not enabled

* inline docs

* Update inline docs

* lint
This commit is contained in:
Tarik Gul
2024-06-27 15:09:48 -04:00
committed by GitHub
parent 6f199db86a
commit ab3f0253bd
3 changed files with 44 additions and 4 deletions
@@ -19,6 +19,7 @@ export interface SignerOptions {
assetId?: AnyNumber | object;
mode?: AnyNumber;
metadataHash?: AnyU8a;
withSignedTransaction?: boolean;
}
export type SubmittableDryRunResult<ApiType extends ApiTypes> =
@@ -66,6 +67,15 @@ export interface SubmittableExtrinsic<ApiType extends ApiTypes, R extends ISubmi
send (statusCb: Callback<R>): SubmittableResultSubscription<ApiType, R>;
/**
* @description Sign and broadcast the constructued transaction.
*
* Note for injected signers:
* As of v12.0.2 and up the `SignerResult` return type for `signPayload` allows for the `signedTransaction` field.
* This allows the signer to input a signed transaction that will modify the payload. This
* The api will ensure that the Call Data is not changed. This allows for the signer to modify the payload to add
* things like `mode`, and `metadataHash` for signedExtensions such as `CheckMetadataHash`.
*/
signAsync (account: AddressOrPair, _options?: Partial<SignerOptions>): PromiseOrObs<ApiType, this>;
/**
+24 -3
View File
@@ -340,16 +340,37 @@ export function createClass <ApiType extends ApiTypes> ({ api, apiType, blockHas
if (isFunction(signer.signPayload)) {
result = await signer.signPayload(payload.toPayload());
// When the signedTransaction is included by the signer, we no longer add
// the signature to the parent class, but instead broadcast the signed transaction directly.
if (result.signedTransaction) {
if (result.signedTransaction && !options.withSignedTransaction) {
throw new Error('The `signedTransaction` field may not be submitted when `withSignedTransaction` is disabled');
}
if (result.signedTransaction && options.withSignedTransaction) {
const ext = this.registry.createTypeUnsafe<Extrinsic>('Extrinsic', [result.signedTransaction]);
const newSignerPayload = this.registry.createTypeUnsafe<SignerPayload>('SignerPayload', [objectSpread({}, {
address,
assetId: ext.assetId ? ext.assetId.toHex() : null,
blockHash: payload.blockHash,
blockNumber: header ? header.number : 0,
era: ext.era.toHex(),
genesisHash: payload.genesisHash,
metadataHash: ext.metadataHash ? ext.metadataHash.toHex() : null,
method: ext.method.toHex(),
mode: ext.mode ? ext.mode.toHex() : null,
nonce: ext.nonce.toHex(),
runtimeVersion: payload.runtimeVersion,
signedExtensions: payload.signedExtensions,
tip: ext.tip.toHex(),
version: payload.version
})]);
if (!ext.isSigned) {
throw new Error(`When using the signedTransaction field, the transaction must be signed. Recieved isSigned: ${ext.isSigned}`);
}
this.#validateSignedTransaction(payload, ext);
// This is only used for signAsync - signAndSend does not need to adjust the super payload or
// add the signature.
super.addSignature(address, result.signature, newSignerPayload.toPayload());
return { id: result.id, signedTransaction: result.signedTransaction };
}
+10 -1
View File
@@ -104,6 +104,12 @@ export interface SignerPayloadJSON {
* @description The version of the extrinsic we are dealing with
*/
version: number;
/**
* @description Optional flag that enables the use of the `signedTransaction` field in
* `singAndSend`, `signAsync`, and `dryRun`.
*/
withSignedTransaction?: boolean;
}
export interface SignerPayloadRawBase {
@@ -152,7 +158,9 @@ export interface SignerResult {
* and instead broadcasting the transaction directly. There is a small validation layer. Please refer
* to the implementation for more information. If the inputted signed transaction is not actually signed, it will fail with an error.
*
* NOTE: This is only implemented for `signPayload`.
* This will also work for `signAsync`. The new payload will be added to the Extrinsic, and will be sent once the consumer calls `.send()`.
*
* NOTE: This is only implemented for `signPayload`, and will only work when the `withSignedTransaction` option is enabled as an option.
*/
signedTransaction?: HexString | Uint8Array;
}
@@ -191,6 +199,7 @@ export interface SignatureOptions {
assetId?: AnyNumber | object;
mode?: AnyNumber;
metadataHash?: Uint8Array | string;
withSignedTransaction?: boolean;
}
interface ExtrinsicSignatureBase {