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 CLI example for generating Cosmos HD path test vectors #834

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/cli/examples/cosmos_purpose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { toHex } from "@cosmjs/encoding";
import { Slip10RawIndex, HdPath, pathToString } from "@cosmjs/crypto";
import { makeCosmosPath, makeSimpleHdPath } from "@cosmjs/proto-signing";

function printPath(path: HdPath) {
console.log(pathToString(path) + ": " + path.map(component => toHex(component.toBytesBigEndian())).join(","));
}

console.log("Simple HD path for account 0, 1, 75_000_000 on the testing chain:");
for (let a of [0, 1, 75_000_000]) {
printPath(makeSimpleHdPath(0, a));
}

console.log("Simple HD path for account 0 on the chains 0, 1, 42, 42_000_000:");
for (let chainIndex of [0, 1, 42, 42_000_000]) {
printPath(makeSimpleHdPath(chainIndex, 0));
}

console.log("Cosmos path with all unhardened sub-trees of length 0, 1, 3 on the testing chain:");
printPath(makeCosmosPath(0));
printPath(makeCosmosPath(0, Slip10RawIndex.normal(7)));
printPath(makeCosmosPath(0, Slip10RawIndex.normal(7), Slip10RawIndex.normal(7), Slip10RawIndex.normal(7)));

console.log("Cosmos path with all hardened sub-trees of length 0, 1, 3 on the testing chain:");
printPath(makeCosmosPath(0));
printPath(makeCosmosPath(0, Slip10RawIndex.hardened(7)));
printPath(makeCosmosPath(0, Slip10RawIndex.hardened(7), Slip10RawIndex.hardened(7), Slip10RawIndex.hardened(7)));

console.log("Cosmos path with hardened/unhardened sub-tree on the testing chain:");
printPath(makeCosmosPath(0, Slip10RawIndex.hardened(2), Slip10RawIndex.normal(3), Slip10RawIndex.hardened(4)))
1 change: 1 addition & 0 deletions packages/cli/run_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set -o errexit -o nounset -o pipefail
command -v shellcheck >/dev/null && shellcheck "$0"

yarn node ./bin/cosmwasm-cli --init examples/coralnet.ts --code "process.exit(0)"
yarn node ./bin/cosmwasm-cli --init examples/cosmos_purpose.ts --code "process.exit(0)"
if [ -n "${LAUNCHPAD_ENABLED:-}" ]; then
yarn node ./bin/cosmwasm-cli --init examples/delegate.ts --code "process.exit(0)"
fi
Expand Down
2 changes: 1 addition & 1 deletion packages/proto-signing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export {
DirectSecp256k1HdWalletOptions,
} from "./directsecp256k1hdwallet";
export { DirectSecp256k1Wallet } from "./directsecp256k1wallet";
export { makeCosmoshubPath } from "./paths";
export { makeCosmoshubPath, makeCosmosPath, makeSimpleHdPath } from "./paths";
export { decodePubkey, encodePubkey } from "./pubkey";
export {
AccountData,
Expand Down
116 changes: 115 additions & 1 deletion packages/proto-signing/src/paths.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Slip10RawIndex } from "@cosmjs/crypto";

import { makeCosmoshubPath } from "./paths";
import { makeCosmoshubPath, makeCosmosPath, makeSimpleHdPath } from "./paths";

describe("paths", () => {
describe("makeCosmoshubPath", () => {
Expand All @@ -23,4 +23,118 @@ describe("paths", () => {
]);
});
});

describe("makeCosmosPath", () => {
it("works with all unhardened sub-trees on the testing chain", () => {
// m/7564153'/0'
expect(makeCosmosPath(0)).toEqual([Slip10RawIndex.hardened(7564153), Slip10RawIndex.hardened(0)]);
// m/7564153'/0'/7
expect(makeCosmosPath(0, Slip10RawIndex.normal(7))).toEqual([
Slip10RawIndex.hardened(7564153),
Slip10RawIndex.hardened(0),
Slip10RawIndex.normal(7),
]);
// m/7564153'/0'/7/7/7
expect(
makeCosmosPath(0, Slip10RawIndex.normal(7), Slip10RawIndex.normal(7), Slip10RawIndex.normal(7)),
).toEqual([
Slip10RawIndex.hardened(7564153),
Slip10RawIndex.hardened(0),
Slip10RawIndex.normal(7),
Slip10RawIndex.normal(7),
Slip10RawIndex.normal(7),
]);
});

it("works with all hardened sub-trees on the testing chain", () => {
// m/7564153'/0'
expect(makeCosmosPath(0)).toEqual([Slip10RawIndex.hardened(7564153), Slip10RawIndex.hardened(0)]);
// m/7564153'/0'/7'
expect(makeCosmosPath(0, Slip10RawIndex.hardened(7))).toEqual([
Slip10RawIndex.hardened(7564153),
Slip10RawIndex.hardened(0),
Slip10RawIndex.hardened(7),
]);
// m/7564153'/0'/7'/7'/7'
expect(
makeCosmosPath(0, Slip10RawIndex.hardened(7), Slip10RawIndex.hardened(7), Slip10RawIndex.hardened(7)),
).toEqual([
Slip10RawIndex.hardened(7564153),
Slip10RawIndex.hardened(0),
Slip10RawIndex.hardened(7),
Slip10RawIndex.hardened(7),
Slip10RawIndex.hardened(7),
]);
});

it("works with hardened/unhardened sub-tree on the testing chain", () => {
// m/7564153'/0'/7'/7'/7'
expect(
makeCosmosPath(0, Slip10RawIndex.hardened(2), Slip10RawIndex.normal(3), Slip10RawIndex.hardened(4)),
).toEqual([
Slip10RawIndex.hardened(7564153),
Slip10RawIndex.hardened(0),
Slip10RawIndex.hardened(2),
Slip10RawIndex.normal(3),
Slip10RawIndex.hardened(4),
]);
});
});

describe("makeSimpleHdPath", () => {
it("works for account on the testing chain", () => {
// m/7564153'/0'/1'/0
expect(makeSimpleHdPath(0, 0)).toEqual([
Slip10RawIndex.hardened(7564153),
Slip10RawIndex.hardened(0),
Slip10RawIndex.hardened(1),
Slip10RawIndex.normal(0),
]);
// m/7564153'/0'/1'/1
expect(makeSimpleHdPath(0, 1)).toEqual([
Slip10RawIndex.hardened(7564153),
Slip10RawIndex.hardened(0),
Slip10RawIndex.hardened(1),
Slip10RawIndex.normal(1),
]);
// m/7564153'/0'/1'/75000000
expect(makeSimpleHdPath(0, 75_000_000)).toEqual([
Slip10RawIndex.hardened(7564153),
Slip10RawIndex.hardened(0),
Slip10RawIndex.hardened(1),
Slip10RawIndex.normal(75_000_000),
]);
});

it("works for account 0 on different chains", () => {
// m/7564153'/0'/1'/0
expect(makeSimpleHdPath(0, 0)).toEqual([
Slip10RawIndex.hardened(7564153),
Slip10RawIndex.hardened(0),
Slip10RawIndex.hardened(1),
Slip10RawIndex.normal(0),
]);
// m/7564153'/1'/1'/0
expect(makeSimpleHdPath(1, 0)).toEqual([
Slip10RawIndex.hardened(7564153),
Slip10RawIndex.hardened(1),
Slip10RawIndex.hardened(1),
Slip10RawIndex.normal(0),
]);
// m/7564153'/42'/1'/0
expect(makeSimpleHdPath(42, 0)).toEqual([
Slip10RawIndex.hardened(7564153),
Slip10RawIndex.hardened(42),
Slip10RawIndex.hardened(1),
Slip10RawIndex.normal(0),
]);
// m/7564153'/42000000'/1'/0
expect(makeSimpleHdPath(42_000_000, 0)).toEqual([
Slip10RawIndex.hardened(7564153),
Slip10RawIndex.hardened(42_000_000),
Slip10RawIndex.hardened(1),
Slip10RawIndex.normal(0),
]);
});
});
});
17 changes: 17 additions & 0 deletions packages/proto-signing/src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,20 @@ export function makeCosmoshubPath(a: number): HdPath {
Slip10RawIndex.normal(a),
];
}

/**
* Creates a Cosmos path under the Cosmos purpose 7564153
* in the form `m/7564153'/chain_index'/*`.
*/
export function makeCosmosPath(chainIndex: number, ...components: Slip10RawIndex[]): HdPath {
const cosmosPurpose = 7564153;
return [Slip10RawIndex.hardened(cosmosPurpose), Slip10RawIndex.hardened(chainIndex), ...components];
}

/**
* Creates a Cosmos simple HD path in the form `m/7564153'/chain_index'/1'/a`
* with a 0-based account index `a`.
*/
export function makeSimpleHdPath(chainIndex: number, a: number): HdPath {
return makeCosmosPath(chainIndex, Slip10RawIndex.hardened(1), Slip10RawIndex.normal(a));
}