From fd465c96eb450b88469865a640da0951f91df718 Mon Sep 17 00:00:00 2001 From: Mahmoud Date: Wed, 25 Oct 2023 18:44:32 +0300 Subject: [PATCH 1/5] init sign bulk psbts --- src/capabilities/index.ts | 1 + src/provider/types.ts | 7 ++++++- src/transactions/signMultipleTransactions.ts | 21 ++++++++++++++++++++ src/transactions/signTransaction.ts | 1 - src/transactions/types.ts | 19 ++++++++++++++++-- 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/transactions/signMultipleTransactions.ts diff --git a/src/capabilities/index.ts b/src/capabilities/index.ts index 958875e..1cc58ef 100644 --- a/src/capabilities/index.ts +++ b/src/capabilities/index.ts @@ -34,6 +34,7 @@ const extractOrValidateCapabilities = ( signTransaction: validateCapability('signTransaction'), sendBtcTransaction: validateCapability('sendBtcTransaction'), createInscription: validateCapability('createInscription'), + signMultipleTransactions: validateCapability('signMultipleTransactions'), }; return Object.entries(capabilityMap).reduce((acc, [capability, value]) => { diff --git a/src/provider/types.ts b/src/provider/types.ts index 19c5385..810bd8f 100644 --- a/src/provider/types.ts +++ b/src/provider/types.ts @@ -3,7 +3,11 @@ import type { CallWalletResponse } from '../call'; import type { GetCapabilitiesResponse } from '../capabilities'; import type { CreateInscriptionResponse } from '../inscriptions'; import type { SignMessageResponse } from '../messages'; -import type { SendBtcTransactionResponse, SignTransactionResponse } from '../transactions'; +import type { + SendBtcTransactionResponse, + SignMultipleTransactionsResponse, + SignTransactionResponse, +} from '../transactions'; interface BaseBitcoinProvider { call: (request: string) => Promise; @@ -12,6 +16,7 @@ interface BaseBitcoinProvider { signTransaction: (request: string) => Promise; sendBtcTransaction: (request: string) => Promise; createInscription: (request: string) => Promise; + signMultipleTransactions: (request: string) => Promise; } export type Capability = keyof BaseBitcoinProvider; diff --git a/src/transactions/signMultipleTransactions.ts b/src/transactions/signMultipleTransactions.ts new file mode 100644 index 0000000..f1b67a1 --- /dev/null +++ b/src/transactions/signMultipleTransactions.ts @@ -0,0 +1,21 @@ +import type { Json } from 'jsontokens'; +import { createUnsecuredToken } from 'jsontokens'; +import { getProviderOrThrow } from '../provider'; +import type { SignMultipleTransactionOptions } from './types'; + +export const signMultipleTransactions = async (options: SignMultipleTransactionOptions) => { + const provider = await getProviderOrThrow(options.getProvider); + + const { psbts } = options.payload; + if (!psbts || !psbts.length) { + throw new Error('psbts array is required'); + } + try { + const request = createUnsecuredToken(options.payload as unknown as Json); + const response = await provider.signMultipleTransactions(request); + options.onFinish?.(response); + } catch (error) { + console.error('[Connect] Error during sign Multiple transactions request', error); + options.onCancel?.(); + } +}; diff --git a/src/transactions/signTransaction.ts b/src/transactions/signTransaction.ts index 3e31d09..e3bd48c 100644 --- a/src/transactions/signTransaction.ts +++ b/src/transactions/signTransaction.ts @@ -1,6 +1,5 @@ import type { Json } from 'jsontokens'; import { createUnsecuredToken } from 'jsontokens'; - import { getProviderOrThrow } from '../provider'; import type { SignTransactionOptions } from './types'; diff --git a/src/transactions/types.ts b/src/transactions/types.ts index 532ba6d..7560aad 100644 --- a/src/transactions/types.ts +++ b/src/transactions/types.ts @@ -32,11 +32,14 @@ export interface InputToSign { sigHash?: number; } -export interface SignTransactionPayload extends RequestPayload { - message: string; +export type PsbtPayload = { psbtBase64: string; inputsToSign: InputToSign[]; broadcast?: boolean; +}; + +export interface SignTransactionPayload extends RequestPayload, PsbtPayload { + message: string; } export interface SignTransactionResponse { @@ -48,3 +51,15 @@ export type SignTransactionOptions = RequestOptions< SignTransactionPayload, SignTransactionResponse >; + +export interface SignMultipleTransactionsPayload extends RequestPayload { + message: string; + psbts: PsbtPayload[]; +} + +export type SignMultipleTransactionsResponse = SignTransactionResponse[]; + +export type SignMultipleTransactionOptions = RequestOptions< + SignMultipleTransactionsPayload, + SignMultipleTransactionsResponse +>; From 4efc9687d5f939fae4aa8287db77489e4957ed98 Mon Sep 17 00:00:00 2001 From: Denys Hriaznov Date: Fri, 27 Oct 2023 12:31:29 +0200 Subject: [PATCH 2/5] Export the `signMultipleTransactions` method --- src/transactions/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/transactions/index.ts b/src/transactions/index.ts index 8fc6c5a..082b03a 100644 --- a/src/transactions/index.ts +++ b/src/transactions/index.ts @@ -1,3 +1,4 @@ export * from './sendBtcTransaction'; export * from './signTransaction'; +export * from './signMultipleTransactions'; export * from './types'; From 89fe39b8119b75f65269c0b94aa8b6e5d0bc66ab Mon Sep 17 00:00:00 2001 From: Mahmoud Date: Mon, 6 Nov 2023 15:12:03 +0200 Subject: [PATCH 3/5] broadcast for all psbts instead of individual ones --- src/transactions/types.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/transactions/types.ts b/src/transactions/types.ts index 7560aad..9fcc4b8 100644 --- a/src/transactions/types.ts +++ b/src/transactions/types.ts @@ -38,6 +38,11 @@ export type PsbtPayload = { broadcast?: boolean; }; +export type SignMultiplePsbtPayload = { + psbtBase64: string; + inputsToSign: InputToSign[]; +}; + export interface SignTransactionPayload extends RequestPayload, PsbtPayload { message: string; } @@ -54,7 +59,8 @@ export type SignTransactionOptions = RequestOptions< export interface SignMultipleTransactionsPayload extends RequestPayload { message: string; - psbts: PsbtPayload[]; + psbts: SignMultiplePsbtPayload[]; + broadcast?: boolean; } export type SignMultipleTransactionsResponse = SignTransactionResponse[]; From 32a2e05a6f65104081ca8938822c23d79a272883 Mon Sep 17 00:00:00 2001 From: Denys Hriaznov Date: Thu, 16 Nov 2023 16:13:40 +0100 Subject: [PATCH 4/5] Add a limit of 100 psbts for the bulk psbt signing --- src/transactions/signMultipleTransactions.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/transactions/signMultipleTransactions.ts b/src/transactions/signMultipleTransactions.ts index f1b67a1..29a8a93 100644 --- a/src/transactions/signMultipleTransactions.ts +++ b/src/transactions/signMultipleTransactions.ts @@ -10,6 +10,11 @@ export const signMultipleTransactions = async (options: SignMultipleTransactionO if (!psbts || !psbts.length) { throw new Error('psbts array is required'); } + + if (psbts.length > 100) { + throw new Error('psbts array must contain less than 100 psbts'); + } + try { const request = createUnsecuredToken(options.payload as unknown as Json); const response = await provider.signMultipleTransactions(request); From 8bd04043430a44ebbb0db70ed156b3b6c275109d Mon Sep 17 00:00:00 2001 From: Mahmoud Date: Wed, 22 Nov 2023 10:23:38 +0200 Subject: [PATCH 5/5] remove broadcast flag for signMultiple --- src/transactions/types.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/transactions/types.ts b/src/transactions/types.ts index 9fcc4b8..1b2cbfb 100644 --- a/src/transactions/types.ts +++ b/src/transactions/types.ts @@ -60,7 +60,6 @@ export type SignTransactionOptions = RequestOptions< export interface SignMultipleTransactionsPayload extends RequestPayload { message: string; psbts: SignMultiplePsbtPayload[]; - broadcast?: boolean; } export type SignMultipleTransactionsResponse = SignTransactionResponse[];