Compare commits

...

69 Commits

Author SHA1 Message Date
semantic-release-bot b757ef5d7c chore(release): 0.1.0-develop.18 [skip ci]
# [0.1.0-develop.18](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.17...v0.1.0-develop.18) (2023-11-18)
2023-11-18 14:22:37 +00:00
Derrick Hammer c071eb4eb7
Merge remote-tracking branch 'origin/develop' into develop 2023-11-18 09:21:45 -05:00
Derrick Hammer ea2530a22e
dep: update kernel module hash 2023-11-18 09:21:42 -05:00
semantic-release-bot b116388896 chore(release): 0.1.0-develop.17 [skip ci]
# [0.1.0-develop.17](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.16...v0.1.0-develop.17) (2023-11-04)

### Bug Fixes

* correct handling of error code ([312f8ab](312f8ab43a))
* need to call sendAuthUpdate ([31cabc6](31cabc6c0a))
* need to use try/catch not if ([4e0021e](4e0021ef23))
2023-11-04 07:35:49 +00:00
Derrick Hammer 3fc08a1e84
Merge remote-tracking branch 'origin/develop' into develop 2023-11-04 03:34:54 -04:00
Derrick Hammer 31cabc6c0a
fix: need to call sendAuthUpdate 2023-11-04 03:34:46 -04:00
Derrick Hammer 4e0021ef23
fix: need to use try/catch not if 2023-11-04 03:34:37 -04:00
Derrick Hammer 312f8ab43a
fix: correct handling of error code 2023-11-04 03:34:21 -04:00
semantic-release-bot e1dc79c0dd chore(release): 0.1.0-develop.16 [skip ci]
# [0.1.0-develop.16](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.15...v0.1.0-develop.16) (2023-11-04)

### Bug Fixes

* call savePortalSessions before kernel load ([c5f7059](c5f7059161))
* need to add indexeddb detection due to browser security ([cd7ad86](cd7ad86869))
2023-11-04 07:16:58 +00:00
Derrick Hammer 9bfbc3a145
Merge remote-tracking branch 'origin/develop' into develop 2023-11-04 03:15:48 -04:00
Derrick Hammer a3f12abb78
dep: update kernel module hash 2023-11-04 03:15:42 -04:00
Derrick Hammer cd7ad86869
fix: need to add indexeddb detection due to browser security 2023-11-04 03:04:34 -04:00
Derrick Hammer c5f7059161
fix: call savePortalSessions before kernel load 2023-10-19 17:12:24 -04:00
Derrick Hammer 760cade655
dep: update libkernel 2023-10-19 17:11:58 -04:00
semantic-release-bot 94fa4f59a6 chore(release): 0.1.0-develop.15 [skip ci]
# [0.1.0-develop.15](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.14...v0.1.0-develop.15) (2023-10-11)
2023-10-11 14:34:16 +00:00
Derrick Hammer e31b86fdf8
Merge remote-tracking branch 'origin/develop' into develop 2023-10-11 10:32:40 -04:00
Derrick Hammer f00738e609
dep: update kernel module hash 2023-10-11 10:32:33 -04:00
semantic-release-bot edc6d75033 chore(release): 0.1.0-develop.14 [skip ci]
# [0.1.0-develop.14](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.13...v0.1.0-develop.14) (2023-09-20)

### Bug Fixes

* add check for new worker domains and ignore any origins from them ([4410def](4410def18c))
2023-09-20 01:48:11 +00:00
Derrick Hammer f8ca0c7afe
Merge remote-tracking branch 'origin/develop' into develop 2023-09-19 21:47:06 -04:00
Derrick Hammer e1699bc2d0
dep: update kernel module hash 2023-09-19 21:47:02 -04:00
Derrick Hammer 4410def18c
fix: add check for new worker domains and ignore any origins from them 2023-09-19 21:46:32 -04:00
semantic-release-bot 31a2008d29 chore(release): 0.1.0-develop.13 [skip ci]
# [0.1.0-develop.13](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.12...v0.1.0-develop.13) (2023-09-16)
2023-09-16 21:09:32 +00:00
Derrick Hammer 8e1bd1a200
Merge remote-tracking branch 'origin/develop' into develop 2023-09-16 17:08:26 -04:00
Derrick Hammer f3614350a8
dep: update kernel hash 2023-09-16 17:08:21 -04:00
semantic-release-bot e694eb1ed3 chore(release): 0.1.0-develop.12 [skip ci]
# [0.1.0-develop.12](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.11...v0.1.0-develop.12) (2023-09-11)

### Bug Fixes

* prevent loadKernel from running twice ([1329712](1329712573))
* save portals after booting, but before executing the kernel ([1f3cb20](1f3cb20bd4))
2023-09-11 09:58:30 +00:00
Derrick Hammer 91762070e4
Merge remote-tracking branch 'origin/develop' into develop 2023-09-11 05:56:54 -04:00
Derrick Hammer 1f3cb20bd4
fix: save portals after booting, but before executing the kernel 2023-09-11 05:56:40 -04:00
Derrick Hammer 1329712573
fix: prevent loadKernel from running twice 2023-09-11 05:56:03 -04:00
semantic-release-bot dba5089a56 chore(release): 0.1.0-develop.11 [skip ci]
# [0.1.0-develop.11](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.10...v0.1.0-develop.11) (2023-09-09)

### Bug Fixes

* set method in response ([167dbe5](167dbe5c34))
2023-09-09 13:23:50 +00:00
Derrick Hammer 341e5ef128
Merge remote-tracking branch 'origin/develop' into develop 2023-09-09 09:22:45 -04:00
Derrick Hammer 167dbe5c34
fix: set method in response 2023-09-09 09:22:40 -04:00
semantic-release-bot 7f64b8331b chore(release): 0.1.0-develop.10 [skip ci]
# [0.1.0-develop.10](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.9...v0.1.0-develop.10) (2023-09-09)
2023-09-09 12:04:55 +00:00
Derrick Hammer 46125cd893
Merge remote-tracking branch 'origin/develop' into develop 2023-09-09 08:03:55 -04:00
Derrick Hammer 6b7d407153
dep: update kernel hash 2023-09-09 08:03:50 -04:00
semantic-release-bot 58d7ca2b9c chore(release): 0.1.0-develop.9 [skip ci]
# [0.1.0-develop.9](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.8...v0.1.0-develop.9) (2023-09-08)
2023-09-08 17:59:49 +00:00
Derrick Hammer 30ebcada31
Merge remote-tracking branch 'origin/develop' into develop 2023-09-08 13:58:49 -04:00
Derrick Hammer dc9e4f13e9
refactor: remove ErrTuple pattern 2023-09-08 13:56:42 -04:00
Derrick Hammer 46a71bdfee
dep: update libweb 2023-09-08 13:56:25 -04:00
semantic-release-bot 268bd41579 chore(release): 0.1.0-develop.8 [skip ci]
# [0.1.0-develop.8](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.7...v0.1.0-develop.8) (2023-09-04)
2023-09-04 11:48:16 +00:00
Derrick Hammer a9e3003929
Merge remote-tracking branch 'origin/develop' into develop 2023-09-04 07:47:23 -04:00
Derrick Hammer 0eac4ae819
dep: update kernel hash 2023-09-04 07:47:04 -04:00
semantic-release-bot d8cbace466 chore(release): 0.1.0-develop.7 [skip ci]
# [0.1.0-develop.7](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.6...v0.1.0-develop.7) (2023-09-04)
2023-09-04 09:13:13 +00:00
Derrick Hammer 849e1f6bac
Merge remote-tracking branch 'origin/develop' into develop 2023-09-04 05:12:07 -04:00
Derrick Hammer c34e112610
dep: update kernel hash 2023-09-04 04:49:48 -04:00
semantic-release-bot 31118808c0 chore(release): 0.1.0-develop.6 [skip ci]
# [0.1.0-develop.6](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.5...v0.1.0-develop.6) (2023-09-03)
2023-09-03 06:41:48 +00:00
Derrick Hammer c101e80e19
Merge remote-tracking branch 'origin/develop' into develop 2023-09-03 02:40:49 -04:00
Derrick Hammer 16dcb1f147
dep: update kernel hash 2023-09-03 02:40:42 -04:00
semantic-release-bot a6198aa5f7 chore(release): 0.1.0-develop.5 [skip ci]
# [0.1.0-develop.5](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.4...v0.1.0-develop.5) (2023-09-02)
2023-09-02 17:10:52 +00:00
Derrick Hammer c429adccbd
Merge remote-tracking branch 'origin/develop' into develop 2023-09-02 13:09:56 -04:00
Derrick Hammer dab3d5e246
dep: update kernel hash 2023-09-02 13:09:52 -04:00
Derrick Hammer 1227845e7d
dep: update kernel hash 2023-09-02 09:59:26 -04:00
semantic-release-bot a38f731d0c chore(release): 0.1.0-develop.4 [skip ci]
# [0.1.0-develop.4](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.3...v0.1.0-develop.4) (2023-08-10)
2023-08-10 07:16:25 +00:00
Derrick Hammer 5c477ffb50
Merge remote-tracking branch 'origin/develop' into develop 2023-08-10 03:15:30 -04:00
Derrick Hammer e1f040f0b7
refactor: switch to using downloadSmallObject 2023-08-10 03:15:24 -04:00
Derrick Hammer 91fb8546e9
dep: update kernel hash 2023-08-10 03:14:41 -04:00
Derrick Hammer b66f8bbdc6
dep: update kernel hash 2023-08-10 03:09:48 -04:00
semantic-release-bot cd9743c023 chore(release): 0.1.0-develop.3 [skip ci]
# [0.1.0-develop.3](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.2...v0.1.0-develop.3) (2023-08-10)
2023-08-10 07:08:21 +00:00
Derrick Hammer b6664478b6
Merge remote-tracking branch 'origin/develop' into develop 2023-08-10 03:06:54 -04:00
Derrick Hammer bd483134cc
dep: update kernel hash 2023-08-10 03:06:48 -04:00
semantic-release-bot a5bd7fd001 chore(release): 0.1.0-develop.2 [skip ci]
# [0.1.0-develop.2](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.1...v0.1.0-develop.2) (2023-07-21)
2023-07-21 14:35:10 +00:00
Derrick Hammer 6fa0d49ce1
refactor: port over the encrypted key exchange and set key from extension 2023-07-21 10:33:44 -04:00
Derrick Hammer 97e083fb21
dep: update libweb 2023-07-21 10:19:19 -04:00
Derrick Hammer b1f476b504
Merge remote-tracking branch 'origin/develop' into develop 2023-07-21 09:26:08 -04:00
Derrick Hammer 8d9af2a4c3
dep: update libweb 2023-07-21 09:26:03 -04:00
semantic-release-bot 2b248e3fdd chore(release): 0.1.0-develop.1 [skip ci]
# [0.1.0-develop.1](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.0.1...v0.1.0-develop.1) (2023-07-21)

### Features

* initial version ([3e9e5ed](3e9e5eda97))
2023-07-21 12:35:00 +00:00
Derrick Hammer d7e1b53d86
ci: fix repo url 2023-07-20 15:52:06 -04:00
Derrick Hammer 45b8eddc4b
ci: setup 2023-07-20 15:50:05 -04:00
Derrick Hammer 6a482df40d
chore: update LICENSE 2023-07-20 15:45:41 -04:00
Derrick Hammer 3e9e5eda97
feat: initial version 2023-07-20 15:43:41 -04:00
15 changed files with 20452 additions and 1 deletions

13
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,13 @@
name: Build/Publish
on:
push:
branches:
- master
- develop
- develop-*
jobs:
main:
uses: lumeweb/github-node-deploy-workflow/.github/workflows/main.yml@master
secrets: inherit

23
.presetterrc.json Normal file
View File

@ -0,0 +1,23 @@
{
"preset": [
"@lumeweb/presetter-kernel-module-preset"
],
"config": {
"official": true,
"vite": {
"build": {
"lib": {
"formats": {
"0": "umd"
}
}
}
}
},
"scripts": {
"build": "cross-env NODE_ENV=production run-s clean build:typescript:* build:vite build:html",
"build:vite": "vite build && shx mv lib/*.cjs lib/bootloader.js",
"build:html": "shx cp src/index.html lib/index.html"
}
}

69
CHANGELOG.md Normal file
View File

@ -0,0 +1,69 @@
# [0.1.0-develop.18](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.17...v0.1.0-develop.18) (2023-11-18)
# [0.1.0-develop.17](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.16...v0.1.0-develop.17) (2023-11-04)
### Bug Fixes
* correct handling of error code ([312f8ab](https://git.lumeweb.com/LumeWeb/hosted-kernel/commit/312f8ab43a5f781817e9b899a938719699c197c8))
* need to call sendAuthUpdate ([31cabc6](https://git.lumeweb.com/LumeWeb/hosted-kernel/commit/31cabc6c0a00ebb4e9379c4d81d7e4990457f2d3))
* need to use try/catch not if ([4e0021e](https://git.lumeweb.com/LumeWeb/hosted-kernel/commit/4e0021ef23f47ebc53b065b6221190786ccb6573))
# [0.1.0-develop.16](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.15...v0.1.0-develop.16) (2023-11-04)
### Bug Fixes
* call savePortalSessions before kernel load ([c5f7059](https://git.lumeweb.com/LumeWeb/hosted-kernel/commit/c5f7059161223dfd96a718109bfb3b396539767b))
* need to add indexeddb detection due to browser security ([cd7ad86](https://git.lumeweb.com/LumeWeb/hosted-kernel/commit/cd7ad86869655cc6dbbe9078c49a295efd435b22))
# [0.1.0-develop.15](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.14...v0.1.0-develop.15) (2023-10-11)
# [0.1.0-develop.14](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.13...v0.1.0-develop.14) (2023-09-20)
### Bug Fixes
* add check for new worker domains and ignore any origins from them ([4410def](https://git.lumeweb.com/LumeWeb/hosted-kernel/commit/4410def18c03af1dfbd7271ce6a234331dfa4615))
# [0.1.0-develop.13](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.12...v0.1.0-develop.13) (2023-09-16)
# [0.1.0-develop.12](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.11...v0.1.0-develop.12) (2023-09-11)
### Bug Fixes
* prevent loadKernel from running twice ([1329712](https://git.lumeweb.com/LumeWeb/hosted-kernel/commit/132971257354b73cbc6f1185dfaebaa4c9f925d3))
* save portals after booting, but before executing the kernel ([1f3cb20](https://git.lumeweb.com/LumeWeb/hosted-kernel/commit/1f3cb20bd424ae07bff97e9c5d79186ad533a349))
# [0.1.0-develop.11](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.10...v0.1.0-develop.11) (2023-09-09)
### Bug Fixes
* set method in response ([167dbe5](https://git.lumeweb.com/LumeWeb/hosted-kernel/commit/167dbe5c342277c1003c5c9538c52d4e70770170))
# [0.1.0-develop.10](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.9...v0.1.0-develop.10) (2023-09-09)
# [0.1.0-develop.9](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.8...v0.1.0-develop.9) (2023-09-08)
# [0.1.0-develop.8](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.7...v0.1.0-develop.8) (2023-09-04)
# [0.1.0-develop.7](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.6...v0.1.0-develop.7) (2023-09-04)
# [0.1.0-develop.6](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.5...v0.1.0-develop.6) (2023-09-03)
# [0.1.0-develop.5](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.4...v0.1.0-develop.5) (2023-09-02)
# [0.1.0-develop.4](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.3...v0.1.0-develop.4) (2023-08-10)
# [0.1.0-develop.3](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.2...v0.1.0-develop.3) (2023-08-10)
# [0.1.0-develop.2](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.1.0-develop.1...v0.1.0-develop.2) (2023-07-21)
# [0.1.0-develop.1](https://git.lumeweb.com/LumeWeb/hosted-kernel/compare/v0.0.1...v0.1.0-develop.1) (2023-07-21)
### Features
* initial version ([3e9e5ed](https://git.lumeweb.com/LumeWeb/hosted-kernel/commit/3e9e5eda97cfc05f88575d1240f56fbf8543984c))

View File

@ -1,6 +1,7 @@
MIT License
Copyright (c) 2023 LumeWeb
Copyright (c) 2023 Hammer Technologies LLC
Copyright (c) 2022 Skynet Labs
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

19830
npm-shrinkwrap.json generated Normal file

File diff suppressed because it is too large Load Diff

23
package.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "@lumeweb/hosted-kernel",
"version": "0.1.0-develop.18",
"type": "module",
"readme": "ERROR: No README data found!",
"repository": {
"type": "git",
"url": "gitea@git.lumeweb.com:LumeWeb/hosted-kernel.git"
},
"scripts": {
"prepare": "presetter bootstrap",
"build": "run build",
"semantic-release": "semantic-release"
},
"devDependencies": {
"@lumeweb/presetter-kernel-module-preset": "^0.1.0-develop.43"
},
"dependencies": {
"@lumeweb/libkernel": "^0.1.0-develop.67",
"@noble/ciphers": "^0.1.4",
"binconv": "^0.2.0"
}
}

13
src/index.html Normal file
View File

@ -0,0 +1,13 @@
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript" src="./bootloader.js"></script>
</body>
</html>

12
src/index.ts Normal file
View File

@ -0,0 +1,12 @@
import { boot } from "./kernel.js";
import { handleIncomingMessage } from "./messages.js";
document.title = "Hosted Lume Kernel";
let header = document.createElement("h1");
header.textContent =
"Something went wrong! You should not be visiting this page, this page should only be accessed via an invisible iframe.";
document.body.appendChild(header);
window.addEventListener("message", handleIncomingMessage);
boot();

166
src/kernel.ts Normal file
View File

@ -0,0 +1,166 @@
import {
downloadSmallObject,
maybeInitDefaultPortals,
savePortals,
savePortalSessions,
setActivePortalMasterKey,
} from "@lumeweb/libweb";
import { log, logErr, sendAuthUpdate } from "./util.js";
import {
defaultKernelLink,
setKernelLoaded,
setLoginComplete,
} from "./vars.js";
import { getStoredUserKey } from "./storage.js";
import { readableStreamToBlob } from "binconv";
import { addContextToErr } from "@lumeweb/libkernel";
let kernelLoadAttempt = false;
function testIndexedDBSupport() {
return new Promise((resolve, reject) => {
// Check for IndexedDB support
if (!("indexedDB" in window)) {
reject(new Error("IndexedDB is not supported by this browser."));
} else {
// Attempt to open an IndexedDB database
const request = indexedDB.open("test_db", 1);
request.onerror = function (event) {
// Error occurred, reject the promise
reject(
// @ts-ignore
new Error("IndexedDB test error: " + event?.target?.error?.code),
);
};
request.onsuccess = function (event) {
// Success, resolve the promise
// @ts-ignore
event?.target?.result.close(); // Close the DB when done
resolve(true);
};
request.onupgradeneeded = function (event: IDBVersionChangeEvent) {
// Database needs to be created or upgraded
// @ts-ignore
const db = event?.target?.result;
if (!db.objectStoreNames.contains("test_store")) {
// Create an object store
db.createObjectStore("test_store", { keyPath: "id" });
}
};
}
});
}
export async function boot() {
let userKey;
try {
await testIndexedDBSupport();
} catch {
setKernelLoaded("indexeddb_error");
sendAuthUpdate();
logErr("indexed db is not supported or is blocked");
return;
}
try {
userKey = getStoredUserKey();
} catch (e) {
logErr(addContextToErr(e, "user key could not be fetched"));
sendAuthUpdate();
return;
}
if (!userKey) {
sendAuthUpdate();
return;
}
log("user is already logged in, attempting to load kernel");
setActivePortalMasterKey(userKey);
setLoginComplete(true);
sendAuthUpdate();
loadKernel();
}
export async function loadKernel() {
if (kernelLoadAttempt) {
return;
}
kernelLoadAttempt = true;
try {
maybeInitDefaultPortals();
} catch (e) {
let err = addContextToErr(e, "unable to init portals");
setKernelLoaded(err);
logErr(err);
sendAuthUpdate();
return;
}
let kernelCode;
try {
kernelCode = await downloadDefaultKernel();
} catch (e) {
let extErr = addContextToErr(e, "unable to download kernel");
setKernelLoaded(extErr);
logErr(extErr);
sendAuthUpdate();
return;
}
savePortals();
savePortalSessions();
try {
await new Promise(async (resolve, reject) => {
const url = URL.createObjectURL(await readableStreamToBlob(kernelCode));
const el = document.createElement("script");
el.src = url;
el.onload = () => {
URL.revokeObjectURL(url);
resolve(null);
};
el.onerror = (
event: Event | string,
source?: string,
lineno?: number,
colno?: number,
error?: Error,
) => {
URL.revokeObjectURL(url);
reject(error);
};
document.head.appendChild(el);
});
setKernelLoaded("success");
sendAuthUpdate();
log("kernel successfully loaded");
return;
} catch (err: any) {
let extErr = addContextToErr(err, "unable to eval kernel");
setKernelLoaded(extErr);
logErr(extErr);
logErr(err.toString());
console.error(extErr);
console.error(err);
sendAuthUpdate();
return;
}
}
async function downloadKernel(kernelCid: string): Promise<ReadableStream> {
return await downloadSmallObject(kernelCid);
}
function downloadDefaultKernel(): Promise<ReadableStream> {
return downloadKernel(defaultKernelLink);
}

81
src/messages.ts Normal file
View File

@ -0,0 +1,81 @@
import exchangeCommunicationKeys from "./messages/exchangeCommunicationKeys.js";
import setLoginKey from "./messages/setLoginKey.js";
const kernelMessageHandlers = {
exchangeCommunicationKeys,
setLoginKey,
};
export async function handleIncomingMessage(event: MessageEvent) {
if (event.source === null) {
return;
}
if (event.source === window) {
return;
}
if (event.origin.endsWith(".module.kernel.lumeweb.com")) {
return;
}
if (!("nonce" in event.data)) {
(event.source as WindowProxy).postMessage(
{
nonce: "N/A",
method: "response",
err: "message sent to kernel with no nonce",
},
event.origin,
);
return;
}
if (!("method" in event.data)) {
(event.source as WindowProxy).postMessage(
{
nonce: event.data.nonce,
method: "response",
err: "message sent to kernel with no method",
},
event.origin,
);
return;
}
if (event.data.method in kernelMessageHandlers) {
let response;
try {
response = await kernelMessageHandlers[event.data.method](
event.data.data,
);
} catch (e: any) {
response = { err: (e as Error).message };
}
(event.source as WindowProxy).postMessage(
{
nonce: event.data.nonce,
data: response,
method: "response",
},
event.origin,
);
return;
}
if (["moduleCall", "response"].includes(event.data.method)) {
return;
}
(event.source as WindowProxy).postMessage(
{
nonce: event.data.nonce,
method: "response",
err:
"unrecognized method (user may need to log in): " + event.data.method,
},
event.origin,
);
return;
}

View File

@ -0,0 +1,12 @@
import { bytesToHex, hexToBytes } from "@lumeweb/libweb";
import {
getCommunicationPubKey,
setFrontendCommunicationPubkey,
} from "../vars.js";
import { log } from "../util.js";
export default function (data: any) {
setFrontendCommunicationPubkey(hexToBytes(data));
return bytesToHex(getCommunicationPubKey());
}

View File

@ -0,0 +1,26 @@
import { secretbox } from "@noble/ciphers/salsa";
import { x25519 } from "@noble/curves/ed25519";
import {
getCommunicationKey,
getFrontendCommunicationPubkey,
setLoginComplete,
} from "../vars.js";
import { saveUserKey } from "../storage.js";
import { hexToBytes } from "@lumeweb/libweb";
export default function (data: any) {
const box = secretbox(
x25519.getSharedSecret(
getCommunicationKey(),
getFrontendCommunicationPubkey(),
),
hexToBytes(data.nonce),
);
const decryptedData = box.open(hexToBytes(data.data));
setLoginComplete(false);
saveUserKey(decryptedData);
return true;
}

75
src/storage.ts Normal file
View File

@ -0,0 +1,75 @@
import {
bytesToHex,
hexToBytes,
setActivePortalMasterKey,
} from "@lumeweb/libweb";
import { addContextToErr } from "@lumeweb/libkernel";
import {
getLoginComplete,
getLogoutComplete,
setLoginComplete,
setLogoutComplete,
} from "./vars.js";
import { log, logErr, reloadKernel, sendAuthUpdate } from "./util.js";
import { loadKernel } from "./kernel.js";
function handleStorage(event: StorageEvent) {
if (event.key !== null && event.key !== "key") {
return;
}
if (getLogoutComplete()) {
reloadKernel();
return;
}
if (event.key === null && getLoginComplete()) {
return;
}
if (event.key === "key" && !getLoginComplete()) {
let userKey;
try {
userKey = getStoredUserKey();
} catch (e) {
logErr(addContextToErr(e, "user key could not be fetched"));
sendAuthUpdate();
return;
}
if (userKey === null) {
sendAuthUpdate();
return;
}
log("user is now logged in, attempting to load kernel");
setActivePortalMasterKey(userKey);
setLoginComplete(true);
loadKernel();
sendAuthUpdate();
return;
}
setLogoutComplete(true);
sendAuthUpdate();
log("attempting to do a page reload");
reloadKernel();
}
window.addEventListener("storage", handleStorage);
export function saveUserKey(key: Uint8Array) {
window.localStorage.setItem("key", bytesToHex(key));
const event = new StorageEvent("storage", {
key: "key",
});
window.dispatchEvent(event);
}
export function getStoredUserKey() {
const key = window.localStorage.getItem("key");
if (key) {
return hexToBytes(key);
}
return null;
}

53
src/util.ts Normal file
View File

@ -0,0 +1,53 @@
import {
getKernelLoaded,
getLoginComplete,
getLogoutComplete,
} from "./vars.js";
import { objAsString } from "@lumeweb/libkernel";
export function sendAuthUpdate() {
window.parent.postMessage(
{
method: "kernelAuthStatus",
data: {
loginComplete: getLoginComplete(),
kernelLoaded: getKernelLoaded(),
logoutComplete: getLogoutComplete(),
},
},
"*",
);
}
function bootloaderWLog(isErr: boolean, ...inputs: any) {
// Build the message, each item gets its own line. We do this because items
// are often full objects.
let message = "[lumeweb-kernel-bootloader]";
for (let i = 0; i < inputs.length; i++) {
message += "\n";
message += objAsString(inputs[i]);
}
// Create the log by sending it to the parent.
window.parent.postMessage(
{
method: "log",
data: {
isErr,
message,
},
},
"*",
);
}
export function log(...inputs: any) {
bootloaderWLog(false, ...inputs);
}
export function logErr(...inputs: any) {
bootloaderWLog(true, ...inputs);
}
export function reloadKernel() {
window.location.reload();
}

54
src/vars.ts Normal file
View File

@ -0,0 +1,54 @@
import { x25519 } from "@noble/curves/ed25519";
export const defaultKernelLink =
"z2H6zWHeWkVFirZ8jCzaPgSXqN9AqQFzQiBC2i4cnKnhwYynrrmA";
const store = new Map<string, any>(
Object.entries({
loginComplete: false,
logoutComplete: false,
kernelLoaded: "not yet",
communicationKey: null,
frontendCommunicationPubKey: null,
}),
);
export function setLoginComplete(status: boolean) {
store.set("loginComplete", status);
}
export function getLoginComplete(): boolean {
return store.get("loginComplete");
}
export function setLogoutComplete(status: boolean) {
store.set("logoutComplete", status);
}
export function getLogoutComplete(): boolean {
return store.get("logoutComplete");
}
export function setKernelLoaded(status: string) {
store.set("kernelLoaded", status);
}
export function getKernelLoaded(): string {
return store.get("kernelLoaded");
}
export function getCommunicationKey(): Uint8Array {
if (!store.get("communicationKey")) {
store.set("communicationKey", x25519.utils.randomPrivateKey());
}
return store.get("communicationKey");
}
export function getCommunicationPubKey() {
return x25519.getPublicKey(getCommunicationKey());
}
export function getFrontendCommunicationPubkey(): Uint8Array {
return store.get("frontendCommunicationPubKey");
}
export function setFrontendCommunicationPubkey(key: Uint8Array) {
store.set("frontendCommunicationPubKey", key);
}