Files: 1258de512d97564568307c5b1f4041dd7a10820f / address.js
771 bytesRaw
1 | const ethUtils = 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 (!ethUtils.isHexPrefixed(value)) { |
10 | throw new Error('Invalid address format') |
11 | } else { |
12 | super(ethUtils.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