@@ -3,6 +3,7 @@ import { RunTab } from "../types/run-tab"
33import { clearInstances , setAccount , setExecEnv } from "./actions"
44import { displayNotification , fetchAccountsListFailed , fetchAccountsListRequest , fetchAccountsListSuccess , setMatchPassphrase , setPassphrase } from "./payload"
55import { toChecksumAddress } from '@ethereumjs/util'
6+ import { SmartAccount } from "../types"
67import "viem/window"
78import { custom , createWalletClient } from "viem"
89import { sepolia } from "viem/chains"
@@ -26,6 +27,14 @@ export const fillAccountsList = async (plugin: RunTab, dispatch: React.Dispatch<
2627 dispatch ( fetchAccountsListRequest ( ) )
2728 try {
2829 let accounts = await plugin . blockchain . getAccounts ( )
30+ const provider = plugin . blockchain . getProvider ( )
31+ if ( provider && provider . startsWith ( 'injected' ) && accounts ?. length ) {
32+ await loadSmartAccounts ( plugin )
33+ if ( plugin . REACT_API . smartAccounts ) {
34+ const safeAddresses = Object . keys ( plugin . REACT_API . smartAccounts )
35+ accounts . push ( ...safeAddresses )
36+ }
37+ }
2938 if ( ! accounts ) accounts = [ ]
3039
3140 const loadedAccounts = { }
@@ -34,7 +43,6 @@ export const fillAccountsList = async (plugin: RunTab, dispatch: React.Dispatch<
3443 const balance = await plugin . blockchain . getBalanceInEther ( account )
3544 loadedAccounts [ account ] = shortenAddress ( account , balance )
3645 }
37- const provider = plugin . blockchain . getProvider ( )
3846
3947 if ( provider && provider . startsWith ( 'injected' ) ) {
4048 const selectedAddress = plugin . blockchain . getInjectedWeb3Address ( )
@@ -93,7 +101,7 @@ export const createNewBlockchainAccount = async (plugin: RunTab, dispatch: React
93101}
94102
95103export const createSmartAccount = async ( plugin : RunTab , dispatch : React . Dispatch < any > ) => {
96- console . log ( 'createSmartAccount action' )
104+ const localStorageKey = 'smartAccounts'
97105
98106 // @ts -ignore
99107 const [ account ] = await window . ethereum ! . request ( { method : 'eth_requestAccounts' } )
@@ -105,21 +113,55 @@ export const createSmartAccount = async (plugin: RunTab, dispatch: React.Dispatc
105113 transport : custom ( window . ethereum ! ) ,
106114 } )
107115
116+ const salt = 0
108117 const safeAccount = await toSafeSmartAccount ( {
109118 client : walletClient ,
110119 entryPoint : {
111120 address : entryPoint07Address ,
112121 version : "0.7" ,
113122 } ,
114123 owners : [ toAccount ( account ) ] ,
115- // saltNonce: 0n, // optional
124+ saltNonce : salt ,
116125 version : "1.4.1"
117126 } )
127+ const safeAddress = safeAccount . address
128+
129+ const sAccount : SmartAccount = {
130+ address : safeAccount . address ,
131+ salt,
132+ ownerEOA : account ,
133+ timestamp : Date . now ( )
134+ }
135+ plugin . REACT_API . smartAccounts [ safeAddress ] = sAccount
136+ // Save smart accounts in local storage
137+ const smartAccountsStr = localStorage . getItem ( localStorageKey )
138+ const smartAccountsObj = JSON . parse ( smartAccountsStr )
139+ smartAccountsObj [ plugin . REACT_API . chainId ] = plugin . REACT_API . smartAccounts
140+ localStorage . setItem ( localStorageKey , JSON . stringify ( smartAccountsObj ) )
118141
119- console . log ( 'safeAccount----->' , safeAccount . address )
120142 return plugin . call ( 'notification' , 'toast' , `Safe account ${ safeAccount . address } created for owner ${ account } ` )
121143}
122144
145+ export const loadSmartAccounts = async ( plugin ) => {
146+ const { chainId } = plugin . REACT_API
147+ const localStorageKey = 'smartAccounts'
148+
149+ const smartAccountsStr = localStorage . getItem ( localStorageKey )
150+ if ( smartAccountsStr ) {
151+ const smartAccountsObj = JSON . parse ( smartAccountsStr )
152+ if ( smartAccountsObj [ chainId ] ) {
153+ plugin . REACT_API . smartAccounts = smartAccountsObj [ chainId ]
154+ } else {
155+ smartAccountsObj [ chainId ] = { }
156+ localStorage . setItem ( localStorageKey , JSON . stringify ( smartAccountsObj ) )
157+ }
158+ } else {
159+ const objToStore = { }
160+ objToStore [ chainId ] = { }
161+ localStorage . setItem ( localStorageKey , JSON . stringify ( objToStore ) )
162+ }
163+ }
164+
123165export const signMessageWithAddress = ( plugin : RunTab , dispatch : React . Dispatch < any > , account : string , message : string , modalContent : ( hash : string , data : string ) => JSX . Element , passphrase ?: string ) => {
124166 plugin . blockchain . signMessage ( message , account , passphrase , ( err , msgHash , signedData ) => {
125167 if ( err ) {
0 commit comments