-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for computing function 4-byte selectors from ABI #357
Comments
I can create a PR for it if you agree it's useful and we agree on the interface. |
This may make sense, but generally if you need the sighash, you often need most of the other parts of the interface as well, which would just be thrown away... let descr = new utils.Interface([ "function transfer(address, uint)" ]);
// The signhash
console.log("sighash", descr.functions..transfer.sighash);
// "0xa9059cbb"
// But also, you would often need/want to encode parameters
console.log(descr.functions.transfer.encode(["0x0123456789012345678901234567890123456789", 1234]));
// "0xa9059cbb000000000000000000000000012345678901234567890123456789012345678900000000000000000000000000000000000000000000000000000000000004d2"
// And the ability to parse the response
console.log(descr.functions.transfer.decode(callResponse)); It may make sense to add as a static method on the Interface, but let me think about it. |
Ok, I agree. This is redundant. One other question. Can I also decode parameters using the |
const abi = [
"function transfer(address to, uint256 amount)"
];
const iface = new ethers.utils.Interface(abi);
const transferFunc = iface.functions.transfer;
// Encoding
let address = "0x0123456789012345678901234567890123456789";
let amount = 1000;
let calldata = transferFunc.encode([address, amount]);
console.log(calldata);
// "0xa9059cbb000000000000000000000000012345678901234567890123456789012345678900000000000000000000000000000000000000000000000000000000000003e8"
// Decoding
console.log(iface.parseTransaction({ data: calldata, value: "0x0" }));
// _TransactionDescription {
// args:
// [ '0x0123456789012345678901234567890123456789',
// BigNumber { _hex: '0x03e8' } ],
// decode: [Function],
// name: 'transfer(address,uint256)',
// signature: 'transfer(address,uint256)',
// sighash: '0xa9059cbb',
// value: BigNumber { _hex: '0x0' } } (note this example just pointed out a bug; for now, |
This should now work with Thanks! :) |
Get first the fragment of abi function
Then, get bytes4 signature of function
|
It would be nice to be able to compute 4-byte selectors from ABI. I'm talking about those: https://www.4byte.directory/signatures/
Smth like:
Should be pretty simple as it just requires removing the parameter names and hashing the thing taking the first 4 bytes.
The text was updated successfully, but these errors were encountered: