Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add network property #200

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .depcheckrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"eslint-import-resolver-typescript",
"eslint-plugin-*",
"prettier-plugin-packagejson",
"ts-jest",
"ts-node",
"typedoc",
"typescript-eslint"
Expand Down
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ module.exports = {
// A preset that is used as a base for Jest's configuration
preset: 'ts-jest',

// Jest doesn't support Prettier 3 yet, so we disable it
// TODO: Remove this when bumping to Jest 30
prettierPath: null,

// Run tests from one or more projects
// projects: undefined,

Expand Down
34 changes: 31 additions & 3 deletions src/BIP44CoinTypeNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '.';
import type { CryptographicFunctions } from './cryptography';
import { hmacSha512, pbkdf2Sha512 } from './cryptography';
import { encodeExtendedKey, PRIVATE_KEY_VERSION } from './extended-keys';
import { encodeExtendedKey } from './extended-keys';
import { mnemonicPhraseToBytes } from './utils';
import fixtures from '../test/fixtures';

Expand Down Expand Up @@ -64,6 +64,7 @@ describe('BIP44CoinTypeNode', () => {
masterFingerprint: node.masterFingerprint,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: node.network,
path: pathString,
privateKey: node.privateKey,
publicKey: node.publicKey,
Expand Down Expand Up @@ -226,6 +227,7 @@ describe('BIP44CoinTypeNode', () => {
masterFingerprint: node.masterFingerprint,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: node.network,
path: pathString,
privateKey: node.privateKey,
publicKey: node.publicKey,
Expand Down Expand Up @@ -263,6 +265,7 @@ describe('BIP44CoinTypeNode', () => {
masterFingerprint: node.masterFingerprint,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: node.network,
path: pathString,
privateKey: node.privateKey,
publicKey: node.publicKey,
Expand Down Expand Up @@ -290,6 +293,7 @@ describe('BIP44CoinTypeNode', () => {
const functions = getMockFunctions();
const node = await BIP44CoinTypeNode.fromDerivationPath(
[defaultBip39NodeToken, BIP44PurposeNodeToken, `bip32:60'`],
'mainnet',
functions,
);

Expand Down Expand Up @@ -443,6 +447,7 @@ describe('BIP44CoinTypeNode', () => {
const functions = getMockFunctions();
const coinTypeNode = await BIP44CoinTypeNode.fromDerivationPath(
[defaultBip39NodeToken, BIP44PurposeNodeToken, `bip32:60'`],
'mainnet',
functions,
);

Expand Down Expand Up @@ -533,7 +538,7 @@ describe('BIP44CoinTypeNode', () => {
});

describe('extendedKey', () => {
it('returns the extended private key for nodes with a private key', async () => {
it('returns the extended private key for nodes with a private key (mainnet)', async () => {
const coinType = 60;
const node = await BIP44CoinTypeNode.fromDerivationPath([
defaultBip39NodeToken,
Expand All @@ -542,12 +547,33 @@ describe('BIP44CoinTypeNode', () => {
]);

const extendedKey = encodeExtendedKey({
version: PRIVATE_KEY_VERSION,
type: 'private',
privateKey: node.privateKeyBytes as Uint8Array,
chainCode: node.chainCodeBytes,
depth: node.depth,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: 'mainnet',
});

expect(node.extendedKey).toStrictEqual(extendedKey);
});

it('returns the extended private key for nodes with a private key (testnet)', async () => {
const coinType = 60;
const node = await BIP44CoinTypeNode.fromDerivationPath(
[defaultBip39NodeToken, BIP44PurposeNodeToken, `bip32:${coinType}'`],
'testnet',
);

const extendedKey = encodeExtendedKey({
type: 'private',
privateKey: node.privateKeyBytes as Uint8Array,
chainCode: node.chainCodeBytes,
depth: node.depth,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: 'testnet',
});

expect(node.extendedKey).toStrictEqual(extendedKey);
Expand Down Expand Up @@ -575,6 +601,7 @@ describe('BIP44CoinTypeNode', () => {
masterFingerprint: node.masterFingerprint,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: node.network,
path: pathString,
privateKey: node.privateKey,
publicKey: node.publicKey,
Expand All @@ -587,6 +614,7 @@ describe('BIP44CoinTypeNode', () => {
masterFingerprint: node.masterFingerprint,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: node.network,
path: pathString,
privateKey: node.privateKey,
publicKey: node.publicKey,
Expand Down
10 changes: 10 additions & 0 deletions src/BIP44CoinTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
BIP44PurposeNodeToken,
CoinTypeHDPathString,
HardenedBIP32Node,
Network,
} from './constants';
import { BIP_32_HARDENED_OFFSET } from './constants';
import type { CryptographicFunctions } from './cryptography';
Expand Down Expand Up @@ -92,6 +93,7 @@ export class BIP44CoinTypeNode implements BIP44CoinTypeNodeInterface {
{
depth: json.depth,
index: json.index,
network: json.network,
parentFingerprint: json.parentFingerprint,
chainCode: hexStringToBytes(json.chainCode),
privateKey: nullableHexStringToBytes(json.privateKey),
Expand All @@ -118,19 +120,23 @@ export class BIP44CoinTypeNode implements BIP44CoinTypeNodeInterface {
* `0 / 1 / 2 / 3 / 4 / 5`
*
* @param derivationPath - The derivation path for the key of this node.
* @param network - The network for the node. This is only used for extended
* keys, and defaults to `mainnet`.
* @param cryptographicFunctions - The cryptographic functions to use. If
* provided, these will be used instead of the built-in implementations.
* @returns A BIP44CoinType node.
*/
static async fromDerivationPath(
derivationPath: CoinTypeHDPathTuple,
network?: Network | undefined,
cryptographicFunctions?: CryptographicFunctions,
): Promise<BIP44CoinTypeNode> {
validateCoinTypeNodeDepth(derivationPath.length - 1);

const node = await BIP44Node.fromDerivationPath(
{
derivationPath,
network,
},
cryptographicFunctions,
);
Expand Down Expand Up @@ -252,6 +258,10 @@ export class BIP44CoinTypeNode implements BIP44CoinTypeNodeInterface {
return this.#node.index;
}

public get network(): Network {
return this.#node.network;
}

public get curve(): SupportedCurve {
return this.#node.curve;
}
Expand Down
72 changes: 63 additions & 9 deletions src/BIP44Node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import type { CryptographicFunctions } from './cryptography';
import { hmacSha512, pbkdf2Sha512 } from './cryptography';
import { compressPublicKey } from './curves/secp256k1';
import { createBip39KeyFromSeed, deriveChildKey } from './derivers/bip39';
import {
encodeExtendedKey,
PRIVATE_KEY_VERSION,
PUBLIC_KEY_VERSION,
} from './extended-keys';
import { encodeExtendedKey } from './extended-keys';
import { hexStringToBytes, mnemonicPhraseToBytes } from './utils';
import fixtures from '../test/fixtures';

Expand Down Expand Up @@ -56,6 +52,7 @@ describe('BIP44Node', () => {
masterFingerprint: node.masterFingerprint,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: node.network,
privateKey: node.privateKey,
publicKey: node.publicKey,
chainCode: node.chainCode,
Expand Down Expand Up @@ -90,6 +87,7 @@ describe('BIP44Node', () => {
masterFingerprint: node.masterFingerprint,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: node.network,
privateKey: node.privateKey,
publicKey: node.publicKey,
chainCode: node.chainCode,
Expand Down Expand Up @@ -208,6 +206,7 @@ describe('BIP44Node', () => {
masterFingerprint: node.masterFingerprint,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: node.network,
privateKey: node.privateKey,
publicKey: node.publicKey,
chainCode: node.chainCode,
Expand Down Expand Up @@ -255,6 +254,7 @@ describe('BIP44Node', () => {
masterFingerprint: node.masterFingerprint,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: node.network,
privateKey: node.privateKey,
publicKey: node.publicKey,
chainCode: node.chainCode,
Expand Down Expand Up @@ -645,7 +645,7 @@ describe('BIP44Node', () => {
});

describe('extendedKey', () => {
it('returns the extended private key for nodes with a private key', async () => {
it('returns the extended private key for nodes with a private key (mainnet)', async () => {
const node = await BIP44Node.fromDerivationPath({
derivationPath: [
defaultBip39NodeToken,
Expand All @@ -656,36 +656,88 @@ describe('BIP44Node', () => {
});

const extendedKey = encodeExtendedKey({
version: PRIVATE_KEY_VERSION,
type: 'private',
privateKey: node.privateKeyBytes as Uint8Array,
chainCode: node.chainCodeBytes,
depth: node.depth,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: 'mainnet',
});

expect(node.extendedKey).toStrictEqual(extendedKey);
});

it('returns the extended public key for nodes with a public key', async () => {
it('returns the extended private key for nodes with a private key (testnet)', async () => {
const node = await BIP44Node.fromDerivationPath({
derivationPath: [
defaultBip39NodeToken,
BIP44PurposeNodeToken,
`bip32:0'`,
`bip32:0'`,
],
network: 'testnet',
});

const extendedKey = encodeExtendedKey({
type: 'private',
privateKey: node.privateKeyBytes as Uint8Array,
chainCode: node.chainCodeBytes,
depth: node.depth,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: 'testnet',
});

expect(node.extendedKey).toStrictEqual(extendedKey);
});

it('returns the extended public key for nodes with a public key (mainnet)', async () => {
const node = await BIP44Node.fromDerivationPath({
derivationPath: [
defaultBip39NodeToken,
BIP44PurposeNodeToken,
`bip32:0'`,
`bip32:0'`,
],
});

const neuteredNode = node.neuter();

const extendedKey = encodeExtendedKey({
type: 'public',
publicKey: neuteredNode.publicKeyBytes,
chainCode: neuteredNode.chainCodeBytes,
depth: neuteredNode.depth,
parentFingerprint: neuteredNode.parentFingerprint,
index: neuteredNode.index,
network: 'mainnet',
});

expect(neuteredNode.extendedKey).toStrictEqual(extendedKey);
});

it('returns the extended public key for nodes with a public key (testnet)', async () => {
const node = await BIP44Node.fromDerivationPath({
derivationPath: [
defaultBip39NodeToken,
BIP44PurposeNodeToken,
`bip32:0'`,
`bip32:0'`,
],
network: 'testnet',
});

const neuteredNode = node.neuter();

const extendedKey = encodeExtendedKey({
version: PUBLIC_KEY_VERSION,
type: 'public',
publicKey: neuteredNode.publicKeyBytes,
chainCode: neuteredNode.chainCodeBytes,
depth: neuteredNode.depth,
parentFingerprint: neuteredNode.parentFingerprint,
index: neuteredNode.index,
network: 'testnet',
});

expect(neuteredNode.extendedKey).toStrictEqual(extendedKey);
Expand Down Expand Up @@ -754,6 +806,7 @@ describe('BIP44Node', () => {
masterFingerprint: node.masterFingerprint,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: node.network,
privateKey: node.privateKey,
publicKey: node.publicKey,
chainCode: node.chainCode,
Expand All @@ -764,6 +817,7 @@ describe('BIP44Node', () => {
masterFingerprint: node.masterFingerprint,
parentFingerprint: node.parentFingerprint,
index: node.index,
network: node.network,
privateKey: node.privateKey,
publicKey: node.publicKey,
chainCode: node.chainCode,
Expand Down
Loading
Loading