github.com/ethereum-optimism/optimism@v1.7.2/packages/core-utils/src/gnosis-safe-checksum.ts (about) 1 import { ethers } from 'ethers' 2 3 // Slightly modified from: 4 // https://github.com/safe-global/safe-react-apps/blob/development/apps/tx-builder/src/lib/checksum.ts 5 6 const stringifyReplacer = (_: string, value: any) => 7 value === undefined ? null : value 8 9 const serializeJSONObject = (json: any): string => { 10 if (Array.isArray(json)) { 11 return `[${json.map((el) => serializeJSONObject(el)).join(',')}]` 12 } 13 14 if (typeof json === 'object' && json !== null) { 15 let acc = '' 16 const keys = Object.keys(json).sort() 17 acc += `{${JSON.stringify(keys, stringifyReplacer)}` 18 19 for (const key of keys) { 20 acc += `${serializeJSONObject(json[key])},` 21 } 22 23 return `${acc}}` 24 } 25 26 return `${JSON.stringify(json, stringifyReplacer)}` 27 } 28 29 const calculateChecksum = (batchFile: any): string | undefined => { 30 const serialized = serializeJSONObject({ 31 ...batchFile, 32 meta: { ...batchFile.meta, name: null }, 33 }) 34 const sha = ethers.utils.solidityKeccak256(['string'], [serialized]) 35 36 return sha || undefined 37 } 38 39 export const addChecksum = (batchFile: any): any => { 40 return { 41 ...batchFile, 42 meta: { 43 ...batchFile.meta, 44 checksum: calculateChecksum(batchFile), 45 }, 46 } 47 }