How to use the bitcoinjs-lib.ECPair 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 flow-typed / flow-typed / definitions / npm / bitcoinjs-lib_v2.x.x / test_bitcoinjs-lib_v2.x.x.js View on Github external
var Gx = new BigInteger('1');
var Gy = new BigInteger('1');
var n = new BigInteger('1');
var h = new BigInteger('1');
var p: Point = new Curve(pi, a, b, Gx, Gy, n, h).G;

(new ECPair(b): ECPair);
(new ECPair(null, p): ECPair);

// $ExpectError
(new ECPair(new Buffer(1)): ECPair);
// $ExpectError
(new ECPair(null, new Buffer(1)): ECPair);

// $ExpectError
(ECPair(new Buffer(1)): ECPair);

(ECPair.fromPublicKeyBuffer(new Buffer(1)): ECPair);

var pair = ECPair.makeRandom();
(pair.verify(new Buffer(1), new ECSignature(new BigInteger('1'), new BigInteger('1'))): boolean);

// $ExpectError
(pair.verify(new Buffer(1)): boolean);
// $ExpectError
(pair.verify(new Buffer(1), new Buffer(1)): boolean);

(new HDNode(pair, new Buffer(1)): HDNode);

// $ExpectError
(new HDNode(pair): HDNode);
github bigearth / bitbox-cli / src / Stealth.js View on Github external
scanDual (scan, receiver, sender) {
    let d = scan.d;
    let R = receiver.Q;
    let eG = sender.Q;
    let eQ = eG.multiply(d) // shared secret
    let c = bigi.fromBuffer(Bitcoin.crypto.sha256(eQ.getEncoded()));
    let cG = G.multiply(c);
    let vG = new Bitcoin.ECPair(null, R.add(cG));
    return vG;
  }
github blockstack / blockstack-browser / app / js / utils / profile-utils.js View on Github external
}
  if (!payload.hasOwnProperty('issuer')) {
    throw new Error('Token doesn\'t have an issuer')
  }
  if (!payload.issuer.hasOwnProperty('publicKey')) {
    throw new Error('Token doesn\'t have an issuer public key')
  }
  if (!payload.hasOwnProperty('claim')) {
    throw new Error('Token doesn\'t have a claim')
  }

  const issuerPublicKey = payload.issuer.publicKey
  const publicKeyBuffer = new Buffer(issuerPublicKey, 'hex')

  const Q = ecurve.Point.decodeFrom(secp256k1, publicKeyBuffer)
  const compressedKeyPair = new ECKeyPair(null, Q, { compressed: true })
  const compressedAddress = compressedKeyPair.getAddress()
  const uncompressedKeyPair = new ECKeyPair(null, Q, { compressed: false })
  const uncompressedAddress = uncompressedKeyPair.getAddress()

  if (verifyingKeyOrAddress === issuerPublicKey) {
    // pass
  } else if (verifyingKeyOrAddress === compressedAddress) {
    // pass
  } else if (verifyingKeyOrAddress === uncompressedAddress) {
    // pass
  } else {
    throw new Error('Token issuer public key does not match the verifying value')
  }

  const tokenVerifier = new TokenVerifier(decodedToken.header.alg, issuerPublicKey)
  if (!tokenVerifier) {
github blockstack / blockstack.js / src / tokenVerifying.es6 View on Github external
}
  if (!payload.hasOwnProperty('issuer')) {
    throw new Error("Token doesn't have an issuer")
  }
  if (!payload.issuer.hasOwnProperty('publicKey')) {
    throw new Error("Token doesn't have an issuer public key")    
  }
  if (!payload.hasOwnProperty('claim')) {
    throw new Error("Token doesn't have a claim")
  }

  const issuerPublicKey = payload.issuer.publicKey,
        publicKeyBuffer = new Buffer(issuerPublicKey, 'hex')

  const Q = ecurve.Point.decodeFrom(secp256k1, publicKeyBuffer),
        compressedKeyPair = new ECKeyPair(null, Q, { compressed: true }),
        compressedAddress = compressedKeyPair.getAddress(),
        uncompressedKeyPair = new ECKeyPair(null, Q, { compressed: false }),
        uncompressedAddress = uncompressedKeyPair.getAddress()

  if (verifyingKeyOrAddress === issuerPublicKey) {
    // pass
  } else if (verifyingKeyOrAddress === compressedAddress) {
    // pass
  } else if (verifyingKeyOrAddress === uncompressedAddress) {
    // pass
  } else {
    throw new Error("Token issuer public key does not match the verifying value")
  }

  let tokenVerifier = new TokenVerifier(decodedToken.header.alg, issuerPublicKey)
  if (!tokenVerifier) {
github blockstack / blockstack-browser / app / js / utils / profile-utils.js View on Github external
throw new Error('Token doesn\'t have an issuer')
  }
  if (!payload.issuer.hasOwnProperty('publicKey')) {
    throw new Error('Token doesn\'t have an issuer public key')
  }
  if (!payload.hasOwnProperty('claim')) {
    throw new Error('Token doesn\'t have a claim')
  }

  const issuerPublicKey = payload.issuer.publicKey
  const publicKeyBuffer = new Buffer(issuerPublicKey, 'hex')

  const Q = ecurve.Point.decodeFrom(secp256k1, publicKeyBuffer)
  const compressedKeyPair = new ECKeyPair(null, Q, { compressed: true })
  const compressedAddress = compressedKeyPair.getAddress()
  const uncompressedKeyPair = new ECKeyPair(null, Q, { compressed: false })
  const uncompressedAddress = uncompressedKeyPair.getAddress()

  if (verifyingKeyOrAddress === issuerPublicKey) {
    // pass
  } else if (verifyingKeyOrAddress === compressedAddress) {
    // pass
  } else if (verifyingKeyOrAddress === uncompressedAddress) {
    // pass
  } else {
    throw new Error('Token issuer public key does not match the verifying value')
  }

  const tokenVerifier = new TokenVerifier(decodedToken.header.alg, issuerPublicKey)
  if (!tokenVerifier) {
    throw new Error('Invalid token verifier')
  }
github blockchain / My-Wallet-V3 / tests / helpers.spec.js View on Github external
parseBIP38toECPair (b58, pass, succ, wrong, error) {
    if (ImportExport.shouldResolve) {
      succ(new Bitcoin.ECPair(BigInteger.fromByteArrayUnsigned(BigInteger.fromBuffer(new Buffer('E9873D79C6D87DC0FB6A5778633389F4453213303DA61F20BD67FC233AA33262', 'hex')).toByteArray()), null, {compressed: true}));
    } else if (ImportExport.shouldReject) {
      wrong();
    } else if (ImportExport.shouldFail) {
      error();
    }
  }
};
github sakurity / securelogin / js / index.js View on Github external
generate: function(s){ 
      return new bitcoin.ECPair(bigi.fromBuffer(s),false,{network: bitcoin.networks.testnet}).getAddress() 
    }
  },
github blockchain / My-Wallet-V3 / src / payment.js View on Github external
function getKey (priv, addr) {
  var format = Helpers.detectPrivateKeyFormat(priv);
  var key = Helpers.privateKeyStringToKey(priv, format);
  var network = constants.getNetwork();
  var ckey = new Bitcoin.ECPair(key.d, null, {compressed: true, network: network});
  var ukey = new Bitcoin.ECPair(key.d, null, {compressed: false, network: network});
  if (ckey.getAddress() === addr) {
    return ckey;
  } else if (ukey.getAddress() === addr) {
    return ukey;
  }
  return key;
}
github blockchain / My-Wallet-V3 / src / import-export.js View on Github external
verifyHashAndReturn();
    } else {
      var ownerentropy = hex.slice(7, 7 + 8);
      var ownersalt = Buffer(!hasLotSeq ? ownerentropy : ownerentropy.slice(0, 4));

      var prefactorA = WalletCrypto.scrypt(passphrase, ownersalt, 16384, 8, 8, 32);
      var passfactor;

      if (!hasLotSeq) {
        passfactor = prefactorA;
      } else {
        var prefactorB = Buffer.concat([prefactorA, Buffer(ownerentropy)]);
        passfactor = hash256(prefactorB);
      }

      var kp = new Bitcoin.ECPair(BigInteger.fromBuffer(passfactor), null, {network: constants.getNetwork()});

      var passpoint = kp.getPublicKeyBuffer();

      var encryptedpart2 = Buffer(hex.slice(23, 23 + 16));

      var addresshashplusownerentropy = Buffer(hex.slice(3, 3 + 12));

      var derived = WalletCrypto.scrypt(passpoint, addresshashplusownerentropy, 1024, 1, 1, 64);
      let k = derived.slice(32);

      var unencryptedpart2Bytes = WalletCrypto.AES.decrypt(encryptedpart2, k, null, AESopts);

      for (var i = 0; i < 16; i++) { unencryptedpart2Bytes[i] ^= derived[i + 16]; }

      var encryptedpart1 = Buffer.concat([Buffer(hex.slice(15, 15 + 8)), Buffer(unencryptedpart2Bytes.slice(0, 0 + 8))]);
github blockstack-packages / blockstack-keychains-js / src / derivation.es6 View on Github external
function getChildPublicKeypair(parentKeypair, entropyBuffer) {
  if (!parentKeypair.Q) {
    throw new TypeError('Parent keypair must have a public key')
  }

  const childKeyMultiplier = getChildKeyMultiplier(parentKeypair, entropyBuffer),
        parentPoint = parentKeypair.Q,
        childPoint = secp256k1.G.multiply(childKeyMultiplier).add(parentPoint)

  if (secp256k1.isInfinity(childPoint)) {
    throw new TypeError('Entropy is resulting in an invalid child public key')
  }

  return new ECPair(null, childPoint, {})
}