laid back parsing for address parser.
This commit is contained in:
parent
f2edf8f4b0
commit
74e67c8a65
@ -161,17 +161,17 @@ Address.prototype.inspect = function inspect() {
|
|||||||
/**
|
/**
|
||||||
* Inject properties from base58 address.
|
* Inject properties from base58 address.
|
||||||
* @private
|
* @private
|
||||||
* @param {Base58Address} address
|
* @param {Base58Address} data
|
||||||
* @throws Parse error
|
* @throws Parse error
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Address.prototype.fromBase58 = function fromBase58(address) {
|
Address.prototype.fromBase58 = function fromBase58(data) {
|
||||||
var i, prefix, type, version, nversion, hash, network, p;
|
var i, prefix, type, version, hash, network, p;
|
||||||
|
|
||||||
if (typeof address === 'string')
|
if (typeof data === 'string')
|
||||||
address = utils.fromBase58(address);
|
data = utils.fromBase58(data);
|
||||||
|
|
||||||
p = new BufferReader(address, true);
|
p = new BufferReader(data, true);
|
||||||
prefix = p.readU8();
|
prefix = p.readU8();
|
||||||
|
|
||||||
for (i = 0; i < networks.types.length; i++) {
|
for (i = 0; i < networks.types.length; i++) {
|
||||||
@ -183,20 +183,17 @@ Address.prototype.fromBase58 = function fromBase58(address) {
|
|||||||
|
|
||||||
assert(type != null, 'Unknown address prefix.');
|
assert(type != null, 'Unknown address prefix.');
|
||||||
|
|
||||||
nversion = network.address.versions[type];
|
if (data.length > 25) {
|
||||||
|
|
||||||
if (nversion == null)
|
|
||||||
nversion = -1;
|
|
||||||
|
|
||||||
if (nversion !== -1) {
|
|
||||||
version = p.readU8();
|
version = p.readU8();
|
||||||
|
assert(data.length === 27 || data.length === 39);
|
||||||
assert(version >= 0 && version <= 16, 'Bad program version.');
|
assert(version >= 0 && version <= 16, 'Bad program version.');
|
||||||
assert(p.readU8() === 0, 'Address version padding is non-zero.');
|
assert(p.readU8() === 0, 'Address version padding is non-zero.');
|
||||||
} else {
|
} else {
|
||||||
version = -1;
|
version = -1;
|
||||||
|
assert(data.length === 25);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'witnessscripthash')
|
if (data.length === 39)
|
||||||
hash = p.readBytes(32);
|
hash = p.readBytes(32);
|
||||||
else
|
else
|
||||||
hash = p.readBytes(20);
|
hash = p.readBytes(20);
|
||||||
@ -233,6 +230,7 @@ Address.prototype.fromScript = function fromScript(script) {
|
|||||||
|
|
||||||
if (script.isProgram()) {
|
if (script.isProgram()) {
|
||||||
program = script.toProgram();
|
program = script.toProgram();
|
||||||
|
// TODO: MAST support
|
||||||
if (program.isUnknown())
|
if (program.isUnknown())
|
||||||
return;
|
return;
|
||||||
this.hash = program.data;
|
this.hash = program.data;
|
||||||
@ -377,7 +375,7 @@ Address.fromScript = function fromScript(script) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Address.prototype.fromHash = function fromHash(hash, type, version, network) {
|
Address.prototype.fromHash = function fromHash(hash, type, version, network) {
|
||||||
var prefix, nversion;
|
var prefix;
|
||||||
|
|
||||||
if (typeof hash === 'string')
|
if (typeof hash === 'string')
|
||||||
hash = new Buffer(hash, 'hex');
|
hash = new Buffer(hash, 'hex');
|
||||||
@ -390,10 +388,6 @@ Address.prototype.fromHash = function fromHash(hash, type, version, network) {
|
|||||||
|
|
||||||
network = bcoin.network.get(network);
|
network = bcoin.network.get(network);
|
||||||
prefix = network.address.prefixes[type];
|
prefix = network.address.prefixes[type];
|
||||||
nversion = network.address.versions[type];
|
|
||||||
|
|
||||||
if (version === -1 && nversion != null)
|
|
||||||
version = nversion;
|
|
||||||
|
|
||||||
assert(prefix != null, 'Not a valid address prefix.');
|
assert(prefix != null, 'Not a valid address prefix.');
|
||||||
|
|
||||||
@ -403,6 +397,8 @@ Address.prototype.fromHash = function fromHash(hash, type, version, network) {
|
|||||||
assert(hash.length === 20, 'Hash is the wrong size.');
|
assert(hash.length === 20, 'Hash is the wrong size.');
|
||||||
else if (version === 0 && type === 'witnessscripthash')
|
else if (version === 0 && type === 'witnessscripthash')
|
||||||
assert(hash.length === 32, 'Hash is the wrong size.');
|
assert(hash.length === 32, 'Hash is the wrong size.');
|
||||||
|
else if (version === 1 && type === 'witnessscripthash')
|
||||||
|
assert(hash.length === 32, 'Hash is the wrong size.');
|
||||||
|
|
||||||
this.hash = hash;
|
this.hash = hash;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
@ -434,11 +430,15 @@ Address.fromHash = function fromHash(hash, type, version, network) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Address.prototype.fromData = function fromData(data, type, version, network) {
|
Address.prototype.fromData = function fromData(data, type, version, network) {
|
||||||
if (type === 'witnessscripthash')
|
if (type === 'witnessscripthash') {
|
||||||
|
assert(version === 0);
|
||||||
data = utils.sha256(data);
|
data = utils.sha256(data);
|
||||||
else
|
} else if (type === 'witnesspubkeyhash') {
|
||||||
|
assert(version === 0);
|
||||||
data = utils.hash160(data);
|
data = utils.hash160(data);
|
||||||
|
} else {
|
||||||
|
data = utils.hash160(data);
|
||||||
|
}
|
||||||
return this.fromHash(data, type, version, network);
|
return this.fromHash(data, type, version, network);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -137,7 +137,7 @@ Network.prototype.getRate = function getRate() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Network.create = function create(options) {
|
Network.create = function create(options) {
|
||||||
var net;
|
var network;
|
||||||
|
|
||||||
if (typeof options === 'string')
|
if (typeof options === 'string')
|
||||||
options = networks[options];
|
options = networks[options];
|
||||||
@ -147,14 +147,14 @@ Network.create = function create(options) {
|
|||||||
if (Network[options.type])
|
if (Network[options.type])
|
||||||
return Network[options.type];
|
return Network[options.type];
|
||||||
|
|
||||||
net = new Network(options);
|
network = new Network(options);
|
||||||
|
|
||||||
Network[net.type] = net;
|
Network[network.type] = network;
|
||||||
|
|
||||||
if (!Network.primary)
|
if (!Network.primary)
|
||||||
Network.primary = net;
|
Network.primary = network;
|
||||||
|
|
||||||
return net;
|
return network;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -2911,7 +2911,8 @@ Script.prototype.toProgram = function toProgram() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test whether the output script is a pay-to-witness-pubkeyhash script.
|
* Test whether the output script is
|
||||||
|
* a pay-to-witness-pubkeyhash program.
|
||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -2922,7 +2923,8 @@ Script.prototype.isWitnessPubkeyhash = function isWitnessPubkeyhash() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test whether the output script is a pay-to-witness-scripthash script.
|
* Test whether the output script is
|
||||||
|
* a pay-to-witness-scripthash program.
|
||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -2932,6 +2934,18 @@ Script.prototype.isWitnessScripthash = function isWitnessScripthash() {
|
|||||||
&& this.raw[1] === 0x20;
|
&& this.raw[1] === 0x20;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test whether the output script
|
||||||
|
* is a pay-to-mast program.
|
||||||
|
* @returns {Boolean}
|
||||||
|
*/
|
||||||
|
|
||||||
|
Script.prototype.isMAST = function isMAST() {
|
||||||
|
return this.raw.length === 34
|
||||||
|
&& this.raw[0] === opcodes.OP_1
|
||||||
|
&& this.raw[1] === 0x20;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test whether the output script is unspendable.
|
* Test whether the output script is unspendable.
|
||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
@ -4622,6 +4636,7 @@ function Program(version, data) {
|
|||||||
this.data = data;
|
this.data = data;
|
||||||
this.type = null;
|
this.type = null;
|
||||||
|
|
||||||
|
// TODO: MAST support
|
||||||
if (version > 0) {
|
if (version > 0) {
|
||||||
// No interpretation of script (anyone can spend)
|
// No interpretation of script (anyone can spend)
|
||||||
this.type = 'unknown';
|
this.type = 'unknown';
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user