From 976884bf550c0ee355974bb8e85a2feb6aa2aaa2 Mon Sep 17 00:00:00 2001 From: Sergey Potekhin Date: Thu, 2 May 2024 16:52:19 +0400 Subject: [PATCH] Pim 1017 (#188) * Light account factory v2.0.0 setup * 1.1.0 support * Implemented light client signMessage * Light account v1.1.0 support * Preffify light account * Cleanup * Cleanup tests * Fix typo & call data encoding * Remove any mention of the v2.0.0 light account * Add changeset * Fix changeset typo --- .changeset/tidy-rats-bow.md | 5 + .../mock-aa-infra/alto/src/constants.ts | 5 + .../mock-aa-infra/alto/src/index.ts | 40 +- .../src/ep-0.6/coreSmartClientActions.test.ts | 19 +- .../src/ep-0.7/coreSmartClinetActions.test.ts | 2 +- packages/permissionless-test/src/utils.ts | 28 ++ packages/permissionless/accounts/index.ts | 16 + .../light/privateKeyToLightSmartAccount.ts | 37 ++ .../light/signerToLightSmartAccount.ts | 407 ++++++++++++++++++ 9 files changed, 555 insertions(+), 4 deletions(-) create mode 100644 .changeset/tidy-rats-bow.md create mode 100644 packages/permissionless/accounts/light/privateKeyToLightSmartAccount.ts create mode 100644 packages/permissionless/accounts/light/signerToLightSmartAccount.ts diff --git a/.changeset/tidy-rats-bow.md b/.changeset/tidy-rats-bow.md new file mode 100644 index 00000000..a8250e40 --- /dev/null +++ b/.changeset/tidy-rats-bow.md @@ -0,0 +1,5 @@ +--- +"permissionless": minor +--- + +Added Alchemy's LightAccount support v1.1.0 diff --git a/packages/permissionless-test/mock-aa-infra/alto/src/constants.ts b/packages/permissionless-test/mock-aa-infra/alto/src/constants.ts index b79afa5b..1034edc6 100644 --- a/packages/permissionless-test/mock-aa-infra/alto/src/constants.ts +++ b/packages/permissionless-test/mock-aa-infra/alto/src/constants.ts @@ -111,3 +111,8 @@ export const KERNEL_V07_FACTORY_CREATECALL: Hex = // Will deploy V0.7 KERNEL_FACTORY_ADDRESS to 0xd703aaE79538628d27099B8c4f621bE4CCd142d5 export const KERNEL_V07_META_FACTORY_CREATECALL: Hex = "0x0000000000000000000000000000000000000000000000000000000000000000608060405234801561001057600080fd5b5060405161080838038061080883398101604081905261002f9161007a565b6100388161003e565b506100aa565b6001600160a01b0316638b78c6d8198190558060007f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08180a350565b60006020828403121561008c57600080fd5b81516001600160a01b03811681146100a357600080fd5b9392505050565b61074f806100b96000396000f3fe6080604052600436106100c25760003560e01c8063b36f97051161007f578063d8b964e611610059578063d8b964e614610172578063f04e283e146101b2578063f2fde38b146101c5578063fee81cf4146101d857600080fd5b8063b36f970514610139578063c5265d5d1461014c578063c7e55f3e1461015f57600080fd5b806325692962146100c75780634a1ce599146100d157806354d1f13d146100e45780636e7dbabb146100ec578063715018a6146100ff5780638da5cb5b14610107575b600080fd5b6100cf610219565b005b6100cf6100df3660046105a7565b610269565b6100cf6102c7565b6100cf6100fa3660046105cb565b610303565b6100cf610336565b34801561011357600080fd5b50638b78c6d819545b6040516001600160a01b0390911681526020015b60405180910390f35b6100cf610147366004610609565b61034a565b61011c61015a366004610637565b6103b1565b6100cf61016d3660046106c2565b610466565b34801561017e57600080fd5b506101a261018d3660046105a7565b60006020819052908152604090205460ff1681565b6040519015158152602001610130565b6100cf6101c03660046105a7565b6104d2565b6100cf6101d33660046105a7565b610512565b3480156101e457600080fd5b5061020b6101f33660046105a7565b63389a75e1600c908152600091909152602090205490565b604051908152602001610130565b60006202a30067ffffffffffffffff164201905063389a75e1600c5233600052806020600c2055337fdbf36a107da19e49527a7176a1babf963b4b0ff8cde35ee35d6cd8f1f9ac7e1d600080a250565b610271610539565b806001600160a01b031663bb9fe6bf6040518163ffffffff1660e01b8152600401600060405180830381600087803b1580156102ac57600080fd5b505af11580156102c0573d6000803e3d6000fd5b5050505050565b63389a75e1600c523360005260006020600c2055337ffa7b8eab7da67f412cc9575ed43464468f9bfbae89d1675917346ca6d8fe3c92600080a2565b61030b610539565b6001600160a01b03919091166000908152602081905260409020805460ff1916911515919091179055565b61033e610539565b6103486000610554565b565b610352610539565b60405163611d2e7560e11b81526001600160a01b03828116600483015283169063c23a5cea90602401600060405180830381600087803b15801561039557600080fd5b505af11580156103a9573d6000803e3d6000fd5b505050505050565b6001600160a01b03841660009081526020819052604081205460ff166103ea57604051633220d5f360e21b815260040160405180910390fd5b604051633a9b44eb60e21b81526001600160a01b0386169063ea6d13ac9061041a908790879087906004016106f9565b6020604051808303816000875af1158015610439573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061045d9190610732565b95945050505050565b61046e610539565b604051621cb65b60e51b815263ffffffff821660048201526001600160a01b03831690630396cb609034906024016000604051808303818588803b1580156104b557600080fd5b505af11580156104c9573d6000803e3d6000fd5b50505050505050565b6104da610539565b63389a75e1600c52806000526020600c20805442111561050257636f5e88186000526004601cfd5b6000905561050f81610554565b50565b61051a610539565b8060601b61053057637448fbae6000526004601cfd5b61050f81610554565b638b78c6d819543314610348576382b429006000526004601cfd5b638b78c6d81980546001600160a01b039092169182907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0600080a355565b6001600160a01b038116811461050f57600080fd5b6000602082840312156105b957600080fd5b81356105c481610592565b9392505050565b600080604083850312156105de57600080fd5b82356105e981610592565b9150602083013580151581146105fe57600080fd5b809150509250929050565b6000806040838503121561061c57600080fd5b823561062781610592565b915060208301356105fe81610592565b6000806000806060858703121561064d57600080fd5b843561065881610592565b9350602085013567ffffffffffffffff8082111561067557600080fd5b818701915087601f83011261068957600080fd5b81358181111561069857600080fd5b8860208285010111156106aa57600080fd5b95986020929092019750949560400135945092505050565b600080604083850312156106d557600080fd5b82356106e081610592565b9150602083013563ffffffff811681146105fe57600080fd5b604081528260408201528284606083013760006060848301015260006060601f19601f8601168301019050826020830152949350505050565b60006020828403121561074457600080fd5b81516105c481610592560000000000000000000000009775137314fe595c943712b0b336327dfa80ae8a" + +/* ========= ALCHEMY LIGHT ACCOUNT RELATED ========= */ +// Will deploy the LightAccountFactory V1.1.0 to 0x00004EC70002a32400f8ae005A26081065620D20 +export const LIGHT_ACCOUNT_FACTORY_V110_CREATECALL: Hex = + "0x4e59b44847b379578588920ca78fbf26c0b4956c5528f3e2f146000008fabf7760a0346100cb576001600160401b0390601f6130cb38819003918201601f1916830191848311848410176100b5578084926020946040528339810103126100cb57516001600160a01b038116908190036100cb576040519161270590818401908111848210176100b55760209284926109c6843981520301906000f080156100a9576080526040516108f590816100d1823960805181818160e00152818161030601526103f70152f35b6040513d6000823e3d90fd5b634e487b7160e01b600052604160045260246000fd5b600080fdfe608080604052600436101561001357600080fd5b600090813560e01c90816311464fbe14610096575080635fbfb9cf1461007c57638cb84e181461004257600080fd5b3461007957602061005b61005536610108565b90610363565b73ffffffffffffffffffffffffffffffffffffffff60405191168152f35b80fd5b503461007957602061005b61009036610108565b90610274565b90503461010457817ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101045760209073ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000168152f35b5080fd5b7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc60409101126101595760043573ffffffffffffffffffffffffffffffffffffffff81168103610159579060243590565b600080fd5b6060810190811067ffffffffffffffff82111761017a57604052565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b90601f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0910116810190811067ffffffffffffffff82111761017a57604052565b60005b8381106101fd5750506000910152565b81810151838201526020016101ed565b90601f60609373ffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0931684526040602085015261026d81518092816040880152602088880191016101ea565b0116010190565b9061027f8183610363565b803b610347575073ffffffffffffffffffffffffffffffffffffffff9182604051917fc4d66de8000000000000000000000000000000000000000000000000000000006020840152166024820152602481526102da8161015e565b6040519061042c8083019183831067ffffffffffffffff84111761017a57839261032c926104948539867f0000000000000000000000000000000000000000000000000000000000000000169061020d565b03906000f5801561033b571690565b6040513d6000823e3d90fd5b73ffffffffffffffffffffffffffffffffffffffff1692915050565b600b9060559261042c60209061046f61047b83604096875190610388838701836101a9565b85825282820195610494873961041d61044973ffffffffffffffffffffffffffffffffffffffff92838c51917fc4d66de80000000000000000000000000000000000000000000000000000000088840152166024820152602481526103ec8161015e565b8b51928391878301957f0000000000000000000000000000000000000000000000000000000000000000168661020d565b037fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe081018352826101a9565b8951958693610460868601998a92519283916101ea565b840191518093868401906101ea565b010380845201826101a9565b5190208351938401528201523081520160ff8153209056fe60406080815261042c908138038061001681610218565b93843982019181818403126102135780516001600160a01b038116808203610213576020838101516001600160401b0394919391858211610213570186601f820112156102135780519061007161006c83610253565b610218565b918083528583019886828401011161021357888661008f930161026e565b813b156101b9577f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc80546001600160a01b031916841790556000927fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b8480a28051158015906101b2575b61010b575b855160e790816103458239f35b855194606086019081118682101761019e578697849283926101889952602788527f416464726573733a206c6f772d6c6576656c2064656c65676174652063616c6c87890152660819985a5b195960ca1b8a8901525190845af4913d15610194573d9061017a61006c83610253565b91825281943d92013e610291565b508038808080806100fe565b5060609250610291565b634e487b7160e01b84526041600452602484fd5b50826100f9565b855162461bcd60e51b815260048101859052602d60248201527f455243313936373a206e657720696d706c656d656e746174696f6e206973206e60448201526c1bdd08184818dbdb9d1c9858dd609a1b6064820152608490fd5b600080fd5b6040519190601f01601f191682016001600160401b0381118382101761023d57604052565b634e487b7160e01b600052604160045260246000fd5b6001600160401b03811161023d57601f01601f191660200190565b60005b8381106102815750506000910152565b8181015183820152602001610271565b919290156102f357508151156102a5575090565b3b156102ae5790565b60405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606490fd5b8251909150156103065750805190602001fd5b6044604051809262461bcd60e51b825260206004830152610336815180928160248601526020868601910161026e565b601f01601f19168101030190fdfe60806040523615605f5773ffffffffffffffffffffffffffffffffffffffff7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc54166000808092368280378136915af43d82803e15605b573d90f35b3d90fd5b73ffffffffffffffffffffffffffffffffffffffff7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc54166000808092368280378136915af43d82803e15605b573d90f3fea26469706673582212205da2750cd2b0cadfd354d8a1ca4752ed7f22214c8069d852f7dc6b8e9e5ee66964736f6c63430008150033a26469706673582212205367f15fddc0d5cbb3b407c1f8fa018b2549200abc34a5978c9abd75b26a675a64736f6c6343000815003360e03462000160576001600160401b0390601f6200270538819003918201601f1916830191848311848410176200016557808492602094604052833981010312620001605751906001600160a01b03821682036200016057306080527f33e4b41198cc5b8053630ed667ea7c0c4c873f7fc8d9a478b5d7259cec0a4a00918260a05260c05281549060ff8260401c166200014e57808083160362000108575b60405161258990816200017c82396080518181816107b201528181610dbd0152610f99015260a0518161141d015260c0518181816109d701528181610bf501528181610cd4015281816111b001528181611387015281816115ff015281816122af01526124b50152f35b6001600160401b031990911681179091556040519081527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d290602090a13880806200009e565b60405163f92ee8a960e01b8152600490fd5b600080fd5b634e487b7160e01b600052604160045260246000fdfe6080604052600436101561001b575b361561001957600080fd5b005b60003560e01c806223de291461019a57806301ffc9a7146101955780630a1028c414610190578063150b7a021461018b5780631626ba7e1461018657806318dfb3c7146101815780633659cfe61461017c5780633a871cdd1461017757806347e1da2a146101725780634a58db191461016d5780634d44560d146101685780634f1ef2861461016357806352d1902d1461015e5780638da5cb5b14610159578063a786cac914610154578063b0d691fe1461014f578063b61d27f61461014a578063bc197c8114610145578063c399ec8814610140578063c4d66de81461013b578063d087d28814610136578063f23a6e6114610131578063f2fde38b1461012c5763f698da250361000e5761184d565b6116f2565b611661565b611580565b6113e0565b61130f565b611248565b6111d4565b611165565b61113d565b61106e565b610f53565b610d4d565b610c76565b610bb3565b610ac3565b61096d565b61075e565b610672565b6105bb565b61052a565b610504565b61027b565b6101f0565b73ffffffffffffffffffffffffffffffffffffffff8116036101bd57565b600080fd5b9181601f840112156101bd5782359167ffffffffffffffff83116101bd57602083818601950101116101bd57565b346101bd5760c07ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd5761022a60043561019f565b61023560243561019f565b61024060443561019f565b67ffffffffffffffff6084358181116101bd576102619036906004016101c2565b505060a4359081116101bd576100199036906004016101c2565b346101bd5760207ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd576004357fffffffff0000000000000000000000000000000000000000000000000000000081168091036101bd57807f150b7a020000000000000000000000000000000000000000000000000000000060209214908115610341575b8115610317575b506040519015158152f35b7f01ffc9a7000000000000000000000000000000000000000000000000000000009150143861030c565b7f4e2312e00000000000000000000000000000000000000000000000000000000081149150610305565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b67ffffffffffffffff81116103ae57604052565b61036b565b6020810190811067ffffffffffffffff8211176103ae57604052565b6060810190811067ffffffffffffffff8211176103ae57604052565b90601f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0910116810190811067ffffffffffffffff8211176103ae57604052565b67ffffffffffffffff81116103ae57601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe01660200190565b9291926104728261042c565b9161048060405193846103eb565b8294818452818301116101bd578281602093846000960137010152565b9080601f830112156101bd578160206104b893359101610466565b90565b60207ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc8201126101bd576004359067ffffffffffffffff82116101bd576104b89160040161049d565b346101bd57602061051c610517366104bb565b611ec8565b818151910120604051908152f35b346101bd5760807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd5761056460043561019f565b61056f60243561019f565b60643567ffffffffffffffff81116101bd5761058f9036906004016101c2565b505060206040517f150b7a02000000000000000000000000000000000000000000000000000000008152f35b346101bd5760407ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd5760243567ffffffffffffffff81116101bd5761061761060f602092369060040161049d565b600435611f6d565b7fffffffff0000000000000000000000000000000000000000000000000000000060405191168152f35b9181601f840112156101bd5782359167ffffffffffffffff83116101bd576020808501948460051b0101116101bd57565b346101bd5760407ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd5767ffffffffffffffff6004358181116101bd576106c2903690600401610641565b916024359081116101bd576106db903690600401610641565b91906106e561249d565b8284036107345760005b8481106106f857005b8061072e6107096001938887611da0565b356107138161019f565b610728610721848988611e06565b3691610466565b90612514565b016106ef565b60046040517fa24a13a6000000000000000000000000000000000000000000000000000000008152fd5b346101bd5760207ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd576004356107998161019f565b73ffffffffffffffffffffffffffffffffffffffff90817f000000000000000000000000000000000000000000000000000000000000000016916107df833014156118d9565b61080e7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc938285541614611964565b61081661241c565b60405190610823826103b3565b600082527f4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd91435460ff161561085d5750506100199150611a7a565b6020600491604094939451928380927f52d1902d00000000000000000000000000000000000000000000000000000000825286165afa6000918161093d575b5061092a576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602e60248201527f45524331393637557067726164653a206e657720696d706c656d656e7461746960448201527f6f6e206973206e6f7420555550530000000000000000000000000000000000006064820152608490fd5b0390fd5b6100199361093891146119ef565b611b66565b61095f91925060203d8111610966575b61095781836103eb565b81019061188e565b903861089c565b503d61094d565b346101bd577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc6060813601126101bd576004359067ffffffffffffffff82116101bd576101609082360301126101bd5760443573ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000163303610a6557610a10610a28926024359060040161234d565b9080610a2c575b506040519081529081906020820190565b0390f35b600080808093337ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff150610a5e6118a9565b5038610a17565b60646040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601c60248201527f6163636f756e743a206e6f742066726f6d20456e747279506f696e74000000006044820152fd5b346101bd5760607ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd5767ffffffffffffffff6004358181116101bd57610b13903690600401610641565b6024358381116101bd57610b2b903690600401610641565b936044359081116101bd57610b44903690600401610641565b92610b4d61249d565b838114801590610ba9575b6107345760005b818110610b6857005b80610ba3610b79600193858a611da0565b35610b838161019f565b610b8e838b89611da0565b35610b9d610721858b8a611e06565b9161253c565b01610b5f565b5085811415610b58565b6000807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc360112610c735773ffffffffffffffffffffffffffffffffffffffff7f00000000000000000000000000000000000000000000000000000000000000001681813b15610c7357602491604051928380927fb760faf900000000000000000000000000000000000000000000000000000000825230600483015234905af18015610c6e57610c62575080f35b610c6b9061039a565b80f35b61189d565b80fd5b346101bd57600060407ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc360112610c7357600435610cb38161019f565b610cbb61241c565b8173ffffffffffffffffffffffffffffffffffffffff807f00000000000000000000000000000000000000000000000000000000000000001692833b15610d49576044908360405195869485937f205c287800000000000000000000000000000000000000000000000000000000855216600484015260243560248401525af18015610c6e57610c62575080f35b8280fd5b60407ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd57600435610d838161019f565b60243567ffffffffffffffff81116101bd57610da390369060040161049d565b9073ffffffffffffffffffffffffffffffffffffffff91827f00000000000000000000000000000000000000000000000000000000000000001692610dea843014156118d9565b610e197f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc948286541614611964565b610e2161241c565b7f4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd91435460ff1615610e575750506100199150611a7a565b6020600491604094939451928380927f52d1902d00000000000000000000000000000000000000000000000000000000825286165afa60009181610f33575b50610f20576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602e60248201527f45524331393637557067726164653a206e657720696d706c656d656e7461746960448201527f6f6e206973206e6f7420555550530000000000000000000000000000000000006064820152608490fd5b61001993610f2e91146119ef565b611c45565b610f4c91925060203d81116109665761095781836103eb565b9038610e96565b346101bd5760007ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd5773ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000163003610fea576040517f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc8152602090f35b60846040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152603860248201527f555550535570677261646561626c653a206d757374206e6f742062652063616c60448201527f6c6564207468726f7567682064656c656761746563616c6c00000000000000006064820152fd5b346101bd5760007ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd57602073ffffffffffffffffffffffffffffffffffffffff7f691ec1a18226d004c07c9f8e5c4a6ff15a7b38db267cf7e3c945aef8be5122005416604051908152f35b919082519283825260005b8481106111295750507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0601f8460006020809697860101520116010190565b6020818301810151848301820152016110ea565b346101bd57610a28611151610517366104bb565b6040519182916020835260208301906110df565b346101bd5760007ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd57602060405173ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000168152f35b346101bd5760607ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd5760043561120f8161019f565b6044359067ffffffffffffffff82116101bd5761123e6112366100199336906004016101c2565b61072161249d565b906024359061253c565b346101bd5760a07ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd5761128260043561019f565b61128d60243561019f565b67ffffffffffffffff6044358181116101bd576112ae903690600401610641565b50506064358181116101bd576112c8903690600401610641565b50506084359081116101bd576112e29036906004016101c2565b50506040517fbc197c81000000000000000000000000000000000000000000000000000000008152602090f35b346101bd5760007ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd576040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015260208160248173ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000165afa8015610c6e576020916000916113c3575b50604051908152f35b6113da9150823d81116109665761095781836103eb565b386113ba565b346101bd5760207ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd5760043561141b8161019f565b7f00000000000000000000000000000000000000000000000000000000000000009081549067ffffffffffffffff60ff8360401c1615921680159081611578575b600114908161156e575b159081611565575b5061153b5782547fffffffffffffffffffffffffffffffffffffffffffffffff00000000000000001660011783556114aa908261150557612227565b6114b057005b80547fffffffffffffffffffffffffffffffffffffffffffffff00ffffffffffffffff169055604051600181527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d290602090a1005b83547fffffffffffffffffffffffffffffffffffffffffffffff00ffffffffffffffff1668010000000000000000178455612227565b60046040517ff92ee8a9000000000000000000000000000000000000000000000000000000008152fd5b9050153861146e565b303b159150611466565b83915061145c565b346101bd5760007ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd576040517f35567e1a0000000000000000000000000000000000000000000000000000000081523060048201526000602482015260208160448173ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000165afa8015610c6e57610a289160009161164357506040519081529081906020820190565b61165b915060203d81116109665761095781836103eb565b38610a17565b346101bd5760a07ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd5761169b60043561019f565b6116a660243561019f565b60843567ffffffffffffffff81116101bd576116c69036906004016101c2565b505060206040517ff23a6e61000000000000000000000000000000000000000000000000000000008152f35b346101bd5760207ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd5760043561172d8161019f565b61173561241c565b73ffffffffffffffffffffffffffffffffffffffff908181169182158015611844575b611813577f691ec1a18226d004c07c9f8e5c4a6ff15a7b38db267cf7e3c945aef8be512200541690818314611813576117ec9073ffffffffffffffffffffffffffffffffffffffff7f691ec1a18226d004c07c9f8e5c4a6ff15a7b38db267cf7e3c945aef8be51220091167fffffffffffffffffffffffff0000000000000000000000000000000000000000825416179055565b7f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0600080a3005b602483604051907fb20f76e30000000000000000000000000000000000000000000000000000000082526004820152fd5b50308314611758565b346101bd5760007ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc3601126101bd576020611886611e21565b604051908152f35b908160209103126101bd575190565b6040513d6000823e3d90fd5b3d156118d4573d906118ba8261042c565b916118c860405193846103eb565b82523d6000602084013e565b606090565b156118e057565b60846040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602c60248201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060448201527f64656c656761746563616c6c00000000000000000000000000000000000000006064820152fd5b1561196b57565b60846040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602c60248201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060448201527f6163746976652070726f787900000000000000000000000000000000000000006064820152fd5b156119f657565b60846040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602960248201527f45524331393637557067726164653a20756e737570706f727465642070726f7860448201527f6961626c655555494400000000000000000000000000000000000000000000006064820152fd5b803b15611ae25773ffffffffffffffffffffffffffffffffffffffff7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc91167fffffffffffffffffffffffff0000000000000000000000000000000000000000825416179055565b60846040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602d60248201527f455243313936373a206e657720696d706c656d656e746174696f6e206973206e60448201527f6f74206120636f6e7472616374000000000000000000000000000000000000006064820152fd5b90611b7082611a7a565b73ffffffffffffffffffffffffffffffffffffffff82167fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b600080a2805115801590611c3d575b611bbf575050565b611c3a9160008060405193611bd3856103cf565b602785527f416464726573733a206c6f772d6c6576656c2064656c65676174652063616c6c60208601527f206661696c6564000000000000000000000000000000000000000000000000006040860152602081519101845af4611c346118a9565b91611ca5565b50565b506000611bb7565b90611c4f82611a7a565b73ffffffffffffffffffffffffffffffffffffffff82167fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b600080a2805115801590611c9d57611bbf575050565b506001611bb7565b91929015611d205750815115611cb9575090565b3b15611cc25790565b60646040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152fd5b825190915015611d335750805190602001fd5b610926906040519182917f08c379a00000000000000000000000000000000000000000000000000000000083526020600484015260248301906110df565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b9190811015611db05760051b0190565b611d71565b9035907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe1813603018212156101bd570180359067ffffffffffffffff82116101bd576020019181360383136101bd57565b90821015611db057611e1d9160051b810190611db5565b9091565b60405160208101907f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f82527fcbe29a6ace531c23849b5cdb1a6b991866eb7dc20deda15202ba6fd921ed2c0060408201527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc660608201524660808201523060a082015260a0815260c0810181811067ffffffffffffffff8211176103ae5760405251902090565b6020815191012060405160208101917f5e3baca2936049843f06038876a12f03627b5edc98025751ecf2ac75626401998352604082015260408152611f0c816103cf565b519020611f17611e21565b90604051917f1901000000000000000000000000000000000000000000000000000000000000602084015260228301526042820152604281526080810181811067ffffffffffffffff8211176103ae5760405290565b60405190602082015260208152604081019080821067ffffffffffffffff8311176103ae57611f9e91604052611ec8565b602081519101209073ffffffffffffffffffffffffffffffffffffffff91827f691ec1a18226d004c07c9f8e5c4a6ff15a7b38db267cf7e3c945aef8be5122005416611fea83836120a3565b600581969296101561207457159485612068575b50508315612056575b505050612032577fffffffff0000000000000000000000000000000000000000000000000000000090565b7f1626ba7e0000000000000000000000000000000000000000000000000000000090565b6120609350612166565b388080612007565b16811493503880611ffe565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602160045260246000fd5b9060418151146000146120cd57611e1d916020820151906060604084015193015160001a906120d7565b5050600090600290565b9291907f7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0831161215a5791608094939160ff602094604051948552168484015260408301526060820152600093849182805260015afa15610c6e57815173ffffffffffffffffffffffffffffffffffffffff811615612154579190565b50600190565b50505050600090600390565b600091929082916040516121e3816121b760208201947f1626ba7e00000000000000000000000000000000000000000000000000000000998a875260248401526040604484015260648301906110df565b037fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe081018352826103eb565b51915afa906121f06118a9565b82612219575b8261220057505090565b6122159192506020808251830101910161188e565b1490565b9150602082511015916121f6565b73ffffffffffffffffffffffffffffffffffffffff9081811691821561231c576122ad839273ffffffffffffffffffffffffffffffffffffffff7f691ec1a18226d004c07c9f8e5c4a6ff15a7b38db267cf7e3c945aef8be51220091167fffffffffffffffffffffffff0000000000000000000000000000000000000000825416179055565b7f0000000000000000000000000000000000000000000000000000000000000000167fec6a23b49d2c363d250c9dda15610e835d428207d15ddb36a6c230e37371ddf1600080a360007f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08180a3565b60246040517fb20f76e300000000000000000000000000000000000000000000000000000000815260006004820152fd5b73ffffffffffffffffffffffffffffffffffffffff91827f691ec1a18226d004c07c9f8e5c4a6ff15a7b38db267cf7e3c945aef8be51220054167f19457468657265756d205369676e6564204d6573736167653a0a33320000000060005281601c526123d06123c9610721603c60002095610140810190611db5565b80946120a3565b600581969296101561207457159485612410575b505083156123fe575b5050506123f957600190565b600090565b6124089350612166565b3880806123ed565b168114935038806123e4565b303314158061245c575b61242c57565b60246040517f4a0bfec1000000000000000000000000000000000000000000000000000000008152336004820152fd5b5073ffffffffffffffffffffffffffffffffffffffff7f691ec1a18226d004c07c9f8e5c4a6ff15a7b38db267cf7e3c945aef8be5122005416331415612426565b73ffffffffffffffffffffffffffffffffffffffff807f00000000000000000000000000000000000000000000000000000000000000001633141590816124e6575b5061242c57565b90507f691ec1a18226d004c07c9f8e5c4a6ff15a7b38db267cf7e3c945aef8be5122005416331415386124df565b600091829182602083519301915af161252b6118a9565b90156125345750565b602081519101fd5b916000928392602083519301915af161252b6118a956fea2646970667358221220c5240b5a614209162da17798c4589910308036b820e321c267b03d8cedb5e48164736f6c634300081500330000000000000000000000005ff137d4b0fdcd49dca30c7cf57e578a026d2789" diff --git a/packages/permissionless-test/mock-aa-infra/alto/src/index.ts b/packages/permissionless-test/mock-aa-infra/alto/src/index.ts index 790ab6b5..cc32ca3b 100644 --- a/packages/permissionless-test/mock-aa-infra/alto/src/index.ts +++ b/packages/permissionless-test/mock-aa-infra/alto/src/index.ts @@ -7,7 +7,7 @@ import { parseEther } from "viem" import { mnemonicToAccount } from "viem/accounts" -import { sendTransaction } from "viem/actions" +import { getTransactionReceipt, sendTransaction } from "viem/actions" import { foundry } from "viem/chains" import { BICONOMY_ACCOUNT_V2_LOGIC_CREATECALL, @@ -25,6 +25,7 @@ import { KERNEL_V07_ECDSA_VALIDATOR_V3_CREATECALL, KERNEL_V07_FACTORY_CREATECALL, KERNEL_V07_META_FACTORY_CREATECALL, + LIGHT_ACCOUNT_FACTORY_V110_CREATECALL, SAFE_MULTI_SEND_CALL_ONLY_CREATECALL, SAFE_MULTI_SEND_CREATECALL, SAFE_PROXY_FACTORY_CREATECALL, @@ -311,6 +312,17 @@ const main = async () => { }) .then(() => console.log("[KERNEL] Deploying V0.7 META FACTORY")) + walletClient + .sendTransaction({ + to: DETERMINISTIC_DEPLOYER, + data: LIGHT_ACCOUNT_FACTORY_V110_CREATECALL, + gas: 15_000_000n, + nonce: nonce++ + }) + .then(() => + console.log("[LIGHT ACCOUNT] Deploying V1.1.0 LightAccount Factory") + ) + let onchainNonce = 0 do { onchainNonce = await client.getTransactionCount({ @@ -354,6 +366,28 @@ const main = async () => { address: kernelFactoryOwner }) + // ==== SETUP ALCHEMY LIGHT ACCOUNT CONTRACTS ==== // + const alchemyLightClientOwner = "0xDdF32240B4ca3184De7EC8f0D5Aba27dEc8B7A5C" + await anvilClient.setBalance({ + address: alchemyLightClientOwner, + value: parseEther("100") + }) + + await anvilClient.impersonateAccount({ + address: alchemyLightClientOwner + }) + + await sendTransaction(walletClient, { + account: alchemyLightClientOwner, + to: "0x0000000000400CdFef5E2714E63d8040b700BC24" /* light account v2.0.0 factory */, + data: "0xfbb1c3d40000000000000000000000000000000000000000000000000000000000015180000000000000000000000000000000000000000000000000016345785d8a0000", + value: parseEther("0.1") + }) + + await anvilClient.stopImpersonatingAccount({ + address: alchemyLightClientOwner + }) + await verifyDeployed([ "0x4e59b44847b379578588920ca78fbf26c0b4956c", "0x914d7Fec6aaC8cd542e72Bca78B30650d45643d7", @@ -381,7 +415,9 @@ const main = async () => { "0x8104e3Ad430EA6d354d013A6789fDFc71E671c43", "0x94F097E1ebEB4ecA3AAE54cabb08905B239A7D27", "0x6723b44Abeec4E71eBE3232BD5B455805baDD22f", - "0xd703aaE79538628d27099B8c4f621bE4CCd142d5" + "0xd703aaE79538628d27099B8c4f621bE4CCd142d5", + "0x00004EC70002a32400f8ae005A26081065620D20", // LightAccountFactory V1.1.0 + "0xae8c656ad28F2B59a196AB61815C16A0AE1c3cba" // LightAccount V1.1.0 implementation ]) } diff --git a/packages/permissionless-test/src/ep-0.6/coreSmartClientActions.test.ts b/packages/permissionless-test/src/ep-0.6/coreSmartClientActions.test.ts index b7b32290..e11d87a6 100644 --- a/packages/permissionless-test/src/ep-0.6/coreSmartClientActions.test.ts +++ b/packages/permissionless-test/src/ep-0.6/coreSmartClientActions.test.ts @@ -7,6 +7,7 @@ import { SignTransactionNotSupportedBySmartAccount, signerToBiconomySmartAccount, signerToEcdsaKernelSmartAccount, + signerToLightSmartAccount, signerToSafeSmartAccount, signerToSimpleSmartAccount } from "permissionless/accounts" @@ -40,7 +41,9 @@ import { fund, getBiconomyClient, getBundlerClient, + getFactoryAddress, getKernelEcdsaClient, + getLightAccountClient, getPimlicoPaymasterClient, getPublicClient, getSafeClient, @@ -48,6 +51,20 @@ import { } from "../utils" describe.each([ + { + name: "Light V1.1.0", + getSmartAccountClient: async ( + conf: AAParamType + ) => getLightAccountClient(conf), + getSmartAccountSigner: async (conf: ExistingSignerParamType) => + signerToLightSmartAccount(conf.publicClient, { + address: conf.existingAddress, // this is the field we are testing + signer: privateKeyToAccount(conf.privateKey), + entryPoint: ENTRYPOINT_ADDRESS_V06, + lightVersion: "v1.1.0" + }), + isEip1271Compliant: true + }, { name: "Simple", getSmartAccountClient: async ( @@ -311,7 +328,7 @@ describe.each([ } expect(eventFound).toBeTruthy() - }, 5000) + }, 10000) test("Can send multiple transactions with paymaster", async () => { const smartClient = await getSmartAccountClient({ diff --git a/packages/permissionless-test/src/ep-0.7/coreSmartClinetActions.test.ts b/packages/permissionless-test/src/ep-0.7/coreSmartClinetActions.test.ts index 5936ebfd..d5dee9ad 100644 --- a/packages/permissionless-test/src/ep-0.7/coreSmartClinetActions.test.ts +++ b/packages/permissionless-test/src/ep-0.7/coreSmartClinetActions.test.ts @@ -304,7 +304,7 @@ describe.each([ } expect(eventFound).toBeTruthy() - }, 5000) + }, 10000) test("Can send multiple transactions with paymaster", async () => { const smartClient = await getSmartAccountClient({ diff --git a/packages/permissionless-test/src/utils.ts b/packages/permissionless-test/src/utils.ts index d20d740f..4bd9d8b4 100644 --- a/packages/permissionless-test/src/utils.ts +++ b/packages/permissionless-test/src/utils.ts @@ -11,6 +11,7 @@ import { type SmartAccount, signerToBiconomySmartAccount, signerToEcdsaKernelSmartAccount, + signerToLightSmartAccount, signerToSafeSmartAccount, signerToSimpleSmartAccount } from "permissionless/accounts" @@ -204,6 +205,33 @@ export const getSimpleAccountClient = async ({ }) } +export const getLightAccountClient = async ({ + entryPoint, + paymasterClient, + privateKey = generatePrivateKey() +}: AAParamType): Promise< + SmartAccountClient> +> => { + const smartAccount = await signerToLightSmartAccount( + publicClient, + { + entryPoint, + signer: privateKeyToAccount(privateKey), + lightVersion: "v1.1.0" + } + ) + + // @ts-ignore + return createSmartAccountClient({ + chain: foundry, + account: smartAccount, + bundlerTransport: http(ALTO_RPC), + middleware: { + // @ts-ignore + sponsorUserOperation: paymasterClient?.sponsorUserOperation + } + }) +} // Only supports v0.6 for now export const getBiconomyClient = async ({ paymasterClient, diff --git a/packages/permissionless/accounts/index.ts b/packages/permissionless/accounts/index.ts index e63b8249..778ebd38 100644 --- a/packages/permissionless/accounts/index.ts +++ b/packages/permissionless/accounts/index.ts @@ -9,6 +9,17 @@ import { signerToSimpleSmartAccount } from "./simple/signerToSimpleSmartAccount" +import { + type PrivateKeyToLightSmartAccountParameters, + privateKeyToLightSmartAccount +} from "./light/privateKeyToLightSmartAccount" + +import { + type LightSmartAccount, + type SignerToLightSmartAccountParameters, + signerToLightSmartAccount +} from "./light/signerToLightSmartAccount" + import { type PrivateKeyToSafeSmartAccountParameters, privateKeyToSafeSmartAccount @@ -53,9 +64,12 @@ export { signerToSafeSmartAccount, type SimpleSmartAccount, signerToSimpleSmartAccount, + type LightSmartAccount, + signerToLightSmartAccount, SignTransactionNotSupportedBySmartAccount, privateKeyToBiconomySmartAccount, privateKeyToSimpleSmartAccount, + privateKeyToLightSmartAccount, type SmartAccount, privateKeyToSafeSmartAccount, type KernelEcdsaSmartAccount, @@ -64,8 +78,10 @@ export { signerToBiconomySmartAccount, toSmartAccount, type SignerToSimpleSmartAccountParameters, + type SignerToLightSmartAccountParameters, type SignerToSafeSmartAccountParameters, type PrivateKeyToSimpleSmartAccountParameters, + type PrivateKeyToLightSmartAccountParameters, type PrivateKeyToSafeSmartAccountParameters, type SignerToEcdsaKernelSmartAccountParameters, type SignerToBiconomySmartAccountParameters, diff --git a/packages/permissionless/accounts/light/privateKeyToLightSmartAccount.ts b/packages/permissionless/accounts/light/privateKeyToLightSmartAccount.ts new file mode 100644 index 00000000..dcc5ab5e --- /dev/null +++ b/packages/permissionless/accounts/light/privateKeyToLightSmartAccount.ts @@ -0,0 +1,37 @@ +import { type Chain, type Client, type Hex, type Transport } from "viem" +import { privateKeyToAccount } from "viem/accounts" +import type { ENTRYPOINT_ADDRESS_V06_TYPE, Prettify } from "../../types" +import { + type LightSmartAccount, + type SignerToLightSmartAccountParameters, + signerToLightSmartAccount +} from "./signerToLightSmartAccount" + +export type PrivateKeyToLightSmartAccountParameters< + entryPoint extends ENTRYPOINT_ADDRESS_V06_TYPE +> = Prettify< + { + privateKey: Hex + } & Omit, "signer"> +> + +/** + * @description Creates an Simple Account from a private key. + * + * @returns A Private Key Simple Account. + */ +export async function privateKeyToLightSmartAccount< + entryPoint extends ENTRYPOINT_ADDRESS_V06_TYPE, + TTransport extends Transport = Transport, + TChain extends Chain | undefined = Chain | undefined +>( + client: Client, + { privateKey, ...rest }: PrivateKeyToLightSmartAccountParameters +): Promise> { + const privateKeyAccount = privateKeyToAccount(privateKey) + + return signerToLightSmartAccount(client, { + signer: privateKeyAccount, + ...rest + }) +} diff --git a/packages/permissionless/accounts/light/signerToLightSmartAccount.ts b/packages/permissionless/accounts/light/signerToLightSmartAccount.ts new file mode 100644 index 00000000..14c47bf8 --- /dev/null +++ b/packages/permissionless/accounts/light/signerToLightSmartAccount.ts @@ -0,0 +1,407 @@ +import { + type Address, + type Chain, + type Client, + type Hex, + type LocalAccount, + type Transport, + type TypedData, + type TypedDataDefinition, + concatHex, + encodeFunctionData, + hashMessage, + hashTypedData +} from "viem" +import { getChainId, signMessage } from "viem/actions" +import { getAccountNonce } from "../../actions/public/getAccountNonce" +import { getSenderAddress } from "../../actions/public/getSenderAddress" +import type { + ENTRYPOINT_ADDRESS_V06_TYPE, + ENTRYPOINT_ADDRESS_V07_TYPE, + Prettify +} from "../../types" +import type { EntryPoint } from "../../types/entrypoint" +import { getEntryPointVersion } from "../../utils" +import { getUserOperationHash } from "../../utils/getUserOperationHash" +import { isSmartAccountDeployed } from "../../utils/isSmartAccountDeployed" +import { toSmartAccount } from "../toSmartAccount" +import { + SignTransactionNotSupportedBySmartAccount, + type SmartAccount, + type SmartAccountSigner +} from "../types" + +export type LightSmartAccount< + entryPoint extends EntryPoint, + transport extends Transport = Transport, + chain extends Chain | undefined = Chain | undefined +> = SmartAccount + +const getAccountInitCode = async ( + owner: Address, + index = BigInt(0) +): Promise => { + if (!owner) throw new Error("Owner account not found") + + return encodeFunctionData({ + abi: [ + { + inputs: [ + { + internalType: "address", + name: "owner", + type: "address" + }, + { + internalType: "uint256", + name: "salt", + type: "uint256" + } + ], + name: "createAccount", + outputs: [ + { + internalType: "contract LightAccount", + name: "ret", + type: "address" + } + ], + stateMutability: "nonpayable", + type: "function" + } + ], + functionName: "createAccount", + args: [owner, index] + }) +} + +const getAccountAddress = async < + entryPoint extends EntryPoint, + TTransport extends Transport = Transport, + TChain extends Chain | undefined = Chain | undefined +>({ + client, + factoryAddress, + entryPoint: entryPointAddress, + owner, + index = BigInt(0) +}: { + client: Client + factoryAddress: Address + owner: Address + entryPoint: entryPoint + index?: bigint +}): Promise
=> { + const entryPointVersion = getEntryPointVersion(entryPointAddress) + + const factoryData = await getAccountInitCode(owner, index) + + if (entryPointVersion === "v0.6") { + return getSenderAddress(client, { + initCode: concatHex([factoryAddress, factoryData]), + entryPoint: entryPointAddress as ENTRYPOINT_ADDRESS_V06_TYPE + }) + } + + // Get the sender address based on the init code + return getSenderAddress(client, { + factory: factoryAddress, + factoryData, + entryPoint: entryPointAddress as ENTRYPOINT_ADDRESS_V07_TYPE + }) +} + +export type LightVersion = "v1.1.0" + +export type SignerToLightSmartAccountParameters< + entryPoint extends EntryPoint, + TSource extends string = string, + TAddress extends Address = Address +> = Prettify<{ + signer: SmartAccountSigner + lightVersion: LightVersion + entryPoint: entryPoint + factoryAddress?: Address + index?: bigint + address?: Address +}> + +async function signWith1271WrapperV1< + TSource extends string = string, + TAddress extends Address = Address +>( + signer: SmartAccountSigner, + chainId: number, + accountAddress: Address, + hashedMessage: Hex +): Promise { + return signer.signTypedData({ + domain: { + chainId: Number(chainId), + name: "LightAccount", + verifyingContract: accountAddress, + version: "1" + }, + types: { + LightAccountMessage: [{ name: "message", type: "bytes" }] + }, + message: { + message: hashedMessage + }, + primaryType: "LightAccountMessage" + }) +} + +const LIGHT_VERSION_TO_ADDRESSES_MAP: { + [key in LightVersion]: { + factoryAddress: Address + } +} = { + "v1.1.0": { + factoryAddress: "0x00004EC70002a32400f8ae005A26081065620D20" + } +} + +const getDefaultAddresses = ( + lightVersion: LightVersion, + { + factoryAddress: _factoryAddress + }: { + factoryAddress?: Address + } +) => { + const factoryAddress = + _factoryAddress ?? + LIGHT_VERSION_TO_ADDRESSES_MAP[lightVersion].factoryAddress + + return { + factoryAddress + } +} + +/** + * @description Creates an Light Account from a private key. + * + * @returns A Private Key Light Account. + */ +export async function signerToLightSmartAccount< + entryPoint extends EntryPoint, + TTransport extends Transport = Transport, + TChain extends Chain | undefined = Chain | undefined, + TSource extends string = string, + TAddress extends Address = Address +>( + client: Client, + { + signer, + address, + lightVersion, + entryPoint: entryPointAddress, + index = BigInt(0), + factoryAddress: _factoryAddress + }: SignerToLightSmartAccountParameters +): Promise> { + const viemSigner: LocalAccount = { + ...signer, + signTransaction: (_, __) => { + throw new SignTransactionNotSupportedBySmartAccount() + } + } as LocalAccount + + if (lightVersion !== "v1.1.0") { + throw new Error( + "Only Light Account version v1.1.0 is supported at the moment" + ) + } + + const { factoryAddress } = getDefaultAddresses(lightVersion, { + factoryAddress: _factoryAddress + }) + + const [accountAddress, chainId] = await Promise.all([ + address ?? + getAccountAddress({ + client, + factoryAddress, + entryPoint: entryPointAddress, + owner: viemSigner.address, + index + }), + client.chain?.id ?? getChainId(client) + ]) + + if (!accountAddress) throw new Error("Account address not found") + + let smartAccountDeployed = await isSmartAccountDeployed( + client, + accountAddress + ) + + return toSmartAccount({ + address: accountAddress, + signMessage: async ({ message }) => { + return signWith1271WrapperV1( + signer, + chainId, + accountAddress, + hashMessage(message) + ) + }, + signTransaction: (_, __) => { + throw new SignTransactionNotSupportedBySmartAccount() + }, + async signTypedData< + const TTypedData extends TypedData | Record, + TPrimaryType extends + | keyof TTypedData + | "EIP712Domain" = keyof TTypedData + >(typedData: TypedDataDefinition) { + return signWith1271WrapperV1( + signer, + chainId, + accountAddress, + hashTypedData(typedData) + ) + }, + client: client, + publicKey: accountAddress, + entryPoint: entryPointAddress, + source: "LightSmartAccount", + async getNonce() { + return getAccountNonce(client, { + sender: accountAddress, + entryPoint: entryPointAddress + }) + }, + async signUserOperation(userOperation) { + return signMessage(client, { + account: viemSigner, + message: { + raw: getUserOperationHash({ + userOperation, + entryPoint: entryPointAddress, + chainId: chainId + }) + } + }) + }, + async getInitCode() { + if (smartAccountDeployed) return "0x" + + smartAccountDeployed = await isSmartAccountDeployed( + client, + accountAddress + ) + + if (smartAccountDeployed) return "0x" + + return concatHex([ + factoryAddress, + await getAccountInitCode(viemSigner.address, index) + ]) + }, + async getFactory() { + if (smartAccountDeployed) return undefined + smartAccountDeployed = await isSmartAccountDeployed( + client, + accountAddress + ) + if (smartAccountDeployed) return undefined + return factoryAddress + }, + async getFactoryData() { + if (smartAccountDeployed) return undefined + smartAccountDeployed = await isSmartAccountDeployed( + client, + accountAddress + ) + if (smartAccountDeployed) return undefined + return getAccountInitCode(viemSigner.address, index) + }, + async encodeDeployCallData(_) { + throw new Error("Light account doesn't support account deployment") + }, + async encodeCallData(args) { + if (Array.isArray(args)) { + const argsArray = args as { + to: Address + value: bigint + data: Hex + }[] + + return encodeFunctionData({ + abi: [ + { + inputs: [ + { + internalType: "address[]", + name: "dest", + type: "address[]" + }, + { + internalType: "uint256[]", + name: "value", + type: "uint256[]" + }, + { + internalType: "bytes[]", + name: "func", + type: "bytes[]" + } + ], + name: "executeBatch", + outputs: [], + stateMutability: "nonpayable", + type: "function" + } + ], + functionName: "executeBatch", + args: [ + argsArray.map((a) => a.to), + argsArray.map((a) => a.value), + argsArray.map((a) => a.data) + ] + }) + } + + const { to, value, data } = args as { + to: Address + value: bigint + data: Hex + } + + return encodeFunctionData({ + abi: [ + { + inputs: [ + { + internalType: "address", + name: "dest", + type: "address" + }, + { + internalType: "uint256", + name: "value", + type: "uint256" + }, + { + internalType: "bytes", + name: "func", + type: "bytes" + } + ], + name: "execute", + outputs: [], + stateMutability: "nonpayable", + type: "function" + } + ], + functionName: "execute", + args: [to, value, data] + }) + }, + async getDummySignature(_userOperation) { + return "0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c" + } + }) +}