Skip to content

Commit

Permalink
Merge pull request #7521 from LedgerHQ/feat/sell-utils
Browse files Browse the repository at this point in the history
feat: add sell utils
  • Loading branch information
chrisduma-ledger authored Aug 13, 2024
2 parents 4cc569d + 834768f commit 3f396a3
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-comics-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/hw-app-exchange": minor
---

Adds sell utils
31 changes: 31 additions & 0 deletions libs/ledgerjs/packages/hw-app-exchange/src/SellUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { decodeSellPayload } from "./SellUtils"; // Ensure the correct path

describe("decodeSellPayload function", () => {
test("should decode NewSellResponse correctly with hex payload", async () => {
const binaryPayload =
"ChRndW1teS5wd25uQHlhaG9vLmNvbRIDQlRDGgMHiUIiIzJNdkpQUHZSZXVubXZwMXFrUVdtRmo0UzZpd0FVUFgyNDdHKgNFVVIyBgoCW8kQAjogTaLYXOXhi1Mp_Edm6beYPbFvO6plliX8MltsZbbY5oQ";
const decodedPayload = await decodeSellPayload(binaryPayload);
expect(decodedPayload).toBeDefined();
expect(decodedPayload).toHaveProperty("traderEmail", "[email protected]");
expect(decodedPayload).toHaveProperty("inCurrency", "BTC");
expect(decodedPayload).toHaveProperty("inAmount");
expect(decodedPayload).toHaveProperty("inAddress", "2MvJPPvReunmvp1qkQWmFj4S6iwAUPX247G");
expect(decodedPayload).toHaveProperty("outCurrency", "EUR");
expect(decodedPayload).toHaveProperty("outAmount");
expect(decodedPayload).toHaveProperty("deviceTransactionId");
});

test("should decode NewSellResponse correctly with base64 payload", async () => {
const binaryPayload =
"ChRndW1teS5wd25uQHlhaG9vLmNvbRIDQlRDGgMHiUIiIzJNdkpQUHZSZXVubXZwMXFrUVdtRmo0UzZpd0FVUFgyNDdHKgNFVVIyBgoCW8kQAjogTaLYXOXhi1Mp_Edm6beYPbFvO6plliX8MltsZbbY5oQ";
const decodedPayload = await decodeSellPayload(binaryPayload);
expect(decodedPayload).toBeDefined();
expect(decodedPayload).toHaveProperty("traderEmail", "[email protected]");
expect(decodedPayload).toHaveProperty("inCurrency", "BTC");
expect(decodedPayload).toHaveProperty("inAmount");
expect(decodedPayload).toHaveProperty("inAddress", "2MvJPPvReunmvp1qkQWmFj4S6iwAUPX247G");
expect(decodedPayload).toHaveProperty("outCurrency", "EUR");
expect(decodedPayload).toHaveProperty("outAmount");
expect(decodedPayload).toHaveProperty("deviceTransactionId");
});
});
34 changes: 34 additions & 0 deletions libs/ledgerjs/packages/hw-app-exchange/src/SellUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import protobuf from "protobufjs";
import * as protoJson from "./generate-protocol.json";
import { isHexadecimal } from "./shared-utils";

export type SellPayload = {
deviceTransactionId: object;
inAddress: string;
inAmount: object;
inCurrency: string;
outAmount: object;
outCurrency: string;
traderEmail: string;
};

export async function decodeSellPayload(payload: string): Promise<SellPayload> {
const buffer = isHexadecimal(payload)
? Buffer.from(payload, "hex")
: Buffer.from(payload, "base64");

const root: { [key: string]: any } = protobuf.Root.fromJSON(protoJson) || {};

const TransactionResponse = root?.nested.ledger_swap?.NewSellResponse;
const err = TransactionResponse.verify(buffer);

if (err) {
throw Error(err);
}

const decodedPayload = TransactionResponse.decode(buffer);

return {
...decodedPayload,
};
}
5 changes: 1 addition & 4 deletions libs/ledgerjs/packages/hw-app-exchange/src/SwapUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import protobuf from "protobufjs";
import * as protoJson from "./generate-protocol.json";
import { isHexadecimal } from "./shared-utils";

type SwapProtobufPayload = {
payinAddress: string;
Expand Down Expand Up @@ -33,10 +34,6 @@ export type SwapPayload = {
deviceTransactionIdNg?: string;
};

function isHexadecimal(str: string): boolean {
return /^[A-F0-9]+$/i.test(str);
}

export async function decodePayloadProtobuf(payload: string): Promise<SwapPayload> {
const buffer = isHexadecimal(payload)
? Buffer.from(payload, "hex")
Expand Down
2 changes: 2 additions & 0 deletions libs/ledgerjs/packages/hw-app-exchange/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Exchange, {
PayloadSignatureComputedFormat,
} from "./Exchange";
import { decodePayloadProtobuf } from "./SwapUtils";
import { decodeSellPayload } from "./SellUtils";

export {
createExchange,
Expand All @@ -18,6 +19,7 @@ export {
PartnerKeyInfo,
isExchangeTypeNg,
PayloadSignatureComputedFormat,
decodeSellPayload,
};

export default Exchange;
3 changes: 3 additions & 0 deletions libs/ledgerjs/packages/hw-app-exchange/src/shared-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isHexadecimal(str: string): boolean {
return /^[A-F0-9]+$/i.test(str);
}

0 comments on commit 3f396a3

Please sign in to comment.