Skip to content

Commit

Permalink
fix: fix sellNg and fundNg (#8027)
Browse files Browse the repository at this point in the history
* fix: fix sellNg

* fix: extend fix to all ng types

* refactor: set SELL literal

* refactor: add swapNg
  • Loading branch information
sarneijim authored Oct 9, 2024
1 parent 1f88e06 commit 9535221
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
9 changes: 5 additions & 4 deletions libs/exchange-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ export class ExchangeModule extends CustomModule {
provider: string;
fromAccountId: string;
transaction: Transaction;
binaryPayload: Buffer;
signature: Buffer;
binaryPayload: string | Buffer; // Support Coinify Buffer legacy
signature: string | Buffer; // Support Coinify Buffer legacy
feeStrategy: ExchangeCompleteParams["feeStrategy"];
}): Promise<string> {
const result = await this.request<ExchangeCompleteParams, ExchangeCompleteResult>(
Expand All @@ -169,8 +169,9 @@ export class ExchangeModule extends CustomModule {
provider,
fromAccountId,
rawTransaction: serializeTransaction(transaction),
hexBinaryPayload: binaryPayload.toString("hex"),
hexSignature: signature.toString("hex"),
hexBinaryPayload:
typeof binaryPayload === "string" ? binaryPayload : binaryPayload.toString("hex"),
hexSignature: typeof signature === "string" ? signature : signature.toString("hex"),
feeStrategy,
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { firstValueFrom, from, Observable } from "rxjs";
import { TransportStatusError, WrongDeviceForAccount } from "@ledgerhq/errors";

import { delay } from "../../../promise";
import { createExchange, ExchangeTypes } from "@ledgerhq/hw-app-exchange";
import {
isExchangeTypeNg,
ExchangeTypes,
createExchange,
PayloadSignatureComputedFormat,
} from "@ledgerhq/hw-app-exchange";
import perFamily from "../../../generated/exchange";
import { getAccountCurrency, getMainAccount } from "../../../account";
import { getAccountBridge } from "../../../bridge";
Expand Down Expand Up @@ -86,11 +91,14 @@ const completeExchange = (
if (unsubscribed) return;

currentStep = "PROCESS_TRANSACTION";
await exchange.processTransaction(Buffer.from(binaryPayload, "hex"), estimatedFees);
const { payload, format }: { payload: Buffer; format: PayloadSignatureComputedFormat } =
isExchangeTypeNg(exchange.transactionType)
? { payload: Buffer.from("." + binaryPayload), format: "jws" }
: { payload: Buffer.from(binaryPayload, "hex"), format: "raw" };
await exchange.processTransaction(payload, estimatedFees, format);
if (unsubscribed) return;

const bufferSignature = Buffer.from(signature, "hex");
const goodSign = convertSignature(bufferSignature, exchangeType);
const goodSign = convertSignature(signature, exchange.transactionType);

currentStep = "CHECK_TRANSACTION_SIGNATURE";
await exchange.checkTransactionSignature(goodSign);
Expand All @@ -110,7 +118,7 @@ const completeExchange = (
estimatedFees: estimatedFees.toString(),
});
currentStep = "CHECK_PAYOUT_ADDRESS";
await exchange.checkPayoutAddress(
await exchange.validatePayoutOrAsset(
payoutAddressConfig,
payoutAddressConfigSignature,
payoutAddressParameters.addressParameters,
Expand Down Expand Up @@ -173,17 +181,10 @@ const completeExchange = (
* @param {ExchangeTypes} exchangeType
* @return {Buffer} The correct format Buffer for AppExchange call.
*/
function convertSignature(bufferSignature: Buffer, exchangeType: ExchangeTypes): Buffer {
const goodSign =
exchangeType === ExchangeTypes.Sell
? bufferSignature
: Buffer.from(secp256k1.signatureExport(bufferSignature));

if (!goodSign) {
throw new Error("Could not check provider signature");
}

return goodSign;
function convertSignature(signature: string, exchangeType: ExchangeTypes): Buffer {
return isExchangeTypeNg(exchangeType)
? Buffer.from(signature, "base64url")
: <Buffer>secp256k1.signatureExport(Buffer.from(signature, "hex"));
}

export default completeExchange;
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const completeExchange = (

try {
currentStep = "CHECK_PAYOUT_ADDRESS";
await exchange.checkPayoutAddress(
await exchange.validatePayoutOrAsset(
payoutAddressConfig,
payoutAddressConfigSignature,
payoutAddressParameters.addressParameters,
Expand Down
2 changes: 1 addition & 1 deletion libs/ledger-live-common/src/exchange/swap/initSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ const initSwap = (input: InitSwapInput): Observable<SwapRequestEvent> => {
await getCurrencyExchangeConfig(payoutCurrency);

try {
await swap.checkPayoutAddress(
await swap.validatePayoutOrAsset(
payoutAddressConfig,
payoutAddressConfigSignature,
payoutAddressParameters.addressParameters,
Expand Down
8 changes: 5 additions & 3 deletions libs/ledgerjs/packages/hw-app-exchange/src/Exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ const CHECK_PARTNER_COMMAND = 0x05;
const PROCESS_TRANSACTION_RESPONSE = 0x06;
const CHECK_TRANSACTION_SIGNATURE = 0x07;
const CHECK_PAYOUT_ADDRESS = 0x08;
const CHECK_ASSET_IN = 0x08;
const CHECK_REFUND_ADDRESS = 0x09;
const SIGN_COIN_TRANSACTION = 0x0a;
const CHECK_ASSET_IN_AND_DISPLAY = 0x0b;

// Extension for PROCESS_TRANSACTION_RESPONSE APDU
const P2_NONE = 0x00 << 4;
Expand Down Expand Up @@ -260,7 +260,7 @@ export default class Exchange {
maybeThrowProtocolError(result);
}

async checkPayoutAddress(
async validatePayoutOrAsset(
payoutCurrencyConfig: Buffer,
currencyConfigSignature: Buffer,
addressParameters: Buffer,
Expand All @@ -280,7 +280,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_AND_DISPLAY,
this.transactionRate,
this.transactionType,
bufferToSend,
Expand Down

0 comments on commit 9535221

Please sign in to comment.