fix: track sighash type changes thru code

This commit is contained in:
fbwoolf
2023-07-26 14:24:57 -05:00
committed by kyranjamie
parent 84236fa23f
commit 62b479c1fc
11 changed files with 42 additions and 23 deletions

View File

@@ -231,7 +231,7 @@
"@babel/core": "7.22.5",
"@babel/preset-react": "7.22.5",
"@babel/preset-typescript": "7.22.5",
"@btckit/types": "0.0.18",
"@btckit/types": "0.0.19",
"@emotion/babel-plugin": "11.11.0",
"@emotion/babel-preset-css-prop": "11.11.0",
"@emotion/cache": "11.11.0",

View File

@@ -1,8 +1,7 @@
import { useMemo } from 'react';
import { useSearchParams } from 'react-router-dom';
import * as btc from '@scure/btc-signer';
import { AllowedSighashTypes } from '@shared/rpc/methods/sign-psbt';
import { ensureArray, undefinedIfLengthZero } from '@shared/utils';
import { useRejectIfLedgerWallet } from '@app/common/rpc-helpers';
@@ -23,7 +22,7 @@ export function usePsbtRequestSearchParams() {
() => ({
appName: payload?.appDetails?.name,
allowedSighash: payload?.allowedSighash
? undefinedIfLengthZero(payload.allowedSighash.map(h => Number(h)) as btc.SignatureHash[])
? undefinedIfLengthZero(payload.allowedSighash.map(h => Number(h)) as AllowedSighashTypes[])
: undefined,
origin,
payload,
@@ -54,7 +53,7 @@ export function useRpcSignPsbtParams() {
requestId,
psbtHex,
allowedSighash: undefinedIfLengthZero(
allowedSighash.map(h => Number(h)) as btc.SignatureHash[]
allowedSighash.map(h => Number(h)) as AllowedSighashTypes[]
),
signAtIndex: undefinedIfLengthZero(ensureArray(signAtIndex).map(h => Number(h))),
};

View File

@@ -1,5 +1,7 @@
import * as btc from '@scure/btc-signer';
import { AllowedSighashTypes } from '@shared/rpc/methods/sign-psbt';
import { useCurrentAccountNativeSegwitIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import { useCurrentAccountTaprootIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks';
@@ -14,7 +16,7 @@ import { PsbtRequestFee } from './components/psbt-request-fee';
import { PsbtRequestRaw } from './components/psbt-request-raw';
interface PsbtRequestDetailsProps {
allowedSighash?: btc.SignatureHash[];
allowedSighash?: AllowedSighashTypes[];
indexesToSign?: number[];
psbtRaw?: RawPsbt;
psbtTxInputs: btc.TransactionInput[];

View File

@@ -8,6 +8,7 @@ import {
getBtcSignerLibNetworkConfigByMode,
} from '@shared/crypto/bitcoin/bitcoin.network';
import { getAddressFromOutScript } from '@shared/crypto/bitcoin/bitcoin.utils';
import { AllowedSighashTypes } from '@shared/rpc/methods/sign-psbt';
import { ensureArray, isDefined, isUndefined } from '@shared/utils';
import { useOrdinalsAwareUtxoQueries } from '@app/query/bitcoin/ordinals/ordinals-aware-utxo.query';
@@ -44,7 +45,7 @@ function getInputValue(index: number, input: btc.TransactionInput) {
}
interface UseParsedInputsArgs {
allowedSighash?: btc.SignatureHash[];
allowedSighash?: AllowedSighashTypes[];
inputs: btc.TransactionInput[];
indexesToSign?: number[];
}

View File

@@ -2,6 +2,8 @@ import { useCallback } from 'react';
import * as btc from '@scure/btc-signer';
import { AllowedSighashTypes } from '@shared/rpc/methods/sign-psbt';
import { subtractMoney } from '@app/common/money/calculate-money';
import { useCurrentAccountNativeSegwitIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import { useCurrentAccountTaprootIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks';
@@ -13,7 +15,7 @@ import { usePsbtInscriptions } from './use-psbt-inscriptions';
import { usePsbtTotals } from './use-psbt-totals';
interface UseParsedPsbtArgs {
allowedSighash?: btc.SignatureHash[];
allowedSighash?: AllowedSighashTypes[];
inputs: btc.TransactionInput[];
indexesToSign?: number[];
outputs: btc.TransactionOutput[];

View File

@@ -4,6 +4,7 @@ import { hexToBytes } from '@noble/hashes/utils';
import * as btc from '@scure/btc-signer';
import { logger } from '@shared/logger';
import { AllowedSighashTypes } from '@shared/rpc/methods/sign-psbt';
import { isString, isUndefined } from '@shared/utils';
import { useCurrentAccountNativeSegwitSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
@@ -12,7 +13,7 @@ import { useCurrentAccountTaprootSigner } from '@app/store/accounts/blockchain/b
export type RawPsbt = ReturnType<typeof btc.RawPSBTV0.decode>;
interface SignPsbtArgs {
allowedSighash?: btc.SignatureHash[];
allowedSighash?: AllowedSighashTypes[];
inputs: btc.TransactionInput[];
indexesToSign?: number[];
tx: btc.Transaction;

View File

@@ -4,6 +4,7 @@ import { useNavigate } from 'react-router-dom';
import * as btc from '@scure/btc-signer';
import { RouteUrls } from '@shared/route-urls';
import { AllowedSighashTypes } from '@shared/rpc/methods/sign-psbt';
import { useRouteHeader } from '@app/common/hooks/use-route-header';
import { PopupHeader } from '@app/features/current-account/popup-header';
@@ -36,7 +37,7 @@ function getPsbtTxOutputs(psbtTx: btc.Transaction) {
}
interface PsbtSignerProps {
allowedSighash?: btc.SignatureHash[];
allowedSighash?: AllowedSighashTypes[];
indexesToSign?: number[];
name?: string;
origin: string;

View File

@@ -11,6 +11,7 @@ import {
} from '@shared/crypto/bitcoin/bitcoin.utils';
import { getTaprootAddressIndexDerivationPath } from '@shared/crypto/bitcoin/p2tr-address-gen';
import { getNativeSegwitAddressIndexDerivationPath } from '@shared/crypto/bitcoin/p2wpkh-address-gen';
import { AllowedSighashTypes } from '@shared/rpc/methods/sign-psbt';
interface Signer<Payment> {
network: BitcoinNetworkModes;
@@ -20,7 +21,7 @@ interface Signer<Payment> {
address: string;
publicKey: Uint8Array;
sign(tx: btc.Transaction): void;
signIndex(tx: btc.Transaction, index: number, allowedSighash?: btc.SignatureHash[]): void;
signIndex(tx: btc.Transaction, index: number, allowedSighash?: AllowedSighashTypes[]): void;
}
interface MakeBitcoinSignerArgs {
@@ -29,7 +30,7 @@ interface MakeBitcoinSignerArgs {
derivationPath: string;
paymentFn(keychain: HDKey, network: BitcoinNetworkModes): any;
signFn(tx: btc.Transaction): void;
signAtIndexFn(tx: btc.Transaction, index: number, allowedSighash?: btc.SignatureHash[]): void;
signAtIndexFn(tx: btc.Transaction, index: number, allowedSighash?: AllowedSighashTypes[]): void;
}
function makeBitcoinSigner<T extends MakeBitcoinSignerArgs>(args: T) {
const { derivationPath, keychain, network, paymentFn, signFn, signAtIndexFn } = args;
@@ -89,7 +90,8 @@ export function bitcoinAddressIndexSignerFactory<T extends BitcoinAddressIndexSi
tx.sign(addressIndexKeychain.privateKey);
},
signAtIndexFn(tx: btc.Transaction, index: number, allowedSighash?: btc.SignatureHash[]) {
// TODO: Revisit allowedSighash type if/when fixed in btc-signer
signAtIndexFn(tx: btc.Transaction, index: number, allowedSighash?: number[]) {
if (!addressIndexKeychain.privateKey)
throw new Error('Unable to sign transaction, no private key found');

View File

@@ -1,14 +1,14 @@
import { DefineRpcMethod, RpcRequest, RpcResponse } from '@btckit/types';
import { AllowAdditionaProperties } from '@btckit/types/dist/types/utils';
import { AllowAdditionalProperties } from '@btckit/types/dist/types/utils';
interface BitcoinContractResponseParams extends AllowAdditionaProperties {
interface BitcoinContractResponseParams extends AllowAdditionalProperties {
bitcoinContractOffer: string;
counterpartyWalletURL: string;
counterpartyWalletName: string;
counterpartyWalletIcon: string;
}
interface BitcoinContractResponseBody extends AllowAdditionaProperties {
interface BitcoinContractResponseBody extends AllowAdditionalProperties {
contractId: string;
action: string;
txId?: string;

View File

@@ -1,4 +1,5 @@
import { DefineRpcMethod, RpcRequest, RpcResponse } from '@btckit/types';
import { DefineRpcMethod, RpcRequest, RpcResponse, SignatureHash } from '@btckit/types';
import * as btc from '@scure/btc-signer';
import * as yup from 'yup';
import { WalletDefaultNetworkConfigurationIds } from '@shared/constants';
@@ -11,11 +12,21 @@ import {
validateRpcParams,
} from './validation.utils';
const sigHashTypes = [0x01, 0x02, 0x03, 0x80, 0x81, 0x82, 0x83];
// TODO: Revisit allowedSighash type if/when fixed in btc-signer
export type AllowedSighashTypes = SignatureHash | btc.SignatureHash;
const rpcSignPsbtParamsSchema = yup.object().shape({
account: accountSchema,
allowedSighash: yup.array().of(yup.mixed().oneOf(Object.values(sigHashTypes))),
allowedSighash: yup
.array()
.of(
yup
.mixed()
.oneOf([
...Object.values(SignatureHash).filter(Number.isInteger),
...Object.values(btc.SignatureHash).filter(Number.isInteger),
])
),
hex: yup.string().required(),
network: yup.string().oneOf(Object.values(WalletDefaultNetworkConfigurationIds)),
signAtIndex: yup.mixed<number | number[]>().test(testIsNumberOrArrayOfNumbers),

View File

@@ -926,10 +926,10 @@
sha.js "^2.4.11"
smart-buffer "^4.1.0"
"@btckit/types@0.0.18":
version "0.0.18"
resolved "https://registry.yarnpkg.com/@btckit/types/-/types-0.0.18.tgz#48bd0b564a9c14bf0c670b0834131e5054c20d69"
integrity sha512-2b/Ny4rZ+TbGW59xiV/PvXRP1cnF40874t12OzeVKa/BghxBFazEayDHce9YtPTkxQ4ilQkmeNrS67FWK4OsGA==
"@btckit/types@0.0.19":
version "0.0.19"
resolved "https://registry.yarnpkg.com/@btckit/types/-/types-0.0.19.tgz#8454326b669d3b029094d3d20026fdc6e6c72319"
integrity sha512-APoOfYSg9SRR4CMXL606IDtpgh+ZD3kS/+iY0BkUALD6HvXo2pVw20L5YYIc+HrgMcF6WN0TH7TXdVs+Vu+kww==
"@coinbase/cbpay-js@1.0.2":
version "1.0.2"