git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 04af14cd7056250c22a08f3950f10cfa2dcbb8c8

Files: 04af14cd7056250c22a08f3950f10cfa2dcbb8c8 / address.js

768 bytesRaw
1const ethUtil = require('ethereumjs-util')
2
3module.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