bitcore-lib-zcash/lib/address.js

121 lines
3.3 KiB
JavaScript
Raw Normal View History

2014-11-20 07:16:27 -08:00
'use strict';
2014-11-21 07:54:56 -08:00
var base58check = require('./encoding/base58check');
var networks = require('./networks');
2014-11-20 07:16:27 -08:00
var Hash = require('./crypto/hash');
2014-10-01 13:28:57 -07:00
2014-10-01 16:20:03 -07:00
function Address(buf) {
if (!(this instanceof Address))
return new Address(buf);
if (Buffer.isBuffer(buf)) {
this.fromBuffer(buf);
} else if (typeof buf === 'string') {
var str = buf;
this.fromString(str);
} else if (buf) {
var obj = buf;
this.set(obj);
}
2014-11-20 07:16:27 -08:00
}
2014-10-01 16:20:03 -07:00
Address.prototype.set = function(obj) {
this.hashbuf = obj.hashbuf || this.hashbuf || null;
this.networkstr = obj.networkstr || this.networkstr || 'mainnet';
this.typestr = obj.typestr || this.typestr || 'pubkeyhash';
return this;
};
Address.prototype.fromBuffer = function(buf) {
if (buf.length !== 1 + 20)
throw new Error('Address buffers must be exactly 21 bytes');
var version = buf[0];
2014-11-21 07:54:56 -08:00
if (version === networks['mainnet']['pubkeyhash']) {
2014-10-01 16:20:03 -07:00
this.networkstr = 'mainnet';
this.typestr = 'pubkeyhash';
2014-11-21 07:54:56 -08:00
} else if (version === networks['mainnet']['scripthash']) {
2014-10-01 16:20:03 -07:00
this.networkstr = 'mainnet';
this.typestr = 'scripthash';
2014-11-21 07:54:56 -08:00
} else if (version === networks['testnet']['pubkeyhash']) {
2014-10-01 16:20:03 -07:00
this.networkstr = 'testnet';
this.typestr = 'pubkeyhash';
2014-11-21 07:54:56 -08:00
} else if (version === networks['testnet']['scripthash']) {
2014-10-01 16:20:03 -07:00
this.networkstr = 'testnet';
this.typestr = 'scripthash';
} else {
this.networkstr = 'unknown';
this.typestr = 'unknown';
}
this.hashbuf = buf.slice(1);
2014-10-01 13:28:57 -07:00
2014-10-01 16:20:03 -07:00
return this;
2014-10-01 13:28:57 -07:00
};
2014-10-01 16:20:03 -07:00
Address.prototype.fromHashbuf = function(hashbuf, networkstr, typestr) {
if (hashbuf.length !== 20)
throw new Error('hashbuf must be exactly 20 bytes');
this.hashbuf = hashbuf;
this.networkstr = networkstr || 'mainnet';
this.typestr = typestr || 'pubkeyhash';
return this;
};
Address.prototype.fromPubkey = function(pubkey, networkstr) {
this.hashbuf = Hash.sha256ripemd160(pubkey.toBuffer());
this.networkstr = networkstr || 'mainnet';
this.typestr = 'pubkeyhash';
return this;
};
Address.prototype.fromScript = function(script, networkstr) {
this.hashbuf = Hash.sha256ripemd160(script.toBuffer());
this.networkstr = networkstr || 'mainnet';
this.typestr = 'scripthash';
return this;
};
2014-10-01 13:28:57 -07:00
2014-10-01 16:20:03 -07:00
Address.prototype.fromString = function(str) {
var buf = base58check.decode(str);
return this.fromBuffer(buf);
}
2014-10-01 13:28:57 -07:00
2014-10-01 16:20:03 -07:00
Address.isValid = function(addrstr) {
try {
var address = new Address().fromString(addrstr);
} catch (e) {
return false;
}
return address.isValid();
};
Address.prototype.isValid = function() {
try {
this.validate();
return true;
} catch (e) {
return false;
}
};
Address.prototype.toBuffer = function() {
2014-11-21 07:54:56 -08:00
var version = new Buffer([networks[this.networkstr][this.typestr]]);
2014-10-01 16:20:03 -07:00
var buf = Buffer.concat([version, this.hashbuf]);
return buf;
};
Address.prototype.toString = function() {
return base58check.encode(this.toBuffer());
};
Address.prototype.validate = function() {
if (!Buffer.isBuffer(this.hashbuf) || this.hashbuf.length !== 20)
throw new Error('hash must be a buffer of 20 bytes');
if (this.networkstr !== 'mainnet' && this.networkstr !== 'testnet')
throw new Error('networkstr must be "mainnet" or "testnet"');
if (this.typestr !== 'pubkeyhash' && this.typestr !== 'scripthash')
throw new Error('typestr must be "pubkeyhash" or "scripthash"');
return this;
};
2014-10-01 13:28:57 -07:00
module.exports = Address;