uri: remove bip20 parsing.
This commit is contained in:
parent
40ff06f11f
commit
428e2a1301
156
lib/btc/uri.js
156
lib/btc/uri.js
@ -30,11 +30,9 @@ function URI(options) {
|
||||
return new URI(options);
|
||||
|
||||
this.address = new Address();
|
||||
this.version = -1;
|
||||
this.amount = -1;
|
||||
this.label = null;
|
||||
this.message = null;
|
||||
this.key = null;
|
||||
this.request = null;
|
||||
|
||||
if (options)
|
||||
@ -110,7 +108,7 @@ URI.fromOptions = function fromOptions(options) {
|
||||
*/
|
||||
|
||||
URI.prototype.fromString = function fromString(str) {
|
||||
var prefix, index, body, query, parts, address, version;
|
||||
var prefix, index, query, address;
|
||||
|
||||
assert(typeof str === 'string');
|
||||
assert(str.length > 8, 'Not a bitcoin URI.');
|
||||
@ -124,32 +122,24 @@ URI.prototype.fromString = function fromString(str) {
|
||||
index = str.indexOf('?');
|
||||
|
||||
if (index === -1) {
|
||||
body = str;
|
||||
address = str;
|
||||
} else {
|
||||
body = str.substring(0, index);
|
||||
address = str.substring(0, index);
|
||||
query = str.substring(index + 1);
|
||||
}
|
||||
|
||||
parts = body.split(';');
|
||||
assert(parts.length <= 2, 'Too many semicolons in body.');
|
||||
|
||||
address = parts[0];
|
||||
|
||||
this.address.fromBase58(address);
|
||||
|
||||
if (parts.length === 2) {
|
||||
version = parts[1];
|
||||
assert(util.isDecimal(version), 'Version is not decimal.');
|
||||
this.version = parseInt(version, 10);
|
||||
}
|
||||
|
||||
if (!query)
|
||||
return this;
|
||||
|
||||
query = parsePairs(query);
|
||||
|
||||
if (query.amount)
|
||||
this.amount = parseAmount(query.amount, query.size);
|
||||
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;
|
||||
@ -157,9 +147,6 @@ URI.prototype.fromString = function fromString(str) {
|
||||
if (query.message)
|
||||
this.message = query.message;
|
||||
|
||||
if (query.send)
|
||||
this.key = KeyRing.fromSecret(query.send);
|
||||
|
||||
if (query.r)
|
||||
this.request = query.r;
|
||||
|
||||
@ -187,9 +174,6 @@ URI.prototype.toString = function toString() {
|
||||
|
||||
str += this.address.toBase58();
|
||||
|
||||
if (this.version !== -1)
|
||||
str += ';version=' + this.version;
|
||||
|
||||
if (this.amount !== -1)
|
||||
query.push('amount=' + Amount.btc(this.amount));
|
||||
|
||||
@ -199,9 +183,6 @@ URI.prototype.toString = function toString() {
|
||||
if (this.message)
|
||||
query.push('message=' + escape(this.message));
|
||||
|
||||
if (this.key)
|
||||
query.push('send=' + this.key.toSecret());
|
||||
|
||||
if (this.request)
|
||||
query.push('r=' + escape(this.request));
|
||||
|
||||
@ -224,11 +205,20 @@ URI.prototype.inspect = function inspect() {
|
||||
* Helpers
|
||||
*/
|
||||
|
||||
function BitcoinQuery() {
|
||||
this.amount = null;
|
||||
this.label = null;
|
||||
this.message = null;
|
||||
this.r = null;
|
||||
}
|
||||
|
||||
function parsePairs(str) {
|
||||
var parts = str.split('&');
|
||||
var data = {};
|
||||
var data = new BitcoinQuery();
|
||||
var i, index, pair, key, value;
|
||||
|
||||
assert(parts.length <= 4, 'Too many keys in querystring.');
|
||||
|
||||
for (i = 0; i < parts.length; i++) {
|
||||
pair = parts[i];
|
||||
index = pair.indexOf('=');
|
||||
@ -241,99 +231,47 @@ function parsePairs(str) {
|
||||
value = pair.substring(index + 1);
|
||||
}
|
||||
|
||||
key = unescape(key);
|
||||
|
||||
if (key.length === 0)
|
||||
continue;
|
||||
|
||||
value = unescape(value);
|
||||
|
||||
if (value.length === 0)
|
||||
continue;
|
||||
|
||||
data[key] = value;
|
||||
switch (key) {
|
||||
case 'amount':
|
||||
assert(data.amount == null, 'Duplicate key in querystring (amount).');
|
||||
data.amount = unescape(value);
|
||||
break;
|
||||
case 'label':
|
||||
assert(data.label == null, 'Duplicate key in querystring (label).');
|
||||
data.label = unescape(value);
|
||||
break;
|
||||
case 'message':
|
||||
assert(data.message == null, 'Duplicate key in querystring (message).');
|
||||
data.message = unescape(value);
|
||||
break;
|
||||
case 'r':
|
||||
assert(data.r == null, 'Duplicate key in querystring (r).');
|
||||
data.r = unescape(value);
|
||||
break;
|
||||
default:
|
||||
assert(false, 'Unknown querystring key: ' + value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
function unescape(str) {
|
||||
try {
|
||||
str = decodeURIComponent(str).replace(/\+/g, ' ');
|
||||
} finally {
|
||||
return str.replace(/\0/g, '');
|
||||
}
|
||||
str = decodeURIComponent(str);
|
||||
str = str.replace(/\+/g, ' ');
|
||||
str = str.replace(/\0/g, '');
|
||||
return str;
|
||||
}
|
||||
|
||||
function escape(str) {
|
||||
try {
|
||||
str = encodeURIComponent(str).replace(/%20/g, '+');
|
||||
} finally {
|
||||
return str;
|
||||
}
|
||||
str = encodeURIComponent(str);
|
||||
str = str.replace(/%20/g, '+');
|
||||
return str;
|
||||
}
|
||||
|
||||
function parseAmount(amount, size) {
|
||||
var value = amount;
|
||||
var exp = 8;
|
||||
var parts;
|
||||
|
||||
assert(typeof amount === 'string');
|
||||
assert(amount.length > 0);
|
||||
|
||||
if (size) {
|
||||
assert(typeof size === 'string');
|
||||
assert(size.length > 0);
|
||||
exp = size;
|
||||
assert(util.isDecimal(exp), 'Exponent is not a decimal.');
|
||||
exp = parseInt(exp, 10);
|
||||
}
|
||||
|
||||
if (value[0] === 'x') {
|
||||
exp = 4;
|
||||
|
||||
assert(value.length > 1);
|
||||
|
||||
value = value.substring(1);
|
||||
parts = value.split('X');
|
||||
assert(parts.length <= 2, 'Too many bases.');
|
||||
|
||||
value = parts[0];
|
||||
assert(value.length > 0, 'Value is empty.');
|
||||
assert(util.isHex(value), 'Value is not hex.');
|
||||
value = parseInt(value, 16);
|
||||
assert(util.isNumber(value), 'Value exceeds 2^53-1 bits.');
|
||||
|
||||
if (parts.length === 2) {
|
||||
exp = parts[1];
|
||||
assert(util.isHex(exp), 'Exponent is not hex.');
|
||||
exp = parseInt(exp, 16);
|
||||
}
|
||||
|
||||
assert(exp <= 4, 'Exponent is too large.');
|
||||
|
||||
value *= Math.pow(16, exp);
|
||||
|
||||
assert(util.isNumber(value), 'Value exceeds 2^53-1 bits.');
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
parts = value.split('X');
|
||||
assert(parts.length <= 2, 'Too many bases.');
|
||||
|
||||
value = parts[0];
|
||||
assert(value.length > 0, 'Value is empty.');
|
||||
assert(value[0] !== '-', 'Value is negative.');
|
||||
assert(util.isFloat(value), 'Value is not a float.');
|
||||
|
||||
if (parts.length === 2) {
|
||||
exp = parts[1];
|
||||
assert(util.isDecimal(exp), 'Exponent is not decimal.');
|
||||
exp = parseInt(exp, 10);
|
||||
}
|
||||
|
||||
return Amount.parse(value, exp, false);
|
||||
return Amount.value(amount);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@ -163,19 +163,9 @@ util.hrtime = function hrtime(time) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether a string is decimal.
|
||||
* @param {String?} obj
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
util.isDecimal = function isDecimal(obj) {
|
||||
return typeof obj === 'string' && /^\d+$/.test(obj);
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether a string is hex. Note that this
|
||||
* _could_ yield a false positive on base58
|
||||
* strings.
|
||||
* Test whether a string is hex (length must be even).
|
||||
* Note that this _could_ yield a false positive on
|
||||
* base58 strings.
|
||||
* @param {String?} obj
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
Loading…
Reference in New Issue
Block a user