-
Notifications
You must be signed in to change notification settings - Fork 378
Deprecate signPayload in favour of new createTransaction method from Signers
#6225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ap211unitech
wants to merge
5
commits into
master
Choose a base branch
from
feat/createTransaction-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+375
−1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
signPayload in favour of new createTransaction method for SignerssignPayload in favour of new createTransaction method from Signers
Member
Author
|
PoC code which I used for testing it: import fs from "fs";
import { ApiPromise, Keyring, WsProvider } from "@polkadot/api";
import { AddressOrPair, Signer, SignerOptions } from "@polkadot/api/types";
import { TxPayloadV1 } from "@polkadot/types/types";
import { KeyringPair, KeyringPair$Json } from "@polkadot/keyring/types";
import { cryptoWaitReady } from "@polkadot/util-crypto";
import { HexString } from "@polkadot/util/types";
import { SignerPayload } from "@polkadot/types/interfaces";
import { GenericExtrinsic } from "@polkadot/types";
import { SubmittableExtrinsic } from "@polkadot/api/promise/types";
const MAIN_ACCOUNT = "5GLFKUxSXTf8MDDKM1vqEFb5TuV1q642qpQT964mrmjeKz4w";
export const signAndSubmit = async (
extrinsic: SubmittableExtrinsic,
address: AddressOrPair,
options: Partial<SignerOptions>
) => {
return await new Promise((resolve, reject) => {
extrinsic
.signAndSend(address, options, async (r) => {
if (r.isError || r.internalError || r.dispatchError)
reject(r.dispatchError?.toString() ?? r.internalError?.message);
if (r.isInBlock) {
const inBlockHash = r.status.asInBlock.toHex();
console.log(`🎉 Tx finalizing with blockhash: ${inBlockHash}`);
resolve("done");
}
})
.catch((e) => {
reject(e);
});
});
};
// Helper to extract 'extra' from extensions
const findExtra = (payloadV1: TxPayloadV1, id: string) => {
const ext = payloadV1.extensions.find((e) => e.id === id);
return ext && ext.extra !== "0x" ? ext.extra : null;
};
// Helper to extract 'additionalSigned' from extensions
const findAdditional = (payloadV1: TxPayloadV1, id: string) => {
const ext = payloadV1.extensions.find((e) => e.id === id);
return ext && ext.additionalSigned !== "0x" ? ext.additionalSigned : null;
};
async function setup() {
await cryptoWaitReady();
const api = await ApiPromise.create({
provider: new WsProvider("wss://asset-hub-paseo.dotters.network"),
noInitWarn: true,
});
await api.isReadyOrError;
const keyring = new Keyring({ type: "sr25519" });
const senderKeyring = keyring.addFromJson(
JSON.parse(
fs.readFileSync(`${MAIN_ACCOUNT}.json`, "utf-8")
) as KeyringPair$Json
);
senderKeyring.unlock("<your account password>");
return { api, senderKeyring };
}
class MockModernSigner implements Signer {
private readonly keypair: KeyringPair;
private readonly api: ApiPromise;
constructor(keypair: KeyringPair, api: ApiPromise) {
this.keypair = keypair;
this.api = api;
}
// 🎯 NEW METHOD: This function is the target of the new implementation
public async createTransaction(payloadV1: TxPayloadV1): Promise<HexString> {
const bestBlockHeight = payloadV1.context.bestBlockHeight;
const blockHash = (
await this.api.rpc.chain.getBlockHash(bestBlockHeight)
).toHex();
const payloadToSign = this.api.registry.createType<SignerPayload>(
"SignerPayload",
{
address: payloadV1.signer,
blockHash,
blockNumber: bestBlockHeight,
era: findExtra(payloadV1, "CheckMortality"),
genesisHash: findAdditional(payloadV1, "CheckGenesis"),
method: payloadV1.callData,
mode: null,
nonce: findExtra(payloadV1, "CheckNonce"),
runtimeVersion: this.api.runtimeVersion,
signedExtensions: payloadV1.extensions.map((e) => e.id),
tip: findExtra(payloadV1, "ChargeTransactionPayment"),
assetId: findExtra(payloadV1, "ChargeAssetTxPayment"),
version: findAdditional(payloadV1, "CheckTxVersion"),
}
);
const { signature: signatureHex } = this.api.registry
.createType("ExtrinsicPayload", payloadToSign.toPayload(), {
version: payloadToSign.version,
})
.sign(this.keypair);
const extrinsic = new GenericExtrinsic(
this.api.registry,
payloadToSign.method
);
extrinsic.addSignature(
payloadV1.signer,
signatureHex,
payloadToSign.toPayload()
);
console.log("Signing successful...Submitting now..✅");
return extrinsic.toHex();
}
}
const main = async () => {
const { api, senderKeyring } = await setup();
console.log("Connected to API...🚀");
const modernSigner = new MockModernSigner(senderKeyring, api);
const ext = api.tx.balances.transferKeepAlive(
"16kVKdv56dtV13tPttUZonKgj7qqkJget5Xkf3yMqEgdwhZK",
1 * 10 ** 10 // 1 PAS
);
await signAndSubmit(ext, senderKeyring.address, { signer: modernSigner });
await api.disconnect();
};
main()
.catch((e) => console.log("Something went wrong!", e))
.finally(() => process.exit(0));Note: You must have to export your account as JSON file, and enter the password to unlock it in the script. |
Member
Author
|
Hi @josepot, could you please review this as well? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
More details here: https://hackmd.io/@arjun-porwal/SJwOGWWpex