Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function generateHtlcTransaction(sender, recipient, hash, value, timeout) {
const hashAlgo = Nimiq.Hash.Algorithm.SHA256
const hashCount = 1
value = Nimiq.Policy.coinsToSatoshis(value)
timeout = $.blockchain.height + timeout
const bufferSize = sender.serializedSize
+ recipient.serializedSize
+ /* hashAlgo */ 1
+ hash.byteLength
+ /* hashCount */ 1
+ /* timeout */ 4;
const buffer = new Nimiq.SerialBuffer(bufferSize);
sender.serialize(buffer);
recipient.serialize(buffer);
buffer.writeUint8(hashAlgo);
buffer.write(hash);
buffer.writeUint8(hashCount);
buffer.writeUint32(timeout);
recipient = Nimiq.Address.CONTRACT_CREATION;
const recipientType = Nimiq.Account.Type.HTLC;
const flags = Nimiq.Transaction.Flag.CONTRACT_CREATION;
return new Nimiq.ExtendedTransaction(sender, Nimiq.Account.Type.BASIC, recipient, recipientType,
value, 0, $.blockchain.height + 1, flags, buffer);
}async function resolveHTLC(address, recipient, hashRoot, preImage) {
address = Nimiq.Address.fromString(address)
recipient = Nimiq.Address.fromString(recipient)
const account = await $.consensus.getAccount(address)
const tx = new Nimiq.ExtendedTransaction(
address, Nimiq.Account.Type.HTLC,
recipient, Nimiq.Account.Type.BASIC,
account.balance, 0,
$.blockchain.height + 1,
Nimiq.Transaction.Flag.NONE, new Uint8Array(0))
const sig = Nimiq.Signature.create($.wallet.keyPair.privateKey, $.wallet.publicKey, tx.serializeContent())
const sigProof = new Nimiq.SignatureProof($.wallet.publicKey, new Nimiq.MerklePath([]), sig)
tx.proof = new Nimiq.SerialBuffer(3 + 2 * Nimiq.Hash.SIZE.get(Nimiq.Hash.Algorithm.SHA256) + sigProof.serializedSize)
tx.proof.writeUint8(Nimiq.HashedTimeLockedContract.ProofType.REGULAR_TRANSFER)
tx.proof.writeUint8(Nimiq.Hash.Algorithm.SHA256)
tx.proof.writeUint8(1)
Nimiq.Hash.fromHex(hashRoot.slice(2)).serialize(tx.proof)
Nimiq.Hash.fromHex(preImage.slice(2)).serialize(tx.proof)
sigProof.serialize(tx.proof)
await sendTransaction(tx)
}async function refundHTLC(address, recipient) {
address = Nimiq.Address.fromString(address)
recipient = Nimiq.Address.fromString(recipient)
const account = await $.consensus.getAccount(address)
const tx = new Nimiq.ExtendedTransaction(
address, Nimiq.Account.Type.HTLC,
recipient, Nimiq.Account.Type.BASIC,
account.balance, 0,
$.blockchain.height + 1,
Nimiq.Transaction.Flag.NONE, new Uint8Array(0))
const sig = Nimiq.Signature.create($.wallet.keyPair.privateKey, $.wallet.publicKey, tx.serializeContent())
const sigProof = new Nimiq.SignatureProof($.wallet.publicKey, new Nimiq.MerklePath([]), sig)
tx.proof = new Nimiq.SerialBuffer(1 + sigProof.serializedSize)
tx.proof.writeUint8(Nimiq.HashedTimeLockedContract.ProofType.TIMEOUT_RESOLVE)
sigProof.serialize(tx.proof)
await waitForBlock(account.timeout)
await sendTransaction(tx)
}