Commit e7d48971 authored by yann300's avatar yann300

fix dynamic bytes

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