UNPKG

@node-dlc/core

Version:
102 lines 5.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ScriptFactory = void 0; const bitcoin_1 = require("@node-dlc/bitcoin"); const crypto_1 = require("@node-dlc/crypto"); class ScriptFactory { /** * Constructs the P2MS commit script used in in the funding transaction * as defined in BOLT3. The pubkeys must be sorted in lexicographical * order. * * @param openPubKey funding_pubkey sent in open_channel * @param acceptPubKey funding_pubkey sent in accept_channel */ static fundingScript(openPubKey, acceptPubKey) { const pubkeys = [openPubKey, acceptPubKey].sort((a, b) => a.compare(b)); return bitcoin_1.Script.p2msLock(2, ...pubkeys); } /** * Constructs an revocable sequence maturing contract using the * provided keys and delay. This script is used in the `to_local` * output of the commmitment transaction as well as the secondary * HTLC-Success and HTLC-Timeout transactions. * * @param revocationPubKey the revocation pubkey that has the ability * to perform a penalty transaction should a revoked version of this * output be spend. * @param delayedPubKey the delayed pubkey spendable after the * sequence delay * @param toSelfDelay the sequence delay in blocks */ static toLocalScript(revocationPubKey, delayedPubKey, toSelfDelay) { return new bitcoin_1.Script(bitcoin_1.OpCode.OP_IF, revocationPubKey, bitcoin_1.OpCode.OP_ELSE, bitcoin_1.Script.number(toSelfDelay), bitcoin_1.OpCode.OP_CHECKSEQUENCEVERIFY, bitcoin_1.OpCode.OP_DROP, delayedPubKey, bitcoin_1.OpCode.OP_ENDIF, bitcoin_1.OpCode.OP_CHECKSIG); // prettier-ignore } /** * Constructs the script for an offered HTLC output of a commitment * transaction as defined in BOLT3. This enables on-chain resolution * of an HTLC to the local node via the secondary HTLC-Timeout * transaction. This secondary transaction is both sequence delayed * and timelocked and requires signatures by both parties to prevent * premature spending. The remote node can immediately resolve the * transaction wit knowledge of the preimage. * * Revocable with witness: * [revocationSig, revocationPubKey] * * Pay to local via the HTLC-Timeout transaction by using witness * [0, remoteHtlcSig, localHtlcSig, <>] * * Pay to remote counterparty without delay using witness * [remoteHtlcSig, preimage] * * @param paymentHash * @param revocationPubKey * @param localHtlcPubKey * @param remoteHtlcPubKey */ static offeredHtlcScript(paymentHash, revocationPubKey, localHtlcPubKey, remoteHtlcPubKey) { return new bitcoin_1.Script( // to remote with revocation key bitcoin_1.OpCode.OP_DUP, bitcoin_1.OpCode.OP_HASH160, (0, crypto_1.hash160)(revocationPubKey), bitcoin_1.OpCode.OP_EQUAL, bitcoin_1.OpCode.OP_IF, bitcoin_1.OpCode.OP_CHECKSIG, bitcoin_1.OpCode.OP_ELSE, remoteHtlcPubKey, bitcoin_1.OpCode.OP_SWAP, bitcoin_1.OpCode.OP_SIZE, bitcoin_1.Script.number(32), bitcoin_1.OpCode.OP_EQUAL, bitcoin_1.OpCode.OP_NOTIF, // to local via HTLC-Timeout transaction (timelocked) bitcoin_1.OpCode.OP_DROP, bitcoin_1.OpCode.OP_2, bitcoin_1.OpCode.OP_SWAP, localHtlcPubKey, bitcoin_1.OpCode.OP_2, bitcoin_1.OpCode.OP_CHECKMULTISIG, bitcoin_1.OpCode.OP_ELSE, // to remote with preimage and signature bitcoin_1.OpCode.OP_HASH160, (0, crypto_1.ripemd160)(paymentHash), bitcoin_1.OpCode.OP_EQUALVERIFY, bitcoin_1.OpCode.OP_CHECKSIG, bitcoin_1.OpCode.OP_ENDIF, bitcoin_1.OpCode.OP_ENDIF); // prettier-ignore } /** * Constructs the script for a received HTLC output of a commitment * transaction as defined in BOLT3. This enables on-chain resolution * of an HTLC to the local node via the secondary HTLC-Success * transaction. This secondary transaction is sequence delayed and * thus local spends require both parties signatures. The remote * node can perform a timeout of this output after the timelock * expires. * * Revocable with witness: * [revocationSig, revocationPubKey] * * Pay to local via the HTLC-Success transaction by using witness * [0, remoteHtlcSig, localHtlcSig, preimage] * * Pay to remote counterparty after the CLTV expiry using witness * [remoteHtlcSig, <>] * * @param paymentHash * @param cltvExpiry * @param revocationPubKey * @param localHtlcPubKey * @param remoteHtlcPubKey */ static receivedHtlcScript(paymentHash, cltvExpiry, revocationPubKey, localHtlcPubKey, remoteHtlcPubKey) { return new bitcoin_1.Script( // to remote with revocation key bitcoin_1.OpCode.OP_DUP, bitcoin_1.OpCode.OP_HASH160, (0, crypto_1.hash160)(revocationPubKey), bitcoin_1.OpCode.OP_EQUAL, bitcoin_1.OpCode.OP_IF, bitcoin_1.OpCode.OP_CHECKSIG, bitcoin_1.OpCode.OP_ELSE, remoteHtlcPubKey, bitcoin_1.OpCode.OP_SWAP, bitcoin_1.OpCode.OP_SIZE, bitcoin_1.Script.number(32), bitcoin_1.OpCode.OP_EQUAL, bitcoin_1.OpCode.OP_IF, // to local via HTLC-Success transaction bitcoin_1.OpCode.OP_HASH160, (0, crypto_1.ripemd160)(paymentHash), bitcoin_1.OpCode.OP_EQUALVERIFY, bitcoin_1.OpCode.OP_2, bitcoin_1.OpCode.OP_SWAP, localHtlcPubKey, bitcoin_1.OpCode.OP_2, bitcoin_1.OpCode.OP_CHECKMULTISIG, bitcoin_1.OpCode.OP_ELSE, // to remote after cltv expiry with signature bitcoin_1.OpCode.OP_DROP, bitcoin_1.Script.number(cltvExpiry), bitcoin_1.OpCode.OP_CHECKLOCKTIMEVERIFY, bitcoin_1.OpCode.OP_DROP, bitcoin_1.OpCode.OP_CHECKSIG, bitcoin_1.OpCode.OP_ENDIF, bitcoin_1.OpCode.OP_ENDIF); // prettier-ignore } } exports.ScriptFactory = ScriptFactory; //# sourceMappingURL=ScriptFactory.js.map