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

sign bulk psbts #42

Merged
merged 7 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions src/capabilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]) => {
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 } 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 @@ -12,6 +16,7 @@ interface BaseBitcoinProvider {
signTransaction: (request: string) => Promise<SignTransactionResponse>;
sendBtcTransaction: (request: string) => Promise<SendBtcTransactionResponse>;
createInscription: (request: string) => Promise<CreateInscriptionResponse>;
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
>;
Loading