address work.

This commit is contained in:
Christopher Jeffrey 2016-06-29 06:07:30 -07:00
parent 203294029c
commit 755d0a2c64
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -155,11 +155,13 @@ Address.prototype.inspect = function inspect() {
*/
Address.prototype.fromBase58 = function fromBase58(data) {
var i, prefix, type, version, hash, network, p;
var i, p, prefix, network, type, version, hash;
if (typeof data === 'string')
data = utils.fromBase58(data);
assert(Buffer.isBuffer(data));
p = new BufferReader(data, true);
prefix = p.readU8();
@ -173,34 +175,17 @@ Address.prototype.fromBase58 = function fromBase58(data) {
assert(type != null, 'Unknown address prefix.');
if (data.length > 25) {
assert(network.address.witness[type], 'Non-witness address too long.');
version = p.readU8();
assert(version >= 0 && version <= 16, 'Bad program version.');
assert(p.readU8() === 0, 'Address version padding is non-zero.');
} else {
assert(data.length === 25, 'Address too short.');
version = -1;
}
hash = p.readBytes(p.left() - 4);
if (version === -1)
assert(hash.length === 20, 'Hash is the wrong size.');
else if (version === 0 && type === 'witnesspubkeyhash')
assert(hash.length === 20, 'Hash is the wrong size.');
else if (version === 0 && type === 'witnessscripthash')
assert(hash.length === 32, 'Hash is the wrong size.');
else if (version === 1 && type === 'witnessscripthash')
assert(hash.length === 32, 'Hash is the wrong size.');
p.verifyChecksum();
this.network = network.type;
this.type = type;
this.hash = hash;
this.version = version;
return this;
return this.fromHash(hash, type, version, network.type);
};
/**
@ -366,17 +351,14 @@ Address.fromScript = function fromScript(script) {
* @param {Buffer|Hash} hash
* @param {AddressType} type
* @param {Number} [version=-1]
* @param {(Network|NetworkType)?} network
* @throws on bad hash size
*/
Address.prototype.fromHash = function fromHash(hash, type, version, network) {
var prefix;
if (typeof hash === 'string')
hash = new Buffer(hash, 'hex');
assert(Buffer.isBuffer(hash));
if (!type)
type = 'pubkeyhash';
@ -384,18 +366,25 @@ Address.prototype.fromHash = function fromHash(hash, type, version, network) {
version = -1;
network = bcoin.network.get(network);
prefix = network.address.prefixes[type];
assert(prefix != null, 'Not a valid address prefix.');
assert(Buffer.isBuffer(hash));
assert(typeof type === 'string');
assert(utils.isNumber(version));
assert(network.address.prefixes[type] != null, 'Not a valid address prefix.');
if (version === -1)
if (version === -1) {
assert(!network.address.witness[type], 'Wrong version (witness)');
assert(hash.length === 20, 'Hash is the wrong size.');
else if (version === 0 && type === 'witnesspubkeyhash')
assert(hash.length === 20, 'Hash is the wrong size.');
else if (version === 0 && type === 'witnessscripthash')
assert(hash.length === 32, 'Hash is the wrong size.');
else if (version === 1 && type === 'witnessscripthash')
assert(hash.length === 32, 'Hash is the wrong size.');
} else {
assert(network.address.witness[type], 'Wrong version (non-witness).');
assert(version >= 0 && version <= 16, 'Bad program version.');
if (version === 0 && type === 'witnesspubkeyhash')
assert(hash.length === 20, 'Hash is the wrong size.');
else if (version === 0 && type === 'witnessscripthash')
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.type = type;
@ -410,6 +399,7 @@ Address.prototype.fromHash = function fromHash(hash, type, version, network) {
* @param {Buffer|Hash} hash
* @param {AddressType} type
* @param {Number} [version=-1]
* @param {(Network|NetworkType)?} network
* @returns {Address}
* @throws on bad hash size
*/
@ -423,6 +413,7 @@ Address.fromHash = function fromHash(hash, type, version, network) {
* @param {Hash|Buffer} hash
* @param {AddressType?} type
* @param {Number?} version - Witness program version.
* @param {(Network|NetworkType)?} network
* @throws on bad hash size
*/
@ -444,6 +435,7 @@ Address.prototype.fromData = function fromData(data, type, version, network) {
* @param {Buffer} data - Data to be hashed.
* @param {AddressType} type
* @param {Number} [version=-1]
* @param {(Network|NetworkType)?} network
* @returns {Address}
* @throws on bad hash size
*/