Skip to content

Commit

Permalink
refactor: Change getStxAddress to use network parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Oct 23, 2024
1 parent 7144c1b commit 7bb0092
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
8 changes: 4 additions & 4 deletions packages/transactions/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ utils.hmacSha256Sync = (key: Uint8Array, ...msgs: Uint8Array[]) => {
export function getAddressFromPrivateKey(
/** Private key bytes or hex string */
privateKey: PrivateKey,
network?: StacksNetworkName | StacksNetwork
network: StacksNetworkName | StacksNetwork = 'mainnet'
): string {
network = networkFrom(network ?? STACKS_MAINNET);
network = networkFrom(network);
const publicKey = privateKeyToPublic(privateKey);
return getAddressFromPublicKey(publicKey, network);
}
Expand All @@ -55,9 +55,9 @@ export function getAddressFromPrivateKey(
export function getAddressFromPublicKey(
/** Public key bytes or hex string */
publicKey: PublicKey,
network?: StacksNetworkName | StacksNetwork
network: StacksNetworkName | StacksNetwork = 'mainnet'
): string {
network = networkFrom(network ?? STACKS_MAINNET);
network = networkFrom(network);
publicKey = typeof publicKey === 'string' ? hexToBytes(publicKey) : publicKey;
const addrVer = addressHashModeToVersion(AddressHashMode.P2PKH, network);
const addr = addressFromVersionHash(addrVer, hashP2PKH(publicKey));
Expand Down
36 changes: 25 additions & 11 deletions packages/wallet-sdk/src/models/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import { HDKey } from '@scure/bip32';
import { makeAuthResponse as _makeAuthResponse } from '@stacks/auth';
import { bytesToHex, utf8ToBytes } from '@stacks/common';

import { FetchFn, createFetchFn } from '@stacks/common';
import {
getPublicKeyFromPrivate,
hashCode,
hashSha256Sync,
publicKeyToBtcAddress,
} from '@stacks/encryption';
import { createFetchFn, FetchFn } from '@stacks/common';
import {
NetworkParam,
StacksNetwork,
StacksNetworkName,
TransactionVersion,
} from '@stacks/network';
import { getAddressFromPrivateKey } from '@stacks/transactions';
import { connectToGaiaHubWithConfig, getHubInfo, makeGaiaAssociationToken } from '../utils';
import { Account, HARDENED_OFFSET } from './common';
Expand All @@ -20,20 +26,28 @@ import {
fetchProfileFromUrl,
signAndUploadProfile,
} from './profile';
import { STACKS_MAINNET, STACKS_TESTNET, TransactionVersion } from '@stacks/network';

export const getStxAddress = ({
export function getStxAddress(
account: Account,
network?: StacksNetworkName | StacksNetwork
): string;
export function getStxAddress({
account,
transactionVersion = TransactionVersion.Testnet,
network = 'mainnet',
}: {
account: Account;
transactionVersion?: TransactionVersion;
}): string => {
return getAddressFromPrivateKey(
account.stxPrivateKey,
transactionVersion == TransactionVersion.Mainnet ? STACKS_MAINNET : STACKS_TESTNET // todo: refactor for `next` wallet update
);
};
} & NetworkParam): string;
export function getStxAddress(
accountOrOptions: Account | ({ account: Account } & NetworkParam),
network: StacksNetworkName | StacksNetwork = 'mainnet'
): string {
if ('account' in accountOrOptions) {
const { account, network = 'mainnet' } = accountOrOptions;
return getAddressFromPrivateKey(account.stxPrivateKey, network);
}

return getAddressFromPrivateKey(accountOrOptions.stxPrivateKey, network);
}

/**
* Get the display name of an account.
Expand Down

0 comments on commit 7bb0092

Please sign in to comment.