Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2.0.46+ext #605

Merged
merged 30 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4f60c4e
Update connector extension
heathsnee Jun 30, 2024
17fdc82
Reload page on wallet change
heathsnee Jun 30, 2024
d74f6e6
Fix wallet interactions
heathsnee Jul 1, 2024
b589377
Fix ledger interactions
heathsnee Jul 1, 2024
ef48066
Handle focus ledger tab action
heathsnee Jul 1, 2024
3a61faa
Update rdt
heathsnee Jul 6, 2024
5ee09ed
Override radix deps to fix types issues
heathsnee Jul 10, 2024
d56b509
Fix radix connect button state
heathsnee Jul 10, 2024
2bba33b
Update gateway module details
heathsnee Jul 10, 2024
a69ef14
Override radix deps to fix types issues
heathsnee Jul 10, 2024
16fcf40
Override radix deps to fix types issues
heathsnee Jul 10, 2024
f921f3e
Example fixes
heathsnee Jul 10, 2024
14f5ee1
Merge pull request #604 from z3us-dapps/feat/connector-1.4.1
heathsnee Jul 10, 2024
b1e8988
v2.0.46+ext
heathsnee Jul 10, 2024
7b06f5f
Merge branch 'main' of github-heathsnee:z3us-dapps/z3us into develop
heathsnee Jul 10, 2024
ac9721e
Add neverthrow to paths
heathsnee Jul 11, 2024
dad587e
Add neverthrow to paths
heathsnee Jul 11, 2024
e7d0d36
Add neverthrow to paths
heathsnee Jul 11, 2024
2a37816
Add neverthrow to paths
heathsnee Jul 11, 2024
ede4a30
Add neverthrow to paths
heathsnee Jul 11, 2024
f244209
Add neverthrow to paths
heathsnee Jul 11, 2024
faa5139
Add neverthrow to paths
heathsnee Jul 11, 2024
1abc034
Add neverthrow to paths
heathsnee Jul 11, 2024
49b21d0
Add neverthrow to paths
heathsnee Jul 11, 2024
735f14a
Add neverthrow to paths
heathsnee Jul 11, 2024
dbc546f
Add neverthrow to paths
heathsnee Jul 11, 2024
c387a63
Add neverthrow to paths
heathsnee Jul 11, 2024
f007b3c
Reset input value on unlock
heathsnee Jul 19, 2024
e98e7c3
Update rdt
heathsnee Jul 19, 2024
52d3798
preserveSymlinks
heathsnee Jul 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion apps/extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,15 @@ In case user changes selected wallet you might want to reset connect button stat
```typescript
const rdt = RadixDappToolkit(options)

window.z3us.onWalletChange(() => { rdt.disconnect() })
window.z3us.onWalletChange(() => {
rdt.disconnect()
})
```

Reload page on z3us wallet change to update Radix Connector underlying state

```typescript
z3us.onWalletChange(() => {
window.location.reload()
})
```
14 changes: 7 additions & 7 deletions apps/extension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "z3us-extension",
"version": "2.0.45",
"version": "2.0.46",
"type": "module",
"scripts": {
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
Expand All @@ -22,14 +22,14 @@
"intl:clean": "node src/helpers/intl-clean"
},
"dependencies": {
"@noble/curves": "^1.4.0",
"@radix-ui/react-avatar": "^1.0.2",
"@radixdlt/babylon-gateway-api-sdk": "^1.4.0",
"@radixdlt/connector-extension": "https://github.com/radixdlt/connector-extension#v1.3.6",
"@radixdlt/radix-connect-schemas": "^1.2.0",
"@radixdlt/babylon-gateway-api-sdk": "^1.6.1",
"@radixdlt/connector-extension": "https://github.com/radixdlt/connector-extension#v1.4.1",
"@radixdlt/radix-connect-webrtc": "^1.2.1",
"@radixdlt/radix-dapp-toolkit": "^1.4.3",
"@radixdlt/radix-engine-toolkit": "^1.0.3",
"@radixnameservice/rns-sdk": "^2.0.0",
"@radixdlt/radix-dapp-toolkit": "2.0.2",
"@radixdlt/radix-engine-toolkit": "^1.0.4",
"@radixnameservice/rns-sdk": "^2.0.2",
"@scure/bip32": "^1.3.2",
"async-mutex": "^0.4.0",
"bip39": "^3.1.0",
Expand Down
4 changes: 2 additions & 2 deletions apps/extension/src/browser/app/message-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const MessageClient = () => {

const onPortDisconnect = () => {
// eslint-disable-next-line no-console
if (port.error) console.error(`Disconnected due to an error: ${port.error.message}`)
if (port.error) console.error(`[APP]: Disconnected due to an error: ${port.error.message}`)
port = browser.runtime.connect({ name: PORT_NAME })
port.onDisconnect.addListener(onPortDisconnect)
port.onMessage.addListener(onPortMessage)
Expand All @@ -43,7 +43,7 @@ export const MessageClient = () => {

const sendMessage = async (
action: BackgroundMessageAction,
payload: BackgroundMessageTypes[keyof BackgroundMessageTypes] = {},
payload: BackgroundMessageTypes[keyof BackgroundMessageTypes],
) => {
const msg = newMessage(action, MessageSource.POPUP, MessageSource.BACKGROUND, payload)
const promise = new Promise<ResponseMessage>(resolve => {
Expand Down
4 changes: 2 additions & 2 deletions apps/extension/src/browser/app/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { WalletInteractionWithOrigin } from '@radixdlt/radix-connect-schemas'
import type { WalletInteraction } from '@radixdlt/radix-dapp-toolkit'

export enum MessageAction {
APP_PING = 'v1-app-ping',
APP_INTERACTION_CANCEL = 'v1-app-interaction-cancel',
}

export type WalletInteractionWithTabId = WalletInteractionWithOrigin & { fromTabId: number; senderURl: string }
export type WalletInteractionWithTabId = WalletInteraction & { fromTabId: number; senderURl: string }
2 changes: 1 addition & 1 deletion apps/extension/src/browser/background/message-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const MessageClient = (logger: AppLogger) => {
})

port.onDisconnect.addListener(() => {
if (port.error) console.error(`Disconnected due to an error: ${port.error.message}`)
if (port.error) console.error(`[BACKGROUND]: Disconnected due to an error: ${port.error.message}`)
})
}

Expand Down
70 changes: 68 additions & 2 deletions apps/extension/src/browser/background/message-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
messageSource as radixMessageSource,
} from '@radixdlt/connector-extension/src/chrome/messages/_types'
import { createMessage as createRadixMessage } from '@radixdlt/connector-extension/src/chrome/messages/create-message'
import type { WalletInteractionWithOrigin } from '@radixdlt/radix-connect-schemas'
import type { WalletInteraction } from '@radixdlt/radix-dapp-toolkit'
import type { PrivateKey } from '@radixdlt/radix-engine-toolkit'
import { Convert } from '@radixdlt/radix-engine-toolkit'
import browser from 'webextension-polyfill'
Expand All @@ -20,6 +20,7 @@ import { MessageAction as AppMessageAction } from '@src/browser/app/types'
import { newMessage } from '@src/browser/messages/message'
import { type Message, type MessageHandlers, MessageSource } from '@src/browser/messages/types'
import { Vault } from '@src/browser/vault/vault'
import { config } from '@src/config'
import type { PublicKeyJSON } from '@src/crypto/key_pair'
import { getPrivateKey, publicKeyToJSON } from '@src/crypto/key_pair'
import { getSecret as cryptoGetSecret, getCombineData } from '@src/crypto/secret'
Expand All @@ -28,6 +29,7 @@ import { signatureToJSON, signatureWithPublicKeyToJSON } from '@src/crypto/signa
import { saveInteractions } from '@src/radix/interaction'
import { type Data } from '@src/types/vault'

import { getExtensionTabsByUrl } from '../tabs'
import { MessageAction } from './types'

const vault = new Vault(globalThis.crypto)
Expand Down Expand Up @@ -210,11 +212,20 @@ async function handleRadixMessage(message: Message) {
radixMsg.messageEvent,
radixMsg.data,
)
case messageDiscriminator.focusLedgerTab: {
const [page] = await getExtensionTabsByUrl(config.popup.pages.ledger)
if (page?.id) {
return browser.tabs.update(page.id, { active: true })
}
return createRadixMessage.confirmationError(radixMessageSource.background, radixMsg.messageId, {
reason: 'failedToFocusLedgerTab',
})
}
case messageDiscriminator.walletResponse:
return radixMsg.data
case messageDiscriminator.dAppRequest: {
try {
const walletInteraction: WalletInteractionWithOrigin = radixMsg.data
const walletInteraction: WalletInteraction = radixMsg.data
const { interactionId, metadata, items } = walletInteraction
if (items) {
switch (items.discriminator) {
Expand Down Expand Up @@ -258,6 +269,61 @@ async function handleRadixMessage(message: Message) {
})
}
}
case messageDiscriminator.walletInteraction: {
try {
const { interactionId, metadata } = radixMsg.interaction.interaction
saveInteractions({
...radixMsg.interaction.interaction,
fromTabId: message.fromTabId,
senderURl: message.senderUrl,
} as WalletInteractionWithTabId)
.then(() => openAppPopup(`#/interaction/${interactionId}`))
// eslint-disable-next-line no-console
.catch(console.error)

return createRadixMessage.sendMessageEventToDapp(
radixMessageSource.contentScript,
messageLifeCycleEvent.receivedByExtension,
{ interactionId, metadata },
)
} catch (error) {
// eslint-disable-next-line no-console
console.error(`⚡️Z3US⚡️: background handleRadixMessage: ${radixMsg?.discriminator}`, radixMsg, error)
return createRadixMessage.confirmationError(radixMessageSource.contentScript, radixMsg.messageId, {
reason: 'failedToDetectWalletLink',
jsError: error,
})
}
}
case messageDiscriminator.cancelWalletInteraction: {
try {
const { interactionId, metadata } = radixMsg.interaction
browser.runtime
.sendMessage(
newMessage(
AppMessageAction.APP_INTERACTION_CANCEL,
MessageSource.BACKGROUND,
MessageSource.POPUP,
radixMsg.interaction,
),
)
// eslint-disable-next-line no-console
.catch(console.error)

return createRadixMessage.sendMessageEventToDapp(
radixMessageSource.contentScript,
messageLifeCycleEvent.receivedByExtension,
{ interactionId, metadata },
)
} catch (error) {
// eslint-disable-next-line no-console
console.error(`⚡️Z3US⚡️: background handleRadixMessage: ${radixMsg?.discriminator}`, radixMsg, error)
return createRadixMessage.confirmationError(radixMessageSource.contentScript, radixMsg.messageId, {
reason: 'failedToDetectWalletLink',
jsError: error,
})
}
}
default:
// eslint-disable-next-line no-console
console.error(`⚡️Z3US⚡️: background handleRadixMessage: ${radixMsg?.discriminator}`, radixMsg, message)
Expand Down
19 changes: 15 additions & 4 deletions apps/extension/src/browser/background/storage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { messageSource } from '@radixdlt/connector-extension/src/chrome/messages/_types'
import { createMessage } from '@radixdlt/connector-extension/src/chrome/messages/create-message'
import type { Storage } from 'webextension-polyfill'
import browser from 'webextension-polyfill'

export const handleStorageChange = async (changes: { [key: string]: Storage.StorageChange }) => {
if (changes?.connectionPassword) {
const radixMsg = createMessage.setConnectionPassword('background', changes?.connectionPassword?.newValue)
return browser.runtime.sendMessage(radixMsg)
export const handleStorageChange = async (changes: { [key: string]: Storage.StorageChange }, area: string) => {
if (changes.connections && area === 'local') {
const radixMsg = createMessage.setConnections(messageSource.background, changes.connections?.newValue || {})
await browser.runtime.sendMessage(radixMsg)
}

if (changes.options && area === 'local') {
const radixMsg = createMessage.setConnectorExtensionOptions(messageSource.background, changes.options.newValue)
await browser.runtime.sendMessage(radixMsg)
}

if (changes.sessionRouter && area === 'local') {
const radixMsg = createMessage.setSessionRouterData(changes.sessionRouter.newValue, messageSource.background)
await browser.runtime.sendMessage(radixMsg)
}
return undefined
}
48 changes: 32 additions & 16 deletions apps/extension/src/browser/content-script/messages-client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/* eslint-disable no-case-declarations */

/* eslint-disable no-console */
import type { Message as RadixMessage } from '@radixdlt/connector-extension/src/chrome/messages/_types'
import {
type Message as RadixMessage,
messageSource as radixMessageSource,
} from '@radixdlt/connector-extension/src/chrome/messages/_types'
import { createMessage as createRadixMessage } from '@radixdlt/connector-extension/src/chrome/messages/create-message'
import type { ExtensionInteraction, WalletInteractionWithOrigin } from '@radixdlt/radix-connect-schemas'
import type { ExtensionInteraction, WalletInteraction } from '@radixdlt/radix-dapp-toolkit'
import browser from 'webextension-polyfill'

import { DAPP_ORIGIN } from 'ui/src/constants/dapp'
Expand All @@ -12,7 +17,7 @@ import { PORT_NAME } from '@src/browser/messages/constants'
import { newMessage } from '@src/browser/messages/message'
import type { Message, ResponseMessage } from '@src/browser/messages/types'
import { MessageSource } from '@src/browser/messages/types'
import { addOriginToMetadata } from '@src/radix/metadata'
import { addOriginToCancelInteraction, addOriginToWalletInteraction } from '@src/radix/add-origin-to-wallet-interaction'

import timeout, { reason } from '../messages/timeout'
import { chromeDAppClient, logger, radixMessageHandler, sendRadixMessage } from './radix'
Expand Down Expand Up @@ -59,7 +64,7 @@ export const MessageClient = () => {
}

const onPortDisconnect = () => {
if (port.error) console.error(`Disconnected due to an error: ${port.error.message}`)
if (port.error) console.error(`[CONTENT]: Disconnected due to an error: ${port.error.message}`)
port = browser.runtime.connect({ name: PORT_NAME })
port.onDisconnect.addListener(onPortDisconnect)
port.onMessage.addListener(onPortMessage)
Expand Down Expand Up @@ -133,35 +138,46 @@ export const MessageClient = () => {
}
}

const handleWalletInteraction = async (walletInteraction: WalletInteractionWithOrigin) => {
const radixMsg = createRadixMessage.dAppRequest('contentScript', walletInteraction)
const handleWalletInteraction = async (walletInteraction: WalletInteraction) => {
const radixMsg = createRadixMessage.dAppRequest(radixMessageSource.contentScript, walletInteraction)
const enabled = await isHandledByRadix()
if (enabled) {
await browser.runtime.sendMessage(addOriginToMetadata(radixMsg))
} else {
await sendMessage(radixMsg)
return browser.runtime.sendMessage(radixMsg)
}
return sendMessage(radixMsg)
}

const handleExtensionInteraction = async (extensionInteraction: ExtensionInteraction) => {
switch (extensionInteraction.discriminator) {
case 'openPopup':
return openAppPopup('#/keystore/new')
case 'extensionStatus':
return checkConnectButtonStatus()
case 'cancelWalletInteraction':
const cancelWalletInteractionMsg = createRadixMessage.cancelWalletInteraction(
addOriginToCancelInteraction(extensionInteraction),
)
if (await isHandledByRadix()) {
await browser.runtime.sendMessage(createRadixMessage.openParingPopup())
} else {
await openAppPopup('#/keystore/new')
return browser.runtime.sendMessage(cancelWalletInteractionMsg)
}
break
case 'extensionStatus':
await checkConnectButtonStatus()
break
return sendMessage(cancelWalletInteractionMsg)
case 'walletInteraction':
const walletInteractionMsg = createRadixMessage.walletInteraction({
...extensionInteraction,
interaction: addOriginToWalletInteraction(extensionInteraction.interaction),
})
if (await isHandledByRadix()) {
return browser.runtime.sendMessage(walletInteractionMsg)
}
return sendMessage(walletInteractionMsg)
default:
logger.error({
reason: 'InvalidExtensionRequest',
interaction: extensionInteraction,
})
break
}
return undefined
}

chromeDAppClient.messageListener(handleWalletInteraction, handleExtensionInteraction)
Expand Down
12 changes: 6 additions & 6 deletions apps/extension/src/browser/content-script/storage.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { hasConnections } from '@radixdlt/connector-extension/src/chrome/helpers/get-connections'
import { createMessage as createRadixMessage } from '@radixdlt/connector-extension/src/chrome/messages/create-message'
import type { Storage } from 'webextension-polyfill'

import { sharedStore } from 'ui/src/store'

import { getConnectionPassword } from '@src/browser/vault/storage'

import { sendRadixMessageToDapp } from './radix'
import { isHandledByRadix } from './radix-connector'

export const checkConnectButtonStatus = async () => {
const enabled = await isHandledByRadix()
if (enabled) {
const connectionPassword = await getConnectionPassword()
sendRadixMessageToDapp(createRadixMessage.extensionStatus(!!connectionPassword))
hasConnections().map(has => sendRadixMessageToDapp(createRadixMessage.extensionStatus(has)))
} else {
await sharedStore.persist.rehydrate()
const sharedState = sharedStore.getState()
sendRadixMessageToDapp(createRadixMessage.extensionStatus(sharedState.keystores.length > 0))
}
}

export const onStorageChange = (changes: { [key: string]: Storage.StorageChange }) => {
if (changes.connectionPassword) checkConnectButtonStatus()
export const onStorageChange = (changes: { [key: string]: Storage.StorageChange }, area: string) => {
if (changes.connections && area === 'local') {
checkConnectButtonStatus()
}
}
5 changes: 2 additions & 3 deletions apps/extension/src/browser/inpage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import '@src/helpers/polyfills'

import { MessageAction as BackgroundMessageAction } from '@src/browser/background/types'
import { MessageClient } from '@src/browser/inpage/message-client'
import { Event , MessageAction as InPageMessageAction } from '@src/browser/inpage/types'
import { Event, MessageAction as InPageMessageAction } from '@src/browser/inpage/types'
import type { ResponseMessage } from '@src/browser/messages/types'
import { config } from '@src/config'
import '@src/helpers/polyfills'

declare global {
interface Window {
Expand Down
Loading
Loading