Commit f879199c3cfb67185d4d4ae91f4ff03a55d831ba
Address: make it a subclass of U256
Alex Beregszaszi committed on 8/24/2016, 2:24:49 AMParent: 011465cea528e67aaf5ce8304698823a4700189e
Files changed
address.js | changed |
address.js | ||
---|---|---|
@@ -1,41 +1,26 @@ | ||
1 | -const ethUtil = require('ethereumjs-util') | |
1 | +const U256 = require('./u256.js') | |
2 | 2 | |
3 | -module.exports = class Address { | |
3 | +module.exports = class Address extends U256 { | |
4 | 4 | constructor (value) { |
5 | - // Special case: duplicate | |
6 | - if (value instanceof Address) { | |
7 | - this._value = new Buffer(value._value) | |
8 | - return | |
5 | + super(value) | |
6 | + if (this._value.byteLength() > 20) { | |
7 | + throw new Error('Invalid address length: ' + this._value.byteLength() + ' for ' + value) | |
9 | 8 | } |
10 | - | |
11 | - if (value instanceof Uint8Array) { | |
12 | - this._value = new Buffer(value) | |
13 | - } else if (typeof value !== 'string') { | |
14 | - throw new Error('Invalid input to address') | |
15 | - } else if (!ethUtil.isHexPrefixed(value)) { | |
16 | - throw new Error('Invalid address format') | |
17 | - } else { | |
18 | - this._value = new Buffer(ethUtil.stripHexPrefix(value), 'hex') | |
19 | - } | |
20 | - | |
21 | - if (this._value.length !== 20) { | |
22 | - throw new Error('Invalid address length') | |
23 | - } | |
24 | 9 | } |
25 | 10 | |
26 | 11 | toBuffer () { |
27 | - return this._value | |
12 | + return super.toBuffer(20) | |
28 | 13 | } |
29 | 14 | |
30 | 15 | toString () { |
31 | - return '0x' + this._value.toString('hex') | |
16 | + return '0x' + this._value.toString('hex', 40) | |
32 | 17 | } |
33 | 18 | |
34 | 19 | isZero () { |
35 | - return this._value.equals(ethUtil.zeros(20)) | |
20 | + return this._value.isZero() | |
36 | 21 | } |
37 | 22 | |
38 | 23 | equals (address) { |
39 | - return this._value.toString('hex') === address.toBuffer().toString('hex') | |
24 | + return this.toString() === address.toString() | |
40 | 25 | } |
41 | 26 | } |
Built with git-ssb-web