Skip to content

Commit

Permalink
feat(cli): add WIF format to make_keychain
Browse files Browse the repository at this point in the history
closes #938
  • Loading branch information
marcosc90 committed Apr 6, 2021
1 parent 2bec5d4 commit 73fea0d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/cli/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
coverageDirectory: './coverage/',
collectCoverage: true,
globals: {
'ts-jest': {
diagnostics: {
ignoreCodes: ['TS151001'],
},
},
},
moduleFileExtensions: ['js', 'ts', 'd.ts'],
setupFilesAfterEnv: ['./tests/setup.js'],
testTimeout: 10000,
};
3 changes: 3 additions & 0 deletions packages/cli/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type StacksKeyInfoType = {
privateKey: string;
address: string;
btcAddress: string;
wif: string;
index: number;
};

Expand Down Expand Up @@ -139,6 +140,7 @@ export async function getStacksWalletKeyInfo(
const child = master.derivePath("m/44'/5757'/0'/0/0"); // taken from stacks-wallet. See https://github.com/blockstack/stacks-wallet
const ecPair = bitcoin.ECPair.fromPrivateKey(child.privateKey!);
const privkey = blockstack.ecPairToHexString(ecPair);
const wif = child.toWIF();

const addr = getPrivateKeyAddress(network, privkey);
let btcAddress: string;
Expand All @@ -160,6 +162,7 @@ export async function getStacksWalletKeyInfo(
privateKey: privkey,
address: c32check.b58ToC32(addr),
btcAddress,
wif,
index: 0,
};
return result;
Expand Down
23 changes: 23 additions & 0 deletions packages/cli/tests/keys.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getStacksWalletKeyInfo } from '../src/keys';
import { getNetwork, CLINetworkAdapter, CLI_NETWORK_OPTS } from '../src/network';
import { loadConfig, DEFAULT_CONFIG_PATH } from '../src/argparse';

// setup
const networkType = 'mainnet';
const configData = loadConfig(DEFAULT_CONFIG_PATH, networkType);
const wrappedNetwork = getNetwork(configData, false);
const blockstackNetwork = new CLINetworkAdapter(wrappedNetwork, {} as CLI_NETWORK_OPTS);

test('getStacksWalletKeyInfo', async () => {
const mnemonic =
'injury oxygen river foot pelican divert proud venture divert tired tomato middle frozen task news bullet love mimic expect share sunset equip absent hub';
const info = await getStacksWalletKeyInfo(blockstackNetwork, mnemonic);

expect(info).toEqual({
privateKey: 'c6776f323c7f50100777472079fd754b706bacdbb8932a6485fb2040ec6df18801',
address: 'SP2QJ66HRQK5A9FMVPJPW72Z0AWRHMNXZ4PQXWZ1M',
btcAddress: '1Gy3JXjEWJNejnfVVeznbZwcd4iDCk9TB2',
wif: 'L3sWBNt3Ft3XV1E587CgqWgTDHbFtxcWQ1W8eke8SeY1i1SK942d',
index: 0,
});
});

0 comments on commit 73fea0d

Please sign in to comment.