UNPKG

910 BJavaScriptView Raw
1import { keyExtractPath } from '../key/index.js';
2import { sr25519DerivePublic } from '../sr25519/index.js';
3import { decodeAddress } from './decode.js';
4import { encodeAddress } from './encode.js';
5function filterHard({ isHard }) {
6 return isHard;
7}
8/**
9 * @name deriveAddress
10 * @summary Creates a sr25519 derived address from the supplied and path.
11 * @description
12 * Creates a sr25519 derived address based on the input address/publicKey and the uri supplied.
13 */
14export function deriveAddress(who, suri, ss58Format) {
15 const { path } = keyExtractPath(suri);
16 if (!path.length || path.every(filterHard)) {
17 throw new Error('Expected suri to contain a combination of non-hard paths');
18 }
19 let publicKey = decodeAddress(who);
20 for (const { chainCode } of path) {
21 publicKey = sr25519DerivePublic(publicKey, chainCode);
22 }
23 return encodeAddress(publicKey, ss58Format);
24}