Skip to content

Commit

Permalink
refactor: add addressVersion to network object
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed May 30, 2024
1 parent 3d8c314 commit bb79c82
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 60 deletions.
1 change: 1 addition & 0 deletions .github/MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { STACKS_DEVNET } from '@stacks/network';
#### Impacts

- @stacks/bns: `BnsContractAddress` was removed, since `.bootAddress` is now a part of the network objects.
- @stacks/transactions: `AddressVersion` was moved to `@stacks/network`.

### StacksNodeApi

Expand Down
17 changes: 5 additions & 12 deletions packages/bns/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ApiParam, IntegerType, utf8ToBytes } from '@stacks/common';
import { ChainId, StacksNetwork } from '@stacks/network';
import { StacksNetwork } from '@stacks/network';
import {
AddressVersion,
ClarityType,
ClarityValue,
FungibleConditionCode,
Expand Down Expand Up @@ -33,12 +32,6 @@ import { decodeFQN, getZonefileHash } from './utils';

export const BNS_CONTRACT_NAME = 'bns';

function getAddressVersion(network: StacksNetwork) {
return network.chainId === ChainId.Mainnet
? AddressVersion.MainnetSingleSig
: AddressVersion.TestnetSingleSig;
}

export interface PriceFunction {
base: IntegerType;
coefficient: IntegerType;
Expand Down Expand Up @@ -298,7 +291,7 @@ export async function buildPreorderNamespaceTx({
const hashedSaltedNamespace = hash160(saltedNamespaceBytes);

const burnSTXPostCondition = createSTXPostCondition(
publicKeyToAddress(getAddressVersion(network), publicKey),
publicKeyToAddress(network.addressVersion.singleSig, publicKey),
FungibleConditionCode.Equal,
stxToBurn
);
Expand Down Expand Up @@ -521,7 +514,7 @@ export async function buildPreorderNameTx({
const hashedSaltedName = hash160(saltedNamesBytes);

const burnSTXPostCondition = createSTXPostCondition(
publicKeyToAddress(getAddressVersion(network), publicKey),
publicKeyToAddress(network.addressVersion.singleSig, publicKey),
FungibleConditionCode.Equal,
stxToBurn
);
Expand Down Expand Up @@ -690,7 +683,7 @@ export async function buildTransferNameTx({
zonefile ? someCV(bufferCV(getZonefileHash(zonefile))) : noneCV(),
];
const postConditionSender = createNonFungiblePostCondition(
publicKeyToAddress(getAddressVersion(network), publicKey),
publicKeyToAddress(network.addressVersion.singleSig, publicKey),
NonFungibleConditionCode.Sends,
parseAssetString(`${network.bootAddress}.bns::names`),
tupleCV({
Expand Down Expand Up @@ -810,7 +803,7 @@ export async function buildRenewNameTx({
zonefile ? someCV(bufferCV(getZonefileHash(zonefile))) : noneCV(),
];
const burnSTXPostCondition = createSTXPostCondition(
publicKeyToAddress(getAddressVersion(network), publicKey),
publicKeyToAddress(network.addressVersion.singleSig, publicKey),
FungibleConditionCode.Equal,
stxToBurn
);
Expand Down
21 changes: 7 additions & 14 deletions packages/bns/tests/bns.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { utf8ToBytes } from '@stacks/common';
import { ChainId, STACKS_TESTNET, StacksNetwork } from '@stacks/network';
import { STACKS_TESTNET } from '@stacks/network';
import {
AddressVersion,
FungibleConditionCode,
NonFungibleConditionCode,
bufferCV,
Expand Down Expand Up @@ -30,12 +29,6 @@ beforeEach(() => {
jest.resetModules();
});

function getAddressVersion(network: StacksNetwork) {
return network.chainId === ChainId.Mainnet
? AddressVersion.MainnetSingleSig
: AddressVersion.TestnetSingleSig;
}

test('canRegisterName true', async () => {
const fullyQualifiedName = 'test.id';

Expand Down Expand Up @@ -325,7 +318,7 @@ test('preorderNamespace', async () => {

const bnsFunctionName = 'namespace-preorder';
const burnSTXPostCondition = createSTXPostCondition(
publicKeyToAddress(getAddressVersion(network), publicKey),
publicKeyToAddress(network.addressVersion.singleSig, publicKey),
FungibleConditionCode.Equal,
stxToBurn
);
Expand Down Expand Up @@ -545,7 +538,7 @@ test('preorderName', async () => {

const bnsFunctionName = 'name-preorder';
const burnSTXPostCondition = createSTXPostCondition(
publicKeyToAddress(getAddressVersion(network), publicKey),
publicKeyToAddress(network.addressVersion.singleSig, publicKey),
FungibleConditionCode.Equal,
stxToBurn
);
Expand Down Expand Up @@ -688,7 +681,7 @@ test('transferName', async () => {

const { namespace, name } = decodeFQN(fullyQualifiedName);
const nameTransferPostConditionOne = createNonFungiblePostCondition(
publicKeyToAddress(getAddressVersion(network), publicKey),
publicKeyToAddress(network.addressVersion.singleSig, publicKey),
NonFungibleConditionCode.Sends,
parseAssetString(`${network.bootAddress}.bns::names`),
tupleCV({
Expand Down Expand Up @@ -755,7 +748,7 @@ test('transferName optionalArguments', async () => {

const { namespace, name } = decodeFQN(fullyQualifiedName);
const nameTransferPostConditionOne = createNonFungiblePostCondition(
publicKeyToAddress(getAddressVersion(network), publicKey),
publicKeyToAddress(network.addressVersion.singleSig, publicKey),
NonFungibleConditionCode.Sends,
parseAssetString(`${network.bootAddress}.bns::names`),
tupleCV({
Expand Down Expand Up @@ -860,7 +853,7 @@ test('renewName', async () => {

const { namespace, name } = decodeFQN(fullyQualifiedName);
const burnSTXPostCondition = createSTXPostCondition(
publicKeyToAddress(getAddressVersion(network), publicKey),
publicKeyToAddress(network.addressVersion.singleSig, publicKey),
FungibleConditionCode.Equal,
stxToBurn
);
Expand Down Expand Up @@ -915,7 +908,7 @@ test('renewName optionalArguments', async () => {

const { namespace, name } = decodeFQN(fullyQualifiedName);
const burnSTXPostCondition = createSTXPostCondition(
publicKeyToAddress(getAddressVersion(network), publicKey),
publicKeyToAddress(network.addressVersion.singleSig, publicKey),
FungibleConditionCode.Equal,
stxToBurn
);
Expand Down
17 changes: 17 additions & 0 deletions packages/network/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ export enum TransactionVersion {
Testnet = 0x80,
}

/**
* Address versions for identifying address types in an encoded Stacks address.
* The address version is a single byte, indicating the address type.
* Every Stacks address starts with `S` followed by a single character indicating the address version.
* The second character is the c32-encoded AddressVersion byte.
*/
export enum AddressVersion {
/** `P` — A single-sig address for mainnet (starting with `SP`) */
MainnetSingleSig = 22,
/** `M` — A multi-sig address for mainnet (starting with `SM`) */
MainnetMultiSig = 20,
/** `T` — A single-sig address for testnet (starting with `ST`) */
TestnetSingleSig = 26,
/** `N` — A multi-sig address for testnet (starting with `SN`) */
TestnetMultiSig = 21,
}

export const DEFAULT_TRANSACTION_VERSION = TransactionVersion.Mainnet;

/** @ignore */
Expand Down
14 changes: 13 additions & 1 deletion packages/network/src/network.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { DEVNET_URL, HIRO_MAINNET_URL, HIRO_TESTNET_URL } from '@stacks/common';
import { ChainId, PeerNetworkId, TransactionVersion } from './constants';
import { AddressVersion, ChainId, PeerNetworkId, TransactionVersion } from './constants';

export interface StacksNetwork {
chainId: number;
transactionVersion: number; // todo: txVersion better?
peerNetworkId: number;
magicBytes: string;
bootAddress: string;
addressVersion: {
singleSig: number;
multiSig: number;
};
// todo: add check32 character bytes string
}

Expand All @@ -16,6 +20,10 @@ export const STACKS_MAINNET: StacksNetwork = {
peerNetworkId: PeerNetworkId.Mainnet,
magicBytes: 'X2', // todo: comment bytes version of magic bytes
bootAddress: 'SP000000000000000000002Q6VF78',
addressVersion: {
singleSig: AddressVersion.MainnetSingleSig,
multiSig: AddressVersion.MainnetMultiSig,
},
};

export const STACKS_TESTNET: StacksNetwork = {
Expand All @@ -24,6 +32,10 @@ export const STACKS_TESTNET: StacksNetwork = {
peerNetworkId: PeerNetworkId.Testnet,
magicBytes: 'T2', // todo: comment bytes version of magic bytes
bootAddress: 'ST000000000000000000002AMW42H',
addressVersion: {
singleSig: AddressVersion.TestnetSingleSig,
multiSig: AddressVersion.TestnetMultiSig,
},
};

export const STACKS_DEVNET: StacksNetwork = {
Expand Down
22 changes: 5 additions & 17 deletions packages/transactions/src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
import { ClarityValue, PrincipalCV } from './clarity';
import {
AddressHashMode,
AddressVersion,
AnchorMode,
ClarityVersion,
FungibleConditionCode,
Expand Down Expand Up @@ -149,10 +148,7 @@ export async function makeUnsignedSTXTokenTransfer(
}

if (txOptions.nonce == null) {
const addressVersion =
options.network.transactionVersion === TransactionVersion.Mainnet
? AddressVersion.MainnetSingleSig
: AddressVersion.TestnetSingleSig;
const addressVersion = network.addressVersion.singleSig;
const address = c32address(addressVersion, transaction.auth.spendingCondition!.signer);
const txNonce = await getNonce({ address, api: options.api });
transaction.setNonce(txNonce);
Expand Down Expand Up @@ -349,10 +345,7 @@ export async function makeUnsignedContractDeploy(
}

if (txOptions.nonce === undefined || txOptions.nonce === null) {
const addressVersion =
options.network.transactionVersion === TransactionVersion.Mainnet
? AddressVersion.MainnetSingleSig
: AddressVersion.TestnetSingleSig;
const addressVersion = network.addressVersion.singleSig;
const address = c32address(addressVersion, transaction.auth.spendingCondition!.signer);
const txNonce = await getNonce({ address, api: options.api });
transaction.setNonce(txNonce);
Expand Down Expand Up @@ -482,10 +475,7 @@ export async function makeUnsignedContractCall(
}

if (txOptions.nonce === undefined || txOptions.nonce === null) {
const addressVersion =
network.transactionVersion === TransactionVersion.Mainnet
? AddressVersion.MainnetSingleSig
: AddressVersion.TestnetSingleSig;
const addressVersion = network.addressVersion.singleSig;
const address = c32address(addressVersion, transaction.auth.spendingCondition!.signer);
const txNonce = await getNonce({ address, api: options.api });
transaction.setNonce(txNonce);
Expand Down Expand Up @@ -729,6 +719,7 @@ export async function sponsorTransaction(
const options = Object.assign(defaultOptions, sponsorOptions);
options.api = defaultApiFromNetwork(options.network, sponsorOptions.api);

const network = networkFrom(options.network);
const sponsorPubKey = privateKeyToPublic(options.sponsorPrivateKey);

if (sponsorOptions.fee == null) {
Expand All @@ -752,10 +743,7 @@ export async function sponsorTransaction(
}

if (sponsorOptions.sponsorNonce == null) {
const addressVersion = whenTransactionVersion(options.transaction.version)({
[TransactionVersion.Mainnet]: AddressVersion.MainnetSingleSig,
[TransactionVersion.Testnet]: AddressVersion.TestnetSingleSig,
}); // detect address version from transaction version
const addressVersion = network.addressVersion.singleSig;
const address = publicKeyToAddress(addressVersion, sponsorPubKey);
const sponsorNonce = await getNonce({ address, api: options.api });
options.sponsorNonce = sponsorNonce;
Expand Down
18 changes: 2 additions & 16 deletions packages/transactions/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,8 @@ export enum AddressHashMode {
export type SingleSigHashMode = AddressHashMode.SerializeP2PKH | AddressHashMode.SerializeP2WPKH;
export type MultiSigHashMode = AddressHashMode.SerializeP2SH | AddressHashMode.SerializeP2WSH;

/**
* Address versions for identifying address types in an encoded Stacks address.
* The address version is a single byte, indicating the address type.
* Every Stacks address starts with `S` followed by a single character indicating the address version.
* The second character is the c32-encoded AddressVersion byte.
*/
export enum AddressVersion {
/** `P` — A single-sig address for mainnet (starting with `SP`) */
MainnetSingleSig = 22,
/** `M` — A multi-sig address for mainnet (starting with `SM`) */
MainnetMultiSig = 20,
/** `T` — A single-sig address for testnet (starting with `ST`) */
TestnetSingleSig = 26,
/** `N` — A multi-sig address for testnet (starting with `SN`) */
TestnetMultiSig = 21,
}
// re-export for backwards compatibility
export { AddressVersion } from '@stacks/network';

// todo: try to remove this
export enum PubKeyEncoding {
Expand Down

0 comments on commit bb79c82

Please sign in to comment.