fix dynamic bytes

pull/7/head
yann300 8 years ago
parent 38786dbe3e
commit e7d4897129
  1. 2
      src/solidity/types/Address.js
  2. 4
      src/solidity/types/Bool.js
  3. 19
      src/solidity/types/DynamicByteArray.js
  4. 2
      src/solidity/types/Enum.js
  5. 4
      src/solidity/types/FixedByteArray.js
  6. 2
      src/solidity/types/StringType.js
  7. 16
      src/solidity/types/util.js
  8. 1
      test/solidity/storageDecoder.js

@ -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) {

Loading…
Cancel
Save