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

chore(web connector): unify sendable schema with klesia #220

Merged
merged 2 commits into from
Nov 6, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/apps-extension-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
test_and_build:
name: "Build and test"
env:
VITE_APP_DEFAULT_NETWORK: Mainnet
VITE_APP_DEFAULT_NETWORK_ID: mina:mainnet
NODE_OPTIONS: "--max_old_space_size=4096"
runs-on: ubuntu-latest
steps:
Expand All @@ -51,7 +51,7 @@ jobs:
TURBO_API: "http://127.0.0.1:9080"
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: pallad
run: VITE_APP_DEFAULT_NETWORK=$VITE_APP_DEFAULT_NETWORK pnpm build:extension
run: VITE_APP_DEFAULT_NETWORK_ID=$VITE_APP_DEFAULT_NETWORK_ID pnpm build:extension
- name: Run unit tests
run: pnpm test:unit
- uses: actions/upload-artifact@v4
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/apps-extension-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
name: Run E2E tests
env:
VITE_APP_E2E: true
VITE_APP_DEFAULT_NETWORK: Devnet
VITE_APP_DEFAULT_NETWORK_ID: mina:devnet
NODE_OPTIONS: "--max_old_space_size=4096"
runs-on: ubuntu-latest
steps:
Expand All @@ -25,7 +25,7 @@ jobs:
TURBO_API: "http://127.0.0.1:9080"
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: pallad
run: VITE_APP_E2E=$VITE_APP_E2E VITE_APP_DEFAULT_NETWORK=$VITE_APP_DEFAULT_NETWORK pnpm build:extension
run: VITE_APP_E2E=$VITE_APP_E2E VITE_APP_DEFAULT_NETWORK_ID=$VITE_APP_DEFAULT_NETWORK_ID pnpm build:extension
- name: Install Chromium for Playwright
working-directory: apps/extension
run: pnpm test:e2e:install
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/packages-ui-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
pages: write
id-token: write
env:
VITE_APP_DEFAULT_NETWORK: Devnet
VITE_APP_DEFAULT_NETWORK_ID: mina:devnet
steps:
- uses: actions/checkout@v4
- name: Setup Pages
Expand Down
133 changes: 0 additions & 133 deletions apps/extension/e2e/provider-enable.spec.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test:e2e:ui": "playwright test --ui"
},
"dependencies": {
"@mina-js/providers": "https://pkg.pr.new/palladians/mina-js/@mina-js/providers@7fe60e8",
"@mina-js/providers": "https://pkg.pr.new/palladians/mina-js/@mina-js/providers@a51d3b4",
"@palladxyz/common": "workspace:*",
"@palladxyz/features": "workspace:*",
"@palladxyz/key-management": "workspace:*",
Expand Down
16 changes: 8 additions & 8 deletions apps/extension/src/background/handlers/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useVault } from "@palladxyz/vault"
import type { NetworkName } from "@palladxyz/vault"
import type { NetworkId } from "@palladxyz/vault"
import { createMinaProvider } from "@palladxyz/web-provider"
import { serializeError } from "serialize-error"
import type { Handler } from "./"
Expand All @@ -14,14 +14,14 @@ export const palladSwitchNetwork: Handler = async ({ data }) => {
try {
const provider = await createMinaProvider()
await useVault.persist.rehydrate()
const { switchNetwork, getChainId } = useVault.getState()
const network = data.network as NetworkName
const { switchNetwork, getNetworkId } = useVault.getState()
const network = data.networkId as NetworkId
await switchNetwork(network)
await useVault.persist.rehydrate()
const chainId = getChainId()
const networkId = getNetworkId()
provider.emit("pallad_event", {
data: {
chainId: chainId,
networkId,
},
type: "chainChanged",
})
Expand All @@ -34,11 +34,11 @@ export const palladConnected: Handler = async () => {
try {
const provider = await createMinaProvider()
await useVault.persist.rehydrate()
const { getChainId } = useVault.getState()
const chainId = getChainId()
const { getNetworkId } = useVault.getState()
const networkId = getNetworkId()
provider.emit("pallad_event", {
data: {
chainId: chainId,
networkId,
},
type: "connect",
})
Expand Down
25 changes: 18 additions & 7 deletions apps/extension/src/background/handlers/web-provider.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import {
AccountsRequestParamsSchema,
ChainIdRequestParamsSchema,
ChainInformationRequestParamsSchema,
CreateNullifierRequestParamsSchema,
GetBalanceRequestParamsSchema,
GetStateRequestParamsSchema,
NetworkIdRequestParamsSchema,
RequestAccountsRequestParamsSchema,
SendTransactionRequestParamsSchema,
SetStateRequestParamsSchema,
SignFieldsRequestParamsSchema,
SignRequestParamsSchema,
SignTransactionRequestParamsSchema,
SwitchChainRequestParamsSchema,
} from "@mina-js/providers"
import { createMinaProvider } from "@palladxyz/web-provider"
import { serializeError } from "serialize-error"
import { z } from "zod"
import type { Handler } from "."
import { createMinaProvider } from "../../../../../packages/web-provider/src"

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

Expand Down Expand Up @@ -50,8 +51,18 @@ export const minaRequestNetwork: Handler = async ({ data }) => {
}
}

export const minaSwitchChain = async () => {
return { error: serializeError(new Error("4200 - Unsupported Method")) }
export const minaSwitchChain: Handler = async ({ data }) => {
try {
const provider = await createMinaProvider()
const payload = SwitchChainRequestParamsSchema.parse({
method: "mina_switchChain",
params: data.params,
context: data.context,
})
return await provider.request(payload)
} catch (error: unknown) {
return { error: serializeError(error) }
}
}

export const minaGetState: Handler = async ({ data }) => {
Expand All @@ -68,11 +79,11 @@ export const minaGetState: Handler = async ({ data }) => {
}
}

export const minaChainId: Handler = async ({ data }) => {
export const minaNetworkId: Handler = async ({ data }) => {
try {
const provider = await createMinaProvider()
const payload = ChainIdRequestParamsSchema.parse({
method: "mina_chainId",
const payload = NetworkIdRequestParamsSchema.parse({
method: "mina_networkId",
context: data.context,
})
return await provider.request(payload)
Expand Down
4 changes: 2 additions & 2 deletions apps/extension/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { runtime, tabs } from "webextension-polyfill"
import {
minaAccounts,
minaAddChain,
minaChainId,
minaCreateNullifier,
minaGetBalance,
minaGetState,
minaNetworkId,
minaRequestAccounts,
minaRequestNetwork,
minaSendTransaction,
Expand All @@ -31,7 +31,7 @@ onMessage("mina_addChain", minaAddChain)
onMessage("mina_requestNetwork", minaRequestNetwork)
onMessage("mina_switchChain", minaSwitchChain)
onMessage("mina_getState", minaGetState)
onMessage("mina_chainId", minaChainId)
onMessage("mina_networkId", minaNetworkId)
onMessage("mina_accounts", minaAccounts)
onMessage("mina_requestAccounts", minaRequestAccounts)
onMessage("mina_sign", minaSign)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"format": "pnpm biome check --write .",
"format:unsafe": "pnpm biome check --write --unsafe .",
"f": "pnpm format",
"cleanup": "turbo run cleanup && rimraf node_modules",
"cleanup": "turbo run cleanup && rimraf node_modules .turbo",
"prepare": "husky install",
"preinstall": "npx only-allow pnpm"
},
Expand All @@ -28,7 +28,7 @@
"@tsconfig/strictest": "2.0.5",
"@turbo/gen": "2.0.9",
"@vitest/coverage-v8": "2.0.5",
"@mina-js/utils": "https://pkg.pr.new/palladians/mina-js/@mina-js/utils@7fe60e8",
"@mina-js/utils": "https://pkg.pr.new/palladians/mina-js/@mina-js/utils@a51d3b4",
"autoprefixer": "10.4.19",
"happy-dom": "14.12.3",
"husky": "9.1.4",
Expand Down
3 changes: 1 addition & 2 deletions packages/features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@palladxyz/pallad-core": "workspace:*",
"@palladxyz/vault": "workspace:*",
"@total-typescript/ts-reset": "0.5.1",
"@zxcvbn-ts/core": "^3.0.4",
"@zxcvbn-ts/core": "3.0.4",
"array-shuffle": "3.0.0",
"class-variance-authority": "0.7.0",
"clsx": "2.1.1",
Expand All @@ -62,7 +62,6 @@
"react-dom": "18.3.1",
"react-error-boundary": "4.0.13",
"react-hook-form": "7.52.1",
"react-mixpanel-browser": "4.1.0",
"react-qr-code": "2.0.15",
"react-router": "6.25.1",
"react-router-dom": "6.25.1",
Expand Down
8 changes: 4 additions & 4 deletions packages/features/src/common/hooks/use-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useAppStore } from "../store/app"
export const useAccount = () => {
const navigate = useNavigate()
const currentWallet = useVault((state) => state.getCurrentWallet())
const currentNetworkName = useVault((state) => state.currentNetworkName)
const currentNetworkId = useVault((state) => state.currentNetworkId)
const getAccountsInfo = useVault((state) => state.getAccountsInfo)
const restartWallet = useVault((state) => state.restartWallet)
const _syncWallet = useVault((state) => state._syncWallet)
Expand All @@ -20,7 +20,7 @@ export const useAccount = () => {
)
const fetchWallet = async () => {
await _syncWallet()
const accountInfo = getAccountsInfo(currentNetworkName, publicKey)
const accountInfo = getAccountsInfo(currentNetworkId, publicKey)
const chain = currentWallet.credential.credential?.chain
const props = getAccountProperties(
accountInfo.accountInfo,
Expand All @@ -30,7 +30,7 @@ export const useAccount = () => {
}
const publicKey = getPublicKey(currentWallet)
const swr = useSWR(
publicKey ? [publicKey, "account", currentNetworkName] : null,
publicKey ? [publicKey, "account", currentNetworkId] : null,
async () => await fetchWallet(),
{
refreshInterval: 30000,
Expand Down Expand Up @@ -73,7 +73,7 @@ export const useAccount = () => {
publicKey,
lockWallet,
restartCurrentWallet,
network: currentNetworkName,
networkId: currentNetworkId,
stakeDelegated,
}
}
7 changes: 4 additions & 3 deletions packages/features/src/common/hooks/use-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ export const useTransaction = ({ hash }: { hash: string }) => {
const _syncTransactions = useVault((state) => state._syncTransactions)
const getTransaction = useVault((state) => state.getTransaction)
const publicKey = getPublicKey(currentWallet)
const currentNetworkName = useVault((state) => state.currentNetworkName)
const networkId = useVault((state) => state.currentNetworkId)
const syncAndGetTransaction = async () => {
if (!providerConfig) return
await _syncTransactions(providerConfig, publicKey)
return getTransaction(currentNetworkName, publicKey, hash, "MINA") // TODO: remove hardcoded 'MINA'
return getTransaction(networkId, publicKey, hash, "MINA") // TODO: remove hardcoded 'MINA'
}
return useSWR(
publicKey ? ["transaction", hash, currentNetworkName] : null,
publicKey ? ["transaction", hash, networkId] : null,
async () => await syncAndGetTransaction(),
)
}
Loading