Commit d8373b22 authored by yann300's avatar yann300

implement int, uint decoding

parent 4e5f7002
'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'
}
'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
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'
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment