Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing regtest prefix in address check #5109

Merged
merged 8 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@
"@radix-ui/react-tooltip": "1.0.7",
"@radix-ui/themes": "2.0.3",
"@reduxjs/toolkit": "1.9.6",
"@scure/base": "1.1.3",
"@scure/bip32": "1.3.2",
"@scure/bip39": "1.2.1",
"@scure/btc-signer": "1.1.0",
"@scure/base": "1.1.5",
"@scure/bip32": "1.3.3",
"@scure/bip39": "1.2.2",
"@scure/btc-signer": "1.2.1",
"@segment/analytics-next": "1.66.0",
"@sentry/react": "7.106.0",
"@sentry/tracing": "7.106.0",
Expand Down Expand Up @@ -188,7 +188,7 @@
"base64url": "3.0.1",
"bignumber.js": "9.1.2",
"bitcoin-address-validation": "2.2.1",
"bitcoinjs-lib": "6.1.3",
"bitcoinjs-lib": "6.1.5",
"bn.js": "5.2.1",
"c32check": "2.0.0",
"chroma-js": "2.4.2",
Expand Down
76 changes: 33 additions & 43 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/app/common/validation/forms/address-validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AddressType, Network, getAddressInfo, validate } from 'bitcoin-address-
import * as yup from 'yup';

import { BitcoinNetworkModes, NetworkConfiguration } from '@shared/constants';
import { bitcoinNetworkModeToCoreNetworkMode } from '@shared/crypto/bitcoin/bitcoin.utils';
import { isString } from '@shared/utils';

import { FormErrorMessages } from '@app/common/error-messages';
Expand Down Expand Up @@ -46,9 +45,16 @@ export function btcTaprootAddressValidator() {
}

function btcAddressNetworkValidatorFactory(network: BitcoinNetworkModes) {
function getAddressNetworkType(network: BitcoinNetworkModes): Network {
// Signet uses testnet address format, this parsing is to please the
// validation library
if (network === 'signet') return Network.testnet;
return network as Network;
}

return (value?: string) => {
if (!isString(value)) return false;
return validate(value, bitcoinNetworkModeToCoreNetworkMode(network) as Network);
return validate(value, getAddressNetworkType(network));
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/app/features/psbt-signer/hooks/use-parsed-inputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export function useParsedInputs({ inputs, indexesToSign }: UseParsedInputsArgs)
const bitcoinNetwork = getBtcSignerLibNetworkConfigByMode(network.chain.bitcoin.bitcoinNetwork);
const bitcoinAddressNativeSegwit = useCurrentAccountNativeSegwitIndexZeroSigner().address;
const { address: bitcoinAddressTaproot } = useCurrentAccountTaprootIndexZeroSigner();
const inscriptions = useGetInscriptionsByOutputQueries(inputs).map(query => {
return query.data?.results[0];
});
const inscriptions = useGetInscriptionsByOutputQueries(inputs).map(
query => query.data?.results[0]
);
const signAll = isUndefined(indexesToSign);

const psbtInputs = useMemo(
Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/psbt-request/use-psbt-request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function usePsbtRequest() {
const tx = getPsbtAsTransaction(payload.hex);

try {
const signingConfig = getDefaultSigningConfig(hexToBytes(payload.hex));
const signingConfig = getDefaultSigningConfig(hexToBytes(payload.hex), signAtIndex);
const signedTx = await signPsbt({ tx, signingConfig });

const signedPsbt = signedTx.toPSBT();
Expand Down
22 changes: 13 additions & 9 deletions src/app/store/accounts/blockchain/bitcoin/bitcoin.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
getAssumedZeroIndexSigningConfig,
} from '@shared/crypto/bitcoin/signer-config';
import { allSighashTypes } from '@shared/rpc/methods/sign-psbt';
import { isNumber } from '@shared/utils';
import { isNumber, isUndefined } from '@shared/utils';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { useWalletType } from '@app/common/use-wallet-type';
Expand Down Expand Up @@ -130,14 +130,18 @@ export function useSignLedgerBitcoinTx() {

const btcSignerPsbtClone = btc.Transaction.fromPSBT(psbt.toBuffer());

const inputByPaymentType = signingConfig.map(config => [
config,
getInputPaymentType(
config.index,
btcSignerPsbtClone.getInput(config.index),
network.chain.bitcoin.bitcoinNetwork
),
]) as readonly [BitcoinInputSigningConfig, PaymentTypes][];
const inputByPaymentType = signingConfig.map(config => {
const inputIndex = btcSignerPsbtClone.getInput(config.index).index;
if (isUndefined(inputIndex)) throw new Error('Input must have an index for payment type');
return [
config,
getInputPaymentType(
inputIndex,
btcSignerPsbtClone.getInput(config.index),
network.chain.bitcoin.bitcoinNetwork
),
];
}) as readonly [BitcoinInputSigningConfig, PaymentTypes][];

//
// Taproot
Expand Down
6 changes: 4 additions & 2 deletions src/shared/crypto/bitcoin/bitcoin.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ export function getInputPaymentType(
): PaymentTypes {
const address = getBitcoinInputAddress(index, input, getBtcSignerLibNetworkConfigByMode(network));
if (address === '') throw new Error('Input address cannot be empty');
if (address.startsWith('bc1p') || address.startsWith('tb1p')) return 'p2tr';
if (address.startsWith('bc1q') || address.startsWith('tb1q')) return 'p2wpkh';
if (address.startsWith('bc1p') || address.startsWith('tb1p') || address.startsWith('bcrt1p'))
return 'p2tr';
if (address.startsWith('bc1q') || address.startsWith('tb1q') || address.startsWith('bcrt1q'))
return 'p2wpkh';
throw new Error('Unable to infer payment type from input address');
}
6 changes: 4 additions & 2 deletions src/shared/crypto/bitcoin/signer-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as btc from '@scure/btc-signer';

import { BitcoinNetworkModes } from '@shared/constants';
import { logger } from '@shared/logger';
import { makeNumberRange } from '@shared/utils';
import { isUndefined, makeNumberRange } from '@shared/utils';

import { getInputPaymentType } from './bitcoin.utils';
import { getTaprootAddressIndexDerivationPath } from './p2tr-address-gen';
Expand Down Expand Up @@ -32,7 +32,9 @@ export function getAssumedZeroIndexSigningConfig({
return indexes.map(inputIndex => {
const input = tx.getInput(inputIndex);

const paymentType = getInputPaymentType(inputIndex, input, 'mainnet');
if (isUndefined(input.index)) throw new Error('Input must have an index for payment type');
const paymentType = getInputPaymentType(input.index, input, network);

switch (paymentType) {
case 'p2wpkh':
return {
Expand Down
Loading
Loading