-
Notifications
You must be signed in to change notification settings - Fork 4
/
calculateLocalAssetInfo.ts
81 lines (68 loc) · 2.39 KB
/
calculateLocalAssetInfo.ts
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { ApiPromise, WsProvider } from '@polkadot/api';
import { xxhashAsU8a, blake2AsU8a } from '@polkadot/util-crypto';
import { u8aToHex, hexToU8a, numberToU8a } from '@polkadot/util';
import yargs from 'yargs';
const args = yargs.options({
asset: { type: 'string', demandOption: true, alias: 'a' },
network: { type: 'string', demandOption: false, alias: 'n' },
}).argv;
// Create Provider
let wsProvider;
if (args['network'] === 'moonbeam') {
wsProvider = new WsProvider('wss://wss.api.moonbeam.network');
} else if (args['network'] === 'moonriver') {
wsProvider = new WsProvider('wss://wss.api.moonriver.moonbeam.network');
} else if (args['network'] === 'moonbase') {
wsProvider = new WsProvider('wss://wss.api.moonbase.moonbeam.network');
} else {
console.error('Network not supported');
process.exit();
}
const calculateStuff = (assetIdHex) => {
let palletEncoder = new TextEncoder().encode('EVM');
let palletHash = xxhashAsU8a(palletEncoder, 128);
let storageEncoder = new TextEncoder().encode('AccountCodes');
let storageHash = xxhashAsU8a(storageEncoder, 128);
let assetAddress = new Uint8Array([
...hexToU8a('0xFFFFFFFE'),
...hexToU8a(assetIdHex),
]);
let addressHash = blake2AsU8a(assetAddress, 128);
let concatKey = new Uint8Array([
...palletHash,
...storageHash,
...addressHash,
...assetAddress,
]);
console.log(`Storage Key ${u8aToHex(concatKey)}`);
console.log(`Asset Address Precompile: ${u8aToHex(assetAddress)}`);
console.log(`Asset ID is ${BigInt(assetIdHex).toString(10)}\n\n`);
};
const main = async () => {
// Wait for Provider
const api = await ApiPromise.create({
provider: wsProvider,
noInitWarn: true,
});
await api.isReady;
const asset: any = api.createType('u128', JSON.parse(args['asset']));
// For Legacy Assed ID we need to add 1 byte that is 4 x length of the original uint8
const legacyAsset = new Uint8Array([
...numberToU8a(4 * asset.toU8a().length),
...asset.toU8a(),
]);
const assetIdHex = u8aToHex(
api.registry.hash(asset.toU8a()).slice(0, 16).reverse()
);
console.log(assetIdHex);
const assetIdLegacyHex = u8aToHex(
api.registry.hash(legacyAsset).slice(0, 16).reverse()
);
// Calculate Good Asset ID Stuff
calculateStuff(assetIdHex);
// Calculate Legacy Asset ID
console.log(`Legacy Asset ID`);
calculateStuff(assetIdLegacyHex);
await api.disconnect();
};
main();