How to use the bitcore.Transaction function in bitcore

To help you get started, we’ve selected a few bitcore examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github AltCoinExchange / altcoin-atomic-trading-platform / wallet-ts / src / common / contract.ts View on Github external
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);
        }
github quartzjer / pennybank / bc-p2cm.js View on Github external
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);
github AltCoinExchange / altcoin-atomic-trading-platform / wallet-ts / src / common / contract.ts View on Github external
}
        }

        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;
github AltCoinExchange / altcoin-atomic-trading-platform / legacy / btcatomicswap / src / redeem.js View on Github external
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
github AltCoinExchange / altcoin-atomic-trading-platform / wallet-ts / src / common / contract.ts View on Github external
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;
            }
github AltCoinExchange / altcoin-atomic-trading-platform / legacy / btcatomicswap / src / contract / build-contract.js View on Github external
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);
  }
github jonasnick / bitcoinconsensus_testcases / bitcore / valid_script.js View on Github external
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
  }
github AltCoinExchange / altcoin-atomic-trading-platform / btcatomicswap / dist / audit-contract.js View on Github external
var auditContract = exports.auditContract = function auditContract(ct, tx) {

  var contract = new Script(ct);
  var contractScriptHashOut = contract.toScriptHashOut();
  var contractAddress = contractScriptHashOut.toAddress();
  var contractAddressString = contractAddress.toJSON().hash;

  var transaction = new Transaction(tx);

  var hasTxOut = transaction.toJSON().outputs.find(function (output) {
    var script = new Script(output.script);
    var address = script.toAddress(_config.configuration.network);
    var addressHash = address.toJSON().hash;
    return addressHash === contractAddressString;
  });

  if (!hasTxOut) {
    console.error('transaction does not contain the secret');
    return;
  }

  var pushes = (0, _extractAtomicSwapContract.extractAtomicSwapContract)(ct);

  var recipientAddrString = pushes.recipientHash.replace('0x', '');
github bitpay / bitcore / lib / client / api.js View on Github external
//Derive proper key to sign, for each input
  var privs = [],
    derived = {};

  var network = new Bitcore.Address(txp.toAddress).network.name;
  var xpriv = new Bitcore.HDPrivateKey(wcd.xPrivKey, network);

  _.each(txp.inputs, function(i) {
    if (!derived[i.path]) {
      derived[i.path] = xpriv.derive(i.path).privateKey;
      privs.push(derived[i.path]);
    }
  });

  var t = new Bitcore.Transaction();

  _.each(txp.inputs, function(i) {
    t.from(i, i.publicKeys, txp.requiredSignatures);
  });

  t.to(txp.toAddress, txp.amount)
    .change(txp.changeAddress.address);

  var signatures = _.map(privs, function(priv, i) {
    return t.getSignatures(priv);
  });

  signatures = _.map(_.sortBy(_.flatten(signatures), 'inputIndex'), function(s) {
    return s.signature.toDER().toString('hex');
  });