How to use the bitcoinjs-lib.ECPair.fromPrivateKey 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 MichaelFedora / unofficial-blockstack-extension / src / common / util.ts View on Github external
export function makeAssociationToken(appPrivateKey: string, identityKey: string): string {

  const appPublicKey = ECPair.fromPrivateKey(Buffer.from(appPrivateKey, 'hex'), { compressed: true }).publicKey.toString('hex');
  const identityPublicKey = ECPair.fromPrivateKey(Buffer.from(identityKey, 'hex'), { compressed: true }).publicKey.toString('hex');

  const FOUR_MONTH_SECONDS = 60 * 60 * 24 * 31 * 4; // apparently a standard
  const salt = randomBytes(16).toString('hex');

  const associationTokenClaim = {
    childToAssociate: appPublicKey,
    iss: identityPublicKey,
    exp: FOUR_MONTH_SECONDS + (new Date().getTime() / 1000),
    salt
  };

  const associationToken = new TokenSigner('ES256K', identityKey).sign(associationTokenClaim);

  return associationToken;
}
github MichaelFedora / unofficial-blockstack-extension / src / common / data / wrapped-node.ts View on Github external
constructor(node: BIP32Interface) {
    if(node instanceof WrappedNode)
      this._node = node.node;
    else
      this._node = node;
    if(this._node.privateKey)
      // @ts-ignore
      this._keyPair = ECPair.fromPrivateKey(this._node.privateKey);
    else if(this._node.publicKey)
      // @ts-ignore
      this._keyPair = ECPair.fromPublicKey(this._node.publicKey);
    else
      this._keyPair = null;
  }
github blockstack / blockstack.js / src / wallet.ts View on Github external
function getNodePrivateKey(node: BIP32Interface): string {
  return ecPairToHexString(ECPair.fromPrivateKey(node.privateKey))
}
github blockstack / blockstack.js / src / utils.ts View on Github external
export function hexStringToECPair(skHex: string): ECPair.ECPairInterface {
  const ecPairOptions = {
    network: config.network.layer1,
    compressed: true
  }

  if (skHex.length === 66) {
    if (skHex.slice(64) !== '01') {
      throw new Error('Improperly formatted private-key hex string. 66-length hex usually '
                      + 'indicates compressed key, but last byte must be == 1')
    }
    return ECPair.fromPrivateKey(Buffer.from(skHex.slice(0, 64), 'hex'), ecPairOptions)
  } else if (skHex.length === 64) {
    ecPairOptions.compressed = false
    return ECPair.fromPrivateKey(Buffer.from(skHex, 'hex'), ecPairOptions)
  } else {
    throw new Error('Improperly formatted private-key hex string: length should be 64 or 66.')
  }
}
github blockstack / blockstack.js / src / keys.ts View on Github external
export function getPublicKeyFromPrivate(privateKey: string) {
  const keyPair = ECPair.fromPrivateKey(Buffer.from(privateKey, 'hex'))
  return keyPair.publicKey.toString('hex')
}
github blockstack / blockstack.js / src / storage / hub.ts View on Github external
export async function getBucketUrl(gaiaHubUrl: string, appPrivateKey: string): Promise {
  const challengeSigner = ECPair.fromPrivateKey(Buffer.from(appPrivateKey, 'hex'))
  const response = await fetchPrivate(`${gaiaHubUrl}/hub_info`)
  const responseText = await response.text()
  const responseJSON = JSON.parse(responseText)
  const readURL = responseJSON.read_url_prefix
  const address = ecPairToAddress(challengeSigner)
  const bucketUrl = `${readURL}${address}/`
  return bucketUrl
}