Skip to content

Commit

Permalink
fix: remove circular dependencies from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsan-javaid authored and reedrosenbluth committed Nov 19, 2021
1 parent e9e626d commit 4a29e42
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 63 deletions.
15 changes: 5 additions & 10 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ import {
DEFAULT_CONFIG_TESTNET_PATH,
ID_ADDRESS_PATTERN,
STACKS_ADDRESS_PATTERN,
DEFAULT_MAX_ID_SEARCH_INDEX,
} from './argparse';

import { encryptBackupPhrase, decryptBackupPhrase } from './encrypt';
Expand All @@ -92,7 +91,6 @@ import { gaiaAuth, gaiaConnect, gaiaUploadProfileAll, getGaiaAddressFromProfile

import {
JSONStringify,
getPrivateKeyAddress,
canonicalPrivateKey,
decodePrivateKey,
makeProfileJWT,
Expand All @@ -111,22 +109,17 @@ import {

import { handleAuth, handleSignIn } from './auth';
import { generateNewAccount, generateWallet, getAppPrivateKey } from '@stacks/wallet-sdk';

import { getMaxIDSearchIndex, setMaxIDSearchIndex, getPrivateKeyAddress } from './common';
// global CLI options
let txOnly = false;
let estimateOnly = false;
let safetyChecks = true;
let receiveFeesPeriod = 52595;
let gracePeriod = 5000;
let noExit = false;
let maxIDSearchIndex = DEFAULT_MAX_ID_SEARCH_INDEX;

let BLOCKSTACK_TEST = !!process.env.BLOCKSTACK_TEST;

export function getMaxIDSearchIndex() {
return maxIDSearchIndex;
}

/*
* Sign a profile.
* @path (string) path to the profile
Expand Down Expand Up @@ -1803,8 +1796,10 @@ export function CLIMain() {
safetyChecks = !CLIOptAsBool(opts, 'U');
receiveFeesPeriod = opts['N'] ? parseInt(CLIOptAsString(opts, 'N')!) : receiveFeesPeriod;
gracePeriod = opts['G'] ? parseInt(CLIOptAsString(opts, 'N')!) : gracePeriod;
maxIDSearchIndex = opts['M'] ? parseInt(CLIOptAsString(opts, 'M')!) : maxIDSearchIndex;

const maxIDSearchIndex = opts['M']
? parseInt(CLIOptAsString(opts, 'M')!)
: getMaxIDSearchIndex();
setMaxIDSearchIndex(maxIDSearchIndex);
const debug = CLIOptAsBool(opts, 'd');
const consensusHash = CLIOptAsString(opts, 'C');
const integration_test = CLIOptAsBool(opts, 'i');
Expand Down
62 changes: 62 additions & 0 deletions packages/cli/src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { DEFAULT_MAX_ID_SEARCH_INDEX } from './argparse';
import { CLINetworkAdapter } from './network';
import * as blockstack from 'blockstack';
import { TransactionSigner } from 'blockstack';
import * as bitcoinjs from 'bitcoinjs-lib';

let maxIDSearchIndex = DEFAULT_MAX_ID_SEARCH_INDEX;

export function getMaxIDSearchIndex() {
return maxIDSearchIndex;
}

export function setMaxIDSearchIndex(index: number) {
maxIDSearchIndex = index;
}

export class CLITransactionSigner implements TransactionSigner {
address: string;
isComplete: boolean;

constructor(address = '') {
this.address = address;
this.isComplete = false;
}

getAddress(): Promise<string> {
return Promise.resolve().then(() => this.address);
}

signTransaction(_txIn: bitcoinjs.TransactionBuilder, _signingIndex: number): Promise<void> {
// eslint-disable-next-line @typescript-eslint/no-empty-function
return Promise.resolve().then(() => {});
}

signerVersion(): number {
return 0;
}
}

/*
* Get a private key's address. Honor the 01 to compress the public key
* @privateKey (string) the hex-encoded private key
*/
export function getPrivateKeyAddress(
network: CLINetworkAdapter,
privateKey: string | CLITransactionSigner
): string {
if (isCLITransactionSigner(privateKey)) {
const pkts = privateKey;
return pkts.address;
} else {
const pk = privateKey;
const ecKeyPair = blockstack.hexStringToECPair(pk);
return network.coerceAddress(blockstack.ecPairToAddress(ecKeyPair));
}
}

export function isCLITransactionSigner(
signer: string | CLITransactionSigner
): signer is CLITransactionSigner {
return (signer as CLITransactionSigner).signerVersion !== undefined;
}
3 changes: 2 additions & 1 deletion packages/cli/src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import * as jsontokens from 'jsontokens';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const ZoneFile = require('zone-file');

import { canonicalPrivateKey, getPrivateKeyAddress, getPublicKeyFromPrivateKey } from './utils';
import { canonicalPrivateKey, getPublicKeyFromPrivateKey } from './utils';
import { getPrivateKeyAddress } from './common';

import { CLINetworkAdapter, NameInfoType } from './network';

Expand Down
5 changes: 1 addition & 4 deletions packages/cli/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import * as bip39 from 'bip39';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const c32check = require('c32check');

import { getPrivateKeyAddress } from './utils';

import { getMaxIDSearchIndex } from './cli';

import { getPrivateKeyAddress, getMaxIDSearchIndex } from './common';
import { CLINetworkAdapter } from './network';

import * as bip32 from 'bip32';
Expand Down
49 changes: 1 addition & 48 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
ID_ADDRESS_PATTERN,
} from './argparse';

import { TransactionSigner } from 'blockstack';
import { CLITransactionSigner, isCLITransactionSigner } from './common';

import { decryptBackupPhrase } from './encrypt';

Expand All @@ -57,29 +57,6 @@ export interface UTXO {
tx_output_n: number;
}

class CLITransactionSigner implements TransactionSigner {
address: string;
isComplete: boolean;

constructor(address = '') {
this.address = address;
this.isComplete = false;
}

getAddress(): Promise<string> {
return Promise.resolve().then(() => this.address);
}

signTransaction(_txIn: bitcoinjs.TransactionBuilder, _signingIndex: number): Promise<void> {
// eslint-disable-next-line @typescript-eslint/no-empty-function
return Promise.resolve().then(() => {});
}

signerVersion(): number {
return 0;
}
}

export class NullSigner extends CLITransactionSigner {}

export class MultiSigKeySigner extends CLITransactionSigner {
Expand Down Expand Up @@ -213,12 +190,6 @@ export class SegwitP2SHKeySigner extends CLITransactionSigner {
}
}

function isCLITransactionSigner(
signer: string | CLITransactionSigner
): signer is CLITransactionSigner {
return (signer as CLITransactionSigner).signerVersion !== undefined;
}

export function hasKeys(signer: string | CLITransactionSigner): boolean {
if (isCLITransactionSigner(signer)) {
const s = signer;
Expand Down Expand Up @@ -394,24 +365,6 @@ export function getPublicKeyFromPrivateKey(privateKey: string): string {
return ecKeyPair.publicKey.toString('hex');
}

/*
* Get a private key's address. Honor the 01 to compress the public key
* @privateKey (string) the hex-encoded private key
*/
export function getPrivateKeyAddress(
network: CLINetworkAdapter,
privateKey: string | CLITransactionSigner
): string {
if (isCLITransactionSigner(privateKey)) {
const pkts = privateKey;
return pkts.address;
} else {
const pk = privateKey;
const ecKeyPair = blockstack.hexStringToECPair(pk);
return network.coerceAddress(blockstack.ecPairToAddress(ecKeyPair));
}
}

/*
* Get the canonical form of a hex-encoded private key
* (i.e. strip the trailing '01' if present)
Expand Down

1 comment on commit 4a29e42

@vercel
Copy link

@vercel vercel bot commented on 4a29e42 Nov 19, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.