Skip to content

Commit cd7e776

Browse files
authored
Merge pull request #1676 from semsuddin/test/add-accountKeyDerivationPathToBip32Path-tests
test(key-management): add tests for accountKeyDerivationPathToBip32Path
2 parents 1b173b3 + 31b6eec commit cd7e776

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

packages/key-management/src/util/key.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ export const deriveAccountPrivateKey = async ({
4949
harden(accountIndex)
5050
]);
5151

52-
// TODO: test
5352
/**
5453
* Constructs the hardened derivation path for the specified
5554
* account key of an HD wallet as specified in CIP 1852

packages/key-management/test/util/key.test.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import { AddressType, GroupedAddress, KeyRole } from '../../src';
1+
import { AddressType, GroupedAddress, KeyPurpose, KeyRole } from '../../src';
22
import { Cardano } from '@cardano-sdk/core';
3-
import { paymentKeyPathFromGroupedAddress, stakeKeyPathFromGroupedAddress } from '../../src/util';
3+
import {
4+
accountKeyDerivationPathToBip32Path,
5+
paymentKeyPathFromGroupedAddress,
6+
stakeKeyPathFromGroupedAddress
7+
} from '../../src/util';
48

59
export const paymentAddress = Cardano.PaymentAddress(
610
'addr1qxdtr6wjx3kr7jlrvrfzhrh8w44qx9krcxhvu3e79zr7497tpmpxjfyhk3vwg6qjezjmlg5nr5dzm9j6nxyns28vsy8stu5lh6'
@@ -25,8 +29,15 @@ const knownAddress: GroupedAddress = {
2529
type: AddressType.Internal
2630
};
2731

28-
const knownAddressKeyPath = [2_147_485_500, 2_147_485_463, 2_147_483_648, 1, 0];
29-
const knownAddressStakeKeyPath = [2_147_485_500, 2_147_485_463, 2_147_483_648, 2, 0];
32+
// CIP-1852: m / 1852' / 1815' / account' / role / index (hardened = n + 0x8000_0000)
33+
const HARDENED_PURPOSE_1852 = 2_147_485_500;
34+
const HARDENED_PURPOSE_1854 = 2_147_485_502;
35+
const HARDENED_COINTYPE_1815 = 2_147_485_463;
36+
const HARDENED_ACCOUNT_0 = 2_147_483_648;
37+
const HARDENED_ACCOUNT_5 = 2_147_483_653;
38+
39+
const knownAddressKeyPath = [HARDENED_PURPOSE_1852, HARDENED_COINTYPE_1815, HARDENED_ACCOUNT_0, KeyRole.Internal, 0];
40+
const knownAddressStakeKeyPath = [HARDENED_PURPOSE_1852, HARDENED_COINTYPE_1815, HARDENED_ACCOUNT_0, KeyRole.Stake, 0];
3041

3142
describe('key utils', () => {
3243
describe('paymentKeyPathFromGroupedAddress', () => {
@@ -44,4 +55,35 @@ describe('key utils', () => {
4455
expect(stakeKeyPathFromGroupedAddress(knownAddress)).toEqual(knownAddressStakeKeyPath);
4556
});
4657
});
58+
describe('accountKeyDerivationPathToBip32Path', () => {
59+
it('returns correct path with default purpose', () => {
60+
const path = accountKeyDerivationPathToBip32Path(0, { index: 0, role: KeyRole.External });
61+
expect(path).toEqual([HARDENED_PURPOSE_1852, HARDENED_COINTYPE_1815, HARDENED_ACCOUNT_0, KeyRole.External, 0]);
62+
});
63+
64+
it('returns correct path for internal key role', () => {
65+
const path = accountKeyDerivationPathToBip32Path(0, { index: 0, role: KeyRole.Internal });
66+
expect(path).toEqual([HARDENED_PURPOSE_1852, HARDENED_COINTYPE_1815, HARDENED_ACCOUNT_0, KeyRole.Internal, 0]);
67+
});
68+
69+
it('returns correct path for stake key role', () => {
70+
const path = accountKeyDerivationPathToBip32Path(0, { index: 0, role: KeyRole.Stake });
71+
expect(path).toEqual([HARDENED_PURPOSE_1852, HARDENED_COINTYPE_1815, HARDENED_ACCOUNT_0, KeyRole.Stake, 0]);
72+
});
73+
74+
it('returns correct path for DRep key role', () => {
75+
const path = accountKeyDerivationPathToBip32Path(0, { index: 0, role: KeyRole.DRep });
76+
expect(path).toEqual([HARDENED_PURPOSE_1852, HARDENED_COINTYPE_1815, HARDENED_ACCOUNT_0, KeyRole.DRep, 0]);
77+
});
78+
79+
it('returns correct path with custom account index', () => {
80+
const path = accountKeyDerivationPathToBip32Path(5, { index: 3, role: KeyRole.External });
81+
expect(path).toEqual([HARDENED_PURPOSE_1852, HARDENED_COINTYPE_1815, HARDENED_ACCOUNT_5, KeyRole.External, 3]);
82+
});
83+
84+
it('returns correct path with custom purpose', () => {
85+
const path = accountKeyDerivationPathToBip32Path(0, { index: 0, role: KeyRole.External }, KeyPurpose.MULTI_SIG);
86+
expect(path).toEqual([HARDENED_PURPOSE_1854, HARDENED_COINTYPE_1815, HARDENED_ACCOUNT_0, KeyRole.External, 0]);
87+
});
88+
});
4789
});

0 commit comments

Comments
 (0)