fix: required allowedSighash

This commit is contained in:
fbwoolf
2023-06-22 12:59:28 -05:00
committed by kyranjamie
parent d86f865177
commit 6ddb8b7197
5 changed files with 10 additions and 16 deletions

View File

@@ -20,7 +20,7 @@ export function usePsbtSigner() {
return useMemo(
() => ({
signPsbtAtIndex(allowedSighash: btc.SignatureHash[], idx: number, tx: btc.Transaction) {
signPsbtAtIndex(idx: number, tx: btc.Transaction, allowedSighash?: btc.SignatureHash[]) {
try {
nativeSegwitSigner?.signIndex(tx, idx, allowedSighash);
} catch (e1) {

View File

@@ -47,8 +47,8 @@ export function usePsbtRequest() {
const indexOrIndexes = payload?.signAtIndex;
const allowedSighash = payload?.allowedSighash;
if (!isUndefined(indexOrIndexes) && !isUndefined(allowedSighash)) {
ensureArray(indexOrIndexes).forEach(idx => signPsbtAtIndex(allowedSighash, idx, tx));
if (!isUndefined(indexOrIndexes)) {
ensureArray(indexOrIndexes).forEach(idx => signPsbtAtIndex(idx, tx, allowedSighash));
} else {
signPsbt(tx);
}

View File

@@ -49,9 +49,9 @@ function useRpcSignPsbt() {
return getDecodedPsbt(psbtHex);
},
onSignPsbt() {
if (!isUndefined(signAtIndex) && !isUndefined(allowedSighash)) {
if (!isUndefined(signAtIndex)) {
signAtIndex.forEach(idx => {
signPsbtAtIndex(allowedSighash, idx, tx);
signPsbtAtIndex(idx, tx, allowedSighash);
});
} else {
signPsbt(tx);

View File

@@ -69,8 +69,8 @@ export async function rpcSignPsbt(message: SignPsbtRequest, port: chrome.runtime
}
if (isDefined(message.params.allowedSighash))
ensureArray(message.params.allowedSighash).forEach(hash =>
requestParams.push(['allowedSighash', hash.toString()])
message.params.allowedSighash.forEach(hash =>
requestParams.push(['allowedSighash', (hash ?? btc.SignatureHash.ALL).toString()])
);
if (isDefined(message.params.signAtIndex))

View File

@@ -1,8 +1,9 @@
import { DefineRpcMethod, RpcRequest, RpcResponse } from '@btckit/types';
import * as btc from '@scure/btc-signer';
import * as yup from 'yup';
import { networkModes } from '@shared/constants';
import { isDefined, isNumber, isUndefined } from '@shared/utils';
import { isNumber, isUndefined } from '@shared/utils';
function testIsNumberOrArrayOfNumbers(value: unknown) {
if (isUndefined(value)) return true;
@@ -12,14 +13,7 @@ function testIsNumberOrArrayOfNumbers(value: unknown) {
const rpcSignPsbtValidator = yup.object().shape({
publicKey: yup.string().required(),
allowedSighash: yup.mixed<number | number[]>().when('signAtIndex', {
is: (signAtIndex: unknown) => isDefined(signAtIndex),
then: schema =>
schema
.test(testIsNumberOrArrayOfNumbers)
.required('allowedSighash required when signAtIndex is provided'),
otherwise: schema => schema.test(testIsNumberOrArrayOfNumbers),
}),
allowedSighash: yup.array().of(yup.mixed().oneOf(Object.values(btc.SignatureHash))),
hex: yup.string().required(),
signAtIndex: yup.mixed<number | number[]>().test(testIsNumberOrArrayOfNumbers),
network: yup.string().oneOf(networkModes),