From c5dba4ac98a6565061ac80046ca4bffc6a17135c Mon Sep 17 00:00:00 2001 From: kadirchan Date: Thu, 21 Nov 2024 14:54:51 +0300 Subject: [PATCH] init --- .erb/configs/webpack.config.main.dev.ts | 7 + package.json | 5 + pnpm-lock.yaml | 134 +++++++++++++++++++ src/main/main.ts | 2 + src/main/prover.ts | 169 ++++++++++++++++++++++++ 5 files changed, 317 insertions(+) create mode 100644 src/main/prover.ts diff --git a/.erb/configs/webpack.config.main.dev.ts b/.erb/configs/webpack.config.main.dev.ts index 33082d4..9c8694d 100644 --- a/.erb/configs/webpack.config.main.dev.ts +++ b/.erb/configs/webpack.config.main.dev.ts @@ -36,6 +36,13 @@ const configuration: webpack.Configuration = { }, }, + externals: { + 'drm-mina-contracts': 'commonjs2 drm-mina-contracts', + o1js: 'commonjs2 o1js', + express: 'commonjs2 express', + axios: 'commonjs2 axios', + }, + plugins: [ // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore diff --git a/package.json b/package.json index 5e3de77..a40a158 100644 --- a/package.json +++ b/package.json @@ -91,12 +91,14 @@ "@radix-ui/react-slot": "^1.1.0", "@vscode/sudo-prompt": "^9.3.1", "autoprefixer": "^10.4.20", + "axios": "^1.7.7", "class-variance-authority": "^0.7.0", "clsx": "^2.1.1", "electron-debug": "^3.2.0", "electron-log": "^4.4.8", "electron-updater": "^6.1.4", "embla-carousel-react": "^8.3.1", + "express": "^4.21.1", "lucide-react": "^0.454.0", "postcss-loader": "^8.1.1", "react": "^18.2.0", @@ -120,6 +122,7 @@ "@types/react": "^18.2.21", "@types/react-dom": "^18.2.7", "@types/react-test-renderer": "^18.0.1", + "@types/express": "^4.17.21", "@types/webpack-bundle-analyzer": "^4.6.0", "@typescript-eslint/eslint-plugin": "^6.7.0", "@typescript-eslint/parser": "^6.7.0", @@ -131,6 +134,7 @@ "css-loader": "^6.8.1", "css-minimizer-webpack-plugin": "^5.0.1", "detect-port": "^1.5.1", + "drm-mina-contracts": "^0.5.0", "electron": "^31.3.0", "electron-builder": "^24.13.3", "electron-devtools-installer": "^3.2.0", @@ -153,6 +157,7 @@ "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", "mini-css-extract-plugin": "^2.7.6", + "o1js": "https://pkg.pr.new/o1-labs/o1js@fc16ce3", "prettier": "^3.0.3", "react-refresh": "^0.14.0", "react-test-renderer": "^18.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f8fe4a3..7cbeeec 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -35,6 +35,9 @@ importers: autoprefixer: specifier: ^10.4.20 version: 10.4.20(postcss@8.4.47) + axios: + specifier: ^1.7.7 + version: 1.7.7 class-variance-authority: specifier: ^0.7.0 version: 0.7.0 @@ -53,6 +56,9 @@ importers: embla-carousel-react: specifier: ^8.3.1 version: 8.3.1(react@18.3.1) + express: + specifier: ^4.21.1 + version: 4.21.1 lucide-react: specifier: ^0.454.0 version: 0.454.0(react@18.3.1) @@ -102,6 +108,9 @@ importers: '@testing-library/react': specifier: ^14.0.0 version: 14.3.1(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@types/express': + specifier: ^4.17.21 + version: 4.17.21 '@types/jest': specifier: ^29.5.5 version: 29.5.14 @@ -150,6 +159,9 @@ importers: detect-port: specifier: ^1.5.1 version: 1.6.1 + drm-mina-contracts: + specifier: ^0.5.0 + version: 0.5.0(encoding@0.1.13) electron: specifier: ^31.3.0 version: 31.7.3 @@ -216,6 +228,9 @@ importers: mini-css-extract-plugin: specifier: ^2.7.6 version: 2.9.2(webpack@5.96.1) + o1js: + specifier: https://pkg.pr.new/o1-labs/o1js@fc16ce3 + version: https://pkg.pr.new/o1-labs/o1js@fc16ce3(encoding@0.1.13) prettier: specifier: ^3.0.3 version: 3.3.3 @@ -2329,6 +2344,9 @@ packages: resolution: {integrity: sha512-RE3mdQ7P3FRSe7eqCWoeQ/Z9QXrtniSjp1wUjt5nRC3WIpz5rSCve6o3fsZ2aCpJtrZjSZgjwXAoTO5k4tEI0w==} engines: {node: '>=4'} + axios@1.7.7: + resolution: {integrity: sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==} + axobject-query@3.2.4: resolution: {integrity: sha512-aPTElBrbifBU1krmZxGZOlBkslORe7Ll7+BDnI50Wy4LgOt69luMgevkDfTq1O/ZgprooPCtWpjCwKSZw/iZ4A==} engines: {node: '>= 0.4'} @@ -2396,6 +2414,9 @@ packages: bl@4.1.0: resolution: {integrity: sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==} + blakejs@1.2.1: + resolution: {integrity: sha512-QXUSXI3QVc/gJME0dBpXrag1kbzOqCjCX8/b54ntNyW6sjtoqxqRk3LTmXzaJoh71zMsDCjM+47jS7XiwN/+fQ==} + bluebird-lst@1.0.9: resolution: {integrity: sha512-7B1Rtx82hjnSD4PGLAjVWeYH3tHAcVUmChh85a3lltKQm6FresXh9ErQo6oAv6CqxttczC3/kEg8SY5NluPuUw==} @@ -2483,6 +2504,10 @@ packages: resolution: {integrity: sha512-v+p6ongsrp0yTGbJXjgxPow2+DL93DASP4kXCDKb8/bwRtt9OEF3whggkkDkGNzgcWy2XaF4a8nZglC7uElscg==} engines: {node: '>=8'} + cachedir@2.4.0: + resolution: {integrity: sha512-9EtFOZR8g22CL7BWjJ9BUx1+A/djkofnyW3aOXZORNW2kxoUpx2h+uN2cOqwPmFhnpVmxg+KW2OjOSgChTEvsQ==} + engines: {node: '>=6'} + call-bind@1.0.7: resolution: {integrity: sha512-GHTSNSYICQ7scH7sZ+M2rFopRoLh8t2bLSW6BbgrtLsahOIB5iyAVJf9GjWK3cYTDaMj4XdBpM1cA6pIS0Kv2w==} engines: {node: '>= 0.4'} @@ -3086,10 +3111,18 @@ packages: dotenv-expand@5.1.0: resolution: {integrity: sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA==} + dotenv@16.4.5: + resolution: {integrity: sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==} + engines: {node: '>=12'} + dotenv@9.0.2: resolution: {integrity: sha512-I9OvvrHp4pIARv4+x9iuewrWycX6CcZtoAu1XrzPxc5UygMJXJZYmBsynku8IkrJwgypE5DGNjDPmPRhDCptUg==} engines: {node: '>=10'} + drm-mina-contracts@0.5.0: + resolution: {integrity: sha512-MrWh2nI/zr3UoT0XUHHd/FXfWh+nu26L5jFYrzIaO3Q9cLr7IsWhOBuBPDs/Ghl0ck6llcIBLEBD1Rtcl+rkFA==} + engines: {node: '>=18.14.0'} + duplexer@0.1.2: resolution: {integrity: sha512-jtD6YG370ZCIi/9GTaJKQxWTZD045+4R4hTk/x1UyoqadyJ9x9CgSi1RlVDQF8U2sxLLSnFkCaMihqljHIWgMg==} @@ -4177,6 +4210,9 @@ packages: resolution: {integrity: sha512-WhB9zCku7EGTj/HQQRz5aUQEUeoQZH2bWcltRErOpymJ4boYE6wL9Tbr23krRPSZ+C5zqNSrSw+Cc7sZZ4b7vg==} engines: {node: '>=0.10.0'} + isomorphic-fetch@3.0.0: + resolution: {integrity: sha512-qvUtwJ3j6qwsF3jLxkZ72qCgjMysPzDfeV240JHiGZsANBYd+EEuu35v7dfrJ9Up0Ak07D7GGSkGhCHTqg/5wA==} + istanbul-lib-coverage@3.2.2: resolution: {integrity: sha512-O8dpsF+r0WV/8MNRKfnmrtCWhuKjxrq2w+jpzBL5UZKTi2LeVWnWOmWRxFlesJONmc+wLAGvKQZEOanko0LFTg==} engines: {node: '>=8'} @@ -4359,6 +4395,9 @@ packages: resolution: {integrity: sha512-2yTgeWTWzMWkHu6Jp9NKgePDaYHbntiwvYuuJLbbN9vl7DC9DvXKOB2BC3ZZ92D3cvV/aflH0osDfwpHepQ53w==} hasBin: true + js-sha256@0.9.0: + resolution: {integrity: sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA==} + js-tokens@4.0.0: resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==} @@ -4798,6 +4837,15 @@ packages: node-api-version@0.2.0: resolution: {integrity: sha512-fthTTsi8CxaBXMaBAD7ST2uylwvsnYxh2PfaScwpMhos6KlSFajXQPcM4ogNE1q2s3Lbz9GCGqeIHC+C6OZnKg==} + node-fetch@2.7.0: + resolution: {integrity: sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==} + engines: {node: 4.x || >=6.0.0} + peerDependencies: + encoding: ^0.1.0 + peerDependenciesMeta: + encoding: + optional: true + node-forge@1.3.1: resolution: {integrity: sha512-dPEtOeMvF9VMcYV/1Wb8CPoVAXtp6MKMlcbAt4ddqmGqUJ6fQZFXkNZNkNlfevtNkGtaSoXf/vNNNSvgrdXwtA==} engines: {node: '>= 6.13.0'} @@ -4839,6 +4887,12 @@ packages: nwsapi@2.2.13: resolution: {integrity: sha512-cTGB9ptp9dY9A5VbMSe7fQBcl/tt22Vcqdq8+eN93rblOuE0aCFu4aZ2vMwct/2t+lFnosm8RkQW1I0Omb1UtQ==} + o1js@https://pkg.pr.new/o1-labs/o1js@fc16ce3: + resolution: {tarball: https://pkg.pr.new/o1-labs/o1js@fc16ce3} + version: 1.9.0 + engines: {node: '>=18.14.0'} + hasBin: true + object-assign@4.1.1: resolution: {integrity: sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==} engines: {node: '>=0.10.0'} @@ -5330,6 +5384,9 @@ packages: resolution: {integrity: sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==} engines: {node: '>= 0.10'} + proxy-from-env@1.1.0: + resolution: {integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==} + psl@1.9.0: resolution: {integrity: sha512-E/ZsdU4HLs/68gYzgGTkMicWTLPdAftJLfJFlLUAAKZGkStNU72sZjT66SnMDVOfOWY/YAoiD7Jxa9iHvngcag==} @@ -5486,6 +5543,9 @@ packages: resolution: {integrity: sha512-6tDA8g98We0zd0GvVeMT9arEOnTw9qM03L9cJXaCjrip1OO764RDBLBfrB4cwzNGDj5OA5ioymC9GkizgWJDUg==} engines: {node: '>=8'} + reflect-metadata@0.1.14: + resolution: {integrity: sha512-ZhYeb6nRaXCfhnndflDK8qI6ZQ/YcWZCISRAWICW9XYqMUwjZM9Z0DveWX/ABN01oxSHwVxKQmxeYZSsm0jh5A==} + reflect.getprototypeof@1.0.6: resolution: {integrity: sha512-fmfw4XgoDke3kdI6h4xcUz1dG8uaiv5q9gcEwLS4Pnth2kxT+GZ7YehS1JTMGBQmtV7Y4GFGbs2re2NqhdozUg==} engines: {node: '>= 0.4'} @@ -6077,6 +6137,9 @@ packages: resolution: {integrity: sha512-Loo5UUvLD9ScZ6jh8beX1T6sO1w2/MpCRpEP7V280GKMVUQ0Jzar2U3UJPsrdbziLEMMhu3Ujnq//rhiFuIeag==} engines: {node: '>=6'} + tr46@0.0.3: + resolution: {integrity: sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==} + tr46@3.0.0: resolution: {integrity: sha512-l7FvfAHlcmulp8kr+flpQZmVwtu7nfRV7NZujtN0OqES8EL4O4e0qqzL0DC5gAvx/ZC/9lk6rhcUwYvkBnBnYA==} engines: {node: '>=12'} @@ -6358,6 +6421,9 @@ packages: wcwidth@1.0.1: resolution: {integrity: sha512-XHPEwS0q6TaxcvG85+8EYkbiCux2XtWG2mkc47Ng2A77BQu9+DqIOJldST4HgPkuea7dvKSj5VgX3P1d4rW8Tg==} + webidl-conversions@3.0.1: + resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==} + webidl-conversions@7.0.0: resolution: {integrity: sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==} engines: {node: '>=12'} @@ -6433,6 +6499,9 @@ packages: resolution: {integrity: sha512-p41ogyeMUrw3jWclHWTQg1k05DSVXPLcVxRTYsXUk+ZooOCZLcoYgPZ/HL/D/N+uQPOtcp1me1WhBEaX02mhWg==} engines: {node: '>=12'} + whatwg-fetch@3.6.20: + resolution: {integrity: sha512-EqhiFU6daOA8kpjOWTL0olhVOF3i7OrFzSYiGsEMB8GcXS+RrzauAERX65xMeNWVqxA6HXH2m69Z9LaKKdisfg==} + whatwg-mimetype@3.0.0: resolution: {integrity: sha512-nt+N2dzIutVRxARx1nghPKGv1xHikU7HKdfafKkLNLindmPU/ch3U31NOCGGA/dmPcmb1VlofO0vnKAcsm0o/Q==} engines: {node: '>=12'} @@ -6441,6 +6510,9 @@ packages: resolution: {integrity: sha512-RKT8HExMpoYx4igMiVMY83lN6UeITKJlBQ+vR/8ZJ8OCdSiN3RwCq+9gH0+Xzj0+5IrM6i4j/6LuvzbZIQgEcQ==} engines: {node: '>=12'} + whatwg-url@5.0.0: + resolution: {integrity: sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==} + which-boxed-primitive@1.0.2: resolution: {integrity: sha512-bwZdv0AKLpplFY2KZRX6TvyuN7ojjr7lwkg6ml0roIy9YeuSr7JS372qlNW18UQYzgYK9ziGcerWqZOmEn9VNg==} @@ -9111,6 +9183,14 @@ snapshots: axe-core@4.10.2: {} + axios@1.7.7: + dependencies: + follow-redirects: 1.15.9 + form-data: 4.0.1 + proxy-from-env: 1.1.0 + transitivePeerDependencies: + - debug + axobject-query@3.2.4: {} axobject-query@4.1.0: {} @@ -9210,6 +9290,8 @@ snapshots: inherits: 2.0.4 readable-stream: 3.6.2 + blakejs@1.2.1: {} + bluebird-lst@1.0.9: dependencies: bluebird: 3.7.2 @@ -9358,6 +9440,8 @@ snapshots: normalize-url: 6.1.0 responselike: 2.0.1 + cachedir@2.4.0: {} + call-bind@1.0.7: dependencies: es-define-property: 1.0.0 @@ -9982,8 +10066,17 @@ snapshots: dotenv-expand@5.1.0: {} + dotenv@16.4.5: {} + dotenv@9.0.2: {} + drm-mina-contracts@0.5.0(encoding@0.1.13): + dependencies: + dotenv: 16.4.5 + o1js: https://pkg.pr.new/o1-labs/o1js@fc16ce3(encoding@0.1.13) + transitivePeerDependencies: + - encoding + duplexer@0.1.2: {} eastasianwidth@0.2.0: {} @@ -11330,6 +11423,13 @@ snapshots: isobject@3.0.1: {} + isomorphic-fetch@3.0.0(encoding@0.1.13): + dependencies: + node-fetch: 2.7.0(encoding@0.1.13) + whatwg-fetch: 3.6.20 + transitivePeerDependencies: + - encoding + istanbul-lib-coverage@3.2.2: {} istanbul-lib-instrument@5.2.1: @@ -11724,6 +11824,8 @@ snapshots: jiti@1.21.6: {} + js-sha256@0.9.0: {} + js-tokens@4.0.0: {} js-yaml@3.14.1: @@ -12143,6 +12245,12 @@ snapshots: dependencies: semver: 7.6.3 + node-fetch@2.7.0(encoding@0.1.13): + dependencies: + whatwg-url: 5.0.0 + optionalDependencies: + encoding: 0.1.13 + node-forge@1.3.1: {} node-int64@0.4.0: {} @@ -12173,6 +12281,17 @@ snapshots: nwsapi@2.2.13: {} + o1js@https://pkg.pr.new/o1-labs/o1js@fc16ce3(encoding@0.1.13): + dependencies: + blakejs: 1.2.1 + cachedir: 2.4.0 + isomorphic-fetch: 3.0.0(encoding@0.1.13) + js-sha256: 0.9.0 + reflect-metadata: 0.1.14 + tslib: 2.8.1 + transitivePeerDependencies: + - encoding + object-assign@4.1.1: {} object-hash@3.0.0: {} @@ -12635,6 +12754,8 @@ snapshots: forwarded: 0.2.0 ipaddr.js: 1.9.1 + proxy-from-env@1.1.0: {} + psl@1.9.0: {} pump@3.0.2: @@ -12800,6 +12921,8 @@ snapshots: indent-string: 4.0.0 strip-indent: 3.0.0 + reflect-metadata@0.1.14: {} + reflect.getprototypeof@1.0.6: dependencies: call-bind: 1.0.7 @@ -13480,6 +13603,8 @@ snapshots: universalify: 0.2.0 url-parse: 1.5.10 + tr46@0.0.3: {} + tr46@3.0.0: dependencies: punycode: 2.3.1 @@ -13763,6 +13888,8 @@ snapshots: dependencies: defaults: 1.0.4 + webidl-conversions@3.0.1: {} + webidl-conversions@7.0.0: {} webpack-bundle-analyzer@4.10.2: @@ -13905,6 +14032,8 @@ snapshots: dependencies: iconv-lite: 0.6.3 + whatwg-fetch@3.6.20: {} + whatwg-mimetype@3.0.0: {} whatwg-url@11.0.0: @@ -13912,6 +14041,11 @@ snapshots: tr46: 3.0.0 webidl-conversions: 7.0.0 + whatwg-url@5.0.0: + dependencies: + tr46: 0.0.3 + webidl-conversions: 3.0.1 + which-boxed-primitive@1.0.2: dependencies: is-bigint: 1.0.4 diff --git a/src/main/main.ts b/src/main/main.ts index b0d9ec4..aaa2b78 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -21,6 +21,8 @@ import { getWindowsSystemInfo } from '@/lib/identity/windows'; import { getMacOSSystemInfo } from '@/lib/identity/macos'; import { WEB_URL } from '@/env'; +import './prover'; + class AppUpdater { constructor() { log.transports.file.level = 'info'; diff --git a/src/main/prover.ts b/src/main/prover.ts new file mode 100644 index 0000000..a3a0e64 --- /dev/null +++ b/src/main/prover.ts @@ -0,0 +1,169 @@ +import { DRM, offchainState } from 'drm-mina-contracts/build/src/DRM.js'; +import { Identifiers } from 'drm-mina-contracts/build/src/lib/DeviceIdentifier.js'; +import { + DeviceSession, + DeviceSessionInput, +} from 'drm-mina-contracts/build/src/lib/DeviceSessionProof.js'; + +import { fetchAccount, Field, Mina, PublicKey, UInt64 } from 'o1js'; + +import axios from 'axios'; +import express from 'express'; + +let drmAddress: PublicKey | undefined; +let gameTokenAddress: PublicKey | undefined; + +let isDeviceSessionCompiled = false; +let isOffChainStateCompiled = false; +let drmInstance: DRM | undefined; + +const setMinaNetwork = () => { + const Network = Mina.Network({ + mina: 'https://api.minascan.io/node/devnet/v1/graphql', + archive: 'https://api.minascan.io/archive/devnet/v1/graphql', + }); + Mina.setActiveInstance(Network); +}; + +const compilePrograms = async () => { + console.time('Compiling DeviceSession'); + await DeviceSession.compile(); + isDeviceSessionCompiled = true; + console.timeEnd('Compiling DeviceSession'); + + console.time('Compiling OffchainState'); + await offchainState.compile(); + isOffChainStateCompiled = true; + console.timeEnd('Compiling OffchainState'); +}; + +const setDRMInstance = () => { + if (!drmAddress) { + throw new Error('DRM address is required'); + } + drmInstance = new DRM(drmAddress); + drmInstance.offchainState.setContractInstance(drmInstance); +}; + +setMinaNetwork(); +compilePrograms(); + +const app = express(); +app.use(express.json()); + +app.post('/', async (req, res) => { + if (!isDeviceSessionCompiled) { + res.status(102).send('DeviceSession not compiled yet'); + return; + } + if (!gameTokenAddress) { + res.status(400).send('Game token address not set yet'); + return; + } + try { + const { rawIdentifiers, currentSession, newSession } = req.body; + console.log(currentSession, ' -> ', newSession); + const identifiers = Identifiers.fromRaw(rawIdentifiers); + const publicInput = new DeviceSessionInput({ + gameToken: gameTokenAddress, + currentSessionKey: UInt64.from(currentSession), + newSessionKey: UInt64.from(newSession), + }); + console.log('Generating proof'); + const proof = await DeviceSession.proofForSession(publicInput, identifiers); + + const response = await axios.post('http://api.drmmina.com/submit-session', { + proof: JSON.stringify(proof.toJSON()), + }); + + if (response.status !== 200) { + throw new Error(`Failed to submit session: ${response.status}`); + } + + console.log('Transaction sent'); + return res.status(200).send('Transaction sent'); + } catch (e) { + console.error(e); + return res.status(500).send('Transaction failed'); + } +}); + +app.post('/set-address', async (req, res) => { + try { + const { drmAddressB58, gameTokenAddressB58 } = req.body; + console.log('DRM address', drmAddressB58); + console.log('Game token address', gameTokenAddressB58); + + if (!drmAddressB58) { + console.error('DRM address is required'); + res.status(400).send('DRM address is required'); + return; + } + + if (!gameTokenAddressB58) { + console.error('Game token address is required'); + res.status(400).send('Game token address is required'); + return; + } + + try { + drmAddress = PublicKey.fromBase58(drmAddressB58); + gameTokenAddress = PublicKey.fromBase58(gameTokenAddressB58); + } catch (e) { + console.error(e); + res.status(400).send('Invalid address'); + return; + } + + setDRMInstance(); + res.status(200).send('Address set successfully'); + return; + } catch (e) { + console.error(e); + res.status(500).send('Address setting failed'); + } +}); + +app.post('/current-session', async (req, res) => { + if (!isOffChainStateCompiled) { + res.status(102).send('OffChainState not compiled yet'); + return; + } + if (!drmAddress) { + res.status(400).send('DRM address not set yet'); + return; + } + if (!drmInstance) { + res.status(400).send('DRM instance not set yet'); + return; + } + + try { + const { deviceHash } = req.body; + + console.log(deviceHash); + + await fetchAccount({ + publicKey: drmAddress, + }); + + const currentSession = await drmInstance!.offchainState.fields.sessions.get( + Field.from(deviceHash), + ); + if (!currentSession.isSome.toBoolean()) { + console.log('Current session not found'); + res.status(200).send({ currentSession: 0 }); + } else { + console.log('Current session', currentSession.value.toString()); + res.status(200).send({ currentSession: currentSession.value.toString() }); + } + } catch (e) { + console.error(e); + res.status(500).send('Current session fetch failed'); + return; + } +}); + +app.listen(4444, () => { + console.log('Server started on port 4444'); +});