Skip to content

Commit

Permalink
refactor: upgrade stacks js, closes LEA-1989
Browse files Browse the repository at this point in the history
  • Loading branch information
fbwoolf committed Jan 22, 2025
1 parent 6a94dbe commit 9e917d1
Show file tree
Hide file tree
Showing 100 changed files with 4,384 additions and 6,434 deletions.
49 changes: 28 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@
"@hirosystems/token-metadata-api-client": "1.2.0",
"@hookform/resolvers": "3.9.1",
"@leather.io/analytics": "2.0.1",
"@leather.io/bitcoin": "0.17.0",
"@leather.io/constants": "0.13.5",
"@leather.io/crypto": "1.6.14",
"@leather.io/models": "0.24.3",
"@leather.io/query": "2.26.16",
"@leather.io/stacks": "1.4.0",
"@leather.io/tokens": "0.12.1",
"@leather.io/bitcoin": "file:../mono/packages/bitcoin",
"@leather.io/constants": "file:../mono/packages/constants",
"@leather.io/crypto": "file:../mono/packages/crypto",
"@leather.io/models": "file:../mono/packages/models",
"@leather.io/query": "file:../mono/packages/query",
"@leather.io/stacks": "file:../mono/packages/stacks",
"@leather.io/tokens": "file:../mono/packages/tokens",
"@leather.io/ui": "1.44.4",
"@leather.io/utils": "0.21.1",
"@leather.io/utils": "file:../mono/packages/utils",
"@ledgerhq/hw-transport-webusb": "6.27.19",
"@noble/hashes": "1.5.0",
"@noble/secp256k1": "2.1.0",
Expand All @@ -171,18 +171,15 @@
"@segment/analytics-next": "1.70.0",
"@sentry/browser": "8.26.0",
"@sentry/tracing": "7.106.0",
"@stacks/auth": "6.15.0",
"@stacks/blockchain-api-client": "6.3.4",
"@stacks/common": "6.13.0",
"@stacks/auth": "7.0.2",
"@stacks/common": "7.0.2",
"@stacks/connect": "7.4.0",
"@stacks/connect-ui": "6.1.1",
"@stacks/encryption": "6.15.0",
"@stacks/network": "6.13.0",
"@stacks/profile": "6.15.0",
"@stacks/encryption": "7.0.2",
"@stacks/network": "7.0.2",
"@stacks/profile": "7.0.2",
"@stacks/rpc-client": "1.0.3",
"@stacks/storage": "6.15.0",
"@stacks/transactions": "6.17.0",
"@stacks/wallet-sdk": "6.15.0",
"@stacks/transactions": "7.0.2",
"@stacks/wallet-sdk": "7.0.2",
"@stitches/react": "1.2.8",
"@storybook/addon-styling-webpack": "1.0.1",
"@styled-system/theme-get": "5.1.2",
Expand Down Expand Up @@ -268,9 +265,9 @@
"@btckit/types": "0.0.19",
"@chromatic-com/storybook": "3.2.2",
"@leather.io/eslint-config": "0.7.0",
"@leather.io/panda-preset": "0.8.0",
"@leather.io/panda-preset": "file:../mono/packages/panda-preset",
"@leather.io/prettier-config": "0.6.0",
"@leather.io/rpc": "2.4.0",
"@leather.io/rpc": "file:../mono/packages/rpc",
"@ls-lint/ls-lint": "2.2.3",
"@mdx-js/loader": "3.0.0",
"@pandacss/dev": "0.46.1",
Expand Down Expand Up @@ -393,7 +390,17 @@
"pnpm": {
"overrides": {
"eslint": "8.56.0",
"levelup>semver": "5.7.2"
"levelup>semver": "5.7.2",
"@leather.io/bitcoin": "file:../mono/packages/bitcoin",
"@leather.io/constants": "file:../mono/packages/constants",
"@leather.io/crypto": "file:../mono/packages/crypto",
"@leather.io/models": "file:../mono/packages/models",
"@leather.io/query": "file:../mono/packages/query",
"@leather.io/tokens": "file:../mono/packages/tokens",
"@leather.io/utils": "file:../mono/packages/utils",
"@leather.io/stacks": "file:../mono/packages/stacks",
"@leather.io/panda-preset": "file:../mono/packages/panda-preset",
"@leather.io/rpc": "file:../mono/packages/rpc"
}
},
"keywords": [
Expand Down
8,976 changes: 3,343 additions & 5,633 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions src/app/common/hooks/account/use-switch-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
useCurrentStacksAccount,
useStacksAccounts,
useTransactionAccountIndex,
useTransactionNetworkVersion,
} from '@app/store/accounts/blockchain/stacks/stacks-account.hooks';

import { trackSwitchAccount } from '../../analytics/track-switch-account';
Expand All @@ -18,7 +17,6 @@ export function useSwitchAccount(callback?: () => void) {
const currentAccount = useCurrentStacksAccount();
const accounts = useStacksAccounts();
const txIndex = useTransactionAccountIndex();
const transactionVersion = useTransactionNetworkVersion();
const [hasSwitched, setHasSwitched] = useHasSwitchedAccounts();

const handleSwitchAccount = useCallback(
Expand All @@ -40,5 +38,5 @@ export function useSwitchAccount(callback?: () => void) {
[txIndex, hasSwitched, currentAccount]
);

return { handleSwitchAccount, getIsActive, transactionVersion };
return { handleSwitchAccount, getIsActive };
}
11 changes: 5 additions & 6 deletions src/app/common/hooks/use-calculate-sip10-fiat-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import {
useAlexCurrencyPriceAsMarketData,
useCryptoCurrencyMarketDataMeanAverage,
} from '@leather.io/query';
import { getPrincipalFromAssetString } from '@leather.io/stacks';
import { createMoney } from '@leather.io/utils';

import { useConfigSbtc } from '@app/query/common/remote-config/remote-config.query';

import { getPrincipalFromContractId } from '../utils';

function castBitcoinMarketDataToSbtcMarketData(bitcoinMarketData: MarketData) {
return createMarketData(
createMarketPair('sBTC', 'USD'),
Expand All @@ -25,10 +24,10 @@ export function useSip10FiatMarketData() {

return useMemo(
() => ({
getTokenMarketData(principal: string, symbol: string) {
const lookupIdentifier = principal.includes('::')
? getPrincipalFromContractId(principal)
: principal;
getTokenMarketData(identifier: string, symbol: string) {
const lookupIdentifier = identifier.includes('::')
? getPrincipalFromAssetString(identifier)
: identifier;

if (isSbtcContract(lookupIdentifier)) {
return castBitcoinMarketDataToSbtcMarketData(bitcoinMarketData);
Expand Down
8 changes: 4 additions & 4 deletions src/app/common/hooks/use-submit-stx-transaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react';

import { StacksTransaction, broadcastTransaction } from '@stacks/transactions';
import { StacksTransactionWire, broadcastTransaction } from '@stacks/transactions';

import { delay, isError } from '@leather.io/utils';

Expand Down Expand Up @@ -33,11 +33,11 @@ export function useSubmitTransactionCallback({ loadingKey }: UseSubmitTransactio

return useCallback(
({ onSuccess, onError }: UseSubmitTransactionCallbackArgs) =>
async (transaction: StacksTransaction) => {
async (transaction: StacksTransactionWire) => {
setIsLoading();
try {
const response = await broadcastTransaction(transaction, stacksNetwork);
if (response.error) {
const response = await broadcastTransaction({ transaction, network: stacksNetwork });
if ('error' in response) {
logger.error('Transaction broadcast', response);
if (response.reason) toast.error(getErrorMessage(response.reason));
onError(response.error);
Expand Down
5 changes: 0 additions & 5 deletions src/app/common/keychain/keychain.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { HDKey } from '@scure/bip32';
import { mnemonicToSeedSync } from '@scure/bip39';
import { createStacksPrivateKey, getPublicKey, publicKeyToString } from '@stacks/transactions';

export function derivePublicKey(key: string) {
return publicKeyToString(getPublicKey(createStacksPrivateKey(key)));
}

export function mnemonicToRootNode(secretKey: string) {
const seed = mnemonicToSeedSync(secretKey);
Expand Down
3 changes: 2 additions & 1 deletion src/app/common/profiles/requests.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ProfileUpdatePayload } from '@stacks/connect';
import { getPublicKeyFromPrivate } from '@stacks/encryption';
import { Person } from '@stacks/profile';
import { getAppPrivateKey } from '@stacks/wallet-sdk';
import { TokenVerifier, decodeToken } from 'jsontokens';

import { isString } from '@leather.io/utils';

import type { ProfileUpdatePayload } from '@shared/utils/legacy-requests';

import { StacksAccount } from '@app/store/accounts/blockchain/stacks/stacks-account.models';

export function getProfileDataContentFromToken(requestToken: string): ProfileUpdatePayload {
Expand Down
4 changes: 2 additions & 2 deletions src/app/common/publish-subscribe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Transaction } from '@scure/btc-signer';
import type { SignatureData } from '@stacks/connect';
import type { StacksTransaction } from '@stacks/transactions';
import type { StacksTransactionWire } from '@stacks/transactions';

import type { UnsignedMessage } from '@shared/signature/signature-types';

Expand Down Expand Up @@ -50,7 +50,7 @@ function createPublishSubscribe<E>(): PubSubType<E> {
export interface GlobalAppEvents {
ledgerStacksTxSigned: {
unsignedTx: string;
signedTx: StacksTransaction;
signedTx: StacksTransactionWire;
};
ledgerStacksTxSigningCancelled: {
unsignedTx: string;
Expand Down
10 changes: 5 additions & 5 deletions src/app/common/signature/requests.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CommonSignaturePayload, SignaturePayload } from '@stacks/connect';
import { deserializeCV } from '@stacks/transactions';
import { TokenInterface, decodeToken } from 'jsontokens';

import { isString } from '@leather.io/utils';

import { StructuredMessageDataDomain } from '@shared/signature/signature-types';
import type { StructuredMessageDataDomain } from '@shared/signature/signature-types';
import type { CommonSignaturePayload, SignaturePayload } from '@shared/utils/legacy-requests';

export function getGenericSignaturePayloadFromToken(requestToken: string): CommonSignaturePayload {
const token = decodeToken(requestToken);
Expand All @@ -18,7 +18,7 @@ export function getSignaturePayloadFromToken(requestToken: string): SignaturePay

export function getStructuredDataPayloadFromToken(requestToken: string) {
const token = decodeToken(requestToken);
if (isString(token.payload)) throw new Error('error decoding json token');
if (isString(token.payload)) throw new Error('Error decoding json token');

const result = token.payload as unknown as TokenInterface & {
message: string;
Expand All @@ -27,7 +27,7 @@ export function getStructuredDataPayloadFromToken(requestToken: string) {

return {
...(result as unknown as CommonSignaturePayload),
message: deserializeCV(Buffer.from(result.message, 'hex')),
domain: deserializeCV(Buffer.from(result.domain, 'hex')) as StructuredMessageDataDomain,
message: deserializeCV(result.message),
domain: deserializeCV(result.domain) as StructuredMessageDataDomain,
};
}
78 changes: 3 additions & 75 deletions src/app/common/stacks-utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { ChainID } from '@stacks/transactions';
import { ChainId } from '@stacks/network';
import BigNumber from 'bignumber.js';
import { c32addressDecode } from 'c32check';

import { STX_DECIMALS } from '@leather.io/constants';
import type { NetworkConfiguration } from '@leather.io/models';
import { initBigNumber, microStxToStx } from '@leather.io/utils';

import { logger } from '@shared/logger';
import { isValidUrl } from '@shared/utils/validate-url';

import { abbreviateNumber } from '@app/common/utils';
Expand Down Expand Up @@ -60,9 +59,9 @@ export const validateStacksAddress = (stacksAddress: string): boolean => {
export function validateAddressChain(address: string, currentNetwork: NetworkConfiguration) {
const prefix = address.slice(0, 2);
switch (currentNetwork.chain.stacks.chainId) {
case ChainID.Mainnet:
case ChainId.Mainnet:
return prefix === 'SM' || prefix === 'SP';
case ChainID.Testnet:
case ChainId.Testnet:
return prefix === 'SN' || prefix === 'ST';
default:
return false;
Expand All @@ -78,74 +77,3 @@ export function getSafeImageCanonicalUri(imageCanonicalUri: string, name: string
? imageCanonicalUri
: '';
}

/**
* Gets the contract name of a fully qualified name of an asset.
*
* @param contractId - the source string: [principal].[contract-name] or [principal].[contract-name]::[asset-name]
*/
export const getStacksContractName = (contractId: string): string => {
if (contractId.includes('.')) {
const parts = contractId?.split('.');
if (contractId.includes('::')) {
return parts[1].split('::')[0];
}
return parts[1];
}
logger.warn(
'getStacksContractName: does not contain a period, does not appear to be a contractId.',
contractId
);
return contractId;
};

/**
* Gets the asset name from a a fully qualified name of an asset.
*
* @param contractId - the fully qualified name of the asset: [principal].[contract-name]::[asset-name]
*/
const getStacksContractAssetName = (contractId: string): string => {
if (!contractId.includes('::')) {
logger.warn(
'getStacksContractAssetName: does not contain "::", does not appear to be a fully qualified name of an asset.',
contractId
);
return contractId;
}
return contractId.split('::')[1];
};

/**
* Gets the parts that make up a fully qualified name of an asset.
*
* @param contractId - the fully qualified name of the asset: [principal].[contract-name]::[asset-name]
*/
export const getStacksContractIdStringParts = (
contractId: string
): {
contractAddress: string;
contractAssetName: string;
contractName: string;
} => {
if (!contractId.includes('.') || !contractId.includes('::')) {
logger.warn(
'getStacksContractIdStringParts: does not contain a period or "::", does not appear to be a fully qualified name of an asset.',
contractId
);
return {
contractAddress: contractId,
contractAssetName: contractId,
contractName: contractId,
};
}

const contractAddress = contractId.split('.')[0];
const contractAssetName = getStacksContractAssetName(contractId);
const contractName = getStacksContractName(contractId);

return {
contractAddress,
contractAssetName,
contractName,
};
};
Loading

0 comments on commit 9e917d1

Please sign in to comment.