Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Prepare transaction
const tx = txb.buildIncomplete()
// Prepare signature hash
const sigHash = bitcoin.Transaction.SIGHASH_ALL
signatureHash = tx.hashForWitnessV0(0, Buffer.from(WITNESS_SCRIPT, 'hex'), 12e2, sigHash)
console.log('Signature hash:')
console.log(signatureHash.toString('hex'))
console.log()
// Happy case: Swap Provider is able to spend the P2WSH
const witnessStackClaimBranch = bitcoin.payments.p2wsh({
redeem: {
input: bitcoin.script.compile([
bitcoin.script.signature.encode(keyPairSwapProvider.sign(signatureHash), sigHash),
Buffer.from(PREIMAGE, 'hex')
]),
output: Buffer.from(WITNESS_SCRIPT, 'hex')
}
}).witness
console.log('Happy case witness stack:')
console.log(witnessStackClaimBranch.map(x => x.toString('hex')))
console.log()
// Failure case: User ask a refund after the timelock has expired
const witnessStackRefundBranch = bitcoin.payments.p2wsh({
redeem: {
input: bitcoin.script.compile([
bitcoin.script.signature.encode(keyPairUser.sign(signatureHash), sigHash),
Buffer.from(PREIMAGE, 'hex')
]),
for (const address of addresses) {
const wif = await this.dumpPrivKey(address)
const wallet = bitcoin.ECPair.fromWIF(wif, this._network)
wallets.push(wallet)
}
let sigs = []
for (let i = 0; i < inputs.length; i++) {
let sigHash
if (segwit) {
sigHash = tx.hashForWitnessV0(inputs[i].index, inputs[i].outputScript, inputs[i].vout.vSat, bitcoin.Transaction.SIGHASH_ALL) // AMOUNT NEEDS TO BE PREVOUT AMOUNT
} else {
sigHash = tx.hashForSignature(inputs[i].index, inputs[i].outputScript, bitcoin.Transaction.SIGHASH_ALL)
}
const sig = bitcoin.script.signature.encode(wallets[i].sign(sigHash), bitcoin.Transaction.SIGHASH_ALL)
sigs.push(sig)
}
return sigs
}
constructor(redeemScript: string, privateKeys: string[]) {
super();
this.redeemScript = Buffer.from(redeemScript, 'hex');
this.privateKeys = privateKeys;
this.isComplete = true;
try {
// try to deduce m (as in m-of-n)
const chunks = bitcoinjs.script.decompile(this.redeemScript);
const firstOp = chunks[0];
this.m = parseInt(bitcoinjs.script.toASM([firstOp]).slice(3), 10);
this.address = bitcoinjs.address.toBase58Check(
bitcoinjs.crypto.hash160(this.redeemScript),
blockstack.config.network.layer1.scriptHash);
} catch (e) {
logger.error(e);
throw new Error('Improper redeem script for multi-sig input.');
}
}
// If we want to run the first scenario we set the timelock (sequence argument)
// txb.addInput(prevTxId, prevOutputIndex, sequence, prevTxScript)
txb.addInput('TX_ID', TX_VOUT, timelock)
txb.addOutput(p2wpkhAlice1.address, 999e5)
const tx = txb.buildIncomplete()
// Prepare the signature hash
const signatureHash = tx.hashForSignature(0, redeemScript, hashType)
// Set input script
const inputScriptFirstBranch = bitcoin.payments.p2sh({
redeem: {
input: bitcoin.script.compile([
bitcoin.script.signature.encode(keyPairAlice1.sign(signatureHash), hashType),
bitcoin.opcodes.OP_TRUE,
]),
output: redeemScript
},
}).input
const inputScriptSecondBranch = bitcoin.payments.p2sh({
redeem: {
input: bitcoin.script.compile([
bitcoin.script.signature.encode(keyPairAlice1.sign(signatureHash), hashType),
bitcoin.script.signature.encode(keyPairBob1.sign(signatureHash), hashType),
bitcoin.opcodes.OP_FALSE
]),
output: redeemScript
}
}).input
const bitcoin = require('bitcoinjs-lib')
const { alice } = require('./wallets.json')
const network = bitcoin.networks.regtest
const witnessScript = bitcoin.script.compile([
bitcoin.opcodes.OP_ADD,
bitcoin.opcodes.OP_5,
bitcoin.opcodes.OP_EQUAL])
console.log('Witness script:')
console.log(witnessScript.toString('hex'))
console.log()
const p2wsh = bitcoin.payments.p2wsh({redeem: {output: witnessScript, network}, network})
const p2sh = bitcoin.payments.p2sh({redeem: p2wsh, network: network})
console.log('P2SH address:')
console.log(p2sh.address)
console.log()
const keyPairAlice1 = bitcoin.ECPair.fromWIF(alice[1].wif, network)
const p2wpkhAlice1 = bitcoin.payments.p2wpkh({pubkey: keyPairAlice1.publicKey, network})
return outputs.map(output => {
if (btc.script.toASM(output.script).startsWith("OP_RETURN")) {
const nullData = btc.script.decompile(output.script)[1];
return {
op_return_data: nullData.toString("hex"),
amount: "0",
script_type: "PAYTOOPRETURN"
};
} else {
const address = bskConfig.network.coerceAddress(
btc.address.fromOutputScript(output.script)
);
return {
address,
amount: `${output.value}`,
script_type: "PAYTOADDRESS"
};
}
readFunc = function(address, value, obj) {
obj.address = address.trim();
obj.value = value;
if (obj.address === Wallet.OP_RETURN) {
var datachunk = Buffer.isBuffer(value) ? value : new Buffer(value, 'utf-8');
obj.scriptPubKey = bitcoin.script.nullData.output.encode(datachunk).toString('hex');
obj.value = 0;
obj.address = null;
}
};
} else {
function csvCheckSigOutput(aQ, bQ, timelock) {
return bitcoin.script.compile([
bitcoin.opcodes.OP_IF,
bitcoin.script.number.encode(timelock),
bitcoin.opcodes.OP_CHECKSEQUENCEVERIFY,
bitcoin.opcodes.OP_DROP,
bitcoin.opcodes.OP_ELSE,
bQ.publicKey,
bitcoin.opcodes.OP_CHECKSIGVERIFY,
bitcoin.opcodes.OP_ENDIF,
aQ.publicKey,
bitcoin.opcodes.OP_CHECKSIG,
])
}
getSwapOutput (recipientAddress, refundAddress, secretHash, nLockTime) {
const recipientPubKeyHash = this.getPubKeyHash(recipientAddress)
const refundPubKeyHash = this.getPubKeyHash(refundAddress)
const OPS = bitcoin.script.OPS
return bitcoin.script.compile([
OPS.OP_IF,
OPS.OP_SIZE,
bitcoin.script.number.encode(32),
OPS.OP_EQUALVERIFY,
OPS.OP_SHA256,
Buffer.from(secretHash, 'hex'),
OPS.OP_EQUALVERIFY,
OPS.OP_DUP,
OPS.OP_HASH160,
recipientPubKeyHash,
OPS.OP_ELSE,
bitcoin.script.number.encode(nLockTime),
OPS.OP_CHECKLOCKTIMEVERIFY,
OPS.OP_DROP,
OPS.OP_DUP,
OPS.OP_HASH160,