-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto-add key to wallet (start with test wallet)
- Loading branch information
1 parent
dd58393
commit b5af5d5
Showing
4 changed files
with
173 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import type { Browser, Runtime, Tabs } from 'webextension-polyfill'; | ||
import type { WalletAddress } from '@interledger/open-payments'; | ||
import type { Cradle } from '@/background/container'; | ||
import { ErrorWithKey, withResolvers } from '@/shared/helpers'; | ||
|
||
export const CONNECTION_NAME = 'key-share'; | ||
|
||
type OnTabRemovedCallback = Parameters< | ||
Browser['tabs']['onRemoved']['addListener'] | ||
>[0]; | ||
type OnConnectCallback = Parameters< | ||
Browser['runtime']['onConnect']['addListener'] | ||
>[0]; | ||
type OnPortMessageListener = Parameters< | ||
Runtime.Port['onMessage']['addListener'] | ||
>[0]; | ||
|
||
type BeginPayload = { walletAddressUrl: string; publicKey: string }; | ||
|
||
export class KeyShareService { | ||
private browser: Cradle['browser']; | ||
private storage: Cradle['storage']; | ||
|
||
private status: null | 'SUCCESS' | 'ERROR' = null; | ||
private tab: Tabs.Tab | null = null; | ||
|
||
constructor({ browser, storage }: Pick<Cradle, 'browser' | 'storage'>) { | ||
Object.assign(this, { browser, storage }); | ||
} | ||
|
||
async addPublicKeyToWallet(walletAddress: WalletAddress) { | ||
const { publicKey } = await this.storage.get(['publicKey']); | ||
if (!publicKey) { | ||
// won't happen, just added for lint fix | ||
throw new Error('No public key found'); | ||
} | ||
const info = walletAddressToProvider(walletAddress); | ||
try { | ||
this.setConnectState('adding-key'); | ||
await this.process(info.url, { | ||
publicKey, | ||
walletAddressUrl: walletAddress.id, | ||
}); | ||
} catch (error) { | ||
if (this.tab?.id) { | ||
// can redirect to OPEN_PAYMENTS_REDIRECT_URL | ||
await this.browser.tabs.remove(this.tab.id); | ||
} | ||
this.setConnectState('error-key'); | ||
throw error; | ||
} | ||
} | ||
|
||
private async process( | ||
url: string, | ||
{ walletAddressUrl, publicKey }: BeginPayload, | ||
) { | ||
const { resolve, reject, promise } = withResolvers(); | ||
|
||
const tab = await this.browser.tabs.create({ url }); | ||
this.tab = tab; | ||
if (!tab.id) { | ||
reject(new Error('Could not create tab')); | ||
return promise; | ||
} | ||
|
||
const onTabCloseListener: OnTabRemovedCallback = (tabId) => { | ||
if (tabId !== tab.id) { | ||
// ignore. not our tab | ||
return; | ||
} | ||
|
||
if (this.status === 'SUCCESS') { | ||
// ok | ||
} else { | ||
reject(new Error('Tab closed before completion')); | ||
} | ||
}; | ||
this.browser.tabs.onRemoved.addListener(onTabCloseListener); | ||
|
||
const onConnectListener: OnConnectCallback = (port) => { | ||
if (port.name !== CONNECTION_NAME) return; | ||
if (port.error) { | ||
reject(new Error(port.error.message)); | ||
return; | ||
} | ||
|
||
port.postMessage({ | ||
action: 'BEGIN', | ||
payload: { walletAddressUrl, publicKey }, | ||
}); | ||
|
||
port.onMessage.addListener(onMessageListener); | ||
|
||
port.onDisconnect.addListener(() => { | ||
// wait for connect again so we can send message again if not connected, | ||
// and not errored already (e.g. page refreshed) | ||
}); | ||
}; | ||
|
||
const onMessageListener: OnPortMessageListener = (message: { | ||
action: string; | ||
payload: any; | ||
}) => { | ||
if (message.action === 'SUCCESS') { | ||
resolve(message.payload); | ||
} else if (message.action === 'ERROR') { | ||
reject(message.payload); | ||
} else if (message.action === 'PROGRESS') { | ||
// can save progress to show in popup | ||
} else { | ||
reject(new Error(`Unexpected message: ${JSON.stringify(message)}`)); | ||
} | ||
}; | ||
|
||
this.browser.runtime.onConnect.addListener(onConnectListener); | ||
|
||
return promise; | ||
} | ||
|
||
private setConnectState(status: 'adding-key' | 'error-key' | null) { | ||
const state = status ? { status } : null; | ||
this.storage.setPopupTransientState('connect', () => state); | ||
} | ||
} | ||
|
||
export function walletAddressToProvider(walletAddress: WalletAddress): { | ||
url: string; | ||
} { | ||
const { host } = new URL(walletAddress.id); | ||
switch (host) { | ||
case 'ilp.rafiki.money': | ||
return { | ||
url: 'https://rafiki.money/settings/developer-keys', | ||
}; | ||
// case 'eu1.fynbos.me': // fynbos dev | ||
// case 'fynbos.me': // fynbos production | ||
default: | ||
throw new ErrorWithKey('connectWalletKeyService_error_notImplemented'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters