Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion packages/mask/background/services/helper/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export async function fetch(url: string) {
return res.blob()
}

export async function fetchJSON<T = unknown>(url: string): Promise<T> {
/** @deprecated */
export async function fetchJSON(url: string): Promise<unknown> {
const res = await globalThis.fetch(url)
return res.json()
}
6 changes: 6 additions & 0 deletions packages/mask/background/services/helper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { fetch, fetchJSON } from './fetch'
export { resolveTCOLink } from './short-link-resolver'
export { openPopupWindow, removePopupWindow } from './popup-opener'
export { __deprecated__getStorage, __deprecated__setStorage } from './deprecated-storage'
export { queryExtensionPermission, requestExtensionPermission } from './request-permission'
export { createPersonaPayload, queryExistedBinding, bindProof } from './nextID'
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { PopupRoutes } from '@masknet/shared-base'
import urlcat from 'urlcat'
import { currentPopupWindowId } from '../../../settings/settings'
import { isLocked } from '../../../plugins/Wallet/services'

export { __deprecated__getStorage, __deprecated__setStorage } from './storage'
export { resolveTCOLink } from '../../../../shared'
export { fetch, fetchJSON } from '../../../../background/services/helper/fetch'
export { requestExtensionPermission, queryExtensionPermission } from './extensionPermission'
export { createPersonaPayload, queryExistedBinding, bindProof } from './nextId'
export { fromHex, toBase64URL } from '@masknet/shared-base'
import { MaskMessages } from '../../../shared'

let currentPopupWindowId = 0
function isLocked() {
return new Promise<boolean>((resolve) => {
const off = MaskMessages.events.wallet_is_locked.on(([type, value]) => {
if (type === 'request') return
off()
resolve(value)
// in case something went wrong
setTimeout(() => resolve(false), 200)
})
MaskMessages.events.wallet_is_locked.sendToLocal(['request'])
})
}
export async function openPopupWindow(route?: PopupRoutes, params?: Record<string, any>) {
const windows = await browser.windows.getAll()
const popup = windows.find((win) => win && win.type === 'popup' && win.id === currentPopupWindowId.value)
const popup = windows.find((win) => win && win.type === 'popup' && win.id === currentPopupWindowId)

// Focus on the pop-up window if it already exists
if (popup) {
await browser.windows.update(currentPopupWindowId.value, { focused: true })
await browser.windows.update(currentPopupWindowId, { focused: true })
} else {
const locked = await isLocked()

Expand Down Expand Up @@ -56,10 +61,10 @@ export async function openPopupWindow(route?: PopupRoutes, params?: Record<strin

// update currentPopupWindowId and clean event
if (id) {
currentPopupWindowId.value = id
currentPopupWindowId = id
browser.windows.onRemoved.addListener(function listener(windowID: number) {
if (windowID === id) {
currentPopupWindowId.value = 0
currentPopupWindowId = 0
}
})

Expand All @@ -75,7 +80,7 @@ export async function openPopupWindow(route?: PopupRoutes, params?: Record<strin
}

export async function removePopupWindow() {
if (currentPopupWindowId.value) {
browser.windows.remove(currentPopupWindowId.value)
}
if (!currentPopupWindowId) return
browser.windows.remove(currentPopupWindowId)
currentPopupWindowId = 0
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { constructRequestPermissionURL } from '../../popups'

import { getPermissionRequestURL } from '../../../shared/definitions/routes'
export async function requestExtensionPermission(permission: browser.permissions.Permissions) {
if (await browser.permissions.contains(permission)) return true
try {
Expand All @@ -12,7 +11,7 @@ export async function requestExtensionPermission(permission: browser.permissions
height: 600,
width: 350,
type: 'popup',
url: constructRequestPermissionURL(permission),
url: getPermissionRequestURL(permission),
})
return new Promise((resolve) => {
browser.windows.onRemoved.addListener(function listener(windowID: number) {
Expand Down
1 change: 1 addition & 0 deletions packages/mask/background/services/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const message = new WebExtensionMessage<Record<string, any>>({ domain: 'services
// #region Setup services
const _service: Record<keyof Services, void> = {
Crypto: setup('Crypto', () => import('./crypto')),
Helper: setup('Helper', () => import('./helper')),
}
const _service_generator: Record<keyof GeneratorServices, void> = {}

Expand Down
2 changes: 2 additions & 0 deletions packages/mask/background/services/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type * as Crypto from './crypto'
import type * as Helper from './helper'
export type Services = {
Crypto: typeof Crypto
Helper: typeof Helper
}
export type GeneratorServices = {}
11 changes: 11 additions & 0 deletions packages/mask/shared/definitions/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PopupRoutes } from '@masknet/shared-base'
export function getPopupRouteURLWithNoParam(kind: PopupRoutes) {
return browser.runtime.getURL(`/popups.html#${kind}`)
}
export function getPermissionRequestURL(permission: browser.permissions.Permissions) {
const { origins = [], permissions = [] } = permission
const params = new URLSearchParams()
for (const each of origins) params.append('origins', each)
for (const each of permissions) params.append('permissions', each)
return `${getPopupRouteURLWithNoParam(PopupRoutes.RequestPermission)}?${params.toString()}`
}
1 change: 0 additions & 1 deletion packages/mask/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export * from './messages'
export * from './flags'
export { InMemoryStorages, PersistentStorages } from './kv-storage'
export * from './helpers/download'
export * from './helpers/resolve-t.co'
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PopupRoutes } from '@masknet/shared-base'
import { ChainId, getChainIdFromNetworkType, getChainRPC, NetworkType, ProviderType } from '@masknet/web3-shared-evm'
import { currentChainIdSettings } from '../../../../plugins/Wallet/settings'
import { getWallets, selectAccountPrepare } from '../../../../plugins/Wallet/services'
import { openPopupWindow } from '../../HelperService'
import { openPopupWindow } from '../../../../../background/services/helper'

// #region providers
const providerPool = new Map<string, HttpProvider>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { WalletRPC } from '../../../plugins/Wallet/messages'
import { INTERNAL_nativeSend, INTERNAL_send } from './send'
import { defer } from '@dimensiondev/kit'
import { hasNativeAPI, nativeAPI } from '../../../../shared/native-rpc'
import { openPopupWindow } from '../HelperService'
import { openPopupWindow } from '../../../../background/services/helper'
import Services from '../../service'
import { toHex } from 'web3-utils'
import { isLessThan } from '@masknet/web3-shared-base'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { MaskMessages } from '../../../utils'
import { PersonaIdentifier, fromBase64URL, PopupRoutes } from '@masknet/shared-base'
import { queryPersonasWithPrivateKey } from '../../../../background/database/persona/db'
import { openPopupWindow } from '../HelperService'
import { openPopupWindow } from '../../../../background/services/helper'
import { delay } from '@dimensiondev/kit'
export interface SignRequest {
/** Use that who to sign this message. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { attachProfileDB, LinkedProfileDetails } from '../../../background/datab
import { deriveLocalKeyFromECDHKey } from '../../utils/mnemonic-code/localKeyGenerate'
import type { PersonaIdentifier, ProfileIdentifier, AESJsonWebKey } from '@masknet/shared-base'
import { BackupOptions, generateBackupJSON } from './WelcomeServices/generateBackupJSON'
import { requestExtensionPermission } from './HelperService/extensionPermission'
import { requestExtensionPermission, openPopupWindow } from './../../../background/services/helper'
import { saveFileFromBuffer } from '../../../shared'
import {
BackupJSONFileLatest,
Expand All @@ -18,7 +18,6 @@ import { assertEnvironment, Environment } from '@dimensiondev/holoflows-kit'
import { convertBackupFileToObject, extraPermissions, fixBackupFilePermission } from '../../utils'
import { v4 as uuid } from 'uuid'
import { getUnconfirmedBackup, restoreBackup, setUnconfirmedBackup } from './WelcomeServices/restoreBackup'
import { openPopupWindow } from './HelperService'
import formatDateTime from 'date-fns/format'

export { generateBackupJSON, generateBackupPreviewInfo } from './WelcomeServices/generateBackupJSON'
Expand Down
10 changes: 0 additions & 10 deletions packages/mask/src/extension/popups/RequestPermission/utils.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PopupRoutes } from '@masknet/shared-base'
import { getRouteURLWithNoParam } from '../utils'
import { getPopupRouteURLWithNoParam } from '..'
import type { ThirdPartyPluginPermission } from '../../background-script/ThirdPartyPlugin/types'

export function constructThirdPartyRequestPermissionURL(
Expand All @@ -9,5 +9,5 @@ export function constructThirdPartyRequestPermissionURL(
const params = new URLSearchParams()
params.set('plugin', pluginManifestURL)
for (const x of permissions) params.append('permission', String(x))
return getRouteURLWithNoParam(PopupRoutes.ThirdPartyRequestPermission) + '?' + params.toString()
return getPopupRouteURLWithNoParam(PopupRoutes.ThirdPartyRequestPermission) + '?' + params.toString()
}
8 changes: 3 additions & 5 deletions packages/mask/src/extension/popups/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type { MaskSDK_SNS_ContextIdentifier } from '../../plugins/External/sns-context'
import { PopupRoutes } from '@masknet/shared-base'

import { getRouteURLWithNoParam } from './utils'
import { getPopupRouteURLWithNoParam } from '../../../shared/definitions/routes'

export function PermissionAwareRedirectOf(url: string, context: MaskSDK_SNS_ContextIdentifier) {
return (
getRouteURLWithNoParam(PopupRoutes.PermissionAwareRedirect) +
getPopupRouteURLWithNoParam(PopupRoutes.PermissionAwareRedirect) +
`?url=${encodeURIComponent(url)}&context=${context}`
)
}
export { constructRequestPermissionURL } from './RequestPermission/utils'
export { getRouteURLWithNoParam } from './utils'
export { getPermissionRequestURL, getPopupRouteURLWithNoParam } from '../../../shared/definitions/routes'
4 changes: 0 additions & 4 deletions packages/mask/src/extension/popups/utils/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/mask/src/extension/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const Services = {
Crypto: add(() => import('./background-script/CryptoService'), 'Crypto'),
Identity: add(() => import('./background-script/IdentityService'), 'Identity'),
Welcome: add(() => import('./background-script/WelcomeService'), 'Welcome'),
Helper: add(() => import('./background-script/HelperService'), 'Helper'),
Helper: add(() => import('../../background/services/helper'), 'Helper'),
Ethereum: add(() => import('./background-script/EthereumService'), 'Ethereum'),
SocialNetwork: add(() => import('./background-script/SocialNetworkService'), 'SocialNetwork'),
Settings: add(() => import('./background-script/SettingsService'), 'Settings'),
Expand All @@ -47,7 +47,7 @@ if (process.env.manifest === '2' && import.meta.webpackHot && isEnvironment(Envi
'./background-script/CryptoService',
'./background-script/IdentityService',
'./background-script/WelcomeService',
'./background-script/HelperService',
'../../background/services/helper',
'./background-script/EthereumService',
'./background-script/SettingsService',
'./background-script/ThirdPartyPlugin',
Expand Down
3 changes: 1 addition & 2 deletions packages/mask/src/plugins/Profile/services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import urlcat from 'urlcat'
import { fetchJSON } from '../../extension/background-script/HelperService'

interface NameInfo {
rnsName: string
Expand All @@ -10,6 +9,6 @@ interface NameInfo {
export async function getRSS3AddressById(id: string) {
if (!id) return ''
const url = urlcat('https://rss3.domains/name/:id', { id })
const rsp = await fetchJSON<NameInfo>(url)
const rsp = (await (await fetch(url)).json()) as NameInfo
return rsp.address
}
13 changes: 12 additions & 1 deletion packages/mask/src/plugins/Wallet/Worker/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import type { Plugin } from '@masknet/plugin-infra'
import { base } from '@masknet/plugin-wallet'
import { MaskMessages } from '../../../utils'
import '../messages'
import { isLocked } from '../services'

const worker: Plugin.Worker.Definition = {
...base,
init(signal) {},
init(signal) {
MaskMessages.events.wallet_is_locked.on(
async ([type]) => {
if (type === 'request') {
MaskMessages.events.wallet_is_locked.sendToLocal(['response', await isLocked()])
}
},
{ signal },
)
},
}
export default worker
5 changes: 1 addition & 4 deletions packages/mask/src/plugins/Wallet/services/cloud.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
__deprecated__getStorage,
__deprecated__setStorage,
} from '../../../extension/background-script/HelperService/storage'
import { __deprecated__getStorage, __deprecated__setStorage } from '../../../../background/services/helper'

const HOST_MAP = {
production: 'https://backup.mask.io/api',
Expand Down
2 changes: 1 addition & 1 deletion packages/mask/src/plugins/Wallet/services/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { WalletMessages } from '@masknet/plugin-wallet'
import type { JsonRpcPayload } from 'web3-core-helpers'
import { createTransaction } from '../../../../background/database/utils/openDB'
import { createWalletDBAccess } from '../database/Wallet.db'
import { openPopupWindow } from '../../../extension/background-script/HelperService'
import { openPopupWindow } from '../../../../background/services/helper'

const MAX_UNCONFIRMED_REQUESTS_SIZE = 1
const MAIN_RECORD_ID = '0'
Expand Down
4 changes: 0 additions & 4 deletions packages/mask/src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ export const currentPersonaIdentifier = createGlobalSettings<string>('currentPer
primary: () => 'DO NOT DISPLAY IT IN UI',
})

export const currentPopupWindowId = createGlobalSettings<number>('currentPopupWindowId', 0, {
primary: () => 'DO NOT DISPLAY IT IN UI',
})

try {
// Migrate language settings
const lng: string = languageSettings.value
Expand Down
2 changes: 2 additions & 0 deletions packages/shared-base/src/Messages/Mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export interface MaskEvents extends MaskSettingsEvents, MaskMobileOnlyEvents, Ma
maskSDKHotModuleReload: void
__kv_backend_persistent__: [string, unknown]
__kv_backend_in_memory__: [string, unknown]
/** @deprecated do not use it in new code. */
wallet_is_locked: ['request'] | ['response', boolean]
}

export interface UpdateEvent<Data> {
Expand Down