Use pathToString from @iov/crypto

This commit is contained in:
Simon Warta 2020-06-04 21:01:17 +02:00
parent 538004a1d6
commit 8b98b6c686
3 changed files with 2 additions and 57 deletions

View File

@ -1,44 +0,0 @@
import { Slip10RawIndex } from "@iov/crypto";
import { debugPath } from "./hdpaths";
describe("hdpaths", () => {
describe("debugPath", () => {
it("works for no component", () => {
// See https://github.com/bitcoin/bips/blob/master/bip-0032/derivation.png from BIP32
expect(debugPath([])).toEqual("m");
});
it("works for normal components", () => {
const one = Slip10RawIndex.normal(1);
expect(debugPath([one])).toEqual("m/1");
expect(debugPath([one, one])).toEqual("m/1/1");
expect(debugPath([one, one, one])).toEqual("m/1/1/1");
const min = Slip10RawIndex.normal(0);
expect(debugPath([min])).toEqual("m/0");
const max = Slip10RawIndex.normal(2 ** 31 - 1);
expect(debugPath([max])).toEqual("m/2147483647");
});
it("works for hardened components", () => {
const one = Slip10RawIndex.hardened(1);
expect(debugPath([one])).toEqual("m/1'");
expect(debugPath([one, one])).toEqual("m/1'/1'");
expect(debugPath([one, one, one])).toEqual("m/1'/1'/1'");
const min = Slip10RawIndex.hardened(0);
expect(debugPath([min])).toEqual("m/0'");
const max = Slip10RawIndex.hardened(2 ** 31 - 1);
expect(debugPath([max])).toEqual("m/2147483647'");
});
it("works for mixed components", () => {
const one = Slip10RawIndex.normal(1);
const two = Slip10RawIndex.hardened(2);
expect(debugPath([one, two, two, one])).toEqual("m/1/2'/2'/1");
});
});
});

View File

@ -1,10 +0,0 @@
import { Slip10RawIndex } from "@iov/crypto";
export function debugPath(path: readonly Slip10RawIndex[]): string {
return path.reduce((current, component): string => {
const componentString = component.isHardened()
? `${component.toNumber() - 2 ** 31}'`
: component.toString();
return current + "/" + componentString;
}, "m");
}

View File

@ -1,6 +1,5 @@
import { makeCosmoshubPath, Pen, Secp256k1Pen } from "@cosmjs/sdk38";
import { debugPath } from "./hdpaths";
import { pathToString } from "@iov/crypto";
export async function createPens(
mnemonic: string,
@ -18,7 +17,7 @@ export async function createPens(
const address = pen.address(addressPrefix);
if (logging) {
const role = i === 0 ? "token holder " : `distributor ${i}`;
console.info(`Created ${role} (${debugPath(path)}): ${address}`);
console.info(`Created ${role} (${pathToString(path)}): ${address}`);
}
pens.push([address, pen]);
}