Files: a3b9c88c4884b2b2f42a88e75023168e5f85a08a / address.js
768 bytesRaw
1 | const ethUtil = require('ethereumjs-util') |
2 | |
3 | module.exports = class Address extends Buffer { |
4 | constructor (value) { |
5 | if (value instanceof Address || value instanceof Uint8Array) { |
6 | super(value) |
7 | } else if (typeof value !== 'string') { |
8 | throw new Error('Invalid input to address') |
9 | } else if (!ethUtil.isHexPrefixed(value)) { |
10 | throw new Error('Invalid address format') |
11 | } else { |
12 | super(ethUtil.stripHexPrefix(value), 'hex') |
13 | } |
14 | |
15 | if (this.length !== 20) { |
16 | throw new Error('Invalid address length') |
17 | } |
18 | } |
19 | |
20 | toString () { |
21 | return '0x' + this.toString('hex') |
22 | } |
23 | |
24 | isZero () { |
25 | return this.every((el) => el === 0) |
26 | } |
27 | |
28 | equals (address) { |
29 | return this.toString('hex') === address.toBuffer().toString('hex') |
30 | } |
31 | } |
32 |
Built with git-ssb-web