question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[question] How to convert address/bignumber to hex string?

See original GitHub issue

Hello, trying to implement the following tutorial in ethers https://medium.com/aigang-network/how-to-read-ethereum-contract-storage-44252c8af925 in order to load data from a “private” mapping.

With respect to the mapping example, how would the following code look in ethers?

index = '0000000000000000000000000000000000000000000000000000000000000005'
key =  '00000000000000000000000xbccc714d56bc0da0fd33d96d2a87b680dd6d0df6'
let newKey =  web3.sha3(key + index, {"encoding":"hex"})
console.log(web3.eth.getStorageAt(contractAddress, newKey))
console.log('DEC: ' + web3.toDecimal(web3.eth.getStorageAt(contractAddress, newKey)))

I have tried the following which raises an error EXCEPTION Error: data out-of-bounds (length=32, offset=64, code=BUFFER_OVERRUN, version=abi/5.0.5):

let key = ethers.getAddress('0x...');
let offset = ethers.BigNumber.from(5);
let mappingKey = ethers.utils.keccak256([key, offset]);
let mappingData = await ethers.provider.getStorageAt(contractAddress, mappingKey, blockNumber);

Specifically, how does one convert the key and offset to appropriately sized hex strings for the hash function? Thanks in advance!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
ricmoocommented, Oct 3, 2020

Heya!

You are on the right track. To accomplish the same thing in ethers, here is the demo re-written a bit, with more documentation added.

const { BigNumber, ethers, utils } = require("ethers");

(async function() {
    // Connect to Ropsten
    const provider = ethers.getDefaultProvider("ropsten");

    // The contract address
    const addr = "0xf1f5896ace3a78c347eb7eab503450bc93bd0c3b";

    // The key into the mapping which is located at the given Solidity slot
    const key = "0xbccc714d56bc0da0fd33d96d2a87b680dd6d0df6";
    const index = 5;

    // The pre-image used to compute the Storage location
    const newKeyPreimage = utils.concat([
        // Mappings' keys in Solidity must all be word-aligned (32 bytes)
        utils.hexZeroPad(key, 32),

        // Similarly with the slot-index into the Solidity variable layout
        utils.hexZeroPad(BigNumber.from(index).toHexString(), 32),
    ]);

    console.log("New Key Preimage:", utils.hexlify(newKeyPreimage));
    // "0x000000000000000000000000bccc714d56bc0da0fd33d96d2a87b680dd6d0df60000000000000000000000000000000000000000000000000000000000000005"

    const newKey = utils.keccak256(newKeyPreimage);
    console.log("New Key:", newKey);
    // "0xafef6be2b419f4d69d56fe34788202bf06650015554457a2470181981bcce7ef"

    const value = await provider.getStorageAt(addr, newKey);
    console.log("Value:", value);
    // "0x0000000000000000000000000000000000000000000000000000000000000058"
})();

I hope that helps! Let me know if you have any other questions. 😃

1reaction
shanecarey17commented, Oct 3, 2020

Ah! Your solution worked perfectly, I was dealing with parsing the return of getStorageAt but was confused because the stack trace did not show which function I had called. Obviously this is because its an async function, but I hadnt realized. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert big number (hex value) to a string or number
Try this const theUsedAccount = await provider.send("eth_requestAccounts", [0]); const balance = await provider.
Read more >
Truffle CMD Bignumber to number; hex to string
I tried instance.getAge().toNumber() but that throws an error saying that toNumber isn't defined. I checked How to convert BigNumber to Number ...
Read more >
BigNumber - ethers
A HexString or a decimal string, either of which may be negative. ... The BigNumber class is immutable, so no operations can change...
Read more >
Big number converter - Mobilefish.com
Big number converter. This service allows you to convert big positive integer numbers into binary, decimal, hexadecimal or base64 encoding schemes.
Read more >
web3.utils — web3.js 1.0.0 documentation
For safe conversion of many types, incl BigNumber.js use utils.toBN. Parameters¶. mixed - String|Number : A number, number string or HEX string to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found