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 25, 2024
1 parent 6d66e7a commit 9b9cd4f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 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
35 changes: 22 additions & 13 deletions packages/wallet-sdk/src/models/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ 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 } from '@stacks/network';
import { getAddressFromPrivateKey } from '@stacks/transactions';
import { connectToGaiaHubWithConfig, getHubInfo, makeGaiaAssociationToken } from '../utils';
import { Account, HARDENED_OFFSET } from './common';
Expand All @@ -20,20 +21,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);

Check warning on line 44 in packages/wallet-sdk/src/models/account.ts

View check run for this annotation

Codecov / codecov/patch

packages/wallet-sdk/src/models/account.ts#L44

Added line #L44 was not covered by tests
}

/**
* Get the display name of an account.
Expand Down Expand Up @@ -124,8 +133,8 @@ export const makeAuthResponse = async ({
{
...(profile || {}),
stxAddress: {
testnet: getStxAddress({ account, transactionVersion: TransactionVersion.Testnet }),
mainnet: getStxAddress({ account, transactionVersion: TransactionVersion.Mainnet }),
testnet: getStxAddress({ account, network: 'testnet' }),
mainnet: getStxAddress({ account, network: 'mainnet' }),
},
...additionalData,
},
Expand Down
1 change: 1 addition & 0 deletions packages/wallet-sdk/src/models/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface LockedWallet {
*
* This helps provide a better UX for users, so we can keep track of accounts they've
* created, and usernames they've used.
* @deprecated The usage of storing wallet related information on Gaia isn't widely used.
*/
export async function restoreWalletAccounts({
wallet,
Expand Down
18 changes: 7 additions & 11 deletions packages/wallet-sdk/tests/derive-keychain.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {
deriveWalletKeys,
DerivationType,
deriveAccount,
getStxAddress,
deriveLegacyConfigPrivateKey,
DerivationType,
selectStxDerivation,
deriveWalletKeys,
fetchUsernameForAccountByDerivationType,
getStxAddress,
selectStxDerivation,
} from '../src';
// https://github.com/paulmillr/scure-bip39
// Secure, audited & minimal implementation of BIP39 mnemonic phrases.
import { mnemonicToSeed } from '@scure/bip39';

import { HDKey } from '@scure/bip32';
import { STACKS_MAINNET } from '@stacks/network';
import fetchMock from 'jest-fetch-mock';
import { STACKS_MAINNET, TransactionVersion } from '@stacks/network';

const SECRET_KEY =
'sound idle panel often situate develop unit text design antenna ' +
Expand All @@ -33,9 +33,7 @@ test('keys are serialized, and can be deserialized properly using wallet private
salt: derived.salt,
stxDerivationType: DerivationType.Wallet,
});
expect(getStxAddress({ account, transactionVersion: TransactionVersion.Mainnet })).toEqual(
WALLET_ADDRESS
);
expect(getStxAddress({ account, network: 'mainnet' })).toEqual(WALLET_ADDRESS);
});

test('keys are serialized, and can be deserialized properly using data private key for stx', async () => {
Expand All @@ -49,9 +47,7 @@ test('keys are serialized, and can be deserialized properly using data private k
salt: derived.salt,
stxDerivationType: DerivationType.Data,
});
expect(getStxAddress({ account, transactionVersion: TransactionVersion.Mainnet })).toEqual(
DATA_ADDRESS
);
expect(getStxAddress({ account, network: 'mainnet' })).toEqual(DATA_ADDRESS);
});

test('backwards compatible legacy config private key derivation', async () => {
Expand Down
10 changes: 3 additions & 7 deletions packages/wallet-sdk/tests/derive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
// Secure, audited & minimal implementation of BIP39 mnemonic phrases.
import { HDKey } from '@scure/bip32';
import { mnemonicToSeed } from '@scure/bip39';
import { STACKS_MAINNET, TransactionVersion } from '@stacks/network';
import { STACKS_MAINNET } from '@stacks/network';
import fetchMock from 'jest-fetch-mock';

const SECRET_KEY =
Expand All @@ -32,9 +32,7 @@ test('keys are serialized, and can be deserialized properly using wallet private
salt: derived.salt,
stxDerivationType: DerivationType.Wallet,
});
expect(getStxAddress({ account, transactionVersion: TransactionVersion.Mainnet })).toEqual(
WALLET_ADDRESS
);
expect(getStxAddress({ account, network: 'mainnet' })).toEqual(WALLET_ADDRESS);
});

test('keys are serialized, and can be deserialized properly using data private key for stx', async () => {
Expand All @@ -48,9 +46,7 @@ test('keys are serialized, and can be deserialized properly using data private k
salt: derived.salt,
stxDerivationType: DerivationType.Data,
});
expect(getStxAddress({ account, transactionVersion: TransactionVersion.Mainnet })).toEqual(
DATA_ADDRESS
);
expect(getStxAddress({ account, network: 'mainnet' })).toEqual(DATA_ADDRESS);
});

test('backwards compatible legacy config private key derivation', async () => {
Expand Down
5 changes: 2 additions & 3 deletions packages/wallet-sdk/tests/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
getGaiaAddress,
getStxAddress,
} from '../src';
import { TransactionVersion } from '@stacks/network';

describe(generateSecretKey, () => {
test('generates a 24 word phrase by default', () => {
Expand Down Expand Up @@ -70,10 +69,10 @@ describe(generateWallet, () => {
'a29c3e73dba79ab0f84cb792bafd65ec71f243ebe67a7ebd842ef5cdce3b21eb'
);

expect(getStxAddress({ account, transactionVersion: TransactionVersion.Testnet })).toEqual(
expect(getStxAddress({ account, network: 'testnet' })).toEqual(
'ST384CVPNDTYA0E92TKJZQTYXQHNZSWGCAH0ER64E'
);
expect(getStxAddress({ account, transactionVersion: TransactionVersion.Mainnet })).toEqual(
expect(getStxAddress({ account, network: 'mainnet' })).toEqual(
'SP384CVPNDTYA0E92TKJZQTYXQHNZSWGCAG7SAPVB'
);

Expand Down

0 comments on commit 9b9cd4f

Please sign in to comment.