How to use the bitcoinjs-lib.crypto 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 CounterpartyXCP / indexd-server / lib / express.js View on Github external
function addressToScriptId(address) {
    let script = null

    if (address.startsWith('bc') || address.startsWith('tb')) {
      // Regtest starts with 'bc' too
      let b32res = bech32.decode(address)
      let witnessData = bech32.fromWords(b32res.words.slice(1))
      let witnessOpcodes = [0, 0x14]
      script = Buffer.from(witnessOpcodes.concat(witnessData))
    } else {
      script = bitcoin.address.toOutputScript(address, network)
      console.log('Output Script ID', script.toString('hex'))
    }

    return bitcoin.crypto.sha256(script).toString('hex')
  }
github bitcoin-studio / Bitcoin-Programming-with-BitcoinJS / code / generate_wallets.js View on Github external
console.log(`${Object.keys(wallet)} master xpriv  `, xprivMaster)
  // Get master EC private key
  let privKeyMaster = master.privateKey.toString('hex')
  console.log(`${Object.keys(wallet)} master privKey  `, privKeyMaster)
  // Get private key WIF
  let wifMaster = master.toWIF()
  console.log(`${Object.keys(wallet)} master wif  `, wifMaster)

  // Get BIP32 extended master public key
  let xpubMaster = master.neutered().toBase58()
  console.log(`${Object.keys(wallet)} master xpub  `, xpubMaster)
  // Get master public key
  let pubKeyMaster =  master.publicKey.toString('hex')
  console.log(`${Object.keys(wallet)} master pubKey  `, pubKeyMaster)
  // Get master public key fingerprint
  let pubKeyFingerprintMaster = bitcoin.crypto.hash160(master.publicKey).slice(0,4).toString('hex')
  console.log(`${Object.keys(wallet)} master pubKey fingerprint `, pubKeyFingerprintMaster)
  console.log()

  // Add cryptographic materials to json file
  walletsJSON +=
    `"${Object.keys(wallet)}": [{
      "entropy": "${entropy}",
      "mnemonic": "${mnemonic}",
      "seed": "${seed.toString('hex')}", 
      "xprivMaster": "${xprivMaster}",
      "privKeyMaster": "${privKeyMaster}",
      "wifMaster": "${wifMaster}",
      "xpubMaster": "${xpubMaster}", 
      "pubKeyMaster": "${pubKeyMaster}",
      "pubKeyFingerprintMaster": "${pubKeyFingerprintMaster}"
    },`
github CoinCulture / course-intro-cryptocurrencies-smart-contracts / btc / p2sh_csv.js View on Github external
// decode the b58check encoded address to hex
HASH160 = bitcoin.address.fromBase58Check(addr).hash.toString('hex')

// form the redeem script in hex. its a 2 of 2 multisig
// we can just use the multisig lib function, but lets do it the hardway instead :)
// redeemScript = bitcoin.script.multisig.output.encode(2, [keyPair.getPublicKeyBuffer(), keyPair2.getPublicKeyBuffer()])
// redeemScriptHex = redeemScript.toString('hex')
pubKey1 = keyPair.getPublicKeyBuffer().toString('hex')
pubKey2 = keyPair2.getPublicKeyBuffer().toString('hex')
redeemScriptHex = ops.OP_2 + lenPrefixedHex(pubKey1) + lenPrefixedHex(pubKey2) + ops.OP_2 + ops.OP_CHECKMULTISIG
p2pkhScriptHex = ops.OP_DUP + ops.OP_HASH160 + lenPrefixedHex(HASH160) + ops.OP_EQUALVERIFY + ops.OP_CHECKSIG
csvScriptHex = ops.OP_3 + ops.OP_CHECKSEQUENCEVERIFY + ops.OP_DROP 
redeemScriptHex = ops.OP_IF + redeemScriptHex + ops.OP_ELSE + csvScriptHex + p2pkhScriptHex + ops.OP_ENDIF
redeemScript = new Buffer(redeemScriptHex, "hex")

scriptHash = bitcoin.crypto.hash160(redeemScript).toString('hex')

scriptPubKeyHex = ops.OP_HASH160 + lenPrefixedHex(scriptHash) + ops.OP_EQUAL
scriptPubKey = new Buffer(scriptPubKeyHex, "hex")

var tx = new bitcoin.Transaction()
txHash = Buffer.from(txid, 'hex').reverse() // txhash is reversed because bitcoin is crazy
tx.addInput(txHash, txOutput, null, null) 
tx.addOutput(p2pkh(HASH160), amount) // test
tx.addOutput(scriptPubKey, 1000)

hashType = bitcoin.Transaction.SIGHASH_ALL
signatureHash = tx.hashForSignature(0, p2pkh(HASH160), hashType)
sig = keyPair.sign(signatureHash).toScriptSignature(hashType)

var scriptSigHex = lenPrefixedHex(sig.toString('hex')) + lenPrefixedHex(pubKey1)
tx.ins[0].script = new Buffer(scriptSigHex, "hex")
github elevenyellow / coinfy / miscellaneous / experiments / send_testnet-segwit.js View on Github external
function getSegwitAddressFromECPair(ecpair) {
    const redeemScript = getRedeemScript(ecpair)
    const redeemScriptHash = Bitcoin.crypto.hash160(redeemScript)
    const scriptPubKey = Bitcoin.script.scriptHash.output.encode(
        redeemScriptHash
    )
    return Bitcoin.address.fromOutputScript(scriptPubKey, network)
}
function getRedeemScript(ecpair) {
github dogethereum / dogethereum-contracts / test / utils.js View on Github external
function publicKeyHashFromKeyPair(keyPair) {
  return `0x${bitcoin.crypto.ripemd160(bitcoin.crypto.sha256(keyPair.publicKey)).toString('hex')}`;
}
github blockai-unofficial / openpublish / test / browser-index.js View on Github external
document.getElementById("generate-wallet").addEventListener("click", function(event) {

  var seedString = document.getElementById("brainwallet-seed").value;
  seed = bitcoin.crypto.sha256(seedString);
  wallet = new bitcoin.Wallet(seed, bitcoin.networks.testnet);
  address = wallet.generateAddress();
  document.getElementById("wallet-address").innerHTML = address;

});
github LedgerHQ / ledger-live-desktop / src / helpers / btc.js View on Github external
const pubKeyToSegwitAddress = (pubKey, scriptVersion) => {
    const script = [0x00, 0x14].concat(Array.from(bitcoin.crypto.hash160(pubKey)))
    const hash160 = bitcoin.crypto.hash160(new Uint8Array(script))
    return bitcoin.address.toBase58Check(hash160, scriptVersion)
  }
github blockchain / My-Wallet-V3 / src / helpers.js View on Github external
function parseMiniKey (miniKey) {
  var check = Bitcoin.crypto.sha256(miniKey + '?');
  if (check[0] !== 0x00) {
    throw new Error('Invalid mini key');
  }
  return Bitcoin.crypto.sha256(miniKey);
}
github bitcoin-studio / Bitcoin-Programming-with-BitcoinJS / code / swap_on2off_p2wsh_funding.js View on Github external
const swapContractGenerator = function(claimPublicKey, refundPublicKey, preimageHash, cltv) {
  return bitcoin.script.compile([
    bitcoin.opcodes.OP_HASH160,
    bitcoin.crypto.ripemd160(Buffer.from(PAYMENT_HASH, 'hex')),
    bitcoin.opcodes.OP_EQUAL,
    bitcoin.opcodes.OP_IF,
    claimPublicKey,
    bitcoin.opcodes.OP_ELSE,
    bitcoin.script.number.encode(cltv),
    bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
    bitcoin.opcodes.OP_DROP,
    refundPublicKey,
    bitcoin.opcodes.OP_ENDIF,
    bitcoin.opcodes.OP_CHECKSIG,
  ])
}
github Overtorment / Cashier-BTC / models / signer.js View on Github external
exports.WIF2segwitAddress = function (WIF) {
  let keyPair = bitcoinjs.ECPair.fromWIF(WIF)
  let pubKey = keyPair.getPublicKeyBuffer()
  let witnessScript = bitcoinjs.script.witnessPubKeyHash.output.encode(bitcoinjs.crypto.hash160(pubKey))
  let scriptPubKey = bitcoinjs.script.scriptHash.output.encode(bitcoinjs.crypto.hash160(witnessScript))
  return bitcoinjs.address.fromOutputScript(scriptPubKey)
}