git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 78a0cadc66db0c286938c220db7175c0da6eee02

Simplifies Address by extending Buffer directly

wanderer authored on 8/17/2016, 2:39:33 PM
Alex Beregszaszi committed on 8/17/2016, 10:38:51 PM
Parent: 53d0a8449b654d395b0965a2e0d05bb3cd6782fa

Files changed

address.jschanged
interface.jschanged
address.jsView
@@ -1,41 +1,31 @@
11 const ethUtils = require('ethereumjs-util')
22
3-module.exports = class Address {
3+module.exports = class Address extends Buffer {
44 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)
5+ if (value instanceof Address || value instanceof Uint8Array) {
6+ super(value)
137 } else if (typeof value !== 'string') {
148 throw new Error('Invalid input to address')
159 } else if (!ethUtils.isHexPrefixed(value)) {
1610 throw new Error('Invalid address format')
1711 } else {
18- this._value = new Buffer(ethUtils.stripHexPrefix(value), 'hex')
12+ super(ethUtils.stripHexPrefix(value), 'hex')
1913 }
2014
21- if (this._value.length !== 20) {
15+ if (this.length !== 20) {
2216 throw new Error('Invalid address length')
2317 }
2418 }
2519
26- toBuffer () {
27- return this._value
28- }
29-
3020 toString () {
31- return '0x' + this._value.toString('hex')
21+ return '0x' + this.toString('hex')
3222 }
3323
3424 isZero () {
35- return this._value.equals(ethUtils.zeros(20))
25+ return this.every((el) => el === 0)
3626 }
3727
3828 equals (address) {
39- return this._value.toString('hex') === address.toBuffer().toString('hex')
29+ return this.toString('hex') === address.toBuffer().toString('hex')
4030 }
4131 }
interface.jsView
@@ -82,9 +82,9 @@
8282 * the given offset.
8383 * @param {integer} offset
8484 */
8585 getAddress (offset) {
86- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.address.toBuffer())
86+ this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.address)
8787 }
8888
8989 /**
9090 * Gets balance of the given account and loads it into memory at the given
@@ -105,18 +105,18 @@
105105 * account with non-empty associated code.
106106 * @param {integer} offset
107107 */
108108 getTxOrigin (offset) {
109- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.origin.toBuffer())
109+ this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.origin)
110110 }
111111
112112 /**
113113 * Gets caller address and loads it into memory at the given offset. This is
114114 * the address of the account that is directly responsible for this execution.
115115 * @param {integer} offset
116116 */
117117 getCaller (offset) {
118- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.caller.toBuffer())
118+ this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.caller)
119119 }
120120
121121 /**
122122 * Gets the deposited value by the instruction/transaction responsible for
@@ -221,9 +221,9 @@
221221 * Gets the block’s beneficiary address and loads into memory.
222222 * @param offset
223223 */
224224 getBlockCoinbase (offset) {
225- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.coinbase.toBuffer())
225+ this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.coinbase)
226226 }
227227
228228 /**
229229 * Get the block’s timestamp.

Built with git-ssb-web