Skip to content

Commit

Permalink
refactor: upgade @noble/* packages
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Jun 5, 2024
1 parent b3411dc commit 1c5b34d
Show file tree
Hide file tree
Showing 11 changed files with 952 additions and 820 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
"@leather-wallet/tokens": "0.5.0",
"@leather-wallet/utils": "0.6.6",
"@ledgerhq/hw-transport-webusb": "6.27.19",
"@noble/hashes": "1.3.2",
"@noble/hashes": "1.4.0",
"@noble/secp256k1": "2.0.0",
"@octokit/types": "12.4.0",
"@radix-ui/colors": "3.0.0",
Expand All @@ -155,10 +155,10 @@
"@radix-ui/react-tooltip": "1.0.7",
"@radix-ui/themes": "2.0.3",
"@reduxjs/toolkit": "2.2.3",
"@scure/base": "1.1.5",
"@scure/bip32": "1.3.3",
"@scure/base": "1.1.6",
"@scure/bip32": "1.4.0",
"@scure/bip39": "1.3.0",
"@scure/btc-signer": "1.2.1",
"@scure/btc-signer": "1.3.2",
"@segment/analytics-next": "1.70.0",
"@sentry/tracing": "7.106.0",
"@stacks/auth": "6.15.0",
Expand Down
1,693 changes: 910 additions & 783 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/app/common/psbt/requests.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Money } from '@leather-wallet/models';
import { isString } from '@leather-wallet/utils';
import * as btc from '@scure/btc-signer';
import type { TransactionInput } from '@scure/btc-signer/psbt';
import { PsbtPayload } from '@stacks/connect';
import { decodeToken } from 'jsontokens';

export interface SignPsbtArgs {
addressNativeSegwitTotal?: Money;
addressTaprootTotal?: Money;
fee?: Money;
inputs: btc.TransactionInput[];
inputs: TransactionInput[];
}

export function getPsbtPayloadFromToken(requestToken: string): PsbtPayload {
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/loaders/bitcoin-account-loader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { P2Ret } from '@scure/btc-signer';
import { P2Ret } from '@scure/btc-signer/payment';

import { useConfigBitcoinEnabled } from '@app/query/common/remote-config/remote-config.query';
import { useCurrentAccountIndex } from '@app/store/accounts/account';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { P2Ret, P2TROut } from '@scure/btc-signer';
import type { P2Ret, P2TROut } from '@scure/btc-signer/payment';

import { ZERO_INDEX } from '@shared/constants';

Expand Down
4 changes: 2 additions & 2 deletions src/app/features/psbt-signer/hooks/use-parsed-inputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useMemo } from 'react';
import type { Inscription } from '@leather-wallet/models';
import { useInscriptionsByOutputs } from '@leather-wallet/query';
import { isDefined, isUndefined } from '@leather-wallet/utils';
import * as btc from '@scure/btc-signer';
import type { TransactionInput } from '@scure/btc-signer/psbt';
import { bytesToHex } from '@stacks/common';

import { getBtcSignerLibNetworkConfigByMode } from '@shared/crypto/bitcoin/bitcoin.network';
Expand All @@ -24,7 +24,7 @@ export interface PsbtInput {
}

interface UseParsedInputsArgs {
inputs: btc.TransactionInput[];
inputs: TransactionInput[];
indexesToSign?: number[];
}
export function useParsedInputs({ inputs, indexesToSign }: UseParsedInputsArgs) {
Expand Down
4 changes: 2 additions & 2 deletions src/app/features/psbt-signer/hooks/use-parsed-outputs.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo } from 'react';

import { isDefined, isUndefined } from '@leather-wallet/utils';
import * as btc from '@scure/btc-signer';
import type { TransactionOutput } from '@scure/btc-signer/psbt';

import { NetworkConfiguration } from '@shared/constants';
import { getBtcSignerLibNetworkConfigByMode } from '@shared/crypto/bitcoin/bitcoin.network';
Expand All @@ -20,7 +20,7 @@ export interface PsbtOutput {

interface UseParsedOutputsArgs {
isPsbtMutable: boolean;
outputs: btc.TransactionOutput[];
outputs: TransactionOutput[];
network: NetworkConfiguration;
}
export function useParsedOutputs({ isPsbtMutable, outputs, network }: UseParsedOutputsArgs) {
Expand Down
7 changes: 4 additions & 3 deletions src/app/features/psbt-signer/hooks/use-psbt-details.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useCallback, useMemo } from 'react';

import type { TransactionInput, TransactionOutput } from '@scure/btc-signer/psbt';

import { createMoney, subtractMoney } from '@leather-wallet/utils';
import * as btc from '@scure/btc-signer';

import { useCurrentAccountNativeSegwitIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import { useCurrentAccountTaprootIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks';
Expand All @@ -13,9 +14,9 @@ import { usePsbtInscriptions } from './use-psbt-inscriptions';
import { usePsbtTotals } from './use-psbt-totals';

interface UsePsbtDetailsArgs {
inputs: btc.TransactionInput[];
inputs: TransactionInput[];
indexesToSign?: number[];
outputs: btc.TransactionOutput[];
outputs: TransactionOutput[];
}
export function usePsbtDetails({ inputs, indexesToSign, outputs }: UsePsbtDetailsArgs) {
const network = useCurrentNetwork();
Expand Down
7 changes: 4 additions & 3 deletions src/app/features/psbt-signer/hooks/use-psbt-signer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMemo } from 'react';
import { isString } from '@leather-wallet/utils';
import { hexToBytes } from '@noble/hashes/utils';
import * as btc from '@scure/btc-signer';
import { RawPSBTV0, RawPSBTV2 } from '@scure/btc-signer/psbt';

import { BitcoinInputSigningConfig } from '@shared/crypto/bitcoin/signer-config';
import { logger } from '@shared/logger';
Expand All @@ -12,7 +13,7 @@ import {
useSignBitcoinTx,
} from '@app/store/accounts/blockchain/bitcoin/bitcoin.hooks';

export type RawPsbt = ReturnType<typeof btc.RawPSBTV0.decode>;
export type RawPsbt = ReturnType<typeof RawPSBTV0.decode>;

interface SignPsbtArgs {
signingConfig: BitcoinInputSigningConfig[];
Expand All @@ -35,11 +36,11 @@ export function usePsbtSigner() {
getRawPsbt(psbt: string | Uint8Array) {
const bytes = isString(psbt) ? hexToBytes(psbt) : psbt;
try {
return btc.RawPSBTV0.decode(bytes);
return RawPSBTV0.decode(bytes);
} catch (e1) {
logger.error(`Unable to decode PSBT as v0, trying v2, ${e1}`);
try {
return btc.RawPSBTV2.decode(bytes);
return RawPSBTV2.decode(bytes);
} catch (e2) {
throw new Error(`Unable to decode PSBT, ${e1 ?? e2}`);
}
Expand Down
14 changes: 6 additions & 8 deletions src/shared/crypto/bitcoin/bitcoin.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defaultWalletKeyId, isDefined, whenNetwork } from '@leather-wallet/util
import { hexToBytes } from '@noble/hashes/utils';
import { HDKey, Versions } from '@scure/bip32';
import * as btc from '@scure/btc-signer';
import type { TransactionInput, TransactionOutput } from '@scure/btc-signer/psbt';

import { BitcoinNetworkModes, NetworkModes } from '@shared/constants';
import { logger } from '@shared/logger';
Expand Down Expand Up @@ -95,10 +96,7 @@ export function getAddressFromOutScript(script: Uint8Array, bitcoinNetwork: BtcS
}
}

export function getBitcoinInputAddress(
input: btc.TransactionInput,
bitcoinNetwork: BtcSignerNetwork
) {
export function getBitcoinInputAddress(input: TransactionInput, bitcoinNetwork: BtcSignerNetwork) {
if (isDefined(input.witnessUtxo))
return getAddressFromOutScript(input.witnessUtxo.script, bitcoinNetwork);
if (isDefined(input.nonWitnessUtxo) && isDefined(input.index))
Expand All @@ -109,7 +107,7 @@ export function getBitcoinInputAddress(
return '';
}

export function getBitcoinInputValue(input: btc.TransactionInput) {
export function getBitcoinInputValue(input: TransactionInput) {
if (isDefined(input.witnessUtxo)) return Number(input.witnessUtxo.amount);
if (isDefined(input.nonWitnessUtxo) && isDefined(input.index))
return Number(input.nonWitnessUtxo.outputs[input.index]?.amount);
Expand Down Expand Up @@ -251,20 +249,20 @@ export function getTaprootAddress({ index, keychain, network }: GetTaprootAddres

export function getPsbtTxInputs(psbtTx: btc.Transaction) {
const inputsLength = psbtTx.inputsLength;
const inputs: btc.TransactionInput[] = [];
const inputs: TransactionInput[] = [];
for (let i = 0; i < inputsLength; i++) inputs.push(psbtTx.getInput(i));
return inputs;
}

export function getPsbtTxOutputs(psbtTx: btc.Transaction) {
const outputsLength = psbtTx.outputsLength;
const outputs: btc.TransactionOutput[] = [];
const outputs: TransactionOutput[] = [];
for (let i = 0; i < outputsLength; i++) outputs.push(psbtTx.getOutput(i));
return outputs;
}

export function getInputPaymentType(
input: btc.TransactionInput,
input: TransactionInput,
network: BitcoinNetworkModes
): PaymentType {
const address = getBitcoinInputAddress(input, getBtcSignerLibNetworkConfigByMode(network));
Expand Down
27 changes: 16 additions & 11 deletions src/shared/rpc/methods/sign-psbt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { DefineRpcMethod, RpcRequest, RpcResponse, SignatureHash } from '@btckit/types';
import * as btc from '@scure/btc-signer';
import {
SignatureHash as BtcKitSignatureHash,
DefineRpcMethod,
RpcRequest,
RpcResponse,
} from '@btckit/types';
import { SignatureHash } from '@scure/btc-signer/transaction';
import * as yup from 'yup';

import { WalletDefaultNetworkConfigurationIds } from '@shared/constants';
Expand All @@ -13,17 +18,17 @@ import {
} from './validation.utils';

// TODO: Revisit allowedSighash type if/when fixed in btc-signer
export type AllowedSighashTypes = SignatureHash | btc.SignatureHash;
export type AllowedSighashTypes = BtcKitSignatureHash | SignatureHash;
// Pass all sighashTypes through as allowed to btc-signer
export const allSighashTypes = [
btc.SignatureHash.DEFAULT,
SignatureHash.ALL,
SignatureHash.NONE,
SignatureHash.SINGLE,
btc.SignatureHash.ANYONECANPAY,
SignatureHash.ALL_ANYONECANPAY,
SignatureHash.NONE_ANYONECANPAY,
SignatureHash.SINGLE_ANYONECANPAY,
SignatureHash.DEFAULT,
BtcKitSignatureHash.ALL,
BtcKitSignatureHash.NONE,
BtcKitSignatureHash.SINGLE,
SignatureHash.ANYONECANPAY,
BtcKitSignatureHash.ALL_ANYONECANPAY,
BtcKitSignatureHash.NONE_ANYONECANPAY,
BtcKitSignatureHash.SINGLE_ANYONECANPAY,
];

const rpcSignPsbtParamsSchema = yup.object().shape({
Expand Down

0 comments on commit 1c5b34d

Please sign in to comment.