-
Notifications
You must be signed in to change notification settings - Fork 5
/
deriveAddress.ts
43 lines (35 loc) · 1.52 KB
/
deriveAddress.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
import { encodeAddress, decodeAddress } from "@polkadot/keyring";
import { u8aSorted } from "@polkadot/util";
import { blake2AsU8a } from "@polkadot/util-crypto";
type Options = {
addresses: string; // CSV of the addresses.
ss58Prefix: string; // Prefix for the network encoding to use.
threshold: string; // Number of addresses that are needed to approve an action.
}
const derivePubkey = (addresses: string[], threshold = 1): Uint8Array => {
const prefix = "modlpy/utilisuba";
const payload = new Uint8Array(prefix.length + 1 + 32 * addresses.length + 2);
payload.set(
Array.from(prefix).map((c) => c.charCodeAt(0)),
0
);
payload[prefix.length] = addresses.length << 2;
const pubkeys = addresses.map((addr) => decodeAddress(addr));
u8aSorted(pubkeys).forEach((pubkey, idx) => {
payload.set(pubkey, prefix.length + 1 + idx * 32);
});
payload[prefix.length + 1 + 32 * addresses.length] = threshold;
return blake2AsU8a(payload);
}
export const deriveAddress = (opts: Options): void => {
const { addresses, ss58Prefix, threshold } = opts;
if (!addresses) throw new Error("Please provide the addresses option.");
const addrs = addresses.split(",").filter((x) => !!x);
const pubkey = derivePubkey(addrs, Number(threshold));
const msig = encodeAddress(pubkey, Number(ss58Prefix));
console.log("-".repeat(32));
console.log(`Addresses: ${addrs.join(" ")}`);
console.log(`Threshold: ${threshold}`);
console.log(`Multisig Address (SS58: ${ss58Prefix}): ${msig}`);
console.log("-".repeat(32));
}