-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add gen script #44
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: EIP-712 Hashing Bug with Dynamic Bytes
The isAtomic function incorrectly treats dynamic bytes as atomic, causing them to be directly encoded instead of keccak256 hashed in the generated EIP-712 hash function. Additionally, several variables (objs, fieldList, fields, hashString, functionString, obj) are implicitly declared as global, leading to scope pollution and potential conflicts.
gen-js/gen-sol.js#L3-L98
Lines 3 to 98 in 0a77207
| function isAtomic(t) { | |
| return t.startsWith('bytes') || t.startsWith('uint') || t.startsWith('int') || t === 'bool' || t === 'address'; | |
| } | |
| objs = {} | |
| Object.keys(x).forEach(key => { | |
| objs[key] = { | |
| arr: x[key], | |
| objName: key, | |
| deps: [], | |
| }; | |
| }); | |
| function getDeps(obj, deps = []) { | |
| obj.arr.forEach(element => { | |
| if (objs[element.type]) { | |
| if (!deps.includes(element.type)) { | |
| deps.push(element.type); | |
| getDeps(objs[element.type], deps); | |
| } | |
| } | |
| }); | |
| deps.sort(); | |
| return deps; | |
| } | |
| function getInitHashString(obj) { | |
| fieldList = []; | |
| obj.arr.forEach(element => { | |
| fieldList.push(`${element.type} ${element.name}`); | |
| }); | |
| fields = fieldList.join(','); | |
| hashString = `${obj.objName}(${fields})`; | |
| return hashString; | |
| } | |
| function getFullHashString(obj) { | |
| hashString = getInitHashString(obj); | |
| obj.deps.forEach(dep => { | |
| hashString += getInitHashString(objs[dep]); | |
| }); | |
| return hashString; | |
| } | |
| function genTypeHashStatement(obj) { | |
| return ` // keccak256( | |
| // \"${getFullHashString(obj)}\" | |
| // ); | |
| bytes32 constant ${obj.objName}_TYPEHASH = ${keccak256(getFullHashString(obj))};` // do NOT delete spaces | |
| } | |
| function genStruct(obj) { | |
| fieldList = []; | |
| obj.arr.forEach(element => { | |
| ename = element.name; | |
| if (element.name === 'type') { | |
| ename = '_type'; | |
| } | |
| fieldList.push(` ${element.type} ${ename};\n`); | |
| }); | |
| return ` struct ${obj.objName} {\n${fieldList.join('')} }` // do NOT delete spaces | |
| } | |
| function genHashFunction(obj) { | |
| fieldList = [`${obj.objName}_TYPEHASH`]; | |
| obj.arr.forEach(element => { | |
| ename = element.name; | |
| if (element.name === 'type') { | |
| ename = '_type'; | |
| } | |
| if (isAtomic(element.type)) { | |
| hashLine = `obj.${ename}`; | |
| } else if (element.type === 'string') { | |
| hashLine = `keccak256(bytes(obj.${ename}))`; | |
| } else if (element.type === 'bytes') { | |
| hashLine = `keccak256(obj.${ename})`; | |
| } else { | |
| hashLine = `_hash(obj.${ename})`; | |
| } | |
| fieldList.push(hashLine); | |
| }); | |
| functionString = ` function _hash(${obj.objName} memory obj) internal pure returns (bytes32) { | |
| return keccak256(abi.encode( | |
| ${fieldList.join(',\n ')} | |
| )); | |
| }` | |
| return functionString; | |
| } | |
| Object.keys(objs).forEach(key => { | |
| objs[key].deps = getDeps(objs[key]); | |
| }); | |
| Object.keys(objs).forEach(key => { | |
| obj = objs[key]; |
Comment bugbot run to trigger another review on this PR
Was this report helpful? Give feedback by reacting with 👍 or 👎
No description provided.