-
Notifications
You must be signed in to change notification settings - Fork 5
/
calculateDerivedAddress.ts
56 lines (48 loc) · 1.6 KB
/
calculateDerivedAddress.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
import { blake2AsU8a, encodeAddress, decodeAddress } from '@polkadot/util-crypto';
import { u8aToHex, hexToU8a, bnToU8a } from '@polkadot/util';
import yargs from 'yargs';
const args = yargs
.options({
address: { type: 'string', demandOption: true, alias: 'a' },
index: { type: 'array', demandOption: true, alias: 'i' },
})
.check((argv) => {
// Ensure that index array is parsed correctly
for (const i of argv['index'])
if (typeof i !== 'number' || !Number.isInteger(i))
throw new Error(`${i} is not an integer. Please try the command again with integers.`);
return true;
}).argv;
// If address does not start with 0x - decode it
let address = args['address'];
const ethAddress = address.length === 42;
const derivativeToEthAddress = (d: string) => d.slice(0, 42);
let derivative;
for (let l = 0; l < args['index'].length; l++) {
if (!ethAddress) {
address = decodeAddress(address);
} else {
address = hexToU8a(address);
}
// Calculate Derivative Address of Given Index
derivative = u8aToHex(
blake2AsU8a(
new Uint8Array([
...new TextEncoder().encode('modlpy/utilisuba'),
...address,
...bnToU8a(args['index'][l], { bitLength: 16 }),
]),
256
)
);
if (ethAddress) address = derivativeToEthAddress(derivative);
else address = derivative;
}
if (ethAddress) {
console.log(
`Moonbeam derivative at index ${args['index']}: ${derivativeToEthAddress(derivative)}`
);
} else {
console.log(`32 Bytes derivative at index ${args['index']}: ${derivative}`);
console.log(`Encoded: ${encodeAddress(derivative)}`);
}