@@ -3,6 +3,7 @@ import { RunTab } from "../types/run-tab"
3
3
import { clearInstances , setAccount , setExecEnv } from "./actions"
4
4
import { displayNotification , fetchAccountsListFailed , fetchAccountsListRequest , fetchAccountsListSuccess , setMatchPassphrase , setPassphrase } from "./payload"
5
5
import { toChecksumAddress } from '@ethereumjs/util'
6
+ import { SmartAccount } from "../types"
6
7
import "viem/window"
7
8
import { custom , createWalletClient } from "viem"
8
9
import { sepolia } from "viem/chains"
@@ -26,6 +27,14 @@ export const fillAccountsList = async (plugin: RunTab, dispatch: React.Dispatch<
26
27
dispatch ( fetchAccountsListRequest ( ) )
27
28
try {
28
29
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
+ }
29
38
if ( ! accounts ) accounts = [ ]
30
39
31
40
const loadedAccounts = { }
@@ -34,7 +43,6 @@ export const fillAccountsList = async (plugin: RunTab, dispatch: React.Dispatch<
34
43
const balance = await plugin . blockchain . getBalanceInEther ( account )
35
44
loadedAccounts [ account ] = shortenAddress ( account , balance )
36
45
}
37
- const provider = plugin . blockchain . getProvider ( )
38
46
39
47
if ( provider && provider . startsWith ( 'injected' ) ) {
40
48
const selectedAddress = plugin . blockchain . getInjectedWeb3Address ( )
@@ -93,7 +101,7 @@ export const createNewBlockchainAccount = async (plugin: RunTab, dispatch: React
93
101
}
94
102
95
103
export const createSmartAccount = async ( plugin : RunTab , dispatch : React . Dispatch < any > ) => {
96
- console . log ( 'createSmartAccount action' )
104
+ const localStorageKey = 'smartAccounts'
97
105
98
106
// @ts -ignore
99
107
const [ account ] = await window . ethereum ! . request ( { method : 'eth_requestAccounts' } )
@@ -105,21 +113,55 @@ export const createSmartAccount = async (plugin: RunTab, dispatch: React.Dispatc
105
113
transport : custom ( window . ethereum ! ) ,
106
114
} )
107
115
116
+ const salt = 0
108
117
const safeAccount = await toSafeSmartAccount ( {
109
118
client : walletClient ,
110
119
entryPoint : {
111
120
address : entryPoint07Address ,
112
121
version : "0.7" ,
113
122
} ,
114
123
owners : [ toAccount ( account ) ] ,
115
- // saltNonce: 0n, // optional
124
+ saltNonce : salt ,
116
125
version : "1.4.1"
117
126
} )
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 ) )
118
141
119
- console . log ( 'safeAccount----->' , safeAccount . address )
120
142
return plugin . call ( 'notification' , 'toast' , `Safe account ${ safeAccount . address } created for owner ${ account } ` )
121
143
}
122
144
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
+
123
165
export const signMessageWithAddress = ( plugin : RunTab , dispatch : React . Dispatch < any > , account : string , message : string , modalContent : ( hash : string , data : string ) => JSX . Element , passphrase ?: string ) => {
124
166
plugin . blockchain . signMessage ( message , account , passphrase , ( err , msgHash , signedData ) => {
125
167
if ( err ) {
0 commit comments