Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0b4acac
test: create account support functions
jhasuraj01 Apr 12, 2024
66fcdb3
fix: undo createDerivationPathGenerator logic
jhasuraj01 Apr 13, 2024
12ed23d
test: added name generator test for random schemeName
jhasuraj01 Apr 13, 2024
58a7f5a
test: mapDerivationPath, generate Address Per Scheme
jhasuraj01 Apr 15, 2024
3784704
test: makeCreateAccountsObservable
jhasuraj01 Apr 15, 2024
8d68574
test: improved branch coverage
jhasuraj01 Apr 16, 2024
b8999ec
test: improved makeCreateAccountsObservable test
jhasuraj01 Apr 17, 2024
da86830
test: makeCreateAccountsObservable
jhasuraj01 Apr 19, 2024
b20ec44
test: don't run coin-support-utils test
jhasuraj01 Apr 19, 2024
06d03a6
test: undo don't run coin-support-utils test
jhasuraj01 Apr 19, 2024
a1bd014
ci: changed ubuntu version to 22.04
jhasuraj01 Apr 19, 2024
0a39584
ci: undo changed ubuntu version to 22.04
jhasuraj01 Apr 19, 2024
fcec490
chore: skip turbo cache for test
jhasuraj01 Apr 21, 2024
89b8a07
chore: undo skip turbo cache for test
jhasuraj01 Apr 22, 2024
0c640f3
test: removed unwanted files and imporved branch coverage
jhasuraj01 Apr 22, 2024
0777687
chore: skip turbo cache for test
jhasuraj01 Apr 22, 2024
66181f2
chore: undo skip turbo cache for test
jhasuraj01 Apr 22, 2024
5e88f83
refactor: write the output in terms of 0x80000000 to make it more rea…
jhasuraj01 May 2, 2024
1dc62b6
fix: spelling mistakes
jhasuraj01 May 2, 2024
216280d
refactor: corrected order of statements
jhasuraj01 May 2, 2024
c22e01f
refactor: updating desc to func name
jhasuraj01 May 2, 2024
e10660a
refactor: updated test description
jhasuraj01 May 2, 2024
3c43eda
refactor: updated test description
jhasuraj01 May 2, 2024
28f71d4
refactor: updated import path
jhasuraj01 May 2, 2024
dbbaa27
refactor: updated test description
jhasuraj01 May 2, 2024
8a2dae7
refactor: updated test description
jhasuraj01 May 2, 2024
b5232d3
refactor: updated import path
jhasuraj01 May 2, 2024
30174fd
refactor: updated import path
jhasuraj01 May 2, 2024
950eb96
refactor: updated test description
jhasuraj01 May 2, 2024
21663ee
test: expect generator func
jhasuraj01 May 2, 2024
7385e20
refactor: removed duplicate type def
jhasuraj01 May 2, 2024
de036aa
test: removed type def
jhasuraj01 May 2, 2024
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
7 changes: 4 additions & 3 deletions packages/coin-support-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
"scripts": {
"lint": "eslint --ext .ts,tsx,js,jsx src/ --fix",
"lint:check": "eslint --ext .ts,tsx,js,jsx src/",
"pretty": "prettier --write \"src/**/*.ts?(x)\"",
"pretty:check": "prettier --check \"src/**/*.ts?(x)\"",
"pretty": "prettier --write \"src/**/*.ts?(x)\" \"tests/**/*.ts?(x)\"",
"pretty:check": "prettier --check \"src/**/*.ts?(x)\" \"tests/**/*.ts?(x)\"",
"build": "rimraf dist && pnpm build:esm && pnpm build:cjs",
"build:cjs": "tsc -p tsconfig_cjs.json",
"build:esm": "tsc -p tsconfig.json",
"build:dirty": "pnpm build:esm",
"pre-commit": "lint-staged"
"pre-commit": "lint-staged",
"test": "jest"
},
"devDependencies": {
"@cypherock/eslint-config": "workspace:^",
Expand Down
15 changes: 7 additions & 8 deletions packages/coin-support-utils/src/createAccount/createAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,15 @@ export function makeCreateAccountsObservable<
assetId: params.coinId,
});

const derivationPathsPerScheme = await generateDerivationPathsPerScheme(
{
...params,
limit: params.derivationPathLimit,
existingAccounts,
},
);
if (finished) return;
const derivationPathsPerScheme = generateDerivationPathsPerScheme({
...params,
limit: params.derivationPathLimit,
existingAccounts,
});

app = await params.createApp(params.connection);
if (finished) return;

const addressesPerScheme = await generateAddressesPerScheme({
...params,
app,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface IGenerateAddressesPerSchemeParams<T>
export async function generateAddressesPerScheme<T>(
params: IGenerateAddressesPerSchemeParams<T>,
) {
const { derivationPathsPerScheme } = params;
const { derivationPathsPerScheme, getAddressesFromDevice } = params;
const allDerivationPaths = Object.values(derivationPathsPerScheme).reduce(
(a, b) => [...a, ...b],
[],
Expand All @@ -40,7 +40,7 @@ export async function generateAddressesPerScheme<T>(
index: startIndex,
}));

const addresses = await params.getAddressesFromDevice({
const addresses = await getAddressesFromDevice({
...params,
derivationPaths: mappedDerivationPaths,
});
Expand Down
20 changes: 8 additions & 12 deletions packages/coin-support-utils/src/createAccount/schemes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
ICreateAccountParams,
IDerivationPathGenerator,
IDerivationScheme,
} from '@cypherock/coin-support-interfaces';
Expand Down Expand Up @@ -31,25 +30,25 @@ export const createDerivationPathGenerator =
return derivationPaths;
};

export interface IGenerateDerivationPathsPerSchemeParams
extends ICreateAccountParams {
export interface IGenerateDerivationPathsPerSchemeParams {
derivationPathSchemes: Record<string, IDerivationScheme | undefined>;
limit: number;
existingAccounts: IAccount[];
}

export const generateDerivationPathsPerScheme = async (
export const generateDerivationPathsPerScheme = (
params: IGenerateDerivationPathsPerSchemeParams,
) => {
const { derivationPathSchemes, limit, existingAccounts } = params;

const existingDerivationPaths = existingAccounts.map(e => e.derivationPath);

const derivationSchemeNames = Object.keys(derivationPathSchemes).filter(
n => !!derivationPathSchemes[n],
);
const derivationSchemes = Object.entries(derivationPathSchemes).filter(
([, value]) => value !== undefined,
) as [string, IDerivationScheme][];

const pathLimitPerDerivationScheme = Math.floor(
limit / derivationSchemeNames.length,
limit / derivationSchemes.length,
);

const derivedPathsPerScheme: Record<
Expand All @@ -58,10 +57,7 @@ export const generateDerivationPathsPerScheme = async (
> = {};
const derivedPaths: string[] = [];

for (const schemeName of derivationSchemeNames) {
const derivationPathSchemeDetails = derivationPathSchemes[schemeName];
if (!derivationPathSchemeDetails) continue;

for (const [schemeName, derivationPathSchemeDetails] of derivationSchemes) {
const paths = derivationPathSchemeDetails.generator(
// This is done because there can be overlapping derivation paths
// between different schemes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IFixtures } from './types';
import { valid } from './valid';

export const fixtures: IFixtures = {
valid,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface MapDerivationPathTestCases {
name: string;
input: string;
output: number[];
}

export interface IFixtures {
valid: MapDerivationPathTestCases[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MapDerivationPathTestCases } from './types';

export const valid: MapDerivationPathTestCases[] = [
{
name: 'should map derivation path with single index',
input: "m/44'",
output: [0x80000000 + 44],
},
{
name: 'should map derivation path with multiple index',
input: "m/44'/0'/0'/0/0",
output: [2147483692, 2147483648, 2147483648, 0, 0],
},
{
name: 'should map derivation path with multiple index without m prefix',
input: "44'/0'/0'/0/0",
output: [2147483692, 2147483648, 2147483648, 0, 0],
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, test } from '@jest/globals';
import { fixtures } from './__fixtures__';
import { mapDerivationPath } from '../../../src';

describe('01. mapDerivationPath', () => {
fixtures.valid.forEach(({ name, input, output }) => {
test(name, () => {
const result = mapDerivationPath(input);
expect(result).toStrictEqual(output);
});
});
Comment on lines +6 to +11
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs negative test cases as well

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IFixtures } from './types';
import { valid } from './valid';

export const fixtures: IFixtures = {
valid,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createDerivationPathGenerator } from '../../../../src';

export interface DerivationPathGeneratorTestCases {
name: string;
input: {
basePath: string;
existingDerivationPaths: string[];
limit: number;
};
output: ReturnType<ReturnType<typeof createDerivationPathGenerator>>;
}

export interface IFixtures {
valid: DerivationPathGeneratorTestCases[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { DerivationPathGeneratorTestCases } from './types';

export const valid: DerivationPathGeneratorTestCases[] = [
{
name: 'should generate derivation path from zero index',
input: {
basePath: "m/44'/0'/0'/0/i",
existingDerivationPaths: [],
limit: 3,
},
output: [
{
derivationPath: "m/44'/0'/0'/0/0",
index: 0,
},
{
derivationPath: "m/44'/0'/0'/0/1",
index: 1,
},
{
derivationPath: "m/44'/0'/0'/0/2",
index: 2,
},
],
},
{
name: 'should generate derivation path excluding existing derivation path',
input: {
basePath: "m/44'/0'/0'/0/i",
existingDerivationPaths: ["m/44'/0'/0'/0/1"],
limit: 2,
},
output: [
{
derivationPath: "m/44'/0'/0'/0/0",
index: 0,
},
{
derivationPath: "m/44'/0'/0'/0/2",
index: 2,
},
],
},
{
name: 'should return basePath if it does not include i',
input: {
basePath: "m/44'/0'/0'/0/0",
existingDerivationPaths: [],
limit: 2,
},
output: [
{
derivationPath: "m/44'/0'/0'/0/0",
index: 0,
},
],
},
{
name: 'should return return empty array if base path is already derived and it does not include i',
input: {
basePath: "m/44'/0'/0'/0/0",
existingDerivationPaths: ["m/44'/0'/0'/0/0"],
limit: 2,
},
output: [],
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, test } from '@jest/globals';
import { createDerivationPathGenerator } from '../../../src';
import { fixtures } from './__fixtures__';

describe('01. createDerivationPathGenerator', () => {
fixtures.valid.forEach(({ name, input, output }) => {
test(name, () => {
const generator = createDerivationPathGenerator(input.basePath);
expect(generator).toBeInstanceOf(Function);
const result = generator(input.existingDerivationPaths, input.limit);
expect(result).toStrictEqual(output);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IFixtures } from './types';
import { valid } from './valid';

export const fixtures: IFixtures = {
valid,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
IGenerateDerivationPathsPerSchemeParams,
generateDerivationPathsPerScheme,
} from '../../../../src';

export interface DerivationPathPerSchemeGeneratorTestCases {
name: string;
input: IGenerateDerivationPathsPerSchemeParams;
output: ReturnType<typeof generateDerivationPathsPerScheme>;
}

export interface IFixtures {
valid: DerivationPathPerSchemeGeneratorTestCases[];
}
Loading