new Address(data, network, type) → {Address}
Instantiate an address from an address String or Buffer, a public key or script hash Buffer, or an instance of PublicKey or Script.
This is an immutable class, and if the first parameter provided to this constructor is an
Address instance, the same argument will be returned.
An address has two key properties: network and type. The type is either
Address.PayToPublicKeyHash (value is the 'pubkeyhash' string)
or Address.PayToScriptHash (the string 'scripthash'). The network is an instance of Network.
Parameters:
| Name | Type | Argument | Description |
|---|---|---|---|
data |
* | The encoded data in various formats |
|
network |
Network | String | number |
<optional> |
The network: 'livenet' or 'testnet' |
type |
String |
<optional> |
The type of address: 'script' or 'pubkey' |
- Source:
Returns:
A new valid and frozen instance of an Address
- Type
- Address
Example
// validate that an input field is valid
var error = Address.getValidationError(input, 'testnet');
if (!error) {
var address = Address(input, 'testnet');
} else {
// invalid network or checksum (typo?)
var message = error.messsage;
}
// get an address from a public key
var address = Address(publicKey, 'testnet').toString();
Members
-
<static> PayToPublicKeyHash
-
- Source:
-
<static> PayToScriptHash
-
- Source:
Methods
-
<private, static> _classifyFromVersion(buffer) → {Object}
-
Internal function to discover the network and type
Parameters:
Name Type Description bufferBuffer An instance of a hex encoded address Buffer
- Source:
Returns:
An object with keys: network and type
- Type
- Object
-
<private, static> _transformBuffer(buffer, network, type) → {Object}
-
Internal function to transform a bitcoin address buffer
Parameters:
Name Type Argument Description bufferBuffer An instance of a hex encoded address Buffer
networkString <optional>
The network: 'livenet' or 'testnet'
typeString <optional>
The type: 'pubkeyhash' or 'scripthash'
- Source:
Returns:
An object with keys: hashBuffer, network and type
- Type
- Object
-
<private, static> _transformHash(hash) → {Object}
-
Internal function to transform a hash buffer
Parameters:
Name Type Description hashBuffer An instance of a hash Buffer
- Source:
Returns:
An object with keys: hashBuffer
- Type
- Object
-
<private, static> _transformPublicKey(pubkey) → {Object}
-
Internal function to transform a PublicKey
Parameters:
Name Type Description pubkeyPublicKey An instance of PublicKey
- Source:
Returns:
An object with keys: hashBuffer, type
- Type
- Object
-
<private, static> _transformScript(script) → {Object}
-
Internal function to transform a Script
Parameters:
Name Type Description scriptScript An instance of Script
- Source:
Returns:
An object with keys: hashBuffer, type
- Type
- Object
-
<private, static> _transformString(data, network, type) → {Object}
-
Internal function to transform a bitcoin address string
Parameters:
Name Type Argument Description dataString An instance of PublicKey
networkString <optional>
The network: 'livenet' or 'testnet'
typeString <optional>
The type: 'pubkeyhash' or 'scripthash'
- Source:
Returns:
An object with keys: hashBuffer, network and type
- Type
- Object
-
<static> fromBuffer(buffer, network, type) → {Address}
-
Instantiate an address from a buffer of the address
Parameters:
Name Type Argument Description bufferBuffer An instance of buffer of the address
networkString <optional>
The network: 'livenet' or 'testnet'
typeString <optional>
The type of address: 'script' or 'pubkey'
- Source:
Returns:
A new valid and frozen instance of an Address
- Type
- Address
-
<static> fromJSON(json) → {Address}
-
Instantiate an address from JSON
Parameters:
Name Type Description jsonString An JSON string or Object with keys: hash, network and type
- Source:
Returns:
A new valid instance of an Address
- Type
- Address
-
<static> fromPublicKey(data, network) → {Address}
-
Instantiate an address from a PublicKey instance
Parameters:
Name Type Description dataPublicKey An instance of PublicKey
networkString The network: 'livenet' or 'testnet'
- Source:
Returns:
A new valid and frozen instance of an Address
- Type
- Address
-
<static> fromPublicKeyHash(hash, network) → {Address}
-
Instantiate an address from a ripemd160 public key hash
Parameters:
Name Type Description hashBuffer An instance of buffer of the hash
networkString The network: 'livenet' or 'testnet'
- Source:
Returns:
A new valid and frozen instance of an Address
- Type
- Address
-
<static> fromScript(script, network) → {Address}
-
Instantiate an address from a Script
Parameters:
Name Type Description scriptScript An instance of Script
networkString The network: 'livenet' or 'testnet'
- Source:
Returns:
A new valid and frozen instance of an Address
- Type
- Address
-
<static> fromScriptHash(hash, network) → {Address}
-
Instantiate an address from a ripemd160 script hash
Parameters:
Name Type Description hashBuffer An instance of buffer of the hash
networkString The network: 'livenet' or 'testnet'
- Source:
Returns:
A new valid and frozen instance of an Address
- Type
- Address
-
<static> fromString(str, network, type) → {Address}
-
Instantiate an address from an address string
Parameters:
Name Type Argument Description strString An string of the bitcoin address
networkString <optional>
The network: 'livenet' or 'testnet'
typeString <optional>
The type of address: 'script' or 'pubkey'
- Source:
Returns:
A new valid and frozen instance of an Address
- Type
- Address
-
<static> getValidationError(data, network, type) → {null|Error}
-
Will return a validation error if exists
Parameters:
Name Type Description dataString The encoded data
networkString The network: 'livenet' or 'testnet'
typeString The type of address: 'script' or 'pubkey'
- Source:
Returns:
The corresponding error message
- Type
- null | Error
Example
var error = Address.getValidationError('15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2', 'testnet'); // a network mismatch error -
<static> isValid(data, network, type) → {boolean}
-
Will return a boolean if an address is valid
Parameters:
Name Type Description dataString The encoded data
networkString The network: 'livenet' or 'testnet'
typeString The type of address: 'script' or 'pubkey'
- Source:
Returns:
The corresponding error message
- Type
- boolean
Example
var valid = Address.isValid('15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2', 'livenet'); // true -
inspect() → {String}
-
Will return a string formatted for the console
- Source:
Returns:
Bitcoin address
- Type
- String
-
isPayToPublicKeyHash()
-
Returns true if an address is of pay to public key hash type
- Source:
Returns:
boolean
-
isPayToScriptHash()
-
Returns true if an address is of pay to script hash type
- Source:
Returns:
boolean
-
toBuffer() → {Buffer}
-
Will return a buffer representation of the address
- Source:
Returns:
Bitcoin address buffer
- Type
- Buffer
-
toJSON() → {Object}
-
- Source:
Returns:
An object of the address
- Type
- Object
-
toObject() → {Object}
-
- Source:
Returns:
An object of the address
- Type
- Object
-
toString() → {String}
-
Will return a the string representation of the address
- Source:
Returns:
Bitcoin address
- Type
- String