Skip to content

Commit 809410e

Browse files
authored
Validate CIP-3 master node entropy size (#171)
1 parent c94ade2 commit 809410e

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/derivers/bip39.test.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,49 @@ describe('createBip39KeyFromSeed', () => {
8383
});
8484

8585
describe('Cip3', () => {
86-
fixtures.cip3.forEach((fixture) => {
87-
describe('entropyToCip3MasterNode', () => {
88-
it('derives the correct bip39 key for ed25519Bip32 curve', async () => {
86+
describe('entropyToCip3MasterNode', () => {
87+
it.each(fixtures.cip3)(
88+
'derives the correct bip39 key for ed25519Bip32 curve',
89+
async (fixture) => {
8990
const result = await entropyToCip3MasterNode(
9091
hexToBytes(fixture.entropyHex),
9192
ed25519Bip32,
9293
);
9394
const { bip39Node } = fixture.nodes;
9495
expect(result.privateKey).toBe(bip39Node.privateKey);
9596
expect(result.chainCode).toBe(bip39Node.chainCode);
96-
});
97+
},
98+
);
99+
100+
it('throws if the entropy is less than 16 bytes', async () => {
101+
await expect(
102+
entropyToCip3MasterNode(new Uint8Array(15), ed25519Bip32),
103+
).rejects.toThrow(
104+
'Invalid entropy: The entropy must be between 16 and 64 bytes long.',
105+
);
97106
});
98107

99-
describe('deriveChildKey', () => {
100-
it('derives the correct child key for ed25519Bip32 curve from mnemonic', async () => {
108+
it('throws if the entropy is greater than 64 bytes', async () => {
109+
await expect(
110+
entropyToCip3MasterNode(new Uint8Array(65), ed25519Bip32),
111+
).rejects.toThrow(
112+
'Invalid entropy: The entropy must be between 16 and 64 bytes long.',
113+
);
114+
});
115+
});
116+
117+
describe('deriveChildKey', () => {
118+
it.each(fixtures.cip3)(
119+
'derives the correct child key for ed25519Bip32 curve from mnemonic',
120+
async (fixture) => {
101121
const result = await deriveChildKey({
102122
path: fixture.mnemonic,
103123
curve: ed25519Bip32,
104124
});
105125
const { bip39Node } = fixture.nodes;
106126
expect(result.privateKey).toBe(bip39Node.privateKey);
107127
expect(result.chainCode).toBe(bip39Node.chainCode);
108-
});
109-
});
128+
},
129+
);
110130
});
111131
});

src/derivers/bip39.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ export async function entropyToCip3MasterNode(
106106
entropy: Uint8Array,
107107
curve: Extract<Curve, { masterNodeGenerationSpec: 'cip3' }>,
108108
): Promise<SLIP10Node> {
109+
assert(
110+
entropy.length >= 16 && entropy.length <= 64,
111+
'Invalid entropy: The entropy must be between 16 and 64 bytes long.',
112+
);
113+
109114
const rootNode = pbkdf2(sha512, curve.secret, entropy, {
110115
c: 4096,
111116
dkLen: 96,

0 commit comments

Comments
 (0)