btc: classify.
This commit is contained in:
parent
51e3341252
commit
32b81f0d95
@ -10,348 +10,353 @@ const assert = require('assert');
|
|||||||
const fixed = require('../utils/fixed');
|
const fixed = require('../utils/fixed');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Amount
|
||||||
* Represents a bitcoin amount (satoshis internally).
|
* Represents a bitcoin amount (satoshis internally).
|
||||||
* @alias module:btc.Amount
|
* @alias module:btc.Amount
|
||||||
* @constructor
|
|
||||||
* @param {(String|Number)?} value
|
|
||||||
* @param {String?} unit
|
|
||||||
* @property {Amount} value
|
* @property {Amount} value
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function Amount(value, unit) {
|
class Amount {
|
||||||
if (!(this instanceof Amount))
|
/**
|
||||||
return new Amount(value, unit);
|
* Create an amount.
|
||||||
|
* @constructor
|
||||||
|
* @param {(String|Number)?} value
|
||||||
|
* @param {String?} unit
|
||||||
|
*/
|
||||||
|
|
||||||
this.value = 0;
|
constructor(value, unit) {
|
||||||
|
this.value = 0;
|
||||||
|
|
||||||
if (value != null)
|
if (value != null)
|
||||||
this.fromOptions(value, unit);
|
this.fromOptions(value, unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inject properties from options.
|
* Inject properties from options.
|
||||||
* @private
|
* @private
|
||||||
* @param {(String|Number)?} value
|
* @param {(String|Number)?} value
|
||||||
* @param {String?} unit
|
* @param {String?} unit
|
||||||
* @returns {Amount}
|
* @returns {Amount}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Amount.prototype.fromOptions = function fromOptions(value, unit) {
|
fromOptions(value, unit) {
|
||||||
if (typeof unit === 'string')
|
if (typeof unit === 'string')
|
||||||
return this.from(unit, value);
|
return this.from(unit, value);
|
||||||
|
|
||||||
if (typeof value === 'number')
|
if (typeof value === 'number')
|
||||||
return this.fromValue(value);
|
return this.fromValue(value);
|
||||||
|
|
||||||
return this.fromBTC(value);
|
return this.fromBTC(value);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get satoshi value.
|
* Get satoshi value.
|
||||||
* @returns {Amount}
|
* @returns {Amount}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Amount.prototype.toValue = function toValue() {
|
toValue() {
|
||||||
return this.value;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get satoshi string or value.
|
|
||||||
* @param {Boolean?} num
|
|
||||||
* @returns {String|Amount}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.prototype.toSatoshis = function toSatoshis(num) {
|
|
||||||
if (num)
|
|
||||||
return this.value;
|
return this.value;
|
||||||
|
|
||||||
return this.value.toString(10);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get bits string or value.
|
|
||||||
* @param {Boolean?} num
|
|
||||||
* @returns {String|Amount}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.prototype.toBits = function toBits(num) {
|
|
||||||
return Amount.encode(this.value, 2, num);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get mbtc string or value.
|
|
||||||
* @param {Boolean?} num
|
|
||||||
* @returns {String|Amount}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.prototype.toMBTC = function toMBTC(num) {
|
|
||||||
return Amount.encode(this.value, 5, num);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get btc string or value.
|
|
||||||
* @param {Boolean?} num
|
|
||||||
* @returns {String|Amount}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.prototype.toBTC = function toBTC(num) {
|
|
||||||
return Amount.encode(this.value, 8, num);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get unit string or value.
|
|
||||||
* @param {String} unit - Can be `sat`,
|
|
||||||
* `ubtc`, `bits`, `mbtc`, or `btc`.
|
|
||||||
* @param {Boolean?} num
|
|
||||||
* @returns {String|Amount}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.prototype.to = function to(unit, num) {
|
|
||||||
switch (unit) {
|
|
||||||
case 'sat':
|
|
||||||
return this.toSatoshis(num);
|
|
||||||
case 'ubtc':
|
|
||||||
case 'bits':
|
|
||||||
return this.toBits(num);
|
|
||||||
case 'mbtc':
|
|
||||||
return this.toMBTC(num);
|
|
||||||
case 'btc':
|
|
||||||
return this.toBTC(num);
|
|
||||||
}
|
}
|
||||||
throw new Error(`Unknown unit "${unit}".`);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert amount to bitcoin string.
|
* Get satoshi string or value.
|
||||||
* @returns {String}
|
* @param {Boolean?} num
|
||||||
*/
|
* @returns {String|Amount}
|
||||||
|
*/
|
||||||
|
|
||||||
Amount.prototype.toString = function toString() {
|
toSatoshis(num) {
|
||||||
return this.toBTC();
|
if (num)
|
||||||
};
|
return this.value;
|
||||||
|
|
||||||
/**
|
return this.value.toString(10);
|
||||||
* Inject properties from value.
|
|
||||||
* @private
|
|
||||||
* @param {Amount} value
|
|
||||||
* @returns {Amount}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.prototype.fromValue = function fromValue(value) {
|
|
||||||
assert(Number.isSafeInteger(value) && value >= 0, 'Value must be an int64.');
|
|
||||||
this.value = value;
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Inject properties from satoshis.
|
|
||||||
* @private
|
|
||||||
* @param {Number|String} value
|
|
||||||
* @returns {Amount}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.prototype.fromSatoshis = function fromSatoshis(value) {
|
|
||||||
this.value = Amount.decode(value, 0);
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Inject properties from bits.
|
|
||||||
* @private
|
|
||||||
* @param {Number|String} value
|
|
||||||
* @returns {Amount}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.prototype.fromBits = function fromBits(value) {
|
|
||||||
this.value = Amount.decode(value, 2);
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Inject properties from mbtc.
|
|
||||||
* @private
|
|
||||||
* @param {Number|String} value
|
|
||||||
* @returns {Amount}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.prototype.fromMBTC = function fromMBTC(value) {
|
|
||||||
this.value = Amount.decode(value, 5);
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Inject properties from btc.
|
|
||||||
* @private
|
|
||||||
* @param {Number|String} value
|
|
||||||
* @returns {Amount}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.prototype.fromBTC = function fromBTC(value) {
|
|
||||||
this.value = Amount.decode(value, 8);
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Inject properties from unit.
|
|
||||||
* @private
|
|
||||||
* @param {String} unit
|
|
||||||
* @param {Number|String} value
|
|
||||||
* @returns {Amount}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.prototype.from = function from(unit, value) {
|
|
||||||
switch (unit) {
|
|
||||||
case 'sat':
|
|
||||||
return this.fromSatoshis(value);
|
|
||||||
case 'ubtc':
|
|
||||||
case 'bits':
|
|
||||||
return this.fromBits(value);
|
|
||||||
case 'mbtc':
|
|
||||||
return this.fromMBTC(value);
|
|
||||||
case 'btc':
|
|
||||||
return this.fromBTC(value);
|
|
||||||
}
|
}
|
||||||
throw new Error(`Unknown unit "${unit}".`);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate amount from options.
|
* Get bits string or value.
|
||||||
* @param {(String|Number)?} value
|
* @param {Boolean?} num
|
||||||
* @param {String?} unit
|
* @returns {String|Amount}
|
||||||
* @returns {Amount}
|
*/
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.fromOptions = function fromOptions(value, unit) {
|
toBits(num) {
|
||||||
return new Amount().fromOptions(value, unit);
|
return Amount.encode(this.value, 2, num);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate amount from value.
|
* Get mbtc string or value.
|
||||||
* @private
|
* @param {Boolean?} num
|
||||||
* @param {Amount} value
|
* @returns {String|Amount}
|
||||||
* @returns {Amount}
|
*/
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.fromValue = function fromValue(value) {
|
toMBTC(num) {
|
||||||
return new Amount().fromValue(value);
|
return Amount.encode(this.value, 5, num);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate amount from satoshis.
|
* Get btc string or value.
|
||||||
* @param {Number|String} value
|
* @param {Boolean?} num
|
||||||
* @returns {Amount}
|
* @returns {String|Amount}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Amount.fromSatoshis = function fromSatoshis(value) {
|
toBTC(num) {
|
||||||
return new Amount().fromSatoshis(value);
|
return Amount.encode(this.value, 8, num);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate amount from bits.
|
* Get unit string or value.
|
||||||
* @param {Number|String} value
|
* @param {String} unit - Can be `sat`,
|
||||||
* @returns {Amount}
|
* `ubtc`, `bits`, `mbtc`, or `btc`.
|
||||||
*/
|
* @param {Boolean?} num
|
||||||
|
* @returns {String|Amount}
|
||||||
|
*/
|
||||||
|
|
||||||
Amount.fromBits = function fromBits(value) {
|
to(unit, num) {
|
||||||
return new Amount().fromBits(value);
|
switch (unit) {
|
||||||
};
|
case 'sat':
|
||||||
|
return this.toSatoshis(num);
|
||||||
|
case 'ubtc':
|
||||||
|
case 'bits':
|
||||||
|
return this.toBits(num);
|
||||||
|
case 'mbtc':
|
||||||
|
return this.toMBTC(num);
|
||||||
|
case 'btc':
|
||||||
|
return this.toBTC(num);
|
||||||
|
}
|
||||||
|
throw new Error(`Unknown unit "${unit}".`);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate amount from mbtc.
|
* Convert amount to bitcoin string.
|
||||||
* @param {Number|String} value
|
* @returns {String}
|
||||||
* @returns {Amount}
|
*/
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.fromMBTC = function fromMBTC(value) {
|
toString() {
|
||||||
return new Amount().fromMBTC(value);
|
return this.toBTC();
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate amount from btc.
|
* Inject properties from value.
|
||||||
* @param {Number|String} value
|
* @private
|
||||||
* @returns {Amount}
|
* @param {Amount} value
|
||||||
*/
|
* @returns {Amount}
|
||||||
|
*/
|
||||||
|
|
||||||
Amount.fromBTC = function fromBTC(value) {
|
fromValue(value) {
|
||||||
return new Amount().fromBTC(value);
|
assert(Number.isSafeInteger(value) && value >= 0,
|
||||||
};
|
'Value must be an int64.');
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate amount from unit.
|
* Inject properties from satoshis.
|
||||||
* @param {String} unit
|
* @private
|
||||||
* @param {Number|String} value
|
* @param {Number|String} value
|
||||||
* @returns {Amount}
|
* @returns {Amount}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Amount.from = function from(unit, value) {
|
fromSatoshis(value) {
|
||||||
return new Amount().from(unit, value);
|
this.value = Amount.decode(value, 0);
|
||||||
};
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inspect amount.
|
* Inject properties from bits.
|
||||||
* @returns {String}
|
* @private
|
||||||
*/
|
* @param {Number|String} value
|
||||||
|
* @returns {Amount}
|
||||||
|
*/
|
||||||
|
|
||||||
Amount.prototype.inspect = function inspect() {
|
fromBits(value) {
|
||||||
return `<Amount: ${this.toString()}>`;
|
this.value = Amount.decode(value, 2);
|
||||||
};
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Safely convert satoshis to a BTC string.
|
* Inject properties from mbtc.
|
||||||
* This function explicitly avoids any
|
* @private
|
||||||
* floating point arithmetic.
|
* @param {Number|String} value
|
||||||
* @param {Amount} value - Satoshis.
|
* @returns {Amount}
|
||||||
* @returns {String} BTC string.
|
*/
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.btc = function btc(value, num) {
|
fromMBTC(value) {
|
||||||
if (typeof value === 'string')
|
this.value = Amount.decode(value, 5);
|
||||||
return value;
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
return Amount.encode(value, 8, num);
|
/**
|
||||||
};
|
* Inject properties from btc.
|
||||||
|
* @private
|
||||||
|
* @param {Number|String} value
|
||||||
|
* @returns {Amount}
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
fromBTC(value) {
|
||||||
* Safely convert a BTC string to satoshis.
|
this.value = Amount.decode(value, 8);
|
||||||
* @param {String} str - BTC
|
return this;
|
||||||
* @returns {Amount} Satoshis.
|
}
|
||||||
* @throws on parse error
|
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.value = function value(str) {
|
/**
|
||||||
if (typeof str === 'number')
|
* Inject properties from unit.
|
||||||
return str;
|
* @private
|
||||||
|
* @param {String} unit
|
||||||
|
* @param {Number|String} value
|
||||||
|
* @returns {Amount}
|
||||||
|
*/
|
||||||
|
|
||||||
return Amount.decode(str, 8);
|
from(unit, value) {
|
||||||
};
|
switch (unit) {
|
||||||
|
case 'sat':
|
||||||
|
return this.fromSatoshis(value);
|
||||||
|
case 'ubtc':
|
||||||
|
case 'bits':
|
||||||
|
return this.fromBits(value);
|
||||||
|
case 'mbtc':
|
||||||
|
return this.fromMBTC(value);
|
||||||
|
case 'btc':
|
||||||
|
return this.fromBTC(value);
|
||||||
|
}
|
||||||
|
throw new Error(`Unknown unit "${unit}".`);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Safely convert satoshis to a BTC string.
|
* Instantiate amount from options.
|
||||||
* @param {Amount} value
|
* @param {(String|Number)?} value
|
||||||
* @param {Number} exp - Exponent.
|
* @param {String?} unit
|
||||||
* @param {Boolean} num - Return a number.
|
* @returns {Amount}
|
||||||
* @returns {String|Number}
|
*/
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.encode = function encode(value, exp, num) {
|
static fromOptions(value, unit) {
|
||||||
if (num)
|
return new this().fromOptions(value, unit);
|
||||||
return fixed.toFloat(value, exp);
|
}
|
||||||
return fixed.encode(value, exp);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Safely convert a BTC string to satoshis.
|
* Instantiate amount from value.
|
||||||
* @param {String|Number} value - BTC
|
* @private
|
||||||
* @param {Number} exp - Exponent.
|
* @param {Amount} value
|
||||||
* @returns {Amount} Satoshis.
|
* @returns {Amount}
|
||||||
* @throws on parse error
|
*/
|
||||||
*/
|
|
||||||
|
|
||||||
Amount.decode = function decode(value, exp) {
|
static fromValue(value) {
|
||||||
if (typeof value === 'number')
|
return new this().fromValue(value);
|
||||||
return fixed.fromFloat(value, exp);
|
}
|
||||||
return fixed.decode(value, exp);
|
|
||||||
};
|
/**
|
||||||
|
* Instantiate amount from satoshis.
|
||||||
|
* @param {Number|String} value
|
||||||
|
* @returns {Amount}
|
||||||
|
*/
|
||||||
|
|
||||||
|
static fromSatoshis(value) {
|
||||||
|
return new this().fromSatoshis(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiate amount from bits.
|
||||||
|
* @param {Number|String} value
|
||||||
|
* @returns {Amount}
|
||||||
|
*/
|
||||||
|
|
||||||
|
static fromBits(value) {
|
||||||
|
return new this().fromBits(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiate amount from mbtc.
|
||||||
|
* @param {Number|String} value
|
||||||
|
* @returns {Amount}
|
||||||
|
*/
|
||||||
|
|
||||||
|
static fromMBTC(value) {
|
||||||
|
return new this().fromMBTC(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiate amount from btc.
|
||||||
|
* @param {Number|String} value
|
||||||
|
* @returns {Amount}
|
||||||
|
*/
|
||||||
|
|
||||||
|
static fromBTC(value) {
|
||||||
|
return new this().fromBTC(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiate amount from unit.
|
||||||
|
* @param {String} unit
|
||||||
|
* @param {Number|String} value
|
||||||
|
* @returns {Amount}
|
||||||
|
*/
|
||||||
|
|
||||||
|
static from(unit, value) {
|
||||||
|
return new this().from(unit, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inspect amount.
|
||||||
|
* @returns {String}
|
||||||
|
*/
|
||||||
|
|
||||||
|
inspect() {
|
||||||
|
return `<Amount: ${this.toString()}>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safely convert satoshis to a BTC string.
|
||||||
|
* This function explicitly avoids any
|
||||||
|
* floating point arithmetic.
|
||||||
|
* @param {Amount} value - Satoshis.
|
||||||
|
* @returns {String} BTC string.
|
||||||
|
*/
|
||||||
|
|
||||||
|
static btc(value, num) {
|
||||||
|
if (typeof value === 'string')
|
||||||
|
return value;
|
||||||
|
|
||||||
|
return Amount.encode(value, 8, num);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safely convert a BTC string to satoshis.
|
||||||
|
* @param {String} str - BTC
|
||||||
|
* @returns {Amount} Satoshis.
|
||||||
|
* @throws on parse error
|
||||||
|
*/
|
||||||
|
|
||||||
|
static value(str) {
|
||||||
|
if (typeof str === 'number')
|
||||||
|
return str;
|
||||||
|
|
||||||
|
return Amount.decode(str, 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safely convert satoshis to a BTC string.
|
||||||
|
* @param {Amount} value
|
||||||
|
* @param {Number} exp - Exponent.
|
||||||
|
* @param {Boolean} num - Return a number.
|
||||||
|
* @returns {String|Number}
|
||||||
|
*/
|
||||||
|
|
||||||
|
static encode(value, exp, num) {
|
||||||
|
if (num)
|
||||||
|
return fixed.toFloat(value, exp);
|
||||||
|
return fixed.encode(value, exp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safely convert a BTC string to satoshis.
|
||||||
|
* @param {String|Number} value - BTC
|
||||||
|
* @param {Number} exp - Exponent.
|
||||||
|
* @returns {Amount} Satoshis.
|
||||||
|
* @throws on parse error
|
||||||
|
*/
|
||||||
|
|
||||||
|
static decode(value, exp) {
|
||||||
|
if (typeof value === 'number')
|
||||||
|
return fixed.fromFloat(value, exp);
|
||||||
|
return fixed.decode(value, exp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Expose
|
* Expose
|
||||||
|
|||||||
290
lib/btc/uri.js
290
lib/btc/uri.js
@ -11,10 +11,9 @@ const Address = require('../primitives/address');
|
|||||||
const Amount = require('./amount');
|
const Amount = require('./amount');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* URI
|
||||||
* Represents a bitcoin URI.
|
* Represents a bitcoin URI.
|
||||||
* @alias module:btc.URI
|
* @alias module:btc.URI
|
||||||
* @constructor
|
|
||||||
* @param {Object|String} options
|
|
||||||
* @property {Address} address
|
* @property {Address} address
|
||||||
* @property {Amount} amount
|
* @property {Amount} amount
|
||||||
* @property {String|null} label
|
* @property {String|null} label
|
||||||
@ -22,185 +21,194 @@ const Amount = require('./amount');
|
|||||||
* @property {String|null} request
|
* @property {String|null} request
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function URI(options) {
|
class URI {
|
||||||
if (!(this instanceof URI))
|
/**
|
||||||
return new URI(options);
|
* Create a bitcoin URI.
|
||||||
|
* @alias module:btc.URI
|
||||||
|
* @constructor
|
||||||
|
* @param {Object|String} options
|
||||||
|
*/
|
||||||
|
|
||||||
this.address = new Address();
|
constructor(options) {
|
||||||
this.amount = -1;
|
this.address = new Address();
|
||||||
this.label = null;
|
this.amount = -1;
|
||||||
this.message = null;
|
this.label = null;
|
||||||
this.request = null;
|
this.message = null;
|
||||||
|
this.request = null;
|
||||||
|
|
||||||
if (options)
|
if (options)
|
||||||
this.fromOptions(options);
|
this.fromOptions(options);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Inject properties from options object.
|
|
||||||
* @private
|
|
||||||
* @param {Object|String} options
|
|
||||||
* @returns {URI}
|
|
||||||
*/
|
|
||||||
|
|
||||||
URI.prototype.fromOptions = function fromOptions(options) {
|
|
||||||
if (typeof options === 'string')
|
|
||||||
return this.fromString(options);
|
|
||||||
|
|
||||||
if (options.address)
|
|
||||||
this.address.fromOptions(options.address);
|
|
||||||
|
|
||||||
if (options.amount != null) {
|
|
||||||
assert(Number.isSafeInteger(options.amount) && options.amount >= 0,
|
|
||||||
'Amount must be a uint64.');
|
|
||||||
this.amount = options.amount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.label) {
|
/**
|
||||||
assert(typeof options.label === 'string', 'Label must be a string.');
|
* Inject properties from options object.
|
||||||
this.label = options.label;
|
* @private
|
||||||
}
|
* @param {Object|String} options
|
||||||
|
* @returns {URI}
|
||||||
|
*/
|
||||||
|
|
||||||
if (options.message) {
|
fromOptions(options) {
|
||||||
assert(typeof options.message === 'string', 'Message must be a string.');
|
if (typeof options === 'string')
|
||||||
this.message = options.message;
|
return this.fromString(options);
|
||||||
}
|
|
||||||
|
|
||||||
if (options.request) {
|
if (options.address)
|
||||||
assert(typeof options.request === 'string', 'Request must be a string.');
|
this.address.fromOptions(options.address);
|
||||||
this.request = options.request;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
if (options.amount != null) {
|
||||||
};
|
assert(Number.isSafeInteger(options.amount) && options.amount >= 0,
|
||||||
|
'Amount must be a uint64.');
|
||||||
|
this.amount = options.amount;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
if (options.label) {
|
||||||
* Instantiate URI from options.
|
assert(typeof options.label === 'string', 'Label must be a string.');
|
||||||
* @param {Object|String} options
|
this.label = options.label;
|
||||||
* @returns {URI}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
URI.fromOptions = function fromOptions(options) {
|
if (options.message) {
|
||||||
return new URI().fromOptions(options);
|
assert(typeof options.message === 'string', 'Message must be a string.');
|
||||||
};
|
this.message = options.message;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
if (options.request) {
|
||||||
* Parse and inject properties from string.
|
assert(typeof options.request === 'string', 'Request must be a string.');
|
||||||
* @private
|
this.request = options.request;
|
||||||
* @param {String} str
|
}
|
||||||
* @param {Network?} network
|
|
||||||
* @returns {URI}
|
|
||||||
*/
|
|
||||||
|
|
||||||
URI.prototype.fromString = function fromString(str, network) {
|
|
||||||
assert(typeof str === 'string');
|
|
||||||
assert(str.length > 8, 'Not a bitcoin URI.');
|
|
||||||
|
|
||||||
const prefix = str.substring(0, 8);
|
|
||||||
|
|
||||||
assert(prefix === 'bitcoin:', 'Not a bitcoin URI.');
|
|
||||||
|
|
||||||
str = str.substring(8);
|
|
||||||
|
|
||||||
const index = str.indexOf('?');
|
|
||||||
|
|
||||||
let addr, qs;
|
|
||||||
if (index === -1) {
|
|
||||||
addr = str;
|
|
||||||
} else {
|
|
||||||
addr = str.substring(0, index);
|
|
||||||
qs = str.substring(index + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.address.fromString(addr, network);
|
|
||||||
|
|
||||||
if (!qs)
|
|
||||||
return this;
|
return this;
|
||||||
|
|
||||||
const query = parsePairs(qs);
|
|
||||||
|
|
||||||
if (query.amount) {
|
|
||||||
assert(query.amount.length > 0, 'Value is empty.');
|
|
||||||
assert(query.amount[0] !== '-', 'Value is negative.');
|
|
||||||
this.amount = Amount.value(query.amount);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (query.label)
|
/**
|
||||||
this.label = query.label;
|
* Instantiate URI from options.
|
||||||
|
* @param {Object|String} options
|
||||||
|
* @returns {URI}
|
||||||
|
*/
|
||||||
|
|
||||||
if (query.message)
|
static fromOptions(options) {
|
||||||
this.message = query.message;
|
return new this().fromOptions(options);
|
||||||
|
}
|
||||||
|
|
||||||
if (query.r)
|
/**
|
||||||
this.request = query.r;
|
* Parse and inject properties from string.
|
||||||
|
* @private
|
||||||
|
* @param {String} str
|
||||||
|
* @param {Network?} network
|
||||||
|
* @returns {URI}
|
||||||
|
*/
|
||||||
|
|
||||||
return this;
|
fromString(str, network) {
|
||||||
};
|
assert(typeof str === 'string');
|
||||||
|
assert(str.length > 8, 'Not a bitcoin URI.');
|
||||||
|
|
||||||
/**
|
const prefix = str.substring(0, 8);
|
||||||
* Instantiate uri from string.
|
|
||||||
* @param {String} str
|
|
||||||
* @param {Network?} network
|
|
||||||
* @returns {URI}
|
|
||||||
*/
|
|
||||||
|
|
||||||
URI.fromString = function fromString(str, network) {
|
assert(prefix === 'bitcoin:', 'Not a bitcoin URI.');
|
||||||
return new URI().fromString(str, network);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
str = str.substring(8);
|
||||||
* Serialize uri to a string.
|
|
||||||
* @returns {String}
|
|
||||||
*/
|
|
||||||
|
|
||||||
URI.prototype.toString = function toString() {
|
const index = str.indexOf('?');
|
||||||
let str = 'bitcoin:';
|
|
||||||
|
|
||||||
str += this.address.toString();
|
let addr, qs;
|
||||||
|
if (index === -1) {
|
||||||
|
addr = str;
|
||||||
|
} else {
|
||||||
|
addr = str.substring(0, index);
|
||||||
|
qs = str.substring(index + 1);
|
||||||
|
}
|
||||||
|
|
||||||
const query = [];
|
this.address.fromString(addr, network);
|
||||||
|
|
||||||
if (this.amount !== -1)
|
if (!qs)
|
||||||
query.push(`amount=${Amount.btc(this.amount)}`);
|
return this;
|
||||||
|
|
||||||
if (this.label)
|
const query = parsePairs(qs);
|
||||||
query.push(`label=${escape(this.label)}`);
|
|
||||||
|
|
||||||
if (this.message)
|
if (query.amount) {
|
||||||
query.push(`message=${escape(this.message)}`);
|
assert(query.amount.length > 0, 'Value is empty.');
|
||||||
|
assert(query.amount[0] !== '-', 'Value is negative.');
|
||||||
|
this.amount = Amount.value(query.amount);
|
||||||
|
}
|
||||||
|
|
||||||
if (this.request)
|
if (query.label)
|
||||||
query.push(`r=${escape(this.request)}`);
|
this.label = query.label;
|
||||||
|
|
||||||
if (query.length > 0)
|
if (query.message)
|
||||||
str += '?' + query.join('&');
|
this.message = query.message;
|
||||||
|
|
||||||
return str;
|
if (query.r)
|
||||||
};
|
this.request = query.r;
|
||||||
|
|
||||||
/**
|
return this;
|
||||||
* Inspect bitcoin uri.
|
}
|
||||||
* @returns {String}
|
|
||||||
*/
|
|
||||||
|
|
||||||
URI.prototype.inspect = function inspect() {
|
/**
|
||||||
return `<URI: ${this.toString()}>`;
|
* Instantiate uri from string.
|
||||||
};
|
* @param {String} str
|
||||||
|
* @param {Network?} network
|
||||||
|
* @returns {URI}
|
||||||
|
*/
|
||||||
|
|
||||||
|
static fromString(str, network) {
|
||||||
|
return new this().fromString(str, network);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serialize uri to a string.
|
||||||
|
* @returns {String}
|
||||||
|
*/
|
||||||
|
|
||||||
|
toString() {
|
||||||
|
let str = 'bitcoin:';
|
||||||
|
|
||||||
|
str += this.address.toString();
|
||||||
|
|
||||||
|
const query = [];
|
||||||
|
|
||||||
|
if (this.amount !== -1)
|
||||||
|
query.push(`amount=${Amount.btc(this.amount)}`);
|
||||||
|
|
||||||
|
if (this.label)
|
||||||
|
query.push(`label=${escape(this.label)}`);
|
||||||
|
|
||||||
|
if (this.message)
|
||||||
|
query.push(`message=${escape(this.message)}`);
|
||||||
|
|
||||||
|
if (this.request)
|
||||||
|
query.push(`r=${escape(this.request)}`);
|
||||||
|
|
||||||
|
if (query.length > 0)
|
||||||
|
str += '?' + query.join('&');
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inspect bitcoin uri.
|
||||||
|
* @returns {String}
|
||||||
|
*/
|
||||||
|
|
||||||
|
inspect() {
|
||||||
|
return `<URI: ${this.toString()}>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helpers
|
* Helpers
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function BitcoinQuery() {
|
class BitcoinQuery {
|
||||||
this.amount = null;
|
constructor() {
|
||||||
this.label = null;
|
this.amount = null;
|
||||||
this.message = null;
|
this.label = null;
|
||||||
this.r = null;
|
this.message = null;
|
||||||
|
this.r = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function parsePairs(str) {
|
function parsePairs(str) {
|
||||||
const parts = str.split('&');
|
const parts = str.split('&');
|
||||||
const data = new BitcoinQuery();
|
const data = new BitcoinQuery();
|
||||||
|
|
||||||
let size = 0;
|
let size = 0;
|
||||||
|
|
||||||
for (const pair of parts) {
|
for (const pair of parts) {
|
||||||
@ -244,7 +252,7 @@ function parsePairs(str) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
size++;
|
size += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user