How to use the bitcoinjs-lib.opcodes function in bitcoinjs-lib

To help you get started, we’ve selected a few bitcoinjs-lib 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 wanchain / wanx / test / btc-txs.js View on Github external
const hashForSignatureArgs = [
  0,
  new Buffer.from(redeemScript, 'hex'),
  bitcoin.Transaction.SIGHASH_ALL
];

const encodeArgs = [
  new Buffer(signedSigHash, 'base64'),
  bitcoin.Transaction.SIGHASH_ALL,
];

const redeemCompileArgs = [
  'signature123',
  new Buffer(signerPublicKey, 'hex'),
  new Buffer(x, 'hex'),
  bitcoin.opcodes.OP_TRUE,
];

const revokeCompileArgs = [
  'signature123',
  new Buffer(signerPublicKey, 'hex'),
  bitcoin.opcodes.OP_FALSE,
];

const p2shArgs = {
  redeem: {
    input: 'compiledScript',
    output: new Buffer.from(redeemScript, 'hex'),
  },
  network: bitcoin.networks[network],
};
github bitcoin-studio / Bitcoin-Programming-with-BitcoinJS / code / algebra_puzzle_p2sh.js View on Github external
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})

const txb = new bitcoin.TransactionBuilder(network)

txb.addInput('TX_ID', TX_VOUT)

txb.addOutput(p2wpkhAlice1.address, 999e5)

const tx = txb.buildIncomplete()

const InputScriptP2SH = bitcoin.script.compile([bitcoin.opcodes.OP_2, bitcoin.opcodes.OP_3, p2sh.redeem.output])
tx.setInputScript(0, InputScriptP2SH)

console.log('Transaction hexadecimal:')
console.log(tx.toHex())
github Colored-Coins / coloredcoinsd / coluutils.js View on Github external
function addHashesOutput(tx, address, sha2, sha1) {
      var chunks = []
      chunks.push(bitcoinjs.opcodes.OP_1)
      chunks.push(address ? new Buffer(address, 'hex') : new Buffer('03ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'hex'))
      chunks.push(Buffer.concat([new Buffer('03', 'hex'), sha2], 33))
      if(sha1) {
        chunks.push(Buffer.concat([new Buffer('030000000000000000000000', 'hex'), sha1], 33))
        chunks.push(bitcoinjs.opcodes.OP_3)
      }
      else
        chunks.push(bitcoinjs.opcodes.OP_2)
      chunks.push(bitcoinjs.opcodes.OP_CHECKMULTISIG)

      console.log(chunks)   

      var script = bitcoinjs.Script.fromChunks(chunks)

      //try compute value to pass mindust
      //TODO: actually comput it with the fee from the api request, this assumes static fee per kb
      tx.outs.unshift({ script: script, value: getNoneMinDustByScript(script) })
    }
github Colored-Coins / coloredcoinsd / coluutils.js View on Github external
function addHashesOutput(tx, address, sha2, sha1) {
      var chunks = []
      chunks.push(bitcoinjs.opcodes.OP_1)
      chunks.push(address ? new Buffer(address, 'hex') : new Buffer('03ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'hex'))
      chunks.push(Buffer.concat([new Buffer('03', 'hex'), sha2], 33))
      if(sha1) {
        chunks.push(Buffer.concat([new Buffer('030000000000000000000000', 'hex'), sha1], 33))
        chunks.push(bitcoinjs.opcodes.OP_3)
      }
      else
        chunks.push(bitcoinjs.opcodes.OP_2)
      chunks.push(bitcoinjs.opcodes.OP_CHECKMULTISIG)

      console.log(chunks)   

      var script = bitcoinjs.Script.fromChunks(chunks)

      //try compute value to pass mindust
      //TODO: actually comput it with the fee from the api request, this assumes static fee per kb
      tx.outs.unshift({ script: script, value: getNoneMinDustByScript(script) })
    }
github bitcoin-studio / Bitcoin-Programming-with-BitcoinJS / code / algebra_puzzle_p2wsh.js View on Github external
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'))

const p2wsh = bitcoin.payments.p2wsh({redeem: {output: witnessScript, network}, network})
console.log('P2WSH address:')
console.log(p2wsh.address)

const keyPairAlice1 = bitcoin.ECPair.fromWIF(alice[1].wif, network)
const p2wpkhAlice1 = bitcoin.payments.p2wpkh({pubkey: keyPairAlice1.publicKey, network})

const txb = new bitcoin.TransactionBuilder(network)

txb.addInput('TX_ID', TX_VOUT)
github Colored-Coins / coloredcoinsd / coluutils.js View on Github external
function addHashesOutput(tx, address, sha2, sha1) {
      var chunks = []
      chunks.push(bitcoinjs.opcodes.OP_1)
      chunks.push(address ? new Buffer(address, 'hex') : new Buffer('03ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'hex'))
      chunks.push(Buffer.concat([new Buffer('03', 'hex'), sha2], 33))
      if(sha1) {
        chunks.push(Buffer.concat([new Buffer('030000000000000000000000', 'hex'), sha1], 33))
        chunks.push(bitcoinjs.opcodes.OP_3)
      }
      else
        chunks.push(bitcoinjs.opcodes.OP_2)
      chunks.push(bitcoinjs.opcodes.OP_CHECKMULTISIG)

      console.log(chunks)   

      var script = bitcoinjs.Script.fromChunks(chunks)

      //try compute value to pass mindust
      //TODO: actually comput it with the fee from the api request, this assumes static fee per kb
github bitcoin-studio / Bitcoin-Programming-with-BitcoinJS / code / cltv_p2wsh.js View on Github external
function cltvCheckSigOutput (aQ, bQ, lockTime) {
  return bitcoin.script.compile([
    bitcoin.opcodes.OP_IF,
    bitcoin.script.number.encode(lockTime),
    bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
    bitcoin.opcodes.OP_DROP,

    bitcoin.opcodes.OP_ELSE,
    bQ.publicKey,
    bitcoin.opcodes.OP_CHECKSIGVERIFY,
    bitcoin.opcodes.OP_ENDIF,

    aQ.publicKey,
    bitcoin.opcodes.OP_CHECKSIG
  ])
}
github bitcoin-studio / Bitcoin-Programming-with-BitcoinJS / code / csv_p2wsh.js View on Github external
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,
  ])
}
github creativechain / creativechain-universe / lib / trantor.js View on Github external
let buildOutData = function (dataHex, error) {
            let outData = ContentData.serializeNumber(PUBLICATION.MAGIC_BYTE) + ContentData.serializeNumber(data.mustBeCompressed) + dataHex.toString('hex');

            outData = Buffer.from(outData, 'hex');
            if (!error) {
                that.log('Final data:', outData.length, outData.toString('hex'));
                let ret = creativecoin.script.compile([
                    creativecoin.opcodes.OP_RETURN,
                    outData
                ]);
                callback(ret);
            } else {
                that.error(error);
            }
        };