Skip to content

Commit

Permalink
fix(sell): use CHECK_ASSET_IN_NO_DISPLAY when checkPayoutAddress is c…
Browse files Browse the repository at this point in the history
…alled

Signed-off-by: Stéphane Prohaszka <[email protected]>
  • Loading branch information
sprohaszka-ledger committed Oct 4, 2024
1 parent 7c12a08 commit c0c03c6
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
1 change: 1 addition & 0 deletions libs/ledgerjs/packages/hw-app-exchange/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@ledgerhq/hw-transport-node-speculos-http": "workspace:^",
"@types/jest": "^29.5.10",
"@types/node": "^20.8.10",
"bip32-path": "^0.4.2",
"documentation": "14.0.2",
"jest": "^29.7.0",
"secp256k1": "5.0.0",
Expand Down
94 changes: 91 additions & 3 deletions libs/ledgerjs/packages/hw-app-exchange/src/Exchange.integ.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import bippath from "bip32-path";
import SpeculosTransportHttp from "@ledgerhq/hw-transport-node-speculos-http";
import Exchange, { ExchangeTypes, PartnerKeyInfo } from "./Exchange";
import Transport from "@ledgerhq/hw-transport";
Expand Down Expand Up @@ -183,26 +184,102 @@ describe("Check SWAP until payload signature", () => {

await exchange.checkPartner(partnerSigned);

const amount = new BigNumber(100_000);
// const amount = BigInt(10_000_000_000_000_000); // In Wei (or the smallest unit of the coin), i.e. 0.01 ETH
const amount = BigInt(4_120_000_000_000_000_000); // 4.12 ETH

const outAmount = BigInt(1234); // 12.34 EUR
const b = new ArrayBuffer(8);
new DataView(b).setBigUint64(0, outAmount);
const bufOutAmount = Buffer.from(new Uint8Array(b));

let encodedPayload = await generateSellPayloadProtobuf({
traderEmail: "[email protected]",
inCurrency: "ETH",
inAmount: Buffer.from(amount.toString(16), "hex"),
inAddress: "0xd692Cb1346262F584D17B4B470954501f6715a82",
outCurrency: "EUR",
outAmount: {
coefficient: Buffer.from("1", "hex"),
exponent: 1,
coefficient: bufOutAmount,
exponent: 2,
},
deviceTransactionId: Buffer.from(transactionId.padStart(32, "0"), "hex"),
});
encodedPayload = convertToJWSPayload(encodedPayload);
console.log("SELL Payload in base64:", encodedPayload.toString());

const estimatedFees = new BigNumber(0);
await exchange.processTransaction(encodedPayload, estimatedFees, "jws");

const payloadSignature = await signMessage(encodedPayload, partnerPrivKey, "rs");
await exchange.checkTransactionSignature(payloadSignature);

const configEth = {
config: Buffer.from("0345544808457468657265756d050345544812", "hex"),
signature: Buffer.from(
"3044022065d7931ab3144362d57e3fdcc5de921fb65024737d917f0ab1f8b173d1ed3c2e022027493568d112dc53c7177f8e5fc915d91a903780a067badf109085a73d360323",
"hex",
),
};
const addressParameters = bip32asBuffer("44'/60'/0'/0/0");
exchange.checkPayoutAddress(configEth.config, configEth.signature, addressParameters);
});

it("NG Sell with prepared data", async () => {
// Given
const exchange = new Exchange(transport, ExchangeTypes.SellNg);

// When
const transactionId = await exchange.startNewTransaction();

// Then
expect(transactionId).toEqual(expect.any(String));
expect(transactionId).toHaveLength(64);

const { partnerInfo, partnerSigned } = await appExchangeSellDataset(ngSignFormat);
await exchange.setPartnerKey(partnerInfo);

await exchange.checkPartner(partnerSigned);

const encodedPayload = Buffer.from(
".ChxzYXJhLm5laWxhLWppbWVuZXpAbGVkZ2VyLmZyEgNCVEMaBAAIK1YiIzJOQTVSZ3U4elhUQnZVTkZwbm1qc0RDaTd2eFVtdmRQODhQKgNFVVIyDAoIAAAAAAAAdTAQAjogNQrqDJf3R_HQ92CBRhSkdSOAGxrrfQvLuqKk9Gv4GEs=",
);

const estimatedFees = new BigNumber(0);
await exchange.processTransaction(encodedPayload, estimatedFees, "jws");

// const payloadSignature = await signMessage(encodedPayload, partnerPrivKey, "rs");
const payloadSignature = Buffer.from(
"XzscgDTYa33fxi0yjSN-NtIn9YaIUzRB4VbkIFOUM7fol_7pM5dREuH623DKq5BUMRZNdUrnazS2NlArRyvHUA==",
"base64url",
);
await exchange.checkTransactionSignature(payloadSignature);

// const configBtc = {
// config: Buffer.from("0342544307426974636f696e00", "hex"),
// signature: Buffer.from(
// "3045022100cb174382302219dca359c0a4d457b2569e31a06b2c25c0088a2bd3fd6c04386a02202c6d0a5b924a414621067e316f021aa13aa5b2eee2bf36ea3cfddebc053b201b",
// "hex",
// ),
// };
const configBtcTesnet = {
config: Buffer.from([
0x3, 0x42, 0x54, 0x43, 0xc, 0x42, 0x69, 0x74, 0x63, 0x6f, 0x69, 0x6e, 0x20, 0x54, 0x65,
0x73, 0x74, 0x0,
]),
signature: Buffer.from([
0x30, 0x45, 0x2, 0x21, 0x0, 0xa5, 0x37, 0xd3, 0x11, 0x8a, 0x9c, 0xd8, 0x94, 0x74, 0x4a,
0x4f, 0xd5, 0x5c, 0xb7, 0x3d, 0x4f, 0xb2, 0x60, 0xfc, 0xf4, 0x6d, 0x8f, 0xc8, 0xed, 0x2a,
0xe6, 0x5e, 0x6c, 0x68, 0x44, 0x55, 0xab, 0x2, 0x20, 0x39, 0x2a, 0xf5, 0x7f, 0xbc, 0x57,
0x7d, 0xf5, 0xd1, 0xfe, 0x4d, 0x7d, 0x57, 0xee, 0xea, 0x76, 0x82, 0x44, 0xf2, 0xa5, 0x76,
0x7d, 0xd9, 0x82, 0x90, 0x4, 0xfe, 0x6f, 0x1d, 0x0, 0x3a, 0x58,
]),
};
const addressParameters = bip32asBuffer("84'/0'/0'/0/1");
exchange.checkPayoutAddress(
configBtcTesnet.config,
configBtcTesnet.signature,
addressParameters,
);
});
});

Expand Down Expand Up @@ -384,3 +461,14 @@ function convertSignatureToDER(sig: Uint8Array): Buffer {
function convertToJWSPayload(raw: Buffer): Buffer {
return Buffer.from("." + raw.toString("base64url"));
}

function bip32asBuffer(path: string): Buffer {
const pathElements = !path ? [] : bippath.fromString(path).toPathArray();

const buffer = Buffer.alloc(1 + pathElements.length * 4);
buffer[0] = pathElements.length;
pathElements.forEach((element, index) => {
buffer.writeUInt32BE(element, 1 + 4 * index);
});
return buffer;
}

Check failure on line 474 in libs/ledgerjs/packages/hw-app-exchange/src/Exchange.integ.test.ts

View workflow job for this annotation

GitHub Actions / Test Libraries / Codecheck Libraries

Insert `⏎`
6 changes: 5 additions & 1 deletion libs/ledgerjs/packages/hw-app-exchange/src/Exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const CHECK_PAYOUT_ADDRESS = 0x08;
const CHECK_ASSET_IN = 0x08;

Check failure on line 33 in libs/ledgerjs/packages/hw-app-exchange/src/Exchange.ts

View workflow job for this annotation

GitHub Actions / Test Libraries / Codecheck Libraries

'CHECK_ASSET_IN' is assigned a value but never used
const CHECK_REFUND_ADDRESS = 0x09;
const SIGN_COIN_TRANSACTION = 0x0a;
const CHECK_ASSET_IN_AND_DISPLAY = 0x0b;

Check failure on line 36 in libs/ledgerjs/packages/hw-app-exchange/src/Exchange.ts

View workflow job for this annotation

GitHub Actions / Test Libraries / Codecheck Libraries

'CHECK_ASSET_IN_AND_DISPLAY' is assigned a value but never used
const CHECK_ASSET_IN_NO_DISPLAY = 0x0d;

// Extension for PROCESS_TRANSACTION_RESPONSE APDU
const P2_NONE = 0x00 << 4;
Expand Down Expand Up @@ -280,7 +282,9 @@ export default class Exchange {
]);
const result: Buffer = await this.transport.send(
0xe0,
this.transactionType === ExchangeTypes.Swap ? CHECK_PAYOUT_ADDRESS : CHECK_ASSET_IN,
this.transactionType === ExchangeTypes.Swap || this.transactionType === ExchangeTypes.SwapNg
? CHECK_PAYOUT_ADDRESS
: CHECK_ASSET_IN_NO_DISPLAY,
this.transactionRate,
this.transactionType,
bufferToSend,
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c0c03c6

Please sign in to comment.