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

feat(extension): add sandbox for running smart contracts #230

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 13 additions & 0 deletions apps/extension/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export const manifest: chrome.runtime.ManifestV3 = {
version: packageJson.version,
action: { default_title: "Click to open panel" },
side_panel: { default_path: "index.html" },
sandbox: {
pages: ["sandbox.html"],
},
permissions: ["storage", "activeTab", "background", "sidePanel"],
background: {
service_worker: "src/background/index.ts",
Expand Down Expand Up @@ -48,4 +51,14 @@ export const manifest: chrome.runtime.ManifestV3 = {
description: "Open the Pallad extension",
},
},
content_security_policy: {
sandbox:
"sandbox allow-scripts allow-forms allow-popups allow-modals; script-src 'self' 'unsafe-eval' 'wasm-unsafe-eval'; worker-src blob: 'self'; child-src blob: 'self'",
},
cross_origin_embedder_policy: {
value: "require-corp",
},
cross_origin_opener_policy: {
value: "same-origin",
},
}
33 changes: 9 additions & 24 deletions apps/extension/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@palladxyz/extension",
"name": "@palladco/extension",
"version": "0.6.2",
"type": "module",
"scripts": {
Expand All @@ -16,29 +16,24 @@
},
"dependencies": {
"@mina-js/providers": "https://pkg.pr.new/palladians/mina-js/@mina-js/providers@a51d3b4",
"@palladxyz/common": "workspace:*",
"@palladxyz/features": "workspace:*",
"@palladxyz/key-management": "workspace:*",
"@palladxyz/vault": "workspace:*",
"@palladxyz/web-provider": "workspace:*",
"@plasmohq/messaging": "0.6.2",
"buffer": "6.0.3",
"@palladco/common": "workspace:*",
"@palladco/contracts": "workspace:*",
"@palladco/features": "workspace:*",
"@palladco/key-management": "workspace:*",
"@palladco/vault": "workspace:*",
"@palladco/web-provider": "workspace:*",
"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",
"tailwindcss-animate": "1.0.7",
"vite-plugin-node-stdlib-browser": "0.2.1",
"ts-pattern": "^5.5.0",
"webext-bridge": "6.0.1",
"webextension-polyfill": "0.12.0",
"zod": "3.23.8"
},
"devDependencies": {
"@esbuild-plugins/node-globals-polyfill": "0.2.3",
"@esbuild-plugins/node-modules-polyfill": "0.2.2",
"@originjs/vite-plugin-commonjs": "1.0.3",
"@playwright/test": "1.45.3",
"@total-typescript/ts-reset": "0.5.1",
"@tsconfig/vite-react": "3.0.2",
Expand All @@ -47,20 +42,10 @@
"@types/react-dom": "18.3.0",
"@types/webextension-polyfill": "0.10.7",
"@vitejs/plugin-react-swc": "3.7.0",
"dedent": "1.5.3",
"path": "0.12.7",
"rollup-plugin-node-polyfills": "0.2.1",
"rollup-plugin-polyfill-node": "0.13.0",
"type-fest": "4.23.0",
"vite": "5.3.5",
"vite-plugin-commonjs": "0.10.1",
"vite-plugin-node-polyfills": "0.17.0",
"vite-plugin-require-transform": "1.0.21",
"vite-plugin-svgr": "4.2.0",
"vite-plugin-top-level-await": "1.4.2",
"vite-plugin-wasm": "3.3.0",
"vite-plugin-web-extension": "4.1.6",
"web-ext": "8.2.0",
"write-json-file": "6.0.0"
"web-ext": "8.2.0"
}
}
2 changes: 1 addition & 1 deletion apps/extension/postcss.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import postcssConfig from "@palladxyz/features/postcss.config.mjs"
import postcssConfig from "@palladco/features/postcss.config.mjs"

export default postcssConfig
1 change: 1 addition & 0 deletions apps/extension/prompt.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</head>

<body>
<iframe id="o1sandbox" title="Sandbox" src="/sandbox.html" allow="cross-origin-isolated" sandbox="allow-scripts" width="0" height="0"></iframe>
<div id="root"></div>
<script type="module" src="/src/prompt.tsx"></script>
</body>
Expand Down
13 changes: 13 additions & 0 deletions apps/extension/sandbox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pallad</title>
</head>

<body>
<div id="root"></div>
<script type="module" src="/src/sandbox/index.ts"></script>
</body>
</html>
4 changes: 2 additions & 2 deletions apps/extension/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "@palladxyz/features/dist/index.css"
import "@palladco/features/dist/index.css"
import "./assets/app.css"

import { Router } from "@palladxyz/features"
import { Router } from "@palladco/features"
import { ThemeProvider } from "next-themes"
import { useEffect } from "react"

Expand Down
6 changes: 3 additions & 3 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 { NetworkId } from "@palladxyz/vault"
import { createMinaProvider } from "@palladxyz/web-provider"
import { useVault } from "@palladco/vault"
import type { NetworkId } from "@palladco/vault"
import { createMinaProvider } from "@palladco/web-provider"
import { serializeError } from "serialize-error"
import type { Handler } from "./"

Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/background/handlers/web-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
SignTransactionRequestParamsSchema,
SwitchChainRequestParamsSchema,
} from "@mina-js/providers"
import { createMinaProvider } from "@palladxyz/web-provider"
import { createMinaProvider } from "@palladco/web-provider"
import { serializeError } from "serialize-error"
import { z } from "zod"
import type { Handler } from "."
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createMinaProvider } from "@palladxyz/web-provider"
import { createMinaProvider } from "@palladco/web-provider"
import { onMessage, sendMessage } from "webext-bridge/background"
import { runtime, tabs } from "webextension-polyfill"
import {
Expand Down
4 changes: 2 additions & 2 deletions apps/extension/src/prompt.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "@palladxyz/features/dist/index.css"
import "@palladco/features/dist/index.css"
import "@total-typescript/ts-reset"

import { WebConnectorRoute } from "@palladxyz/features"
import { WebConnectorRoute } from "@palladco/features"
import { ThemeProvider } from "next-themes"
import React from "react"
import ReactDOM from "react-dom/client"
Expand Down
25 changes: 25 additions & 0 deletions apps/extension/src/sandbox/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Add } from "@palladco/contracts"
import { match } from "ts-pattern"
import { z } from "zod"

const EventTypeSchema = z.enum(["run"])
const ContractTypeSchema = z.enum(["add"])

window.addEventListener("message", async (event) => {
// Can also read: payload, form
console.log(event.data)
console.log(event.data.userInput)
const type = EventTypeSchema.parse(event.data.type)
const contract = ContractTypeSchema.parse(event.data.contract)
return match(type)
.with("run", async () => {
return match(contract).with("add", async () => {
await Add.compile()
window.parent.postMessage(
{ type: "compiled", result: "test", userInput: event.data.userInput },
"*",
)
})
})
.exhaustive()
})
2 changes: 1 addition & 1 deletion apps/extension/tailwind.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import tailwindConfig from "@palladxyz/features/tailwind.config.mjs"
import tailwindConfig from "@palladco/features/tailwind.config.mjs"

export default {
content: [
Expand Down
6 changes: 6 additions & 0 deletions apps/extension/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ export default defineConfig(() => {
chunkSizeWarningLimit: 5000,
emptyOutDir: true,
},
server: {
headers: {
"Cross-Origin-Embedder-Policy": "require-corp",
"Cross-Origin-Opener-Policy": "same-origin",
},
},
}
})
Binary file modified bun.lockb
Binary file not shown.
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"scripts": {
"build": "turbo run build",
"b": "bun run build",
"build:extension": "turbo run build --filter=@palladxyz/extension...",
"build:features": "turbo run build --filter=@palladxyz/features...",
"story:features": "turbo run story:build --filter=@palladxyz/features",
"build:extension": "turbo run build --filter=@palladco/extension...",
"build:features": "turbo run build --filter=@palladco/features...",
"story:features": "turbo run story:build --filter=@palladco/features",
"dev:extension": "turbowatch extension.turbowatch.ts",
"lint": "bunx biome check .",
"lint:fix": "bun run lint --write",
"test:unit": "turbo run test:unit",
"test:e2e:extension": "turbo run test:e2e --filter=@palladxyz/extension",
"test:e2e:extension": "turbo run test:e2e --filter=@palladco/extension",
"format": "bunx biome check --write --unsafe .",
"f": "bun run format",
"cleanup": "turbo run cleanup && rimraf node_modules .turbo",
Expand All @@ -34,7 +34,6 @@
"husky": "9.1.4",
"postcss": "8.4.40",
"rimraf": "6.0.1",
"tailwind-merge": "2.4.0",
"tailwindcss": "3.4.7",
"tailwindcss-animate": "1.0.7",
"tsup": "8.2.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/_template/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# @palladxyz/template
# @palladco/template

This is a boilerplate for creating new modules of the wallet.
4 changes: 2 additions & 2 deletions packages/_template/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@palladxyz/template",
"name": "@palladco/template",
"description": "Module template for new packages",
"version": "1.0.0",
"type": "module",
Expand All @@ -18,6 +18,6 @@
"cleanup": "rimraf node_modules dist .turbo"
},
"devDependencies": {
"@palladxyz/common": "workspace:*"
"@palladco/common": "workspace:*"
}
}
2 changes: 1 addition & 1 deletion packages/_template/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "@palladxyz/common/tsconfig.json"
"extends": "@palladco/common/tsconfig.json"
}
2 changes: 1 addition & 1 deletion packages/_template/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { baseTsupConfig } from "@palladxyz/common"
import { baseTsupConfig } from "@palladco/common"
import { defineConfig } from "tsup"

import packageJson from "./package.json"
Expand Down
2 changes: 1 addition & 1 deletion packages/common/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# @palladxyz/common
# @palladco/common

The proto-package for shareable common configuration.
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@palladxyz/common",
"name": "@palladco/common",
"description": "The proto package for shareable common configuration.",
"version": "1.0.0",
"type": "module",
Expand Down
25 changes: 25 additions & 0 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@palladco/contracts",
"version": "0.0.1",
"type": "module",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"scripts": {
"build": "bunx --bun tsc",
"test:unit": "bun test",
"coverage": "bun test --coverage",
"cleanup": "bunx --bun rimraf node_modules dist .turbo"
},
"dependencies": {
"o1js": "^2.0.0"
},
"devDependencies": {
"@palladco/common": "workspace:*"
}
}
26 changes: 26 additions & 0 deletions packages/contracts/src/add/add.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { beforeAll, describe, expect, it } from "bun:test"
import { AccountUpdate, Field, Mina, PrivateKey } from "o1js"
import { Add } from "./add"

describe("Add", () => {
beforeAll(async () => {
await Add.compile()
})

it("works lol", async () => {
const Local = await Mina.LocalBlockchain({ proofsEnabled: true })
Mina.setActiveInstance(Local)
const [deployerAccount] = Local.testAccounts
const zkAppPrivateKey = PrivateKey.random()
const zkAppAddress = zkAppPrivateKey.toPublicKey()
const zkApp = new Add(zkAppAddress)
const txn = await Mina.transaction(deployerAccount, async () => {
AccountUpdate.fundNewAccount(deployerAccount)
await zkApp.deploy()
})
await txn.prove()
await txn.sign([deployerAccount.key, zkAppPrivateKey]).send()
const num = zkApp.num.get()
expect(num).toEqual(Field(1))
})
})
25 changes: 25 additions & 0 deletions packages/contracts/src/add/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Field, SmartContract, State, method, state } from "o1js"

/**
* Basic Example
* See https://docs.minaprotocol.com/zkapps for more info.
*
* The Add contract initializes the state variable 'num' to be a Field(1) value by default when deployed.
* When the 'update' method is called, the Add contract adds Field(2) to its 'num' contract state.
*
* This file is safe to delete and replace with your own contract.
*/
export class Add extends SmartContract {
@state(Field) num = State<Field>()

init() {
super.init()
this.num.set(Field(1))
}

@method async update() {
const currentState = this.num.getAndRequireEquals()
const newState = currentState.add(2)
this.num.set(newState)
}
}
1 change: 1 addition & 0 deletions packages/contracts/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Add } from "./add/add"
29 changes: 29 additions & 0 deletions packages/contracts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"extends": "@palladco/common/tsconfig.json",
"compilerOptions": {
// "experimentalDecorators": true,
// "noImplicitOverride": false,
// "strictPropertyInitialization": false,
// "skipLibCheck": true,
// "emitDecoratorMetadata": true,
// "useDefineForClassFields": false,
"target": "es2020",
"module": "es2022",
"strictPropertyInitialization": false,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowJs": true,
"declaration": true,
"sourceMap": true,
"noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": true,
"useDefineForClassFields": false,
"noImplicitOverride": false,
"outDir": "dist"
},
"include": ["src"]
}
Loading