Skip to content

Commit

Permalink
fix: p2p connection failure
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaccoSordo committed Feb 6, 2025
1 parent 605a266 commit 84b93c7
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 101 deletions.
24 changes: 17 additions & 7 deletions packages/beacon-dapp/src/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ export class DAppClient extends Client {
.catch(async (storageError) => {
logger.error(storageError)
await this.resetInvalidState(false)
this.events.emit(BeaconEvent.INVALID_ACCOUNT_DEACTIVATED)
this.events.emit(BeaconEvent.GENERIC_ERROR, storageError.message)

return undefined
})

Expand Down Expand Up @@ -824,12 +825,11 @@ export class DAppClient extends Client {
p2pTransport
.connect()
.then()
.catch((err) => {
.catch(async (err) => {
logger.error(err)
if (err.message === 'The account is deactivated.') {
this.hideUI(['alert'])
abortHandler()
}
await this.hideUI(['alert']) // hide pairing alert
setTimeout(() => this.events.emit(BeaconEvent.GENERIC_ERROR, err.message), 1000)
abortHandler()
})
return p2pTransport.getPairingRequestInfo()
},
Expand Down Expand Up @@ -866,7 +866,17 @@ export class DAppClient extends Client {
private async resetInvalidState(emit: boolean = true) {
this.accountManager.removeAllAccounts()
this._activeAccount = ExposedPromise.resolve<AccountInfo | undefined>(undefined)
this.storage.set(StorageKey.ACTIVE_ACCOUNT, undefined)

Object.values(StorageKey)
.filter(
(key) =>
!key.includes('wc@2') &&
!key.includes('secret') &&
!key.includes('sdk_version') &&
!key.includes('user-id')
)
.forEach((key) => this.storage.delete(key))

emit && this.events.emit(BeaconEvent.INVALID_ACTIVE_ACCOUNT_STATE)
!emit && this.hideUI(['alert'])
await Promise.all([
Expand Down
190 changes: 102 additions & 88 deletions packages/beacon-dapp/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export enum BeaconEvent {
HIDE_UI = 'HIDE_UI',
INVALID_ACTIVE_ACCOUNT_STATE = 'INVALID_ACTIVE_ACCOUNT_STATE',
INVALID_ACCOUNT_DEACTIVATED = 'INVALID_ACCOUNT_DEACTIVATED',
GENERIC_ERROR = 'GENERIC_ERROR',
PAIR_INIT = 'PAIR_INIT',
PAIR_SUCCESS = 'PAIR_SUCCESS',
CHANNEL_CLOSED = 'CHANNEL_CLOSED',
Expand Down Expand Up @@ -196,8 +197,9 @@ export interface BeaconEventType {
[BeaconEvent.NO_PERMISSIONS]: undefined
[BeaconEvent.ACTIVE_ACCOUNT_SET]: AccountInfo
[BeaconEvent.ACTIVE_TRANSPORT_SET]: Transport
[BeaconEvent.INVALID_ACTIVE_ACCOUNT_STATE]: undefined
[BeaconEvent.INVALID_ACTIVE_ACCOUNT_STATE]: undefined
[BeaconEvent.INVALID_ACCOUNT_DEACTIVATED]: undefined
[BeaconEvent.GENERIC_ERROR]: string
[BeaconEvent.SHOW_PREPARE]: { walletInfo?: WalletInfo }
[BeaconEvent.HIDE_UI]: ('alert' | 'toast')[] | undefined
[BeaconEvent.PAIR_INIT]: {
Expand All @@ -211,9 +213,9 @@ export interface BeaconEventType {
featuredWallets?: string[]
}
[BeaconEvent.PAIR_SUCCESS]:
| ExtendedPostMessagePairingResponse
| ExtendedP2PPairingResponse
| ExtendedWalletConnectPairingResponse
| ExtendedPostMessagePairingResponse
| ExtendedP2PPairingResponse
| ExtendedWalletConnectPairingResponse
[BeaconEvent.CHANNEL_CLOSED]: string
[BeaconEvent.INTERNAL_ERROR]: { text: string; buttons?: AlertButton[] }
[BeaconEvent.UNKNOWN]: undefined
Expand Down Expand Up @@ -331,11 +333,23 @@ const showInvalidActiveAccountState = async (): Promise<void> => {
*/
const showInvalidAccountDeactivated = async (): Promise<void> => {
await openAlert({
title: 'Error',
title: 'Info',
body: `Your session has expired. Please pair with your wallet again.`
})
}

/**
* Show an "Invalid state" alert
*/
const showGenericErrorAlert = async (errorMessage: string): Promise<void> => {
await openAlert({
title: 'Error',
body: `${errorMessage}.<br />Please try again.<br />If this problem persists please reach out <a href="mailto:support@walletbeacon.io?subject=${encodeURIComponent(
'Beacon error'
)}&body=${encodeURIComponent(errorMessage)}">support@walletbeacon.io</a>`
})
}

/**
* Show an error toast
*
Expand Down Expand Up @@ -531,22 +545,22 @@ const showProofOfEventChallengeSuccessAlert = async (
state: 'finished',
actions: output.isAccepted
? [
{
text: `Payload hash: ${output.payloadHash}`,
actionText: 'Copy to clipboard',
actionCallback: async (): Promise<void> => {
navigator.clipboard.writeText(output.payloadHash).then(
() => {
logger.log('showSignSuccessAlert', 'Copying to clipboard was successful!')
},
(err) => {
logger.error('showSignSuccessAlert', 'Could not copy text to clipboard: ', err)
}
)
await closeToast()
{
text: `Payload hash: ${output.payloadHash}`,
actionText: 'Copy to clipboard',
actionCallback: async (): Promise<void> => {
navigator.clipboard.writeText(output.payloadHash).then(
() => {
logger.log('showSignSuccessAlert', 'Copying to clipboard was successful!')
},
(err) => {
logger.error('showSignSuccessAlert', 'Could not copy text to clipboard: ', err)
}
)
await closeToast()
}
}
}
]
]
: []
})
}
Expand All @@ -565,22 +579,22 @@ const showSimulatedProofOfEventChallengeSuccessAlert = async (
state: 'finished',
actions: !output.errorMessage
? [
{
text: 'Operation list',
actionText: 'Copy to clipboard',
actionCallback: async (): Promise<void> => {
navigator.clipboard.writeText(output.operationsList).then(
() => {
logger.log('showSignSuccessAlert', 'Copying to clipboard was successful!')
},
(err) => {
logger.error('showSignSuccessAlert', 'Could not copy text to clipboard: ', err)
}
)
await closeToast()
{
text: 'Operation list',
actionText: 'Copy to clipboard',
actionCallback: async (): Promise<void> => {
navigator.clipboard.writeText(output.operationsList).then(
() => {
logger.log('showSignSuccessAlert', 'Copying to clipboard was successful!')
},
(err) => {
logger.error('showSignSuccessAlert', 'Could not copy text to clipboard: ', err)
}
)
await closeToast()
}
}
}
]
]
: [{ text: 'Error message', actionText: output.errorMessage }]
})
}
Expand Down Expand Up @@ -769,6 +783,7 @@ export const defaultEventCallbacks: {
[BeaconEvent.ACTIVE_TRANSPORT_SET]: emptyHandler(),
[BeaconEvent.INVALID_ACTIVE_ACCOUNT_STATE]: showInvalidActiveAccountState,
[BeaconEvent.INVALID_ACCOUNT_DEACTIVATED]: showInvalidAccountDeactivated,
[BeaconEvent.GENERIC_ERROR]: showGenericErrorAlert,
[BeaconEvent.SHOW_PREPARE]: showPrepare,
[BeaconEvent.HIDE_UI]: hideUI,
[BeaconEvent.PAIR_INIT]: showPairAlert,
Expand All @@ -787,59 +802,58 @@ export class BeaconEventHandler {
private readonly callbackMap: {
[key in BeaconEvent]: BeaconEventHandlerFunction<any>[] // TODO: Fix type
} = {
[BeaconEvent.PERMISSION_REQUEST_SENT]: [defaultEventCallbacks.PERMISSION_REQUEST_SENT],
[BeaconEvent.PERMISSION_REQUEST_SUCCESS]: [defaultEventCallbacks.PERMISSION_REQUEST_SUCCESS],
[BeaconEvent.PERMISSION_REQUEST_ERROR]: [defaultEventCallbacks.PERMISSION_REQUEST_ERROR],
[BeaconEvent.PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT]: [
defaultEventCallbacks.PERMISSION_REQUEST_SENT
],
[BeaconEvent.PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS]: [
defaultEventCallbacks.PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS
],
[BeaconEvent.PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR]: [
defaultEventCallbacks.PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR
],
[BeaconEvent.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT]: [
defaultEventCallbacks.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT
],
[BeaconEvent.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS]: [
defaultEventCallbacks.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS
],
[BeaconEvent.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR]: [
defaultEventCallbacks.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR
],
[BeaconEvent.OPERATION_REQUEST_SENT]: [defaultEventCallbacks.OPERATION_REQUEST_SENT],
[BeaconEvent.OPERATION_REQUEST_SUCCESS]: [defaultEventCallbacks.OPERATION_REQUEST_SUCCESS],
[BeaconEvent.OPERATION_REQUEST_ERROR]: [defaultEventCallbacks.OPERATION_REQUEST_ERROR],
[BeaconEvent.SIGN_REQUEST_SENT]: [defaultEventCallbacks.SIGN_REQUEST_SENT],
[BeaconEvent.SIGN_REQUEST_SUCCESS]: [defaultEventCallbacks.SIGN_REQUEST_SUCCESS],
[BeaconEvent.SIGN_REQUEST_ERROR]: [defaultEventCallbacks.SIGN_REQUEST_ERROR],
// TODO: ENCRYPTION
// [BeaconEvent.ENCRYPT_REQUEST_SENT]: [defaultEventCallbacks.ENCRYPT_REQUEST_SENT],
// [BeaconEvent.ENCRYPT_REQUEST_SUCCESS]: [defaultEventCallbacks.ENCRYPT_REQUEST_SUCCESS],
// [BeaconEvent.ENCRYPT_REQUEST_ERROR]: [defaultEventCallbacks.ENCRYPT_REQUEST_ERROR],
[BeaconEvent.BROADCAST_REQUEST_SENT]: [defaultEventCallbacks.BROADCAST_REQUEST_SENT],
[BeaconEvent.BROADCAST_REQUEST_SUCCESS]: [defaultEventCallbacks.BROADCAST_REQUEST_SUCCESS],
[BeaconEvent.BROADCAST_REQUEST_ERROR]: [defaultEventCallbacks.BROADCAST_REQUEST_ERROR],
[BeaconEvent.ACKNOWLEDGE_RECEIVED]: [defaultEventCallbacks.ACKNOWLEDGE_RECEIVED],
[BeaconEvent.LOCAL_RATE_LIMIT_REACHED]: [defaultEventCallbacks.LOCAL_RATE_LIMIT_REACHED],
[BeaconEvent.NO_PERMISSIONS]: [defaultEventCallbacks.NO_PERMISSIONS],
[BeaconEvent.ACTIVE_ACCOUNT_SET]: [defaultEventCallbacks.ACTIVE_ACCOUNT_SET],
[BeaconEvent.ACTIVE_TRANSPORT_SET]: [defaultEventCallbacks.ACTIVE_TRANSPORT_SET],
[BeaconEvent.INVALID_ACTIVE_ACCOUNT_STATE]: [
defaultEventCallbacks.INVALID_ACTIVE_ACCOUNT_STATE
],
[BeaconEvent.INVALID_ACCOUNT_DEACTIVATED]: [
defaultEventCallbacks.INVALID_ACCOUNT_DEACTIVATED
],
[BeaconEvent.SHOW_PREPARE]: [defaultEventCallbacks.SHOW_PREPARE],
[BeaconEvent.HIDE_UI]: [defaultEventCallbacks.HIDE_UI],
[BeaconEvent.PAIR_INIT]: [defaultEventCallbacks.PAIR_INIT],
[BeaconEvent.PAIR_SUCCESS]: [defaultEventCallbacks.PAIR_SUCCESS],
[BeaconEvent.CHANNEL_CLOSED]: [defaultEventCallbacks.CHANNEL_CLOSED],
[BeaconEvent.INTERNAL_ERROR]: [defaultEventCallbacks.INTERNAL_ERROR],
[BeaconEvent.UNKNOWN]: [defaultEventCallbacks.UNKNOWN]
}
[BeaconEvent.PERMISSION_REQUEST_SENT]: [defaultEventCallbacks.PERMISSION_REQUEST_SENT],
[BeaconEvent.PERMISSION_REQUEST_SUCCESS]: [defaultEventCallbacks.PERMISSION_REQUEST_SUCCESS],
[BeaconEvent.PERMISSION_REQUEST_ERROR]: [defaultEventCallbacks.PERMISSION_REQUEST_ERROR],
[BeaconEvent.PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT]: [
defaultEventCallbacks.PERMISSION_REQUEST_SENT
],
[BeaconEvent.PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS]: [
defaultEventCallbacks.PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS
],
[BeaconEvent.PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR]: [
defaultEventCallbacks.PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR
],
[BeaconEvent.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT]: [
defaultEventCallbacks.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT
],
[BeaconEvent.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS]: [
defaultEventCallbacks.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS
],
[BeaconEvent.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR]: [
defaultEventCallbacks.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR
],
[BeaconEvent.OPERATION_REQUEST_SENT]: [defaultEventCallbacks.OPERATION_REQUEST_SENT],
[BeaconEvent.OPERATION_REQUEST_SUCCESS]: [defaultEventCallbacks.OPERATION_REQUEST_SUCCESS],
[BeaconEvent.OPERATION_REQUEST_ERROR]: [defaultEventCallbacks.OPERATION_REQUEST_ERROR],
[BeaconEvent.SIGN_REQUEST_SENT]: [defaultEventCallbacks.SIGN_REQUEST_SENT],
[BeaconEvent.SIGN_REQUEST_SUCCESS]: [defaultEventCallbacks.SIGN_REQUEST_SUCCESS],
[BeaconEvent.SIGN_REQUEST_ERROR]: [defaultEventCallbacks.SIGN_REQUEST_ERROR],
// TODO: ENCRYPTION
// [BeaconEvent.ENCRYPT_REQUEST_SENT]: [defaultEventCallbacks.ENCRYPT_REQUEST_SENT],
// [BeaconEvent.ENCRYPT_REQUEST_SUCCESS]: [defaultEventCallbacks.ENCRYPT_REQUEST_SUCCESS],
// [BeaconEvent.ENCRYPT_REQUEST_ERROR]: [defaultEventCallbacks.ENCRYPT_REQUEST_ERROR],
[BeaconEvent.BROADCAST_REQUEST_SENT]: [defaultEventCallbacks.BROADCAST_REQUEST_SENT],
[BeaconEvent.BROADCAST_REQUEST_SUCCESS]: [defaultEventCallbacks.BROADCAST_REQUEST_SUCCESS],
[BeaconEvent.BROADCAST_REQUEST_ERROR]: [defaultEventCallbacks.BROADCAST_REQUEST_ERROR],
[BeaconEvent.ACKNOWLEDGE_RECEIVED]: [defaultEventCallbacks.ACKNOWLEDGE_RECEIVED],
[BeaconEvent.LOCAL_RATE_LIMIT_REACHED]: [defaultEventCallbacks.LOCAL_RATE_LIMIT_REACHED],
[BeaconEvent.NO_PERMISSIONS]: [defaultEventCallbacks.NO_PERMISSIONS],
[BeaconEvent.ACTIVE_ACCOUNT_SET]: [defaultEventCallbacks.ACTIVE_ACCOUNT_SET],
[BeaconEvent.ACTIVE_TRANSPORT_SET]: [defaultEventCallbacks.ACTIVE_TRANSPORT_SET],
[BeaconEvent.INVALID_ACTIVE_ACCOUNT_STATE]: [
defaultEventCallbacks.INVALID_ACTIVE_ACCOUNT_STATE
],
[BeaconEvent.INVALID_ACCOUNT_DEACTIVATED]: [defaultEventCallbacks.INVALID_ACCOUNT_DEACTIVATED],
[BeaconEvent.GENERIC_ERROR]: [defaultEventCallbacks.GENERIC_ERROR],
[BeaconEvent.SHOW_PREPARE]: [defaultEventCallbacks.SHOW_PREPARE],
[BeaconEvent.HIDE_UI]: [defaultEventCallbacks.HIDE_UI],
[BeaconEvent.PAIR_INIT]: [defaultEventCallbacks.PAIR_INIT],
[BeaconEvent.PAIR_SUCCESS]: [defaultEventCallbacks.PAIR_SUCCESS],
[BeaconEvent.CHANNEL_CLOSED]: [defaultEventCallbacks.CHANNEL_CLOSED],
[BeaconEvent.INTERNAL_ERROR]: [defaultEventCallbacks.INTERNAL_ERROR],
[BeaconEvent.UNKNOWN]: [defaultEventCallbacks.UNKNOWN]
}
constructor(
eventsToOverride: {
[key in BeaconEvent]?: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,38 @@ const RESPONSE_WAIT_TIME_MS: number = 1000
const REGIONS_AND_SERVERS: NodeDistributions = {
[Regions.EUROPE_WEST]: [
'beacon-node-1.diamond.papers.tech',
'beacon-node-1.diamond.walletbeacon.io',
'beacon-node-1.sky.papers.tech',
'beacon-node-1.sky.walletbeacon.io',
'beacon-node-2.sky.papers.tech',
'beacon-node-2.sky.walletbeacon.io',
'beacon-node-1.hope.papers.tech',
'beacon-node-1.hope.walletbeacon.io',
'beacon-node-1.hope-2.papers.tech',
'beacon-node-1.hope-2.walletbeacon.io',
'beacon-node-1.hope-3.papers.tech',
'beacon-node-1.hope-3.walletbeacon.io',
'beacon-node-1.hope-4.papers.tech',
'beacon-node-1.hope-5.papers.tech'
'beacon-node-1.hope-4.walletbeacon.io',
'beacon-node-1.hope-5.papers.tech',
'beacon-node-1.hope-5.walletbeacon.io'
],
[Regions.NORTH_AMERICA_EAST]: ['beacon-node-1.beacon-server-1.papers.tech'],
[Regions.NORTH_AMERICA_WEST]: ['beacon-node-1.beacon-server-2.papers.tech'],
[Regions.ASIA_EAST]: ['beacon-node-1.beacon-server-3.papers.tech'],
[Regions.AUSTRALIA]: ['beacon-node-1.beacon-server-4.papers.tech']
[Regions.NORTH_AMERICA_EAST]: [
'beacon-node-1.beacon-server-1.papers.tech',
'beacon-node-1.beacon-server-1.walletbeacon.io'
],
[Regions.NORTH_AMERICA_WEST]: [
'beacon-node-1.beacon-server-2.papers.tech',
'beacon-node-1.beacon-server-2.walletbeacon.io'
],
[Regions.ASIA_EAST]: [
'beacon-node-1.beacon-server-3.papers.tech',
'beacon-node-1.beacon-server-3.walletbeacon.io'
],
[Regions.AUSTRALIA]: [
'beacon-node-1.beacon-server-4.papers.tech',
'beacon-node-1.beacon-server-4.walletbeacon.io'
]
}

interface BeaconInfoResponse {
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-ui/src/components/info/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const Info: Component<InfoProps> = (props: InfoProps) => {
</div>
)}
<h3 class="info-title">{props.title}</h3>
{props.description && <div class="info-description">{props.description}</div>}
{props.description && <div class="info-description" innerHTML={props.description} />}
{props.data && <pre class="info-data">{props.data}</pre>}
<div class="info-buttons">
<For each={props.buttons}>
Expand Down

0 comments on commit 84b93c7

Please sign in to comment.