pico-engine 1.4.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +188 -0
- package/dist/RulesetRegistryLoaderFs.js +2 -0
- package/dist/auth/AuthService.js +601 -0
- package/dist/auth/index.js +8 -0
- package/dist/auth/webauthn.js +97 -0
- package/dist/bundledKrl.js +42 -0
- package/dist/cli.js +11 -0
- package/dist/identity/IdentityService.js +340 -0
- package/dist/identity/crossPico.js +238 -0
- package/dist/identity/dbHelpers.js +67 -0
- package/dist/identity/didcommIngressRoutes.js +44 -0
- package/dist/identity/didoModule.js +252 -0
- package/dist/identity/errors.js +11 -0
- package/dist/identity/establishSubscription.js +97 -0
- package/dist/identity/index.js +15 -0
- package/dist/identity/peerDid.js +9 -0
- package/dist/identity/skyIntro.js +194 -0
- package/dist/identity/skyProtocol.js +105 -0
- package/dist/identity/store.js +233 -0
- package/dist/identity/types.js +13 -0
- package/dist/identity/veramoDidComm.js +260 -0
- package/dist/identity/webvh.js +266 -0
- package/dist/identity/webvhCrypto.js +128 -0
- package/dist/identity/webvhRoutes.js +61 -0
- package/dist/index.js +100 -44
- package/dist/oauth/OAuthService.js +388 -0
- package/dist/oauth/acg.js +495 -0
- package/dist/oauth/channelEligibility.js +26 -0
- package/dist/oauth/errors.js +12 -0
- package/dist/oauth/index.js +21 -0
- package/dist/oauth/krlModule.js +27 -0
- package/dist/oauth/meshOAuth.js +43 -0
- package/dist/provisionRoot.js +118 -0
- package/dist/server.js +609 -15
- package/dist/uiChannelPolicies.js +48 -0
- package/krl/io.picolabs.did-o.krl +5 -1
- package/krl/io.picolabs.oauth.krl +48 -0
- package/krl/io.picolabs.pico-engine-ui.krl +106 -18
- package/krl/io.picolabs.subscription.krl +242 -23
- package/krl/io.picolabs.wrangler.krl +225 -16
- package/package.json +38 -9
- package/public/docs/krl/_sidebar.md +1 -0
- package/public/docs/krl/discovery.md +144 -0
- package/public/docs/krl/modules/ctx.md +8 -0
- package/public/docs/krl/modules/event.md +41 -0
- package/public/pico-engine-ui.css +1 -1
- package/public/pico-engine-ui.js +24 -20
- package/public/pico-labs-logo.png +0 -0
- package/src/RulesetRegistryLoaderFs.ts +2 -0
- package/src/auth/AuthService.ts +850 -0
- package/src/auth/index.ts +17 -0
- package/src/auth/webauthn.ts +182 -0
- package/src/bundledKrl.ts +42 -0
- package/src/cli.ts +11 -0
- package/src/identity/IdentityService.ts +571 -0
- package/src/identity/crossPico.ts +371 -0
- package/src/identity/dbHelpers.ts +51 -0
- package/src/identity/didcommIngressRoutes.ts +54 -0
- package/src/identity/didoModule.ts +388 -0
- package/src/identity/errors.ts +8 -0
- package/src/identity/establishSubscription.ts +160 -0
- package/src/identity/index.ts +24 -0
- package/src/identity/peerDid.ts +22 -0
- package/src/identity/skyIntro.ts +321 -0
- package/src/identity/skyProtocol.ts +192 -0
- package/src/identity/store.ts +343 -0
- package/src/identity/types.ts +63 -0
- package/src/identity/veramoDidComm.ts +363 -0
- package/src/identity/webvh.ts +325 -0
- package/src/identity/webvhCrypto.ts +200 -0
- package/src/identity/webvhRoutes.ts +64 -0
- package/src/index.ts +145 -53
- package/src/oauth/OAuthService.ts +510 -0
- package/src/oauth/acg.ts +666 -0
- package/src/oauth/channelEligibility.ts +26 -0
- package/src/oauth/errors.ts +10 -0
- package/src/oauth/index.ts +14 -0
- package/src/oauth/krlModule.ts +31 -0
- package/src/oauth/meshOAuth.ts +50 -0
- package/src/provisionRoot.ts +151 -0
- package/src/server.ts +700 -24
- package/src/uiChannelPolicies.ts +49 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeWebAuthnAdapter = makeWebAuthnAdapter;
|
|
4
|
+
const server_1 = require("@simplewebauthn/server");
|
|
5
|
+
function makeWebAuthnAdapter() {
|
|
6
|
+
return {
|
|
7
|
+
async generateRegistrationOptions(req) {
|
|
8
|
+
const excludeCredentials = (req.excludeCredentials || []).map((cred) => {
|
|
9
|
+
const id = trimBase64URLPadding(cred.id);
|
|
10
|
+
const transports = (cred.transports || []).filter(Boolean);
|
|
11
|
+
return transports.length > 0 ? { id, transports } : { id };
|
|
12
|
+
});
|
|
13
|
+
return (0, server_1.generateRegistrationOptions)({
|
|
14
|
+
rpName: req.rpName,
|
|
15
|
+
rpID: req.rpID,
|
|
16
|
+
userID: req.userID,
|
|
17
|
+
userName: req.userName,
|
|
18
|
+
userDisplayName: req.userDisplayName,
|
|
19
|
+
attestationType: "none",
|
|
20
|
+
excludeCredentials,
|
|
21
|
+
authenticatorSelection: req.additionalPasskey
|
|
22
|
+
? {
|
|
23
|
+
residentKey: "preferred",
|
|
24
|
+
userVerification: "preferred",
|
|
25
|
+
}
|
|
26
|
+
: {
|
|
27
|
+
residentKey: "required",
|
|
28
|
+
userVerification: "preferred",
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
async verifyRegistration(req) {
|
|
33
|
+
const verification = await (0, server_1.verifyRegistrationResponse)({
|
|
34
|
+
response: req.response,
|
|
35
|
+
expectedChallenge: req.expectedChallenge,
|
|
36
|
+
expectedOrigin: req.expectedOrigin,
|
|
37
|
+
expectedRPID: req.expectedRPID,
|
|
38
|
+
requireUserVerification: false,
|
|
39
|
+
});
|
|
40
|
+
if (!verification.verified || !verification.registrationInfo) {
|
|
41
|
+
return { verified: false };
|
|
42
|
+
}
|
|
43
|
+
const cred = verification.registrationInfo.credential;
|
|
44
|
+
return {
|
|
45
|
+
verified: true,
|
|
46
|
+
credential: {
|
|
47
|
+
id: cred.id,
|
|
48
|
+
publicKey: cred.publicKey,
|
|
49
|
+
counter: cred.counter,
|
|
50
|
+
transports: mergeTransports(cred.transports, req.response),
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
async generateAuthenticationOptions(req) {
|
|
55
|
+
return (0, server_1.generateAuthenticationOptions)({
|
|
56
|
+
rpID: req.rpID,
|
|
57
|
+
userVerification: "preferred",
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
async verifyAuthentication(req) {
|
|
61
|
+
var _a, _b;
|
|
62
|
+
const verification = await (0, server_1.verifyAuthenticationResponse)({
|
|
63
|
+
response: req.response,
|
|
64
|
+
expectedChallenge: req.expectedChallenge,
|
|
65
|
+
expectedOrigin: req.expectedOrigin,
|
|
66
|
+
expectedRPID: req.expectedRPID,
|
|
67
|
+
credential: {
|
|
68
|
+
id: req.credential.id,
|
|
69
|
+
publicKey: req.credential.publicKey,
|
|
70
|
+
counter: req.credential.counter,
|
|
71
|
+
transports: req.credential.transports,
|
|
72
|
+
},
|
|
73
|
+
requireUserVerification: false,
|
|
74
|
+
});
|
|
75
|
+
return {
|
|
76
|
+
verified: verification.verified,
|
|
77
|
+
newCounter: (_b = (_a = verification.authenticationInfo) === null || _a === void 0 ? void 0 : _a.newCounter) !== null && _b !== void 0 ? _b : req.credential.counter,
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function trimBase64URLPadding(id) {
|
|
83
|
+
return id.replace(/=+$/g, "");
|
|
84
|
+
}
|
|
85
|
+
function mergeTransports(fromAttestation, response) {
|
|
86
|
+
const merged = [...(fromAttestation || [])].filter(Boolean);
|
|
87
|
+
if (merged.length > 0) {
|
|
88
|
+
return merged;
|
|
89
|
+
}
|
|
90
|
+
if (response.authenticatorAttachment === "platform") {
|
|
91
|
+
return ["internal"];
|
|
92
|
+
}
|
|
93
|
+
if (response.authenticatorAttachment === "cross-platform") {
|
|
94
|
+
return ["usb", "ble", "nfc"];
|
|
95
|
+
}
|
|
96
|
+
return [];
|
|
97
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BUNDLED_KRL_FILES = void 0;
|
|
4
|
+
exports.bundledKrlDir = bundledKrlDir;
|
|
5
|
+
exports.resolveBundledKrlUrl = resolveBundledKrlUrl;
|
|
6
|
+
const fs = require("fs");
|
|
7
|
+
const path = require("path");
|
|
8
|
+
const toFileUrl_1 = require("./utils/toFileUrl");
|
|
9
|
+
/**
|
|
10
|
+
* OS-layer rulesets shipped inside the pico-engine package. On flush, these
|
|
11
|
+
* always reload from the running engine's krl/ directory so dev edits apply
|
|
12
|
+
* even when the pico was provisioned from a different absolute file:// path.
|
|
13
|
+
*/
|
|
14
|
+
exports.BUNDLED_KRL_FILES = [
|
|
15
|
+
"io.picolabs.pico-engine-ui.krl",
|
|
16
|
+
"io.picolabs.wrangler.krl",
|
|
17
|
+
"io.picolabs.subscription.krl",
|
|
18
|
+
"io.picolabs.pds.krl",
|
|
19
|
+
];
|
|
20
|
+
function bundledKrlDir() {
|
|
21
|
+
return path.resolve(__dirname, "..", "krl");
|
|
22
|
+
}
|
|
23
|
+
function resolveBundledKrlUrl(url) {
|
|
24
|
+
let filename = null;
|
|
25
|
+
try {
|
|
26
|
+
const parsed = new URL(url);
|
|
27
|
+
if (parsed.protocol === "file:") {
|
|
28
|
+
filename = path.basename(parsed.pathname);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch (_a) {
|
|
32
|
+
return url;
|
|
33
|
+
}
|
|
34
|
+
if (!filename || !exports.BUNDLED_KRL_FILES.includes(filename)) {
|
|
35
|
+
return url;
|
|
36
|
+
}
|
|
37
|
+
const localPath = path.join(bundledKrlDir(), filename);
|
|
38
|
+
if (fs.existsSync(localPath)) {
|
|
39
|
+
return (0, toFileUrl_1.toFileUrl)(localPath);
|
|
40
|
+
}
|
|
41
|
+
return url;
|
|
42
|
+
}
|
package/dist/cli.js
CHANGED
|
@@ -23,6 +23,10 @@ Environment Variables:
|
|
|
23
23
|
PORT - The port the http server should listen on. By default it's 3000
|
|
24
24
|
PICO_ENGINE_HOME - Where the database and other files should be stored. By default it's ~/.pico-engine/
|
|
25
25
|
PICO_ENGINE_BASE_URL - The public url prefix to reach this engine. By default it's http://localhost:$PORT
|
|
26
|
+
PICO_ENGINE_ALLOW_SELF_SIGNUP - Set to "true" or "1" to allow anyone to register a new account
|
|
27
|
+
after the engine is bootstrapped. Default is false (login only; bootstrap and invite still work).
|
|
28
|
+
PICO_ENGINE_ALLOW_LOCALHOST_C - Set to "0" to require passkey session on /c/* even from localhost.
|
|
29
|
+
Default (unset) allows localhost without session for in-engine ctx:event HTTP loops. See MEMORY.md.
|
|
26
30
|
`);
|
|
27
31
|
process.exit(0);
|
|
28
32
|
}
|
|
@@ -41,6 +45,13 @@ if (typeof process.env.PICO_ENGINE_BASE_URL === "string" &&
|
|
|
41
45
|
process.env.PICO_ENGINE_BASE_URL.length > 0) {
|
|
42
46
|
env.base_url = process.env.PICO_ENGINE_BASE_URL;
|
|
43
47
|
}
|
|
48
|
+
const allowSelfSignupEnv = process.env.PICO_ENGINE_ALLOW_SELF_SIGNUP;
|
|
49
|
+
if (allowSelfSignupEnv === "true" || allowSelfSignupEnv === "1") {
|
|
50
|
+
env.allowSelfSignup = true;
|
|
51
|
+
}
|
|
52
|
+
else if (allowSelfSignupEnv === "false" || allowSelfSignupEnv === "0") {
|
|
53
|
+
env.allowSelfSignup = false;
|
|
54
|
+
}
|
|
44
55
|
(0, index_1.startEngine)(env).catch((err) => {
|
|
45
56
|
console.error("Failed to start engine.");
|
|
46
57
|
console.error(err);
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IdentityService = void 0;
|
|
4
|
+
const cuid = require("cuid");
|
|
5
|
+
const cross_fetch_1 = require("cross-fetch");
|
|
6
|
+
const errors_1 = require("./errors");
|
|
7
|
+
const webvh_1 = require("./webvh");
|
|
8
|
+
const veramoDidComm_1 = require("./veramoDidComm");
|
|
9
|
+
const establishSubscription_1 = require("./establishSubscription");
|
|
10
|
+
const crossPico_1 = require("./crossPico");
|
|
11
|
+
const skyProtocol_1 = require("./skyProtocol");
|
|
12
|
+
const skyIntro_1 = require("./skyIntro");
|
|
13
|
+
/**
|
|
14
|
+
* Engine identity primitive — store-backed DID state and DIDComm capabilities.
|
|
15
|
+
* Crypto (pack/unpack) is delegated to Veramo when installed (Epic 2+).
|
|
16
|
+
*/
|
|
17
|
+
class IdentityService {
|
|
18
|
+
constructor(deps) {
|
|
19
|
+
this.store = deps.store;
|
|
20
|
+
this.getBaseUrl = deps.getBaseUrl;
|
|
21
|
+
this.pf = deps.pf;
|
|
22
|
+
}
|
|
23
|
+
async ensureWebvhDid(picoId, options = {}) {
|
|
24
|
+
return (0, webvh_1.ensureWebvhDid)(this.store, this.pf, () => this.getBaseUrl(), picoId, options);
|
|
25
|
+
}
|
|
26
|
+
async ensureWebvhForAllLoadedPicos() {
|
|
27
|
+
await (0, webvh_1.ensureWebvhForLoadedPicos)(this.store, this.pf, () => this.getBaseUrl());
|
|
28
|
+
for (const pico of this.pf.loadedPicos()) {
|
|
29
|
+
await (0, veramoDidComm_1.warmPeerDidsForPico)(this.store, pico.id);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Layer 2 subscription establish — create did:peer + internal Rx + persist keys.
|
|
34
|
+
* Idempotent when called again with the same subscriptionId.
|
|
35
|
+
*/
|
|
36
|
+
async establishSubscription(picoId, input) {
|
|
37
|
+
return (0, establishSubscription_1.establishSubscriptionIdentity)(this.store, this.pf, picoId, input);
|
|
38
|
+
}
|
|
39
|
+
async establishSubscriptionFromBus(picoId, bus) {
|
|
40
|
+
return this.establishSubscription(picoId, (0, establishSubscription_1.establishInputFromBus)(bus));
|
|
41
|
+
}
|
|
42
|
+
async linkRemotePeer(picoId, subscriptionId, input) {
|
|
43
|
+
return (0, establishSubscription_1.linkRemotePeer)(this.store, picoId, subscriptionId, input);
|
|
44
|
+
}
|
|
45
|
+
async teardownSubscription(picoId, subscriptionId) {
|
|
46
|
+
await this.store.deletePeerSubscription(picoId, subscriptionId);
|
|
47
|
+
}
|
|
48
|
+
async findSubscriptionIdForDid(picoId, did) {
|
|
49
|
+
return this.store.findSubscriptionIdForDid(picoId, did);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Query a remote pico by DID — layer2 subscription route first, then cold did:webvh.
|
|
53
|
+
*/
|
|
54
|
+
async picoQuery(picoId, did, query) {
|
|
55
|
+
const subId = await this.findSubscriptionIdForDid(picoId, did);
|
|
56
|
+
if (subId) {
|
|
57
|
+
return this.crossPicoQuery(picoId, undefined, subId, query);
|
|
58
|
+
}
|
|
59
|
+
return this.coldDidQuery(picoId, did, query);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Send an event to a remote pico by DID via an established layer2 subscription.
|
|
63
|
+
*/
|
|
64
|
+
async picoEvent(picoId, did, event) {
|
|
65
|
+
const subId = await this.findSubscriptionIdForDid(picoId, did);
|
|
66
|
+
if (!subId) {
|
|
67
|
+
throw new errors_1.IdentityError(`No layer2 subscription route for DID ${did}`, 404);
|
|
68
|
+
}
|
|
69
|
+
return this.crossPicoEvent(picoId, undefined, subId, event);
|
|
70
|
+
}
|
|
71
|
+
async coldDidQuery(picoId, did, query) {
|
|
72
|
+
const from = await this.getMyDid(picoId);
|
|
73
|
+
if (!from) {
|
|
74
|
+
throw new errors_1.IdentityError("Pico has no did:webvh for cold DID query", 404);
|
|
75
|
+
}
|
|
76
|
+
const message = this.generateMessage({
|
|
77
|
+
type: "https://picolabs.io/query/1.0/query",
|
|
78
|
+
from,
|
|
79
|
+
to: [did],
|
|
80
|
+
body: query,
|
|
81
|
+
});
|
|
82
|
+
const doc = await this.resolveDid(picoId, did);
|
|
83
|
+
const endpoint = (0, crossPico_1.extractDidcommEndpoint)(doc);
|
|
84
|
+
const packed = await this.packMessage(picoId, message, from, did);
|
|
85
|
+
const res = await (0, cross_fetch_1.default)(endpoint, {
|
|
86
|
+
method: "POST",
|
|
87
|
+
headers: { "Content-Type": "application/didcomm-encrypted+json" },
|
|
88
|
+
body: packed,
|
|
89
|
+
});
|
|
90
|
+
if (!res.ok) {
|
|
91
|
+
throw new errors_1.IdentityError(`Cold DID query failed: HTTP ${res.status}`, res.status);
|
|
92
|
+
}
|
|
93
|
+
const data = await res.json();
|
|
94
|
+
if (data.result !== undefined) {
|
|
95
|
+
return data.result;
|
|
96
|
+
}
|
|
97
|
+
return data;
|
|
98
|
+
}
|
|
99
|
+
async getMyDid(picoId) {
|
|
100
|
+
return this.store.getWebvhDid(picoId);
|
|
101
|
+
}
|
|
102
|
+
async getPublicIntro(picoId) {
|
|
103
|
+
const value = await this.store.getPublicIntro(picoId);
|
|
104
|
+
if (value === null) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
async setPublicIntro(picoId, enabled) {
|
|
110
|
+
await this.store.putPublicIntro(picoId, enabled);
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
/** Whether unsolicited DIDComm intro to this pico's did:webvh is allowed. */
|
|
114
|
+
async acceptsPublicIntro(picoId) {
|
|
115
|
+
return this.getPublicIntro(picoId);
|
|
116
|
+
}
|
|
117
|
+
async getWebvhLogJsonl(picoId) {
|
|
118
|
+
const log = await this.store.getWebvhLog(picoId);
|
|
119
|
+
if (!log || log.length === 0) {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
return (0, webvh_1.formatDidLogAsJsonl)(log);
|
|
123
|
+
}
|
|
124
|
+
async getParallelWebDoc(picoId) {
|
|
125
|
+
return (0, webvh_1.parallelWebDocForPico)(this.store, picoId);
|
|
126
|
+
}
|
|
127
|
+
async addRoute(picoId, type, domain, name) {
|
|
128
|
+
const routes = (await this.store.getRoutes(picoId)) || {};
|
|
129
|
+
routes[type] = { domain, name };
|
|
130
|
+
await this.store.putRoutes(picoId, routes);
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
async getRoutes(picoId) {
|
|
134
|
+
return (await this.store.getRoutes(picoId)) || {};
|
|
135
|
+
}
|
|
136
|
+
generateMessage(options) {
|
|
137
|
+
const message = {
|
|
138
|
+
id: cuid(),
|
|
139
|
+
typ: "application/didcomm-plain+json",
|
|
140
|
+
type: options.type,
|
|
141
|
+
body: options.body,
|
|
142
|
+
from: options.from,
|
|
143
|
+
to: options.to,
|
|
144
|
+
thid: options.thid,
|
|
145
|
+
pthid: options.pthid,
|
|
146
|
+
created_time: Math.floor(Date.now() / 1000),
|
|
147
|
+
};
|
|
148
|
+
if (options.expires_time) {
|
|
149
|
+
message.expires_time =
|
|
150
|
+
Math.floor(Date.now() / 1000) + options.expires_time;
|
|
151
|
+
}
|
|
152
|
+
return message;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Pack a plaintext DIDComm message (authcrypt). Requires @veramo/did-comm and
|
|
156
|
+
* supporting packages to be installed (`npm install` in packages/pico-engine).
|
|
157
|
+
*/
|
|
158
|
+
async packMessage(picoId, message, from, to) {
|
|
159
|
+
return (0, veramoDidComm_1.packDidCommMessage)(picoId, message, from, to, this.store, {
|
|
160
|
+
resolveDid: (did) => this.resolveDid("", did),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
async unpackMessage(picoId, jwe) {
|
|
164
|
+
return (0, veramoDidComm_1.unpackDidCommMessage)(picoId, jwe, this.store);
|
|
165
|
+
}
|
|
166
|
+
/** Create a did:peer (num_algo 2). Prefer establishSubscription for subscriptions. */
|
|
167
|
+
async createPeerDid(_picoId) {
|
|
168
|
+
return (0, veramoDidComm_1.createPeerDid)();
|
|
169
|
+
}
|
|
170
|
+
async registerPeerSubscription(picoId, record) {
|
|
171
|
+
await this.store.putPeerSubscription(picoId, record);
|
|
172
|
+
}
|
|
173
|
+
async crossPicoEvent(fromPicoId, toPicoId, subscriptionId, event) {
|
|
174
|
+
return (0, crossPico_1.crossPicoEvent)({
|
|
175
|
+
store: this.store,
|
|
176
|
+
pf: this.pf,
|
|
177
|
+
getBaseUrl: this.getBaseUrl,
|
|
178
|
+
identity: this,
|
|
179
|
+
}, fromPicoId, toPicoId, subscriptionId, event);
|
|
180
|
+
}
|
|
181
|
+
async crossPicoQuery(fromPicoId, toPicoId, subscriptionId, query) {
|
|
182
|
+
return (0, crossPico_1.crossPicoQuery)({
|
|
183
|
+
store: this.store,
|
|
184
|
+
pf: this.pf,
|
|
185
|
+
getBaseUrl: this.getBaseUrl,
|
|
186
|
+
identity: this,
|
|
187
|
+
}, fromPicoId, toPicoId, subscriptionId, query);
|
|
188
|
+
}
|
|
189
|
+
skyIntroDeps() {
|
|
190
|
+
return {
|
|
191
|
+
store: this.store,
|
|
192
|
+
pf: this.pf,
|
|
193
|
+
getBaseUrl: this.getBaseUrl,
|
|
194
|
+
identity: this,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
async sendSkyIntro(picoId, input) {
|
|
198
|
+
return (0, skyIntro_1.sendSkyIntro)(this.skyIntroDeps(), picoId, input);
|
|
199
|
+
}
|
|
200
|
+
async sendSkyIntroResponse(picoId, subscriptionId, toPeerDid, response, options = {}) {
|
|
201
|
+
return (0, skyIntro_1.sendSkyIntroResponse)(this.skyIntroDeps(), picoId, subscriptionId, toPeerDid, response, options);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Door verify: unpack JWE at ingress, route by sender peer DID, deliver SKY payload.
|
|
205
|
+
*/
|
|
206
|
+
async handleDidcommIngress(ingressEci, jwe) {
|
|
207
|
+
const picoId = await this.store.findPicoIdByIngressEci(ingressEci);
|
|
208
|
+
if (!picoId) {
|
|
209
|
+
throw new errors_1.IdentityError("Unknown DIDComm ingress channel", 404);
|
|
210
|
+
}
|
|
211
|
+
const { message } = await this.unpackMessage(picoId, jwe);
|
|
212
|
+
const senderDid = message.from;
|
|
213
|
+
if (!senderDid) {
|
|
214
|
+
throw new errors_1.IdentityError("DIDComm message missing from", 400);
|
|
215
|
+
}
|
|
216
|
+
if (message.type === skyProtocol_1.SKY_INTRO) {
|
|
217
|
+
return (0, skyIntro_1.handleSkyIntroAtIngress)(this.skyIntroDeps(), picoId, message);
|
|
218
|
+
}
|
|
219
|
+
if (message.type === skyProtocol_1.SKY_INTRO_RESPONSE) {
|
|
220
|
+
return (0, skyIntro_1.handleSkyIntroResponseAtIngress)(this.skyIntroDeps(), picoId, message);
|
|
221
|
+
}
|
|
222
|
+
// Channel policy (Epic 7): authenticated sender peer DID → subscription record →
|
|
223
|
+
// internal Rx channel. Door verify proves crypto + subscription relationship;
|
|
224
|
+
// pf.eventWait / pf.query enforce assertEventPolicy / assertQueryPolicy on rxEci.
|
|
225
|
+
const sub = await this.store.findPeerSubscriptionBySenderDid(picoId, senderDid);
|
|
226
|
+
if (!sub || !sub.rxEci) {
|
|
227
|
+
throw new errors_1.IdentityError("unknown_subscription", 404);
|
|
228
|
+
}
|
|
229
|
+
const callerDid = sub.remoteWebvhDid || senderDid;
|
|
230
|
+
if (message.type === skyProtocol_1.SKY_EVENT) {
|
|
231
|
+
const body = (0, skyProtocol_1.parseSkyEventBody)(message.body);
|
|
232
|
+
try {
|
|
233
|
+
const result = await this.pf.eventWait({
|
|
234
|
+
eci: sub.rxEci,
|
|
235
|
+
domain: body.domain,
|
|
236
|
+
name: body.name,
|
|
237
|
+
data: {
|
|
238
|
+
attrs: Object.assign(Object.assign({}, body.attrs), { callerDid }),
|
|
239
|
+
},
|
|
240
|
+
time: 0,
|
|
241
|
+
}, undefined);
|
|
242
|
+
return {
|
|
243
|
+
eid: result.eid,
|
|
244
|
+
responses: result.responses,
|
|
245
|
+
directives: result.responses,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
catch (err) {
|
|
249
|
+
if (isPolicyError(err)) {
|
|
250
|
+
throw new errors_1.IdentityError("policy_denied", 403);
|
|
251
|
+
}
|
|
252
|
+
throw err;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (message.type === skyProtocol_1.SKY_QUERY) {
|
|
256
|
+
const body = (0, skyProtocol_1.parseSkyQueryBody)(message.body);
|
|
257
|
+
try {
|
|
258
|
+
const result = await this.pf.query({
|
|
259
|
+
eci: sub.rxEci,
|
|
260
|
+
rid: body.rid,
|
|
261
|
+
name: body.name,
|
|
262
|
+
args: body.args,
|
|
263
|
+
});
|
|
264
|
+
return { status: "ok", result };
|
|
265
|
+
}
|
|
266
|
+
catch (err) {
|
|
267
|
+
if (isPolicyError(err)) {
|
|
268
|
+
throw new errors_1.IdentityError("policy_denied", 403);
|
|
269
|
+
}
|
|
270
|
+
throw new errors_1.IdentityError(err.message || "query_failed", 502);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const routes = await this.getRoutes(picoId);
|
|
274
|
+
const route = routes[message.type];
|
|
275
|
+
if (route) {
|
|
276
|
+
return { status: "routed", type: message.type };
|
|
277
|
+
}
|
|
278
|
+
throw new errors_1.IdentityError(`Unknown SKY message type: ${message.type}`, 400);
|
|
279
|
+
}
|
|
280
|
+
/** Resolve a DID from the local store, then via did:webvh remote resolution. */
|
|
281
|
+
async resolveDid(picoId, did) {
|
|
282
|
+
const normalized = did.trim();
|
|
283
|
+
const cached = await this.store.getDidDoc(picoId, normalized);
|
|
284
|
+
if (cached) {
|
|
285
|
+
return cached.doc;
|
|
286
|
+
}
|
|
287
|
+
if (normalized.startsWith("did:webvh:")) {
|
|
288
|
+
const localPicoId = await this.store.findPicoIdByWebvhDid(normalized);
|
|
289
|
+
if (localPicoId) {
|
|
290
|
+
const log = await this.store.getWebvhLog(localPicoId);
|
|
291
|
+
if (log && log.length > 0) {
|
|
292
|
+
const resolved = await (0, webvh_1.resolveLocalWebvhLog)(log);
|
|
293
|
+
await this.store.putDidDoc(picoId, {
|
|
294
|
+
did: resolved.did,
|
|
295
|
+
doc: resolved.doc,
|
|
296
|
+
cachedAt: new Date().toISOString(),
|
|
297
|
+
});
|
|
298
|
+
return resolved.doc;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
try {
|
|
302
|
+
const resolved = await (0, webvh_1.resolveWebvhDid)(this.store, normalized);
|
|
303
|
+
await this.store.putDidDoc(picoId, {
|
|
304
|
+
did: resolved.did,
|
|
305
|
+
doc: resolved.doc,
|
|
306
|
+
cachedAt: new Date().toISOString(),
|
|
307
|
+
});
|
|
308
|
+
return resolved.doc;
|
|
309
|
+
}
|
|
310
|
+
catch (err) {
|
|
311
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
312
|
+
const logUrl = (0, webvh_1.portableWebvhLogHttpUrl)(normalized);
|
|
313
|
+
throw new errors_1.IdentityError(`Remote did:webvh resolution failed${logUrl ? ` (${logUrl})` : ""}: ${detail}`, 404);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (normalized.startsWith("did:peer:")) {
|
|
317
|
+
const doc = await (0, veramoDidComm_1.resolvePeerDidDocument)(normalized);
|
|
318
|
+
await this.store.putDidDoc(picoId, {
|
|
319
|
+
did: normalized,
|
|
320
|
+
doc,
|
|
321
|
+
cachedAt: new Date().toISOString(),
|
|
322
|
+
});
|
|
323
|
+
return doc;
|
|
324
|
+
}
|
|
325
|
+
throw new errors_1.IdentityError(`DID not found in identity store: ${normalized}`, 404);
|
|
326
|
+
}
|
|
327
|
+
createInviteUrl(base64) {
|
|
328
|
+
const host = this.getBaseUrl();
|
|
329
|
+
if (host.indexOf("localhost") >= 0) {
|
|
330
|
+
return "http://example.com/invite?_oob=" + base64;
|
|
331
|
+
}
|
|
332
|
+
return host + "/invite?_oob=" + base64;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
exports.IdentityService = IdentityService;
|
|
336
|
+
function isPolicyError(err) {
|
|
337
|
+
return (err instanceof Error &&
|
|
338
|
+
(err.message.includes("channel policy") ||
|
|
339
|
+
err.message.includes("Denied by channel")));
|
|
340
|
+
}
|