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 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
6 changes: 3 additions & 3 deletions src/addresses/types.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type { RequestOptions, RequestPayload } from '../types';

export enum AddressPurpose {
export enum AddressPurposes {
Copy link
Contributor

Choose a reason for hiding this comment

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

why are we changing these?

Copy link
Member

Choose a reason for hiding this comment

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

The PR also fixes a bug where the AddressPurposes type name has been reverted to what it originally was since this attribute is used in extension and app.

Ordinals = 'ordinals',
Payment = 'payment',
}

export interface GetAddressPayload extends RequestPayload {
purposes: AddressPurpose[];
purposes: AddressPurposes[];
message: string;
}

export interface Address {
address: string;
publicKey: string;
purpose: AddressPurpose;
purpose: AddressPurposes;
}

export interface GetAddressResponse {
Expand Down
23 changes: 20 additions & 3 deletions src/transactions/sendBtcTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ 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,7 +21,7 @@ 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');
}
Expand All @@ -20,7 +30,14 @@ export const sendBtcTransaction = async (options: SendBtcTransactionOptions) =>
}

try {
const request = createUnsecuredToken(options.payload as unknown as Json);
const serializedRecipients: SerializedRecipient[] = serializer(recipients);
const serialisedPayload: SerializedSendBtcTransactionPayload = {
Copy link
Member

Choose a reason for hiding this comment

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

should be serializedPayload

network,
senderAddress,
message,
recipients: serializedRecipients,
};
const request = createUnsecuredToken(serialisedPayload 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