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

Fix bigInt serialisation Issue #25

Merged
merged 5 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 32 additions & 3 deletions src/transactions/sendBtcTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@ import type { Json } from 'jsontokens';
import { createUnsecuredToken } from 'jsontokens';

import { getDefaultProvider } from '../provider';
import type { SendBtcTransactionOptions } from './types';
import type {
Recipient,
SendBtcTransactionOptions,
SerializedRecipient,
SerializedSendBtcTransactionPayload,
} from './types';

const serializer = (recipient: Recipient[]): SerializedRecipient[] => {
yknl marked this conversation as resolved.
Show resolved Hide resolved
return recipient.map((value) => {
const { address, amountSats } = value;
return {
address,
amountSats: amountSats.toString(),
};
});
};

export const sendBtcTransaction = async (options: SendBtcTransactionOptions) => {
const { getProvider = getDefaultProvider } = options;
Expand All @@ -11,16 +26,30 @@ export const sendBtcTransaction = async (options: SendBtcTransactionOptions) =>
throw new Error('No Bitcoin wallet installed');
}

const { recipients, senderAddress } = options.payload;
const { recipients, senderAddress, network, message } = options.payload;
if (!recipients || recipients.length === 0) {
Copy link
Member

Choose a reason for hiding this comment

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

we need to also add a check on the recipients's type before attempting to serialize it and pass it to the extension

throw new Error('At least one recipient is required');
}
if (
recipients.some(
(item) => typeof item.address !== 'string' || typeof item.amountSats !== 'bigint'
)
) {
throw new Error('Incorrect recipient format');
}
if (!senderAddress) {
throw new Error('The sender address is required');
}

try {
const request = createUnsecuredToken(options.payload as unknown as Json);
const serializedRecipients: SerializedRecipient[] = serializer(recipients);
const serializedPayload: SerializedSendBtcTransactionPayload = {
network,
senderAddress,
message,
recipients: serializedRecipients,
};
const request = createUnsecuredToken(serializedPayload as unknown as Json);
const response = await provider.sendBtcTransaction(request);
options.onFinish?.(response);
} catch (error) {
Expand Down
8 changes: 8 additions & 0 deletions src/transactions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ export interface Recipient {
amountSats: bigint;
}

export type SerializedRecipient = Omit<Recipient, 'amountSats'> & {
amountSats: string;
};

export interface SendBtcTransactionPayload extends RequestPayload {
recipients: Recipient[];
senderAddress: string;
message?: string;
}

export type SerializedSendBtcTransactionPayload = Omit<SendBtcTransactionPayload, 'recipients'> & {
recipients: SerializedRecipient[];
};

export type SendBtcTransactionResponse = string;

export type SendBtcTransactionOptions = RequestOptions<
Expand Down
Loading