diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index f332891a8..0b6f49018 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -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'; @@ -92,7 +91,6 @@ import { gaiaAuth, gaiaConnect, gaiaUploadProfileAll, getGaiaAddressFromProfile import { JSONStringify, - getPrivateKeyAddress, canonicalPrivateKey, decodePrivateKey, makeProfileJWT, @@ -111,7 +109,7 @@ 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; @@ -119,14 +117,9 @@ 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 @@ -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'); diff --git a/packages/cli/src/common.ts b/packages/cli/src/common.ts new file mode 100644 index 000000000..c9ce72e04 --- /dev/null +++ b/packages/cli/src/common.ts @@ -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 { + return Promise.resolve().then(() => this.address); + } + + signTransaction(_txIn: bitcoinjs.TransactionBuilder, _signingIndex: number): Promise { + // 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; +} diff --git a/packages/cli/src/data.ts b/packages/cli/src/data.ts index 8cba31df5..d76e7ff2f 100644 --- a/packages/cli/src/data.ts +++ b/packages/cli/src/data.ts @@ -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'; diff --git a/packages/cli/src/keys.ts b/packages/cli/src/keys.ts index adfdc524c..8c6fbc1bb 100644 --- a/packages/cli/src/keys.ts +++ b/packages/cli/src/keys.ts @@ -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'; diff --git a/packages/cli/src/utils.ts b/packages/cli/src/utils.ts index 2c2400fdf..dd0f0e060 100644 --- a/packages/cli/src/utils.ts +++ b/packages/cli/src/utils.ts @@ -42,7 +42,7 @@ import { ID_ADDRESS_PATTERN, } from './argparse'; -import { TransactionSigner } from 'blockstack'; +import { CLITransactionSigner, isCLITransactionSigner } from './common'; import { decryptBackupPhrase } from './encrypt'; @@ -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 { - return Promise.resolve().then(() => this.address); - } - - signTransaction(_txIn: bitcoinjs.TransactionBuilder, _signingIndex: number): Promise { - // 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 { @@ -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; @@ -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)