put mempow in webworker and changed wasm import

This commit is contained in:
PhilReact 2024-11-18 01:12:05 +02:00
parent db754eb121
commit 14214ba6f0
9 changed files with 426 additions and 93 deletions

301
package-lock.json generated
View File

@ -79,7 +79,9 @@
"slate": "^0.103.0",
"slate-react": "^0.109.0",
"tippy.js": "^6.3.7",
"tiptap-extension-resize-image": "^1.1.8"
"tiptap-extension-resize-image": "^1.1.8",
"vite-plugin-top-level-await": "^1.4.4",
"vite-plugin-wasm": "^3.3.0"
},
"devDependencies": {
"@testing-library/dom": "^10.3.0",
@ -1421,7 +1423,6 @@
"cpu": [
"ppc64"
],
"dev": true,
"optional": true,
"os": [
"aix"
@ -1437,7 +1438,6 @@
"cpu": [
"arm"
],
"dev": true,
"optional": true,
"os": [
"android"
@ -1453,7 +1453,6 @@
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"android"
@ -1469,7 +1468,6 @@
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"android"
@ -1485,7 +1483,6 @@
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"darwin"
@ -1501,7 +1498,6 @@
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"darwin"
@ -1517,7 +1513,6 @@
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"freebsd"
@ -1533,7 +1528,6 @@
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"freebsd"
@ -1549,7 +1543,6 @@
"cpu": [
"arm"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -1565,7 +1558,6 @@
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -1581,7 +1573,6 @@
"cpu": [
"ia32"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -1597,7 +1588,6 @@
"cpu": [
"loong64"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -1613,7 +1603,6 @@
"cpu": [
"mips64el"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -1629,7 +1618,6 @@
"cpu": [
"ppc64"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -1645,7 +1633,6 @@
"cpu": [
"riscv64"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -1661,7 +1648,6 @@
"cpu": [
"s390x"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -1677,7 +1663,6 @@
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -1693,7 +1678,6 @@
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"netbsd"
@ -1709,7 +1693,6 @@
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"openbsd"
@ -1725,7 +1708,6 @@
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"sunos"
@ -1741,7 +1723,6 @@
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"win32"
@ -1757,7 +1738,6 @@
"cpu": [
"ia32"
],
"dev": true,
"optional": true,
"os": [
"win32"
@ -1773,7 +1753,6 @@
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"win32"
@ -2516,7 +2495,6 @@
"version": "0.3.6",
"resolved": "https://registry.npmjs.org/@jridgewell/source-map/-/source-map-0.3.6.tgz",
"integrity": "sha512-1ZJTZebgqllO79ue2bm3rIGud/bOe0pP5BjSRCRxxYkEZS8STV7zN84UBbiYu7jy+eCKSnVIUgoWWE/tt+shMQ==",
"dev": true,
"optional": true,
"peer": true,
"dependencies": {
@ -3014,6 +2992,22 @@
"resolved": "https://registry.npmjs.org/@remirror/core-constants/-/core-constants-3.0.0.tgz",
"integrity": "sha512-42aWfPrimMfDKDi4YegyS7x+/0tlzaqwPQCULLanv3DMIlu96KTJR0fM5isWX2UViOqlGnX6YFgqWepcX+XMNg=="
},
"node_modules/@rollup/plugin-virtual": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/@rollup/plugin-virtual/-/plugin-virtual-3.0.2.tgz",
"integrity": "sha512-10monEYsBp3scM4/ND4LNH5Rxvh3e/cVeL3jWTgZ2SrQ+BmUoQcopVQvnaMcOnykb1VkxUFuDAN+0FnpTFRy2A==",
"engines": {
"node": ">=14.0.0"
},
"peerDependencies": {
"rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0"
},
"peerDependenciesMeta": {
"rollup": {
"optional": true
}
}
},
"node_modules/@rollup/rollup-android-arm-eabi": {
"version": "4.13.0",
"resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.13.0.tgz",
@ -3021,7 +3015,6 @@
"cpu": [
"arm"
],
"dev": true,
"optional": true,
"os": [
"android"
@ -3034,7 +3027,6 @@
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"android"
@ -3047,7 +3039,6 @@
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"darwin"
@ -3060,7 +3051,6 @@
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"darwin"
@ -3073,7 +3063,6 @@
"cpu": [
"arm"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -3086,7 +3075,6 @@
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -3099,7 +3087,6 @@
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -3112,7 +3099,6 @@
"cpu": [
"riscv64"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -3125,7 +3111,6 @@
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -3138,7 +3123,6 @@
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"linux"
@ -3151,7 +3135,6 @@
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"win32"
@ -3164,7 +3147,6 @@
"cpu": [
"ia32"
],
"dev": true,
"optional": true,
"os": [
"win32"
@ -3177,7 +3159,6 @@
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"win32"
@ -3213,6 +3194,206 @@
"url": "https://github.com/sindresorhus/is?sponsor=1"
}
},
"node_modules/@swc/core": {
"version": "1.9.2",
"resolved": "https://registry.npmjs.org/@swc/core/-/core-1.9.2.tgz",
"integrity": "sha512-dYyEkO6mRYtZFpnOsnYzv9rY69fHAHoawYOjGOEcxk9WYtaJhowMdP/w6NcOKnz2G7GlZaenjkzkMa6ZeQeMsg==",
"hasInstallScript": true,
"dependencies": {
"@swc/counter": "^0.1.3",
"@swc/types": "^0.1.15"
},
"engines": {
"node": ">=10"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/swc"
},
"optionalDependencies": {
"@swc/core-darwin-arm64": "1.9.2",
"@swc/core-darwin-x64": "1.9.2",
"@swc/core-linux-arm-gnueabihf": "1.9.2",
"@swc/core-linux-arm64-gnu": "1.9.2",
"@swc/core-linux-arm64-musl": "1.9.2",
"@swc/core-linux-x64-gnu": "1.9.2",
"@swc/core-linux-x64-musl": "1.9.2",
"@swc/core-win32-arm64-msvc": "1.9.2",
"@swc/core-win32-ia32-msvc": "1.9.2",
"@swc/core-win32-x64-msvc": "1.9.2"
},
"peerDependencies": {
"@swc/helpers": "*"
},
"peerDependenciesMeta": {
"@swc/helpers": {
"optional": true
}
}
},
"node_modules/@swc/core-darwin-arm64": {
"version": "1.9.2",
"resolved": "https://registry.npmjs.org/@swc/core-darwin-arm64/-/core-darwin-arm64-1.9.2.tgz",
"integrity": "sha512-nETmsCoY29krTF2PtspEgicb3tqw7Ci5sInTI03EU5zpqYbPjoPH99BVTjj0OsF53jP5MxwnLI5Hm21lUn1d6A==",
"cpu": [
"arm64"
],
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": ">=10"
}
},
"node_modules/@swc/core-darwin-x64": {
"version": "1.9.2",
"resolved": "https://registry.npmjs.org/@swc/core-darwin-x64/-/core-darwin-x64-1.9.2.tgz",
"integrity": "sha512-9gD+bwBz8ZByjP6nZTXe/hzd0tySIAjpDHgkFiUrc+5zGF+rdTwhcNrzxNHJmy6mw+PW38jqII4uspFHUqqxuQ==",
"cpu": [
"x64"
],
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": ">=10"
}
},
"node_modules/@swc/core-linux-arm-gnueabihf": {
"version": "1.9.2",
"resolved": "https://registry.npmjs.org/@swc/core-linux-arm-gnueabihf/-/core-linux-arm-gnueabihf-1.9.2.tgz",
"integrity": "sha512-kYq8ief1Qrn+WmsTWAYo4r+Coul4dXN6cLFjiPZ29Cv5pyU+GFvSPAB4bEdMzwy99rCR0u2P10UExaeCjurjvg==",
"cpu": [
"arm"
],
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=10"
}
},
"node_modules/@swc/core-linux-arm64-gnu": {
"version": "1.9.2",
"resolved": "https://registry.npmjs.org/@swc/core-linux-arm64-gnu/-/core-linux-arm64-gnu-1.9.2.tgz",
"integrity": "sha512-n0W4XiXlmEIVqxt+rD3ZpkogsEWUk1jJ+i5bQNgB+1JuWh0fBE8c/blDgTQXa0GB5lTPVDZQussgdNOCnAZwiA==",
"cpu": [
"arm64"
],
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=10"
}
},
"node_modules/@swc/core-linux-arm64-musl": {
"version": "1.9.2",
"resolved": "https://registry.npmjs.org/@swc/core-linux-arm64-musl/-/core-linux-arm64-musl-1.9.2.tgz",
"integrity": "sha512-8xzrOmsyCC1zrx2Wzx/h8dVsdewO1oMCwBTLc1gSJ/YllZYTb04pNm6NsVbzUX2tKddJVRgSJXV10j/NECLwpA==",
"cpu": [
"arm64"
],
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=10"
}
},
"node_modules/@swc/core-linux-x64-gnu": {
"version": "1.9.2",
"resolved": "https://registry.npmjs.org/@swc/core-linux-x64-gnu/-/core-linux-x64-gnu-1.9.2.tgz",
"integrity": "sha512-kZrNz/PjRQKcchWF6W292jk3K44EoVu1ad5w+zbS4jekIAxsM8WwQ1kd+yjUlN9jFcF8XBat5NKIs9WphJCVXg==",
"cpu": [
"x64"
],
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=10"
}
},
"node_modules/@swc/core-linux-x64-musl": {
"version": "1.9.2",
"resolved": "https://registry.npmjs.org/@swc/core-linux-x64-musl/-/core-linux-x64-musl-1.9.2.tgz",
"integrity": "sha512-TTIpR4rjMkhX1lnFR+PSXpaL83TrQzp9znRdp2TzYrODlUd/R20zOwSo9vFLCyH6ZoD47bccY7QeGZDYT3nlRg==",
"cpu": [
"x64"
],
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=10"
}
},
"node_modules/@swc/core-win32-arm64-msvc": {
"version": "1.9.2",
"resolved": "https://registry.npmjs.org/@swc/core-win32-arm64-msvc/-/core-win32-arm64-msvc-1.9.2.tgz",
"integrity": "sha512-+Eg2d4icItKC0PMjZxH7cSYFLWk0aIp94LNmOw6tPq0e69ax6oh10upeq0D1fjWsKLmOJAWEvnXlayZcijEXDw==",
"cpu": [
"arm64"
],
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">=10"
}
},
"node_modules/@swc/core-win32-ia32-msvc": {
"version": "1.9.2",
"resolved": "https://registry.npmjs.org/@swc/core-win32-ia32-msvc/-/core-win32-ia32-msvc-1.9.2.tgz",
"integrity": "sha512-nLWBi4vZDdM/LkiQmPCakof8Dh1/t5EM7eudue04V1lIcqx9YHVRS3KMwEaCoHLGg0c312Wm4YgrWQd9vwZ5zQ==",
"cpu": [
"ia32"
],
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">=10"
}
},
"node_modules/@swc/core-win32-x64-msvc": {
"version": "1.9.2",
"resolved": "https://registry.npmjs.org/@swc/core-win32-x64-msvc/-/core-win32-x64-msvc-1.9.2.tgz",
"integrity": "sha512-ik/k+JjRJBFkXARukdU82tSVx0CbExFQoQ78qTO682esbYXzjdB5eLVkoUbwen299pnfr88Kn4kyIqFPTje8Xw==",
"cpu": [
"x64"
],
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">=10"
}
},
"node_modules/@swc/counter": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/@swc/counter/-/counter-0.1.3.tgz",
"integrity": "sha512-e2BR4lsJkkRlKZ/qCHPw9ZaSxc0MVUd7gtbtaB7aMvHeJVYe8sOB8DBZkP2DtISHGSku9sCK6T6cnY0CtXrOCQ=="
},
"node_modules/@swc/types": {
"version": "0.1.15",
"resolved": "https://registry.npmjs.org/@swc/types/-/types-0.1.15.tgz",
"integrity": "sha512-XKaZ+dzDIQ9Ot9o89oJQ/aluI17+VvUnIpYJTcZtvv1iYX6MzHh3Ik2CSR7MdPKpPwfZXHBeCingb2b4PoDVdw==",
"dependencies": {
"@swc/counter": "^0.1.3"
}
},
"node_modules/@szmarczak/http-timer": {
"version": "4.0.6",
"resolved": "https://registry.npmjs.org/@szmarczak/http-timer/-/http-timer-4.0.6.tgz",
@ -4079,8 +4260,7 @@
"node_modules/@types/estree": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz",
"integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==",
"devOptional": true
"integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw=="
},
"node_modules/@types/filesystem": {
"version": "0.0.35",
@ -7540,7 +7720,6 @@
"version": "0.19.12",
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.19.12.tgz",
"integrity": "sha512-aARqgq8roFBj054KvQr5f1sFu0D65G+miZRCuJyJ0G13Zwx7vRar5Zhn2tkQNzIXcBrNVsv/8stehpj+GAjgbg==",
"devOptional": true,
"hasInstallScript": true,
"bin": {
"esbuild": "bin/esbuild"
@ -10576,7 +10755,6 @@
"version": "3.3.7",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.7.tgz",
"integrity": "sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==",
"devOptional": true,
"funding": [
{
"type": "github",
@ -13655,7 +13833,6 @@
"version": "8.4.37",
"resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.37.tgz",
"integrity": "sha512-7iB/v/r7Woof0glKLH8b1SPHrsX7uhdO+Geb41QpF/+mWZHU3uxxSlN+UXGVit1PawOYDToO+AbZzhBzWRDwbQ==",
"devOptional": true,
"funding": [
{
"type": "opencollective",
@ -14865,7 +15042,6 @@
"version": "4.13.0",
"resolved": "https://registry.npmjs.org/rollup/-/rollup-4.13.0.tgz",
"integrity": "sha512-3YegKemjoQnYKmsBlOHfMLVPPA5xLkQ8MHLLSw/fBrFaVkEayL51DilPpNNLq1exr98F2B1TzrV0FUlN3gWRPg==",
"devOptional": true,
"dependencies": {
"@types/estree": "1.0.5"
},
@ -15340,7 +15516,6 @@
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.0.tgz",
"integrity": "sha512-itJW8lvSA0TXEphiRoawsCksnlf8SyvmFzIhltqAHluXd88pkCd+cXJVHTDwdCr0IzwptSm035IHQktUu1QUMg==",
"devOptional": true,
"engines": {
"node": ">=0.10.0"
}
@ -15747,7 +15922,6 @@
"version": "5.36.0",
"resolved": "https://registry.npmjs.org/terser/-/terser-5.36.0.tgz",
"integrity": "sha512-IYV9eNMuFAV4THUspIRXkLakHnV6XO7FEdtKjf/mDyrnqUg9LnlOn6/RwRvM9SZjR4GUq8Nk8zj67FzVARr74w==",
"dev": true,
"optional": true,
"peer": true,
"dependencies": {
@ -15767,7 +15941,6 @@
"version": "2.20.3",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz",
"integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==",
"dev": true,
"optional": true,
"peer": true
},
@ -16239,6 +16412,18 @@
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw=="
},
"node_modules/uuid": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-10.0.0.tgz",
"integrity": "sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ==",
"funding": [
"https://github.com/sponsors/broofa",
"https://github.com/sponsors/ctavan"
],
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/validate-npm-package-license": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz",
@ -16297,7 +16482,6 @@
"version": "5.1.6",
"resolved": "https://registry.npmjs.org/vite/-/vite-5.1.6.tgz",
"integrity": "sha512-yYIAZs9nVfRJ/AiOLCA91zzhjsHUgMjB+EigzFb6W2XTLO8JixBCKCjvhKZaye+NKYHCrkv3Oh50dH9EdLU2RA==",
"devOptional": true,
"dependencies": {
"esbuild": "^0.19.3",
"postcss": "^8.4.35",
@ -16371,6 +16555,27 @@
"url": "https://opencollective.com/vitest"
}
},
"node_modules/vite-plugin-top-level-await": {
"version": "1.4.4",
"resolved": "https://registry.npmjs.org/vite-plugin-top-level-await/-/vite-plugin-top-level-await-1.4.4.tgz",
"integrity": "sha512-QyxQbvcMkgt+kDb12m2P8Ed35Sp6nXP+l8ptGrnHV9zgYDUpraO0CPdlqLSeBqvY2DToR52nutDG7mIHuysdiw==",
"dependencies": {
"@rollup/plugin-virtual": "^3.0.2",
"@swc/core": "^1.7.0",
"uuid": "^10.0.0"
},
"peerDependencies": {
"vite": ">=2.8"
}
},
"node_modules/vite-plugin-wasm": {
"version": "3.3.0",
"resolved": "https://registry.npmjs.org/vite-plugin-wasm/-/vite-plugin-wasm-3.3.0.tgz",
"integrity": "sha512-tVhz6w+W9MVsOCHzxo6SSMSswCeIw4HTrXEi6qL3IRzATl83jl09JVO1djBqPSwfjgnpVHNLYcaMbaDX5WB/pg==",
"peerDependencies": {
"vite": "^2 || ^3 || ^4 || ^5"
}
},
"node_modules/vitest": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/vitest/-/vitest-1.6.0.tgz",

View File

@ -47,8 +47,8 @@
"@types/chrome": "^0.0.263",
"asmcrypto.js": "2.3.2",
"axios": "^1.7.7",
"buffer": "6.0.3",
"bcryptjs": "2.4.3",
"buffer": "6.0.3",
"chokidar": "^3.6.0",
"compressorjs": "^1.2.1",
"cordova-plugin-android-permissions": "^1.1.5",
@ -83,7 +83,9 @@
"slate": "^0.103.0",
"slate-react": "^0.109.0",
"tippy.js": "^6.3.7",
"tiptap-extension-resize-image": "^1.1.8"
"tiptap-extension-resize-image": "^1.1.8",
"vite-plugin-top-level-await": "^1.4.4",
"vite-plugin-wasm": "^3.3.0"
},
"devDependencies": {
"@testing-library/dom": "^10.3.0",

View File

@ -1032,7 +1032,7 @@ function App() {
.sendMessage("decryptWallet", {
password: authenticatePassword,
wallet: rawWallet,
})
}, 120000)
.then((response) => {
if (response && !response.error) {
setAuthenticatePassword("");

View File

@ -19,6 +19,7 @@ import {
encryptSingle,
objectToBase64,
} from "./qdn/encryption/group-encryption";
import ChatComputePowWorker from './chatComputePow.worker.js?worker';
import { reusableGet } from "./qdn/publish/pubish";
import { signChat } from "./transactions/signChat";
import { createTransaction } from "./transactions/transactions";
@ -410,6 +411,30 @@ async function checkWebviewFocus() {
window.addEventListener("message", handleMessage);
});
}
const worker = new ChatComputePowWorker()
export async function performPowTask(chatBytes, difficulty) {
return new Promise((resolve, reject) => {
worker.onmessage = (e) => {
if (e.data.error) {
reject(new Error(e.data.error));
} else {
resolve(e.data);
}
};
worker.onerror = (err) => {
reject(err);
};
// Send the task to the worker
worker.postMessage({
chatBytes,
path: `${import.meta.env.BASE_URL}memory-pow.wasm.full`,
difficulty,
});
});
}
function playNotificationSound() {
// chrome.runtime.sendMessage({ action: "PLAY_NOTIFICATION_SOUND" });
@ -1399,7 +1424,6 @@ async function sendChatForBuyOrder({ qortAddress, recipientPublicKey, message, a
};
const balance = await getBalanceInfo();
const hasEnoughBalance = +balance < 4 ? false : true;
const difficulty = 8;
const jsonData = {
addresses: message.addresses,
foreignKey: message.foreignKey,
@ -1460,11 +1484,11 @@ async function sendChatForBuyOrder({ qortAddress, recipientPublicKey, message, a
}
const path = `${import.meta.env.BASE_URL}memory-pow.wasm.full`;
const { nonce, chatBytesArray } = await computePow({
chatBytes: tx.chatBytes,
path,
difficulty,
});
const chatBytes = tx.chatBytes;
const difficulty = 8;
const { nonce, chatBytesArray } = await performPowTask(chatBytes, difficulty);
let _response = await signChatFunc(
chatBytesArray,
nonce,
@ -1477,6 +1501,9 @@ async function sendChatForBuyOrder({ qortAddress, recipientPublicKey, message, a
return _response;
}
export async function sendChatGroup({
groupId,
typeMessage,
@ -1497,7 +1524,7 @@ export async function sendChatGroup({
};
// const balance = await getBalanceInfo();
// const hasEnoughBalance = +balance < 4 ? false : true;
const difficulty = 8;
const txBody = {
timestamp: Date.now(),
@ -1520,13 +1547,12 @@ export async function sendChatGroup({
// if (!hasEnoughBalance) {
// throw new Error("Must have at least 4 QORT to send a chat message");
// }
const path = `${import.meta.env.BASE_URL}memory-pow.wasm.full`;
const { nonce, chatBytesArray } = await computePow({
chatBytes: tx.chatBytes,
path,
difficulty,
});
const chatBytes = tx.chatBytes;
const difficulty = 8;
const { nonce, chatBytesArray } = await performPowTask(chatBytes, difficulty);
let _response = await signChatFunc(chatBytesArray, nonce, null, keyPair);
if (_response?.error) {
throw new Error(_response?.message);
@ -1572,7 +1598,6 @@ export async function sendChatDirect({
// const balance = await getBalanceInfo();
// const hasEnoughBalance = +balance < 4 ? false : true;
const difficulty = 8;
const finalJson = {
message: messageText,
@ -1600,13 +1625,10 @@ export async function sendChatDirect({
// if (!hasEnoughBalance) {
// throw new Error("Must have at least 4 QORT to send a chat message");
// }
const path = `${import.meta.env.BASE_URL}memory-pow.wasm.full`;
const { nonce, chatBytesArray } = await computePow({
chatBytes: tx.chatBytes,
path,
difficulty,
});
const chatBytes = tx.chatBytes;
const difficulty = 8;
const { nonce, chatBytesArray } = await performPowTask(chatBytes, difficulty);
let _response = await signChatFunc(chatBytesArray, nonce, null, keyPair);
if (_response?.error) {

View File

@ -0,0 +1,107 @@
import { Sha256 } from 'asmcrypto.js';
import wasmInit from './memory-pow.wasm?init';
let compute; // Exported compute function from Wasm
let memory; // WebAssembly.Memory instance
let heap; // Uint8Array view of the memory buffer
let brk = 512 * 1024; // Initial brk set to 512 KiB
const allocations = new Map(); // Track allocations by pointer
let workBufferPtr = null; // Reuse work buffer
const workBufferLength = 8 * 1024 * 1024; // 8 MiB
// Load the WebAssembly module
async function loadWasm() {
try {
memory = new WebAssembly.Memory({ initial: 256, maximum: 256 }); // 16 MiB
heap = new Uint8Array(memory.buffer);
const importObject = {
env: {
memory, // Pass memory to Wasm
abort: () => { throw new Error('Wasm abort called'); }, // Handle abort calls from Wasm
},
};
const wasmModule = await wasmInit(importObject);
compute = wasmModule.exports.compute2;
console.log('Wasm loaded successfully:', compute);
} catch (error) {
console.error('Error loading Wasm:', error);
throw error;
}
}
// Memory allocation function
function sbrk(size) {
const old = brk;
// If a previous allocation exists for this size, reuse it
if (allocations.has(size)) {
return allocations.get(size);
}
brk += size;
// Grow memory if needed
if (brk > memory.buffer.byteLength) {
const pagesNeeded = Math.ceil((brk - memory.buffer.byteLength) / (64 * 1024)); // 64 KiB per page
console.log(`Growing memory by ${pagesNeeded} pages`);
try {
memory.grow(pagesNeeded);
heap = new Uint8Array(memory.buffer); // Update heap view
} catch (e) {
console.error('Failed to grow memory:', e);
throw new RangeError('WebAssembly.Memory.grow(): Maximum memory size exceeded');
}
}
allocations.set(size, old); // Track the allocation
return old;
}
// Proof-of-Work computation function
async function computePow(chatBytes, difficulty) {
if (!compute) {
throw new Error('WebAssembly module not initialized. Call loadWasm first.');
}
const chatBytesArray = Uint8Array.from(Object.values(chatBytes));
const chatBytesHash = new Sha256().process(chatBytesArray).finish().result;
// Allocate memory for the hash
const hashPtr = sbrk(32);
const hashAry = new Uint8Array(memory.buffer, hashPtr, 32);
hashAry.set(chatBytesHash);
// Reuse the work buffer if already allocated
if (!workBufferPtr) {
workBufferPtr = sbrk(workBufferLength);
}
console.log('Starting POW computation...');
const nonce = compute(hashPtr, workBufferPtr, workBufferLength, difficulty);
console.log('POW computation finished.');
return { nonce, chatBytesArray };
}
// Worker event listener
self.addEventListener('message', async (e) => {
const { chatBytes, difficulty } = e.data;
try {
// Initialize Wasm if not already done
if (!compute) {
await loadWasm();
}
// Perform the POW computation
const result = await computePow(chatBytes, difficulty);
// Send the result back to the main thread
self.postMessage(result);
} catch (error) {
console.error('Error in worker:', error);
self.postMessage({ error: error.message });
}
});

BIN
src/memory-pow.wasm Normal file

Binary file not shown.

View File

@ -13,7 +13,8 @@ import {
sendQortFee,
sendCoin as sendCoinFunc,
isUsingLocal,
createBuyOrderTx
createBuyOrderTx,
performPowTask
} from "../background";
import { getNameInfo } from "../backgroundFunctions/encryption";
import { showSaveFilePicker } from "../components/Apps/useQortalMessageListener";
@ -1066,7 +1067,6 @@ export const sendChatMessage = async (data, isFromExtension) => {
publicKey: uint8PublicKey,
};
const difficulty = 8;
const tx = await createTransaction(18, keyPair, {
timestamp: sendTimestamp,
recipient: recipient,
@ -1078,15 +1078,13 @@ export const sendChatMessage = async (data, isFromExtension) => {
isEncrypted: 1,
isText: 1,
});
const path = `${import.meta.env.BASE_URL}memory-pow.wasm.full`;
const { nonce, chatBytesArray } = await computePow({
chatBytes: tx.chatBytes,
path,
difficulty,
});
const chatBytes = tx.chatBytes;
const difficulty = 8;
const { nonce, chatBytesArray } = await performPowTask(chatBytes, difficulty);
let _response = await signChatFunc(chatBytesArray, nonce, null, keyPair);
if (_response?.error) {
throw new Error(_response?.message);
@ -1106,7 +1104,6 @@ export const sendChatMessage = async (data, isFromExtension) => {
publicKey: uint8PublicKey,
};
const difficulty = 8;
const txBody = {
timestamp: Date.now(),
@ -1125,14 +1122,11 @@ export const sendChatMessage = async (data, isFromExtension) => {
// if (!hasEnoughBalance) {
// throw new Error("Must have at least 4 QORT to send a chat message");
// }
const path = `${import.meta.env.BASE_URL}memory-pow.wasm.full`;
const { nonce, chatBytesArray } = await computePow({
chatBytes: tx.chatBytes,
path,
difficulty,
});
const chatBytes = tx.chatBytes;
const difficulty = 8;
const { nonce, chatBytesArray } = await performPowTask(chatBytes, difficulty);
let _response = await signChatFunc(chatBytesArray, nonce, null, keyPair);
if (_response?.error) {
throw new Error(_response?.message);

Binary file not shown.

View File

@ -3,18 +3,21 @@ import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
// Import path module for resolving file paths
import fixReactVirtualized from 'esbuild-plugin-react-virtualized'
import wasm from 'vite-plugin-wasm';
import topLevelAwait from 'vite-plugin-top-level-await';
export default defineConfig({
test: {
environment: 'jsdom',
globals: true,
setupFiles: ['./src/test/setup.ts']
},
plugins: [react()],
assetsInclude: ['**/*.wasm'],
plugins: [react(), wasm(), topLevelAwait()],
optimizeDeps: {
esbuildOptions: {
plugins: [fixReactVirtualized],
},
},
}
}
});