Skip to content

Commit

Permalink
Use StacksNetworkName instead of union type (#1500)
Browse files Browse the repository at this point in the history
  • Loading branch information
friedger committed May 17, 2023
1 parent 534cd5c commit efd2255
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/network/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class StacksNetwork {
})
.then(nameInfo => {
// the returned address _should_ be in the correct network ---
// blockstackd gets into trouble because it tries to coerce back to mainnet
// stacks node gets into trouble because it tries to coerce back to mainnet
// and the regtest transaction generation libraries want to use testnet addresses
if (nameInfo.address) {
return Object.assign({}, nameInfo, { address: nameInfo.address });
Expand Down
10 changes: 8 additions & 2 deletions packages/stacking/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ export const BitcoinNetworkVersion = {
P2PKH: 0x6f, // 111
P2SH: 0xc4, // 196
},
regtest: {
devnet: {
// equivalent to testnet for our purposes
P2PKH: 0x6f, // 111
P2SH: 0xc4, // 196
},
mocknet: {
// equivalent to testnet for our purposes
P2PKH: 0x6f, // 111
P2SH: 0xc4, // 196
Expand Down Expand Up @@ -59,7 +64,8 @@ export const SEGWIT_V1_ADDR_PREFIX = /^(bc1p|tb1p|bcrt1p)/i;
export const SegwitPrefix = {
mainnet: 'bc',
testnet: 'tb',
regtest: 'bcrt',
devnet: 'bcrt',
mocknet: 'bcrt',
} as const;
/** @ignore */
export const SEGWIT_ADDR_PREFIXES = /^(bc|tb)/i;
Expand Down
13 changes: 7 additions & 6 deletions packages/stacking/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
SEGWIT_V1_ADDR_PREFIX,
StackingErrors,
} from './constants';
import { StacksNetworkName, StacksNetworks } from '@stacks/network';

export class InvalidAddressError extends Error {
innerError?: Error;
Expand Down Expand Up @@ -226,7 +227,7 @@ export function poxAddressToTuple(poxAddress: string) {

function legacyHashModeToBtcAddressVersion(
hashMode: PoXAddressVersion,
network: 'mainnet' | 'testnet' | 'regtest'
network: StacksNetworkName
): number {
switch (hashMode) {
case PoXAddressVersion.P2PKH:
Expand All @@ -244,9 +245,9 @@ function legacyHashModeToBtcAddressVersion(
function _poxAddressToBtcAddress_Values(
version: number,
hashBytes: Uint8Array,
network: 'mainnet' | 'testnet' | 'regtest'
network: StacksNetworkName
): string {
if (!['mainnet', 'testnet', 'regtest'].includes(network)) throw new Error('Invalid network.');
if (!StacksNetworks.includes(network)) throw new Error('Invalid network.');

switch (version) {
case PoXAddressVersion.P2PKH:
Expand All @@ -271,7 +272,7 @@ function _poxAddressToBtcAddress_Values(

function _poxAddressToBtcAddress_ClarityValue(
poxAddrClarityValue: ClarityValue,
network: 'mainnet' | 'testnet' | 'regtest'
network: StacksNetworkName
): string {
const poxAddr = extractPoxAddressFromClarityValue(poxAddrClarityValue);
return _poxAddressToBtcAddress_Values(poxAddr.version, poxAddr.hashBytes, network);
Expand All @@ -280,11 +281,11 @@ function _poxAddressToBtcAddress_ClarityValue(
export function poxAddressToBtcAddress(
version: number,
hashBytes: Uint8Array,
network: 'mainnet' | 'testnet' | 'regtest'
network: StacksNetworkName
): string;
export function poxAddressToBtcAddress(
poxAddrClarityValue: ClarityValue,
network: 'mainnet' | 'testnet' | 'regtest'
network: StacksNetworkName
): string;
export function poxAddressToBtcAddress(...args: any[]): string {
if (typeof args[0] === 'number') return _poxAddressToBtcAddress_Values(args[0], args[1], args[2]);
Expand Down

1 comment on commit efd2255

@vercel
Copy link

@vercel vercel bot commented on efd2255 May 17, 2023

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.