Commit e7d48971 authored by yann300's avatar yann300

fix dynamic bytes

parent 38786dbe
......@@ -8,7 +8,7 @@ function Address () {
}
Address.prototype.decodeFromStorage = function (location, storageContent) {
return util.extractValue(location, storageContent, this.storageBytes)
return '0x' + util.extractHexByte(location, storageContent, this.storageBytes)
}
module.exports = Address
......@@ -8,8 +8,8 @@ function Bool () {
}
Bool.prototype.decodeFromStorage = function (location, storageContent) {
var value = util.extractValue(location, storageContent, this.storageBytes)
return value !== '0x00'
var value = util.extractHexByte(location, storageContent, this.storageBytes)
return value !== '00'
}
module.exports = Bool
......@@ -9,26 +9,23 @@ function DynamicByteArray () {
}
DynamicByteArray.prototype.decodeFromStorage = function (location, storageContent) {
var value = util.extractValue(location, storageContent, this.storageBytes)
var key = util.dynamicTypePointer(location)
var value = util.extractHexByte(location, storageContent, this.storageBytes)
var key = util.sha3(location.slot)
if (storageContent[key] && storageContent[key] !== '0x') {
var ret = ''
var length = parseInt(value) - 1
var slots = Math.ceil(length / 64)
var currentSlot = storageContent[key]
key = new BN(key.replace('0x', ''), 16)
for (var k = 0; k < slots; k++) {
if (!currentSlot) {
break
}
ret += currentSlot.replace('0x', '')
var regex = /(00)+$/
while (currentSlot) {
currentSlot = currentSlot.replace('0x', '').replace(regex, '')
ret += currentSlot
key = key.add(new BN(1))
currentSlot = storageContent['0x' + key.toString(16)]
}
return ret.substr(0, length)
return '0x' + ret
} else {
var size = value.substr(value.length - 2, 2)
return value.substr(0, parseInt(size, 16) + 2)
return '0x' + value.substr(0, parseInt(size, 16))
}
}
......
......@@ -14,7 +14,7 @@ function Enum (enumDef) {
}
Enum.prototype.decodeFromStorage = function (location, storageContent) {
var value = util.extractValue(location, storageContent, this.storageBytes)
var value = util.extractHexByte(location, storageContent, this.storageBytes)
value = parseInt(value)
return this.enumDef.children[value].attributes.name
}
......
......@@ -9,8 +9,8 @@ function FixedByteArray (storageBytes) {
}
FixedByteArray.prototype.decodeFromStorage = function (location, storageContent) {
var value = util.extractValue(location, storageContent, this.storageBytes)
return '0x' + utileth.unpad(value.replace('0x', '')).toUpperCase()
var value = util.extractHexByte(location, storageContent, this.storageBytes)
return '0x' + utileth.unpad(value).toUpperCase()
}
module.exports = FixedByteArray
......@@ -10,8 +10,8 @@ function StringType () {
StringType.prototype.decodeFromStorage = function (location, storageContent) {
var value = this.dynamicBytes.decodeFromStorage(location, storageContent)
value = value.replace('0x', '')
var ret = ''
value = value.replace('0x', '')
for (var k = 0; k < value.length; k += 2) {
var raw = value.substr(k, 2)
var str = String.fromCharCode(parseInt(raw, 16))
......
......@@ -3,9 +3,10 @@ var ethutil = require('ethereumjs-util')
var BN = require('ethereumjs-util').BN
module.exports = {
extractHexByteSlice: extractHexByteSlice,
readFromStorage: readFromStorage,
decodeInt: decodeInt
decodeInt: decodeInt,
extractHexByte: extractHexByte,
sha3: sha3
}
function decodeInt (location, storageContent, byteLength, signed) {
......@@ -40,3 +41,14 @@ function extractHexByteSlice (slotValue, byteLength, offsetFromLSB) {
var offset = slotValue.length - 2 * offsetFromLSB - 2 * byteLength
return slotValue.substr(offset, 2 * byteLength)
}
function extractHexByte (location, storageContent, byteLength) {
var slotvalue = readFromStorage(location.slot, storageContent)
return extractHexByteSlice(slotvalue, byteLength, location.offset)
}
function sha3 (slot) {
var remoteSlot = ethutil.bufferToHex(ethutil.setLengthLeft(slot, 32))
var key = ethutil.sha3(remoteSlot)
return ethutil.bufferToHex(key)
}
......@@ -49,7 +49,6 @@ function testIntStorage (st) {
st.equal(decoded['i256'], '0')
st.equal(decoded['i'], '0')
st.equal(decoded['ishrink'], '0')
st.end()
}
function testByteStorage (st) {
......
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