-
Notifications
You must be signed in to change notification settings - Fork 382
/
claim.mjs
64 lines (53 loc) · 2.35 KB
/
claim.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/// TODO: make this into a rpc test when integrated in Shiden & Astar
import { JsonRpcProvider, Wallet } from "ethers"
import { WsProvider, ApiPromise, Keyring } from '@polkadot/api';
async function waitForTx(tx, signer, api) {
return new Promise((resolve, reject) => {
tx.signAndSend(signer, (result) => {
if (result.status.isInBlock) {
console.log(`\t Transaction included at blockHash ${result.status.asInBlock}`);
} else if (result.status.isFinalized) {
console.log(`\t Transaction finalized at blockHash ${result.status.asFinalized}`);
result.events.forEach(({ phase, event: { data, method, section } }) => {
console.log(`\t' ${phase}: ${section}.${method}:: ${data}`);
});
resolve(result.txHash)
} else if (result.isError) {
reject();
}
})
})
}
async function buildSignature(signer, substrateAddress, api, chainId) {
return await signer.signTypedData({
chainId,
name: "Astar EVM Claim",
version: "1",
salt: await api.query.system.blockHash(0) // genisis hash
}, {
Claim: [
{ name: 'substrateAddress', type: 'bytes' }
],
}, {
substrateAddress
})
}
async function claimEvmAccount(account, evmAddress, signature, api) {
return await waitForTx(api.tx.accounts.claimEvmAccount(evmAddress, signature), account)
}
async function main() {
const api = await ApiPromise.create({ provider: new WsProvider('ws://127.0.0.1:9945') });
await api.isReady;
const keyring = new Keyring({ type: 'sr25519' });
const alice = keyring.addFromUri('//Alice', { name: 'Alice default' })
const provider = new JsonRpcProvider("http://127.0.0.1:9945");
const { chainId } = await provider.getNetwork();
const ethSigner = new Wallet("0x01ab6e801c06e59ca97a14fc0a1978b27fa366fc87450e0b65459dd3515b7391", provider);
const sig = await buildSignature(ethSigner, alice.publicKey, api, chainId);
console.log(`Signature - ${sig}`)
const hash = await claimEvmAccount(alice, ethSigner.address, sig, api);
console.log(`Claim Extrisic - ${hash}`);
console.log(`Claimed Account ${await api.query.accounts.EvmToNative(alice.address)}, EVM Account: ${ethSigner.address}`);
api.disconnect();
}
await main()