-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7521 from LedgerHQ/feat/sell-utils
feat: add sell utils
- Loading branch information
Showing
6 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
libs/ledgerjs/packages/hw-app-exchange/src/SellUtils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |