Skip to content

Commit

Permalink
chore(web connector): clean up services (#219)
Browse files Browse the repository at this point in the history
* chore(web connector): clean up services

* fix(web connector): refactor test suites

* fix(wallet): fix ui tx submission

* fix(wallet): refactor tx submission
  • Loading branch information
mrcnk authored Oct 30, 2024
1 parent bf49ce1 commit a3ef42b
Show file tree
Hide file tree
Showing 55 changed files with 800 additions and 1,365 deletions.
5 changes: 3 additions & 2 deletions apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
"test:e2e:ui": "playwright test --ui"
},
"dependencies": {
"@mina-js/providers": "https://pkg.pr.new/palladians/mina-js/@mina-js/providers@7fe60e8",
"@palladxyz/common": "workspace:*",
"@palladxyz/features": "workspace:*",
"@palladxyz/key-management": "workspace:*",
"@palladxyz/web-provider": "workspace:*",
"@palladxyz/vault": "workspace:*",
"@palladxyz/web-provider": "workspace:*",
"@plasmohq/messaging": "0.6.2",
"buffer": "6.0.3",
"p-debounce": "4.0.0",
"mitt": "3.0.1",
"next-themes": "0.3.0",
"p-debounce": "4.0.0",
"react": "18.3.1",
"react-dom": "18.3.1",
"serialize-error": "11.0.3",
Expand Down
10 changes: 5 additions & 5 deletions apps/extension/src/background/handlers/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useVault } from "@palladxyz/vault"
import type { NetworkName } from "@palladxyz/vault"
import { MinaProvider } from "@palladxyz/web-provider"
import { createMinaProvider } from "@palladxyz/web-provider"
import { serializeError } from "serialize-error"
import type { Handler } from "./"

Expand All @@ -12,13 +12,13 @@ export const palladSidePanel: Handler = async ({ sender }) => {

export const palladSwitchNetwork: Handler = async ({ data }) => {
try {
const provider = await MinaProvider.getInstance()
const provider = await createMinaProvider()
await useVault.persist.rehydrate()
const { switchNetwork, getChainId } = useVault.getState()
const network = data.network as NetworkName
await switchNetwork(network)
await useVault.persist.rehydrate()
const chainId = await getChainId()
const chainId = getChainId()
provider.emit("pallad_event", {
data: {
chainId: chainId,
Expand All @@ -32,10 +32,10 @@ export const palladSwitchNetwork: Handler = async ({ data }) => {

export const palladConnected: Handler = async () => {
try {
const provider = await MinaProvider.getInstance()
const provider = await createMinaProvider()
await useVault.persist.rehydrate()
const { getChainId } = useVault.getState()
const chainId = await getChainId()
const chainId = getChainId()
provider.emit("pallad_event", {
data: {
chainId: chainId,
Expand Down
137 changes: 74 additions & 63 deletions apps/extension/src/background/handlers/web-provider.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import {
AccountsRequestParamsSchema,
ChainIdRequestParamsSchema,
ChainInformationRequestParamsSchema,
CreateNullifierRequestParamsSchema,
GetBalanceRequestParamsSchema,
GetStateRequestParamsSchema,
RequestAccountsRequestParamsSchema,
SendTransactionRequestParamsSchema,
SetStateRequestParamsSchema,
SignFieldsRequestParamsSchema,
SignRequestParamsSchema,
SignTransactionRequestParamsSchema,
} from "@mina-js/providers"
import { serializeError } from "serialize-error"
import { z } from "zod"
import type { Handler } from "."
import {
MinaProvider,
Validation,
} from "../../../../../packages/web-provider/src"
import { createMinaProvider } from "../../../../../packages/web-provider/src"

export const OriginSchema = z.string().url()

export const minaSetState: Handler = async ({ data }) => {
try {
const provider = await MinaProvider.getInstance()
const params = Validation.setStateRequestSchema.parse(data)
return await provider.request({
const provider = await createMinaProvider()
const payload = SetStateRequestParamsSchema.parse({
method: "mina_setState",
params,
params: data.params,
context: data.context,
})
return await provider.request(payload)
} catch (error: unknown) {
return { error: serializeError(error) }
}
Expand All @@ -24,12 +39,12 @@ export const minaAddChain = async () => {

export const minaRequestNetwork: Handler = async ({ data }) => {
try {
const provider = await MinaProvider.getInstance()
const params = Validation.requestSchema.parse(data)
return await provider.request({
method: "mina_requestNetwork",
params,
const provider = await createMinaProvider()
const payload = ChainInformationRequestParamsSchema.parse({
method: "mina_chainInformation",
context: data.context,
})
return await provider.request(payload)
} catch (error: unknown) {
return { error: serializeError(error) }
}
Expand All @@ -41,139 +56,135 @@ export const minaSwitchChain = async () => {

export const minaGetState: Handler = async ({ data }) => {
try {
const provider = await MinaProvider.getInstance()
const params = Validation.getStateRequestSchema.parse(data)
return await provider.request({
const provider = await createMinaProvider()
const payload = GetStateRequestParamsSchema.parse({
method: "mina_getState",
params,
params: data.params,
context: data.context,
})
return await provider.request(payload)
} catch (error: unknown) {
return { error: serializeError(error) }
}
}

export const minaChainId: Handler = async ({ data }) => {
try {
const provider = await MinaProvider.getInstance()
const params = Validation.requestSchema.parse(data)
return await provider.request({
const provider = await createMinaProvider()
const payload = ChainIdRequestParamsSchema.parse({
method: "mina_chainId",
params,
context: data.context,
})
return await provider.request(payload)
} catch (error: unknown) {
return { error: serializeError(error) }
}
}

export const minaAccounts: Handler = async ({ data }) => {
try {
const provider = await MinaProvider.getInstance()
const params = Validation.requestSchema.parse(data)
return await provider.request({
const provider = await createMinaProvider()
const payload = AccountsRequestParamsSchema.parse({
method: "mina_accounts",
params,
context: data.context,
})
return await provider.request(payload)
} catch (error: unknown) {
return { error: serializeError(error) }
}
}

export const minaRequestAccounts: Handler = async ({ data }) => {
try {
const provider = await MinaProvider.getInstance()
const params = Validation.requestSchema.parse(data)
return await provider.request({
const provider = await createMinaProvider()
const payload = RequestAccountsRequestParamsSchema.parse({
method: "mina_requestAccounts",
params,
context: data.context,
})
return await provider.request(payload)
} catch (error: unknown) {
return { error: serializeError(error) }
}
}

export const minaSign: Handler = async ({ data }) => {
try {
const provider = await MinaProvider.getInstance()
const params = Validation.signMessageRequestSchema.parse(data)
return await provider.request({
const provider = await createMinaProvider()
const payload = SignRequestParamsSchema.parse({
method: "mina_sign",
params,
params: data.params,
context: data.context,
})
return await provider.request(payload)
} catch (error: unknown) {
return { error: serializeError(error) }
}
}

export const minaSignFields: Handler = async ({ data }) => {
try {
const provider = await MinaProvider.getInstance()
const params = Validation.signFieldsRequestSchema.parse(data)
return await provider.request({
const provider = await createMinaProvider()
const payload = SignFieldsRequestParamsSchema.parse({
method: "mina_signFields",
params,
params: data.params,
context: data.context,
})
return await provider.request(payload)
} catch (error: unknown) {
return { error: serializeError(error) }
}
}

export const minaSignTransaction: Handler = async ({ data }) => {
try {
const provider = await MinaProvider.getInstance()
const params = Validation.signTransactionRequestSchema.parse(data)
return await provider.request({
const provider = await createMinaProvider()
const payload = SignTransactionRequestParamsSchema.parse({
method: "mina_signTransaction",
params,
params: data.params,
context: data.context,
})
return await provider.request(payload)
} catch (error: unknown) {
return { error: serializeError(error) }
}
}

export const minaGetBalance: Handler = async ({ data }) => {
try {
const provider = await MinaProvider.getInstance()
const params = Validation.requestSchema.parse(data)
return await provider.request({
const provider = await createMinaProvider()
const payload = GetBalanceRequestParamsSchema.parse({
method: "mina_getBalance",
params,
context: data.context,
})
return await provider.request(payload)
} catch (error: unknown) {
return { error: serializeError(error) }
}
}

export const minaCreateNullifier: Handler = async ({ data }) => {
try {
const provider = await MinaProvider.getInstance()
const params = Validation.createNullifierRequestSchema.parse(data)
return await provider.request({
const provider = await createMinaProvider()
const payload = CreateNullifierRequestParamsSchema.parse({
method: "mina_createNullifier",
params,
params: data.params,
context: data.context,
})
return await provider.request(payload)
} catch (error: unknown) {
return { error: serializeError(error) }
}
}

export const minaSendTransaction: Handler = async ({ data }) => {
try {
const provider = await MinaProvider.getInstance()
const params = Validation.sendTransactionRequestSchema.parse(data)
return await provider.request({
const provider = await createMinaProvider()
const payload = SendTransactionRequestParamsSchema.parse({
method: "mina_sendTransaction",
params,
params: data.params,
context: data.context,
})
} catch (error: unknown) {
return { error: serializeError(error) }
}
}

export const palladIsConnected: Handler = async ({ data }) => {
try {
const provider = await MinaProvider.getInstance()
const { origin } = Validation.requestSchema.parse(data)
return await provider.isConnected({ origin })
return await provider.request(payload)
} catch (error: unknown) {
return { error: serializeError(error) }
}
Expand Down
8 changes: 3 additions & 5 deletions apps/extension/src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MinaProvider } from "@palladxyz/web-provider"
import { createMinaProvider } from "@palladxyz/web-provider"
import { onMessage, sendMessage } from "webext-bridge/background"
import { runtime, tabs } from "webextension-polyfill"
import {
Expand All @@ -17,7 +17,6 @@ import {
minaSignTransaction,
minaSwitchChain,
palladConnected,
palladIsConnected,
palladSidePanel,
palladSwitchNetwork,
} from "./handlers"
Expand Down Expand Up @@ -45,7 +44,6 @@ onMessage("mina_sendTransaction", minaSendTransaction)
/**
* Wallet handlers
*/
onMessage("pallad_isConnected", palladIsConnected)
onMessage("pallad_switchNetwork", palladSwitchNetwork)
onMessage("pallad_connected", palladConnected)
onMessage("pallad_sidePanel", palladSidePanel)
Expand All @@ -64,13 +62,13 @@ runtime.onConnect.addListener(async (port) => {
}
})
runtime.onInstalled.addListener(async ({ reason }) => {
const provider = await MinaProvider.getInstance()
const provider = await createMinaProvider()
await chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true })
if (reason === "install") {
if (!E2E_TESTING)
await tabs.create({ url: "https://get.pallad.co/welcome" })
}
provider.on("pallad_event", async (data) => {
provider.on("pallad_event" as never, async (data: any) => {
const { permissions } = await chrome.storage.local.get("permissions")
const urls = Object.entries(permissions)
.filter(([_, allowed]) => allowed === "ALLOWED")
Expand Down
8 changes: 4 additions & 4 deletions apps/extension/src/inject/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ const inject = () => {
try {
const result: any = await sendMessage(
data.method,
{ ...data.payload, origin },
{ params: data.params, context: { origin } },
"background",
)
if (result?.error) {
response = { jsonrpc: "1.0", error: deserializeError(result.error) }
response = { jsonrpc: "2.0", error: deserializeError(result.error) }
} else {
response = { jsonrpc: "1.0", result }
response = { jsonrpc: "2.0", result }
}
return responseChannel.postMessage({
response,
})
} catch (error) {
response = { jsonrpc: "1.0", error }
response = { jsonrpc: "2.0", error }
return responseChannel.postMessage({
response,
})
Expand Down
5 changes: 4 additions & 1 deletion apps/extension/src/inject/rpc/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export const provider = {
request: async ({ method, params }: { method: string; params: any }) =>
await debouncedCall({
method,
payload: { ...params, origin: window.location.origin },
params,
context: {
origin: window.location.origin,
},
}),
isPallad: true,
on: _events.on,
Expand Down
Loading

0 comments on commit a3ef42b

Please sign in to comment.