mirror of
https://github.com/alexgo-io/stacks.js.git
synced 2026-01-12 17:52:41 +08:00
fix: remove circular dependencies from cli
This commit is contained in:
committed by
Reed Rosenbluth
parent
e9e626d8b3
commit
4a29e42cea
@@ -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');
|
||||
|
||||
62
packages/cli/src/common.ts
Normal file
62
packages/cli/src/common.ts
Normal file
@@ -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;
|
||||
}
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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<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 {
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user