Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!pushes) {
console.log('contract is not an atomic swap script recognized by this tool');
return;
}
const ctTx = new Transaction(strCtTx);
const refundAddrString = pushes.refundHash160.replace('0x', '');
const refundAddress = Util.NewAddressPubKeyHash(refundAddrString, 'testnet');
const contractP2SH = Util.NewAddressScriptHash(strCt, this.configuration.network);
let ctTxOutIdx = -1;
for (let i = 0; i < ctTx.outputs.length; i++) {
const scr = new Script(ctTx.outputs[i].script);
const address = scr.toAddress(this.configuration.network);
const addressHash = address.toJSON().hash;
if (addressHash === contractP2SH.toJSON().hash) {
ctTxOutIdx = i;
break;
}
}
if (ctTxOutIdx === -1) {
console.log('transaction does not contain a contract output');
return;
}
// TODO: "getrawchangeaddres" WTF?
// const addr = new Address(await getChangeAddress())
public async buildRefund(strCt, strCtTx, privateKey) {
console.log('buildRefund');
// TODO: change strCt, strCtTx to ct, ctTx
const contract = new Script(strCt);
const pushes = this.extractAtomicSwapContract(strCt);
if (!pushes) {
console.log('contract is not an atomic swap script recognized by this tool');
return;
}
const ctTx = new Transaction(strCtTx);
const refundAddrString = pushes.refundHash160.replace('0x', '');
const refundAddress = Util.NewAddressPubKeyHash(refundAddrString, 'testnet');
const contractP2SH = Util.NewAddressScriptHash(strCt, this.configuration.network);
let ctTxOutIdx = -1;
for (let i = 0; i < ctTx.outputs.length; i++) {
var refundP2SHContract = exports.refundP2SHContract = function refundP2SHContract(contract, sig, pubkey, secret) {
var script = new Script();
// script.add(sig);
script.add(new Buffer(sig));
script.add(new Buffer(pubkey, 'hex'));
script.add(Opcode.OP_0);
script.add(new Buffer(contract, 'hex'));
// script.add(new Buffer(secret, 'hex'));
// script.add(Buffer.from(secret, "hex"));
return script;
};
const amount = ctTx.outputs[ctTxOutIdx].satoshis - refundFee;
output = Transaction.Output({
script: outScript,
satoshis: amount,
});
refundTx.removeOutput(0);
refundTx.addOutput(output);
const input = Transaction.Input({
prevTxId: ctTx.id,
outputIndex: ctTxOutIdx,
sequenceNumber: 0,
script: new Script(ctTx.outputs[ctTxOutIdx].script),
});
refundTx.uncheckedAddInput(input);
const inputIndex = 0;
const {sig, pubKey} = await this.createSig(refundTx, inputIndex, contract, refundAddress, privateKey);
// TODO: Check
const script = this.refundP2SHContract(contract.toHex(), sig.toTxFormat(), pubKey.toString(), '');
refundTx.inputs[0].setScript(script);
return {
refundFee,
refundTx,
};
export const refundP2SHContract = (contract, sig, pubkey, secret) => {
const script = new Script();
// script.add(sig);
script.add(new Buffer(sig));
script.add(new Buffer(pubkey, 'hex'));
script.add(Opcode.OP_0);
script.add(new Buffer(contract, 'hex'));
// script.add(new Buffer(secret, 'hex'));
// script.add(Buffer.from(secret, "hex"));
return script;
};
fs.readFile(fileName, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var lines = data.split('\n');
try {
scriptPubkey = new bitcore.Script(lines[0]);
tx = bitcore.Transaction(lines[1]);
nIn = parseInt(lines[2]);
var flags = bitcore.Script.Interpreter.SCRIPT_VERIFY_P2SH | bitcore.Script.Interpreter.SCRIPT_VERIFY_DERSIG
//var flags = 0
var interpreter = bitcore.Script.Interpreter();
var verified = interpreter.verify(tx.inputs[nIn].script, scriptPubkey, tx, nIn);
var stack = interpreter.stack
if (verified) {
console.log(1);
} else {
console.log(0);
console.log(interpreter.errstr)
}
} catch (err) {
console.log(0);
return
transaction.toJSON().inputs.map(input => {
const script = new Script(input.scriptString);
const pops = script.toString().split(' ');
const data = pops.filter(opcode => opcode.indexOf('0x') !== -1).map(opdata => opdata.replace('0x', ''));
return data;
}),
);
var atomicSwapContract = exports.atomicSwapContract = function atomicSwapContract(refundAddress, pkhThem, lockTime, secretHash) {
var conv = function conv(num) {
var b = new ArrayBuffer(4);
new DataView(b).setUint32(0, num);
return Array.from(new Uint32Array(b));
};
var decimalToHexString = function decimalToHexString(number) {
if (number < 0) {
number = 0xFFFFFFFF + number + 1;
}
return number.toString(16).toUpperCase();
};
var script = new Script();
script.add(Opcode.OP_IF);
script.add(Opcode.OP_RIPEMD160);
script.add(new Buffer(secretHash, 'hex'));
script.add(Opcode.OP_EQUALVERIFY);
script.add(Opcode.OP_DUP);
script.add(Opcode.OP_HASH160);
script.add(new Buffer(pkhThem, 'hex'));
script.add(Opcode.OP_ELSE);
script.add(new Buffer(decimalToHexString(conv(lockTime)[0]), 'hex'));
script.add('OP_CHECKLOCKTIMEVERIFY');
script.add(Opcode.OP_DROP);
script.add(Opcode.OP_DUP);
script.add(Opcode.OP_HASH160);
script.add(new Buffer(refundAddress, 'hex'));
const hasTxOut = transaction.toJSON().outputs.find((output => {
const script = new Script(output.script);
const address = script.toAddress(configuration.network);
const addressHash = address.toJSON().hash;
return addressHash === contractAddressString;
}));