@calimero-network/mero-react 9.4.0 → 9.5.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/dist/index.cjs +262 -196
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +263 -197
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1075,6 +1075,15 @@ interface AccountAdminDeps {
|
|
|
1075
1075
|
found?: typeof foundDelegatedNamespace;
|
|
1076
1076
|
latestVersion?: (registryUrl: string, pkg: string) => Promise<string>;
|
|
1077
1077
|
signInvitation?: typeof signGroupInvitation;
|
|
1078
|
+
/**
|
|
1079
|
+
* Redeem an invitation for this account: resolve the relay the invitation
|
|
1080
|
+
* names, have it admit the account, and switch the session onto that relay.
|
|
1081
|
+
* The provider supplies it (it owns the session); without it a join is
|
|
1082
|
+
* refused by name. Rejects with the refusal's reason and HTTP `status`.
|
|
1083
|
+
*/
|
|
1084
|
+
join?: (namespaceId: string, invitation: SignedGroupOpenInvitation) => Promise<{
|
|
1085
|
+
namespaceId: string;
|
|
1086
|
+
}>;
|
|
1078
1087
|
}
|
|
1079
1088
|
declare function createAccountAdmin(input: {
|
|
1080
1089
|
session: DelegatedSession;
|
package/dist/index.d.ts
CHANGED
|
@@ -1075,6 +1075,15 @@ interface AccountAdminDeps {
|
|
|
1075
1075
|
found?: typeof foundDelegatedNamespace;
|
|
1076
1076
|
latestVersion?: (registryUrl: string, pkg: string) => Promise<string>;
|
|
1077
1077
|
signInvitation?: typeof signGroupInvitation;
|
|
1078
|
+
/**
|
|
1079
|
+
* Redeem an invitation for this account: resolve the relay the invitation
|
|
1080
|
+
* names, have it admit the account, and switch the session onto that relay.
|
|
1081
|
+
* The provider supplies it (it owns the session); without it a join is
|
|
1082
|
+
* refused by name. Rejects with the refusal's reason and HTTP `status`.
|
|
1083
|
+
*/
|
|
1084
|
+
join?: (namespaceId: string, invitation: SignedGroupOpenInvitation) => Promise<{
|
|
1085
|
+
namespaceId: string;
|
|
1086
|
+
}>;
|
|
1078
1087
|
}
|
|
1079
1088
|
declare function createAccountAdmin(input: {
|
|
1080
1089
|
session: DelegatedSession;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createContext, useMemo, useState, useRef, useCallback, useEffect, useContext } from 'react';
|
|
2
|
-
import { signGroupInvitation, LocalStorageTokenStore, parseAuthCallback, MeroJs, buildAuthLoginUrl, DEFAULT_LOCAL_NODE_PORTS, discoverLocalNodes, nodeEndpoint, readEnrolmentCallback, completeDeviceEnrolment,
|
|
2
|
+
import { signGroupInvitation, signMemberJoinOp, CloudClient, LocalStorageTokenStore, parseAuthCallback, MeroJs, buildAuthLoginUrl, DEFAULT_LOCAL_NODE_PORTS, discoverLocalNodes, nodeEndpoint, readEnrolmentCallback, completeDeviceEnrolment, deviceEnrolmentUrl, memberJoinedOpenOp, signerFromSecret, groupDeletedOp, memberAddedOp, memberRemovedOp, subgroupCreation, memberRoleSetOp, defaultCapabilitiesSetOp, subgroupVisibilitySetOp, groupMetadataSetOp, memberMetadataSetOp, contextMetadataSetOp, groupReparentedOp, contextDetachedOp, compareSemver, AuthRevokedError, HTTPError, memberLeftOp, memberCapabilitySetOp, attestRelayNodeKey, createMeroClient, RelayClient, createLocalStorageNonceSource, classifyError, login, defaultAudience, createSignedReleaseVerifier, fetchNodeRelease, cloudNodeReleaseUrl, DEFAULT_RELEASE_MIRROR, fetchNodeReleaseVersion } from '@calimero-network/mero-js';
|
|
3
3
|
export * from '@calimero-network/mero-js';
|
|
4
4
|
export { CAPABILITIES, DEFAULT_LOCAL_NODE_PORTS, discoverLocalNodes, hasCap, localNodeUrl, nodeEndpoint, probeNodeHealth, withCap, withoutCap } from '@calimero-network/mero-js';
|
|
5
5
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
@@ -559,8 +559,23 @@ var NODE_ONLY = /* @__PURE__ */ new Set([
|
|
|
559
559
|
"uninstallApplication",
|
|
560
560
|
"generateContextIdentity",
|
|
561
561
|
"deleteContext",
|
|
562
|
-
"
|
|
563
|
-
"
|
|
562
|
+
"createContextAlias",
|
|
563
|
+
"createApplicationAlias",
|
|
564
|
+
"createDeviceAlias",
|
|
565
|
+
"deleteContextAlias",
|
|
566
|
+
"deleteApplicationAlias",
|
|
567
|
+
"deleteDeviceAlias",
|
|
568
|
+
"deleteNamespace",
|
|
569
|
+
"createGroupInvitation",
|
|
570
|
+
"joinGroup",
|
|
571
|
+
"setTeeAdmissionPolicy",
|
|
572
|
+
"getTeeAdmissionPolicy",
|
|
573
|
+
"deleteBlob",
|
|
574
|
+
"listAccountDevices",
|
|
575
|
+
"revokeAccountDevice",
|
|
576
|
+
"getGroupUpgradeStatus",
|
|
577
|
+
"getMigrationStatus",
|
|
578
|
+
"getCascadeStatus"
|
|
564
579
|
]);
|
|
565
580
|
function createAccountAdmin(input, deps = {}) {
|
|
566
581
|
const { session: s, read, app } = input;
|
|
@@ -570,6 +585,11 @@ function createAccountAdmin(input, deps = {}) {
|
|
|
570
585
|
const found = deps.found ?? foundDelegatedNamespace;
|
|
571
586
|
const latestVersion = deps.latestVersion ?? latestPublishedVersion;
|
|
572
587
|
const signInvitation = deps.signInvitation ?? signGroupInvitation;
|
|
588
|
+
const join = deps.join;
|
|
589
|
+
async function joinNamespace(namespaceId, req) {
|
|
590
|
+
if (!join) throw new NotForAccountError("joinNamespace without a join handler");
|
|
591
|
+
return join(namespaceId, req.invitation);
|
|
592
|
+
}
|
|
573
593
|
const relay = () => {
|
|
574
594
|
if (read === null) throw new NoRelayError("this call");
|
|
575
595
|
return read;
|
|
@@ -692,6 +712,25 @@ function createAccountAdmin(input, deps = {}) {
|
|
|
692
712
|
// The relay syncs its own view; an account has nothing to pull.
|
|
693
713
|
async syncGroup(groupId) {
|
|
694
714
|
return { groupId, synced: true };
|
|
715
|
+
},
|
|
716
|
+
async syncContext() {
|
|
717
|
+
},
|
|
718
|
+
joinNamespace,
|
|
719
|
+
async updateMemberRole(groupId, identity, req) {
|
|
720
|
+
await group(s, groupId, memberRoleSetOp(identity, req.role));
|
|
721
|
+
},
|
|
722
|
+
// An open subgroup is joined the way `joinContext` joins one: MemberJoinedOpen
|
|
723
|
+
// on the subgroup, and nothing to do for a member already in it.
|
|
724
|
+
async joinSubgroupInheritance(groupId) {
|
|
725
|
+
const { members } = await relay().listGroupMembers(groupId);
|
|
726
|
+
if (members.some((m) => m.identity.toLowerCase() === s.account.toLowerCase())) {
|
|
727
|
+
return { groupId, memberPublicKey: s.account, wasInherited: false };
|
|
728
|
+
}
|
|
729
|
+
await root(s, groupId, memberJoinedOpenOp({ member: s.account, groupId, credential: s.credential }));
|
|
730
|
+
return { groupId, memberPublicKey: s.account, wasInherited: true };
|
|
731
|
+
},
|
|
732
|
+
async detachContextFromGroup(groupId, contextId) {
|
|
733
|
+
await group(s, groupId, contextDetachedOp(contextId));
|
|
695
734
|
}
|
|
696
735
|
};
|
|
697
736
|
if (read === null) {
|
|
@@ -699,6 +738,7 @@ function createAccountAdmin(input, deps = {}) {
|
|
|
699
738
|
get(_target, prop) {
|
|
700
739
|
if (typeof prop !== "string") return void 0;
|
|
701
740
|
if (prop === "getNodeIdentity") return writes.getNodeIdentity;
|
|
741
|
+
if (prop === "joinNamespace") return joinNamespace;
|
|
702
742
|
if (prop in EMPTY_READS) return EMPTY_READS[prop];
|
|
703
743
|
return async () => {
|
|
704
744
|
throw new NoRelayError(prop);
|
|
@@ -718,6 +758,217 @@ function createAccountAdmin(input, deps = {}) {
|
|
|
718
758
|
}
|
|
719
759
|
});
|
|
720
760
|
}
|
|
761
|
+
function explainAdmitFailure(status, body) {
|
|
762
|
+
const detail = body ? `: ${body}` : "";
|
|
763
|
+
switch (status) {
|
|
764
|
+
case 400:
|
|
765
|
+
return `The node refused the join as malformed (400)${detail}. The signature covers the invitation exactly as sent, so an edited or re-serialised invitation fails here \u2014 as does a namespace id that is not the invitation's own group.`;
|
|
766
|
+
case 403:
|
|
767
|
+
return `The node refused to carry this join (403)${detail}. Either it is not in the invitation's signed admitters list \u2014 being live and listed by the cloud is not the same thing \u2014 or the invitation itself was rejected as expired or not the inviter\u2019s to issue.`;
|
|
768
|
+
case 409:
|
|
769
|
+
return `That node holds no device of its own, so it cannot endorse anyone (409)${detail}. Another admitter from the same invitation would work.`;
|
|
770
|
+
default:
|
|
771
|
+
return `The join was not published (HTTP ${status})${detail}.`;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
function joinNonce(account) {
|
|
775
|
+
const source = createLocalStorageNonceSource(`calimero.delegated.joinnonce.${account}`);
|
|
776
|
+
return () => source.next();
|
|
777
|
+
}
|
|
778
|
+
function origin(url) {
|
|
779
|
+
return url.replace(/\/+$/, "");
|
|
780
|
+
}
|
|
781
|
+
async function joinWithNode(input) {
|
|
782
|
+
const { account, credential, deviceSecret } = input.credential;
|
|
783
|
+
let signedOp;
|
|
784
|
+
try {
|
|
785
|
+
signedOp = await signMemberJoinOp({
|
|
786
|
+
namespaceId: input.namespaceId,
|
|
787
|
+
member: account,
|
|
788
|
+
invitation: input.invitation,
|
|
789
|
+
credential,
|
|
790
|
+
deviceSecret,
|
|
791
|
+
nonce: await (input.deps?.nonce ?? joinNonce(account))()
|
|
792
|
+
});
|
|
793
|
+
} catch (e) {
|
|
794
|
+
return {
|
|
795
|
+
ok: false,
|
|
796
|
+
step: "sign",
|
|
797
|
+
reason: `The join op could not be signed, so nothing was sent: ${e instanceof Error ? e.message : String(e)}`
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
const body = { invitation: input.invitation, signedOp };
|
|
801
|
+
const url = input.admitUrl ?? `${origin(input.nodeUrl)}/admin-api/namespaces/${input.namespaceId}/admit`;
|
|
802
|
+
const doFetch = input.deps?.fetch ?? fetch;
|
|
803
|
+
let response;
|
|
804
|
+
try {
|
|
805
|
+
response = await doFetch(url, {
|
|
806
|
+
method: "POST",
|
|
807
|
+
// No Authorization header and no request proof, deliberately: the route is
|
|
808
|
+
// unauthenticated because the joiner's signature is the authorization.
|
|
809
|
+
headers: { "Content-Type": "application/json", Accept: "application/json" },
|
|
810
|
+
body: JSON.stringify(body)
|
|
811
|
+
});
|
|
812
|
+
} catch (e) {
|
|
813
|
+
return {
|
|
814
|
+
ok: false,
|
|
815
|
+
step: "admit",
|
|
816
|
+
reason: `The admitter at ${input.nodeUrl} could not be reached: ${e instanceof Error ? e.message : String(e)}. The invitation is untouched \u2014 this is the node being unreachable, not a refusal.`
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
const text = await response.text();
|
|
820
|
+
if (!response.ok) {
|
|
821
|
+
return {
|
|
822
|
+
ok: false,
|
|
823
|
+
step: "admit",
|
|
824
|
+
status: response.status,
|
|
825
|
+
reason: explainAdmitFailure(response.status, text)
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
let published = false;
|
|
829
|
+
try {
|
|
830
|
+
const parsed = text ? JSON.parse(text) : {};
|
|
831
|
+
published = parsed.data?.published === true;
|
|
832
|
+
} catch {
|
|
833
|
+
published = false;
|
|
834
|
+
}
|
|
835
|
+
return {
|
|
836
|
+
ok: true,
|
|
837
|
+
published,
|
|
838
|
+
session: { relayUrl: origin(input.nodeUrl), account, credential, deviceSecret }
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
function normaliseAccount(account) {
|
|
842
|
+
if (!account) return null;
|
|
843
|
+
const trimmed = account.trim().toLowerCase();
|
|
844
|
+
const bare = trimmed.startsWith("0x") ? trimmed.slice(2) : trimmed;
|
|
845
|
+
return bare.length > 0 ? bare : null;
|
|
846
|
+
}
|
|
847
|
+
function isInvited(node, invited) {
|
|
848
|
+
if (invited.size === 0) return true;
|
|
849
|
+
const account = normaliseAccount(node.account);
|
|
850
|
+
return account !== null && invited.has(account);
|
|
851
|
+
}
|
|
852
|
+
async function resolveRelayFromInvitation(input) {
|
|
853
|
+
const cloud = input.cloud ?? new CloudClient({
|
|
854
|
+
cloudBaseUrl: input.cloudBaseUrl,
|
|
855
|
+
routingCredential: { credential: input.credential, deviceSecret: input.deviceSecret }
|
|
856
|
+
});
|
|
857
|
+
let nodes;
|
|
858
|
+
let servable;
|
|
859
|
+
try {
|
|
860
|
+
const routing = await cloud.getNamespaceRouting(input.namespaceId);
|
|
861
|
+
nodes = routing.nodes;
|
|
862
|
+
servable = routing.servable;
|
|
863
|
+
} catch (e) {
|
|
864
|
+
return {
|
|
865
|
+
ok: false,
|
|
866
|
+
step: "admitters-lookup",
|
|
867
|
+
reason: `The cloud could not be asked which nodes serve namespace ${input.namespaceId}: ${e instanceof Error ? e.message : String(e)}. Nothing is known about the invitation yet \u2014 this is the lookup failing, not the invitation.`
|
|
868
|
+
};
|
|
869
|
+
}
|
|
870
|
+
if (nodes.length === 0) {
|
|
871
|
+
return {
|
|
872
|
+
ok: false,
|
|
873
|
+
step: "no-nodes",
|
|
874
|
+
reason: "The cloud lists no nodes serving this namespace, so there is no hosted node to be admitted through. The namespace has no fleet assignment \u2014 whoever invited you is running their own node, and admission has to go through a node the cloud routes to."
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
const invited = new Set(
|
|
878
|
+
(input.admitters ?? []).map(normaliseAccount).filter((a) => a !== null)
|
|
879
|
+
);
|
|
880
|
+
const named = nodes.filter((n) => isInvited(n, invited));
|
|
881
|
+
if (named.length === 0) {
|
|
882
|
+
return {
|
|
883
|
+
ok: false,
|
|
884
|
+
step: "not-invited",
|
|
885
|
+
reason: `${nodes.length} node${nodes.length === 1 ? "" : "s"} serve this namespace, but your invitation names none of them. The signed admitter list is a snapshot from when the invitation was minted, so a node assigned since is healthy and will still refuse the claim. Ask for a fresh invitation.`
|
|
886
|
+
};
|
|
887
|
+
}
|
|
888
|
+
const usable = named.filter((n) => n.canAdmit);
|
|
889
|
+
const candidates = usable.length > 0 ? usable : named;
|
|
890
|
+
const advisory = usable.length === 0 || !servable;
|
|
891
|
+
const addressable = candidates.filter(
|
|
892
|
+
(n) => typeof n.relayUrl === "string" && n.relayUrl.length > 0
|
|
893
|
+
);
|
|
894
|
+
if (addressable.length === 0) {
|
|
895
|
+
return {
|
|
896
|
+
ok: false,
|
|
897
|
+
step: "no-relay-url",
|
|
898
|
+
reason: "A node your invitation names can admit you, but the cloud knows no address for it yet, so there would be nothing to connect through afterwards. Try again shortly."
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
const chosen = addressable.find((n) => n.canExecute) ?? addressable.find((n) => n.fresh) ?? addressable[0];
|
|
902
|
+
return {
|
|
903
|
+
ok: true,
|
|
904
|
+
relayUrl: chosen.relayUrl,
|
|
905
|
+
// The cloud's ready-made URL when it gave one, so the path is never rebuilt
|
|
906
|
+
// from a guess; the fallback is the documented route on the same origin.
|
|
907
|
+
admitUrl: chosen.admitUrl ?? `${chosen.relayUrl.replace(/\/+$/, "")}/admin-api/namespaces/${input.namespaceId}/admit`,
|
|
908
|
+
admitterAccount: chosen.account,
|
|
909
|
+
peerId: chosen.peerId,
|
|
910
|
+
writable: chosen.canExecute,
|
|
911
|
+
// Stale when the node's own heartbeat lapsed OR when the cloud's summary
|
|
912
|
+
// disagrees with trying it at all. Both mean the same thing to a caller:
|
|
913
|
+
// this was attempted on advisory data, so if the admit fails that is why.
|
|
914
|
+
stale: !chosen.fresh || advisory
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
// src/delegated/bootstrap-from-invitation.ts
|
|
919
|
+
async function bootstrapFromInvitation(input) {
|
|
920
|
+
const resolve = input.deps?.resolve ?? resolveRelayFromInvitation;
|
|
921
|
+
const join = input.deps?.join ?? joinWithNode;
|
|
922
|
+
let relay = null;
|
|
923
|
+
let nodeUrl = input.nodeUrl;
|
|
924
|
+
let admitUrl;
|
|
925
|
+
if (!nodeUrl) {
|
|
926
|
+
const resolved = await resolve({
|
|
927
|
+
namespaceId: input.namespaceId,
|
|
928
|
+
// The signed list, read off the invitation itself rather than passed
|
|
929
|
+
// separately: two sources for one fact is how an intersection ends up
|
|
930
|
+
// being performed against the wrong list.
|
|
931
|
+
admitters: input.invitation.invitation.admitters,
|
|
932
|
+
credential: input.credential.credential,
|
|
933
|
+
deviceSecret: input.credential.deviceSecret,
|
|
934
|
+
cloudBaseUrl: input.cloudBaseUrl
|
|
935
|
+
});
|
|
936
|
+
if (!resolved.ok) return resolved;
|
|
937
|
+
relay = resolved;
|
|
938
|
+
nodeUrl = resolved.relayUrl;
|
|
939
|
+
admitUrl = resolved.admitUrl;
|
|
940
|
+
}
|
|
941
|
+
const joined = await join({
|
|
942
|
+
nodeUrl,
|
|
943
|
+
admitUrl,
|
|
944
|
+
namespaceId: input.namespaceId,
|
|
945
|
+
invitation: input.invitation,
|
|
946
|
+
credential: input.credential,
|
|
947
|
+
deps: { fetch: input.deps?.fetch, nonce: input.deps?.nonce }
|
|
948
|
+
});
|
|
949
|
+
if (!joined.ok) return joined;
|
|
950
|
+
return { ok: true, published: joined.published, relay, session: joined.session };
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
// src/delegated/join-as-account.ts
|
|
954
|
+
async function joinAsAccount(session, namespaceId, invitation, opts) {
|
|
955
|
+
const bootstrap = opts.bootstrap ?? bootstrapFromInvitation;
|
|
956
|
+
const outcome = await bootstrap({
|
|
957
|
+
namespaceId,
|
|
958
|
+
invitation,
|
|
959
|
+
credential: { account: session.account, credential: session.credential, deviceSecret: session.deviceSecret },
|
|
960
|
+
cloudBaseUrl: opts.cloudBaseUrl
|
|
961
|
+
});
|
|
962
|
+
if (!outcome.ok) {
|
|
963
|
+
throw Object.assign(new Error(outcome.reason), {
|
|
964
|
+
step: outcome.step,
|
|
965
|
+
...outcome.status !== void 0 ? { status: outcome.status } : {}
|
|
966
|
+
});
|
|
967
|
+
}
|
|
968
|
+
if (outcome.session.relayUrl) rememberRelay(session.account, outcome.session.relayUrl, { namespaceId });
|
|
969
|
+
opts.onJoined(outcome.session);
|
|
970
|
+
return { namespaceId };
|
|
971
|
+
}
|
|
721
972
|
|
|
722
973
|
// src/auth/node-trust.ts
|
|
723
974
|
function sameOrigin(a, b) {
|
|
@@ -1271,13 +1522,19 @@ function MeroProvider({
|
|
|
1271
1522
|
const admin = useMemo(() => {
|
|
1272
1523
|
const app = { packageName, packageVersion, registryUrl };
|
|
1273
1524
|
if (delegated !== null) {
|
|
1274
|
-
|
|
1525
|
+
const deps = {
|
|
1526
|
+
join: (namespaceId, invitation) => joinAsAccount(delegated, namespaceId, invitation, { cloudBaseUrl, onJoined: connectWithAccount })
|
|
1527
|
+
};
|
|
1528
|
+
if (delegated.relayUrl === null) return createAccountAdmin({ session: delegated, read: null, app }, deps);
|
|
1275
1529
|
if (!mero) return null;
|
|
1276
|
-
return createAccountAdmin(
|
|
1530
|
+
return createAccountAdmin(
|
|
1531
|
+
{ session: delegated, read: mero.admin, app },
|
|
1532
|
+
deps
|
|
1533
|
+
);
|
|
1277
1534
|
}
|
|
1278
1535
|
const nodeAdmin = mero?.admin;
|
|
1279
1536
|
return nodeAdmin ? createNodeAdmin({ admin: nodeAdmin, app }) : null;
|
|
1280
|
-
}, [mero, delegated, packageName, packageVersion, registryUrl]);
|
|
1537
|
+
}, [mero, delegated, packageName, packageVersion, registryUrl, cloudBaseUrl, connectWithAccount]);
|
|
1281
1538
|
const contextValue = useMemo(
|
|
1282
1539
|
() => ({
|
|
1283
1540
|
mero,
|
|
@@ -4173,197 +4430,6 @@ function MigrationAdminPanel({ namespaceId, pollIntervalMs, className }) {
|
|
|
4173
4430
|
] })
|
|
4174
4431
|
] });
|
|
4175
4432
|
}
|
|
4176
|
-
function normaliseAccount(account) {
|
|
4177
|
-
if (!account) return null;
|
|
4178
|
-
const trimmed = account.trim().toLowerCase();
|
|
4179
|
-
const bare = trimmed.startsWith("0x") ? trimmed.slice(2) : trimmed;
|
|
4180
|
-
return bare.length > 0 ? bare : null;
|
|
4181
|
-
}
|
|
4182
|
-
function isInvited(node, invited) {
|
|
4183
|
-
if (invited.size === 0) return true;
|
|
4184
|
-
const account = normaliseAccount(node.account);
|
|
4185
|
-
return account !== null && invited.has(account);
|
|
4186
|
-
}
|
|
4187
|
-
async function resolveRelayFromInvitation(input) {
|
|
4188
|
-
const cloud = input.cloud ?? new CloudClient({
|
|
4189
|
-
cloudBaseUrl: input.cloudBaseUrl,
|
|
4190
|
-
routingCredential: { credential: input.credential, deviceSecret: input.deviceSecret }
|
|
4191
|
-
});
|
|
4192
|
-
let nodes;
|
|
4193
|
-
let servable;
|
|
4194
|
-
try {
|
|
4195
|
-
const routing = await cloud.getNamespaceRouting(input.namespaceId);
|
|
4196
|
-
nodes = routing.nodes;
|
|
4197
|
-
servable = routing.servable;
|
|
4198
|
-
} catch (e) {
|
|
4199
|
-
return {
|
|
4200
|
-
ok: false,
|
|
4201
|
-
step: "admitters-lookup",
|
|
4202
|
-
reason: `The cloud could not be asked which nodes serve namespace ${input.namespaceId}: ${e instanceof Error ? e.message : String(e)}. Nothing is known about the invitation yet \u2014 this is the lookup failing, not the invitation.`
|
|
4203
|
-
};
|
|
4204
|
-
}
|
|
4205
|
-
if (nodes.length === 0) {
|
|
4206
|
-
return {
|
|
4207
|
-
ok: false,
|
|
4208
|
-
step: "no-nodes",
|
|
4209
|
-
reason: "The cloud lists no nodes serving this namespace, so there is no hosted node to be admitted through. The namespace has no fleet assignment \u2014 whoever invited you is running their own node, and admission has to go through a node the cloud routes to."
|
|
4210
|
-
};
|
|
4211
|
-
}
|
|
4212
|
-
const invited = new Set(
|
|
4213
|
-
(input.admitters ?? []).map(normaliseAccount).filter((a) => a !== null)
|
|
4214
|
-
);
|
|
4215
|
-
const named = nodes.filter((n) => isInvited(n, invited));
|
|
4216
|
-
if (named.length === 0) {
|
|
4217
|
-
return {
|
|
4218
|
-
ok: false,
|
|
4219
|
-
step: "not-invited",
|
|
4220
|
-
reason: `${nodes.length} node${nodes.length === 1 ? "" : "s"} serve this namespace, but your invitation names none of them. The signed admitter list is a snapshot from when the invitation was minted, so a node assigned since is healthy and will still refuse the claim. Ask for a fresh invitation.`
|
|
4221
|
-
};
|
|
4222
|
-
}
|
|
4223
|
-
const usable = named.filter((n) => n.canAdmit);
|
|
4224
|
-
const candidates = usable.length > 0 ? usable : named;
|
|
4225
|
-
const advisory = usable.length === 0 || !servable;
|
|
4226
|
-
const addressable = candidates.filter(
|
|
4227
|
-
(n) => typeof n.relayUrl === "string" && n.relayUrl.length > 0
|
|
4228
|
-
);
|
|
4229
|
-
if (addressable.length === 0) {
|
|
4230
|
-
return {
|
|
4231
|
-
ok: false,
|
|
4232
|
-
step: "no-relay-url",
|
|
4233
|
-
reason: "A node your invitation names can admit you, but the cloud knows no address for it yet, so there would be nothing to connect through afterwards. Try again shortly."
|
|
4234
|
-
};
|
|
4235
|
-
}
|
|
4236
|
-
const chosen = addressable.find((n) => n.canExecute) ?? addressable.find((n) => n.fresh) ?? addressable[0];
|
|
4237
|
-
return {
|
|
4238
|
-
ok: true,
|
|
4239
|
-
relayUrl: chosen.relayUrl,
|
|
4240
|
-
// The cloud's ready-made URL when it gave one, so the path is never rebuilt
|
|
4241
|
-
// from a guess; the fallback is the documented route on the same origin.
|
|
4242
|
-
admitUrl: chosen.admitUrl ?? `${chosen.relayUrl.replace(/\/+$/, "")}/admin-api/namespaces/${input.namespaceId}/admit`,
|
|
4243
|
-
admitterAccount: chosen.account,
|
|
4244
|
-
peerId: chosen.peerId,
|
|
4245
|
-
writable: chosen.canExecute,
|
|
4246
|
-
// Stale when the node's own heartbeat lapsed OR when the cloud's summary
|
|
4247
|
-
// disagrees with trying it at all. Both mean the same thing to a caller:
|
|
4248
|
-
// this was attempted on advisory data, so if the admit fails that is why.
|
|
4249
|
-
stale: !chosen.fresh || advisory
|
|
4250
|
-
};
|
|
4251
|
-
}
|
|
4252
|
-
function explainAdmitFailure(status, body) {
|
|
4253
|
-
const detail = body ? `: ${body}` : "";
|
|
4254
|
-
switch (status) {
|
|
4255
|
-
case 400:
|
|
4256
|
-
return `The node refused the join as malformed (400)${detail}. The signature covers the invitation exactly as sent, so an edited or re-serialised invitation fails here \u2014 as does a namespace id that is not the invitation's own group.`;
|
|
4257
|
-
case 403:
|
|
4258
|
-
return `The node refused to carry this join (403)${detail}. Either it is not in the invitation's signed admitters list \u2014 being live and listed by the cloud is not the same thing \u2014 or the invitation itself was rejected as expired or not the inviter\u2019s to issue.`;
|
|
4259
|
-
case 409:
|
|
4260
|
-
return `That node holds no device of its own, so it cannot endorse anyone (409)${detail}. Another admitter from the same invitation would work.`;
|
|
4261
|
-
default:
|
|
4262
|
-
return `The join was not published (HTTP ${status})${detail}.`;
|
|
4263
|
-
}
|
|
4264
|
-
}
|
|
4265
|
-
function joinNonce(account) {
|
|
4266
|
-
const source = createLocalStorageNonceSource(`calimero.delegated.joinnonce.${account}`);
|
|
4267
|
-
return () => source.next();
|
|
4268
|
-
}
|
|
4269
|
-
function origin(url) {
|
|
4270
|
-
return url.replace(/\/+$/, "");
|
|
4271
|
-
}
|
|
4272
|
-
async function joinWithNode(input) {
|
|
4273
|
-
const { account, credential, deviceSecret } = input.credential;
|
|
4274
|
-
let signedOp;
|
|
4275
|
-
try {
|
|
4276
|
-
signedOp = await signMemberJoinOp({
|
|
4277
|
-
namespaceId: input.namespaceId,
|
|
4278
|
-
member: account,
|
|
4279
|
-
invitation: input.invitation,
|
|
4280
|
-
credential,
|
|
4281
|
-
deviceSecret,
|
|
4282
|
-
nonce: await (input.deps?.nonce ?? joinNonce(account))()
|
|
4283
|
-
});
|
|
4284
|
-
} catch (e) {
|
|
4285
|
-
return {
|
|
4286
|
-
ok: false,
|
|
4287
|
-
step: "sign",
|
|
4288
|
-
reason: `The join op could not be signed, so nothing was sent: ${e instanceof Error ? e.message : String(e)}`
|
|
4289
|
-
};
|
|
4290
|
-
}
|
|
4291
|
-
const body = { invitation: input.invitation, signedOp };
|
|
4292
|
-
const url = input.admitUrl ?? `${origin(input.nodeUrl)}/admin-api/namespaces/${input.namespaceId}/admit`;
|
|
4293
|
-
const doFetch = input.deps?.fetch ?? fetch;
|
|
4294
|
-
let response;
|
|
4295
|
-
try {
|
|
4296
|
-
response = await doFetch(url, {
|
|
4297
|
-
method: "POST",
|
|
4298
|
-
// No Authorization header and no request proof, deliberately: the route is
|
|
4299
|
-
// unauthenticated because the joiner's signature is the authorization.
|
|
4300
|
-
headers: { "Content-Type": "application/json", Accept: "application/json" },
|
|
4301
|
-
body: JSON.stringify(body)
|
|
4302
|
-
});
|
|
4303
|
-
} catch (e) {
|
|
4304
|
-
return {
|
|
4305
|
-
ok: false,
|
|
4306
|
-
step: "admit",
|
|
4307
|
-
reason: `The admitter at ${input.nodeUrl} could not be reached: ${e instanceof Error ? e.message : String(e)}. The invitation is untouched \u2014 this is the node being unreachable, not a refusal.`
|
|
4308
|
-
};
|
|
4309
|
-
}
|
|
4310
|
-
const text = await response.text();
|
|
4311
|
-
if (!response.ok) {
|
|
4312
|
-
return {
|
|
4313
|
-
ok: false,
|
|
4314
|
-
step: "admit",
|
|
4315
|
-
status: response.status,
|
|
4316
|
-
reason: explainAdmitFailure(response.status, text)
|
|
4317
|
-
};
|
|
4318
|
-
}
|
|
4319
|
-
let published = false;
|
|
4320
|
-
try {
|
|
4321
|
-
const parsed = text ? JSON.parse(text) : {};
|
|
4322
|
-
published = parsed.data?.published === true;
|
|
4323
|
-
} catch {
|
|
4324
|
-
published = false;
|
|
4325
|
-
}
|
|
4326
|
-
return {
|
|
4327
|
-
ok: true,
|
|
4328
|
-
published,
|
|
4329
|
-
session: { relayUrl: origin(input.nodeUrl), account, credential, deviceSecret }
|
|
4330
|
-
};
|
|
4331
|
-
}
|
|
4332
|
-
|
|
4333
|
-
// src/delegated/bootstrap-from-invitation.ts
|
|
4334
|
-
async function bootstrapFromInvitation(input) {
|
|
4335
|
-
const resolve = input.deps?.resolve ?? resolveRelayFromInvitation;
|
|
4336
|
-
const join = input.deps?.join ?? joinWithNode;
|
|
4337
|
-
let relay = null;
|
|
4338
|
-
let nodeUrl = input.nodeUrl;
|
|
4339
|
-
let admitUrl;
|
|
4340
|
-
if (!nodeUrl) {
|
|
4341
|
-
const resolved = await resolve({
|
|
4342
|
-
namespaceId: input.namespaceId,
|
|
4343
|
-
// The signed list, read off the invitation itself rather than passed
|
|
4344
|
-
// separately: two sources for one fact is how an intersection ends up
|
|
4345
|
-
// being performed against the wrong list.
|
|
4346
|
-
admitters: input.invitation.invitation.admitters,
|
|
4347
|
-
credential: input.credential.credential,
|
|
4348
|
-
deviceSecret: input.credential.deviceSecret,
|
|
4349
|
-
cloudBaseUrl: input.cloudBaseUrl
|
|
4350
|
-
});
|
|
4351
|
-
if (!resolved.ok) return resolved;
|
|
4352
|
-
relay = resolved;
|
|
4353
|
-
nodeUrl = resolved.relayUrl;
|
|
4354
|
-
admitUrl = resolved.admitUrl;
|
|
4355
|
-
}
|
|
4356
|
-
const joined = await join({
|
|
4357
|
-
nodeUrl,
|
|
4358
|
-
admitUrl,
|
|
4359
|
-
namespaceId: input.namespaceId,
|
|
4360
|
-
invitation: input.invitation,
|
|
4361
|
-
credential: input.credential,
|
|
4362
|
-
deps: { fetch: input.deps?.fetch, nonce: input.deps?.nonce }
|
|
4363
|
-
});
|
|
4364
|
-
if (!joined.ok) return joined;
|
|
4365
|
-
return { ok: true, published: joined.published, relay, session: joined.session };
|
|
4366
|
-
}
|
|
4367
4433
|
function useDelegatedBootstrap(options = {}) {
|
|
4368
4434
|
const { connectWithAccount, cloudBaseUrl: providerCloud } = useMero();
|
|
4369
4435
|
const [running, setRunning] = useState(false);
|