Skip to content

Commit

Permalink
Let the Nimiq SDK calculate the HTLC address
Browse files Browse the repository at this point in the history
Instead of manually calculating it, we can use the built-in logic for HTLC creation transactions. We still need to recreate the parsed `transaction` though, because the recipient data is only known here and is part of the transaction hash (from which the HTLC address is calculated).
  • Loading branch information
sisou committed Jan 13, 2025
1 parent 39ae28e commit 34fec1c
Showing 1 changed file with 2 additions and 13 deletions.
15 changes: 2 additions & 13 deletions src/request/swap-iframe/SwapIFrameApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,21 +409,10 @@ class SwapIFrameApi extends BitcoinRequestParserMixin(RequestParser) { // eslint
const privateKey = new Nimiq.PrivateKey(Nimiq.BufferUtils.fromHex(privateKeys.nim));
const publicKey = Nimiq.PublicKey.derive(privateKey);

let transaction = Nimiq.Transaction.fromPlain({
...storedRequest.fund.transaction.toPlain(),
data: {
type: 'raw',
raw: Nimiq.BufferUtils.toHex(parsedRequest.fund.htlcData),
},
// This NULL-address as the recipient gets replaced below
recipient: new Nimiq.Address(new Uint8Array(20)).toUserFriendlyAddress(),
});
// Calculate the contract address of the HTLC that gets created and recreate the transaction
// with that address as the recipient:
const contractAddress = new Nimiq.Address(Nimiq.BufferUtils.fromHex(transaction.hash()));
let transaction = storedRequest.fund.transaction;
transaction = new Nimiq.Transaction(
transaction.sender, transaction.senderType, transaction.senderData,
contractAddress, transaction.recipientType, transaction.data,
new Nimiq.Address(new Uint8Array(20)), transaction.recipientType, parsedRequest.fund.htlcData,
transaction.value, transaction.fee,
transaction.flags, transaction.validityStartHeight, transaction.networkId,
);
Expand Down

0 comments on commit 34fec1c

Please sign in to comment.