Last active
June 30, 2022 20:50
-
-
Save daopunk/646b35cfe220cb816e78c27b13ae017b to your computer and use it in GitHub Desktop.
getLongStr to access >32 byte strings in Ethereum state with ethers.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function getLongStr(slot, contractAddress) { | |
| const paddedSlot = utils.hexZeroPad(slot, 32); | |
| const storageReference = await ethers.provider.getStorageAt(contractAddress, paddedSlot); | |
| const baseSlot = utils.keccak256(paddedSlot); | |
| const sLength = BigNumber.from(storageReference).shr(1).toNumber(); | |
| const totalSlots = Math.ceil(sLength / 32); | |
| let storageLocation = BigNumber.from(baseSlot).toHexString(); | |
| let str = ""; | |
| for (let i=1; i <= totalSlots; i++) { | |
| const stringDataPerSlot = await ethers.provider.getStorageAt(contractAddress, storageLocation); | |
| str = str.concat(utils.toUtf8String(stringDataPerSlot)); | |
| storageLocation = BigNumber.from(baseSlot).add(i).toHexString(); | |
| } | |
| return str.replace(/\x00/g, ''); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment