1 |
|
2 |
|
3 |
|
4 | import { u8aConcat } from '@polkadot/util';
|
5 | import { base58Encode } from "../base58/index.js";
|
6 | import { decodeAddress } from "./decode.js";
|
7 | import { defaults } from "./defaults.js";
|
8 | import { sshash } from "./sshash.js";
|
9 | export function encodeAddress(key, ss58Format = defaults.prefix) {
|
10 |
|
11 | const u8a = decodeAddress(key);
|
12 |
|
13 | if (ss58Format < 0 || ss58Format > 16383 || [46, 47].includes(ss58Format)) {
|
14 | throw new Error('Out of range ss58Format specified');
|
15 | } else if (!defaults.allowedDecodedLengths.includes(u8a.length)) {
|
16 | throw new Error(`Expected a valid key to convert, with length ${defaults.allowedDecodedLengths.join(', ')}`);
|
17 | }
|
18 |
|
19 | const input = u8aConcat(ss58Format < 64 ? [ss58Format] : [(ss58Format & 0b0000000011111100) >> 2 | 0b01000000, ss58Format >> 8 | (ss58Format & 0b0000000000000011) << 6], u8a);
|
20 | return base58Encode(u8aConcat(input, sshash(input).subarray(0, [32, 33].includes(u8a.length) ? 2 : 1)));
|
21 | } |
\ | No newline at end of file |