implement int, uint decoding

pull/7/head
yann300 8 years ago
parent 4e5f700263
commit d8373b227b
  1. 20
      src/solidity/types/Int.js
  2. 11
      src/solidity/types/Uint.js
  3. 13
      src/solidity/types/util.js

@ -1,4 +1,7 @@
'use strict'
var util = require('./util')
var BN = require('ethereumjs-util').BN
var ethutil = require('ethereumjs-util')
function Int (storageBytes) {
this.storageSlots = 1
@ -7,7 +10,22 @@ function Int (storageBytes) {
}
Int.prototype.decodeFromStorage = function (location, storageContent) {
return '<not implemented yet>'
var slot = ethutil.bufferToHex(ethutil.setLengthLeft(location.slot, 32))
if (!storageContent[slot]) {
return '0'
}
var value = util.extractValue(storageContent[slot], this.storageBytes, location)
var bigNumber = new BN(value.replace('0x', ''), 16)
if (isNegative(bigNumber, this.storageBytes)) {
return ethutil.fromSigned(ethutil.toUnsigned(bigNumber)).toString(10)
} else {
return bigNumber.toString(10)
}
}
module.exports = Int
function isNegative (value, storageBytes) {
var binary = value.toString(2)
return binary.length < storageBytes ? false : binary[0] === '1'
}

@ -1,4 +1,7 @@
'use strict'
var util = require('./util')
var ethutil = require('ethereumjs-util')
var BN = require('ethereumjs-util').BN
function Uint (storageBytes) {
this.storageSlots = 1
@ -7,7 +10,13 @@ function Uint (storageBytes) {
}
Uint.prototype.decodeFromStorage = function (location, storageContent) {
return '<not implemented yet>'
var slot = ethutil.bufferToHex(ethutil.setLengthLeft(location.slot, 32))
if (!storageContent[slot]) {
return '0'
}
var value = util.extractValue(storageContent[slot], this.storageBytes, location)
var bigNumber = new BN(value.replace('0x', ''), 16)
return bigNumber.toString(10)
}
module.exports = Uint

@ -0,0 +1,13 @@
module.exports = {
extractValue: function (slotValue, storageBytes, location) {
slotValue = slotValue.replace('0x', '')
var offset = slotValue.length - 2 * location.offset - storageBytes / 4
if (offset >= 0) {
return '0x' + slotValue.substr(offset, storageBytes / 4)
} else if (offset + storageBytes > 0) {
return '0x' + slotValue.substr(0, storageBytes / 4 + offset)
} else {
return '0x0'
}
}
}
Loading…
Cancel
Save