Files: aa7f348ee71a97e479efcc2d058b302bcf683a0d / address.js
969 bytesRaw
1 | const ethUtil = require('ethereumjs-util') |
2 | |
3 | module.exports = class Address { |
4 | constructor (value) { |
5 | // Special case: duplicate |
6 | if (value instanceof Address) { |
7 | this._value = new Buffer(value._value) |
8 | return |
9 | } |
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 | } |
25 | |
26 | toBuffer () { |
27 | return this._value |
28 | } |
29 | |
30 | toString () { |
31 | return '0x' + this._value.toString('hex') |
32 | } |
33 | |
34 | isZero () { |
35 | return this._value.equals(ethUtil.zeros(20)) |
36 | } |
37 | |
38 | equals (address) { |
39 | return this._value.toString('hex') === address.toBuffer().toString('hex') |
40 | } |
41 | } |
42 |
Built with git-ssb-web