Example #1
0
/**
 * Converts string to hash and to an address
 * @param  {String}  hash         The string to be hashed
 * @param  {boolean} uncompressed Uncompressed or not
 * @return {Object}               Public Key Address
 */
function hashToAddr(hash, uncompressed) {

  var value = new Buffer(hash);
  var h = bitcore.crypto.Hash.sha256(value);
  var bn = bitcore.crypto.BN.fromBuffer(h);
  var privateKey = new bitcore.PrivateKey(bn);


  var wif = bitcore.PrivateKey(bn).toWIF();

  var publicKey1 = new bitcore.PublicKey(privateKey);

  var pointx = publicKey1.point.x.toString('hex');
  var pointy = publicKey1.point.y.toString('hex');

  //padding left
  pointx = String("00000000" + pointx).slice(-64);
  pointy = String("00000000" + pointy).slice(-64);

  var publicKey2 = new bitcore.PublicKey('04' + pointx + pointy);

  var addr1 = publicKey1.toAddress();
  var addr2 = publicKey2.toAddress();

  if (uncompressed) {
    return addr2;
  } else {
    return addr1;
  }

}
CWPrivateKey.prototype.init = function(priv) {
  try {
    if (typeof priv === "string") {
      priv = bitcore.PrivateKey(priv, NETWORK);
    }
    this.priv = priv;
  } catch (err) {
    this.priv = null;
  }
}
Example #3
0
    this.sign = function(hash, privateKey, hashEncoding) {
      if (!hashEncoding)
        hashEncoding = 'base64';

      if (typeof hash == 'string')
        hash = new Buffer(hash, hashEncoding);

      if (typeof privateKey == 'string')
        privateKey = bitcore.PrivateKey(Buffer(privateKey, 'base64').toString('hex'));

      return bitcore.crypto.ECDSA.sign(hash, privateKey).toBuffer().toString('base64');
    };
CWPrivateKey.prototype.getAltAddress = function() {
  var tmpPriv = this.priv.toObject();
  tmpPriv.compressed = !tmpPriv.compressed;

  return bitcore.PrivateKey(tmpPriv).toAddress(NETWORK).toString();
}
Example #5
0
var BN = bitcore.crypto.BN;
var async = require('async');
var rimraf = require('rimraf');
var bitcoind;

/* jshint unused: false */
var should = chai.should();
var assert = chai.assert;
var sinon = require('sinon');
var BitcoinRPC = require('bitcoind-rpc');
var transactionData = [];
var blockHashes = [];
var utxos;
var client;
var coinbasePrivateKey;
var privateKey = bitcore.PrivateKey();
var destKey = bitcore.PrivateKey();

describe('Bitcoind Functionality', function() {

  before(function(done) {
    this.timeout(60000);

    // Add the regtest network
    bitcore.Networks.enableRegtest();
    var regtestNetwork = bitcore.Networks.get('regtest');

    var datadir = __dirname + '/data';

    rimraf(datadir + '/regtest', function(err) {
Example #6
0
/**
 * Returns in wif format the privateKey provided.
 *
 * @param {string} priv
 * @returns {string}
 */
function privToWif(priv) {
    return bitcore.PrivateKey(priv).toWIF();
}
Example #7
0
// This script assumes that it is being used in conjunction with a fresh regtest
// environment that was setup with the accompanying freeze-regtest.sh script.

'use strict';

const LOCK_UNTIL_BLOCK = 150; // pick a block height above the current tip

const args = require('./args-regtest.js');
const bitcore = require('bitcore-lib');

bitcore.Networks.defaultNetwork = bitcore.Networks.testnet; // works for regtest

// note: using a fixed private key of 1 for ease of demonstration only.
// address = mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r
// privKey WIF = cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87JcbXMTcA
const privKey = bitcore.PrivateKey(bitcore.crypto.BN.One);

const redeemScript = bitcore.Script.empty()
  // useful generic way to get the minimal encoding of the locktime stack argument
  .add(bitcore.crypto.BN.fromNumber(LOCK_UNTIL_BLOCK).toScriptNumBuffer())
  .add('OP_CHECKLOCKTIMEVERIFY').add('OP_DROP')
  .add(bitcore.Script.buildPublicKeyHashOut(privKey.toAddress()));

// address = 2MxEcV5dx8CsDAByqN5PbB3NPmhyBkNLHVD
const p2shAddress = bitcore.Address.payingTo(redeemScript);

const freezeTransaction = new bitcore.Transaction().from({
  txid: args.txid, // tran id of an utxo associated with the privKey's address
  vout: Number(args.vout), // output index of the utxo within the transaction with id = txid
  scriptPubKey: args.scriptPubKey, // scriptPubKey of the utxo txid[vout]
  satoshis: Number(args.satoshis), // value of the utxo txid[vout]
Example #8
0
'use strict';

const LOCK_UNTIL_BLOCK = 150; // pick a block height above the current tip

const args = require('./args-regtest.js');
const bitcore = require('bitcore-lib');

bitcore.Networks.defaultNetwork = bitcore.Networks.testnet; // works for regtest

// use a compressed format brain wallet key for ease of testing. doing so yields
// fixed addresses.

// address = mkESjLZW66TmHhiFX8MCaBjrhZ543PPh9a
// privKey WIF = cP3voGKJHVSrUsEdrj8HnrpwLNgNngrgijMyyyRowRo15ZattbHm
const alice = bitcore.PrivateKey(bitcore.crypto.Hash.sha256(Buffer.from('alice', 'utf8')).toString('hex'));

// address = mrsU9wTxs1UB4wqWZbuA6Nd6uYxs2VKe8T
const bob = bitcore.PrivateKey(bitcore.crypto.Hash.sha256(Buffer.from('bob', 'utf8')).toString('hex'));

const redeemScript = bitcore.Script.empty()
  .add('OP_IF')
    .add(bob.toPublicKey().toBuffer()).add('OP_CHECKSIGVERIFY')
  .add('OP_ELSE')
    // useful generic way to get the minimal encoding of the locktime stack argument
    .add(bitcore.crypto.BN.fromNumber(LOCK_UNTIL_BLOCK).toScriptNumBuffer())
    .add('OP_CHECKLOCKTIMEVERIFY').add('OP_DROP')
  .add('OP_ENDIF')
  .add(alice.toPublicKey().toBuffer()).add('OP_CHECKSIG');

const p2shAddress = bitcore.Address.payingTo(redeemScript);
Example #9
0
  before(function(done) {
    this.timeout(30000);

    var datadir = __dirname + '/data';

    testKey = bitcore.PrivateKey(testWIF);

    rimraf(datadir + '/regtest', function(err) {

      if (err) {
        throw err;
      }

      var configuration = {
        datadir: datadir,
        network: 'regtest',
        services: [
          {
            name: 'db',
            module: DBService,
            config: {}
          },
          {
            name: 'bitcoind',
            module: BitcoinService,
            config: {}
          },
          {
            name: 'address',
            module: AddressService,
            config: {}
          }
        ]
      };

      node = new BitcoreNode(configuration);

      regtest = bitcore.Networks.get('regtest');
      should.exist(regtest);

      node.on('error', function(err) {
        log.error(err);
      });

      node.on('ready', function() {

        client = new BitcoinRPC({
          protocol: 'https',
          host: '127.0.0.1',
          port: 18332,
          user: '******',
          pass: '******',
          rejectUnauthorized: false
        });

        var syncedHandler = function() {
          if (node.services.db.tip.__height === 150) {
            node.removeListener('synced', syncedHandler);
            done();
          }
        };

        node.on('synced', syncedHandler);

        client.generate(150, function(err, response) {
          if (err) {
            throw err;
          }
        });
      });

      node.start(function(err) {
        if (err) {
          throw err;
        }
      });


    });
  });