Skip to content

Commit

Permalink
fix auto login (#676)
Browse files Browse the repository at this point in the history
* fix ss autologin

* fix ss autologin

* fix autologin
  • Loading branch information
amitojsingh366 authored Jun 9, 2024
1 parent c3b527b commit 6f15caf
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 179 deletions.
91 changes: 38 additions & 53 deletions packages/keepkey-desktop-app/src/pages/Browser/Browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,45 +81,43 @@ const clearClipBoardIfWCString = async () => {
}

const checkIfSSDApp = (currentUrl: string) => {
const url = new URL(currentUrl)
const kkDesktopApiKey = localStorage.getItem('@app/serviceKey')
const webview = getWebview()

if (
url.origin === getConfig().REACT_APP_SHAPESHIFT_DAPP_URL ||
url.origin === 'http://localhost:3000'
) {
if (!webview) return

webview
.executeJavaScript('localStorage.getItem("localWalletType");', true)
.then((savedWalletType: any) => {
if (!savedWalletType || savedWalletType === 'keepkey') {
const localWalletDeviceId = localStorage.getItem('localWalletDeviceId')
webview
.executeJavaScript('localStorage.getItem("localWalletDeviceId");', true)
.then((savedWalletId: any) => {
if (!savedWalletId || savedWalletId !== localWalletDeviceId) {
if (!kkDesktopApiKey || !localWalletDeviceId) return

ipcListeners.getSSAutoLogin(localWalletDeviceId, kkDesktopApiKey).then(
(injection: any) => {
console.log('INJECTION', injection)
webview.executeJavaScript(injection)
},
// .then(() => webview.reload())
)
}
})
}
})
.catch(console.error)
try {
if (currentUrl === 'about:blank') return
const url = new URL(currentUrl)
if (
url.origin === getConfig().REACT_APP_SHAPESHIFT_DAPP_URL ||
url.origin === 'http://localhost:3000'
) {
const webview = getWebview()
if (!webview) return
webview
.executeJavaScript('localStorage.getItem("localWalletType");', true)
.then((savedWalletType: any) => {
if (!savedWalletType || savedWalletType === 'keepkey') {
const localWalletDeviceId = localStorage.getItem('localWalletDeviceId')
webview
.executeJavaScript('localStorage.getItem("localWalletDeviceId");', true)
.then((savedWalletId: any) => {
if (!savedWalletId || savedWalletId !== localWalletDeviceId) {
const kkDesktopApiKey = localStorage.getItem('@app/serviceKey')
if (!kkDesktopApiKey || !localWalletDeviceId) return

ipcListeners.getSSAutoLogin(localWalletDeviceId, kkDesktopApiKey).then(
injection => {
console.log('INJECTION', injection)
webview.executeJavaScript(injection)
},
// .then(() => webview.reload())
)
}
})
}
})
.catch(console.error)
}
} catch (error) {
console.error(error)
}

if (kkDesktopApiKey)
ipcListeners
.getBrowserInjection(kkDesktopApiKey)
.then((injection: any) => webview.executeJavaScript(injection))
}

export const Browser = () => {
Expand Down Expand Up @@ -201,6 +199,7 @@ export const Browser = () => {
webview.executeJavaScript(`localStorage.setItem(
'WCM_RECENT_WALLET_DATA',
'{"id": "fdcaaa47c154988ff2ce28d39248eb10366ec60c7de725f73b0d33b5bb9b9a64","name": "KeepKey Desktop","homepage": "https://www.keepkey.com/","image_id": "eb4227d9-366c-466c-db8f-ab7e45985500","order": 5690,"desktop": {"native": "keepkey://launch","universal": ""}}')`)
checkIfSSDApp(webview.getURL())
}
webview.addEventListener('dom-ready', listener)
return () => {
Expand All @@ -212,7 +211,7 @@ export const Browser = () => {
const webview = getWebviewWc()!
const listener = () => {
setWebviewWcReady(true)
ipcListeners.then((injection: any) => webview.executeJavaScript(injection))
//ipcListeners.then((injection: any) => webview.executeJavaScript(injection))
}
webview.addEventListener('dom-ready', listener)
return () => {
Expand Down Expand Up @@ -276,20 +275,6 @@ export const Browser = () => {
}
}, [])

useEffect(() => {
const webview = getWebview()!
const listener = () => {
const url = webview.getURL()
if (url === 'about:blank') return
//dispatch({ type: WalletActions.SET_BROWSER_URL, payload: url })
checkIfSSDApp(url)
}
webview.addEventListener('did-finish-load', listener)
return () => {
webview.removeEventListener('did-finish-load', listener)
}
}, [dispatch])

useEffect(() => {
const webview = getWebview()!
if (webviewReady && (url !== webview.getURL() || forceLoad)) {
Expand Down
118 changes: 0 additions & 118 deletions packages/keepkey-desktop/assets/browser_injection.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/keepkey-desktop/assets/ss_autologin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Initialized in browser_injection.js
// const KK_SDK_API_KEY = 'API_KEY_HERE'
const KK_SDK_API_KEY = 'API_KEY_HERE'
const KK_DEVICE_ID = 'WALLET_DEVICE_ID_HERE'

console.log('LOADED SS AUTOLOGIN SCRIPT')
Expand Down
8 changes: 7 additions & 1 deletion packages/keepkey-desktop/scripts/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ export const dev = async () => {

await build()

// only build dir in dev, speeds up the process
await electronBuilder.build({
dir: true,
x64: false,
ia32: false,
armv7l: false,
arm64: false,
universal: false,
})

console.log('Launching unpacked app...')
Expand Down Expand Up @@ -51,7 +57,7 @@ export const dev = async () => {

child.stderr.on('data', data => {
console.error(`stderr [electron main] : ${data}`)
console.error("stderr [electron main] : ",JSON.stringify(data))
console.error('stderr [electron main] : ', JSON.stringify(data))
})

process.on('SIGINT', () => {
Expand Down
5 changes: 0 additions & 5 deletions packages/keepkey-desktop/src/ipcListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,6 @@ export const ipcListeners: IpcListeners = {
})
},

async getBrowserInjection(sdkApiKey: string) {
const injection = readFileSync(path.join(__dirname, 'assets/browser_injection.js'))
return injection.toString().replace('API_KEY_HERE', sdkApiKey)
},

async getSSAutoLogin(walletId: string, sdkApiKey: string) {
const injection = readFileSync(path.join(__dirname, 'assets/ss_autologin.js'))
return injection
Expand Down
1 change: 0 additions & 1 deletion packages/keepkey-desktop/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export type IpcListeners = {
forceReconnect(): Promise<void>
setAlwaysOnTop(value: boolean): Promise<void>
clearLocalStorage(): Promise<void>
getBrowserInjection(sdkApiKey: string): Promise<string>
getSSAutoLogin(walletId: string, sdkApiKey: string): Promise<string>
getProtocolLaunchUrl(): Promise<string | undefined>
handleWalletConnectUrlInProtocol(connect: (uri: string) => Promise<void>): Promise<void>
Expand Down

0 comments on commit 6f15caf

Please sign in to comment.