Public Key

Represents a bitcoin public key and is needed to be able to receive bitcoin, as is usually represented as a bitcoin Address. See the official Bitcoin Wiki.

A PublicKey in Bitcore is an immutable object and can be instantiated from a Point, string, PrivateKey, Buffer or a BN.

Instantiate a Public Key

Here is how to instantiate a public key:


var privateKey = new PrivateKey();

// from a private key
var publicKey = new PublicKey(privateKey);

// from a der hex encoded string
var publicKey2 = new PublicKey('02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc');

Validating a Public Key

A public key point should be on the secp256k1 curve, instantiating a new PublicKey will validate this and will throw an error if it's invalid. To check that a public key is valid:

if (PublicKey.isValid('02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc')){
  // valid public key
}

Compressed vs Uncompressed

It's important to note that there are two possible ways to represent a public key. The standard is compressed and includes the X value and parity (as represented above in the documentation). There is also a longer version that is uncompressed which includes both X and Y values. Using this encoding will generate a different bitcoin address, so be careful when selecting the encoding. Uncompressed public keys start with 0x04; compressed public keys begin with 0x03 or 0x02 depending on whether they're greater or less than the midpoint of the curve. These prefix bytes are all used in official secp256k1 documentation.

Example:

> var bitcore = require('bitcore');

// compressed public key starting with 0x03 (greater than midpoint of curve)
> var compressedPK = bitcore.PublicKey('030589ee559348bd6a7325994f9c8eff12bd'+
    '5d73cc683142bd0dd1a17abc99b0dc');
> compressedPK.compressed;
true
> compressedPK.toAddress().toString();
'1KbUJ4x8epz6QqxkmZbTc4f79JbWWz6g37'
// compressed public key starting with 0x02 (smaller than midpoint of curve)
> var compressedPK2 = new bitcore.PublicKey('02a1633cafcc01ebfb6d78e39f687a1f'+
    '0995c62fc95f51ead10a02ee0be551b5dc');
> compressedPK2.compressed;
true
> compressedPK.toAddress().toString();
'1KbUJ4x8epz6QqxkmZbTc4f79JbWWz6g37'
// uncompressed public key, starting with 0x04. Contains both X and Y encoded
> var uncompressed = bitcore.PublicKey('0479BE667EF9DCBBAC55A06295CE870B07029'+
    'BFCDB2DCE28D959F2815B16F81798483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68'+
    '554199C47D08FFB10D4B8');
> uncompressed.compressed
false
> uncompressed.toAddress().toString()
'1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm'

PublicKey

Kind: global class

new PublicKey(data, extra)

Instantiate a PublicKey from a PrivateKey, Point, string, or Buffer.

There are two internal properties, network and compressed, that deal with importing a PublicKey from a PrivateKey in WIF format. More details described on PrivateKey

Returns: PublicKey - A new valid instance of an PublicKey

Param Type Description
data string The encoded data in various formats
extra Object additional options
[extra.network] Network Which network should the address for this public key be for
[extra.compressed] String If the public key is compressed

Example

// instantiate from a private key
var key = PublicKey(privateKey, true);

// export to as a DER hex encoded string
var exported = key.toString();

// import the public key
var imported = PublicKey.fromString(exported);

publicKey.toObject ⇒ Object

Kind: instance property of PublicKey
Returns: Object - A plain object of the PublicKey

publicKey.toBuffer ⇒ Buffer

Will output the PublicKey to a DER Buffer

Kind: instance property of PublicKey
Returns: Buffer - A DER hex encoded buffer

publicKey._classifyArgs(data, extra)

Internal function to differentiate between arguments passed to the constructor

Kind: instance method of PublicKey

Param Type
data *
extra Object

publicKey._getID() ⇒ Buffer

Will return a sha256 + ripemd160 hash of the serialized public key

Kind: instance method of PublicKey
See: https://github.com/bitcoin/bitcoin/blob/master/src/pubkey.h#L141

publicKey.toAddress(network) ⇒ Address

Will return an address for the public key

Kind: instance method of PublicKey
Returns: Address - An address generated from the public key

Param Type Description
network String | Network Which network should the address be for

publicKey.toString() ⇒ string

Will output the PublicKey to a DER encoded hex string

Kind: instance method of PublicKey
Returns: string - A DER hex encoded string

publicKey.inspect() ⇒ string

Will return a string formatted for the console

Kind: instance method of PublicKey
Returns: string - Public key

PublicKey.fromDER ⇒ PublicKey

Instantiate a PublicKey from a Buffer

Kind: static property of PublicKey
Returns: PublicKey - A new valid instance of PublicKey

Param Type Description
buf Buffer A DER hex buffer
[strict] bool if set to false, will loosen some conditions

PublicKey.fromPrivateKey(privkey) ⇒ PublicKey

Instantiate a PublicKey from a PrivateKey

Kind: static method of PublicKey
Returns: PublicKey - A new valid instance of PublicKey

Param Type Description
privkey PrivateKey An instance of PrivateKey

PublicKey.fromPoint(point, [compressed]) ⇒ PublicKey

Instantiate a PublicKey from a Point

Kind: static method of PublicKey
Returns: PublicKey - A new valid instance of PublicKey

Param Type Description
point Point A Point instance
[compressed] boolean whether to store this public key as compressed format

PublicKey.fromString(str, [encoding]) ⇒ PublicKey

Instantiate a PublicKey from a DER hex encoded string

Kind: static method of PublicKey
Returns: PublicKey - A new valid instance of PublicKey

Param Type Description
str string A DER hex string
[encoding] String The type of string encoding

PublicKey.fromX(odd, x) ⇒ PublicKey

Instantiate a PublicKey from an X Point

Kind: static method of PublicKey
Returns: PublicKey - A new valid instance of PublicKey

Param Type Description
odd Boolean If the point is above or below the x axis
x Point The x point

PublicKey.getValidationError(data) ⇒ null | Error

Check if there would be any errors when initializing a PublicKey

Kind: static method of PublicKey
Returns: null | Error - An error if exists

Param Type Description
data string The encoded data in various formats

PublicKey.isValid(data) ⇒ Boolean

Check if the parameters are valid

Kind: static method of PublicKey
Returns: Boolean - If the public key would be valid

Param Type Description
data string The encoded data in various formats