Cryo Explorer Ethereum Mainnet

Address Contract Verified

Address 0xB453Ba76d9CA952115E9e32CA069C96e846c5D9D
Balance 0 ETH
Nonce 1
Code Size 9776 bytes
Indexed Transactions 0
External Etherscan · Sourcify

Contract Bytecode

9776 bytes
0x60806040526004361015610011575f80fd5b5f3560e01c80630170863214611c77578063046dc16614611ba45780630d49b711146117d65780630d9b50111461175557806312e9a03f146116d45780631a296e02146116ac5780631c31f7101461162d5780633d55e9dc14610f3a578063565a2e2c14610f1257806360ac85d614610e5a578063715018a614610df757806379ba509714610d7257806384b0196e14610c6b57806388543f0e14610c465780638da5cb5b14610c1f57806392b3462014610b0f578063ae7c82a4146109ff578063b35ee93114610842578063c8cb3834146106f4578063e30c3978146106cc578063e841436f1461063b578063f2fde38b146105cd578063f7ece0cf146102a45763f84f2bb114610121575f80fd5b346102a05760203660031901126102a05761013a611d56565b610142612192565b6001600160a01b0381165f8181526007602052604090205490919060ff1661025b5761016f821515611ed9565b6006549061010082101561020b57600160401b8210156101f75761019c8260016101ba94016006556120ad565b90919060018060a01b038084549260031b9316831b921b1916179055565b805f52600760205260405f20600160ff198254161790557f268cfd1ffbcc858c8b0c9c92b0913ae4b84050562b073e137553c65f2dadf8d95f80a2005b634e487b7160e01b5f52604160045260245ffd5b60405162461bcd60e51b815260206004820152602260248201527f43616e6e6f7420616464206d6f7265207468616e203235362061646472657373604482015261657360f01b6064820152608490fd5b60405162461bcd60e51b815260206004820152601b60248201527f54686973206164647265737320616c72656164792065786973747300000000006044820152606490fd5b5f80fd5b346102a05760603660031901126102a05767ffffffffffffffff6004358181116102a0576102d6903690600401611d80565b6024929192916024358181116102a0576102f4903690600401611d80565b9390916044359081116102a05761030f903690600401611d80565b335f526020966009885261032960ff60405f205416611f25565b818703610569578587036105065786156104ac57969493929190610351610100881115611f84565b5f975f955b88871061039a57604080518b815261ffff8b1660208201527fde16772e5c4365a3057b0336ad2eef600c1aad8cd2b5b3146aded1c20da7186691819081015b0390a1005b90919293949596986103ad888a89612117565b6103b690612127565b600c54906103c58a8789612117565b35918c6040519283918201946323b872dd60e01b8652600160a01b6001900384168c8401926104109360409194939294606082019560018060a01b0380921683521660208201520152565b03601f19810183526104229083611e2d565b61042d8b8688612117565b61043690612127565b91519060a01c62ffffff16925f8094938194f19061045261213b565b829060019361047c575b501561047557811b81175b999701959493929190610356565b811b610467565b8051801592508d908315610494575b5050508c61045c565b6104a4935082018101910161217a565b8c8c8161048b565b60405162461bcd60e51b815260048101899052602c60248201527f546865206e756d626572206f6620736f7572636573206d75737420626520677260448201526b06561746572207468616e20360a41b6064820152608490fd5b60405162461bcd60e51b815260048101899052603560248201527f546865206e756d626572206f6620736f7572636573206d757374206d6174636860448201527420746865206e756d626572206f6620746f6b656e7360581b6064820152608490fd5b60405162461bcd60e51b815260048101899052603660248201527f546865206e756d626572206f6620736f7572636573206d757374206d6174636860448201527520746865206e756d626572206f6620616d6f756e747360501b6064820152608490fd5b346102a05760203660031901126102a0576105e6611d56565b6105ee612192565b60018060a01b0380911690816bffffffffffffffffffffffff60a01b60015416176001555f54167f38d16b8cac22d99fc7c124b9cd0de2d3fa1faef420bfe791d8c362d765e227005f80a3005b346102a0575f3660031901126102a057604051806006549182815260208091019260065f527ff652222313e28459528d920b65115c16c04f3efc82aaedc97be59f3f377c0d3f915f905b8282106106ac576106a88561069c81890382611e2d565b60405191829182611db1565b0390f35b83546001600160a01b031686529485019460019384019390910190610685565b346102a0575f3660031901126102a0576001546040516001600160a01b039091168152602090f35b346102a0576020806003193601126102a05761070e611d56565b90610717612192565b6001600160a01b0382165f81815260058352604090205460ff166107fd57610740811515611ed9565b6004546101008110156107ad57600160401b8110156101f7577fedfa2f73691730ba0803543209339afdd054637240c6ff0a7ae0dad3c191648c9361019c82600161078e94016004556120e2565b805f526005825260405f20600160ff19825416179055604051908152a1005b60405162461bcd60e51b815260048101849052602260248201527f43616e6e6f7420616464206d6f7265207468616e203235362061646472657373604482015261657360f01b6064820152608490fd5b60405162461bcd60e51b815260048101839052601b60248201527f54686973206164647265737320616c72656164792065786973747300000000006044820152606490fd5b346102a05760203660031901126102a05761085b611d56565b610863612192565b6001600160a01b03908116610879811515611ed9565b805f52600560205260ff9160ff60405f205416156109ba576004545f93845b8282821610610986575b50505f1993848201918211610972576108ee926108c16108ca936120e2565b939054926120e2565b92909360031b1c169060018060a01b038084549260031b9316831b921b1916179055565b60045490811561095e577f0820ed7a122a6fc9da8baebd1b26fd87a75060c4d45f69af14c3bfc5f697a4ae926020920161093f61092a826120e2565b81549060018060a01b039060031b1b19169055565b600455805f526005825260405f2060ff198154169055604051908152a1005b634e487b7160e01b5f52603160045260245ffd5b634e487b7160e01b5f52601160045260245ffd5b8484610991836120e2565b90549060031b1c16146109af57811681811461097257600101610898565b9450859050806108a2565b60405162461bcd60e51b815260206004820152601b60248201527f54686973206164647265737320646f6573206e6f7420657869737400000000006044820152606490fd5b346102a05760203660031901126102a057610a18611d56565b610a20612192565b6001600160a01b0390811690610a37821515611ed9565b815f52600760205260ff9060ff60405f205416156109ba576006545f92835b8282821610610adb575b50505f199283820191821161097257610a8892610a7f6108ca936120ad565b939054926120ad565b600654801561095e5701610a9e61092a826120ad565b600655805f52600760205260405f2060ff1981541690557faa90e6027f3fa1cff5b8bf3d47699b427ea4d86aff9205ce0b950651699ec8535f80a2005b8584610ae6836120ad565b90549060031b1c1614610b0457811681811461097257600101610a56565b935085905080610a60565b346102a05760203660031901126102a057610b28611d56565b610b30612192565b6001600160a01b0390811690610b47821515611ed9565b815f52600960205260ff9060ff60405f205416156109ba576008545f92835b8282821610610beb575b50505f199283820191821161097257610b9892610b8f6108ca93612078565b93905492612078565b600854801561095e5701610bae61092a82612078565b600855805f52600960205260405f2060ff1981541690557f6df77230bf10214430f5e1f2c440c26235bb596e7c441b0b1b34b3ad87ff5a775f80a2005b8584610bf683612078565b90549060031b1c1614610c1457811681811461097257600101610b66565b935085905080610b70565b346102a0575f3660031901126102a0575f546040516001600160a01b039091168152602090f35b346102a0575f3660031901126102a057602062ffffff600c5460a01c16604051908152f35b346102a0575f3660031901126102a057610ca47f466f78436f6e6e6563740000000000000000000000000000000000000000000a6123e0565b610ccd7f3100000000000000000000000000000000000000000000000000000000000001612507565b60405160208082019282841067ffffffffffffffff8511176101f757916020610d278594610d1997966040525f8452604051978897600f60f81b895260e0858a015260e0890190611e4f565b908782036040890152611e4f565b914660608701523060808701525f60a087015285830360c087015251918281520192915f5b828110610d5b57505050500390f35b835185528695509381019392810192600101610d4c565b346102a0575f3660031901126102a0576001546001600160a01b033381831603610ddf576bffffffffffffffffffffffff60a01b8092166001555f549133908316175f553391167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e05f80a3005b60405163118cdaa760e01b8152336004820152602490fd5b346102a0575f3660031901126102a057610e0f612192565b600180546001600160a01b03199081169091555f80549182168155906001600160a01b03167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08280a3005b346102a05760203660031901126102a057610e73611d56565b610e7b612192565b6001600160a01b0381165f8181526009602052604090205490919060ff1661025b57610ea8821515611ed9565b6008549061010082101561020b57600160401b8210156101f75761019c826001610ed59401600855612078565b805f52600960205260405f20600160ff198254161790557f53835a305263ea069766c10e84a0dca8ed2ec1972741a2739691ba71ab8646fb5f80a2005b346102a0575f3660031901126102a057600c546040516001600160a01b039091168152602090f35b346102a0576101203660031901126102a05767ffffffffffffffff6024358181116102a057610f6d903690600401611d80565b90916044358181116102a057610f87903690600401611d80565b92606435928084116102a057366023850112156102a0578360040135938185116101f7578460051b60405195610fc06020830188611e2d565b865260246020870191830101913683116102a057602401905b828210611615575050506084359081116102a057610ffb903690600401611d80565b959060ff60c4351660c435036102a05760a43542116115d1576004355f52600a60205260ff60405f20541661157d57335f52600760205261104260ff60405f205416611f25565b868603611525578286036114cd578451938415958688811597886114c3575b50501561146b57871561141e5761107c610100891115611f84565b6004355f52600a60205260405f20600160ff198254161790556040516110b8816110aa602082018989611fd0565b03601f198101835282611e2d565b60208151910120886040516110d6816110aa60208201809589611fd0565b51902060405190916001600160fb1b038c116102a0576111b4928261111260208f956111ab9660051b808c848401378101038084520182611e2d565b6020815191012090604051927fd60669c8706c9bca64c8413c9803046610614781e97930f95003297d3fd840e7602085015260043560408501526060840152608083015260a082015260a43560c082015260c0815261117081611df5565b6020815191012061117f6121a5565b906040519161190160f01b835260028301526022820152610104359060e43590604260c43591206122d0565b9092919261235d565b600b546001600160a01b039081169116036113c757979594939291905f985f965b89881061121857604080518c815261ffff8c1660208201527ff7279a39c173eff8423944f5549c89148efa02a3f0a80be8feb8d11b9cbea08f9181908101610395565b90919293949596979987829061139e575b15611359576001905f8087878f8f8f808f8f6112cb6112898f958f9861128361127e838f9c9b61127e9b6112d09d8f1461133d5750611266612046565b905460039190911b1c6001600160a01b03169e612117565b612127565b9b612117565b6040516323b872dd60e01b602082019081526001600160a01b039a8b1660248301529a90991660448a0152356064808a01919091528852969796608489611e2d565b612117565b62ffffff600c5460a01c16935193f16112e761213b565b8161130e575b501561130757811b81175b9a9801969594939291906111d5565b811b6112f8565b8051801592508215611323575b50508d6112ed565b611336925060208091830101910161217a565b8d8061131b565b6001600160a01b0390611351908490612032565b51169e612117565b60405162461bcd60e51b815260206004820152601760248201527f547265617375727920646f6573206e6f742065786973740000000000000000006044820152606490fd5b506001600160a01b036113b18a85612032565b51165f52600560205260ff60405f205416611229565b60405162461bcd60e51b815260206004820152602960248201527f496e76616c6964206d756c74692073656e6420726571756573742064617461206044820152687369676e617475726560b81b6064820152608490fd5b60405162461bcd60e51b815260206004820152603260248201525f805160206125db83398151915260448201527102062652067726561746572207468616e20360741b6064820152608490fd5b60405162461bcd60e51b815260206004820152603f60248201525f805160206125db83398151915260448201527f206d6174636820746865206e756d626572206f662074726561737572696573006064820152608490fd5b149050888b611061565b60405162461bcd60e51b815260206004820152603b60248201525f805160206125db83398151915260448201527f206d6174636820746865206e756d626572206f6620746f6b656e7300000000006064820152608490fd5b60405162461bcd60e51b815260206004820152603c60248201525f805160206125db83398151915260448201527f206d6174636820746865206e756d626572206f6620616d6f756e7473000000006064820152608490fd5b60405162461bcd60e51b815260206004820152602660248201527f5468697320726571756573742068617320616c7265616479206265656e2065786044820152651958dd5d195960d21b6064820152608490fd5b606460405162461bcd60e51b815260206004820152602060248201527f546869732072657175657374206d69737365642074686520646561646c696e656044820152fd5b6020809161162284611d6c565b815201910190610fd9565b346102a05760203660031901126102a057611646611d56565b61164e612192565b600c546001600160a01b03918216918290821661166d82821415611e8d565b611678821515611ed9565b7fe72eaf6addaa195f3c83095031dd08f3a96808dcf047babed1fe4e4f69d6c6225f80a36001600160a01b03191617600c55005b346102a0575f3660031901126102a057600b546040516001600160a01b039091168152602090f35b346102a0575f3660031901126102a057604051806008549182815260208091019260085f527ff3f7a9fe364faab93b216da50a3214154f22a0a2b415b23a84c8169e8b636ee3915f905b828210611735576106a88561069c81890382611e2d565b83546001600160a01b03168652948501946001938401939091019061171e565b346102a0575f3660031901126102a057604051806004549182815260208091019260045f527f8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd19b915f905b8282106117b6576106a88561069c81890382611e2d565b83546001600160a01b03168652948501946001938401939091019061179f565b346102a0576101003660031901126102a05767ffffffffffffffff6024358181116102a057611809903690600401611d80565b6044929192358281116102a057611824903690600401611d80565b926064359081116102a05761183d903690600401611d80565b929060ff60a4351660a435036102a05760843542116115d1576004355f52600a60205260ff60405f20541661157d57335f52600760205261188460ff60405f205416611f25565b838503611525578185036114cd5760605192831596878781159687611b9a575b50501561146b57861561141e576118bf610100881115611f84565b6004355f52600a60205260405f20600160ff198254161790556040516118ed816110aa602082018886611fd0565b60208151910120604051602081019061190b816110aa848d89611fd0565b51902060405190916001600160fb1b0389116102a0576119de928261194660206111ab958d60051b808c848401378101038084520182611e2d565b6020815191012090604051927fd60669c8706c9bca64c8413c9803046610614781e97930f95003297d3fd840e7602085015260043560408501526060840152608083015260a082015260843560c082015260c081526119a481611df5565b602081519101206119b36121a5565b906040519161190160f01b83526002830152602282015260e4359060c43590604260a43591206122d0565b600b546001600160a01b039081169116036113c7579694939291905f975f955b888710611a4157604080518b815261ffff8b1660208201527ff7279a39c173eff8423944f5549c89148efa02a3f0a80be8feb8d11b9cbea08f9181908101610395565b9091929394959698868a90611b72575b15611359576001905f808787828f8f8f8e611ae7611aa5838e611a9f61127e8361127e99611aed9b8d14611b5957611a87612046565b905460039190911b1c6001600160a01b03169d612117565b9a612117565b6040516323b872dd60e01b602082019081526001600160a01b03998a166024830152999098166044890152356064808901919091528752959695608488611e2d565b8c612117565b62ffffff600c5460a01c16935193f1611b0461213b565b81611b2a575b5015611b2357811b81175b9997019594939291906119fe565b811b611b15565b8051801592508215611b3f575b50508c611b0a565b611b52925060208091830101910161217a565b8c80611b37565b6001600160a01b03611b6a8361200b565b51169d612117565b506001600160a01b03611b848961200b565b51165f52600560205260ff60405f205416611a51565b149050878a6118a4565b346102a05760203660031901126102a057611bbd611d56565b611bc5612192565b600b546001600160a01b03918216918116611be283821415611e8d565b8215611c1e5782907f2d025324f0a785e8c12d0a0d91a9caa49df4ef20ff87e0df7213a1d4f3157beb5f80a36001600160a01b03191617600b55005b60405162461bcd60e51b815260206004820152602b60248201527f5a65726f2061646472657373206e6f7420616c6c6f77656420666f722073696760448201526a6e6572206164647265737360a81b6064820152608490fd5b346102a05760203660031901126102a05762ffffff600435818116918282036102a057611ca2612192565b8215611cff5760407f7dd4fb54d26a9aab2535f029e94d8af71856ea25b32a91b277651234bce3c77591600c54948251918660a01c1682526020820152a162ffffff60a01b1990911660a09190911b62ffffff60a01b1617600c55005b60405162461bcd60e51b815260206004820152602960248201527f5472616e7366657220676173206c696d6974206d75737420626520677265617460448201526806572207468616e20360bc1b6064820152608490fd5b600435906001600160a01b03821682036102a057565b35906001600160a01b03821682036102a057565b9181601f840112156102a05782359167ffffffffffffffff83116102a0576020808501948460051b0101116102a057565b60209060206040818301928281528551809452019301915f5b828110611dd8575050505090565b83516001600160a01b031685529381019392810192600101611dca565b60e0810190811067ffffffffffffffff8211176101f757604052565b6040810190811067ffffffffffffffff8211176101f757604052565b90601f8019910116810190811067ffffffffffffffff8211176101f757604052565b91908251928382525f5b848110611e79575050825f602080949584010152601f8019910116010190565b602081830181015184830182015201611e59565b15611e9457565b60405162461bcd60e51b815260206004820152601b60248201527f43616e6e6f7420736574207468652073616d65206164647265737300000000006044820152606490fd5b15611ee057565b60405162461bcd60e51b815260206004820152601860248201527f5a65726f2061646472657373206e6f7420616c6c6f77656400000000000000006044820152606490fd5b15611f2c57565b60405162461bcd60e51b815260206004820152602a60248201527f4f6e6c7920616e206f70657261746f722063616e2065786563757465207468696044820152693990333ab731ba34b7b760b11b6064820152608490fd5b15611f8b57565b60405162461bcd60e51b815260206004820152601d60248201527f546865206d6178696d756d2062617463682073697a65206973203235360000006044820152606490fd5b905f5b818110611fe05750505090565b91929091906001906001600160a01b03611ff986611d6c565b16815260209081019401929101611fd3565b60605181101561201e5760051b60800190565b634e487b7160e01b5f52603260045260245ffd5b805182101561201e5760209160051b010190565b6004541561201e5760045f527f8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd19b905f90565b60085481101561201e5760085f527ff3f7a9fe364faab93b216da50a3214154f22a0a2b415b23a84c8169e8b636ee301905f90565b60065481101561201e5760065f527ff652222313e28459528d920b65115c16c04f3efc82aaedc97be59f3f377c0d3f01905f90565b60045481101561201e5760045f527f8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd19b01905f90565b919081101561201e5760051b0190565b356001600160a01b03811681036102a05790565b3d15612175573d9067ffffffffffffffff82116101f7576040519161216a601f8201601f191660200184611e2d565b82523d5f602084013e565b606090565b908160209103126102a0575180151581036102a05790565b5f546001600160a01b03163303610ddf57565b307f000000000000000000000000b453ba76d9ca952115e9e32ca069c96e846c5d9d6001600160a01b031614806122a7575b15612200577f1bc3d4fefbff71eb2b69e0220c3b5c31b4e9d095e198d806e2e8c791f78843bd90565b60405160208101907f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f82527f718ae74a06a067b4bdd93040e5799741c27d6a7a77ee6a0a8b0a563d539db3fc60408201527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc660608201524660808201523060a082015260a0815260c0810181811067ffffffffffffffff8211176101f75760405251902090565b507f000000000000000000000000000000000000000000000000000000000000000146146121d7565b91907f7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a08411612352579160209360809260ff5f9560405194855216868401526040830152606082015282805260015afa15612347575f516001600160a01b0381161561233d57905f905f90565b505f906001905f90565b6040513d5f823e3d90fd5b5050505f9160039190565b60048110156123cc578061236f575050565b600181036123895760405163f645eedf60e01b8152600490fd5b600281036123aa5760405163fce698f760e01b815260048101839052602490fd5b6003146123b45750565b602490604051906335e2f38360e21b82526004820152fd5b634e487b7160e01b5f52602160045260245ffd5b60ff811461241e5760ff811690601f821161240c576040519161240283611e11565b8252602082015290565b604051632cd44ac360e21b8152600490fd5b506040515f600254906001908260011c600184169283156124fd575b60209485831085146124e95782875286949081156124c9575060011461246c575b505061246992500382611e2d565b90565b9093915060025f527f405787fa12a823e0f2b7631cc41b3ba8828b3321ca811111fa75cd3aa3bb5ace935f915b8183106124b157505061246993508201015f8061245b565b85548784018501529485019486945091830191612499565b91505061246994925060ff191682840152151560051b8201015f8061245b565b634e487b7160e01b5f52602260045260245ffd5b90607f169061243a565b60ff81146125295760ff811690601f821161240c576040519161240283611e11565b506040515f600354906001908260011c600184169283156125d0575b60209485831085146124e95782875286949081156124c9575060011461257357505061246992500382611e2d565b9093915060035f527fc2575a0e9e593c00f959f8c92f12db2869c3395a3b0502d05e2516446f71f85b935f915b8183106125b857505061246993508201015f8061245b565b855487840185015294850194869450918301916125a0565b90607f169061254556fe546865206e756d626572206f662062656e65666963696172696573206d757374a2646970667358221220f48e10c97ce9eb4462fe7bbb6a23bca31cb43abe75c98c5012d353c2ed6bec5064736f6c63430008170033

Verified Source Code Full Match

Compiler: v0.8.23+commit.f704f362 EVM: shanghai Optimization: Yes (200 runs)
FoxConnect.sol 384 lines
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.23;

import "@openzeppelin/contracts/access/Ownable2Step.sol";
import "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
import "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol";
import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";

contract FoxConnect is Ownable2Step, EIP712 {
    address[] treasuries;
    mapping(address => bool) treasuriesMap;

    address[] multiSendOperators;
    mapping(address => bool) multiSendOperatorMap;

    address[] withdrawOperators;
    mapping(address => bool) withdrawOperatorMap;

    mapping(bytes32 => bool) idHashMap;
    address signerAddress;

    address beneficiary;

    uint24 transferFromGasLimit;

    event WithdrawOperatorAdded(address indexed operator);
    event WithdrawOperatorRemoved(address indexed operator);
    event MultiSendOperatorAdded(address indexed operator);
    event MultiSendOperatorRemoved(address indexed operator);
    event BeneficiaryUpdated(address indexed previousBeneficiary, address indexed newBeneficiary);
    event SignerUpdated(address indexed previousSigner, address indexed newSigner);
    event TreasuryAdded(address);
    event TreasuryRemoved(address);
    event WithdrawalStatus(uint256 result, uint16 batchSize);
    event MultiSendStatus(uint256 result, uint16 batchSize);
    event TransferGasLimitUpdated(uint24 oldGasLimit, uint24 newGasLimit);

    uint16 constant MAX_ENTRIES = 256;

    constructor(
        address[] memory _treasuries,
        address _signerAddress,
        address[] memory _withdrawOperators,
        address[] memory _multiSendOperators,
        address _beneficiary,
        uint24 _transferFromGasLimit
    ) Ownable(_msgSender()) EIP712("FoxConnect", "1") {
        require(
            _withdrawOperators.length <= MAX_ENTRIES,
            "Cannot add more than 256 withdraw operators"
        );
        require(
            _multiSendOperators.length <= MAX_ENTRIES,
            "Cannot add more than 256 multi-send operators"
        );
        require(
            _treasuries.length <= MAX_ENTRIES,
            "Cannot add more than 256 treasuries"
        );
        require(
            _withdrawOperators.length > 0,
            "At least one withdraw operator must be provided"
        );
        require(
            _multiSendOperators.length > 0,
            "At least one multi-send operator must be provided"
        );
        require(
            _treasuries.length > 0,
            "At least one treasury address must be provided"
        );
        require(
            _signerAddress != address(0),
            "Zero address not allowed for signer address"
        );
        require(
            _beneficiary != address(0),
            "Zero address not allowed for beneficiary address"
        );

        require(
            _transferFromGasLimit > 0,
            "Transfer gas limit must be greater than 0"
        );

        transferFromGasLimit = _transferFromGasLimit;
        signerAddress = _signerAddress;
        beneficiary = _beneficiary;

        for (uint8 i = 0; i < _treasuries.length; i++) {
            require(
                _treasuries[i] != address(0),
                "Zero address not allowed for treasury address"
            );
            treasuriesMap[_treasuries[i]] = true;
            treasuries.push(_treasuries[i]);
        }

        for (uint8 i = 0; i < _multiSendOperators.length; i++) {
            require(
                _multiSendOperators[i] != address(0),
                "Zero address not allowed for multi-send operator address"
            );
            multiSendOperatorMap[_multiSendOperators[i]] = true;
            multiSendOperators.push(_multiSendOperators[i]);
        }

        for (uint8 i = 0; i < _withdrawOperators.length; i++) {
            require(
                _withdrawOperators[i] != address(0),
                "Zero address not allowed for withdraw operator address"
            );
            withdrawOperatorMap[_withdrawOperators[i]] = true;
            withdrawOperators.push(_withdrawOperators[i]);
        }
    }

    function _addAddress(
        address[] storage addresses,
        address newAddress,
        mapping(address => bool) storage map
    ) private {
        require(!map[newAddress], "This address already exists");
        require(newAddress != address(0), "Zero address not allowed");
        require(addresses.length < MAX_ENTRIES, "Cannot add more than 256 addresses");

        addresses.push(newAddress);
        map[newAddress] = true;
    }

    function _removeAddress(
        address[] storage addresses,
        address addressToRemove,
        mapping(address => bool) storage map
    ) private {
        require(addressToRemove != address(0), "Zero address not allowed");
        require(map[addressToRemove], "This address does not exist");

        uint8 index = 0;
        for (uint8 i = 0; i < addresses.length; i++) {
            if (addresses[i] == addressToRemove) {
                index = i;
                break;
            }
        }
        addresses[index] = addresses[addresses.length - 1];
        addresses.pop();
        map[addressToRemove] = false;
    }

    function setTransferGasLimit(uint24 _transferGasLimit) public onlyOwner {
        require(_transferGasLimit > 0, "Transfer gas limit must be greater than 0");
        emit TransferGasLimitUpdated(transferFromGasLimit, _transferGasLimit);
        transferFromGasLimit = _transferGasLimit;
    }

    function getTransferGasLimit() public view returns (uint24) {
        return transferFromGasLimit;
    }

    function multiSend(
        bytes32 _requestIdHash,
        address[] calldata _tokens,
        address[] calldata _beneficiaries,
        uint256[] calldata _amounts,
        uint256 _deadline,
        uint8 _v,
        bytes32 _r,
        bytes32 _s
    ) public {
        // An uninitialized array is more gas-efficient than using 'new address[](0)'.
        // This way, we avoid the overhead of initializing the array with n elements with the default value.
        address[] memory defaultTreasuries;
        multiSend(
            _requestIdHash,
            _tokens,
            _beneficiaries,
            defaultTreasuries,
            _amounts,
            _deadline,
            _v,
            _r,
            _s
        );
    }

    function multiSend(
        bytes32 _requestIdHash,
        address[] calldata _tokens,
        address[] calldata _beneficiaries,
        address[] memory _treasuries,
        uint256[] calldata _amounts,
        uint256 _deadline,
        uint8 _v,
        bytes32 _r,
        bytes32 _s
    ) public {
        require(
            block.timestamp <= _deadline,
            "This request missed the deadline"
        );

        require(
            !idHashMap[_requestIdHash],
            "This request has already been executed"
        );
        require(
            multiSendOperatorMap[msg.sender],
            "Only an operator can execute this function"
        );
        require(
            _beneficiaries.length == _amounts.length,
            "The number of beneficiaries must match the number of amounts"
        );

        require(
            _beneficiaries.length == _tokens.length,
            "The number of beneficiaries must match the number of tokens"
        );

        bool isDefaultTreasury = _treasuries.length == 0;
        // gas saving - avoid unnecessary checking when it is defaultTreasury
        require(
            isDefaultTreasury || _treasuries.length == _beneficiaries.length,
            "The number of beneficiaries must match the number of treasuries"
        );

        require(
            _beneficiaries.length > 0,
            "The number of beneficiaries must be greater than 0"
        );
        require(_beneficiaries.length <= MAX_ENTRIES, "The maximum batch size is 256");

        idHashMap[_requestIdHash] = true;

        bytes32 TYPEHASH = keccak256("MultiSend(bytes32 idHash,address[] tokens,address[] beneficiaries,uint256[] amounts,uint256 deadline)");
        bytes32 structHash = keccak256(abi.encode(
            TYPEHASH,
            _requestIdHash,
            keccak256(abi.encodePacked(_tokens)),
            keccak256(abi.encodePacked(_beneficiaries)),
            keccak256(abi.encodePacked(_amounts)),
            _deadline
        ));

        bytes32 hash = _hashTypedDataV4(structHash);

        address signer = ECDSA.recover(hash, _v, _r, _s);

        require(signer == signerAddress, "Invalid multi send request data signature");

        uint256 status;
        for (uint256 i = 0; i < _beneficiaries.length; i++) {
            // Checking for isDefaultTreasury || treasuriesMap[_treasuries[i]] is more gas-efficient than only checking for treasuriesMap[_treasuries[i]]
            require(
                isDefaultTreasury || treasuriesMap[_treasuries[i]],
                "Treasury does not exist"
            );

            // Within a batch, it’s possible for one transfer to fail while others succeeds.
            // We are using a low-level call because we need to get the return value of the transfer without reverting the whole transaction.
            // The contract emits an event containing the result of each transfer.
            bytes memory payload = abi.encodeWithSignature(
                "transferFrom(address,address,uint256)",
                isDefaultTreasury ? treasuries[0] : _treasuries[i],
                _beneficiaries[i],
                _amounts[i]
            );
            (bool success, bytes memory result) = _tokens[i].call{ gas: transferFromGasLimit }(payload);
            status = success && (result.length == 0 || abi.decode(result,(bool)))
                ? (status << 1) | 0x1
                : status << 1;
        }

        emit MultiSendStatus(status, uint16(_beneficiaries.length));
    }

    function withdraw(
        address[] calldata tokens,
        address[] calldata sources,
        uint256[] calldata amounts
    ) public {
        require(
            withdrawOperatorMap[msg.sender],
            "Only an operator can execute this function"
        );
        require(
            sources.length == amounts.length,
            "The number of sources must match the number of amounts"
        );

        require(
            sources.length == tokens.length,
            "The number of sources must match the number of tokens"
        );

        require(
            sources.length > 0,
            "The number of sources must be greater than 0"
        );
        require(sources.length <= MAX_ENTRIES, "The maximum batch size is 256");

        uint256 status;

        for (uint256 i = 0; i < sources.length; i++) {
            bytes memory payload = abi.encodeWithSignature(
                "transferFrom(address,address,uint256)",
                sources[i],
                beneficiary,
                amounts[i]
            );
            (bool success, bytes memory result) = tokens[i].call{ gas: transferFromGasLimit }(payload);
            status = success && (result.length == 0 || abi.decode(result,(bool)))
                ? (status << 1) | 0x1
                : status << 1;
        }

        emit WithdrawalStatus(status, uint16(sources.length));
    }

    function setBeneficiary(address _beneficiary) public onlyOwner {
        require(_beneficiary != beneficiary, "Cannot set the same address");
        require(_beneficiary != address(0), "Zero address not allowed");
        emit BeneficiaryUpdated(beneficiary, _beneficiary);
        beneficiary = _beneficiary;
    }

    function getBeneficiary() public view returns (address) {
        return beneficiary;
    }

    function getSignerAddress() public view returns (address) {
        return signerAddress;
    }

    function setSignerAddress(address _signerAddress) public onlyOwner {
        require(_signerAddress != signerAddress, "Cannot set the same address");
        require(_signerAddress != address(0), "Zero address not allowed for signer address");
        emit SignerUpdated(signerAddress, _signerAddress);
        signerAddress = _signerAddress;
    }

    function addTreasury(address _treasury) public onlyOwner {
        _addAddress(treasuries, _treasury, treasuriesMap);
        emit TreasuryAdded(_treasury);
    }

    function removeTreasury(address _treasury) public onlyOwner {
        _removeAddress(treasuries, _treasury, treasuriesMap);
        emit TreasuryRemoved(_treasury);
    }

    function getTreasuries() public view returns (address[] memory) {
        return treasuries;
    }

    function addWithdrawOperator(address _operator) public onlyOwner {
        _addAddress(withdrawOperators, _operator, withdrawOperatorMap);
        emit WithdrawOperatorAdded(_operator);
    }

    function removeWithdrawOperator(address _operator) public onlyOwner {
        _removeAddress(withdrawOperators, _operator, withdrawOperatorMap);
        emit WithdrawOperatorRemoved(_operator);
    }

    function getWithdrawOperators() public view returns (address[] memory) {
        return withdrawOperators;
    }

    function addMultiSendOperator(address _operator) public onlyOwner {
        _addAddress(multiSendOperators, _operator, multiSendOperatorMap);
        emit MultiSendOperatorAdded(_operator);
    }

    function removeMultiSendOperator(address _operator) public onlyOwner {
        _removeAddress(multiSendOperators, _operator, multiSendOperatorMap);
        emit MultiSendOperatorRemoved(_operator);
    }

    function getMultiSendOperators() public view returns (address[] memory) {
        return multiSendOperators;
    }
}
Context.sol 28 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)

pragma solidity ^0.8.20;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}
Strings.sol 94 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/Strings.sol)

pragma solidity ^0.8.20;

import {Math} from "./math/Math.sol";
import {SignedMath} from "./math/SignedMath.sol";

/**
 * @dev String operations.
 */
library Strings {
    bytes16 private constant HEX_DIGITS = "0123456789abcdef";
    uint8 private constant ADDRESS_LENGTH = 20;

    /**
     * @dev The `value` string doesn't fit in the specified `length`.
     */
    error StringsInsufficientHexLength(uint256 value, uint256 length);

    /**
     * @dev Converts a `uint256` to its ASCII `string` decimal representation.
     */
    function toString(uint256 value) internal pure returns (string memory) {
        unchecked {
            uint256 length = Math.log10(value) + 1;
            string memory buffer = new string(length);
            uint256 ptr;
            /// @solidity memory-safe-assembly
            assembly {
                ptr := add(buffer, add(32, length))
            }
            while (true) {
                ptr--;
                /// @solidity memory-safe-assembly
                assembly {
                    mstore8(ptr, byte(mod(value, 10), HEX_DIGITS))
                }
                value /= 10;
                if (value == 0) break;
            }
            return buffer;
        }
    }

    /**
     * @dev Converts a `int256` to its ASCII `string` decimal representation.
     */
    function toStringSigned(int256 value) internal pure returns (string memory) {
        return string.concat(value < 0 ? "-" : "", toString(SignedMath.abs(value)));
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
     */
    function toHexString(uint256 value) internal pure returns (string memory) {
        unchecked {
            return toHexString(value, Math.log256(value) + 1);
        }
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
     */
    function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
        uint256 localValue = value;
        bytes memory buffer = new bytes(2 * length + 2);
        buffer[0] = "0";
        buffer[1] = "x";
        for (uint256 i = 2 * length + 1; i > 1; --i) {
            buffer[i] = HEX_DIGITS[localValue & 0xf];
            localValue >>= 4;
        }
        if (localValue != 0) {
            revert StringsInsufficientHexLength(value, length);
        }
        return string(buffer);
    }

    /**
     * @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal
     * representation.
     */
    function toHexString(address addr) internal pure returns (string memory) {
        return toHexString(uint256(uint160(addr)), ADDRESS_LENGTH);
    }

    /**
     * @dev Returns true if the two strings are equal.
     */
    function equal(string memory a, string memory b) internal pure returns (bool) {
        return bytes(a).length == bytes(b).length && keccak256(bytes(a)) == keccak256(bytes(b));
    }
}
Ownable.sol 100 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)

pragma solidity ^0.8.20;

import {Context} from "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * The initial owner is set to the address provided by the deployer. This can
 * later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    /**
     * @dev The caller account is not authorized to perform an operation.
     */
    error OwnableUnauthorizedAccount(address account);

    /**
     * @dev The owner is not a valid owner account. (eg. `address(0)`)
     */
    error OwnableInvalidOwner(address owner);

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the address provided by the deployer as the initial owner.
     */
    constructor(address initialOwner) {
        if (initialOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(initialOwner);
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        if (owner() != _msgSender()) {
            revert OwnableUnauthorizedAccount(_msgSender());
        }
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        if (newOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}
Math.sol 415 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/math/Math.sol)

pragma solidity ^0.8.20;

/**
 * @dev Standard math utilities missing in the Solidity language.
 */
library Math {
    /**
     * @dev Muldiv operation overflow.
     */
    error MathOverflowedMulDiv();

    enum Rounding {
        Floor, // Toward negative infinity
        Ceil, // Toward positive infinity
        Trunc, // Toward zero
        Expand // Away from zero
    }

    /**
     * @dev Returns the addition of two unsigned integers, with an overflow flag.
     */
    function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            uint256 c = a + b;
            if (c < a) return (false, 0);
            return (true, c);
        }
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, with an overflow flag.
     */
    function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b > a) return (false, 0);
            return (true, a - b);
        }
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
     */
    function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
            // benefit is lost if 'b' is also tested.
            // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
            if (a == 0) return (true, 0);
            uint256 c = a * b;
            if (c / a != b) return (false, 0);
            return (true, c);
        }
    }

    /**
     * @dev Returns the division of two unsigned integers, with a division by zero flag.
     */
    function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a / b);
        }
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
     */
    function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a % b);
        }
    }

    /**
     * @dev Returns the largest of two numbers.
     */
    function max(uint256 a, uint256 b) internal pure returns (uint256) {
        return a > b ? a : b;
    }

    /**
     * @dev Returns the smallest of two numbers.
     */
    function min(uint256 a, uint256 b) internal pure returns (uint256) {
        return a < b ? a : b;
    }

    /**
     * @dev Returns the average of two numbers. The result is rounded towards
     * zero.
     */
    function average(uint256 a, uint256 b) internal pure returns (uint256) {
        // (a + b) / 2 can overflow.
        return (a & b) + (a ^ b) / 2;
    }

    /**
     * @dev Returns the ceiling of the division of two numbers.
     *
     * This differs from standard division with `/` in that it rounds towards infinity instead
     * of rounding towards zero.
     */
    function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
        if (b == 0) {
            // Guarantee the same behavior as in a regular Solidity division.
            return a / b;
        }

        // (a + b - 1) / b can overflow on addition, so we distribute.
        return a == 0 ? 0 : (a - 1) / b + 1;
    }

    /**
     * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or
     * denominator == 0.
     * @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv) with further edits by
     * Uniswap Labs also under MIT license.
     */
    function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
        unchecked {
            // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
            // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
            // variables such that product = prod1 * 2^256 + prod0.
            uint256 prod0 = x * y; // Least significant 256 bits of the product
            uint256 prod1; // Most significant 256 bits of the product
            assembly {
                let mm := mulmod(x, y, not(0))
                prod1 := sub(sub(mm, prod0), lt(mm, prod0))
            }

            // Handle non-overflow cases, 256 by 256 division.
            if (prod1 == 0) {
                // Solidity will revert if denominator == 0, unlike the div opcode on its own.
                // The surrounding unchecked block does not change this fact.
                // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
                return prod0 / denominator;
            }

            // Make sure the result is less than 2^256. Also prevents denominator == 0.
            if (denominator <= prod1) {
                revert MathOverflowedMulDiv();
            }

            ///////////////////////////////////////////////
            // 512 by 256 division.
            ///////////////////////////////////////////////

            // Make division exact by subtracting the remainder from [prod1 prod0].
            uint256 remainder;
            assembly {
                // Compute remainder using mulmod.
                remainder := mulmod(x, y, denominator)

                // Subtract 256 bit number from 512 bit number.
                prod1 := sub(prod1, gt(remainder, prod0))
                prod0 := sub(prod0, remainder)
            }

            // Factor powers of two out of denominator and compute largest power of two divisor of denominator.
            // Always >= 1. See https://cs.stackexchange.com/q/138556/92363.

            uint256 twos = denominator & (0 - denominator);
            assembly {
                // Divide denominator by twos.
                denominator := div(denominator, twos)

                // Divide [prod1 prod0] by twos.
                prod0 := div(prod0, twos)

                // Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
                twos := add(div(sub(0, twos), twos), 1)
            }

            // Shift in bits from prod1 into prod0.
            prod0 |= prod1 * twos;

            // Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
            // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
            // four bits. That is, denominator * inv = 1 mod 2^4.
            uint256 inverse = (3 * denominator) ^ 2;

            // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also
            // works in modular arithmetic, doubling the correct bits in each step.
            inverse *= 2 - denominator * inverse; // inverse mod 2^8
            inverse *= 2 - denominator * inverse; // inverse mod 2^16
            inverse *= 2 - denominator * inverse; // inverse mod 2^32
            inverse *= 2 - denominator * inverse; // inverse mod 2^64
            inverse *= 2 - denominator * inverse; // inverse mod 2^128
            inverse *= 2 - denominator * inverse; // inverse mod 2^256

            // Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
            // This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
            // less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
            // is no longer required.
            result = prod0 * inverse;
            return result;
        }
    }

    /**
     * @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
     */
    function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
        uint256 result = mulDiv(x, y, denominator);
        if (unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0) {
            result += 1;
        }
        return result;
    }

    /**
     * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded
     * towards zero.
     *
     * Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
     */
    function sqrt(uint256 a) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }

        // For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
        //
        // We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
        // `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
        //
        // This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
        // → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
        // → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
        //
        // Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
        uint256 result = 1 << (log2(a) >> 1);

        // At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
        // since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
        // every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
        // into the expected uint128 result.
        unchecked {
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            return min(result, a / result);
        }
    }

    /**
     * @notice Calculates sqrt(a), following the selected rounding direction.
     */
    function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = sqrt(a);
            return result + (unsignedRoundsUp(rounding) && result * result < a ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 2 of a positive value rounded towards zero.
     * Returns 0 if given 0.
     */
    function log2(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >> 128 > 0) {
                value >>= 128;
                result += 128;
            }
            if (value >> 64 > 0) {
                value >>= 64;
                result += 64;
            }
            if (value >> 32 > 0) {
                value >>= 32;
                result += 32;
            }
            if (value >> 16 > 0) {
                value >>= 16;
                result += 16;
            }
            if (value >> 8 > 0) {
                value >>= 8;
                result += 8;
            }
            if (value >> 4 > 0) {
                value >>= 4;
                result += 4;
            }
            if (value >> 2 > 0) {
                value >>= 2;
                result += 2;
            }
            if (value >> 1 > 0) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 2, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log2(value);
            return result + (unsignedRoundsUp(rounding) && 1 << result < value ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 10 of a positive value rounded towards zero.
     * Returns 0 if given 0.
     */
    function log10(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >= 10 ** 64) {
                value /= 10 ** 64;
                result += 64;
            }
            if (value >= 10 ** 32) {
                value /= 10 ** 32;
                result += 32;
            }
            if (value >= 10 ** 16) {
                value /= 10 ** 16;
                result += 16;
            }
            if (value >= 10 ** 8) {
                value /= 10 ** 8;
                result += 8;
            }
            if (value >= 10 ** 4) {
                value /= 10 ** 4;
                result += 4;
            }
            if (value >= 10 ** 2) {
                value /= 10 ** 2;
                result += 2;
            }
            if (value >= 10 ** 1) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 10, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log10(value);
            return result + (unsignedRoundsUp(rounding) && 10 ** result < value ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 256 of a positive value rounded towards zero.
     * Returns 0 if given 0.
     *
     * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
     */
    function log256(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >> 128 > 0) {
                value >>= 128;
                result += 16;
            }
            if (value >> 64 > 0) {
                value >>= 64;
                result += 8;
            }
            if (value >> 32 > 0) {
                value >>= 32;
                result += 4;
            }
            if (value >> 16 > 0) {
                value >>= 16;
                result += 2;
            }
            if (value >> 8 > 0) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 256, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log256(value);
            return result + (unsignedRoundsUp(rounding) && 1 << (result << 3) < value ? 1 : 0);
        }
    }

    /**
     * @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers.
     */
    function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {
        return uint8(rounding) % 2 == 1;
    }
}
StorageSlot.sol 135 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/StorageSlot.sol)
// This file was procedurally generated from scripts/generate/templates/StorageSlot.js.

pragma solidity ^0.8.20;

/**
 * @dev Library for reading and writing primitive types to specific storage slots.
 *
 * Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
 * This library helps with reading and writing to such slots without the need for inline assembly.
 *
 * The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
 *
 * Example usage to set ERC1967 implementation slot:
 * ```solidity
 * contract ERC1967 {
 *     bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
 *
 *     function _getImplementation() internal view returns (address) {
 *         return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
 *     }
 *
 *     function _setImplementation(address newImplementation) internal {
 *         require(newImplementation.code.length > 0);
 *         StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
 *     }
 * }
 * ```
 */
library StorageSlot {
    struct AddressSlot {
        address value;
    }

    struct BooleanSlot {
        bool value;
    }

    struct Bytes32Slot {
        bytes32 value;
    }

    struct Uint256Slot {
        uint256 value;
    }

    struct StringSlot {
        string value;
    }

    struct BytesSlot {
        bytes value;
    }

    /**
     * @dev Returns an `AddressSlot` with member `value` located at `slot`.
     */
    function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `BooleanSlot` with member `value` located at `slot`.
     */
    function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
     */
    function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `Uint256Slot` with member `value` located at `slot`.
     */
    function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `StringSlot` with member `value` located at `slot`.
     */
    function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `StringSlot` representation of the string storage pointer `store`.
     */
    function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := store.slot
        }
    }

    /**
     * @dev Returns an `BytesSlot` with member `value` located at `slot`.
     */
    function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
     */
    function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := store.slot
        }
    }
}
ShortStrings.sol 123 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/ShortStrings.sol)

pragma solidity ^0.8.20;

import {StorageSlot} from "./StorageSlot.sol";

// | string  | 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA   |
// | length  | 0x                                                              BB |
type ShortString is bytes32;

/**
 * @dev This library provides functions to convert short memory strings
 * into a `ShortString` type that can be used as an immutable variable.
 *
 * Strings of arbitrary length can be optimized using this library if
 * they are short enough (up to 31 bytes) by packing them with their
 * length (1 byte) in a single EVM word (32 bytes). Additionally, a
 * fallback mechanism can be used for every other case.
 *
 * Usage example:
 *
 * ```solidity
 * contract Named {
 *     using ShortStrings for *;
 *
 *     ShortString private immutable _name;
 *     string private _nameFallback;
 *
 *     constructor(string memory contractName) {
 *         _name = contractName.toShortStringWithFallback(_nameFallback);
 *     }
 *
 *     function name() external view returns (string memory) {
 *         return _name.toStringWithFallback(_nameFallback);
 *     }
 * }
 * ```
 */
library ShortStrings {
    // Used as an identifier for strings longer than 31 bytes.
    bytes32 private constant FALLBACK_SENTINEL = 0x00000000000000000000000000000000000000000000000000000000000000FF;

    error StringTooLong(string str);
    error InvalidShortString();

    /**
     * @dev Encode a string of at most 31 chars into a `ShortString`.
     *
     * This will trigger a `StringTooLong` error is the input string is too long.
     */
    function toShortString(string memory str) internal pure returns (ShortString) {
        bytes memory bstr = bytes(str);
        if (bstr.length > 31) {
            revert StringTooLong(str);
        }
        return ShortString.wrap(bytes32(uint256(bytes32(bstr)) | bstr.length));
    }

    /**
     * @dev Decode a `ShortString` back to a "normal" string.
     */
    function toString(ShortString sstr) internal pure returns (string memory) {
        uint256 len = byteLength(sstr);
        // using `new string(len)` would work locally but is not memory safe.
        string memory str = new string(32);
        /// @solidity memory-safe-assembly
        assembly {
            mstore(str, len)
            mstore(add(str, 0x20), sstr)
        }
        return str;
    }

    /**
     * @dev Return the length of a `ShortString`.
     */
    function byteLength(ShortString sstr) internal pure returns (uint256) {
        uint256 result = uint256(ShortString.unwrap(sstr)) & 0xFF;
        if (result > 31) {
            revert InvalidShortString();
        }
        return result;
    }

    /**
     * @dev Encode a string into a `ShortString`, or write it to storage if it is too long.
     */
    function toShortStringWithFallback(string memory value, string storage store) internal returns (ShortString) {
        if (bytes(value).length < 32) {
            return toShortString(value);
        } else {
            StorageSlot.getStringSlot(store).value = value;
            return ShortString.wrap(FALLBACK_SENTINEL);
        }
    }

    /**
     * @dev Decode a string that was encoded to `ShortString` or written to storage using {setWithFallback}.
     */
    function toStringWithFallback(ShortString value, string storage store) internal pure returns (string memory) {
        if (ShortString.unwrap(value) != FALLBACK_SENTINEL) {
            return toString(value);
        } else {
            return store;
        }
    }

    /**
     * @dev Return the length of a string that was encoded to `ShortString` or written to storage using
     * {setWithFallback}.
     *
     * WARNING: This will return the "byte length" of the string. This may not reflect the actual length in terms of
     * actual characters as the UTF-8 encoding of a single character can span over multiple bytes.
     */
    function byteLengthWithFallback(ShortString value, string storage store) internal view returns (uint256) {
        if (ShortString.unwrap(value) != FALLBACK_SENTINEL) {
            return byteLength(value);
        } else {
            return bytes(store).length;
        }
    }
}
Ownable2Step.sol 59 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable2Step.sol)

pragma solidity ^0.8.20;

import {Ownable} from "./Ownable.sol";

/**
 * @dev Contract module which provides access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * The initial owner is specified at deployment time in the constructor for `Ownable`. This
 * can later be changed with {transferOwnership} and {acceptOwnership}.
 *
 * This module is used through inheritance. It will make available all functions
 * from parent (Ownable).
 */
abstract contract Ownable2Step is Ownable {
    address private _pendingOwner;

    event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Returns the address of the pending owner.
     */
    function pendingOwner() public view virtual returns (address) {
        return _pendingOwner;
    }

    /**
     * @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual override onlyOwner {
        _pendingOwner = newOwner;
        emit OwnershipTransferStarted(owner(), newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual override {
        delete _pendingOwner;
        super._transferOwnership(newOwner);
    }

    /**
     * @dev The new owner accepts the ownership transfer.
     */
    function acceptOwnership() public virtual {
        address sender = _msgSender();
        if (pendingOwner() != sender) {
            revert OwnableUnauthorizedAccount(sender);
        }
        _transferOwnership(sender);
    }
}
IERC1271.sol 17 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC1271.sol)

pragma solidity ^0.8.20;

/**
 * @dev Interface of the ERC1271 standard signature validation method for
 * contracts as defined in https://eips.ethereum.org/EIPS/eip-1271[ERC-1271].
 */
interface IERC1271 {
    /**
     * @dev Should return whether the signature provided is valid for the provided data
     * @param hash      Hash of the data to be signed
     * @param signature Signature byte array associated with _data
     */
    function isValidSignature(bytes32 hash, bytes memory signature) external view returns (bytes4 magicValue);
}
IERC5267.sol 28 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC5267.sol)

pragma solidity ^0.8.20;

interface IERC5267 {
    /**
     * @dev MAY be emitted to signal that the domain could have changed.
     */
    event EIP712DomainChanged();

    /**
     * @dev returns the fields and values that describe the domain separator used by this contract for EIP-712
     * signature.
     */
    function eip712Domain()
        external
        view
        returns (
            bytes1 fields,
            string memory name,
            string memory version,
            uint256 chainId,
            address verifyingContract,
            bytes32 salt,
            uint256[] memory extensions
        );
}
SignedMath.sol 43 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/math/SignedMath.sol)

pragma solidity ^0.8.20;

/**
 * @dev Standard signed math utilities missing in the Solidity language.
 */
library SignedMath {
    /**
     * @dev Returns the largest of two signed numbers.
     */
    function max(int256 a, int256 b) internal pure returns (int256) {
        return a > b ? a : b;
    }

    /**
     * @dev Returns the smallest of two signed numbers.
     */
    function min(int256 a, int256 b) internal pure returns (int256) {
        return a < b ? a : b;
    }

    /**
     * @dev Returns the average of two signed numbers without overflow.
     * The result is rounded towards zero.
     */
    function average(int256 a, int256 b) internal pure returns (int256) {
        // Formula from the book "Hacker's Delight"
        int256 x = (a & b) + ((a ^ b) >> 1);
        return x + (int256(uint256(x) >> 255) & (a ^ b));
    }

    /**
     * @dev Returns the absolute unsigned value of a signed value.
     */
    function abs(int256 n) internal pure returns (uint256) {
        unchecked {
            // must be unchecked in order to support `n = type(int256).min`
            return uint256(n >= 0 ? n : -n);
        }
    }
}
ECDSA.sol 174 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/cryptography/ECDSA.sol)

pragma solidity ^0.8.20;

/**
 * @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
 *
 * These functions can be used to verify that a message was signed by the holder
 * of the private keys of a given address.
 */
library ECDSA {
    enum RecoverError {
        NoError,
        InvalidSignature,
        InvalidSignatureLength,
        InvalidSignatureS
    }

    /**
     * @dev The signature derives the `address(0)`.
     */
    error ECDSAInvalidSignature();

    /**
     * @dev The signature has an invalid length.
     */
    error ECDSAInvalidSignatureLength(uint256 length);

    /**
     * @dev The signature has an S value that is in the upper half order.
     */
    error ECDSAInvalidSignatureS(bytes32 s);

    /**
     * @dev Returns the address that signed a hashed message (`hash`) with `signature` or an error. This will not
     * return address(0) without also returning an error description. Errors are documented using an enum (error type)
     * and a bytes32 providing additional information about the error.
     *
     * If no error is returned, then the address can be used for verification purposes.
     *
     * The `ecrecover` EVM precompile allows for malleable (non-unique) signatures:
     * this function rejects them by requiring the `s` value to be in the lower
     * half order, and the `v` value to be either 27 or 28.
     *
     * IMPORTANT: `hash` _must_ be the result of a hash operation for the
     * verification to be secure: it is possible to craft signatures that
     * recover to arbitrary addresses for non-hashed data. A safe way to ensure
     * this is by receiving a hash of the original message (which may otherwise
     * be too long), and then calling {MessageHashUtils-toEthSignedMessageHash} on it.
     *
     * Documentation for signature generation:
     * - with https://web3js.readthedocs.io/en/v1.3.4/web3-eth-accounts.html#sign[Web3.js]
     * - with https://docs.ethers.io/v5/api/signer/#Signer-signMessage[ethers]
     */
    function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError, bytes32) {
        if (signature.length == 65) {
            bytes32 r;
            bytes32 s;
            uint8 v;
            // ecrecover takes the signature parameters, and the only way to get them
            // currently is to use assembly.
            /// @solidity memory-safe-assembly
            assembly {
                r := mload(add(signature, 0x20))
                s := mload(add(signature, 0x40))
                v := byte(0, mload(add(signature, 0x60)))
            }
            return tryRecover(hash, v, r, s);
        } else {
            return (address(0), RecoverError.InvalidSignatureLength, bytes32(signature.length));
        }
    }

    /**
     * @dev Returns the address that signed a hashed message (`hash`) with
     * `signature`. This address can then be used for verification purposes.
     *
     * The `ecrecover` EVM precompile allows for malleable (non-unique) signatures:
     * this function rejects them by requiring the `s` value to be in the lower
     * half order, and the `v` value to be either 27 or 28.
     *
     * IMPORTANT: `hash` _must_ be the result of a hash operation for the
     * verification to be secure: it is possible to craft signatures that
     * recover to arbitrary addresses for non-hashed data. A safe way to ensure
     * this is by receiving a hash of the original message (which may otherwise
     * be too long), and then calling {MessageHashUtils-toEthSignedMessageHash} on it.
     */
    function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
        (address recovered, RecoverError error, bytes32 errorArg) = tryRecover(hash, signature);
        _throwError(error, errorArg);
        return recovered;
    }

    /**
     * @dev Overload of {ECDSA-tryRecover} that receives the `r` and `vs` short-signature fields separately.
     *
     * See https://eips.ethereum.org/EIPS/eip-2098[EIP-2098 short signatures]
     */
    function tryRecover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address, RecoverError, bytes32) {
        unchecked {
            bytes32 s = vs & bytes32(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
            // We do not check for an overflow here since the shift operation results in 0 or 1.
            uint8 v = uint8((uint256(vs) >> 255) + 27);
            return tryRecover(hash, v, r, s);
        }
    }

    /**
     * @dev Overload of {ECDSA-recover} that receives the `r and `vs` short-signature fields separately.
     */
    function recover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address) {
        (address recovered, RecoverError error, bytes32 errorArg) = tryRecover(hash, r, vs);
        _throwError(error, errorArg);
        return recovered;
    }

    /**
     * @dev Overload of {ECDSA-tryRecover} that receives the `v`,
     * `r` and `s` signature fields separately.
     */
    function tryRecover(
        bytes32 hash,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) internal pure returns (address, RecoverError, bytes32) {
        // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
        // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
        // the valid range for s in (301): 0 < s < secp256k1n ÷ 2 + 1, and for v in (302): v ∈ {27, 28}. Most
        // signatures from current libraries generate a unique signature with an s-value in the lower half order.
        //
        // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
        // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
        // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
        // these malleable signatures as well.
        if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
            return (address(0), RecoverError.InvalidSignatureS, s);
        }

        // If the signature is valid (and not malleable), return the signer address
        address signer = ecrecover(hash, v, r, s);
        if (signer == address(0)) {
            return (address(0), RecoverError.InvalidSignature, bytes32(0));
        }

        return (signer, RecoverError.NoError, bytes32(0));
    }

    /**
     * @dev Overload of {ECDSA-recover} that receives the `v`,
     * `r` and `s` signature fields separately.
     */
    function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address) {
        (address recovered, RecoverError error, bytes32 errorArg) = tryRecover(hash, v, r, s);
        _throwError(error, errorArg);
        return recovered;
    }

    /**
     * @dev Optionally reverts with the corresponding custom error according to the `error` argument provided.
     */
    function _throwError(RecoverError error, bytes32 errorArg) private pure {
        if (error == RecoverError.NoError) {
            return; // no error: do nothing
        } else if (error == RecoverError.InvalidSignature) {
            revert ECDSAInvalidSignature();
        } else if (error == RecoverError.InvalidSignatureLength) {
            revert ECDSAInvalidSignatureLength(uint256(errorArg));
        } else if (error == RecoverError.InvalidSignatureS) {
            revert ECDSAInvalidSignatureS(errorArg);
        }
    }
}
EIP712.sol 160 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/cryptography/EIP712.sol)

pragma solidity ^0.8.20;

import {MessageHashUtils} from "./MessageHashUtils.sol";
import {ShortStrings, ShortString} from "../ShortStrings.sol";
import {IERC5267} from "../../interfaces/IERC5267.sol";

/**
 * @dev https://eips.ethereum.org/EIPS/eip-712[EIP 712] is a standard for hashing and signing of typed structured data.
 *
 * The encoding scheme specified in the EIP requires a domain separator and a hash of the typed structured data, whose
 * encoding is very generic and therefore its implementation in Solidity is not feasible, thus this contract
 * does not implement the encoding itself. Protocols need to implement the type-specific encoding they need in order to
 * produce the hash of their typed data using a combination of `abi.encode` and `keccak256`.
 *
 * This contract implements the EIP 712 domain separator ({_domainSeparatorV4}) that is used as part of the encoding
 * scheme, and the final step of the encoding to obtain the message digest that is then signed via ECDSA
 * ({_hashTypedDataV4}).
 *
 * The implementation of the domain separator was designed to be as efficient as possible while still properly updating
 * the chain id to protect against replay attacks on an eventual fork of the chain.
 *
 * NOTE: This contract implements the version of the encoding known as "v4", as implemented by the JSON RPC method
 * https://docs.metamask.io/guide/signing-data.html[`eth_signTypedDataV4` in MetaMask].
 *
 * NOTE: In the upgradeable version of this contract, the cached values will correspond to the address, and the domain
 * separator of the implementation contract. This will cause the {_domainSeparatorV4} function to always rebuild the
 * separator from the immutable values, which is cheaper than accessing a cached version in cold storage.
 *
 * @custom:oz-upgrades-unsafe-allow state-variable-immutable
 */
abstract contract EIP712 is IERC5267 {
    using ShortStrings for *;

    bytes32 private constant TYPE_HASH =
        keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");

    // Cache the domain separator as an immutable value, but also store the chain id that it corresponds to, in order to
    // invalidate the cached domain separator if the chain id changes.
    bytes32 private immutable _cachedDomainSeparator;
    uint256 private immutable _cachedChainId;
    address private immutable _cachedThis;

    bytes32 private immutable _hashedName;
    bytes32 private immutable _hashedVersion;

    ShortString private immutable _name;
    ShortString private immutable _version;
    string private _nameFallback;
    string private _versionFallback;

    /**
     * @dev Initializes the domain separator and parameter caches.
     *
     * The meaning of `name` and `version` is specified in
     * https://eips.ethereum.org/EIPS/eip-712#definition-of-domainseparator[EIP 712]:
     *
     * - `name`: the user readable name of the signing domain, i.e. the name of the DApp or the protocol.
     * - `version`: the current major version of the signing domain.
     *
     * NOTE: These parameters cannot be changed except through a xref:learn::upgrading-smart-contracts.adoc[smart
     * contract upgrade].
     */
    constructor(string memory name, string memory version) {
        _name = name.toShortStringWithFallback(_nameFallback);
        _version = version.toShortStringWithFallback(_versionFallback);
        _hashedName = keccak256(bytes(name));
        _hashedVersion = keccak256(bytes(version));

        _cachedChainId = block.chainid;
        _cachedDomainSeparator = _buildDomainSeparator();
        _cachedThis = address(this);
    }

    /**
     * @dev Returns the domain separator for the current chain.
     */
    function _domainSeparatorV4() internal view returns (bytes32) {
        if (address(this) == _cachedThis && block.chainid == _cachedChainId) {
            return _cachedDomainSeparator;
        } else {
            return _buildDomainSeparator();
        }
    }

    function _buildDomainSeparator() private view returns (bytes32) {
        return keccak256(abi.encode(TYPE_HASH, _hashedName, _hashedVersion, block.chainid, address(this)));
    }

    /**
     * @dev Given an already https://eips.ethereum.org/EIPS/eip-712#definition-of-hashstruct[hashed struct], this
     * function returns the hash of the fully encoded EIP712 message for this domain.
     *
     * This hash can be used together with {ECDSA-recover} to obtain the signer of a message. For example:
     *
     * ```solidity
     * bytes32 digest = _hashTypedDataV4(keccak256(abi.encode(
     *     keccak256("Mail(address to,string contents)"),
     *     mailTo,
     *     keccak256(bytes(mailContents))
     * )));
     * address signer = ECDSA.recover(digest, signature);
     * ```
     */
    function _hashTypedDataV4(bytes32 structHash) internal view virtual returns (bytes32) {
        return MessageHashUtils.toTypedDataHash(_domainSeparatorV4(), structHash);
    }

    /**
     * @dev See {IERC-5267}.
     */
    function eip712Domain()
        public
        view
        virtual
        returns (
            bytes1 fields,
            string memory name,
            string memory version,
            uint256 chainId,
            address verifyingContract,
            bytes32 salt,
            uint256[] memory extensions
        )
    {
        return (
            hex"0f", // 01111
            _EIP712Name(),
            _EIP712Version(),
            block.chainid,
            address(this),
            bytes32(0),
            new uint256[](0)
        );
    }

    /**
     * @dev The name parameter for the EIP712 domain.
     *
     * NOTE: By default this function reads _name which is an immutable value.
     * It only reads from storage if necessary (in case the value is too large to fit in a ShortString).
     */
    // solhint-disable-next-line func-name-mixedcase
    function _EIP712Name() internal view returns (string memory) {
        return _name.toStringWithFallback(_nameFallback);
    }

    /**
     * @dev The version parameter for the EIP712 domain.
     *
     * NOTE: By default this function reads _version which is an immutable value.
     * It only reads from storage if necessary (in case the value is too large to fit in a ShortString).
     */
    // solhint-disable-next-line func-name-mixedcase
    function _EIP712Version() internal view returns (string memory) {
        return _version.toStringWithFallback(_versionFallback);
    }
}
MessageHashUtils.sol 86 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/cryptography/MessageHashUtils.sol)

pragma solidity ^0.8.20;

import {Strings} from "../Strings.sol";

/**
 * @dev Signature message hash utilities for producing digests to be consumed by {ECDSA} recovery or signing.
 *
 * The library provides methods for generating a hash of a message that conforms to the
 * https://eips.ethereum.org/EIPS/eip-191[EIP 191] and https://eips.ethereum.org/EIPS/eip-712[EIP 712]
 * specifications.
 */
library MessageHashUtils {
    /**
     * @dev Returns the keccak256 digest of an EIP-191 signed data with version
     * `0x45` (`personal_sign` messages).
     *
     * The digest is calculated by prefixing a bytes32 `messageHash` with
     * `"\x19Ethereum Signed Message:\n32"` and hashing the result. It corresponds with the
     * hash signed when using the https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`] JSON-RPC method.
     *
     * NOTE: The `messageHash` parameter is intended to be the result of hashing a raw message with
     * keccak256, although any bytes32 value can be safely used because the final digest will
     * be re-hashed.
     *
     * See {ECDSA-recover}.
     */
    function toEthSignedMessageHash(bytes32 messageHash) internal pure returns (bytes32 digest) {
        /// @solidity memory-safe-assembly
        assembly {
            mstore(0x00, "\x19Ethereum Signed Message:\n32") // 32 is the bytes-length of messageHash
            mstore(0x1c, messageHash) // 0x1c (28) is the length of the prefix
            digest := keccak256(0x00, 0x3c) // 0x3c is the length of the prefix (0x1c) + messageHash (0x20)
        }
    }

    /**
     * @dev Returns the keccak256 digest of an EIP-191 signed data with version
     * `0x45` (`personal_sign` messages).
     *
     * The digest is calculated by prefixing an arbitrary `message` with
     * `"\x19Ethereum Signed Message:\n" + len(message)` and hashing the result. It corresponds with the
     * hash signed when using the https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`] JSON-RPC method.
     *
     * See {ECDSA-recover}.
     */
    function toEthSignedMessageHash(bytes memory message) internal pure returns (bytes32) {
        return
            keccak256(bytes.concat("\x19Ethereum Signed Message:\n", bytes(Strings.toString(message.length)), message));
    }

    /**
     * @dev Returns the keccak256 digest of an EIP-191 signed data with version
     * `0x00` (data with intended validator).
     *
     * The digest is calculated by prefixing an arbitrary `data` with `"\x19\x00"` and the intended
     * `validator` address. Then hashing the result.
     *
     * See {ECDSA-recover}.
     */
    function toDataWithIntendedValidatorHash(address validator, bytes memory data) internal pure returns (bytes32) {
        return keccak256(abi.encodePacked(hex"19_00", validator, data));
    }

    /**
     * @dev Returns the keccak256 digest of an EIP-712 typed data (EIP-191 version `0x01`).
     *
     * The digest is calculated from a `domainSeparator` and a `structHash`, by prefixing them with
     * `\x19\x01` and hashing the result. It corresponds to the hash signed by the
     * https://eips.ethereum.org/EIPS/eip-712[`eth_signTypedData`] JSON-RPC method as part of EIP-712.
     *
     * See {ECDSA-recover}.
     */
    function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32 digest) {
        /// @solidity memory-safe-assembly
        assembly {
            let ptr := mload(0x40)
            mstore(ptr, hex"19_01")
            mstore(add(ptr, 0x02), domainSeparator)
            mstore(add(ptr, 0x22), structHash)
            digest := keccak256(ptr, 0x42)
        }
    }
}
SignatureChecker.sol 48 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/cryptography/SignatureChecker.sol)

pragma solidity ^0.8.20;

import {ECDSA} from "./ECDSA.sol";
import {IERC1271} from "../../interfaces/IERC1271.sol";

/**
 * @dev Signature verification helper that can be used instead of `ECDSA.recover` to seamlessly support both ECDSA
 * signatures from externally owned accounts (EOAs) as well as ERC1271 signatures from smart contract wallets like
 * Argent and Safe Wallet (previously Gnosis Safe).
 */
library SignatureChecker {
    /**
     * @dev Checks if a signature is valid for a given signer and data hash. If the signer is a smart contract, the
     * signature is validated against that smart contract using ERC1271, otherwise it's validated using `ECDSA.recover`.
     *
     * NOTE: Unlike ECDSA signatures, contract signatures are revocable, and the outcome of this function can thus
     * change through time. It could return true at block N and false at block N+1 (or the opposite).
     */
    function isValidSignatureNow(address signer, bytes32 hash, bytes memory signature) internal view returns (bool) {
        (address recovered, ECDSA.RecoverError error, ) = ECDSA.tryRecover(hash, signature);
        return
            (error == ECDSA.RecoverError.NoError && recovered == signer) ||
            isValidERC1271SignatureNow(signer, hash, signature);
    }

    /**
     * @dev Checks if a signature is valid for a given signer and data hash. The signature is validated
     * against the signer smart contract using ERC1271.
     *
     * NOTE: Unlike ECDSA signatures, contract signatures are revocable, and the outcome of this function can thus
     * change through time. It could return true at block N and false at block N+1 (or the opposite).
     */
    function isValidERC1271SignatureNow(
        address signer,
        bytes32 hash,
        bytes memory signature
    ) internal view returns (bool) {
        (bool success, bytes memory result) = signer.staticcall(
            abi.encodeCall(IERC1271.isValidSignature, (hash, signature))
        );
        return (success &&
            result.length >= 32 &&
            abi.decode(result, (bytes32)) == bytes32(IERC1271.isValidSignature.selector));
    }
}

Read Contract

eip712Domain 0x84b0196e → bytes1, string, string, uint256, address, bytes32, uint256[]
getBeneficiary 0x565a2e2c → address
getMultiSendOperators 0xe841436f → address[]
getSignerAddress 0x1a296e02 → address
getTransferGasLimit 0x88543f0e → uint24
getTreasuries 0x0d9b5011 → address[]
getWithdrawOperators 0x12e9a03f → address[]
owner 0x8da5cb5b → address
pendingOwner 0xe30c3978 → address

Write Contract 15 functions

These functions modify contract state and require a wallet transaction to execute.

acceptOwnership 0x79ba5097
No parameters
addMultiSendOperator 0xf84f2bb1
address _operator
addTreasury 0xc8cb3834
address _treasury
addWithdrawOperator 0x60ac85d6
address _operator
multiSend 0x0d49b711
bytes32 _requestIdHash
address[] _tokens
address[] _beneficiaries
uint256[] _amounts
uint256 _deadline
uint8 _v
bytes32 _r
bytes32 _s
multiSend 0x3d55e9dc
bytes32 _requestIdHash
address[] _tokens
address[] _beneficiaries
address[] _treasuries
uint256[] _amounts
uint256 _deadline
uint8 _v
bytes32 _r
bytes32 _s
removeMultiSendOperator 0xae7c82a4
address _operator
removeTreasury 0xb35ee931
address _treasury
removeWithdrawOperator 0x92b34620
address _operator
renounceOwnership 0x715018a6
No parameters
setBeneficiary 0x1c31f710
address _beneficiary
setSignerAddress 0x046dc166
address _signerAddress
setTransferGasLimit 0x01708632
uint24 _transferGasLimit
transferOwnership 0xf2fde38b
address newOwner
withdraw 0xf7ece0cf
address[] tokens
address[] sources
uint256[] amounts

Recent Transactions

No transactions found for this address