const ethUtil = require('ethereumjs-util') module.exports = class Address { constructor (value) { // Special case: duplicate if (value instanceof Address) { this._value = new Buffer(value._value) return } if (value instanceof Uint8Array) { this._value = new Buffer(value) } else if (typeof value !== 'string') { throw new Error('Invalid input to address') } else if (!ethUtil.isHexPrefixed(value)) { throw new Error('Invalid address format') } else { this._value = new Buffer(ethUtil.stripHexPrefix(value), 'hex') } if (this._value.length !== 20) { throw new Error('Invalid address length') } } toBuffer () { return this._value } toString () { return '0x' + this._value.toString('hex') } isZero () { return this._value.equals(ethUtil.zeros(20)) } equals (address) { return this._value.toString('hex') === address.toBuffer().toString('hex') } }