Private Key

Represents a bitcoin private key and is needed to be able to spend bitcoin and sign transactions. See the official Bitcoin Wiki for more information about private keys. A PrivateKey in Bitcore is an immutable object that has methods to import and export into a variety of formats including Wallet Import Format.

Instantiate a Private Key

Here is how to create a new private key. It will generate a new random number using window.crypto or the Node.js crypto library.

var privateKey = new PrivateKey();

// Creates a private key from a hexa encoded number
var privateKey2 = new PrivateKey('b221d9dbb083a7f33428d7c2a3c3198ae925614d70210e28716ccaa7cd4ddb79');

To export and import a private key, you can do the following:

// encode into wallet export format
var exported = privateKey.toWIF();

// instantiate from the exported (and saved) private key
var imported = PrivateKey.fromWIF('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m');

Note: The WIF (Wallet Import Format) includes information about the network and if the associated public key is compressed or uncompressed (thus the same bitcoin address will be generated by using this format).

To generate an Address or PublicKey from a PrivateKey:

var publicKey = privateKey.toPublicKey();
var address = publicKey.toAddress(Networks.livenet);

Validating a Private Key

The code to do these validations looks like this:

// validate an address
if (PrivateKey.isValid(input)){
  ...
}

// get the specific validation error that can occurred
var error = PrivateKey.getValidationError(input, Networks.livenet);
if (error) {
  // handle the error
}

PrivateKey

Kind: global class

new PrivateKey(data, network)

Instantiate a PrivateKey from a BN, Buffer and WIF.

Returns: PrivateKey - A new valid instance of an PrivateKey

Param Type Description
data string The encoded data in various formats
network Network | string a Network object, or a string with the network name

Example

// generate a new random key
var key = PrivateKey();

// get the associated address
var address = key.toAddress();

// encode into wallet export format
var exported = key.toWIF();

// instantiate from the exported (and saved) private key
var imported = PrivateKey.fromWIF(exported);

privateKey.toObject ⇒ Object

Kind: instance property of PrivateKey
Returns: Object - A plain object representation

privateKey._classifyArguments(data, network) ⇒ Object

Internal helper to instantiate PrivateKey internal info object from different kinds of arguments passed to the constructor.

Kind: instance method of PrivateKey

Param Type Description
data *
network Network | string a Network object, or a string with the network name

privateKey.toString() ⇒ string

Will output the PrivateKey encoded as hex string

Kind: instance method of PrivateKey

privateKey.toWIF() ⇒ string

Will output the PrivateKey to a WIF string

Kind: instance method of PrivateKey
Returns: string - A WIP representation of the private key

privateKey.toBigNumber() ⇒ BN

Will return the private key as a BN instance

Kind: instance method of PrivateKey
Returns: BN - A BN instance of the private key

privateKey.toBuffer() ⇒ Buffer

Will return the private key as a BN buffer

Kind: instance method of PrivateKey
Returns: Buffer - A buffer of the private key

privateKey.toPublicKey() ⇒ PublicKey

Will return the corresponding public key

Kind: instance method of PrivateKey
Returns: PublicKey - A public key generated from the private key

privateKey.toAddress([network]) ⇒ Address

Will return an address for the private key

Kind: instance method of PrivateKey
Returns: Address - An address generated from the private key

Param Type Description
[network] Network optional parameter specifying the desired network for the address

privateKey.inspect() ⇒ string

Will return a string formatted for the console

Kind: instance method of PrivateKey
Returns: string - Private key

PrivateKey.fromString ⇒ PrivateKey

Instantiate a PrivateKey from a WIF string

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

Param Type Description
str string The WIF encoded private key string

PrivateKey.fromBuffer(arg, network) ⇒ PrivateKey

Instantiate a PrivateKey from a Buffer with the DER or WIF representation

Kind: static method of PrivateKey

Param Type
arg Buffer
network Network

PrivateKey.fromObject(obj)

Instantiate a PrivateKey from a plain JavaScript object

Kind: static method of PrivateKey

Param Type Description
obj Object The output from privateKey.toObject()

PrivateKey.fromRandom([network]) ⇒ PrivateKey

Instantiate a PrivateKey from random bytes

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

Param Type Description
[network] string Either "livenet" or "testnet"

PrivateKey.getValidationError(data, [network]) ⇒ null | Error

Check if there would be any errors when initializing a PrivateKey

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

Param Type Description
data string The encoded data in various formats
[network] string Either "livenet" or "testnet"

PrivateKey.isValid(data, [network]) ⇒ Boolean

Check if the parameters are valid

Kind: static method of PrivateKey
Returns: Boolean - If the private key is would be valid

Param Type Description
data string The encoded data in various formats
[network] string Either "livenet" or "testnet"