address parsing.

This commit is contained in:
Christopher Jeffrey 2016-03-31 01:28:17 -07:00
parent 82ee87c0f5
commit 7b1af73c46
3 changed files with 21 additions and 26 deletions

View File

@ -442,8 +442,9 @@ Address.compileHash = function compileHash(hash, prefixType) {
prefix = network.address.prefixes[prefixType];
version = network.address.versions[prefixType];
assert(prefix != null);
assert(hash.length === 20 || hash.length === 32);
assert(prefix != null, 'Not a valid address prefix.');
assert(hash.length === 20 || hash.length === 32,
'Hash is the wrong length.');
size = 1 + hash.length + 4;
@ -489,7 +490,7 @@ Address.parse = function parse(addr, prefixType) {
prefix = network.address.prefixes[prefixType];
version = network.address.versions[prefixType];
assert(prefix != null);
assert(prefix != null, 'Not a valid address prefix.');
// prefix
size = 1;
@ -506,27 +507,21 @@ Address.parse = function parse(addr, prefixType) {
else
size += 20;
if (addr.length !== size + 4) {
utils.debug('Address is not the right length.');
return;
}
assert(addr.length === size + 4, 'Address is not the right length.');
if (addr[0] !== prefix) {
utils.debug('Address is not the right prefix.');
return;
}
assert(addr[0] === prefix, 'Address is not the right prefix.');
if (version != null && (addr[1] !== version || addr[2] !== 0)) {
utils.debug('Address is not the right program version.');
return;
if (version != null) {
assert(addr[1] === version,
'Address is not the right program version.');
assert(addr[2] === 0,
'Address version padding is zero.');
}
chk = utils.checksum(addr.slice(0, -4));
if (utils.readU32(chk, 0) !== utils.readU32(addr, size)) {
utils.debug('Address checksum failed.');
return;
}
assert(utils.readU32(chk, 0) === utils.readU32(addr, size),
'Address checksum failed.');
return {
type: prefixType,
@ -539,8 +534,11 @@ Address.validate = function validate(addr, prefix) {
if (!addr)
return false;
if (!Address.parse(addr, prefix))
try {
Address.parse(addr, prefix);
} catch (e) {
return false;
}
return true;
};

View File

@ -77,7 +77,7 @@ MTX.prototype.witnessHash = function witnessHash(enc) {
if (this.isCoinbase()) {
return enc === 'hex'
? utils.toHex(constants.zeroHash)
: new Buffer(constants.zeroHash);
: utils.slice(constants.zeroHash);
}
if (!this.hasWitness())
@ -181,7 +181,8 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
redeemScript = addr.program.encode();
vector = input.witness.items;
dummy = new Buffer([]);
assert(addr.program.code[0] === opcodes.OP_0, 'Non-zero version passed to address.');
assert(addr.program.code[0] === opcodes.OP_0,
'Non-zero version passed to address.');
if (addr.program.code[1].length === 32) {
// P2WSH nested within pay-to-scripthash
// (it had to be this complicated, didn't it?)

View File

@ -1827,10 +1827,6 @@ Script.createOutputScript = function(options) {
script = Script.createMultisig(keys, m, n);
} else if (options.address) {
address = bcoin.address.parse(options.address);
if (!address)
throw new Error(options.address + ' is not a valid address.');
if (address.type === 'pubkeyhash')
script = Script.createPubkeyhash(address.hash);
else if (address.type === 'scripthash')
@ -1838,7 +1834,7 @@ Script.createOutputScript = function(options) {
else if (address.version !== -1)
script = Script.createWitnessProgram(address.version, address.hash);
else
throw new Error('Cannot parse address: ' + options.address);
assert(false);
} else if (options.key) {
script = Script.createPubkey(utils.ensureBuffer(options.key));
} else if (options.flags) {