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

test: coin-support-utils db operations #451

Draft
wants to merge 18 commits into
base: develop
Choose a base branch
from
Draft
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
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: [2147483692],
},
{
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('mapDerivationPath', () => {
fixtures.valid.forEach(({ name, input, output }) => {
test(name, () => {
const result = mapDerivationPath(input);
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,16 @@
export interface DerivationPathGeneratorTestCases {
name: string;
input: {
basePath: string;
existingDerivationPaths: string[];
limit: number;
};
output: {
derivationPath: string;
index: number;
}[];
}

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 alreadty 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/createAccount';
import { fixtures } from './__fixtures__';

describe('01. Create Derivation Path Generator', () => {
fixtures.valid.forEach(({ name, input, output }) => {
test(name, () => {
const generator = createDerivationPathGenerator(input.basePath);
const result = generator(input.existingDerivationPaths, input.limit);
expect(generator).toBeInstanceOf(Function);
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,17 @@
import { IGenerateDerivationPathsPerSchemeParams } from '../../../../src/createAccount';

export interface DerivationPathPerSchemeGeneratorTestCases {
name: string;
input: IGenerateDerivationPathsPerSchemeParams;
output: Record<
string,
{
derivationPath: string;
index: number;
}[]
>;
}

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