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 2 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';
21 changes: 21 additions & 0 deletions src/transactions/signMultipleTransactions.ts
Original file line number Diff line number Diff line change
@@ -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?.();
}
};
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
19 changes: 17 additions & 2 deletions src/transactions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -48,3 +51,15 @@ export type SignTransactionOptions = RequestOptions<
SignTransactionPayload,
SignTransactionResponse
>;

export interface SignMultipleTransactionsPayload extends RequestPayload {
message: string;
psbts: PsbtPayload[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking on this, do we expect some psbts to have broadcast set to true and some to false or should boroadcast maybe be pulled out of the PsbtPayload type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this was the point i brought up during the daily before maybe @yknl can chime in on that one

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also only passing an address without specifying and index

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pull it out. Having some transactions broadcasted and some not would be bad UX. Developers can always make another request for transactions that they don't intend to broadcast immediately.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-aboelenein will you change the interface here so that broadcast boolean property is related to all of the psbts sent to sign?

}

export type SignMultipleTransactionsResponse = SignTransactionResponse[];

export type SignMultipleTransactionOptions = RequestOptions<
SignMultipleTransactionsPayload,
SignMultipleTransactionsResponse
>;
Loading