diff --git a/packages/network/src/network.ts b/packages/network/src/network.ts index e236338d1..03f298990 100644 --- a/packages/network/src/network.ts +++ b/packages/network/src/network.ts @@ -64,11 +64,13 @@ export function networkFromName(name: StacksNetworkName) { } } +/** @ignore */ export function networkFrom(network: StacksNetworkName | StacksNetwork) { if (typeof network === 'string') return networkFromName(network); return network; } +/** @ignore */ export function deriveDefaultUrl(network?: StacksNetwork | StacksNetworkName) { if (!network) return HIRO_MAINNET_URL; // default to mainnet if no network is given diff --git a/packages/stacking/src/index.ts b/packages/stacking/src/index.ts index d0e17f1b3..cb0696a21 100644 --- a/packages/stacking/src/index.ts +++ b/packages/stacking/src/index.ts @@ -13,7 +13,7 @@ import { intToBigInt, isInstance, } from '@stacks/common'; -import { StacksNetwork } from '@stacks/network'; +import { StacksNetwork, StacksNetworkName, networkFrom } from '@stacks/network'; import { BurnchainRewardListResponse, BurnchainRewardSlotHolderListResponse, @@ -341,9 +341,14 @@ export class StackingClient { public api: StacksNodeApi; - constructor(opts: { address: string; network: StacksNetwork; api?: StacksNodeApi | ApiOpts }) { + // todo: make more constructor opts optional + constructor(opts: { + address: string; + network: StacksNetworkName | StacksNetwork; + api?: StacksNodeApi | ApiOpts; + }) { this.address = opts.address; - this.network = opts.network; + this.network = networkFrom(opts.network); this.api = isInstance(opts.api, StacksNodeApi) ? opts.api : new StacksNodeApi({ url: opts.api?.url, fetch: opts.api?.fetch, network: opts.network }); diff --git a/packages/stacking/src/utils.ts b/packages/stacking/src/utils.ts index 3a643f810..9a20d6316 100644 --- a/packages/stacking/src/utils.ts +++ b/packages/stacking/src/utils.ts @@ -6,7 +6,7 @@ import { base58CheckEncode, verifyMessageSignatureRsv, } from '@stacks/encryption'; -import { StacksNetwork, StacksNetworkName, StacksNetworks } from '@stacks/network'; +import { StacksNetwork, StacksNetworkName, StacksNetworks, networkFrom } from '@stacks/network'; import { BufferCV, ClarityType, @@ -317,6 +317,7 @@ export function poxAddressToBtcAddress( network: StacksNetworkName ): string; export function poxAddressToBtcAddress(...args: any[]): string { + // todo: allow these helpers to take a bitcoin network instead of a stacks network, once we have a concept of bitcoin networks in the codebase if (typeof args[0] === 'number') return _poxAddressToBtcAddress_Values(args[0], args[1], args[2]); return _poxAddressToBtcAddress_ClarityValue(args[0], args[1]); } @@ -411,7 +412,7 @@ export interface Pox4SignatureOptions { rewardCycle: number; /** lock period (in cycles) */ period: number; - network: StacksNetwork; + network: StacksNetworkName | StacksNetwork; /** Maximum amount of uSTX that can be locked during this function call */ maxAmount: IntegerType; /** Random integer to prevent signature re-use */ @@ -472,10 +473,11 @@ export function pox4SignatureMessage({ poxAddress, rewardCycle, period: lockPeriod, - network, + network: networkOrName, maxAmount, authId, }: Pox4SignatureOptions) { + const network = networkFrom(networkOrName); const message = tupleCV({ 'pox-addr': poxAddressToTuple(poxAddress), 'reward-cycle': uintCV(rewardCycle), diff --git a/packages/wallet-sdk/src/derive.ts b/packages/wallet-sdk/src/derive.ts index ceb044781..b2a597837 100644 --- a/packages/wallet-sdk/src/derive.ts +++ b/packages/wallet-sdk/src/derive.ts @@ -3,7 +3,13 @@ import { HDKey } from '@scure/bip32'; import { ApiParam, bytesToHex, defaultApiLike, utf8ToBytes } from '@stacks/common'; import { compressPrivateKey, createSha2Hash } from '@stacks/encryption'; -import { STACKS_MAINNET, StacksNetwork, deriveDefaultUrl } from '@stacks/network'; +import { + STACKS_MAINNET, + StacksNetwork, + StacksNetworkName, + deriveDefaultUrl, + networkFrom, +} from '@stacks/network'; import { getAddressFromPrivateKey } from '@stacks/transactions'; import { Account, HARDENED_OFFSET, WalletKeys } from './models/common'; import { fetchFirstName } from './usernames'; @@ -141,15 +147,15 @@ const selectDerivationTypeForUsername = async ({ }): Promise => { if (network) { const nameInfo = await getNameInfo({ name: username }); - let stxPrivateKey = deriveStxPrivateKey({ rootNode, index }); + const stxPrivateKey = deriveStxPrivateKey({ rootNode, index }); let derivedAddress = getAddressFromPrivateKey(stxPrivateKey); if (derivedAddress !== nameInfo.address) { // try data private key - stxPrivateKey = deriveDataPrivateKey({ + const dataPrivateKey = deriveDataPrivateKey({ rootNode, index, }); - derivedAddress = getAddressFromPrivateKey(stxPrivateKey); + derivedAddress = getAddressFromPrivateKey(dataPrivateKey); if (derivedAddress !== nameInfo.address) { return DerivationType.Unknown; } else { @@ -200,7 +206,7 @@ export const fetchUsernameForAccountByDerivationType = async ( rootNode: HDKey; index: number; derivationType: DerivationType.Wallet | DerivationType.Data; - network?: StacksNetwork; + network?: StacksNetworkName | StacksNetwork; } & ApiParam ): Promise<{ username: string | undefined; @@ -208,7 +214,7 @@ export const fetchUsernameForAccountByDerivationType = async ( const api = defaultApiLike({ ...{ url: deriveDefaultUrl(opts.network) }, ...opts.api }); // try to find existing usernames owned by given derivation path - const selectedNetwork = opts.network ?? STACKS_MAINNET; + const selectedNetwork = opts.network ? networkFrom(opts.network) : STACKS_MAINNET; const privateKey = derivePrivateKeyByType(opts); const address = getAddressFromPrivateKey(privateKey, selectedNetwork.transactionVersion); const username = await fetchFirstName({ address, api });