Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'use strict';
var chai = require('chai');
/* jshint unused: false */
var should = chai.should();
var expect = chai.expect;
var bitcore = require('bitcore');
var P2P = require('../');
var Peer = P2P.Peer;
var MessagesData = require('./data/messages');
var Messages = P2P.Messages;
var Pool = P2P.Pool;
var Networks = bitcore.Networks;
var dns = require('dns');
var sinon = require('sinon');
describe('Pool', function() {
it('should be able to create instance', function() {
var pool = new Pool();
should.exist(pool.network);
expect(pool.network).to.satisfy(function(network) {
return network === Networks.testnet || network === Networks.livenet;
});
});
it('should be able to create instance setting the network', function() {
var pool = new Pool(Networks.testnet);
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++) {
const PK = PrivateKey.fromWIF(privateKey);
const refundAddr = PK.toPublicKey().toAddress(this.configuration.network);
const themAddr = new Address(them);
const contract = this.atomicSwapContract(
refundAddr.toJSON().hash,
themAddr.toJSON().hash,
lockTime,
secretHash,
);
const contractP2SH = Util.NewAddressScriptHash(contract.toHex(), this.configuration.network);
const contractP2SHPkScript = Script.buildScriptHashOut(contractP2SH);
const contractTx = new Transaction();
const value = Math.round(amount * 100000000);
// console.log(value);
const output = Transaction.Output({
script: contractP2SHPkScript,
satoshis: value,
});
contractTx.addOutput(output);
const transaction: BtcTransaction = new BtcTransaction(this.configuration);
await transaction.fundTransaction(refundAddr, contractTx);
// SIGN TRANSACTION
const signatures = contractTx.getSignatures(privateKey);
for (const signature of signatures) {
contractTx.applySignature(signature);
}
broadcast(tx, function(id){
console.log("funded to",id);
var tx2 = new bitcore.Transaction()
.from({txId:id, outputIndex:0, inputIndex:0, satoshis:10000, script:P2SHFund.toString()}, [publicKey1, publicKey2], 1)
.to(address, 10000)
.sign(privateKey2);
// console.log('tx2 input',tx2.inputs[0]);
// work around hard-wired multisig to get the signature (TODO make a real input class for P2CM)
// var signature = Sighash.sign(tx2, privateKey2, 1, 0, P2CMScript).toBuffer();
// console.log("tx2 signed",signature);
// use the real script
/*
var s = new bitcore.Script();
s.add('OP_0');
s.add(signature);
s.add(B_secret);
}
}
if (ctTxOutIdx === -1) {
console.log('transaction does not contain a contract output');
return;
}
// TODO: "getrawchangeaddres" WTF?
// const addr = new Address(await getChangeAddress())
const addr = 'mnopGXXKQdt6mXnwHeRcdWNsaksoqKcvwZ';
const outScript = Script.buildPublicKeyHashOut(addr);
// https://bitcoin.org/en/developer-examples#offline-signing
const refundTx = new Transaction();
const lockTime = new BufferReader(pushes.lockTime).readUInt32LE();
refundTx.lockUntilDate(lockTime);
// TODO: "refund output value of %v is dust"
let output = Transaction.Output({
script: outScript,
satoshis: 0,
});
refundTx.addOutput(output);
const feePerKb = await this.getFeePerKb();
console.log('Fee per kb:', feePerKb);
const redeemSerializeSize = Util.EstimateRefundSerializeSize(contract, refundTx.outputs);
const refundFee = Util.FeeForSerializeSize(feePerKb, redeemSerializeSize) * 100000000;
const amount = ctTx.outputs[ctTxOutIdx].satoshis - refundFee;
console.log("transaction does not contain a contract output");
return
}
const PK = PrivateKey.fromWIF(privateKey);
const newRawAddr = PK.toPublicKey().toAddress(configuration.network);
// const addr = new Address(newRawAddr);
// TODO: "getrawchangeaddres" + erroe await getChangeAddress()
// TODO: pass redeemToAddr as parametar
const redeemToAddr = new Address("moPkgMW7QkDpH8iR5nuDuNB6K7UWFWTtXq")
const outScript = Script.buildPublicKeyHashOut(redeemToAddr);
// https://bitcoin.org/en/developer-examples#offline-signing
const redeemTx = new Transaction()
// TODO: "redeem output value of %v is dust"
let output = Transaction.Output({
script: outScript,
satoshis: 0,
})
redeemTx.addOutput(output)
const feePerKb = await getFeePerKb()
const redeemSerializeSize = estimateRedeemSerializeSize(contract, redeemTx.outputs)
const fee = feeForSerializeSize(feePerKb, redeemSerializeSize) * 100000000
const amount = ctTx.outputs[ctTxOutIdx].satoshis - fee
public async redeem(strCt, strCtTx, secret, privateKey) {
// 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 recipientAddrString = pushes.recipientHash.replace('0x', '');
const recipientAddress = Util.NewAddressPubKeyHash(recipientAddrString, '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;
}
const PK = PrivateKey.fromWIF(privateKey);
const refundAddr = PK.toPublicKey().toAddress(configuration.network);
const themAddr = new Address(them);
const contract = atomicSwapContract(
refundAddr.toJSON().hash,
themAddr.toJSON().hash,
lockTime,
secretHash,
);
const contractP2SH = AddressUtil.NewAddressScriptHash(contract.toHex(), configuration.network);
const contractP2SHPkScript = Script.buildScriptHashOut(contractP2SH);
const contractTx = new Transaction();
const value = Math.round(amount * 100000000)
// console.log(value);
const output = Transaction.Output({
script: contractP2SHPkScript,
satoshis: value,
});
contractTx.addOutput(output);
await fundTransaction(refundAddr, contractTx);
//SIGN TRANSACTION
const signitures = contractTx.getSignatures(privateKey);
for (let signiture of signitures) {
contractTx.applySignature(signiture);
}
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
PK = PrivateKey.fromWIF(privateKey);
refundAddr = PK.toPublicKey().toAddress(_config.configuration.network);
themAddr = new Address(them);
contract = (0, _atomicSwapContract.atomicSwapContract)(refundAddr.toJSON().hash, themAddr.toJSON().hash, lockTime, secretHash);
contractP2SH = _addressUtil.AddressUtil.NewAddressScriptHash(contract.toHex(), _config.configuration.network);
contractP2SHPkScript = Script.buildScriptHashOut(contractP2SH);
contractTx = new Transaction();
value = Math.round(amount * 100000000);
// console.log(value);
output = Transaction.Output({
script: contractP2SHPkScript,
satoshis: value
});
contractTx.addOutput(output);
_context.next = 12;
return (0, _fundTransaction.fundTransaction)(refundAddr, contractTx);