How to use the bsv.Transaction function in bsv

To help you get started, we’ve selected a few bsv 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 BitcoinFiles / bitcoinfiles-sdk / test / send.js View on Github external
it('should send txs', async () => {

        const rawtx = '01000000012ad543bf57024014458cf524385248b479d396709e511def541952b718111b91010000006b483045022100ed160167df04b7bd63ebdc4e48b43501ce789ef9a82849245ca2e6687a0306f602201097253115885d35e7d52c6fa7f2b77d6a063dd7c4fd5253eb093c53f577de454121020e7ab2251bb7b40fb25a84906577310de5c7e510ffa376492ccf3fa2e91deb2cffffffff02da940200000000001976a9148f881918cd3589d7ff585a0e8456fa48ea4fd30d88acac841200000000001976a91482a1a3c8458bed0a6cecbd7adcd39edfc11934d888ac00000000';
        const tx = new bsv.Transaction(rawtx);

        console.log('tx', tx.toJSON());

        // decode raw transaction hex
        await (async () => {
            try {
            let decodeRawTransaction = await BITBOX.RawTransactions.decodeRawTransaction(rawtx);
            console.log(JSON.stringify(decodeRawTransaction));
            } catch(error) {
            console.error(error)
        }})();

        const cb = '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff1c03e8db082f7376706f6f6c2e636f6d2f0923a65738686a6ade010080ffffffff018419854a000000001976a914492558fb8ca71a3591316d095afc0f20ef7d42f788ac00000000';

        await (async () => {
            try {
github libitx / preserve-cli / lib / commands / deploy.js View on Github external
.then(utxos => {
        const tx      = new bsv.Transaction().change(process.env.ADDRESS),
              script  = new bsv.Script();
        let   fee     = 0;

        // Add OP_RETURN output
        script.add(bsv.Opcode.OP_RETURN);
        data.forEach(item => {
          // Hex string
          if (typeof item === 'string' && /^0x/i.test(item)) {
            script.add(Buffer.from(item.slice(2), 'hex'))
          // Opcode number
          } else if (typeof item === 'number') {
            script.add(item)
          // Opcode
          } else if (typeof item === 'object' && item.hasOwnProperty('op')) {
            script.add({ opcodenum: item.op })
          // All else
github bowstave / meta-writer / index.js View on Github external
const result = await sendTX(tx.toString())

    utxos = [bsv.Transaction.UnspentOutput({
      address: parentKey.publicKey.toAddress().toString(),
      txId: result,
      outputIndex: 0,
      satoshis: tx.outputs[0].satoshis,
      scriptPubKey: tx.outputs[0].script.toHex()
    })]
  } else {
    utxos = filterUTXOs(utxos, feeForMetanetNode)
  }

  // Create metanet node
  // First, estimate the fee for the metanet node transaction
  let metaTX = new bsv.Transaction().from(utxos)

  metaTX.addOutput(new bsv.Transaction.Output({ script, satoshis: 0 }))

  if (parentKey === null) {
    metaTX.fee(feeForMetanetNode)
    metaTX.change(fundingKey.publicKey.toAddress())
    metaTX.sign(fundingKey.privateKey)
  } else {
    metaTX.sign(parentKey.privateKey)
  }

  const result = await sendTX(metaTX.toString())
  // console.log(result, 'metaTX:', metaTX.toString())

  return result
}
github unwriter / datapay / index.js View on Github external
var build = function(options, callback) {
  let script = null;
  let rpcaddr = (options.pay && options.pay.rpc) ? options.pay.rpc : defaults.rpc;
  if (options.tx) {
    // if tx exists, check to see if it's already been signed.
    // if it's a signed transaction
    // and the request is trying to override using 'data' or 'pay',
    // we should throw an error
    let tx = new bitcoin.Transaction(options.tx)
    // transaction is already signed
    if (tx.inputs.length > 0 && tx.inputs[0].script) {
      if (options.pay || options.data) {
        callback(new Error("the transaction is already signed and cannot be modified"))
        return;
      }
    }
  } else {
    // construct script only if transaction doesn't exist
    // if a 'transaction' attribute exists, the 'data' should be ignored to avoid confusion
    if (options.data) {
      script = _script(options)
    }
  }
  // Instantiate pay
  if (options.pay && options.pay.key) {
github libitx / preserve-cli / lib / commands / split.js View on Github external
buildTx(argv, utxos) {
    const satoshis = utxos
      .map(utxo => utxo.satoshis)
      .reduce((total, amt) => total + amt, 0);
    if (satoshis === 0) throw new Error('Insufficient balance');

    const tx = new bsv.Transaction().from(utxos);
    let fee = 0, cost = 0;
    while(cost <= satoshis) {
      tx.to(process.env.ADDRESS, argv.size)
      fee   = tx._estimateFee();
      cost  = helper.outputSum(tx) + fee + argv.size + 546;
    }
    tx.fee(fee)
      .change(process.env.ADDRESS)
      .sign(process.env.PRIVATE)
    console.log(`Splitting balance of ${ chalk.white(satoshis) } satoshis into ${ chalk.white(tx.outputs.length) } outputs…`)
    return inquirer
      .prompt([{ type: 'confirm', name: 'proceed', message: 'Continue?', default: false }])
      .then(res => res.proceed ? tx : process.exit(0))
  }
}
github monkeylord / MiniGate / index.js View on Github external
return ecl.blockchainTransaction_get(txid, false).then(tx=>{
                if(!tx.code){
                    tx = bsv.Transaction(tx)
                    //console.log(tx.id)
                    Cache.setCache(tx.id,tx.toString())
                }
                return tx
            })
        })
github interplanaria / bpu / index.js View on Github external
return new Promise(function(resolve, reject) {
    let gene = new bsv.Transaction(transaction);
    let inputs = gene.inputs ? collect(o, "in", gene.inputs) : []
    let outputs = gene.outputs ? collect(o, "out", gene.outputs) : []
    resolve({ tx: { h: gene.hash }, in: inputs, out: outputs, lock: gene.nLockTime })
  })
}
github libitx / proxypay / src / proxy-payment.js View on Github external
.then(utxos => {
        const tx = new bsv.Transaction()
          .from(utxos)
          .change(changeAddress)

        const fee = tx._estimateFee()
        tx.fee(fee).sign(this.privKey)
        return bitindex.broadcastTx(tx)
      })
      .then(tx => {
github libitx / proxypay / src / proxy-payment.js View on Github external
this.options = {
      ...defaults,
      ...options
    }

    if (typeof this.options.key === 'string') {
      this.privKey = new bsv.PrivateKey(this.options.key)
    } else {
      this.privKey = this.options.key
    }

    if (typeof this.privKey !== 'object' || !Object.keys(this.privKey).includes('publicKey') ) {
      throw new Error('Must initiate ProxyPayment with valid private key')
    }

    this.tx = new bsv.Transaction()
    this.tx.change(this.options.changeAddress || this.address)
    this.fee = 0;

    this.addOutput(this.options.outputs);
    this.addInput(this.options.inputs);
    this.estimateFee()

    this.onCreate   = this.options.onCreate;
    this.onFunded   = this.options.onFunded;
    this.onPayment  = this.options.onPayment;
    this.onError    = this.options.onError;

    this.onCreate()
  }