Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Handle network string as a URI. #151

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73,294 changes: 38,075 additions & 35,219 deletions bitcore-lib.js

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions bitcore-lib.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/networks.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ Take a look at this modified snippet from [networks.js](https://github.com/bitpa
```javascript
var livenet = new Network();
_.extend(livenet, {
name: 'livenet',
alias: 'mainnet',
name: 'livenet/btc',
alias: 'mainnet/btc',
pubkeyhash: 0x00,
privatekey: 0x80,
scripthash: 0x05,
Expand All @@ -43,8 +43,8 @@ _.extend(livenet, {

var testnet = new Network();
_.extend(testnet, {
name: 'testnet',
alias: 'testnet',
name: 'testnet/btc',
alias: 'testnet/btc',
pubkeyhash: 0x6f,
privatekey: 0xef,
scripthash: 0xc4,
Expand Down
2 changes: 1 addition & 1 deletion docs/uri.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The code for validating URIs looks like this:
var uriString = 'bitcoin:12A1MyfXbW6RhdRAZEqofac5jCQQjwEPBu?amount=1.2';
var valid = URI.isValid(uriString);
var uri = new URI(uriString);
console.log(uri.address.network, uri.amount); // 'livenet', 120000000
console.log(uri.address.network, uri.amount); // 'livenet/btc, 120000000
```

## URI Parameters
Expand Down
32 changes: 16 additions & 16 deletions lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var PublicKey = require('./publickey');
* ```
*
* @param {*} data - The encoded data in various formats
* @param {Network|String|number=} network - The network: 'livenet' or 'testnet'
* @param {Network|String|number=} network - The network: Constants.LIVENET or Constants.TESTNET
* @param {string=} type - The type of address: 'script' or 'pubkey'
* @returns {Address} A new valid and frozen instance of an Address
* @constructor
Expand All @@ -63,7 +63,7 @@ function Address(data, network, type) {
$.checkArgument(data, 'First argument is required, please include address data.', 'guide/address.html');

if (network && !Networks.get(network)) {
throw new TypeError('Second argument must be "livenet" or "testnet".');
throw new TypeError('Second argument must be "livenet/btc" or "testnet/btc".');
}

if (type && (type !== Address.PayToPublicKeyHash && type !== Address.PayToScriptHash)) {
Expand All @@ -88,7 +88,7 @@ function Address(data, network, type) {
/**
* Internal function used to split different kinds of arguments of the constructor
* @param {*} data - The encoded data in various formats
* @param {Network|String|number=} network - The network: 'livenet' or 'testnet'
* @param {Network|String|number=} network - The network: Constants.LIVENET or Constants.TESTNET
* @param {string=} type - The type of address: 'script' or 'pubkey'
* @returns {Object} An "info" object with "type", "network", and "hashBuffer"
*/
Expand Down Expand Up @@ -180,7 +180,7 @@ Address._classifyFromVersion = function(buffer) {
* Internal function to transform a bitcoin address buffer
*
* @param {Buffer} buffer - An instance of a hex encoded address Buffer
* @param {string=} network - The network: 'livenet' or 'testnet'
* @param {string=} network - The network: Constants.LIVENET or Constants.TESTNET
* @param {string=} type - The type: 'pubkeyhash' or 'scripthash'
* @returns {Object} An object with keys: hashBuffer, network and type
* @private
Expand Down Expand Up @@ -254,7 +254,7 @@ Address._transformScript = function(script, network) {
*
* @param {Array} publicKeys - a set of public keys to create an address
* @param {number} threshold - the number of signatures needed to release the funds
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
* @param {String|Network} network - either a Network instance, Constants.LIVENET, or Constants.TESTNET
* @return {Address}
*/
Address.createMultisig = function(publicKeys, threshold, network) {
Expand All @@ -266,7 +266,7 @@ Address.createMultisig = function(publicKeys, threshold, network) {
* Internal function to transform a bitcoin address string
*
* @param {string} data
* @param {String|Network=} network - either a Network instance, 'livenet', or 'testnet'
* @param {String|Network=} network - either a Network instance, Constants.LIVENET, or Constants.TESTNET
* @param {string=} type - The type: 'pubkeyhash' or 'scripthash'
* @returns {Object} An object with keys: hashBuffer, network and type
* @private
Expand All @@ -285,7 +285,7 @@ Address._transformString = function(data, network, type) {
* Instantiate an address from a PublicKey instance
*
* @param {PublicKey} data
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
* @param {String|Network} network - either a Network instance, Constants.LIVENET, or Constants.TESTNET
* @returns {Address} A new valid and frozen instance of an Address
*/
Address.fromPublicKey = function(data, network) {
Expand All @@ -298,7 +298,7 @@ Address.fromPublicKey = function(data, network) {
* Instantiate an address from a ripemd160 public key hash
*
* @param {Buffer} hash - An instance of buffer of the hash
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
* @param {String|Network} network - either a Network instance, Constants.LIVENET, or Constants.TESTNET
* @returns {Address} A new valid and frozen instance of an Address
*/
Address.fromPublicKeyHash = function(hash, network) {
Expand All @@ -310,7 +310,7 @@ Address.fromPublicKeyHash = function(hash, network) {
* Instantiate an address from a ripemd160 script hash
*
* @param {Buffer} hash - An instance of buffer of the hash
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
* @param {String|Network} network - either a Network instance, Constants.LIVENET, or Constants.TESTNET
* @returns {Address} A new valid and frozen instance of an Address
*/
Address.fromScriptHash = function(hash, network) {
Expand All @@ -326,7 +326,7 @@ Address.fromScriptHash = function(hash, network) {
* see {{Address#fromScript}}
*
* @param {Script} script - An instance of Script
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
* @param {String|Network} network - either a Network instance, Constants.LIVENET, or Constants.TESTNET
* @returns {Address} A new valid and frozen instance of an Address
*/
Address.payingTo = function(script, network) {
Expand All @@ -345,7 +345,7 @@ Address.payingTo = function(script, network) {
* to that script's hash instead, use {{Address#payingTo}}
*
* @param {Script} script - An instance of Script
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
* @param {String|Network} network - either a Network instance, Constants.LIVENET, or Constants.TESTNET
* @returns {Address} A new valid and frozen instance of an Address
*/
Address.fromScript = function(script, network) {
Expand All @@ -358,7 +358,7 @@ Address.fromScript = function(script, network) {
* Instantiate an address from a buffer of the address
*
* @param {Buffer} buffer - An instance of buffer of the address
* @param {String|Network=} network - either a Network instance, 'livenet', or 'testnet'
* @param {String|Network=} network - either a Network instance, Constants.LIVENET, or Constants.TESTNET
* @param {string=} type - The type of address: 'script' or 'pubkey'
* @returns {Address} A new valid and frozen instance of an Address
*/
Expand All @@ -371,7 +371,7 @@ Address.fromBuffer = function(buffer, network, type) {
* Instantiate an address from an address string
*
* @param {string} str - An string of the bitcoin address
* @param {String|Network=} network - either a Network instance, 'livenet', or 'testnet'
* @param {String|Network=} network - either a Network instance, Constants.LIVENET, or Constants.TESTNET
* @param {string=} type - The type of address: 'script' or 'pubkey'
* @returns {Address} A new valid and frozen instance of an Address
*/
Expand Down Expand Up @@ -405,7 +405,7 @@ Address.fromObject = function fromObject(obj) {
* ```
*
* @param {string} data - The encoded data
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
* @param {String|Network} network - either a Network instance, Constants.LIVENET, or Constants.TESTNET
* @param {string} type - The type of address: 'script' or 'pubkey'
* @returns {null|Error} The corresponding error message
*/
Expand All @@ -425,11 +425,11 @@ Address.getValidationError = function(data, network, type) {
*
* @example
* ```javascript
* assert(Address.isValid('15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2', 'livenet'));
* assert(Address.isValid('15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2', Constants.LIVENET));
* ```
*
* @param {string} data - The encoded data
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
* @param {String|Network} network - either a Network instance, Constants.LIVENET, or Constants.TESTNET
* @param {string} type - The type of address: 'script' or 'pubkey'
* @returns {boolean} The corresponding error message
*/
Expand Down
10 changes: 10 additions & 0 deletions lib/common/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

var Constants = {};

Constants.LIVENET = 'livenet/btc';
Constants.LIVENET_ALIAS = 'mainnet/btc';
Constants.TESTNET = 'testnet/btc';
Constants.TESTNET_ALIAS = 'regtest/btc';

module.exports = Constants;
2 changes: 1 addition & 1 deletion lib/hdprivatekey.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ HDPrivateKey.prototype.inspect = function() {
* Returns a plain object with a representation of this private key.
*
* Fields include:<ul>
* <li> network: either 'livenet' or 'testnet'
* <li> network: either Constants.LIVENET or Constants.TESTNET
* <li> depth: a number ranging from 0 to 255
* <li> fingerPrint: a number ranging from 0 to 2^32-1, taken from the hash of the
* <li> associated public key
Expand Down
2 changes: 1 addition & 1 deletion lib/hdpublickey.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ HDPublicKey.prototype.inspect = function() {
* Returns a plain JavaScript object with information to reconstruct a key.
*
* Fields are: <ul>
* <li> network: 'livenet' or 'testnet'
* <li> network: Constants.LIVENET or Constants.TESTNET
* <li> depth: a number from 0 to 255, the depth to the master extended key
* <li> fingerPrint: a number of 32 bits taken from the hash of the public key
* <li> fingerPrint: a number of 32 bits taken from the hash of this key's
Expand Down
13 changes: 7 additions & 6 deletions lib/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var BufferUtil = require('./util/buffer');
var JSUtil = require('./util/js');
var networks = [];
var networkMaps = {};
var Constants = require('./common/constants');

/**
* A network is merely a map containing values that correspond to version
Expand Down Expand Up @@ -127,8 +128,8 @@ function removeNetwork(network) {
}

addNetwork({
name: 'livenet',
alias: 'mainnet',
name: Constants.LIVENET,
alias: Constants.LIVENET_ALIAS,
pubkeyhash: 0x00,
privatekey: 0x80,
scripthash: 0x05,
Expand All @@ -150,11 +151,11 @@ addNetwork({
* @instance
* @member Networks#livenet
*/
var livenet = get('livenet');
var livenet = get(Constants.LIVENET);

addNetwork({
name: 'testnet',
alias: 'regtest',
name: Constants.TESTNET,
alias: Constants.TESTNET_ALIAS,
pubkeyhash: 0x6f,
privatekey: 0xef,
scripthash: 0xc4,
Expand All @@ -166,7 +167,7 @@ addNetwork({
* @instance
* @member Networks#testnet
*/
var testnet = get('testnet');
var testnet = get(Constants.TESTNET);

// Add configurable values for testnet/regtest

Expand Down
2 changes: 1 addition & 1 deletion lib/privatekey.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function PrivateKey(data, network) {
throw new TypeError('Number must be less than N');
}
if (typeof(info.network) === 'undefined') {
throw new TypeError('Must specify the network ("livenet" or "testnet")');
throw new TypeError('Must specify the network ("livenet/btc" or "testnet/btc")');
}

JSUtil.defineImmutable(this, {
Expand Down
Loading