Skip to content

Commit

Permalink
refactor: Minor refactoring to match common theme
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Oct 22, 2024
1 parent 8cf2e30 commit e78cc74
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 49 deletions.
1 change: 1 addition & 0 deletions .github/MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ This likely was a misunderstood and unused feature.
- `makeRandomPrivKey` was renamed to `randomPrivateKey` and now returns a compressed private key.
- `generateSecretKey` was renamed to `randomSeedPhrase`.
- `nextYear`, `nextMonth`, `nextHour`, `makeUUID4`, `updateQueryStringParameter`, `getAesCbcOutputLength`, `getAPIUsageErrorMessage`, `isSameOriginAbsoluteUrl`, `isLaterVersion`, `getBase64OutputLength`, were marked as deprecated.
- `encrypt` and `decrypt` in `@stacks/wallet-sdk` (aliases of `encryptMnemonic` and `decryptMnemonic` in the `@stacks/encryption` package respectively) were removed.

## Stacks.js (<=4.x.x) → (5.x.x)

Expand Down
2 changes: 2 additions & 0 deletions packages/bns/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { utf8ToBytes } from '@stacks/common';
import { hash160 } from '@stacks/transactions';

/** @ignore */
export function decodeFQN(fqdn: string): {
name: string;
namespace: string;
Expand All @@ -20,4 +21,5 @@ export function decodeFQN(fqdn: string): {
};
}

/** @ignore */
export const getZonefileHash = (zonefile: string) => hash160(utf8ToBytes(zonefile));
2 changes: 1 addition & 1 deletion packages/encryption/src/cryptoRandom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import { utils } from '@noble/secp256k1';
*/
export const randomBytes = (bytesLength: number = 32): Uint8Array => utils.randomBytes(bytesLength);

/** Optional function to generate cryptographically secure random bytes */
/** @deprecated @ignore */
export type GetRandomBytes = (count: number) => Uint8Array;
4 changes: 2 additions & 2 deletions packages/encryption/src/ec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function isValidPublicKey(pub: string): {
/**
* Hex encodes a 32-byte bigint instance.
* The result string is zero padded and always 64 characters in length.
* @ignore @internal
* @ignore @internal @deprecated
*/
export function getHexFromBN(bnInput: bigint): string {
const hexOut = bnInput.toString(16);
Expand All @@ -221,7 +221,7 @@ export function getHexFromBN(bnInput: bigint): string {

/**
* Converts to zero padded 32 bytes
* @ignore
* @ignore @deprecated
*/
export function getBytesFromBN(bnInput: bigint): Uint8Array {
// todo: remove method?
Expand Down
1 change: 1 addition & 0 deletions packages/encryption/src/messageSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function hashMessage(message: string, prefix: string = chainPrefix): Uint
}

export function encodeMessage(
/** UTF-8 string or Uint8Array (bytes) */
message: string | Uint8Array,
prefix: string = chainPrefix
): Uint8Array {
Expand Down
3 changes: 3 additions & 0 deletions packages/encryption/src/varuint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function ensureUInt53(n: number) {
if (n < 0 || n > MAX_SAFE_INTEGER || n % 1 !== 0) throw new RangeError('value out of range');
}

/** @ignore */
export function encode(number: number, bytes?: Uint8Array, offset: number = 0) {
ensureUInt53(number);
if (!bytes) bytes = new Uint8Array(encodingLength(number));
Expand Down Expand Up @@ -57,6 +58,7 @@ export function encode(number: number, bytes?: Uint8Array, offset: number = 0) {
return bytes;
}

/** @ignore */
export function decode(bytes: Uint8Array, offset: number = 0) {
const first = readUInt8(bytes, offset);

Expand All @@ -83,6 +85,7 @@ export function decode(bytes: Uint8Array, offset: number = 0) {
}
}

/** @ignore */
export function encodingLength(number: number) {
ensureUInt53(number);

Expand Down
29 changes: 21 additions & 8 deletions packages/stacking/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sha256 } from '@noble/hashes/sha256';
import { bech32, bech32m } from '@scure/base';
import { IntegerType, PrivateKey, bigIntToBytes, hexToBytes } from '@stacks/common';
import { IntegerType, PrivateKey, bigIntToBytes, bytesToHex, hexToBytes } from '@stacks/common';
import {
base58CheckDecode,
base58CheckEncode,
Expand Down Expand Up @@ -79,6 +79,7 @@ function nativeAddressToSegwitVersion(
);
}

/** @ignore */
function bech32Decode(btcAddress: string) {
const { words: bech32Words } = bech32.decode(btcAddress);
const witnessVersion = bech32Words[0];
Expand All @@ -92,6 +93,7 @@ function bech32Decode(btcAddress: string) {
};
}

/** @ignore */
function bech32MDecode(btcAddress: string) {
const { words: bech32MWords } = bech32m.decode(btcAddress);
const witnessVersion = bech32MWords[0];
Expand All @@ -105,6 +107,7 @@ function bech32MDecode(btcAddress: string) {
};
}

/** @ignore */
function decodeNativeSegwitBtcAddress(btcAddress: string): {
witnessVersion: number;
data: Uint8Array;
Expand All @@ -117,6 +120,14 @@ function decodeNativeSegwitBtcAddress(btcAddress: string): {
}

export function decodeBtcAddress(btcAddress: string): {
version: PoXAddressVersion;
data: string;
} {
const { version, data } = decodeBtcAddressBytes(btcAddress);
return { version, data: bytesToHex(data) };
}

export function decodeBtcAddressBytes(btcAddress: string): {
version: PoXAddressVersion;
data: Uint8Array;
} {
Expand Down Expand Up @@ -233,7 +244,7 @@ export function getErrorString(error: StackingErrors): string {
* @returns The converted PoX address as a tuple of version and hashbytes.
*/
export function poxAddressToTuple(poxAddress: string) {
const { version, data } = decodeBtcAddress(poxAddress);
const { version, data } = decodeBtcAddressBytes(poxAddress);
const versionBuff = bufferCV(bigIntToBytes(BigInt(version), 1));
const hashBuff = bufferCV(data);
return tupleCV({
Expand Down Expand Up @@ -261,26 +272,28 @@ function legacyHashModeToBtcAddressVersion(

function _poxAddressToBtcAddress_Values(
version: number,
hashBytes: Uint8Array,
hash: string | Uint8Array,
network: StacksNetworkName
): string {
if (!StacksNetworks.includes(network)) throw new Error('Invalid network.');

hash = typeof hash === 'string' ? hexToBytes(hash) : hash;

switch (version) {
case PoXAddressVersion.P2PKH:
case PoXAddressVersion.P2SH:
case PoXAddressVersion.P2SHP2WPKH:
case PoXAddressVersion.P2SHP2WSH: {
const btcAddrVersion = legacyHashModeToBtcAddressVersion(version, network);
return base58CheckEncode(btcAddrVersion, hashBytes);
return base58CheckEncode(btcAddrVersion, hash);
}
case PoXAddressVersion.P2WPKH:
case PoXAddressVersion.P2WSH: {
const words = bech32.toWords(hashBytes);
const words = bech32.toWords(hash);
return bech32.encode(SegwitPrefix[network], [SEGWIT_V0, ...words]);
}
case PoXAddressVersion.P2TR: {
const words = bech32m.toWords(hashBytes);
const words = bech32m.toWords(hash);
return bech32m.encode(SegwitPrefix[network], [SEGWIT_V1, ...words]);
}
}
Expand All @@ -299,13 +312,13 @@ function _poxAddressToBtcAddress_ClarityValue(
* Converts a PoX address to a Bitcoin address.
*
* @param version - The version of the PoX address (as a single number, not a Uint8array).
* @param hashBytes - The hash bytes of the PoX address.
* @param hash - The hash bytes of the PoX address.
* @param network - The network the PoX address is on.
* @returns The corresponding Bitcoin address.
*/
export function poxAddressToBtcAddress(
version: number,
hashBytes: Uint8Array,
hash: string | Uint8Array,
network: StacksNetworkName // todo: allow NetworkParam in the future (minor)
): string;
/**
Expand Down
6 changes: 3 additions & 3 deletions packages/stacking/tests/stacking-2.4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@stacks/internal';
import { STACKS_MAINNET, STACKS_TESTNET } from '@stacks/network';
import { ContractCallPayload, deserializeTransaction, fetchNonce } from '@stacks/transactions';
import { StackingClient, decodeBtcAddress } from '../src';
import { StackingClient, decodeBtcAddress, decodeBtcAddressBytes } from '../src';
import { PoxOperationPeriod } from '../src/constants';
import { BTC_ADDRESS_CASES } from './utils.test';

Expand Down Expand Up @@ -817,7 +817,7 @@ describe('delegated stacking', () => {
expect(rewardSet).toBeDefined();
expect(rewardSet?.total_ustx).toBe(FULL_AMOUNT);
expect(rewardSet?.pox_address.version[0]).toEqual(decodeBtcAddress(poolPoxAddress).version);
expect(rewardSet?.pox_address.hashbytes).toEqual(decodeBtcAddress(poolPoxAddress).data);
expect(rewardSet?.pox_address.hashbytes).toEqual(decodeBtcAddressBytes(poolPoxAddress).data);
});

test('delegator stacks for multiple stackers in a pool, then increases commitment (requires >= pox-2)', async () => {
Expand Down Expand Up @@ -1007,7 +1007,7 @@ describe('delegated stacking', () => {
expect(rewardSet).toBeDefined();
expect(rewardSet?.total_ustx).toBe(AMOUNT_75 * 2n); // 1.5x the FULL_AMOUNT (aka everything the stackers stacked together)
expect(rewardSet?.pox_address.version[0]).toEqual(decodeBtcAddress(poolPoxAddress).version);
expect(rewardSet?.pox_address.hashbytes).toEqual(decodeBtcAddress(poolPoxAddress).data);
expect(rewardSet?.pox_address.hashbytes).toEqual(decodeBtcAddressBytes(poolPoxAddress).data);
});
});

Expand Down
4 changes: 2 additions & 2 deletions packages/stacking/tests/stacking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ test('pox address to btc address', () => {
expect(btcAddress).toBe(item.expectedBtcAddr);
const decodedAddress = decodeBtcAddress(btcAddress);
expect(decodedAddress.version).toBe(item.version);
expect(bytesToHex(decodedAddress.data)).toBe(bytesToHex(item.hashBytes));
expect(decodedAddress.data).toBe(bytesToHex(item.hashBytes));
});

vectors.forEach(item => {
Expand All @@ -1205,7 +1205,7 @@ test('pox address to btc address', () => {
expect(btcAddress).toBe(item.expectedBtcAddr);
const decodedAddress = decodeBtcAddress(btcAddress);
expect(decodedAddress.version).toBe(item.version);
expect(bytesToHex(decodedAddress.data)).toBe(bytesToHex(item.hashBytes));
expect(decodedAddress.data).toBe(bytesToHex(item.hashBytes));
});
});

Expand Down
10 changes: 5 additions & 5 deletions packages/stacking/tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bytesToHex, hexToBytes } from '@stacks/common';
import { hexToBytes } from '@stacks/common';
import { PoXAddressVersion } from '../src/constants';
import { decodeBtcAddress, poxAddressToBtcAddress, poxAddressToTuple } from '../src/utils';

Expand Down Expand Up @@ -68,8 +68,8 @@ test.each(BTC_ADDRESS_CASES)(
({ address, expectedVersion, expectedHash, expectedLength }) => {
const decoded = decodeBtcAddress(address);
expect(decoded.version).toBe(expectedVersion);
expect(decoded.data).toEqual(hexToBytes(expectedHash));
expect(decoded.data).toHaveLength(expectedLength);
expect(decoded.data).toEqual(expectedHash);
expect(decoded.data).toHaveLength(expectedLength * 2);

const tuple = poxAddressToTuple(address);
expect(hexToBytes(tuple.value['version'].value)).toHaveLength(1);
Expand Down Expand Up @@ -265,7 +265,7 @@ test.each(BTC_ADDRESS_CASES_API)(
'decoding and encoding btc address $format',
({ address, hash, format, network }) => {
const decoded = decodeBtcAddress(address);
expect(bytesToHex(decoded.data)).toBe(hash);
expect(decoded.data).toBe(hash);
expect(decoded.version).toBe(FORMAT_TO_VERSION[format]);

const encoded1 = poxAddressToBtcAddress(decoded.version, decoded.data, network);
Expand Down Expand Up @@ -310,7 +310,7 @@ const BTC_ADDRESS_CASES_HASH = [

test.each(BTC_ADDRESS_CASES_HASH)('decoding btc address hash', ({ address, hash }) => {
const decoded = decodeBtcAddress(address);
expect(bytesToHex(decoded.data)).toBe(hash);
expect(decoded.data).toBe(hash);
});

const BTC_ADDRESS_CASES_INVALID_POX_ADDRESS = [
Expand Down
2 changes: 1 addition & 1 deletion packages/transactions/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const rightPadHexToLength = (hexString: string, length: number): string =
export const exceedsMaxLengthBytes = (string: string, maxLengthBytes: number): boolean =>
string ? utf8ToBytes(string).length > maxLengthBytes : false;

/** @internal */
/** @internal @deprecated */
export function cloneDeep<T>(obj: T): T {
return lodashCloneDeep(obj);
}
Expand Down
21 changes: 0 additions & 21 deletions packages/wallet-sdk/src/encryption.ts

This file was deleted.

10 changes: 5 additions & 5 deletions packages/wallet-sdk/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { wordlist } from '@scure/bip39/wordlists/english';
// https://github.com/paulmillr/scure-bip32
// Secure, audited & minimal implementation of BIP32 hierarchical deterministic (HD) wallets.
import { HDKey } from '@scure/bip32';
import { Wallet, getRootNode } from './models/common';
import { encrypt } from './encryption';
import { deriveAccount, deriveWalletKeys } from './derive';
import { DerivationType } from '.';
import { bytesToHex } from '@stacks/common';
import { encryptMnemonic } from '@stacks/encryption';
import { DerivationType } from '.';
import { deriveAccount, deriveWalletKeys } from './derive';
import { Wallet, getRootNode } from './models/common';

export type AllowedKeyEntropyBits = 128 | 256;

Expand Down Expand Up @@ -47,7 +47,7 @@ export const generateWallet = async ({
secretKey: string;
password: string;
}): Promise<Wallet> => {
const ciphertextBytes = await encrypt(secretKey, password);
const ciphertextBytes = await encryptMnemonic(secretKey, password);
const encryptedSecretKey = bytesToHex(ciphertextBytes);

const rootPrivateKey = await mnemonicToSeed(secretKey);
Expand Down
1 change: 0 additions & 1 deletion packages/wallet-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ export * from './models/common';
export * from './models/profile';
export * from './models/wallet-config';
export * from './models/legacy-wallet-config';
export * from './encryption';
export * from './utils';

0 comments on commit e78cc74

Please sign in to comment.