1 | import { ed25519PairFromSeed } from '../../ed25519/index.js';
|
2 | import { mnemonicValidate } from '../../mnemonic/index.js';
|
3 | import { HARDENED, hdValidatePath } from '../validatePath.js';
|
4 | import { ledgerDerivePrivate } from './derivePrivate.js';
|
5 | import { ledgerMaster } from './master.js';
|
6 | export function hdLedger(_mnemonic, path) {
|
7 | const words = _mnemonic
|
8 | .split(' ')
|
9 | .map((s) => s.trim())
|
10 | .filter((s) => s);
|
11 | if (![12, 24, 25].includes(words.length)) {
|
12 | throw new Error('Expected a mnemonic with 24 words (or 25 including a password)');
|
13 | }
|
14 | const [mnemonic, password] = words.length === 25
|
15 | ? [words.slice(0, 24).join(' '), words[24]]
|
16 | : [words.join(' '), ''];
|
17 | if (!mnemonicValidate(mnemonic)) {
|
18 | throw new Error('Invalid mnemonic passed to ledger derivation');
|
19 | }
|
20 | else if (!hdValidatePath(path)) {
|
21 | throw new Error('Invalid derivation path');
|
22 | }
|
23 | const parts = path.split('/').slice(1);
|
24 | let seed = ledgerMaster(mnemonic, password);
|
25 | for (const p of parts) {
|
26 | const n = parseInt(p.replace(/'$/, ''), 10);
|
27 | seed = ledgerDerivePrivate(seed, (n < HARDENED) ? (n + HARDENED) : n);
|
28 | }
|
29 | return ed25519PairFromSeed(seed.slice(0, 32));
|
30 | }
|