Commit 78a0cadc66db0c286938c220db7175c0da6eee02
Simplifies Address by extending Buffer directly
wanderer authored on 8/17/2016, 2:39:33 PMAlex Beregszaszi committed on 8/17/2016, 10:38:51 PM
Parent: 53d0a8449b654d395b0965a2e0d05bb3cd6782fa
Files changed
address.js | changed |
interface.js | changed |
address.js | ||
---|---|---|
@@ -1,41 +1,31 @@ | ||
1 | 1 | const ethUtils = require('ethereumjs-util') |
2 | 2 | |
3 | -module.exports = class Address { | |
3 | +module.exports = class Address extends Buffer { | |
4 | 4 | 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) | |
13 | 7 | } else if (typeof value !== 'string') { |
14 | 8 | throw new Error('Invalid input to address') |
15 | 9 | } else if (!ethUtils.isHexPrefixed(value)) { |
16 | 10 | throw new Error('Invalid address format') |
17 | 11 | } else { |
18 | - this._value = new Buffer(ethUtils.stripHexPrefix(value), 'hex') | |
12 | + super(ethUtils.stripHexPrefix(value), 'hex') | |
19 | 13 | } |
20 | 14 | |
21 | - if (this._value.length !== 20) { | |
15 | + if (this.length !== 20) { | |
22 | 16 | throw new Error('Invalid address length') |
23 | 17 | } |
24 | 18 | } |
25 | 19 | |
26 | - toBuffer () { | |
27 | - return this._value | |
28 | - } | |
29 | - | |
30 | 20 | toString () { |
31 | - return '0x' + this._value.toString('hex') | |
21 | + return '0x' + this.toString('hex') | |
32 | 22 | } |
33 | 23 | |
34 | 24 | isZero () { |
35 | - return this._value.equals(ethUtils.zeros(20)) | |
25 | + return this.every((el) => el === 0) | |
36 | 26 | } |
37 | 27 | |
38 | 28 | equals (address) { |
39 | - return this._value.toString('hex') === address.toBuffer().toString('hex') | |
29 | + return this.toString('hex') === address.toBuffer().toString('hex') | |
40 | 30 | } |
41 | 31 | } |
interface.js | ||
---|---|---|
@@ -82,9 +82,9 @@ | ||
82 | 82 | * the given offset. |
83 | 83 | * @param {integer} offset |
84 | 84 | */ |
85 | 85 | 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) | |
87 | 87 | } |
88 | 88 | |
89 | 89 | /** |
90 | 90 | * Gets balance of the given account and loads it into memory at the given |
@@ -105,18 +105,18 @@ | ||
105 | 105 | * account with non-empty associated code. |
106 | 106 | * @param {integer} offset |
107 | 107 | */ |
108 | 108 | 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) | |
110 | 110 | } |
111 | 111 | |
112 | 112 | /** |
113 | 113 | * Gets caller address and loads it into memory at the given offset. This is |
114 | 114 | * the address of the account that is directly responsible for this execution. |
115 | 115 | * @param {integer} offset |
116 | 116 | */ |
117 | 117 | 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) | |
119 | 119 | } |
120 | 120 | |
121 | 121 | /** |
122 | 122 | * Gets the deposited value by the instruction/transaction responsible for |
@@ -221,9 +221,9 @@ | ||
221 | 221 | * Gets the block’s beneficiary address and loads into memory. |
222 | 222 | * @param offset |
223 | 223 | */ |
224 | 224 | 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) | |
226 | 226 | } |
227 | 227 | |
228 | 228 | /** |
229 | 229 | * Get the block’s timestamp. |
Built with git-ssb-web