-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddress.ts
More file actions
48 lines (44 loc) · 1.27 KB
/
address.ts
File metadata and controls
48 lines (44 loc) · 1.27 KB
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
import { cryptoWaitReady, decodeAddress } from "@polkadot/util-crypto";
import { u8aToHex } from "@polkadot/util";
import { encodeAddress } from "@polkadot/util-crypto";
// Humanode mainnet SS58 format (produces `hm...`-prefixed addresses).
export const HUMANODE_SS58_FORMAT = 5234;
export async function addressToPublicKeyHex(
address: string,
): Promise<string | null> {
const trimmed = address.trim();
if (!trimmed) return null;
await cryptoWaitReady();
try {
return u8aToHex(decodeAddress(trimmed));
} catch {
return null;
}
}
export async function canonicalizeHmndAddress(
address: string,
): Promise<string | null> {
const trimmed = address.trim();
if (!trimmed) return null;
await cryptoWaitReady();
try {
// decodeAddress accepts any SS58 format; re-encode into the Humanode prefix.
return encodeAddress(trimmed, HUMANODE_SS58_FORMAT);
} catch {
return null;
}
}
export async function addressesReferToSameKey(
a: string,
b: string,
): Promise<boolean> {
const left = a.trim();
const right = b.trim();
if (!left || !right) return false;
if (left === right) return true;
const [pkA, pkB] = await Promise.all([
addressToPublicKeyHex(left),
addressToPublicKeyHex(right),
]);
return Boolean(pkA && pkB && pkA === pkB);
}