git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 7be79ad95e85298c6cac7020fe62ac700afebc56

Files: 7be79ad95e85298c6cac7020fe62ac700afebc56 / deps / address.js

977 bytesRaw
1const BN = require('bn.js')
2const U256 = require('./u256.js')
3
4module.exports = class Address extends U256 {
5 constructor (value) {
6 super(value)
7 if (this._value.byteLength() > 20) {
8 throw new Error('Invalid address length: ' + this._value.byteLength() + ' for ' + value)
9 }
10 }
11
12 // This assumes Uint8Array in LSB (WASM code)
13 static fromMemory (value) {
14 return new Address(new BN(value, 16, 'be'))
15 }
16
17 // This assumes Uint8Array in LSB (WASM code)
18 toMemory () {
19 return this._value.toBuffer('be', 20)
20 }
21
22 toBuffer () {
23 return super.toBuffer(20)
24 }
25
26 toArray () {
27 return [...this.toBuffer()]
28 }
29
30 // Needs to be displayed as a hex always
31 toString () {
32 return '0x' + this._value.toString('hex', 40)
33 }
34
35 static zero () {
36 return new Address('0x0000000000000000000000000000000000000000')
37 }
38
39 isZero () {
40 return this._value.isZero()
41 }
42
43 equals (address) {
44 return this.toString() === address.toString()
45 }
46}
47

Built with git-ssb-web