@tinycloud/node-sdk 1.2.0 → 1.4.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 +279 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +82 -7
- package/dist/index.d.ts +82 -7
- package/dist/index.js +267 -17
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -17025,40 +17025,44 @@ var require_utils2 = __commonJS({
|
|
|
17025
17025
|
// src/index.ts
|
|
17026
17026
|
var index_exports = {};
|
|
17027
17027
|
__export(index_exports, {
|
|
17028
|
-
CapabilityKeyRegistry: () =>
|
|
17029
|
-
CapabilityKeyRegistryErrorCodes: () =>
|
|
17028
|
+
CapabilityKeyRegistry: () => import_sdk_core10.CapabilityKeyRegistry,
|
|
17029
|
+
CapabilityKeyRegistryErrorCodes: () => import_sdk_core10.CapabilityKeyRegistryErrorCodes,
|
|
17030
|
+
DataVaultService: () => import_sdk_core8.DataVaultService,
|
|
17030
17031
|
DatabaseHandle: () => import_sdk_core7.DatabaseHandle,
|
|
17031
17032
|
DelegatedAccess: () => DelegatedAccess,
|
|
17032
|
-
DelegationErrorCodes: () =>
|
|
17033
|
-
DelegationManager: () =>
|
|
17033
|
+
DelegationErrorCodes: () => import_sdk_core9.DelegationErrorCodes,
|
|
17034
|
+
DelegationManager: () => import_sdk_core9.DelegationManager,
|
|
17034
17035
|
FileSessionStorage: () => FileSessionStorage,
|
|
17035
17036
|
KVService: () => import_sdk_core6.KVService,
|
|
17036
17037
|
MemorySessionStorage: () => MemorySessionStorage,
|
|
17037
17038
|
NodeUserAuthorization: () => NodeUserAuthorization,
|
|
17038
17039
|
PrefixedKVService: () => import_sdk_core6.PrefixedKVService,
|
|
17039
17040
|
PrivateKeySigner: () => PrivateKeySigner,
|
|
17040
|
-
ProtocolMismatchError: () =>
|
|
17041
|
+
ProtocolMismatchError: () => import_sdk_core12.ProtocolMismatchError,
|
|
17041
17042
|
SQLAction: () => import_sdk_core7.SQLAction,
|
|
17042
17043
|
SQLService: () => import_sdk_core7.SQLService,
|
|
17043
|
-
ServiceContext: () =>
|
|
17044
|
-
SharingService: () =>
|
|
17045
|
-
Space: () =>
|
|
17046
|
-
SpaceErrorCodes: () =>
|
|
17047
|
-
SpaceService: () =>
|
|
17044
|
+
ServiceContext: () => import_sdk_core13.ServiceContext,
|
|
17045
|
+
SharingService: () => import_sdk_core9.SharingService,
|
|
17046
|
+
Space: () => import_sdk_core11.Space,
|
|
17047
|
+
SpaceErrorCodes: () => import_sdk_core11.SpaceErrorCodes,
|
|
17048
|
+
SpaceService: () => import_sdk_core11.SpaceService,
|
|
17048
17049
|
TinyCloud: () => import_sdk_core5.TinyCloud,
|
|
17049
17050
|
TinyCloudNode: () => TinyCloudNode,
|
|
17050
|
-
|
|
17051
|
+
VaultAction: () => import_sdk_core8.VaultAction,
|
|
17052
|
+
VaultHeaders: () => import_sdk_core8.VaultHeaders,
|
|
17053
|
+
VersionCheckError: () => import_sdk_core12.VersionCheckError,
|
|
17051
17054
|
WasmKeyProvider: () => WasmKeyProvider,
|
|
17052
|
-
buildSpaceUri: () =>
|
|
17053
|
-
checkNodeVersion: () =>
|
|
17054
|
-
createCapabilityKeyRegistry: () =>
|
|
17055
|
-
createSharingService: () =>
|
|
17056
|
-
createSpaceService: () =>
|
|
17055
|
+
buildSpaceUri: () => import_sdk_core11.buildSpaceUri,
|
|
17056
|
+
checkNodeVersion: () => import_sdk_core12.checkNodeVersion,
|
|
17057
|
+
createCapabilityKeyRegistry: () => import_sdk_core10.createCapabilityKeyRegistry,
|
|
17058
|
+
createSharingService: () => import_sdk_core9.createSharingService,
|
|
17059
|
+
createSpaceService: () => import_sdk_core11.createSpaceService,
|
|
17060
|
+
createVaultCrypto: () => import_sdk_core8.createVaultCrypto,
|
|
17057
17061
|
createWasmKeyProvider: () => createWasmKeyProvider,
|
|
17058
17062
|
defaultSignStrategy: () => defaultSignStrategy,
|
|
17059
17063
|
deserializeDelegation: () => deserializeDelegation,
|
|
17060
|
-
makePublicSpaceId: () =>
|
|
17061
|
-
parseSpaceUri: () =>
|
|
17064
|
+
makePublicSpaceId: () => import_sdk_core11.makePublicSpaceId,
|
|
17065
|
+
parseSpaceUri: () => import_sdk_core11.parseSpaceUri,
|
|
17062
17066
|
serializeDelegation: () => serializeDelegation
|
|
17063
17067
|
});
|
|
17064
17068
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -17365,6 +17369,7 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17365
17369
|
this.sessionExpirationMs = config.sessionExpirationMs ?? 60 * 60 * 1e3;
|
|
17366
17370
|
this.autoCreateSpace = config.autoCreateSpace ?? false;
|
|
17367
17371
|
this.tinycloudHosts = config.tinycloudHosts ?? ["https://node.tinycloud.xyz"];
|
|
17372
|
+
this.enablePublicSpace = config.enablePublicSpace ?? true;
|
|
17368
17373
|
this.sessionManager = new import_node_sdk_wasm2.TCWSessionManager();
|
|
17369
17374
|
}
|
|
17370
17375
|
/**
|
|
@@ -17396,12 +17401,12 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17396
17401
|
* Create the space on the TinyCloud server (host delegation).
|
|
17397
17402
|
* This registers the user as the owner of the space.
|
|
17398
17403
|
*/
|
|
17399
|
-
async hostSpace() {
|
|
17404
|
+
async hostSpace(targetSpaceId) {
|
|
17400
17405
|
if (!this._tinyCloudSession || !this._address || !this._chainId) {
|
|
17401
17406
|
throw new Error("Must be signed in to host space");
|
|
17402
17407
|
}
|
|
17403
17408
|
const host = this.tinycloudHosts[0];
|
|
17404
|
-
const spaceId = this._tinyCloudSession.spaceId;
|
|
17409
|
+
const spaceId = targetSpaceId ?? this._tinyCloudSession.spaceId;
|
|
17405
17410
|
const peerId = await (0, import_sdk_core2.fetchPeerId)(host, spaceId);
|
|
17406
17411
|
const siwe = (0, import_node_sdk_wasm2.generateHostSIWEMessage)({
|
|
17407
17412
|
address: this._address,
|
|
@@ -17416,6 +17421,13 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17416
17421
|
const result = await (0, import_sdk_core2.submitHostDelegation)(host, headers);
|
|
17417
17422
|
return result.success;
|
|
17418
17423
|
}
|
|
17424
|
+
/**
|
|
17425
|
+
* Create a specific space on the server via host delegation.
|
|
17426
|
+
* Used for lazy creation of additional spaces (e.g., public).
|
|
17427
|
+
*/
|
|
17428
|
+
async hostPublicSpace(spaceId) {
|
|
17429
|
+
return this.hostSpace(spaceId);
|
|
17430
|
+
}
|
|
17419
17431
|
/**
|
|
17420
17432
|
* Ensure the user's space exists on the TinyCloud server.
|
|
17421
17433
|
* Creates the space if it doesn't exist and autoCreateSpace is enabled.
|
|
@@ -17429,11 +17441,33 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17429
17441
|
throw new Error("Must be signed in to ensure space exists");
|
|
17430
17442
|
}
|
|
17431
17443
|
const host = this.tinycloudHosts[0];
|
|
17444
|
+
const primarySpaceId = this._tinyCloudSession.spaceId;
|
|
17432
17445
|
const result = await (0, import_sdk_core2.activateSessionWithHost)(
|
|
17433
17446
|
host,
|
|
17434
17447
|
this._tinyCloudSession.delegationHeader
|
|
17435
17448
|
);
|
|
17436
17449
|
if (result.success) {
|
|
17450
|
+
const primarySkipped = result.skipped?.includes(primarySpaceId);
|
|
17451
|
+
if (!primarySkipped) {
|
|
17452
|
+
return;
|
|
17453
|
+
}
|
|
17454
|
+
if (!this.autoCreateSpace) {
|
|
17455
|
+
return;
|
|
17456
|
+
}
|
|
17457
|
+
const created = await this.hostSpace();
|
|
17458
|
+
if (!created) {
|
|
17459
|
+
throw new Error(`Failed to create space: ${primarySpaceId}`);
|
|
17460
|
+
}
|
|
17461
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
17462
|
+
const retryResult = await (0, import_sdk_core2.activateSessionWithHost)(
|
|
17463
|
+
host,
|
|
17464
|
+
this._tinyCloudSession.delegationHeader
|
|
17465
|
+
);
|
|
17466
|
+
if (!retryResult.success) {
|
|
17467
|
+
throw new Error(
|
|
17468
|
+
`Failed to activate session after creating space: ${retryResult.error}`
|
|
17469
|
+
);
|
|
17470
|
+
}
|
|
17437
17471
|
return;
|
|
17438
17472
|
}
|
|
17439
17473
|
if (result.status === 404) {
|
|
@@ -17442,9 +17476,7 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17442
17476
|
}
|
|
17443
17477
|
const created = await this.hostSpace();
|
|
17444
17478
|
if (!created) {
|
|
17445
|
-
throw new Error(
|
|
17446
|
-
`Failed to create space: ${this._tinyCloudSession.spaceId}`
|
|
17447
|
-
);
|
|
17479
|
+
throw new Error(`Failed to create space: ${primarySpaceId}`);
|
|
17448
17480
|
}
|
|
17449
17481
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
17450
17482
|
const retryResult = await (0, import_sdk_core2.activateSessionWithHost)(
|
|
@@ -17512,11 +17544,13 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17512
17544
|
siwe: prepared.siwe,
|
|
17513
17545
|
signature: signature2
|
|
17514
17546
|
};
|
|
17547
|
+
const spacesMetadata = this.enablePublicSpace ? { public: (0, import_node_sdk_wasm2.makeSpaceId)(address, chainId, "public") } : void 0;
|
|
17515
17548
|
const tinyCloudSession = {
|
|
17516
17549
|
address,
|
|
17517
17550
|
chainId,
|
|
17518
17551
|
sessionKey: keyId,
|
|
17519
17552
|
spaceId,
|
|
17553
|
+
spaces: spacesMetadata,
|
|
17520
17554
|
delegationCid: session.delegationCid,
|
|
17521
17555
|
delegationHeader: session.delegationHeader,
|
|
17522
17556
|
verificationMethod: this.sessionManager.getDID(keyId),
|
|
@@ -17534,6 +17568,7 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17534
17568
|
delegationHeader: session.delegationHeader,
|
|
17535
17569
|
delegationCid: session.delegationCid,
|
|
17536
17570
|
spaceId,
|
|
17571
|
+
spaces: spacesMetadata,
|
|
17537
17572
|
verificationMethod: this.sessionManager.getDID(keyId)
|
|
17538
17573
|
},
|
|
17539
17574
|
expiresAt: expirationTime.toISOString(),
|
|
@@ -17663,11 +17698,13 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17663
17698
|
siwe: prepared.siwe,
|
|
17664
17699
|
signature: signature2
|
|
17665
17700
|
};
|
|
17701
|
+
const spacesMetadata = this.enablePublicSpace ? { public: (0, import_node_sdk_wasm2.makeSpaceId)(address, chainId, "public") } : void 0;
|
|
17666
17702
|
const tinyCloudSession = {
|
|
17667
17703
|
address,
|
|
17668
17704
|
chainId,
|
|
17669
17705
|
sessionKey: keyId,
|
|
17670
17706
|
spaceId: prepared.spaceId,
|
|
17707
|
+
spaces: spacesMetadata,
|
|
17671
17708
|
delegationCid: session.delegationCid,
|
|
17672
17709
|
delegationHeader: session.delegationHeader,
|
|
17673
17710
|
verificationMethod: this.sessionManager.getDID(keyId),
|
|
@@ -17689,6 +17726,7 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17689
17726
|
delegationHeader: session.delegationHeader,
|
|
17690
17727
|
delegationCid: session.delegationCid,
|
|
17691
17728
|
spaceId: prepared.spaceId,
|
|
17729
|
+
spaces: spacesMetadata,
|
|
17692
17730
|
verificationMethod: this.sessionManager.getDID(keyId)
|
|
17693
17731
|
},
|
|
17694
17732
|
expiresAt,
|
|
@@ -18009,7 +18047,8 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18009
18047
|
spacePrefix: config.prefix,
|
|
18010
18048
|
sessionExpirationMs: config.sessionExpirationMs ?? 60 * 60 * 1e3,
|
|
18011
18049
|
tinycloudHosts: [host],
|
|
18012
|
-
autoCreateSpace: config.autoCreateSpace
|
|
18050
|
+
autoCreateSpace: config.autoCreateSpace,
|
|
18051
|
+
enablePublicSpace: config.enablePublicSpace ?? true
|
|
18013
18052
|
});
|
|
18014
18053
|
this.tc = new import_sdk_core4.TinyCloud(this.auth);
|
|
18015
18054
|
}
|
|
@@ -18083,6 +18122,46 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18083
18122
|
await this.tc.signIn();
|
|
18084
18123
|
this.initializeServices();
|
|
18085
18124
|
}
|
|
18125
|
+
/**
|
|
18126
|
+
* Restore a previously established session from stored delegation data.
|
|
18127
|
+
*
|
|
18128
|
+
* This is used by the CLI to restore a session that was created via the
|
|
18129
|
+
* browser-based delegation flow (OpenKey `/delegate` page). Instead of
|
|
18130
|
+
* signing in with a private key, it injects the delegation data directly.
|
|
18131
|
+
*
|
|
18132
|
+
* @param sessionData - The stored delegation data from the browser flow
|
|
18133
|
+
*/
|
|
18134
|
+
async restoreSession(sessionData) {
|
|
18135
|
+
this._kv = void 0;
|
|
18136
|
+
this._sql = void 0;
|
|
18137
|
+
this._serviceContext = void 0;
|
|
18138
|
+
if (sessionData.address) {
|
|
18139
|
+
this._address = sessionData.address;
|
|
18140
|
+
}
|
|
18141
|
+
if (sessionData.chainId) {
|
|
18142
|
+
this._chainId = sessionData.chainId;
|
|
18143
|
+
}
|
|
18144
|
+
this._serviceContext = new import_sdk_core4.ServiceContext({
|
|
18145
|
+
invoke: import_node_sdk_wasm4.invoke,
|
|
18146
|
+
fetch: globalThis.fetch.bind(globalThis),
|
|
18147
|
+
hosts: [this.config.host]
|
|
18148
|
+
});
|
|
18149
|
+
this._kv = new import_sdk_core4.KVService({});
|
|
18150
|
+
this._kv.initialize(this._serviceContext);
|
|
18151
|
+
this._serviceContext.registerService("kv", this._kv);
|
|
18152
|
+
this._sql = new import_sdk_core4.SQLService({});
|
|
18153
|
+
this._sql.initialize(this._serviceContext);
|
|
18154
|
+
this._serviceContext.registerService("sql", this._sql);
|
|
18155
|
+
const serviceSession = {
|
|
18156
|
+
delegationHeader: sessionData.delegationHeader,
|
|
18157
|
+
delegationCid: sessionData.delegationCid,
|
|
18158
|
+
spaceId: sessionData.spaceId,
|
|
18159
|
+
verificationMethod: sessionData.verificationMethod,
|
|
18160
|
+
jwk: sessionData.jwk
|
|
18161
|
+
};
|
|
18162
|
+
this._serviceContext.setSession(serviceSession);
|
|
18163
|
+
this.initializeV2Services(serviceSession);
|
|
18164
|
+
}
|
|
18086
18165
|
/**
|
|
18087
18166
|
* Connect a wallet to upgrade from session-only mode to wallet mode.
|
|
18088
18167
|
*
|
|
@@ -18124,7 +18203,8 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18124
18203
|
spacePrefix: prefix,
|
|
18125
18204
|
sessionExpirationMs: this.config.sessionExpirationMs ?? 60 * 60 * 1e3,
|
|
18126
18205
|
tinycloudHosts: [host],
|
|
18127
|
-
autoCreateSpace: this.config.autoCreateSpace
|
|
18206
|
+
autoCreateSpace: this.config.autoCreateSpace,
|
|
18207
|
+
enablePublicSpace: this.config.enablePublicSpace ?? true
|
|
18128
18208
|
});
|
|
18129
18209
|
this.tc = new import_sdk_core4.TinyCloud(this.auth);
|
|
18130
18210
|
this.config.prefix = prefix;
|
|
@@ -18138,6 +18218,7 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18138
18218
|
if (!session) {
|
|
18139
18219
|
return;
|
|
18140
18220
|
}
|
|
18221
|
+
this.tc.initializeServices(import_node_sdk_wasm4.invoke, [this.config.host]);
|
|
18141
18222
|
this._serviceContext = new import_sdk_core4.ServiceContext({
|
|
18142
18223
|
invoke: import_node_sdk_wasm4.invoke,
|
|
18143
18224
|
fetch: globalThis.fetch.bind(globalThis),
|
|
@@ -18157,6 +18238,43 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18157
18238
|
jwk: session.jwk
|
|
18158
18239
|
};
|
|
18159
18240
|
this._serviceContext.setSession(serviceSession);
|
|
18241
|
+
this.tc.serviceContext.setSession(serviceSession);
|
|
18242
|
+
const vaultCrypto = (0, import_sdk_core4.createVaultCrypto)({
|
|
18243
|
+
vault_encrypt: import_node_sdk_wasm4.vault_encrypt,
|
|
18244
|
+
vault_decrypt: import_node_sdk_wasm4.vault_decrypt,
|
|
18245
|
+
vault_derive_key: import_node_sdk_wasm4.vault_derive_key,
|
|
18246
|
+
vault_x25519_from_seed: import_node_sdk_wasm4.vault_x25519_from_seed,
|
|
18247
|
+
vault_x25519_dh: import_node_sdk_wasm4.vault_x25519_dh,
|
|
18248
|
+
vault_random_bytes: import_node_sdk_wasm4.vault_random_bytes,
|
|
18249
|
+
vault_sha256: import_node_sdk_wasm4.vault_sha256
|
|
18250
|
+
});
|
|
18251
|
+
const self2 = this;
|
|
18252
|
+
this._vault = new import_sdk_core4.DataVaultService({
|
|
18253
|
+
spaceId: session.spaceId,
|
|
18254
|
+
crypto: vaultCrypto,
|
|
18255
|
+
tc: {
|
|
18256
|
+
kv: this._kv,
|
|
18257
|
+
ensurePublicSpace: async () => {
|
|
18258
|
+
try {
|
|
18259
|
+
await self2.ensurePublicSpace();
|
|
18260
|
+
return { ok: true, data: void 0 };
|
|
18261
|
+
} catch (error) {
|
|
18262
|
+
return { ok: false, error: { code: "STORAGE_ERROR", message: error instanceof Error ? error.message : String(error), service: "vault" } };
|
|
18263
|
+
}
|
|
18264
|
+
},
|
|
18265
|
+
get publicKV() {
|
|
18266
|
+
return self2._publicKV ?? self2.tc.publicKV;
|
|
18267
|
+
},
|
|
18268
|
+
readPublicSpace: (host, spaceId, key2) => import_sdk_core4.TinyCloud.readPublicSpace(host, spaceId, key2),
|
|
18269
|
+
makePublicSpaceId: import_sdk_core4.TinyCloud.makePublicSpaceId,
|
|
18270
|
+
did: this.did,
|
|
18271
|
+
address: this._address,
|
|
18272
|
+
chainId: this._chainId,
|
|
18273
|
+
hosts: [this.config.host]
|
|
18274
|
+
}
|
|
18275
|
+
});
|
|
18276
|
+
this._vault.initialize(this._serviceContext);
|
|
18277
|
+
this._serviceContext.registerService("vault", this._vault);
|
|
18160
18278
|
this.initializeV2Services(serviceSession);
|
|
18161
18279
|
}
|
|
18162
18280
|
/**
|
|
@@ -18197,7 +18315,32 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18197
18315
|
isRevoked: false,
|
|
18198
18316
|
allowSubDelegation: true
|
|
18199
18317
|
};
|
|
18200
|
-
|
|
18318
|
+
const delegations = [rootDelegation];
|
|
18319
|
+
if (tcSession.spaces) {
|
|
18320
|
+
for (const [spaceName, spaceId] of Object.entries(tcSession.spaces)) {
|
|
18321
|
+
delegations.push({
|
|
18322
|
+
cid: tcSession.delegationCid,
|
|
18323
|
+
delegateDID: tcSession.verificationMethod,
|
|
18324
|
+
spaceId,
|
|
18325
|
+
path: "",
|
|
18326
|
+
actions: [
|
|
18327
|
+
"tinycloud.kv/put",
|
|
18328
|
+
"tinycloud.kv/get",
|
|
18329
|
+
"tinycloud.kv/del",
|
|
18330
|
+
"tinycloud.kv/list",
|
|
18331
|
+
"tinycloud.kv/metadata",
|
|
18332
|
+
"tinycloud.sql/read",
|
|
18333
|
+
"tinycloud.sql/write",
|
|
18334
|
+
"tinycloud.sql/admin",
|
|
18335
|
+
"tinycloud.sql/*"
|
|
18336
|
+
],
|
|
18337
|
+
expiry: this.getSessionExpiry(),
|
|
18338
|
+
isRevoked: false,
|
|
18339
|
+
allowSubDelegation: true
|
|
18340
|
+
});
|
|
18341
|
+
}
|
|
18342
|
+
}
|
|
18343
|
+
this._capabilityRegistry.registerKey(sessionKey, delegations);
|
|
18201
18344
|
}
|
|
18202
18345
|
this._delegationManager = new import_sdk_core4.DelegationManager({
|
|
18203
18346
|
hosts: [this.config.host],
|
|
@@ -18215,7 +18358,16 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18215
18358
|
createKVService: (spaceId) => {
|
|
18216
18359
|
const kvService = new import_sdk_core4.KVService({});
|
|
18217
18360
|
if (this._serviceContext) {
|
|
18218
|
-
|
|
18361
|
+
const spaceScopedContext = new import_sdk_core4.ServiceContext({
|
|
18362
|
+
invoke: this._serviceContext.invoke,
|
|
18363
|
+
fetch: this._serviceContext.fetch,
|
|
18364
|
+
hosts: this._serviceContext.hosts
|
|
18365
|
+
});
|
|
18366
|
+
const session = this._serviceContext.session;
|
|
18367
|
+
if (session) {
|
|
18368
|
+
spaceScopedContext.setSession({ ...session, spaceId });
|
|
18369
|
+
}
|
|
18370
|
+
kvService.initialize(spaceScopedContext);
|
|
18219
18371
|
}
|
|
18220
18372
|
return kvService;
|
|
18221
18373
|
},
|
|
@@ -18350,6 +18502,16 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18350
18502
|
}
|
|
18351
18503
|
return this._sql;
|
|
18352
18504
|
}
|
|
18505
|
+
/**
|
|
18506
|
+
* Data Vault operations - client-side encrypted KV storage.
|
|
18507
|
+
* Call `vault.unlock(signer)` after signIn() to derive encryption keys.
|
|
18508
|
+
*/
|
|
18509
|
+
get vault() {
|
|
18510
|
+
if (!this._vault) {
|
|
18511
|
+
throw new Error("Not signed in. Call signIn() first.");
|
|
18512
|
+
}
|
|
18513
|
+
return this._vault;
|
|
18514
|
+
}
|
|
18353
18515
|
// ===========================================================================
|
|
18354
18516
|
// v2 Service Accessors
|
|
18355
18517
|
// ===========================================================================
|
|
@@ -18523,21 +18685,99 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18523
18685
|
// Public Space Methods
|
|
18524
18686
|
// ===========================================================================
|
|
18525
18687
|
/**
|
|
18526
|
-
* Ensure the user's public space exists.
|
|
18527
|
-
* Creates
|
|
18528
|
-
*
|
|
18688
|
+
* Ensure the user's public space exists and is accessible.
|
|
18689
|
+
* Creates the space and activates a session delegation for it.
|
|
18690
|
+
* This is the trigger for lazy public space creation — call it
|
|
18691
|
+
* before writing to spaces.get('public').kv.
|
|
18529
18692
|
*/
|
|
18530
18693
|
async ensurePublicSpace() {
|
|
18531
|
-
if (!this.
|
|
18694
|
+
if (!this.auth || !this.session || !this.signer) {
|
|
18532
18695
|
throw new Error("Not signed in. Call signIn() first.");
|
|
18533
18696
|
}
|
|
18534
|
-
|
|
18697
|
+
const publicSpaceId = this.session.spaces?.public;
|
|
18698
|
+
if (!publicSpaceId) {
|
|
18699
|
+
throw new Error("Public space not enabled. Set enablePublicSpace: true in config.");
|
|
18700
|
+
}
|
|
18701
|
+
await this.auth.hostPublicSpace(publicSpaceId);
|
|
18702
|
+
const kvActions = [
|
|
18703
|
+
"tinycloud.kv/put",
|
|
18704
|
+
"tinycloud.kv/get",
|
|
18705
|
+
"tinycloud.kv/del",
|
|
18706
|
+
"tinycloud.kv/list",
|
|
18707
|
+
"tinycloud.kv/metadata"
|
|
18708
|
+
];
|
|
18709
|
+
const abilities = { kv: { "": kvActions } };
|
|
18710
|
+
const now = /* @__PURE__ */ new Date();
|
|
18711
|
+
const expiryMs = 60 * 60 * 1e3;
|
|
18712
|
+
const expirationTime = new Date(now.getTime() + expiryMs);
|
|
18713
|
+
const prepared = (0, import_node_sdk_wasm4.prepareSession)({
|
|
18714
|
+
abilities,
|
|
18715
|
+
address: (0, import_node_sdk_wasm4.ensureEip55)(this.session.address),
|
|
18716
|
+
chainId: this.session.chainId,
|
|
18717
|
+
domain: new URL(this.config.host).hostname,
|
|
18718
|
+
issuedAt: now.toISOString(),
|
|
18719
|
+
expirationTime: expirationTime.toISOString(),
|
|
18720
|
+
spaceId: publicSpaceId,
|
|
18721
|
+
jwk: this.session.jwk,
|
|
18722
|
+
parents: [this.session.delegationCid]
|
|
18723
|
+
});
|
|
18724
|
+
const signature2 = await this.signer.signMessage(prepared.siwe);
|
|
18725
|
+
const delegationSession = (0, import_node_sdk_wasm4.completeSessionSetup)({
|
|
18726
|
+
...prepared,
|
|
18727
|
+
signature: signature2
|
|
18728
|
+
});
|
|
18729
|
+
const activateResult = await (0, import_sdk_core4.activateSessionWithHost)(
|
|
18730
|
+
this.config.host,
|
|
18731
|
+
delegationSession.delegationHeader
|
|
18732
|
+
);
|
|
18733
|
+
if (!activateResult.success) {
|
|
18734
|
+
throw new Error(`Failed to activate public space delegation: ${activateResult.error}`);
|
|
18735
|
+
}
|
|
18736
|
+
if (this._capabilityRegistry && this.session) {
|
|
18737
|
+
const sessionKey = {
|
|
18738
|
+
id: this.session.sessionKey,
|
|
18739
|
+
did: this.session.verificationMethod,
|
|
18740
|
+
type: "session",
|
|
18741
|
+
jwk: this.session.jwk,
|
|
18742
|
+
priority: 0
|
|
18743
|
+
};
|
|
18744
|
+
this._capabilityRegistry.registerKey(sessionKey, [{
|
|
18745
|
+
cid: delegationSession.delegationCid,
|
|
18746
|
+
delegateDID: this.session.verificationMethod,
|
|
18747
|
+
spaceId: publicSpaceId,
|
|
18748
|
+
path: "",
|
|
18749
|
+
actions: kvActions,
|
|
18750
|
+
expiry: expirationTime,
|
|
18751
|
+
isRevoked: false,
|
|
18752
|
+
allowSubDelegation: true
|
|
18753
|
+
}]);
|
|
18754
|
+
}
|
|
18755
|
+
if (this._serviceContext) {
|
|
18756
|
+
const publicKV = new import_sdk_core4.KVService({ prefix: "" });
|
|
18757
|
+
const publicContext = new import_sdk_core4.ServiceContext({
|
|
18758
|
+
invoke: import_node_sdk_wasm4.invoke,
|
|
18759
|
+
fetch: this._serviceContext.fetch,
|
|
18760
|
+
hosts: this._serviceContext.hosts
|
|
18761
|
+
});
|
|
18762
|
+
publicContext.setSession({
|
|
18763
|
+
delegationHeader: delegationSession.delegationHeader,
|
|
18764
|
+
delegationCid: delegationSession.delegationCid,
|
|
18765
|
+
spaceId: publicSpaceId,
|
|
18766
|
+
verificationMethod: this.session.verificationMethod,
|
|
18767
|
+
jwk: this.session.jwk
|
|
18768
|
+
});
|
|
18769
|
+
publicKV.initialize(publicContext);
|
|
18770
|
+
this._publicKV = publicKV;
|
|
18771
|
+
}
|
|
18535
18772
|
}
|
|
18536
18773
|
/**
|
|
18537
18774
|
* Get a KVService scoped to the user's own public space.
|
|
18538
18775
|
* Writes require authentication (owner/delegate).
|
|
18539
18776
|
*/
|
|
18540
18777
|
get publicKV() {
|
|
18778
|
+
if (this._publicKV) {
|
|
18779
|
+
return this._publicKV;
|
|
18780
|
+
}
|
|
18541
18781
|
if (!this.tc) {
|
|
18542
18782
|
throw new Error("Not signed in. Call signIn() first.");
|
|
18543
18783
|
}
|
|
@@ -18635,7 +18875,7 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18635
18875
|
domain: new URL(this.config.host).hostname,
|
|
18636
18876
|
issuedAt: now.toISOString(),
|
|
18637
18877
|
expirationTime: expirationTime.toISOString(),
|
|
18638
|
-
spaceId: session.spaceId,
|
|
18878
|
+
spaceId: params.spaceIdOverride ?? session.spaceId,
|
|
18639
18879
|
delegateUri: params.delegateDID,
|
|
18640
18880
|
parents: [session.delegationCid]
|
|
18641
18881
|
});
|
|
@@ -18654,7 +18894,7 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18654
18894
|
return {
|
|
18655
18895
|
cid: delegationSession.delegationCid,
|
|
18656
18896
|
delegationHeader: delegationSession.delegationHeader,
|
|
18657
|
-
spaceId: session.spaceId,
|
|
18897
|
+
spaceId: params.spaceIdOverride ?? session.spaceId,
|
|
18658
18898
|
path: params.path,
|
|
18659
18899
|
actions: params.actions,
|
|
18660
18900
|
disableSubDelegation: params.disableSubDelegation ?? false,
|
|
@@ -18873,10 +19113,12 @@ var import_sdk_core9 = require("@tinycloud/sdk-core");
|
|
|
18873
19113
|
var import_sdk_core10 = require("@tinycloud/sdk-core");
|
|
18874
19114
|
var import_sdk_core11 = require("@tinycloud/sdk-core");
|
|
18875
19115
|
var import_sdk_core12 = require("@tinycloud/sdk-core");
|
|
19116
|
+
var import_sdk_core13 = require("@tinycloud/sdk-core");
|
|
18876
19117
|
// Annotate the CommonJS export names for ESM import in node:
|
|
18877
19118
|
0 && (module.exports = {
|
|
18878
19119
|
CapabilityKeyRegistry,
|
|
18879
19120
|
CapabilityKeyRegistryErrorCodes,
|
|
19121
|
+
DataVaultService,
|
|
18880
19122
|
DatabaseHandle,
|
|
18881
19123
|
DelegatedAccess,
|
|
18882
19124
|
DelegationErrorCodes,
|
|
@@ -18897,6 +19139,8 @@ var import_sdk_core12 = require("@tinycloud/sdk-core");
|
|
|
18897
19139
|
SpaceService,
|
|
18898
19140
|
TinyCloud,
|
|
18899
19141
|
TinyCloudNode,
|
|
19142
|
+
VaultAction,
|
|
19143
|
+
VaultHeaders,
|
|
18900
19144
|
VersionCheckError,
|
|
18901
19145
|
WasmKeyProvider,
|
|
18902
19146
|
buildSpaceUri,
|
|
@@ -18904,6 +19148,7 @@ var import_sdk_core12 = require("@tinycloud/sdk-core");
|
|
|
18904
19148
|
createCapabilityKeyRegistry,
|
|
18905
19149
|
createSharingService,
|
|
18906
19150
|
createSpaceService,
|
|
19151
|
+
createVaultCrypto,
|
|
18907
19152
|
createWasmKeyProvider,
|
|
18908
19153
|
defaultSignStrategy,
|
|
18909
19154
|
deserializeDelegation,
|