@@ -4,22 +4,42 @@ import Log from 'electron-log'
44export default class Keytar {
55 static readonly appService : string = 'MyVergies'
66 static readonly walletService : string = 'MyVergies Wallet'
7+ static readonly accessErrorTitle : string = 'Keychain Access Needed'
8+ static readonly accessErrorMessage : string = 'MyVergies could not access the macOS Keychain. Enter your Mac login password and choose "Always Allow" if prompted, then try again. If you denied access earlier, restart MyVergies and retry.'
79
8- static setCredentials ( service : string , account : string , credentials : string ) {
10+ static async setCredentials ( service : string , account : string , credentials : string ) {
911 Log . info ( `set credentials for service "${ service } " and account "${ account } "` )
1012
11- return electron . ipcRenderer . sendSync ( 'set-password' , service , account , credentials )
13+ return await this . request ( 'set-password' , service , account , credentials )
1214 }
1315
14- static getCredentials ( service : string , account : string ) {
16+ static async getCredentials ( service : string , account : string ) {
1517 Log . info ( `get credentials for service "${ service } " and account "${ account } "` )
1618
17- return electron . ipcRenderer . sendSync ( 'get-password' , service , account )
19+ return await this . request ( 'get-password' , service , account )
1820 }
1921
20- static deleteCredentials ( service : string , account : string ) {
22+ static async deleteCredentials ( service : string , account : string ) {
2123 Log . info ( `delete credentials for service "${ service } " and account "${ account } "` )
2224
23- return electron . ipcRenderer . sendSync ( 'delete-password' , service , account )
25+ return await this . request ( 'delete-password' , service , account )
26+ }
27+
28+ static isAccessError ( error : any ) : boolean {
29+ return Boolean ( error && error . isKeytarAccessError )
30+ }
31+
32+ protected static async request ( channel : string , ...args : any [ ] ) {
33+ try {
34+ return await electron . ipcRenderer . invoke ( channel , ...args )
35+ } catch ( error ) {
36+ Log . error ( `keychain request "${ channel } " failed: ${ error instanceof Error ? error . message : String ( error ) } ` )
37+
38+ const wrappedError : any = new Error ( Keytar . accessErrorMessage )
39+ wrappedError . isKeytarAccessError = true
40+ wrappedError . originalMessage = error instanceof Error ? error . message : String ( error )
41+
42+ throw wrappedError
43+ }
2444 }
2545}
0 commit comments