Skip to content

Commit

Permalink
sign bulk psbts (#42)
Browse files Browse the repository at this point in the history
* init sign bulk psbts

* Export the `signMultipleTransactions` method

* broadcast for all psbts instead of individual ones

* Add a limit of 100 psbts for the bulk psbt signing

* remove broadcast flag for signMultiple

---------

Co-authored-by: Denys Hriaznov <[email protected]>
Co-authored-by: Den <[email protected]>
Co-authored-by: Tim Man <[email protected]>
  • Loading branch information
4 people authored Nov 29, 2023
1 parent da590f9 commit 488fdeb
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/capabilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const extractOrValidateCapabilities = (
sendBtcTransaction: validateCapability('sendBtcTransaction'),
createInscription: validateCapability('createInscription'),
createRepeatInscriptions: validateCapability('createRepeatInscriptions'),
signMultipleTransactions: validateCapability('signMultipleTransactions'),
};

return Object.entries(capabilityMap).reduce((acc, [capability, value]) => {
Expand Down
7 changes: 6 additions & 1 deletion src/provider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import type { CallWalletResponse } from '../call';
import type { GetCapabilitiesResponse } from '../capabilities';
import type { CreateInscriptionResponse, CreateRepeatInscriptionsResponse } 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<CallWalletResponse>;
Expand All @@ -13,6 +17,7 @@ interface BaseBitcoinProvider {
sendBtcTransaction: (request: string) => Promise<SendBtcTransactionResponse>;
createInscription: (request: string) => Promise<CreateInscriptionResponse>;
createRepeatInscriptions: (request: string) => Promise<CreateRepeatInscriptionsResponse>;
signMultipleTransactions: (request: string) => Promise<SignMultipleTransactionsResponse>;
}

export type Capability = keyof BaseBitcoinProvider;
Expand Down
1 change: 1 addition & 0 deletions src/transactions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './sendBtcTransaction';
export * from './signTransaction';
export * from './signMultipleTransactions';
export * from './types';
26 changes: 26 additions & 0 deletions src/transactions/signMultipleTransactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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');
}

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);
options.onFinish?.(response);
} catch (error) {
console.error('[Connect] Error during sign Multiple transactions request', error);
options.onCancel?.();
}
};
1 change: 0 additions & 1 deletion src/transactions/signTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Json } from 'jsontokens';
import { createUnsecuredToken } from 'jsontokens';

import { getProviderOrThrow } from '../provider';
import type { SignTransactionOptions } from './types';

Expand Down
24 changes: 22 additions & 2 deletions src/transactions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,19 @@ export interface InputToSign {
sigHash?: number;
}

export interface SignTransactionPayload extends RequestPayload {
message: string;
export type PsbtPayload = {
psbtBase64: string;
inputsToSign: InputToSign[];
broadcast?: boolean;
};

export type SignMultiplePsbtPayload = {
psbtBase64: string;
inputsToSign: InputToSign[];
};

export interface SignTransactionPayload extends RequestPayload, PsbtPayload {
message: string;
}

export interface SignTransactionResponse {
Expand All @@ -48,3 +56,15 @@ export type SignTransactionOptions = RequestOptions<
SignTransactionPayload,
SignTransactionResponse
>;

export interface SignMultipleTransactionsPayload extends RequestPayload {
message: string;
psbts: SignMultiplePsbtPayload[];
}

export type SignMultipleTransactionsResponse = SignTransactionResponse[];

export type SignMultipleTransactionOptions = RequestOptions<
SignMultipleTransactionsPayload,
SignMultipleTransactionsResponse
>;

0 comments on commit 488fdeb

Please sign in to comment.