[Question]
- How can I make raw-transaction(unsigned-transaction) with essential parameter(fromAddress, toAddress, value, nonce, fee etc)? I want to know low-level algorithm.
- What kind of Cryptography is used on Polkadot? I think it use sr25519, isn't it?
- In below code log, which data on trsfRawTx is used for sign? And Which Cryptography-function(js) can I use?
- I'm using send() function for broadcasting signedTx. Is there other function to broadcast signTx by parameter?
- For signing, I'm using mnemonic & keyring, and I can get address & public-key from keyring. How can I get private-key? If it can, How Can I sign by private-key?
[Environment] I'm developing a nodejs program on shibuya testnet.
[Goal]
- I want to make raw-transaction(unsigned-transaction) for tranferring & staking token.
- I want to sign raw-transaction and get signed-transaction on offchain-environment.
- I want to broadcast signed-transaction to node(public-endpoint).
[Attempt]
- I tried to make sampleTransfer SampleCode using provided sample-example.
- Just using provided api, have no problem, but I have to migrate on my system.
const sampleTransfer = async () => {
// Create a new instance of the api
const provider = new WsProvider('wss://rpc.shibuya.astar.network');
const api = new ApiPromise(options({ provider }));
const testAcnt1 = 'VxJhKGJ7wsVwZvaikWQzhexb9VawgFUtBuXNAmDAhBBVi4U';
const testAcnt2 = '5HBSNV13JH7LuEtJUBNuKgcaPgNaXGBZhCknUWrKmD9p8vhA';
await api.isReady;
console.log('===== STEP1-RawTx-Start =====');
const trsfRawTx = api.tx.balances.transfer(testAcnt1, BigInt(200000000000000000));
console.log(trsfRawTx);
// Submittable { initialU8aLength: undefined, registry: TypeRegistry {} }
console.log('===== ===== =====');
console.log(`${trsfRawTx}`);
// {"signature":{"signer":{"id":"VwJHxi1vTNdnJ2KXfQ7tWL8ERiUGv9bwhmf5RDWKGySQmsy"},"signature":{"ed25519":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},"era":{"immortalEra":"0x00"},"nonce":0,"tip":0},"method":{"callIndex":"0x1f00","args":{"dest":{"id":"VxJhKGJ7wsVwZvaikWQzhexb9VawgFUtBuXNAmDAhBBVi4U"},"value":"0x000000000000000002c68af0bb140000"}}}
console.log('===== STEP1-RawTx-End =====');
console.log('===== STEP2-SignRawTx-Start =====');
const keyring = new Keyring({ type: 'sr25519' });
const testAcnt2Mnemonic = '...MNEMONIC...';
const testAcnt2Key = keyring.addFromUri(testAcnt2Mnemonic);
console.log(`testAcnt2Key : ${testAcnt2Key.address}`);
// testAcnt2Key : 5HBSNV13JH7LuEtJUBNuKgcaPgNaXGBZhCknUWrKmD9p8vhA
console.log('===== ===== =====');
const signedTx = await trsfRawTx.signAsync(testAcnt2Key);
console.log(signedTx);
// Submittable { initialU8aLength: undefined, registry: TypeRegistry {} }
console.log('===== ===== =====');
console.log(`signedTx : ${signedTx}`);
// signedTx : {"signature":{"signer":{"id":"b42omy85WkT84v8xUq2MLmrUj5hVUtJiQFviE4Bda9mjH2V"},"signature":{"sr25519":"0x424a05493a61a47b6952b3357bcc08e2d210b566be82ec7fe506840483a9831800c3d355eb4302f676cbeb7d4252602c1d99ddda58e8c292171b72094ed2ac86"},"era":{"mortalEra":"0xe400"},"nonce":6,"tip":0},"method":{"callIndex":"0x1f00","args":{"dest":{"id":"VxJhKGJ7wsVwZvaikWQzhexb9VawgFUtBuXNAmDAhBBVi4U"},"value":"0x000000000000000002c68af0bb140000"}}}
console.log('===== STEP2-SignRawTx-End =====');
console.log('===== STEP3-SendSignedTx-Start =====');
const txhash = await signedTx.send();
console.log(txhash);
/*Type(32) [Uint8Array] [
200,
2,
33,
72,
119,
185,
239,
160,
204,
131,
232,
7,
240,
121,
215,
203,
71,
157,
105,
98,
100,
46,
130,
158,
88,
129,
19,
225,
222,
218,
10,
203,
registry: TypeRegistry {},
initialU8aLength: 32
]*/
console.log('===== ===== =====');
console.log(`txhash : ${txhash}`);
// txhash : 0xc802214877b9efa0cc83e807f079d7cb479d6962642e829e588113e1deda0acb
console.log('===== STEP3-SendSignedTx-End =====');
}
sampleTransfer().catch(console.error).finally(() => process.exit());