How to use the bitcoinjs-lib.address 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 BlueWallet / BlueWallet / tests / unit / signer.js View on Github external
it('should return valid tx hex for RBF-able segwit transactions', function (done) {
      let signer = require('../../models/signer')
      let utxos = [{ txid: '1e1a8cced5580eecd0ac15845fc3adfafbb0f5944a54950e4a16b8f6d1e9b715', vout: 1, address: '3Bsssbs4ANCGNETvGLJ3Fvri6SiVnH1fbi', account: '3Bsssbs4ANCGNETvGLJ3Fvri6SiVnH1fbi', scriptPubKey: 'a9146fbf1cee74734503297e46a0db3e3fbb06f2e9d387', amount: 0.1, confirmations: 108, spendable: false, solvable: false, safe: true }]
      let txhex = signer.createSegwitTransaction(utxos, '1Pb81K1xJnMjUfFgKUbva6gr1HCHXxHVnr', 0.001, 0.0001, 'KyWpryAKPiXXbipxWhtprZjSLVjp22sxbVnJssq2TCNQxs1SuMeD', '3Bsssbs4ANCGNETvGLJ3Fvri6SiVnH1fbi', 0)
      assert.equal(txhex, '0100000000010115b7e9d1f6b8164a0e95544a94f5b0fbfaadc35f8415acd0ec0e58d5ce8c1a1e0100000017160014f90e5bca5635b84bd828064586bd7eb117fee9a90000000002905f0100000000001976a914f7c6c1f9f6142107ed293c8fbf85fbc49eb5f1b988ace00f97000000000017a9146fbf1cee74734503297e46a0db3e3fbb06f2e9d38702483045022100bd687693e57161282a80affb82f18386cbf319bca72ca2c16320b0f3b087bee802205e22a9a16b86628ea08eab83aebec1348c476e9d0c90cd41aa73c47f50d86aab0121039425479ea581ebc7f55959da8c2e1a1063491768860386335dd4630b5eeacfc500000000')
      // now, testing change addess, destination address, amounts & fees...
      let tx = bitcoinjs.Transaction.fromHex(txhex)
      assert.equal(bitcoinjs.address.fromOutputScript(tx.outs[0].script), '1Pb81K1xJnMjUfFgKUbva6gr1HCHXxHVnr')
      assert.equal(bitcoinjs.address.fromOutputScript(tx.outs[1].script), '3Bsssbs4ANCGNETvGLJ3Fvri6SiVnH1fbi')
      assert.equal(tx.outs[0].value, 90000) // 0.0009 because we deducted fee 0.0001
      assert.equal(tx.outs[1].value, 9900000) // 0.099 because 0.1 - 0.001
      done()
    })
github LiskHQ / lisk-desktop / src / utils / validators.js View on Github external
export const validateAddress = (tokenType, address, netCode = 1) => {
  if (address === '') {
    return -1;
  }

  switch (tokenType) {
    // Reference: https://github.com/bitcoinjs/bitcoinjs-lib/issues/890
    case tokenMap.BTC.key:
      try {
        const config = getBtcConfig(netCode);
        bitcoin.address.fromBase58Check(address); // eliminates segwit addresses
        bitcoin.address.toOutputScript(address, config.network);
        return 0;
      } catch (e) {
        return 1;
      }

    case tokenMap.LSK.key:
    default:
      return reg.address.test(address) ? 0 : 1;
  }
};
github blocktrail / blocktrail-sdk-nodejs / lib / services / spv_bridge_bitcoin_service.js View on Github external
results.forEach(function(utxo) {
            var address = utxo['address'];

            if (typeof batchResults[address] === "undefined") {
                batchResults[address] = [];
            }

            batchResults[address].push({
                'hash': utxo['tx_hash'],
                'index': utxo['tx_pos'],
                'value': utxo['value'],
                'script_hex': bitcoin.address.toOutputScript(address, bitcoin.networks.bitcoincash),
                'confirmations': 1 // TODO quickfix
            });
        });
        deferred.resolve(batchResults);
github CoinCulture / course-intro-cryptocurrencies-smart-contracts / btc / p2sh_p2pkh.js View on Github external
const bitcoin = require('bitcoinjs-lib')
const {hexLength, lenPrefixedHex, ops} = require('./util')

var addr = 'mh8tGnF6RCsnWUMTw1WL9UWjjgyMRRTM8t'
var wif = 'cT8gHG8a3gHPBDDLve4A6SKUjTQwNnJ3A3oGjzrqZmXGQJ7dfmQ6'
var txid = 'd3560b97fedfaefee50d188f160c5592586c38bdc7ec3575845c1647d9a44e59'
var txOutput = 0
var amount = 530150000

var keyPair = bitcoin.ECPair.fromWIF(wif, bitcoin.networks.testnet);

var tx = new bitcoin.TransactionBuilder(bitcoin.networks.testnet)
tx.addInput(txid, txOutput)

// decode the b58check encoded address to hex
HASH160 = bitcoin.address.fromBase58Check(addr).hash.toString('hex')

// form the redeem script in hex
// its just a simple p2pkh
redeemScriptHex = ops.OP_DUP + ops.OP_HASH160 + lenPrefixedHex(HASH160) + ops.OP_EQUALVERIFY + ops.OP_CHECKSIG
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")


// note we call addOutput on the inner tx, which takes a scriptPubKey rather than just an addr
tx.addOutput(addr, amount-1000) // test 
tx.tx.addOutput(scriptPubKey, 1000)
tx.sign(0, keyPair);
github blocktrail / wallet-recovery-tool / src / libs / blocktrail-sdk / lib / wallet.js View on Github external
]);

    var multisig = bitcoin.script.multisig.output.encode(2, pubKeys);
    var scriptType = parseInt(path.split("/")[2]);

    var ws, rs;
    if (this.network !== "bitcoincash" && scriptType === Wallet.CHAIN_BTC_SEGWIT) {
        ws = multisig;
        rs = bitcoin.script.witnessScriptHash.output.encode(bitcoin.crypto.sha256(ws));
    } else {
        ws = null;
        rs = multisig;
    }

    var spk = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(rs));
    var addr = bitcoin.address.fromOutputScript(spk, this.network);

    return {
        witnessScript: ws,
        redeemScript: rs,
        scriptPubKey: spk,
        address: addr
    };
};
github blockstack / stacks-wallet / app / vendor / blockstack-trezor / TrezorSigner.js View on Github external
return outputs.map(output => {
      if (btc.script.toASM(output.script).startsWith("OP_RETURN")) {
        const nullData = btc.script.decompile(output.script)[1];
        return {
          op_return_data: nullData.toString("hex"),
          amount: "0",
          script_type: "PAYTOOPRETURN"
        };
      } else {
        const address = bskConfig.network.coerceAddress(
          btc.address.fromOutputScript(output.script)
        );
        return {
          address,
          amount: `${output.value}`,
          script_type: "PAYTOADDRESS"
        };
      }
    });
  }
github chainside / btcnodejs / lib / address.js View on Github external
bc: "mainnet",
    tb: "testnet"
};
SegwitAddress.networks = {
    mainnet: {
        prefix: "bc"
    },
    testnet: {
        prefix: "tb"
    }
};
module.exports = {
    Address,
    SegwitAddress
};
const addr = require("bitcoinjs-lib").address;
const net = require("./network");
const ByteBuffer = require("bytebuffer");
const $ = require("../tools/conversions");
const abtb = require("arraybuffer-to-buffer");
const scripts = require("./scripts");
github mappum / webcoin / src / node.js View on Github external
function addressToItem (address) {
  try {
    return bitcoin.address.fromBase58Check(address).hash
  } catch (err) {}

  try {
    return bitcoin.address.fromBech32(address).data
  } catch (err) {}

  throw Error('Unknown address type')
}
github janoside / btc-rpc-explorer / routes / baseActionsRouter.js View on Github external
var address = req.params.address;

	res.locals.address = address;
	res.locals.limit = limit;
	res.locals.offset = offset;
	res.locals.sort = sort;
	res.locals.paginationBaseUrl = `/address/${address}?sort=${sort}`;
	res.locals.transactions = [];
	res.locals.addressApiSupport = addressApi.getCurrentAddressApiFeatureSupport();
	
	res.locals.result = {};

	try {
		res.locals.addressObj = bitcoinjs.address.fromBase58Check(address);

	} catch (err) {
		if (!err.toString().startsWith("Error: Non-base58 character")) {
			res.locals.pageErrors.push(utils.logError("u3gr02gwef", err));
		}

		try {
			res.locals.addressObj = bitcoinjs.address.fromBech32(address);

		} catch (err2) {
			res.locals.pageErrors.push(utils.logError("u02qg02yqge", err));
		}
	}

	if (global.miningPoolsConfigs) {
		for (var i = 0; i < global.miningPoolsConfigs.length; i++) {
github mappum / webcoin / src / node.js View on Github external
function addressToItem (address) {
  try {
    return bitcoin.address.fromBase58Check(address).hash
  } catch (err) {}

  try {
    return bitcoin.address.fromBech32(address).data
  } catch (err) {}

  throw Error('Unknown address type')
}