Skip to content

Commit 691c7aa

Browse files
committed
feat: support changeAddress on btc
1 parent fd522e1 commit 691c7aa

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

.changeset/purple-lamps-rhyme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ledgerhq/wallet-api-core": minor
3+
---
4+
5+
Support changeAddress on bitcoin

packages/core/src/families/bitcoin/serializer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ export function serializeBitcoinTransaction({
77
recipient,
88
feePerByte,
99
opReturnData,
10+
changeAddress,
1011
}: BitcoinTransaction): RawBitcoinTransaction {
1112
return {
1213
family,
1314
amount: amount.toString(),
1415
recipient,
1516
feePerByte: feePerByte?.toString(),
1617
opReturnDataHex: opReturnData?.toString("hex"),
18+
changeAddress,
1719
};
1820
}
1921

@@ -23,6 +25,7 @@ export function deserializeBitcoinTransaction({
2325
recipient,
2426
feePerByte,
2527
opReturnDataHex,
28+
changeAddress,
2629
}: RawBitcoinTransaction): BitcoinTransaction {
2730
return {
2831
family,
@@ -32,5 +35,6 @@ export function deserializeBitcoinTransaction({
3235
opReturnData: opReturnDataHex
3336
? Buffer.from(opReturnDataHex, "hex")
3437
: undefined,
38+
changeAddress,
3539
};
3640
}

packages/core/src/families/bitcoin/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type BitcoinTransaction = TransactionCommon & {
77
readonly family: RawBitcoinTransaction["family"];
88
feePerByte?: BigNumber;
99
opReturnData?: Buffer;
10+
changeAddress?: string;
1011
};
1112

1213
export type RawBitcoinTransaction = z.infer<typeof schemaRawBitcoinTransaction>;

packages/core/src/families/bitcoin/validation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export const schemaRawBitcoinTransaction = schemaTransactionCommon.extend({
55
family: z.literal(schemaFamilies.enum.bitcoin),
66
feePerByte: z.string().optional(),
77
opReturnDataHex: z.string().max(160).optional(),
8+
changeAddress: z.string().optional(),
89
});

packages/core/tests/serializers.spec.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,41 @@ describe("serializers.ts", () => {
231231
feePerByte: undefined,
232232
});
233233
});
234+
235+
it("should succeed to serialize a bitcoin transaction with changeAddress", () => {
236+
const transaction: BitcoinTransaction = {
237+
amount: new BigNumber(100),
238+
recipient: "recipient",
239+
family,
240+
changeAddress: "changeAddress",
241+
};
242+
const serializedTransaction = serializeTransaction(transaction);
243+
244+
expect(serializedTransaction).toEqual({
245+
family,
246+
amount: "100",
247+
recipient: "recipient",
248+
feePerByte: undefined,
249+
changeAddress: "changeAddress",
250+
});
251+
});
252+
253+
it("should succeed to serialize a bitcoin transaction without changeAddress", () => {
254+
const transaction: BitcoinTransaction = {
255+
amount: new BigNumber(100),
256+
recipient: "recipient",
257+
family,
258+
};
259+
const serializedTransaction = serializeTransaction(transaction);
260+
261+
expect(serializedTransaction).toEqual({
262+
family,
263+
amount: "100",
264+
recipient: "recipient",
265+
feePerByte: undefined,
266+
changeAddress: undefined,
267+
});
268+
});
234269
});
235270

236271
describe("kaspa", () => {
@@ -1114,6 +1149,43 @@ describe("serializers.ts", () => {
11141149
feePerByte: undefined,
11151150
});
11161151
});
1152+
1153+
it("should succeed to deserialize a bitcoin transaction with changeAddress", () => {
1154+
const serializedTransaction: RawBitcoinTransaction = {
1155+
family,
1156+
amount: "0",
1157+
recipient: "recipient",
1158+
changeAddress: "changeAddress",
1159+
};
1160+
1161+
const transaction = deserializeTransaction(serializedTransaction);
1162+
1163+
expect(transaction).toEqual({
1164+
family,
1165+
amount: new BigNumber(0),
1166+
recipient: "recipient",
1167+
feePerByte: undefined,
1168+
changeAddress: "changeAddress",
1169+
});
1170+
});
1171+
1172+
it("should succeed to deserialize a bitcoin transaction without changeAddress", () => {
1173+
const serializedTransaction: RawBitcoinTransaction = {
1174+
family,
1175+
amount: "0",
1176+
recipient: "recipient",
1177+
};
1178+
1179+
const transaction = deserializeTransaction(serializedTransaction);
1180+
1181+
expect(transaction).toEqual({
1182+
family,
1183+
amount: new BigNumber(0),
1184+
recipient: "recipient",
1185+
feePerByte: undefined,
1186+
changeAddress: undefined,
1187+
});
1188+
});
11171189
});
11181190

11191191
describe("kaspa", () => {

0 commit comments

Comments
 (0)