|
|
@ -5,9 +5,9 @@ var URL = require('url'); |
|
|
|
|
|
|
|
var Address = require('./address'); |
|
|
|
var Unit = require('./unit'); |
|
|
|
var JSUtil = require('./util/js'); |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Bitcore URI |
|
|
|
* |
|
|
|
* Instantiate an URI from a bitcoin URI String or an Object. An URI instance |
|
|
@ -35,11 +35,13 @@ var URI = function(data, knownParams) { |
|
|
|
this.knownParams = knownParams || []; |
|
|
|
this.address = this.network = this.amount = this.message = null; |
|
|
|
|
|
|
|
if (typeof(data) == 'string') { |
|
|
|
if (typeof(data) === 'string') { |
|
|
|
var params = URI.parse(data); |
|
|
|
if (params.amount) params.amount = this._parseAmount(params.amount); |
|
|
|
if (params.amount) { |
|
|
|
params.amount = this._parseAmount(params.amount); |
|
|
|
} |
|
|
|
this._fromObject(params); |
|
|
|
} else if (typeof(data) == 'object') { |
|
|
|
} else if (typeof(data) === 'object') { |
|
|
|
this._fromObject(data); |
|
|
|
} else { |
|
|
|
throw new TypeError('Unrecognized data format.'); |
|
|
@ -47,7 +49,32 @@ var URI = function(data, knownParams) { |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Instantiate a URI from a String |
|
|
|
* |
|
|
|
* @param {String} str - JSON string or object of the URI |
|
|
|
* @returns {URI} A new instance of a URI |
|
|
|
*/ |
|
|
|
URI.fromString = function fromString(str) { |
|
|
|
if (typeof(str) !== 'string') { |
|
|
|
throw TypeError('Expected a string'); |
|
|
|
} |
|
|
|
return new URI(str); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Instantiate a URI from JSON |
|
|
|
* |
|
|
|
* @param {String|Object} json - JSON string or object of the URI |
|
|
|
* @returns {URI} A new instance of a URI |
|
|
|
*/ |
|
|
|
URI.fromJSON = function fromJSON(json) { |
|
|
|
if (JSUtil.isValidJson(json)) { |
|
|
|
json = JSON.parse(json); |
|
|
|
} |
|
|
|
return new URI(json); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Check if an bitcoin URI string is valid |
|
|
|
* |
|
|
|
* @example |
|
|
@ -63,13 +90,12 @@ URI.isValid = function(arg, knownParams) { |
|
|
|
try { |
|
|
|
var uri = new URI(arg, knownParams); |
|
|
|
return true; |
|
|
|
} catch(err) { |
|
|
|
} catch (err) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Convert a bitcoin URI string into a simple object. |
|
|
|
* |
|
|
|
* @param {string} uri - A bitcoin URI string |
|
|
@ -90,8 +116,9 @@ URI.parse = function(uri) { |
|
|
|
return info.query; |
|
|
|
}; |
|
|
|
|
|
|
|
URI.Members = ['address', 'amount', 'message', 'label', 'r']; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Internal function to load the URI instance with an object. |
|
|
|
* |
|
|
|
* @param {Object} obj - Object with the information |
|
|
@ -100,9 +127,10 @@ URI.parse = function(uri) { |
|
|
|
* @throws {Error} Unknown required argument |
|
|
|
*/ |
|
|
|
URI.prototype._fromObject = function(obj) { |
|
|
|
var members = ['address', 'amount', 'message', 'label', 'r']; |
|
|
|
|
|
|
|
if (!Address.isValid(obj.address)) throw new TypeError('Invalid bitcoin address'); |
|
|
|
if (!Address.isValid(obj.address)) { |
|
|
|
throw new TypeError('Invalid bitcoin address'); |
|
|
|
} |
|
|
|
|
|
|
|
this.address = new Address(obj.address); |
|
|
|
this.network = this.address.network; |
|
|
@ -115,13 +143,12 @@ URI.prototype._fromObject = function(obj) { |
|
|
|
throw Error('Unknown required argument ' + key); |
|
|
|
} |
|
|
|
|
|
|
|
var destination = members.indexOf(key) > -1 ? this : this.extras; |
|
|
|
var destination = URI.Members.indexOf(key) > -1 ? this : this.extras; |
|
|
|
destination[key] = obj[key]; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Internal function to transform a BTC string amount into satoshis |
|
|
|
* |
|
|
|
* @param {String} amount - Amount BTC string |
|
|
@ -129,21 +156,49 @@ URI.prototype._fromObject = function(obj) { |
|
|
|
* @returns {Object} Amount represented in satoshis |
|
|
|
*/ |
|
|
|
URI.prototype._parseAmount = function(amount) { |
|
|
|
var amount = Number(amount); |
|
|
|
if (isNaN(amount)) throw new TypeError('Invalid amount'); |
|
|
|
amount = Number(amount); |
|
|
|
if (isNaN(amount)) { |
|
|
|
throw new TypeError('Invalid amount'); |
|
|
|
} |
|
|
|
return Unit.fromBTC(amount).toSatoshis(); |
|
|
|
}; |
|
|
|
|
|
|
|
URI.prototype.toJSON = function() { |
|
|
|
var json = {}; |
|
|
|
for (var i = 0; i < URI.Members.length; i++) { |
|
|
|
var m = URI.Members[i]; |
|
|
|
if (this.hasOwnProperty(m) && typeof(this[m]) !== 'undefined') { |
|
|
|
if (typeof(this[m].toString) === 'function') { |
|
|
|
json[m] = this[m].toString(); |
|
|
|
} else { |
|
|
|
json[m] = this[m]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
_.extend(json, this.extras); |
|
|
|
return json; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Will return a the string representation of the URI |
|
|
|
* |
|
|
|
* @returns {String} Bitcoin URI string |
|
|
|
*/ |
|
|
|
URI.prototype.toString = function() { |
|
|
|
var query = _.clone(this.extras); |
|
|
|
if (this.amount) query.amount = Unit.fromSatoshis(this.amount).toBTC(); |
|
|
|
if (this.message) query.message = this.message; |
|
|
|
var query = {}; |
|
|
|
if (this.amount) { |
|
|
|
query.amount = Unit.fromSatoshis(this.amount).toBTC(); |
|
|
|
} |
|
|
|
if (this.message) { |
|
|
|
query.message = this.message; |
|
|
|
} |
|
|
|
if (this.label) { |
|
|
|
query.label = this.label; |
|
|
|
} |
|
|
|
if (this.r) { |
|
|
|
query.r = this.r; |
|
|
|
} |
|
|
|
_.extend(query, this.extras); |
|
|
|
|
|
|
|
return URL.format({ |
|
|
|
protocol: 'bitcoin:', |
|
|
@ -153,13 +208,12 @@ URI.prototype.toString = function() { |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Will return a string formatted for the console |
|
|
|
* |
|
|
|
* @returns {String} Bitcoin URI |
|
|
|
*/ |
|
|
|
URI.prototype.inspect = function() { |
|
|
|
return '<URI: ' + this.toString()+ '>'; |
|
|
|
} |
|
|
|
return '<URI: ' + this.toString() + '>'; |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports = URI; |
|
|
|