interface.jsView |
---|
2 | 2 | * This is the Ethereum interface that is exposed to the WASM instance which |
3 | 3 | * enables to interact with the Ethereum Environment |
4 | 4 | */ |
5 | 5 | const constants = require('./constants.js') |
| 6 | +const Address = require('./address.js') |
6 | 7 | |
7 | 8 | |
8 | 9 | module.exports = class Interface { |
9 | 10 | constructor (environment) { |
80 | 81 | * the given offset. |
81 | 82 | * @param {integer} offset |
82 | 83 | */ |
83 | 84 | address (offset) { |
84 | | - this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.address) |
| 85 | + this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.address.toBuffer()) |
85 | 86 | } |
86 | 87 | |
87 | 88 | |
88 | 89 | * Gets balance of the given account and loads it into memory at the given |
90 | 91 | * @param {integer} addressOffset the memory offset to laod the address |
91 | 92 | * @param {integer} resultOffset |
92 | 93 | */ |
93 | 94 | balance (addressOffset, offset) { |
94 | | - const address = this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES) |
| 95 | + const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) |
95 | 96 | |
96 | 97 | const balance = this.environment.parent.environment.getBalance(address) |
97 | 98 | this.setMemory(offset, constants.BALANCE_SIZE_BYTES, balance) |
98 | 99 | } |
103 | 104 | * account with non-empty associated code. |
104 | 105 | * @param {integer} offset |
105 | 106 | */ |
106 | 107 | origin (offset) { |
107 | | - this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.origin) |
| 108 | + this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.origin.toBuffer()) |
108 | 109 | } |
109 | 110 | |
110 | 111 | |
111 | 112 | * Gets caller address and loads it into memory at the given offset. This is |
112 | 113 | * the address of the account that is directly responsible for this execution. |
113 | 114 | * @param {integer} offset |
114 | 115 | */ |
115 | 116 | caller (offset) { |
116 | | - this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.caller) |
| 117 | + this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.caller.toBuffer()) |
117 | 118 | } |
118 | 119 | |
119 | 120 | |
120 | 121 | * Gets the deposited value by the instruction/transaction responsible for |
170 | 171 | * @param {integer} addressOffset the offset in memory to load the address from |
171 | 172 | * @return {integer} |
172 | 173 | */ |
173 | 174 | extCodeSize (addressOffset) { |
174 | | - const address = this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES) |
| 175 | + const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) |
175 | 176 | const code = this.environment.getCode(address) |
176 | 177 | return code.length |
177 | 178 | } |
178 | 179 | |
183 | 184 | * @param {integer} codeOffset the code offset |
184 | 185 | * @param {integer} length the length of code to copy |
185 | 186 | */ |
186 | 187 | extCodeCopy (addressOffset, offset, codeOffset, length) { |
187 | | - const address = this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES) |
| 188 | + const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) |
188 | 189 | let code = this.environment.getCode(address) |
189 | 190 | code = new Uint8Array(code, codeOffset, length) |
190 | 191 | this.setMemory(offset, length, code) |
191 | 192 | } |
212 | 213 | * Gets the block’s beneficiary address and loads into memory. |
213 | 214 | * @param offset |
214 | 215 | */ |
215 | 216 | coinbase (offset) { |
216 | | - this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.coinbase) |
| 217 | + this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.coinbase.toBuffer()) |
217 | 218 | } |
218 | 219 | |
219 | 220 | |
220 | 221 | * Get the block’s timestamp. |
291 | 292 | if (gas === undefined) { |
292 | 293 | gas = this.gasLeft() |
293 | 294 | } |
294 | 295 | |
295 | | - const address = this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES) |
| 296 | + const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) |
296 | 297 | const value = this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES) |
297 | 298 | const data = this.getMemory(dataOffset, dataLength) |
298 | 299 | |
299 | 300 | const [result, errorCode] = this.environment.call(gas, address, value, data) |
314 | 315 | * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not |
315 | 316 | */ |
316 | 317 | callDelegate (gas, addressOffset, dataOffset, dataLength, resultOffset, resultLength) { |
317 | 318 | const data = this.getMemory(dataOffset, dataLength) |
318 | | - const address = this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES) |
| 319 | + const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) |
319 | 320 | const [result, errorCode] = this.environment.callDelegate(gas, address, data) |
320 | 321 | this.setMemory(resultOffset, resultLength, result) |
321 | 322 | return errorCode |
322 | 323 | } |
373 | 374 | * balance to an address path |
374 | 375 | * @param {integer} offset the offset to load the address from |
375 | 376 | */ |
376 | 377 | suicide (addressOffset) { |
377 | | - this.environment.suicideAddress = this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES) |
| 378 | + this.environment.suicideAddress = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) |
378 | 379 | } |
379 | 380 | |
380 | 381 | getMemory (offset, length) { |
381 | 382 | return new Uint8Array(this.module.exports.memory, offset, length) |