solidity decoder types updated

pull/5370/head
aniket-engg 4 years ago committed by Aniket
parent e09d21e5c7
commit 1c895cdb7a
  1. 4
      libs/remix-debug/src/solidity-decoder/types/Address.ts
  2. 4
      libs/remix-debug/src/solidity-decoder/types/ArrayType.ts
  3. 4
      libs/remix-debug/src/solidity-decoder/types/Bool.ts
  4. 3
      libs/remix-debug/src/solidity-decoder/types/DynamicByteArray.ts
  5. 4
      libs/remix-debug/src/solidity-decoder/types/Enum.ts
  6. 4
      libs/remix-debug/src/solidity-decoder/types/FixedByteArray.ts
  7. 4
      libs/remix-debug/src/solidity-decoder/types/Int.ts
  8. 4
      libs/remix-debug/src/solidity-decoder/types/Mapping.ts
  9. 11
      libs/remix-debug/src/solidity-decoder/types/RefType.ts
  10. 8
      libs/remix-debug/src/solidity-decoder/types/StringType.ts
  11. 4
      libs/remix-debug/src/solidity-decoder/types/Struct.ts
  12. 4
      libs/remix-debug/src/solidity-decoder/types/Uint.ts
  13. 8
      libs/remix-debug/src/solidity-decoder/types/ValueType.ts
  14. 31
      libs/remix-debug/src/solidity-decoder/types/util.ts

@ -2,7 +2,7 @@
const util = require('./util') const util = require('./util')
const ValueType = require('./ValueType') const ValueType = require('./ValueType')
class Address extends ValueType { export class Address extends ValueType {
constructor () { constructor () {
super(1, 20, 'address') super(1, 20, 'address')
} }
@ -14,5 +14,3 @@ class Address extends ValueType {
return '0x' + util.extractHexByteSlice(value, this.storageBytes, 0).toUpperCase() return '0x' + util.extractHexByteSlice(value, this.storageBytes, 0).toUpperCase()
} }
} }
module.exports = Address

@ -5,7 +5,7 @@ const sha3256 = remixLib.util.sha3_256
const BN = require('ethereumjs-util').BN const BN = require('ethereumjs-util').BN
const RefType = require('./RefType') const RefType = require('./RefType')
class ArrayType extends RefType { export class ArrayType extends RefType {
constructor (underlyingType, arraySize, location) { constructor (underlyingType, arraySize, location) {
let storageSlots = null let storageSlots = null
@ -104,5 +104,3 @@ class ArrayType extends RefType {
} }
} }
} }
module.exports = ArrayType

@ -2,7 +2,7 @@
const ValueType = require('./ValueType') const ValueType = require('./ValueType')
const util = require('./util') const util = require('./util')
class Bool extends ValueType { export class Bool extends ValueType {
constructor () { constructor () {
super(1, 1, 'bool') super(1, 1, 'bool')
} }
@ -15,5 +15,3 @@ class Bool extends ValueType {
return value !== '00' return value !== '00'
} }
} }
module.exports = Bool

@ -5,7 +5,7 @@ const sha3256 = remixLib.util.sha3_256
const BN = require('ethereumjs-util').BN const BN = require('ethereumjs-util').BN
const RefType = require('./RefType') const RefType = require('./RefType')
class DynamicByteArray extends RefType { export class DynamicByteArray extends RefType {
constructor (location) { constructor (location) {
super(1, 32, 'bytes', location) super(1, 32, 'bytes', location)
} }
@ -56,4 +56,3 @@ class DynamicByteArray extends RefType {
} }
} }
module.exports = DynamicByteArray

@ -1,7 +1,7 @@
'use strict' 'use strict'
const ValueType = require('./ValueType') const ValueType = require('./ValueType')
class Enum extends ValueType { export class Enum extends ValueType {
constructor (enumDef) { constructor (enumDef) {
let storageBytes = 0 let storageBytes = 0
let length = enumDef.members.length let length = enumDef.members.length
@ -24,5 +24,3 @@ class Enum extends ValueType {
return 'INVALID_ENUM<' + value + '>' return 'INVALID_ENUM<' + value + '>'
} }
} }
module.exports = Enum

@ -1,7 +1,7 @@
'use strict' 'use strict'
const ValueType = require('./ValueType') const ValueType = require('./ValueType')
class FixedByteArray extends ValueType { export class FixedByteArray extends ValueType {
constructor (storageBytes) { constructor (storageBytes) {
super(1, storageBytes, 'bytes' + storageBytes) super(1, storageBytes, 'bytes' + storageBytes)
} }
@ -10,5 +10,3 @@ class FixedByteArray extends ValueType {
return '0x' + value.substr(0, 2 * this.storageBytes).toUpperCase() return '0x' + value.substr(0, 2 * this.storageBytes).toUpperCase()
} }
} }
module.exports = FixedByteArray

@ -2,7 +2,7 @@
const util = require('./util') const util = require('./util')
const ValueType = require('./ValueType') const ValueType = require('./ValueType')
class Int extends ValueType { export class Int extends ValueType {
constructor (storageBytes) { constructor (storageBytes) {
super(1, storageBytes, 'int' + storageBytes * 8) super(1, storageBytes, 'int' + storageBytes * 8)
} }
@ -12,5 +12,3 @@ class Int extends ValueType {
return util.decodeIntFromHex(value, this.storageBytes, true) return util.decodeIntFromHex(value, this.storageBytes, true)
} }
} }
module.exports = Int

@ -3,7 +3,7 @@ const RefType = require('./RefType')
const util = require('./util') const util = require('./util')
const ethutil = require('ethereumjs-util') const ethutil = require('ethereumjs-util')
class Mapping extends RefType { export class Mapping extends RefType {
constructor (underlyingTypes, location, fullType) { constructor (underlyingTypes, location, fullType) {
super(1, 32, fullType, 'storage') super(1, 32, fullType, 'storage')
this.keyType = underlyingTypes.keyType this.keyType = underlyingTypes.keyType
@ -76,5 +76,3 @@ function concatTypedArrays (a, b) { // a, b TypedArray of same type
c.set(b, a.length) c.set(b, a.length)
return c return c
} }
module.exports = Mapping

@ -1,7 +1,14 @@
'use strict' 'use strict'
const util = require('./util') const util = require('./util')
class RefType { export class RefType {
location
storageSlots
storageBytes
typeName
basicType
constructor (storageSlots, storageBytes, typeName, location) { constructor (storageSlots, storageBytes, typeName, location) {
this.location = location this.location = location
this.storageSlots = storageSlots this.storageSlots = storageSlots
@ -71,5 +78,3 @@ class RefType {
return this.location.indexOf('memory') === 0 return this.location.indexOf('memory') === 0
} }
} }
module.exports = RefType

@ -1,7 +1,7 @@
'use strict' 'use strict'
const DynamicBytes = require('./DynamicByteArray') const DynamicBytes = require('./DynamicByteArray')
class StringType extends DynamicBytes { export class StringType extends DynamicBytes {
constructor (location) { constructor (location) {
super(location) super(location)
this.typeName = 'string' this.typeName = 'string'
@ -41,12 +41,10 @@ function format (decoded) {
value = value.replace('0x', '').replace(/(..)/g, '%$1') value = value.replace('0x', '').replace(/(..)/g, '%$1')
const ret = {length: decoded.length, raw: decoded.value, type: 'string'} const ret = {length: decoded.length, raw: decoded.value, type: 'string'}
try { try {
ret.value = decodeURIComponent(value) ret['value'] = decodeURIComponent(value)
} catch (e) { } catch (e) {
ret.error = 'Invalid UTF8 encoding' ret['error'] = 'Invalid UTF8 encoding'
ret.raw = decoded.value ret.raw = decoded.value
} }
return ret return ret
} }
module.exports = StringType

@ -2,7 +2,7 @@
const util = require('./util') const util = require('./util')
const RefType = require('./RefType') const RefType = require('./RefType')
class Struct extends RefType { export class Struct extends RefType {
constructor (memberDetails, location, fullType) { constructor (memberDetails, location, fullType) {
super(memberDetails.storageSlots, 32, 'struct ' + fullType, location) super(memberDetails.storageSlots, 32, 'struct ' + fullType, location)
this.members = memberDetails.members this.members = memberDetails.members
@ -36,5 +36,3 @@ class Struct extends RefType {
return {value: ret, type: this.typeName} return {value: ret, type: this.typeName}
} }
} }
module.exports = Struct

@ -2,7 +2,7 @@
const util = require('./util') const util = require('./util')
const ValueType = require('./ValueType') const ValueType = require('./ValueType')
class Uint extends ValueType { export class Uint extends ValueType {
constructor (storageBytes) { constructor (storageBytes) {
super(1, storageBytes, 'uint' + storageBytes * 8) super(1, storageBytes, 'uint' + storageBytes * 8)
} }
@ -12,5 +12,3 @@ class Uint extends ValueType {
return util.decodeIntFromHex(value, this.storageBytes, false) return util.decodeIntFromHex(value, this.storageBytes, false)
} }
} }
module.exports = Uint

@ -1,7 +1,13 @@
'use strict' 'use strict'
var util = require('./util') var util = require('./util')
class ValueType { export class ValueType {
storageSlots
storageBytes
typeName
basicType
constructor (storageSlots, storageBytes, typeName) { constructor (storageSlots, storageBytes, typeName) {
this.storageSlots = storageSlots this.storageSlots = storageSlots
this.storageBytes = storageBytes this.storageBytes = storageBytes

@ -1,8 +1,7 @@
'use strict' 'use strict'
const ethutil = require('ethereumjs-util') import { BN, bufferToHex, unpad } from 'ethereumjs-util'
const BN = require('ethereumjs-util').BN
function decodeIntFromHex (value, byteLength, signed) { export function decodeIntFromHex (value, byteLength, signed) {
let bigNumber = new BN(value, 16) let bigNumber = new BN(value, 16)
if (signed) { if (signed) {
bigNumber = bigNumber.fromTwos(8 * byteLength) bigNumber = bigNumber.fromTwos(8 * byteLength)
@ -10,8 +9,8 @@ function decodeIntFromHex (value, byteLength, signed) {
return bigNumber.toString(10) return bigNumber.toString(10)
} }
function readFromStorage(slot, storageResolver) { export function readFromStorage(slot, storageResolver) {
const hexSlot = '0x' + normalizeHex(ethutil.bufferToHex(slot)) const hexSlot = '0x' + normalizeHex(bufferToHex(slot))
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
storageResolver.storageSlot(hexSlot, (error, slot) => { storageResolver.storageSlot(hexSlot, (error, slot) => {
if (error) { if (error) {
@ -32,7 +31,7 @@ function readFromStorage(slot, storageResolver) {
* @param {Int} byteLength - Length of the byte slice to extract * @param {Int} byteLength - Length of the byte slice to extract
* @param {Int} offsetFromLSB - byte distance from the right end slot value to the right end of the byte slice * @param {Int} offsetFromLSB - byte distance from the right end slot value to the right end of the byte slice
*/ */
function extractHexByteSlice (slotValue, byteLength, offsetFromLSB) { export function extractHexByteSlice (slotValue, byteLength, offsetFromLSB) {
const offset = slotValue.length - 2 * offsetFromLSB - 2 * byteLength const offset = slotValue.length - 2 * offsetFromLSB - 2 * byteLength
return slotValue.substr(offset, 2 * byteLength) return slotValue.substr(offset, 2 * byteLength)
} }
@ -44,7 +43,7 @@ function extractHexByteSlice (slotValue, byteLength, offsetFromLSB) {
* @param {Object} storageResolver - storage resolver * @param {Object} storageResolver - storage resolver
* @param {Int} byteLength - Length of the byte slice to extract * @param {Int} byteLength - Length of the byte slice to extract
*/ */
async function extractHexValue (location, storageResolver, byteLength) { export async function extractHexValue (location, storageResolver, byteLength) {
let slotvalue let slotvalue
try { try {
slotvalue = await readFromStorage(location.slot, storageResolver) slotvalue = await readFromStorage(location.slot, storageResolver)
@ -54,11 +53,11 @@ async function extractHexValue (location, storageResolver, byteLength) {
return extractHexByteSlice(slotvalue, byteLength, location.offset) return extractHexByteSlice(slotvalue, byteLength, location.offset)
} }
function toBN (value) { export function toBN (value) {
if (value instanceof BN) { if (value instanceof BN) {
return value return value
} else if (value.match && value.match(/^(0x)?([a-f0-9]*)$/)) { } else if (value.match && value.match(/^(0x)?([a-f0-9]*)$/)) {
value = ethutil.unpad(value.replace(/^(0x)/, '')) value = unpad(value.replace(/^(0x)/, ''))
value = new BN(value === '' ? '0' : value, 16) value = new BN(value === '' ? '0' : value, 16)
} else if (!isNaN(value)) { } else if (!isNaN(value)) {
value = new BN(value) value = new BN(value)
@ -66,19 +65,19 @@ function toBN (value) {
return value return value
} }
function add (value1, value2) { export function add (value1, value2) {
return toBN(value1).add(toBN(value2)) return toBN(value1).add(toBN(value2))
} }
function sub (value1, value2) { export function sub (value1, value2) {
return toBN(value1).sub(toBN(value2)) return toBN(value1).sub(toBN(value2))
} }
function removeLocation (type) { export function removeLocation (type) {
return type.replace(/( storage ref| storage pointer| memory| calldata)/g, '') return type.replace(/( storage ref| storage pointer| memory| calldata)/g, '')
} }
function extractLocation (type) { export function extractLocation (type) {
let match = type.match(/( storage ref| storage pointer| memory| calldata)?$/) let match = type.match(/( storage ref| storage pointer| memory| calldata)?$/)
if (match[1] !== '') { if (match[1] !== '') {
return match[1].trim() return match[1].trim()
@ -86,7 +85,7 @@ function extractLocation (type) {
return null return null
} }
function extractLocationFromAstVariable (node) { export function extractLocationFromAstVariable (node) {
if (node.storageLocation !== 'default') { if (node.storageLocation !== 'default') {
return node.storageLocation return node.storageLocation
} else if (node.stateVariable) { } else if (node.stateVariable) {
@ -95,12 +94,10 @@ function extractLocationFromAstVariable (node) {
return 'default' // local variables => storage, function parameters & return values => memory, state => storage return 'default' // local variables => storage, function parameters & return values => memory, state => storage
} }
function normalizeHex (hex) { export function normalizeHex (hex) {
hex = hex.replace('0x', '') hex = hex.replace('0x', '')
if (hex.length < 64) { if (hex.length < 64) {
return (new Array(64 - hex.length + 1).join('0')) + hex return (new Array(64 - hex.length + 1).join('0')) + hex
} }
return hex return hex
} }
module.exports = {readFromStorage, decodeIntFromHex, extractHexValue, extractHexByteSlice, toBN, add, sub, extractLocation, removeLocation, normalizeHex, extractLocationFromAstVariable}
Loading…
Cancel
Save