How to use the bitcore-lib.crypto function in bitcore-lib

To help you get started, we’ve selected a few bitcore-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 dogethereum / dogethereum-contracts / test / utils.js View on Github external
operatorSignItsEthAddress: function(operatorPrivateKeyString, operatorEthAddress) {
      // bitcoreLib.PrivateKey marks the private key as compressed if it receives a String as a parameter.
      // bitcoreLib.PrivateKey marks the private key as uncompressed if it receives a Buffer as a parameter.
      // In fact, private keys are not compressed/uncompressed. The compressed/uncompressed attribute
      // is used when generating a compressed/uncompressed public key from the private key.
      // Ethereum addresses are first 20 bytes of keccak256(uncompressed public key)
      // Dogecoin public key hashes are calculated: ripemd160((sha256(compressed public key));
      const operatorPrivateKeyCompressed = bitcoreLib.PrivateKey(module.exports.remove0x(operatorPrivateKeyString));
      const operatorPrivateKeyUncompressed = bitcoreLib.PrivateKey(module.exports.fromHex(operatorPrivateKeyString))
      const operatorPublicKeyCompressedString = "0x" + operatorPrivateKeyCompressed.toPublicKey().toString();

      // Generate the msg to be signed: double sha256 of operator eth address
      const operatorEthAddressHash = bitcoreLib.crypto.Hash.sha256sha256(module.exports.fromHex(operatorEthAddress));

      // Operator private key uncompressed sign msg
      var ecdsa = new ECDSA();
      ecdsa.hashbuf = operatorEthAddressHash;
      ecdsa.privkey = operatorPrivateKeyUncompressed;
      ecdsa.pubkey = operatorPrivateKeyUncompressed.toPublicKey();
      ecdsa.signRandomK();
      ecdsa.calci();
      var ecdsaSig = ecdsa.sig;
      var signature = "0x" + ecdsaSig.toCompact().toString('hex');
      return [operatorPublicKeyCompressedString, signature];
  },
  forgeDogeBlockHeader,
github bitpay / bitcore-mnemonic / packages / bitcore-wallet-client / lib / credentials.js View on Github external
function walletPrivKeyFromOldCopayWallet(w) {
    // IN BWS, the master Pub Keys are not sent to the server, 
    // so it is safe to use them as seed for wallet's shared secret.
    var seed = w.publicKeyRing.copayersExtPubKeys.sort().join('');
    var seedBuf = new Buffer(seed);
    var privKey = new Bitcore.PrivateKey.fromBuffer(Bitcore.crypto.Hash.sha256(seedBuf));
    return privKey.toString();
  };
github bitpay / bitcore / packages / bitcore-payment-protocol / lib / common.js View on Github external
'use strict';

var bitcore = require('bitcore-lib');
var protobufjs = require('protobufjs/dist/protobuf');
var RootCerts = require('./rootcerts');
var rfc5280 = require('asn1.js-rfc5280');

var PublicKey = bitcore.PublicKey;
var PrivateKey = bitcore.PrivateKey;
var Signature = bitcore.crypto.Signature;
var ECDSA = bitcore.crypto.ECDSA;
var sha256sha256 = bitcore.crypto.Hash.sha256sha256;
var varintBufNum = bitcore.encoding.BufferWriter.varintBufNum;

// BIP 70 - payment protocol
function PaymentProtocol(currency) {
  this.messageType = null;
  this.message = null;
  this.currency = currency || 'BTC';
}

PaymentProtocol.PAYMENT_REQUEST_MAX_SIZE = 50000;
PaymentProtocol.PAYMENT_MAX_SIZE = 50000;
PaymentProtocol.PAYMENT_ACK_MAX_SIZE = 60000;

PaymentProtocol.JSON_PAYMENT_REQUEST_CONTENT_TYPE = 'application/payment-request';
PaymentProtocol.JSON_PAYMENT_CONTENT_TYPE = 'application/payment';
PaymentProtocol.JSON_PAYMENT_ACK_CONTENT_TYPE = 'application/payment-ack';
github bitpay / bitcore-channel / test / consumer.js View on Github external
sequenceNumber: 0xffffffff
      };

      var spendingCommitmentTx = Consumer.createChannelTransaction(opts);
      var scriptSig = spendingCommitmentTx.inputs[0].script;
      var scriptPubKey = commitmentTx.outputs[0].script;
      var interpreter = new Interpreter();
      var res = interpreter.verify(scriptSig, scriptPubKey, spendingCommitmentTx, 0, flags);
      res.should.be.false;
      //this also means that the provider's key was not supplied and therefore did not sign
      interpreter.errstr.should.equal('SCRIPT_ERR_UNSATISFIED_LOCKTIME');
      //this ensures that this transaction is a true 2 of 2, protecting both consumer and provider
      spendingCommitmentTx.inputs[0].sequenceNumber.should.equal(0xffffffff);
      spendingCommitmentTx.nLockTime.should.equal(0);
      var sh = bitcore.Transaction.sighash;
      var sig = bitcore.crypto.Signature.fromTxFormat(spendingCommitmentTx.inputs[0].script.chunks[0].buf);
      sh.verify(spendingCommitmentTx, sig, consumerPrivKey.publicKey, 0, scriptPubKey);
    });
github poetapp / node / src / Helpers / Claim.ts View on Github external
export function isValidSignature(claim: Claim): boolean {
  try {
    return bitcore.crypto.ECDSA.verify(
      Buffer.from(claim.id, 'hex'),
      bitcore.crypto.Signature.fromString(claim.signature),
      new bitcore.PublicKey(claim.publicKey)
    )
  } catch (exception) {
    console.log(JSON.stringify({
      message: 'Exception caught while attempting to verify signature.',
      claim,
      exception
    }, null, 2))
    return false
  }
}
github poetapp / poet-js / src / Claim.ts View on Github external
export const getClaimSignature = async (claim: Claim, privateKey: string): Promise => {
  if (!claim.publicKey) throw new IllegalArgumentException('Cannot sign a claim that has an empty .publicKey field.')
  if (new bitcore.PrivateKey(privateKey).publicKey.toString() !== claim.publicKey)
    throw new IllegalArgumentException(
      "Cannot sign this claim with the provided privateKey. It doesn\t match the claim's public key."
    )
  const generatedClaimId = await getClaimId(claim)
  if (!claim.id) throw new IllegalArgumentException('Cannot sign a claim that has an empty .id field.')
  if (claim.id !== generatedClaimId)
    throw new IllegalArgumentException('Cannot sign a claim whose id has been altered or generated incorrectly.')

  const signature = bitcore.crypto.ECDSA.sign(Buffer.from(claim.id, 'hex'), new bitcore.PrivateKey(privateKey))
  return signature.toString()
}
github mappum / webcoin / lib / blockchain.js View on Github external
var EventEmitter = require('events').EventEmitter
var util = require('util')
var async = require('async')
var bitcore = require('bitcore-lib')
var Inventory = require('bitcore-p2p').Inventory
var BN = bitcore.crypto.BN
var constants = require('./constants.js')
var BlockStore = require('./blockStore.js')
var u = require('./utils.js')

if (process.browser) {
  require('setimmediate')
}

function noop () {}

var TIME_MARGIN = 2 * 60 * 60
var storeClosedError = new Error('Store is closed')

var Blockchain = module.exports = function (opts) {
  var self = this
  opts = opts || {}
github bitpay / bitcore / packages / bitcore-wallet-client / src / lib / paypro.ts View on Github external
var hashbuf = Buffer.from(hash, 'hex');
    let sigbuf = Buffer.from(signature, 'hex');

    let s_r = Buffer.alloc(32);
    let s_s = Buffer.alloc(32);

    sigbuf.copy(s_r, 0, 0);
    sigbuf.copy(s_s, 0, 32);

    let s_rBN = Bitcore.crypto.BN.fromBuffer(s_r);
    let s_sBN = Bitcore.crypto.BN.fromBuffer(s_s);

    let pub = Bitcore.PublicKey.fromString(keyData.publicKey);

    let sig = new Bitcore.crypto.Signature();
    sig.set({ r: s_rBN, s: s_sBN });

    let valid = Bitcore.crypto.ECDSA.verify(
      hashbuf,
      sig,
      pub
    );

    if (!valid) {
      return callback(new Error('Response signature invalid'));
    }

    return callback(null, keyData.owner);
  }
github bitpay / bitcore-wallet-client / lib / api.js View on Github external
API.prototype.decryptBIP38PrivateKey = function(encryptedPrivateKeyBase58, passphrase, opts, cb) {
  var Bip38 = require('bip38');
  var bip38 = new Bip38();

  var privateKeyWif;
  try {
    privateKeyWif = bip38.decrypt(encryptedPrivateKeyBase58, passphrase);
  } catch (ex) {
    return cb(new Error('Could not decrypt BIP38 private key', ex));
  }

  var privateKey = new Bitcore.PrivateKey(privateKeyWif);
  var address = privateKey.publicKey.toAddress().toString();
  var addrBuff = new Buffer(address, 'ascii');
  var actualChecksum = Bitcore.crypto.Hash.sha256sha256(addrBuff).toString('hex').substring(0, 8);
  var expectedChecksum = Bitcore.encoding.Base58Check.decode(encryptedPrivateKeyBase58).toString('hex').substring(6, 14);

  if (actualChecksum != expectedChecksum)
    return cb(new Error('Incorrect passphrase'));

  return cb(null, privateKeyWif);
};
github bitpay / bitcore-channel / lib / consumer.js View on Github external
'use strict';

var bitcore = require('bitcore-lib');
var Address = bitcore.Address;
var Hash = bitcore.crypto.Hash;
var PrivateKey = bitcore.PrivateKey;
var Output = bitcore.Transaction.Output;
var Signature = bitcore.crypto.Signature;
var Transaction = require('./transaction/transaction');
var Script = require('./transaction/script');
var utils = require('./utils');

var createChannelTransaction = function(opts) {

  var network = opts.network || 'livenet';

  if (! opts.consumerPrivateKey) {
    throw new Error('consumer private key needed for this transaction');
  }
  if (!opts.satoshis) {
    throw new Error('bitcoin amount in satoshis is needed');