@tinycloud/node-sdk 1.7.0 → 2.0.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/core.cjs +2441 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.d.cts +1114 -0
- package/dist/core.d.ts +1114 -0
- package/dist/core.js +2425 -0
- package/dist/core.js.map +1 -0
- package/dist/index.cjs +564 -308
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +35 -1043
- package/dist/index.d.ts +35 -1043
- package/dist/index.js +529 -279
- package/dist/index.js.map +1 -1
- package/package.json +7 -3
package/dist/index.cjs
CHANGED
|
@@ -17025,55 +17025,92 @@ var require_utils2 = __commonJS({
|
|
|
17025
17025
|
// src/index.ts
|
|
17026
17026
|
var index_exports = {};
|
|
17027
17027
|
__export(index_exports, {
|
|
17028
|
-
|
|
17029
|
-
|
|
17030
|
-
|
|
17031
|
-
|
|
17028
|
+
AutoApproveSpaceCreationHandler: () => import_sdk_core6.AutoApproveSpaceCreationHandler,
|
|
17029
|
+
CapabilityKeyRegistry: () => import_sdk_core12.CapabilityKeyRegistry,
|
|
17030
|
+
CapabilityKeyRegistryErrorCodes: () => import_sdk_core12.CapabilityKeyRegistryErrorCodes,
|
|
17031
|
+
DataVaultService: () => import_sdk_core10.DataVaultService,
|
|
17032
|
+
DatabaseHandle: () => import_sdk_core8.DatabaseHandle,
|
|
17032
17033
|
DelegatedAccess: () => DelegatedAccess,
|
|
17033
|
-
DelegationErrorCodes: () =>
|
|
17034
|
-
DelegationManager: () =>
|
|
17035
|
-
DuckDbAction: () =>
|
|
17036
|
-
DuckDbDatabaseHandle: () =>
|
|
17037
|
-
DuckDbService: () =>
|
|
17034
|
+
DelegationErrorCodes: () => import_sdk_core11.DelegationErrorCodes,
|
|
17035
|
+
DelegationManager: () => import_sdk_core11.DelegationManager,
|
|
17036
|
+
DuckDbAction: () => import_sdk_core9.DuckDbAction,
|
|
17037
|
+
DuckDbDatabaseHandle: () => import_sdk_core9.DuckDbDatabaseHandle,
|
|
17038
|
+
DuckDbService: () => import_sdk_core9.DuckDbService,
|
|
17038
17039
|
FileSessionStorage: () => FileSessionStorage,
|
|
17039
|
-
KVService: () =>
|
|
17040
|
+
KVService: () => import_sdk_core7.KVService,
|
|
17040
17041
|
MemorySessionStorage: () => MemorySessionStorage,
|
|
17041
17042
|
NodeUserAuthorization: () => NodeUserAuthorization,
|
|
17042
|
-
|
|
17043
|
+
NodeWasmBindings: () => NodeWasmBindings,
|
|
17044
|
+
PrefixedKVService: () => import_sdk_core7.PrefixedKVService,
|
|
17043
17045
|
PrivateKeySigner: () => PrivateKeySigner,
|
|
17044
|
-
ProtocolMismatchError: () =>
|
|
17045
|
-
SQLAction: () =>
|
|
17046
|
-
SQLService: () =>
|
|
17047
|
-
ServiceContext: () =>
|
|
17048
|
-
SharingService: () =>
|
|
17049
|
-
|
|
17050
|
-
|
|
17051
|
-
|
|
17046
|
+
ProtocolMismatchError: () => import_sdk_core14.ProtocolMismatchError,
|
|
17047
|
+
SQLAction: () => import_sdk_core8.SQLAction,
|
|
17048
|
+
SQLService: () => import_sdk_core8.SQLService,
|
|
17049
|
+
ServiceContext: () => import_sdk_core15.ServiceContext,
|
|
17050
|
+
SharingService: () => import_sdk_core11.SharingService,
|
|
17051
|
+
SilentNotificationHandler: () => import_sdk_core6.SilentNotificationHandler,
|
|
17052
|
+
Space: () => import_sdk_core13.Space,
|
|
17053
|
+
SpaceErrorCodes: () => import_sdk_core13.SpaceErrorCodes,
|
|
17054
|
+
SpaceService: () => import_sdk_core13.SpaceService,
|
|
17052
17055
|
TinyCloud: () => import_sdk_core5.TinyCloud,
|
|
17053
17056
|
TinyCloudNode: () => TinyCloudNode,
|
|
17054
|
-
UnsupportedFeatureError: () =>
|
|
17055
|
-
VaultHeaders: () =>
|
|
17056
|
-
VaultPublicSpaceKVActions: () =>
|
|
17057
|
-
VersionCheckError: () =>
|
|
17057
|
+
UnsupportedFeatureError: () => import_sdk_core14.UnsupportedFeatureError,
|
|
17058
|
+
VaultHeaders: () => import_sdk_core10.VaultHeaders,
|
|
17059
|
+
VaultPublicSpaceKVActions: () => import_sdk_core10.VaultPublicSpaceKVActions,
|
|
17060
|
+
VersionCheckError: () => import_sdk_core14.VersionCheckError,
|
|
17058
17061
|
WasmKeyProvider: () => WasmKeyProvider,
|
|
17059
|
-
buildSpaceUri: () =>
|
|
17060
|
-
checkNodeInfo: () =>
|
|
17061
|
-
createCapabilityKeyRegistry: () =>
|
|
17062
|
-
createSharingService: () =>
|
|
17063
|
-
createSpaceService: () =>
|
|
17064
|
-
createVaultCrypto: () =>
|
|
17062
|
+
buildSpaceUri: () => import_sdk_core13.buildSpaceUri,
|
|
17063
|
+
checkNodeInfo: () => import_sdk_core14.checkNodeInfo,
|
|
17064
|
+
createCapabilityKeyRegistry: () => import_sdk_core12.createCapabilityKeyRegistry,
|
|
17065
|
+
createSharingService: () => import_sdk_core11.createSharingService,
|
|
17066
|
+
createSpaceService: () => import_sdk_core13.createSpaceService,
|
|
17067
|
+
createVaultCrypto: () => import_sdk_core10.createVaultCrypto,
|
|
17065
17068
|
createWasmKeyProvider: () => createWasmKeyProvider,
|
|
17066
17069
|
defaultSignStrategy: () => defaultSignStrategy,
|
|
17070
|
+
defaultSpaceCreationHandler: () => import_sdk_core6.defaultSpaceCreationHandler,
|
|
17067
17071
|
deserializeDelegation: () => deserializeDelegation,
|
|
17068
|
-
makePublicSpaceId: () =>
|
|
17069
|
-
parseSpaceUri: () =>
|
|
17072
|
+
makePublicSpaceId: () => import_sdk_core13.makePublicSpaceId,
|
|
17073
|
+
parseSpaceUri: () => import_sdk_core13.parseSpaceUri,
|
|
17070
17074
|
serializeDelegation: () => serializeDelegation
|
|
17071
17075
|
});
|
|
17072
17076
|
module.exports = __toCommonJS(index_exports);
|
|
17073
|
-
var import_sdk_core5 = require("@tinycloud/sdk-core");
|
|
17074
17077
|
|
|
17075
|
-
// src/
|
|
17078
|
+
// src/NodeWasmBindings.ts
|
|
17076
17079
|
var import_node_sdk_wasm = require("@tinycloud/node-sdk-wasm");
|
|
17080
|
+
var _NodeWasmBindings = class _NodeWasmBindings {
|
|
17081
|
+
constructor() {
|
|
17082
|
+
this.invoke = import_node_sdk_wasm.invoke;
|
|
17083
|
+
this.prepareSession = import_node_sdk_wasm.prepareSession;
|
|
17084
|
+
this.completeSessionSetup = import_node_sdk_wasm.completeSessionSetup;
|
|
17085
|
+
this.ensureEip55 = import_node_sdk_wasm.ensureEip55;
|
|
17086
|
+
this.makeSpaceId = import_node_sdk_wasm.makeSpaceId;
|
|
17087
|
+
this.createDelegation = import_node_sdk_wasm.createDelegation;
|
|
17088
|
+
this.generateHostSIWEMessage = import_node_sdk_wasm.generateHostSIWEMessage;
|
|
17089
|
+
this.siweToDelegationHeaders = import_node_sdk_wasm.siweToDelegationHeaders;
|
|
17090
|
+
this.protocolVersion = import_node_sdk_wasm.protocolVersion;
|
|
17091
|
+
// Vault crypto
|
|
17092
|
+
this.vault_encrypt = import_node_sdk_wasm.vault_encrypt;
|
|
17093
|
+
this.vault_decrypt = import_node_sdk_wasm.vault_decrypt;
|
|
17094
|
+
this.vault_derive_key = import_node_sdk_wasm.vault_derive_key;
|
|
17095
|
+
this.vault_x25519_from_seed = import_node_sdk_wasm.vault_x25519_from_seed;
|
|
17096
|
+
this.vault_x25519_dh = import_node_sdk_wasm.vault_x25519_dh;
|
|
17097
|
+
this.vault_random_bytes = import_node_sdk_wasm.vault_random_bytes;
|
|
17098
|
+
this.vault_sha256 = import_node_sdk_wasm.vault_sha256;
|
|
17099
|
+
if (!_NodeWasmBindings.panicHookInitialized) {
|
|
17100
|
+
(0, import_node_sdk_wasm.initPanicHook)();
|
|
17101
|
+
_NodeWasmBindings.panicHookInitialized = true;
|
|
17102
|
+
}
|
|
17103
|
+
}
|
|
17104
|
+
createSessionManager() {
|
|
17105
|
+
return new import_node_sdk_wasm.TCWSessionManager();
|
|
17106
|
+
}
|
|
17107
|
+
// No ensureInitialized needed — Node WASM is synchronous
|
|
17108
|
+
};
|
|
17109
|
+
_NodeWasmBindings.panicHookInitialized = false;
|
|
17110
|
+
var NodeWasmBindings = _NodeWasmBindings;
|
|
17111
|
+
|
|
17112
|
+
// src/signers/PrivateKeySigner.ts
|
|
17113
|
+
var import_node_sdk_wasm2 = require("@tinycloud/node-sdk-wasm");
|
|
17077
17114
|
var PrivateKeySigner = class {
|
|
17078
17115
|
/**
|
|
17079
17116
|
* Create a new PrivateKeySigner.
|
|
@@ -17111,7 +17148,7 @@ var PrivateKeySigner = class {
|
|
|
17111
17148
|
const pubKeyWithoutPrefix = publicKey.slice(4);
|
|
17112
17149
|
const hash3 = keccak2563("0x" + pubKeyWithoutPrefix);
|
|
17113
17150
|
const address = "0x" + hash3.slice(-40);
|
|
17114
|
-
return (0,
|
|
17151
|
+
return (0, import_node_sdk_wasm2.ensureEip55)(address);
|
|
17115
17152
|
} catch {
|
|
17116
17153
|
return "0x" + this.privateKeyHex.slice(0, 40);
|
|
17117
17154
|
}
|
|
@@ -17130,11 +17167,20 @@ var PrivateKeySigner = class {
|
|
|
17130
17167
|
*/
|
|
17131
17168
|
async signMessage(message) {
|
|
17132
17169
|
const messageStr = typeof message === "string" ? message : Buffer.from(message).toString("utf-8");
|
|
17133
|
-
const signature2 = (0,
|
|
17170
|
+
const signature2 = (0, import_node_sdk_wasm2.signEthereumMessage)(messageStr, this.privateKeyHex);
|
|
17134
17171
|
return signature2.startsWith("0x") ? signature2 : "0x" + signature2;
|
|
17135
17172
|
}
|
|
17136
17173
|
};
|
|
17137
17174
|
|
|
17175
|
+
// src/TinyCloudNode.ts
|
|
17176
|
+
var import_sdk_core3 = require("@tinycloud/sdk-core");
|
|
17177
|
+
|
|
17178
|
+
// src/authorization/NodeUserAuthorization.ts
|
|
17179
|
+
var import_sdk_core = require("@tinycloud/sdk-core");
|
|
17180
|
+
|
|
17181
|
+
// src/authorization/strategies.ts
|
|
17182
|
+
var defaultSignStrategy = { type: "auto-sign" };
|
|
17183
|
+
|
|
17138
17184
|
// src/storage/MemorySessionStorage.ts
|
|
17139
17185
|
var MemorySessionStorage = class {
|
|
17140
17186
|
constructor() {
|
|
@@ -17206,142 +17252,12 @@ var MemorySessionStorage = class {
|
|
|
17206
17252
|
}
|
|
17207
17253
|
};
|
|
17208
17254
|
|
|
17209
|
-
// src/storage/FileSessionStorage.ts
|
|
17210
|
-
var import_sdk_core = require("@tinycloud/sdk-core");
|
|
17211
|
-
var import_fs = require("fs");
|
|
17212
|
-
var import_path = require("path");
|
|
17213
|
-
var FileSessionStorage = class {
|
|
17214
|
-
/**
|
|
17215
|
-
* Create a new FileSessionStorage.
|
|
17216
|
-
*
|
|
17217
|
-
* @param baseDir - Directory to store session files (default: ~/.tinycloud/sessions)
|
|
17218
|
-
*/
|
|
17219
|
-
constructor(baseDir) {
|
|
17220
|
-
this.baseDir = baseDir || this.getDefaultDir();
|
|
17221
|
-
this.ensureDirectoryExists();
|
|
17222
|
-
}
|
|
17223
|
-
/**
|
|
17224
|
-
* Get the default session storage directory.
|
|
17225
|
-
*/
|
|
17226
|
-
getDefaultDir() {
|
|
17227
|
-
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
17228
|
-
return (0, import_path.join)(home, ".tinycloud", "sessions");
|
|
17229
|
-
}
|
|
17230
|
-
/**
|
|
17231
|
-
* Ensure the storage directory exists.
|
|
17232
|
-
*/
|
|
17233
|
-
ensureDirectoryExists() {
|
|
17234
|
-
if (!(0, import_fs.existsSync)(this.baseDir)) {
|
|
17235
|
-
(0, import_fs.mkdirSync)(this.baseDir, { recursive: true });
|
|
17236
|
-
}
|
|
17237
|
-
}
|
|
17238
|
-
/**
|
|
17239
|
-
* Get the file path for an address.
|
|
17240
|
-
*/
|
|
17241
|
-
getFilePath(address) {
|
|
17242
|
-
const normalizedAddress = address.toLowerCase();
|
|
17243
|
-
const filename = `${normalizedAddress.replace("0x", "")}.json`;
|
|
17244
|
-
return (0, import_path.join)(this.baseDir, filename);
|
|
17245
|
-
}
|
|
17246
|
-
/**
|
|
17247
|
-
* Save a session for an address.
|
|
17248
|
-
*/
|
|
17249
|
-
async save(address, session) {
|
|
17250
|
-
const filePath = this.getFilePath(address);
|
|
17251
|
-
const data = JSON.stringify(session, null, 2);
|
|
17252
|
-
(0, import_fs.writeFileSync)(filePath, data, "utf-8");
|
|
17253
|
-
}
|
|
17254
|
-
/**
|
|
17255
|
-
* Load a session for an address.
|
|
17256
|
-
*/
|
|
17257
|
-
async load(address) {
|
|
17258
|
-
const filePath = this.getFilePath(address);
|
|
17259
|
-
if (!(0, import_fs.existsSync)(filePath)) {
|
|
17260
|
-
return null;
|
|
17261
|
-
}
|
|
17262
|
-
try {
|
|
17263
|
-
const data = (0, import_fs.readFileSync)(filePath, "utf-8");
|
|
17264
|
-
const parsed = JSON.parse(data);
|
|
17265
|
-
const validation = (0, import_sdk_core.validatePersistedSessionData)(parsed);
|
|
17266
|
-
if (!validation.ok) {
|
|
17267
|
-
console.warn(`Invalid session data for ${address}:`, validation.error.message);
|
|
17268
|
-
(0, import_fs.unlinkSync)(filePath);
|
|
17269
|
-
return null;
|
|
17270
|
-
}
|
|
17271
|
-
const session = validation.data;
|
|
17272
|
-
const expiresAt = new Date(session.expiresAt);
|
|
17273
|
-
if (expiresAt < /* @__PURE__ */ new Date()) {
|
|
17274
|
-
(0, import_fs.unlinkSync)(filePath);
|
|
17275
|
-
return null;
|
|
17276
|
-
}
|
|
17277
|
-
return session;
|
|
17278
|
-
} catch (error) {
|
|
17279
|
-
try {
|
|
17280
|
-
(0, import_fs.unlinkSync)(filePath);
|
|
17281
|
-
} catch {
|
|
17282
|
-
}
|
|
17283
|
-
return null;
|
|
17284
|
-
}
|
|
17285
|
-
}
|
|
17286
|
-
/**
|
|
17287
|
-
* Clear a session for an address.
|
|
17288
|
-
*/
|
|
17289
|
-
async clear(address) {
|
|
17290
|
-
const filePath = this.getFilePath(address);
|
|
17291
|
-
if ((0, import_fs.existsSync)(filePath)) {
|
|
17292
|
-
(0, import_fs.unlinkSync)(filePath);
|
|
17293
|
-
}
|
|
17294
|
-
}
|
|
17295
|
-
/**
|
|
17296
|
-
* Check if a session exists for an address.
|
|
17297
|
-
*/
|
|
17298
|
-
exists(address) {
|
|
17299
|
-
const filePath = this.getFilePath(address);
|
|
17300
|
-
if (!(0, import_fs.existsSync)(filePath)) {
|
|
17301
|
-
return false;
|
|
17302
|
-
}
|
|
17303
|
-
try {
|
|
17304
|
-
const data = (0, import_fs.readFileSync)(filePath, "utf-8");
|
|
17305
|
-
const session = JSON.parse(data);
|
|
17306
|
-
const expiresAt = new Date(session.expiresAt);
|
|
17307
|
-
if (expiresAt < /* @__PURE__ */ new Date()) {
|
|
17308
|
-
(0, import_fs.unlinkSync)(filePath);
|
|
17309
|
-
return false;
|
|
17310
|
-
}
|
|
17311
|
-
return true;
|
|
17312
|
-
} catch {
|
|
17313
|
-
return false;
|
|
17314
|
-
}
|
|
17315
|
-
}
|
|
17316
|
-
/**
|
|
17317
|
-
* Check if file system storage is available.
|
|
17318
|
-
*/
|
|
17319
|
-
isAvailable() {
|
|
17320
|
-
try {
|
|
17321
|
-
this.ensureDirectoryExists();
|
|
17322
|
-
return (0, import_fs.existsSync)(this.baseDir);
|
|
17323
|
-
} catch {
|
|
17324
|
-
return false;
|
|
17325
|
-
}
|
|
17326
|
-
}
|
|
17327
|
-
};
|
|
17328
|
-
|
|
17329
17255
|
// src/authorization/NodeUserAuthorization.ts
|
|
17330
|
-
var
|
|
17331
|
-
var import_node_sdk_wasm2 = require("@tinycloud/node-sdk-wasm");
|
|
17332
|
-
|
|
17333
|
-
// src/authorization/strategies.ts
|
|
17334
|
-
var defaultSignStrategy = { type: "auto-sign" };
|
|
17335
|
-
|
|
17336
|
-
// src/authorization/NodeUserAuthorization.ts
|
|
17337
|
-
var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
17256
|
+
var NodeUserAuthorization = class {
|
|
17338
17257
|
constructor(config) {
|
|
17339
17258
|
this.extensions = [];
|
|
17340
17259
|
this._nodeFeatures = [];
|
|
17341
|
-
|
|
17342
|
-
(0, import_node_sdk_wasm2.initPanicHook)();
|
|
17343
|
-
_NodeUserAuthorization.wasmInitialized = true;
|
|
17344
|
-
}
|
|
17260
|
+
this.wasm = config.wasmBindings;
|
|
17345
17261
|
this.signer = config.signer;
|
|
17346
17262
|
this.signStrategy = config.signStrategy ?? defaultSignStrategy;
|
|
17347
17263
|
this.sessionStorage = config.sessionStorage ?? new MemorySessionStorage();
|
|
@@ -17384,9 +17300,10 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17384
17300
|
};
|
|
17385
17301
|
this.sessionExpirationMs = config.sessionExpirationMs ?? 60 * 60 * 1e3;
|
|
17386
17302
|
this.autoCreateSpace = config.autoCreateSpace ?? false;
|
|
17303
|
+
this.spaceCreationHandler = config.spaceCreationHandler;
|
|
17387
17304
|
this.tinycloudHosts = config.tinycloudHosts ?? ["https://node.tinycloud.xyz"];
|
|
17388
17305
|
this.enablePublicSpace = config.enablePublicSpace ?? true;
|
|
17389
|
-
this.sessionManager =
|
|
17306
|
+
this.sessionManager = this.wasm.createSessionManager();
|
|
17390
17307
|
}
|
|
17391
17308
|
/**
|
|
17392
17309
|
* The current active session (web-core compatible).
|
|
@@ -17426,8 +17343,8 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17426
17343
|
}
|
|
17427
17344
|
const host = this.tinycloudHosts[0];
|
|
17428
17345
|
const spaceId = targetSpaceId ?? this._tinyCloudSession.spaceId;
|
|
17429
|
-
const peerId = await (0,
|
|
17430
|
-
const siwe =
|
|
17346
|
+
const peerId = await (0, import_sdk_core.fetchPeerId)(host, spaceId);
|
|
17347
|
+
const siwe = this.wasm.generateHostSIWEMessage({
|
|
17431
17348
|
address: this._address,
|
|
17432
17349
|
chainId: this._chainId,
|
|
17433
17350
|
domain: this.domain,
|
|
@@ -17436,8 +17353,8 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17436
17353
|
peerId
|
|
17437
17354
|
});
|
|
17438
17355
|
const signature2 = await this.signMessage(siwe);
|
|
17439
|
-
const headers =
|
|
17440
|
-
const result = await (0,
|
|
17356
|
+
const headers = this.wasm.siweToDelegationHeaders({ siwe, signature: signature2 });
|
|
17357
|
+
const result = await (0, import_sdk_core.submitHostDelegation)(host, headers);
|
|
17441
17358
|
return result.success;
|
|
17442
17359
|
}
|
|
17443
17360
|
/**
|
|
@@ -17461,52 +17378,87 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17461
17378
|
}
|
|
17462
17379
|
const host = this.tinycloudHosts[0];
|
|
17463
17380
|
const primarySpaceId = this._tinyCloudSession.spaceId;
|
|
17464
|
-
const result = await (0,
|
|
17381
|
+
const result = await (0, import_sdk_core.activateSessionWithHost)(
|
|
17465
17382
|
host,
|
|
17466
17383
|
this._tinyCloudSession.delegationHeader
|
|
17467
17384
|
);
|
|
17385
|
+
const handler = this.spaceCreationHandler ?? (this.autoCreateSpace ? new import_sdk_core.AutoApproveSpaceCreationHandler() : void 0);
|
|
17386
|
+
const creationContext = {
|
|
17387
|
+
spaceId: primarySpaceId,
|
|
17388
|
+
address: this._address,
|
|
17389
|
+
chainId: this._chainId,
|
|
17390
|
+
host
|
|
17391
|
+
};
|
|
17468
17392
|
if (result.success) {
|
|
17469
17393
|
const primarySkipped = result.skipped?.includes(primarySpaceId);
|
|
17470
17394
|
if (!primarySkipped) {
|
|
17471
17395
|
return;
|
|
17472
17396
|
}
|
|
17473
|
-
if (!
|
|
17397
|
+
if (!handler) {
|
|
17474
17398
|
return;
|
|
17475
17399
|
}
|
|
17476
|
-
const
|
|
17477
|
-
if (!
|
|
17478
|
-
|
|
17400
|
+
const confirmed = await handler.confirmSpaceCreation(creationContext);
|
|
17401
|
+
if (!confirmed) {
|
|
17402
|
+
return;
|
|
17403
|
+
}
|
|
17404
|
+
try {
|
|
17405
|
+
const created = await this.hostSpace();
|
|
17406
|
+
if (!created) {
|
|
17407
|
+
const err = new Error(`Failed to create space: ${primarySpaceId}`);
|
|
17408
|
+
handler.onSpaceCreationFailed?.(creationContext, err);
|
|
17409
|
+
throw err;
|
|
17410
|
+
}
|
|
17411
|
+
} catch (error) {
|
|
17412
|
+
handler.onSpaceCreationFailed?.(creationContext, error instanceof Error ? error : new Error(String(error)));
|
|
17413
|
+
throw error;
|
|
17479
17414
|
}
|
|
17480
17415
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
17481
|
-
const retryResult = await (0,
|
|
17416
|
+
const retryResult = await (0, import_sdk_core.activateSessionWithHost)(
|
|
17482
17417
|
host,
|
|
17483
17418
|
this._tinyCloudSession.delegationHeader
|
|
17484
17419
|
);
|
|
17485
17420
|
if (!retryResult.success) {
|
|
17486
|
-
|
|
17421
|
+
const err = new Error(
|
|
17487
17422
|
`Failed to activate session after creating space: ${retryResult.error}`
|
|
17488
17423
|
);
|
|
17424
|
+
handler.onSpaceCreationFailed?.(creationContext, err);
|
|
17425
|
+
throw err;
|
|
17489
17426
|
}
|
|
17427
|
+
handler.onSpaceCreated?.(creationContext);
|
|
17490
17428
|
return;
|
|
17491
17429
|
}
|
|
17492
17430
|
if (result.status === 404) {
|
|
17493
|
-
if (!
|
|
17431
|
+
if (!handler) {
|
|
17494
17432
|
return;
|
|
17495
17433
|
}
|
|
17496
|
-
const
|
|
17497
|
-
if (!
|
|
17498
|
-
|
|
17434
|
+
const confirmed = await handler.confirmSpaceCreation(creationContext);
|
|
17435
|
+
if (!confirmed) {
|
|
17436
|
+
return;
|
|
17437
|
+
}
|
|
17438
|
+
try {
|
|
17439
|
+
const created = await this.hostSpace();
|
|
17440
|
+
if (!created) {
|
|
17441
|
+
const err = new Error(`Failed to create space: ${primarySpaceId}`);
|
|
17442
|
+
handler.onSpaceCreationFailed?.(creationContext, err);
|
|
17443
|
+
throw err;
|
|
17444
|
+
}
|
|
17445
|
+
} catch (error) {
|
|
17446
|
+
handler.onSpaceCreationFailed?.(creationContext, error instanceof Error ? error : new Error(String(error)));
|
|
17447
|
+
throw error;
|
|
17499
17448
|
}
|
|
17500
17449
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
17501
|
-
const retryResult = await (0,
|
|
17450
|
+
const retryResult = await (0, import_sdk_core.activateSessionWithHost)(
|
|
17502
17451
|
host,
|
|
17503
17452
|
this._tinyCloudSession.delegationHeader
|
|
17504
17453
|
);
|
|
17505
17454
|
if (!retryResult.success) {
|
|
17506
|
-
|
|
17455
|
+
const err = new Error(
|
|
17507
17456
|
`Failed to activate session after creating space: ${retryResult.error}`
|
|
17508
17457
|
);
|
|
17458
|
+
handler.onSpaceCreationFailed?.(creationContext, err);
|
|
17459
|
+
throw err;
|
|
17509
17460
|
}
|
|
17461
|
+
handler.onSpaceCreated?.(creationContext);
|
|
17510
17462
|
return;
|
|
17511
17463
|
}
|
|
17512
17464
|
throw new Error(`Failed to activate session: ${result.error}`);
|
|
@@ -17523,7 +17475,7 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17523
17475
|
async signIn() {
|
|
17524
17476
|
this._address = await this.signer.getAddress();
|
|
17525
17477
|
this._chainId = await this.signer.getChainId();
|
|
17526
|
-
const address =
|
|
17478
|
+
const address = this.wasm.ensureEip55(this._address);
|
|
17527
17479
|
const chainId = this._chainId;
|
|
17528
17480
|
const keyId = `session-${Date.now()}`;
|
|
17529
17481
|
this.sessionManager.renameSessionKeyId("default", keyId);
|
|
@@ -17532,10 +17484,10 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17532
17484
|
throw new Error("Failed to create session key");
|
|
17533
17485
|
}
|
|
17534
17486
|
const jwk = JSON.parse(jwkString);
|
|
17535
|
-
const spaceId =
|
|
17487
|
+
const spaceId = this.wasm.makeSpaceId(address, chainId, this.spacePrefix);
|
|
17536
17488
|
const now = /* @__PURE__ */ new Date();
|
|
17537
17489
|
const expirationTime = new Date(now.getTime() + this.sessionExpirationMs);
|
|
17538
|
-
const prepared =
|
|
17490
|
+
const prepared = this.wasm.prepareSession({
|
|
17539
17491
|
abilities: this.defaultActions,
|
|
17540
17492
|
address,
|
|
17541
17493
|
chainId,
|
|
@@ -17551,7 +17503,7 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17551
17503
|
message: prepared.siwe,
|
|
17552
17504
|
type: "siwe"
|
|
17553
17505
|
});
|
|
17554
|
-
const session =
|
|
17506
|
+
const session = this.wasm.completeSessionSetup({
|
|
17555
17507
|
...prepared,
|
|
17556
17508
|
signature: signature2
|
|
17557
17509
|
});
|
|
@@ -17563,7 +17515,7 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17563
17515
|
siwe: prepared.siwe,
|
|
17564
17516
|
signature: signature2
|
|
17565
17517
|
};
|
|
17566
|
-
const spacesMetadata = this.enablePublicSpace ? { public:
|
|
17518
|
+
const spacesMetadata = this.enablePublicSpace ? { public: this.wasm.makeSpaceId(address, chainId, "public") } : void 0;
|
|
17567
17519
|
const tinyCloudSession = {
|
|
17568
17520
|
address,
|
|
17569
17521
|
chainId,
|
|
@@ -17599,7 +17551,7 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17599
17551
|
this._tinyCloudSession = tinyCloudSession;
|
|
17600
17552
|
this._address = address;
|
|
17601
17553
|
this._chainId = chainId;
|
|
17602
|
-
const nodeInfo = await (0,
|
|
17554
|
+
const nodeInfo = await (0, import_sdk_core.checkNodeInfo)(this.tinycloudHosts[0], this.wasm.protocolVersion());
|
|
17603
17555
|
this._nodeFeatures = nodeInfo.features;
|
|
17604
17556
|
for (const ext of this.extensions) {
|
|
17605
17557
|
if (ext.afterSignIn) {
|
|
@@ -17662,7 +17614,7 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17662
17614
|
* ```
|
|
17663
17615
|
*/
|
|
17664
17616
|
async prepareSessionForSigning() {
|
|
17665
|
-
const address =
|
|
17617
|
+
const address = this.wasm.ensureEip55(await this.signer.getAddress());
|
|
17666
17618
|
const chainId = await this.signer.getChainId();
|
|
17667
17619
|
const keyId = `session-${Date.now()}`;
|
|
17668
17620
|
this.sessionManager.renameSessionKeyId("default", keyId);
|
|
@@ -17671,10 +17623,10 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17671
17623
|
throw new Error("Failed to create session key");
|
|
17672
17624
|
}
|
|
17673
17625
|
const jwk = JSON.parse(jwkString);
|
|
17674
|
-
const spaceId =
|
|
17626
|
+
const spaceId = this.wasm.makeSpaceId(address, chainId, this.spacePrefix);
|
|
17675
17627
|
const now = /* @__PURE__ */ new Date();
|
|
17676
17628
|
const expirationTime = new Date(now.getTime() + this.sessionExpirationMs);
|
|
17677
|
-
const prepared =
|
|
17629
|
+
const prepared = this.wasm.prepareSession({
|
|
17678
17630
|
abilities: this.defaultActions,
|
|
17679
17631
|
address,
|
|
17680
17632
|
chainId,
|
|
@@ -17704,11 +17656,11 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17704
17656
|
* @param jwk - The JWK from `prepareSessionForSigning()`
|
|
17705
17657
|
*/
|
|
17706
17658
|
async signInWithPreparedSession(prepared, signature2, keyId, jwk) {
|
|
17707
|
-
const session =
|
|
17659
|
+
const session = this.wasm.completeSessionSetup({
|
|
17708
17660
|
...prepared,
|
|
17709
17661
|
signature: signature2
|
|
17710
17662
|
});
|
|
17711
|
-
const address =
|
|
17663
|
+
const address = this.wasm.ensureEip55(await this.signer.getAddress());
|
|
17712
17664
|
const chainId = await this.signer.getChainId();
|
|
17713
17665
|
const clientSession = {
|
|
17714
17666
|
address,
|
|
@@ -17718,7 +17670,7 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17718
17670
|
siwe: prepared.siwe,
|
|
17719
17671
|
signature: signature2
|
|
17720
17672
|
};
|
|
17721
|
-
const spacesMetadata = this.enablePublicSpace ? { public:
|
|
17673
|
+
const spacesMetadata = this.enablePublicSpace ? { public: this.wasm.makeSpaceId(address, chainId, "public") } : void 0;
|
|
17722
17674
|
const tinyCloudSession = {
|
|
17723
17675
|
address,
|
|
17724
17676
|
chainId,
|
|
@@ -17758,7 +17710,7 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17758
17710
|
this._tinyCloudSession = tinyCloudSession;
|
|
17759
17711
|
this._address = address;
|
|
17760
17712
|
this._chainId = chainId;
|
|
17761
|
-
const nodeInfo = await (0,
|
|
17713
|
+
const nodeInfo = await (0, import_sdk_core.checkNodeInfo)(this.tinycloudHosts[0], this.wasm.protocolVersion());
|
|
17762
17714
|
this._nodeFeatures = nodeInfo.features;
|
|
17763
17715
|
for (const ext of this.extensions) {
|
|
17764
17716
|
if (ext.afterSignIn) {
|
|
@@ -17835,35 +17787,27 @@ var _NodeUserAuthorization = class _NodeUserAuthorization {
|
|
|
17835
17787
|
});
|
|
17836
17788
|
}
|
|
17837
17789
|
};
|
|
17838
|
-
/** Flag to ensure WASM panic hook is only initialized once */
|
|
17839
|
-
_NodeUserAuthorization.wasmInitialized = false;
|
|
17840
|
-
var NodeUserAuthorization = _NodeUserAuthorization;
|
|
17841
|
-
|
|
17842
|
-
// src/TinyCloudNode.ts
|
|
17843
|
-
var import_sdk_core4 = require("@tinycloud/sdk-core");
|
|
17844
|
-
var import_node_sdk_wasm4 = require("@tinycloud/node-sdk-wasm");
|
|
17845
17790
|
|
|
17846
17791
|
// src/DelegatedAccess.ts
|
|
17847
|
-
var
|
|
17848
|
-
var import_node_sdk_wasm3 = require("@tinycloud/node-sdk-wasm");
|
|
17792
|
+
var import_sdk_core2 = require("@tinycloud/sdk-core");
|
|
17849
17793
|
var DelegatedAccess = class {
|
|
17850
|
-
constructor(session, delegation, host) {
|
|
17794
|
+
constructor(session, delegation, host, invoke2) {
|
|
17851
17795
|
this.session = session;
|
|
17852
17796
|
this._delegation = delegation;
|
|
17853
17797
|
this.host = host;
|
|
17854
|
-
this._serviceContext = new
|
|
17855
|
-
invoke:
|
|
17798
|
+
this._serviceContext = new import_sdk_core2.ServiceContext({
|
|
17799
|
+
invoke: invoke2,
|
|
17856
17800
|
fetch: globalThis.fetch.bind(globalThis),
|
|
17857
17801
|
hosts: [host]
|
|
17858
17802
|
});
|
|
17859
17803
|
const prefix = this._delegation.path.replace(/\/$/, "");
|
|
17860
|
-
this._kv = new
|
|
17804
|
+
this._kv = new import_sdk_core2.KVService({ prefix });
|
|
17861
17805
|
this._kv.initialize(this._serviceContext);
|
|
17862
17806
|
this._serviceContext.registerService("kv", this._kv);
|
|
17863
|
-
this._sql = new
|
|
17807
|
+
this._sql = new import_sdk_core2.SQLService({});
|
|
17864
17808
|
this._sql.initialize(this._serviceContext);
|
|
17865
17809
|
this._serviceContext.registerService("sql", this._sql);
|
|
17866
|
-
this._duckdb = new
|
|
17810
|
+
this._duckdb = new import_sdk_core2.DuckDbService({});
|
|
17867
17811
|
this._duckdb.initialize(this._serviceContext);
|
|
17868
17812
|
this._serviceContext.registerService("duckdb", this._duckdb);
|
|
17869
17813
|
const serviceSession = {
|
|
@@ -17970,7 +17914,7 @@ var WasmKeyProvider = class {
|
|
|
17970
17914
|
* @returns Array of key IDs
|
|
17971
17915
|
*/
|
|
17972
17916
|
listKeys() {
|
|
17973
|
-
const keys = this.sessionManager.listSessionKeys();
|
|
17917
|
+
const keys = this.sessionManager.listSessionKeys?.();
|
|
17974
17918
|
return Array.isArray(keys) ? keys : [];
|
|
17975
17919
|
}
|
|
17976
17920
|
/**
|
|
@@ -17990,7 +17934,7 @@ function createWasmKeyProvider(sessionManager) {
|
|
|
17990
17934
|
|
|
17991
17935
|
// src/TinyCloudNode.ts
|
|
17992
17936
|
var DEFAULT_HOST = "https://node.tinycloud.xyz";
|
|
17993
|
-
var
|
|
17937
|
+
var TinyCloudNode = class _TinyCloudNode {
|
|
17994
17938
|
/**
|
|
17995
17939
|
* Create a new TinyCloudNode instance.
|
|
17996
17940
|
*
|
|
@@ -18019,15 +17963,20 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18019
17963
|
this.auth = null;
|
|
18020
17964
|
this.tc = null;
|
|
18021
17965
|
this._chainId = 1;
|
|
18022
|
-
if (!_TinyCloudNode.wasmInitialized) {
|
|
18023
|
-
(0, import_node_sdk_wasm4.initPanicHook)();
|
|
18024
|
-
_TinyCloudNode.wasmInitialized = true;
|
|
18025
|
-
}
|
|
18026
17966
|
this.config = {
|
|
18027
17967
|
...config,
|
|
18028
17968
|
host: config.host ?? DEFAULT_HOST
|
|
18029
17969
|
};
|
|
18030
|
-
|
|
17970
|
+
if (config.wasmBindings) {
|
|
17971
|
+
this.wasmBindings = config.wasmBindings;
|
|
17972
|
+
} else if (_TinyCloudNode.nodeDefaults) {
|
|
17973
|
+
this.wasmBindings = _TinyCloudNode.nodeDefaults.createWasmBindings();
|
|
17974
|
+
} else {
|
|
17975
|
+
throw new Error(
|
|
17976
|
+
"wasmBindings must be provided in config. Import from '@tinycloud/node-sdk' (not '/core') for automatic Node.js defaults."
|
|
17977
|
+
);
|
|
17978
|
+
}
|
|
17979
|
+
this.sessionManager = this.wasmBindings.createSessionManager();
|
|
18031
17980
|
const defaultKeyId = "default";
|
|
18032
17981
|
let jwkStr = this.sessionManager.jwk(defaultKeyId);
|
|
18033
17982
|
if (jwkStr) {
|
|
@@ -18040,22 +17989,23 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18040
17989
|
throw new Error("Failed to get session key JWK");
|
|
18041
17990
|
}
|
|
18042
17991
|
this.sessionKeyJwk = JSON.parse(jwkStr);
|
|
18043
|
-
this._capabilityRegistry = new
|
|
17992
|
+
this._capabilityRegistry = new import_sdk_core3.CapabilityKeyRegistry();
|
|
18044
17993
|
this._keyProvider = new WasmKeyProvider({
|
|
18045
17994
|
sessionManager: this.sessionManager
|
|
18046
17995
|
});
|
|
18047
|
-
this.
|
|
17996
|
+
this.notificationHandler = config.notificationHandler ?? new import_sdk_core3.SilentNotificationHandler();
|
|
17997
|
+
this._sharingService = new import_sdk_core3.SharingService({
|
|
18048
17998
|
hosts: [this.config.host],
|
|
18049
17999
|
// session: undefined - not needed for receive()
|
|
18050
|
-
invoke:
|
|
18000
|
+
invoke: this.wasmBindings.invoke,
|
|
18051
18001
|
fetch: globalThis.fetch.bind(globalThis),
|
|
18052
18002
|
keyProvider: this._keyProvider,
|
|
18053
18003
|
registry: this._capabilityRegistry,
|
|
18054
18004
|
// delegationManager: undefined - not needed for receive()
|
|
18055
18005
|
createKVService: (config2) => {
|
|
18056
18006
|
const prefix = config2.pathPrefix?.replace(/\/$/, "");
|
|
18057
|
-
const kvService = new
|
|
18058
|
-
const kvContext = new
|
|
18007
|
+
const kvService = new import_sdk_core3.KVService({ prefix });
|
|
18008
|
+
const kvContext = new import_sdk_core3.ServiceContext({
|
|
18059
18009
|
invoke: config2.invoke,
|
|
18060
18010
|
fetch: config2.fetch ?? globalThis.fetch.bind(globalThis),
|
|
18061
18011
|
hosts: config2.hosts
|
|
@@ -18065,27 +18015,48 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18065
18015
|
return kvService;
|
|
18066
18016
|
}
|
|
18067
18017
|
});
|
|
18068
|
-
if (config.
|
|
18069
|
-
this.signer =
|
|
18070
|
-
|
|
18071
|
-
|
|
18072
|
-
|
|
18073
|
-
|
|
18074
|
-
|
|
18075
|
-
|
|
18076
|
-
|
|
18077
|
-
|
|
18078
|
-
|
|
18079
|
-
tinycloudHosts: [host],
|
|
18080
|
-
autoCreateSpace: config.autoCreateSpace,
|
|
18081
|
-
enablePublicSpace: config.enablePublicSpace ?? true
|
|
18082
|
-
});
|
|
18083
|
-
this.tc = new import_sdk_core4.TinyCloud(this.auth);
|
|
18018
|
+
if (config.signer) {
|
|
18019
|
+
this.signer = config.signer;
|
|
18020
|
+
this.setupAuth(config);
|
|
18021
|
+
} else if (config.privateKey) {
|
|
18022
|
+
if (!_TinyCloudNode.nodeDefaults) {
|
|
18023
|
+
throw new Error(
|
|
18024
|
+
"privateKey requires PrivateKeySigner. Either provide a signer in config, or import from '@tinycloud/node-sdk' (not '/core') for automatic Node.js defaults."
|
|
18025
|
+
);
|
|
18026
|
+
}
|
|
18027
|
+
this.signer = _TinyCloudNode.nodeDefaults.createSigner(config.privateKey, this._chainId);
|
|
18028
|
+
this.setupAuth(config);
|
|
18084
18029
|
}
|
|
18085
18030
|
}
|
|
18031
|
+
/** @internal Register Node.js-specific defaults (NodeWasmBindings, PrivateKeySigner) */
|
|
18032
|
+
static registerNodeDefaults(defaults) {
|
|
18033
|
+
_TinyCloudNode.nodeDefaults = defaults;
|
|
18034
|
+
}
|
|
18086
18035
|
get nodeFeatures() {
|
|
18087
18036
|
return this.auth?.nodeFeatures ?? [];
|
|
18088
18037
|
}
|
|
18038
|
+
/**
|
|
18039
|
+
* Set up authorization handler and TinyCloud instance.
|
|
18040
|
+
* @internal
|
|
18041
|
+
*/
|
|
18042
|
+
setupAuth(config) {
|
|
18043
|
+
const host = this.config.host;
|
|
18044
|
+
const domain = config.domain ?? new URL(host).hostname;
|
|
18045
|
+
this.auth = new NodeUserAuthorization({
|
|
18046
|
+
signer: this.signer,
|
|
18047
|
+
signStrategy: { type: "auto-sign" },
|
|
18048
|
+
wasmBindings: this.wasmBindings,
|
|
18049
|
+
sessionStorage: config.sessionStorage ?? new MemorySessionStorage(),
|
|
18050
|
+
domain,
|
|
18051
|
+
spacePrefix: config.prefix,
|
|
18052
|
+
sessionExpirationMs: config.sessionExpirationMs ?? 60 * 60 * 1e3,
|
|
18053
|
+
tinycloudHosts: [host],
|
|
18054
|
+
autoCreateSpace: config.autoCreateSpace,
|
|
18055
|
+
enablePublicSpace: config.enablePublicSpace ?? true,
|
|
18056
|
+
spaceCreationHandler: config.spaceCreationHandler
|
|
18057
|
+
});
|
|
18058
|
+
this.tc = new import_sdk_core3.TinyCloud(this.auth);
|
|
18059
|
+
}
|
|
18089
18060
|
/**
|
|
18090
18061
|
* Get the primary identity DID for this user.
|
|
18091
18062
|
* - If wallet connected and signed in: returns PKH DID (did:pkh:eip155:{chainId}:{address})
|
|
@@ -18147,6 +18118,7 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18147
18118
|
"Cannot signIn() in session-only mode. Provide a privateKey in config to create your own space."
|
|
18148
18119
|
);
|
|
18149
18120
|
}
|
|
18121
|
+
await this.wasmBindings.ensureInitialized?.();
|
|
18150
18122
|
this._address = await this.signer.getAddress();
|
|
18151
18123
|
this._chainId = await this.signer.getChainId();
|
|
18152
18124
|
this._kv = void 0;
|
|
@@ -18155,6 +18127,7 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18155
18127
|
this._serviceContext = void 0;
|
|
18156
18128
|
await this.tc.signIn();
|
|
18157
18129
|
this.initializeServices();
|
|
18130
|
+
this.notificationHandler.success("Successfully signed in");
|
|
18158
18131
|
}
|
|
18159
18132
|
/**
|
|
18160
18133
|
* Restore a previously established session from stored delegation data.
|
|
@@ -18166,6 +18139,7 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18166
18139
|
* @param sessionData - The stored delegation data from the browser flow
|
|
18167
18140
|
*/
|
|
18168
18141
|
async restoreSession(sessionData) {
|
|
18142
|
+
await this.wasmBindings.ensureInitialized?.();
|
|
18169
18143
|
this._kv = void 0;
|
|
18170
18144
|
this._sql = void 0;
|
|
18171
18145
|
this._duckdb = void 0;
|
|
@@ -18176,18 +18150,18 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18176
18150
|
if (sessionData.chainId) {
|
|
18177
18151
|
this._chainId = sessionData.chainId;
|
|
18178
18152
|
}
|
|
18179
|
-
this._serviceContext = new
|
|
18180
|
-
invoke:
|
|
18153
|
+
this._serviceContext = new import_sdk_core3.ServiceContext({
|
|
18154
|
+
invoke: this.wasmBindings.invoke,
|
|
18181
18155
|
fetch: globalThis.fetch.bind(globalThis),
|
|
18182
18156
|
hosts: [this.config.host]
|
|
18183
18157
|
});
|
|
18184
|
-
this._kv = new
|
|
18158
|
+
this._kv = new import_sdk_core3.KVService({});
|
|
18185
18159
|
this._kv.initialize(this._serviceContext);
|
|
18186
18160
|
this._serviceContext.registerService("kv", this._kv);
|
|
18187
|
-
this._sql = new
|
|
18161
|
+
this._sql = new import_sdk_core3.SQLService({});
|
|
18188
18162
|
this._sql.initialize(this._serviceContext);
|
|
18189
18163
|
this._serviceContext.registerService("sql", this._sql);
|
|
18190
|
-
this._duckdb = new
|
|
18164
|
+
this._duckdb = new import_sdk_core3.DuckDbService({});
|
|
18191
18165
|
this._duckdb.initialize(this._serviceContext);
|
|
18192
18166
|
this._serviceContext.registerService("duckdb", this._duckdb);
|
|
18193
18167
|
const serviceSession = {
|
|
@@ -18198,6 +18172,43 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18198
18172
|
jwk: sessionData.jwk
|
|
18199
18173
|
};
|
|
18200
18174
|
this._serviceContext.setSession(serviceSession);
|
|
18175
|
+
const wasm = this.wasmBindings;
|
|
18176
|
+
const vaultCrypto = (0, import_sdk_core3.createVaultCrypto)({
|
|
18177
|
+
vault_encrypt: wasm.vault_encrypt,
|
|
18178
|
+
vault_decrypt: wasm.vault_decrypt,
|
|
18179
|
+
vault_derive_key: wasm.vault_derive_key,
|
|
18180
|
+
vault_x25519_from_seed: wasm.vault_x25519_from_seed,
|
|
18181
|
+
vault_x25519_dh: wasm.vault_x25519_dh,
|
|
18182
|
+
vault_random_bytes: wasm.vault_random_bytes,
|
|
18183
|
+
vault_sha256: wasm.vault_sha256
|
|
18184
|
+
});
|
|
18185
|
+
const self2 = this;
|
|
18186
|
+
this._vault = new import_sdk_core3.DataVaultService({
|
|
18187
|
+
spaceId: sessionData.spaceId,
|
|
18188
|
+
crypto: vaultCrypto,
|
|
18189
|
+
tc: {
|
|
18190
|
+
kv: this._kv,
|
|
18191
|
+
ensurePublicSpace: async () => {
|
|
18192
|
+
try {
|
|
18193
|
+
await self2.ensurePublicSpace();
|
|
18194
|
+
return { ok: true, data: void 0 };
|
|
18195
|
+
} catch (error) {
|
|
18196
|
+
return { ok: false, error: { code: "STORAGE_ERROR", message: error instanceof Error ? error.message : String(error), service: "vault" } };
|
|
18197
|
+
}
|
|
18198
|
+
},
|
|
18199
|
+
get publicKV() {
|
|
18200
|
+
return self2._publicKV ?? self2.tc.publicKV;
|
|
18201
|
+
},
|
|
18202
|
+
readPublicSpace: (host, spaceId, key2) => import_sdk_core3.TinyCloud.readPublicSpace(host, spaceId, key2),
|
|
18203
|
+
makePublicSpaceId: import_sdk_core3.TinyCloud.makePublicSpaceId,
|
|
18204
|
+
did: this.did,
|
|
18205
|
+
address: sessionData.address ?? this._address ?? "",
|
|
18206
|
+
chainId: sessionData.chainId ?? this._chainId,
|
|
18207
|
+
hosts: [this.config.host]
|
|
18208
|
+
}
|
|
18209
|
+
});
|
|
18210
|
+
this._vault.initialize(this._serviceContext);
|
|
18211
|
+
this._serviceContext.registerService("vault", this._vault);
|
|
18201
18212
|
this.initializeV2Services(serviceSession);
|
|
18202
18213
|
}
|
|
18203
18214
|
/**
|
|
@@ -18232,19 +18243,63 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18232
18243
|
const prefix = options?.prefix ?? "default";
|
|
18233
18244
|
const host = this.config.host;
|
|
18234
18245
|
const domain = new URL(host).hostname;
|
|
18235
|
-
|
|
18246
|
+
if (!_TinyCloudNode.nodeDefaults) {
|
|
18247
|
+
throw new Error(
|
|
18248
|
+
"connectWallet() requires PrivateKeySigner. Use connectSigner() instead, or import from '@tinycloud/node-sdk' (not '/core') for automatic Node.js defaults."
|
|
18249
|
+
);
|
|
18250
|
+
}
|
|
18251
|
+
this.signer = _TinyCloudNode.nodeDefaults.createSigner(privateKey);
|
|
18252
|
+
this.auth = new NodeUserAuthorization({
|
|
18253
|
+
signer: this.signer,
|
|
18254
|
+
signStrategy: { type: "auto-sign" },
|
|
18255
|
+
wasmBindings: this.wasmBindings,
|
|
18256
|
+
sessionStorage: options?.sessionStorage ?? this.config.sessionStorage ?? new MemorySessionStorage(),
|
|
18257
|
+
domain,
|
|
18258
|
+
spacePrefix: prefix,
|
|
18259
|
+
sessionExpirationMs: this.config.sessionExpirationMs ?? 60 * 60 * 1e3,
|
|
18260
|
+
tinycloudHosts: [host],
|
|
18261
|
+
autoCreateSpace: this.config.autoCreateSpace,
|
|
18262
|
+
enablePublicSpace: this.config.enablePublicSpace ?? true,
|
|
18263
|
+
spaceCreationHandler: this.config.spaceCreationHandler
|
|
18264
|
+
});
|
|
18265
|
+
this.tc = new import_sdk_core3.TinyCloud(this.auth);
|
|
18266
|
+
this.config.prefix = prefix;
|
|
18267
|
+
}
|
|
18268
|
+
/**
|
|
18269
|
+
* Connect any ISigner to upgrade from session-only mode to wallet mode.
|
|
18270
|
+
*
|
|
18271
|
+
* Same as connectWallet() but accepts any ISigner implementation instead
|
|
18272
|
+
* of a raw private key string. Use this for browser wallets, hardware wallets,
|
|
18273
|
+
* or custom signing backends.
|
|
18274
|
+
*
|
|
18275
|
+
* Note: This does NOT automatically sign in. Call signIn() after connecting.
|
|
18276
|
+
*
|
|
18277
|
+
* @param signer - Any ISigner implementation
|
|
18278
|
+
* @param options - Optional configuration
|
|
18279
|
+
* @param options.prefix - Space name prefix (defaults to "default")
|
|
18280
|
+
*/
|
|
18281
|
+
connectSigner(signer, options) {
|
|
18282
|
+
if (this.signer) {
|
|
18283
|
+
throw new Error("Signer already connected. Cannot connect another signer.");
|
|
18284
|
+
}
|
|
18285
|
+
const prefix = options?.prefix ?? "default";
|
|
18286
|
+
const host = this.config.host;
|
|
18287
|
+
const domain = new URL(host).hostname;
|
|
18288
|
+
this.signer = signer;
|
|
18236
18289
|
this.auth = new NodeUserAuthorization({
|
|
18237
18290
|
signer: this.signer,
|
|
18238
18291
|
signStrategy: { type: "auto-sign" },
|
|
18292
|
+
wasmBindings: this.wasmBindings,
|
|
18239
18293
|
sessionStorage: options?.sessionStorage ?? this.config.sessionStorage ?? new MemorySessionStorage(),
|
|
18240
18294
|
domain,
|
|
18241
18295
|
spacePrefix: prefix,
|
|
18242
18296
|
sessionExpirationMs: this.config.sessionExpirationMs ?? 60 * 60 * 1e3,
|
|
18243
18297
|
tinycloudHosts: [host],
|
|
18244
18298
|
autoCreateSpace: this.config.autoCreateSpace,
|
|
18245
|
-
enablePublicSpace: this.config.enablePublicSpace ?? true
|
|
18299
|
+
enablePublicSpace: this.config.enablePublicSpace ?? true,
|
|
18300
|
+
spaceCreationHandler: this.config.spaceCreationHandler
|
|
18246
18301
|
});
|
|
18247
|
-
this.tc = new
|
|
18302
|
+
this.tc = new import_sdk_core3.TinyCloud(this.auth);
|
|
18248
18303
|
this.config.prefix = prefix;
|
|
18249
18304
|
}
|
|
18250
18305
|
/**
|
|
@@ -18256,23 +18311,23 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18256
18311
|
if (!session) {
|
|
18257
18312
|
return;
|
|
18258
18313
|
}
|
|
18259
|
-
this.tc.initializeServices(
|
|
18260
|
-
this._serviceContext = new
|
|
18261
|
-
invoke:
|
|
18314
|
+
this.tc.initializeServices(this.wasmBindings.invoke, [this.config.host]);
|
|
18315
|
+
this._serviceContext = new import_sdk_core3.ServiceContext({
|
|
18316
|
+
invoke: this.wasmBindings.invoke,
|
|
18262
18317
|
fetch: globalThis.fetch.bind(globalThis),
|
|
18263
18318
|
hosts: [this.config.host]
|
|
18264
18319
|
});
|
|
18265
|
-
this._kv = new
|
|
18320
|
+
this._kv = new import_sdk_core3.KVService({});
|
|
18266
18321
|
this._kv.initialize(this._serviceContext);
|
|
18267
18322
|
this._serviceContext.registerService("kv", this._kv);
|
|
18268
18323
|
const features = this.nodeFeatures;
|
|
18269
18324
|
if (features.length === 0 || features.includes("sql")) {
|
|
18270
|
-
this._sql = new
|
|
18325
|
+
this._sql = new import_sdk_core3.SQLService({});
|
|
18271
18326
|
this._sql.initialize(this._serviceContext);
|
|
18272
18327
|
this._serviceContext.registerService("sql", this._sql);
|
|
18273
18328
|
}
|
|
18274
18329
|
if (features.length === 0 || features.includes("duckdb")) {
|
|
18275
|
-
this._duckdb = new
|
|
18330
|
+
this._duckdb = new import_sdk_core3.DuckDbService({});
|
|
18276
18331
|
this._duckdb.initialize(this._serviceContext);
|
|
18277
18332
|
this._serviceContext.registerService("duckdb", this._duckdb);
|
|
18278
18333
|
}
|
|
@@ -18285,17 +18340,18 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18285
18340
|
};
|
|
18286
18341
|
this._serviceContext.setSession(serviceSession);
|
|
18287
18342
|
this.tc.serviceContext.setSession(serviceSession);
|
|
18288
|
-
const
|
|
18289
|
-
|
|
18290
|
-
|
|
18291
|
-
|
|
18292
|
-
|
|
18293
|
-
|
|
18294
|
-
|
|
18295
|
-
|
|
18343
|
+
const wasm = this.wasmBindings;
|
|
18344
|
+
const vaultCrypto = (0, import_sdk_core3.createVaultCrypto)({
|
|
18345
|
+
vault_encrypt: wasm.vault_encrypt,
|
|
18346
|
+
vault_decrypt: wasm.vault_decrypt,
|
|
18347
|
+
vault_derive_key: wasm.vault_derive_key,
|
|
18348
|
+
vault_x25519_from_seed: wasm.vault_x25519_from_seed,
|
|
18349
|
+
vault_x25519_dh: wasm.vault_x25519_dh,
|
|
18350
|
+
vault_random_bytes: wasm.vault_random_bytes,
|
|
18351
|
+
vault_sha256: wasm.vault_sha256
|
|
18296
18352
|
});
|
|
18297
18353
|
const self2 = this;
|
|
18298
|
-
this._vault = new
|
|
18354
|
+
this._vault = new import_sdk_core3.DataVaultService({
|
|
18299
18355
|
spaceId: session.spaceId,
|
|
18300
18356
|
crypto: vaultCrypto,
|
|
18301
18357
|
tc: {
|
|
@@ -18311,8 +18367,8 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18311
18367
|
get publicKV() {
|
|
18312
18368
|
return self2._publicKV ?? self2.tc.publicKV;
|
|
18313
18369
|
},
|
|
18314
|
-
readPublicSpace: (host, spaceId, key2) =>
|
|
18315
|
-
makePublicSpaceId:
|
|
18370
|
+
readPublicSpace: (host, spaceId, key2) => import_sdk_core3.TinyCloud.readPublicSpace(host, spaceId, key2),
|
|
18371
|
+
makePublicSpaceId: import_sdk_core3.TinyCloud.makePublicSpaceId,
|
|
18316
18372
|
did: this.did,
|
|
18317
18373
|
address: this._address,
|
|
18318
18374
|
chainId: this._chainId,
|
|
@@ -18328,7 +18384,7 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18328
18384
|
* @internal
|
|
18329
18385
|
*/
|
|
18330
18386
|
initializeV2Services(serviceSession) {
|
|
18331
|
-
this._capabilityRegistry = new
|
|
18387
|
+
this._capabilityRegistry = new import_sdk_core3.CapabilityKeyRegistry();
|
|
18332
18388
|
const tcSession = this.auth?.tinyCloudSession;
|
|
18333
18389
|
if (tcSession && this._address) {
|
|
18334
18390
|
const sessionKey = {
|
|
@@ -18402,23 +18458,23 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18402
18458
|
}
|
|
18403
18459
|
this._capabilityRegistry.registerKey(sessionKey, delegations);
|
|
18404
18460
|
}
|
|
18405
|
-
this._delegationManager = new
|
|
18461
|
+
this._delegationManager = new import_sdk_core3.DelegationManager({
|
|
18406
18462
|
hosts: [this.config.host],
|
|
18407
18463
|
session: serviceSession,
|
|
18408
|
-
invoke:
|
|
18464
|
+
invoke: this.wasmBindings.invoke,
|
|
18409
18465
|
fetch: globalThis.fetch.bind(globalThis)
|
|
18410
18466
|
});
|
|
18411
|
-
this._spaceService = new
|
|
18467
|
+
this._spaceService = new import_sdk_core3.SpaceService({
|
|
18412
18468
|
hosts: [this.config.host],
|
|
18413
18469
|
session: serviceSession,
|
|
18414
|
-
invoke:
|
|
18470
|
+
invoke: this.wasmBindings.invoke,
|
|
18415
18471
|
fetch: globalThis.fetch.bind(globalThis),
|
|
18416
18472
|
capabilityRegistry: this._capabilityRegistry,
|
|
18417
18473
|
userDid: this.did,
|
|
18418
18474
|
createKVService: (spaceId) => {
|
|
18419
|
-
const kvService = new
|
|
18475
|
+
const kvService = new import_sdk_core3.KVService({});
|
|
18420
18476
|
if (this._serviceContext) {
|
|
18421
|
-
const spaceScopedContext = new
|
|
18477
|
+
const spaceScopedContext = new import_sdk_core3.ServiceContext({
|
|
18422
18478
|
invoke: this._serviceContext.invoke,
|
|
18423
18479
|
fetch: this._serviceContext.fetch,
|
|
18424
18480
|
hosts: this._serviceContext.hosts
|
|
@@ -18472,7 +18528,10 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18472
18528
|
delegationManager: this._delegationManager,
|
|
18473
18529
|
sessionExpiry: this.getSessionExpiry(),
|
|
18474
18530
|
// WASM-based delegation creation (preferred - no server roundtrip)
|
|
18475
|
-
createDelegationWasm: (params) => this.createDelegationWrapper(params)
|
|
18531
|
+
createDelegationWasm: (params) => this.createDelegationWrapper(params),
|
|
18532
|
+
// Root delegation for long-lived share links (bypasses session expiry)
|
|
18533
|
+
// In node-sdk we have direct signer access, so no popup needed
|
|
18534
|
+
onRootDelegationNeeded: this.signer ? async (params) => this.createRootDelegationForSharing(params) : void 0
|
|
18476
18535
|
});
|
|
18477
18536
|
this._spaceService.updateConfig({
|
|
18478
18537
|
sharingService: this._sharingService
|
|
@@ -18499,7 +18558,7 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18499
18558
|
spaceId: params.session.spaceId,
|
|
18500
18559
|
verificationMethod: params.session.verificationMethod
|
|
18501
18560
|
};
|
|
18502
|
-
const result =
|
|
18561
|
+
const result = this.wasmBindings.createDelegation(
|
|
18503
18562
|
wasmSession,
|
|
18504
18563
|
params.delegateDID,
|
|
18505
18564
|
params.spaceId,
|
|
@@ -18517,6 +18576,67 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18517
18576
|
expiry: new Date(result.expiry * 1e3)
|
|
18518
18577
|
};
|
|
18519
18578
|
}
|
|
18579
|
+
/**
|
|
18580
|
+
* Create a direct root delegation from the wallet to a share key.
|
|
18581
|
+
* This bypasses the session delegation chain, allowing share links
|
|
18582
|
+
* with expiry longer than the current session.
|
|
18583
|
+
* @internal
|
|
18584
|
+
*/
|
|
18585
|
+
async createRootDelegationForSharing(params) {
|
|
18586
|
+
if (!this.signer) {
|
|
18587
|
+
return void 0;
|
|
18588
|
+
}
|
|
18589
|
+
const session = this.auth?.tinyCloudSession;
|
|
18590
|
+
if (!session) {
|
|
18591
|
+
return void 0;
|
|
18592
|
+
}
|
|
18593
|
+
try {
|
|
18594
|
+
const host = this.config.host;
|
|
18595
|
+
const now = /* @__PURE__ */ new Date();
|
|
18596
|
+
const abilities = {
|
|
18597
|
+
kv: {
|
|
18598
|
+
[params.path]: params.actions
|
|
18599
|
+
}
|
|
18600
|
+
};
|
|
18601
|
+
const prepared = this.wasmBindings.prepareSession({
|
|
18602
|
+
abilities,
|
|
18603
|
+
address: this.wasmBindings.ensureEip55(session.address),
|
|
18604
|
+
chainId: session.chainId,
|
|
18605
|
+
domain: new URL(host).hostname,
|
|
18606
|
+
issuedAt: now.toISOString(),
|
|
18607
|
+
expirationTime: params.requestedExpiry.toISOString(),
|
|
18608
|
+
spaceId: params.spaceId,
|
|
18609
|
+
delegateUri: params.shareKeyDID
|
|
18610
|
+
});
|
|
18611
|
+
const signature2 = await this.signer.signMessage(prepared.siwe);
|
|
18612
|
+
const delegationSession = this.wasmBindings.completeSessionSetup({
|
|
18613
|
+
...prepared,
|
|
18614
|
+
signature: signature2
|
|
18615
|
+
});
|
|
18616
|
+
const activateResult = await (0, import_sdk_core3.activateSessionWithHost)(
|
|
18617
|
+
host,
|
|
18618
|
+
delegationSession.delegationHeader
|
|
18619
|
+
);
|
|
18620
|
+
if (!activateResult.success) {
|
|
18621
|
+
return void 0;
|
|
18622
|
+
}
|
|
18623
|
+
return {
|
|
18624
|
+
cid: delegationSession.delegationCid,
|
|
18625
|
+
delegateDID: params.shareKeyDID,
|
|
18626
|
+
delegatorDID: `did:pkh:eip155:${session.chainId}:${session.address}`,
|
|
18627
|
+
spaceId: params.spaceId,
|
|
18628
|
+
path: params.path,
|
|
18629
|
+
actions: params.actions,
|
|
18630
|
+
expiry: params.requestedExpiry,
|
|
18631
|
+
isRevoked: false,
|
|
18632
|
+
allowSubDelegation: true,
|
|
18633
|
+
createdAt: now,
|
|
18634
|
+
authHeader: delegationSession.delegationHeader.Authorization
|
|
18635
|
+
};
|
|
18636
|
+
} catch {
|
|
18637
|
+
return void 0;
|
|
18638
|
+
}
|
|
18639
|
+
}
|
|
18520
18640
|
/**
|
|
18521
18641
|
* Track a received delegation in the capability registry.
|
|
18522
18642
|
* @internal
|
|
@@ -18560,7 +18680,7 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18560
18680
|
if (!this._sql) {
|
|
18561
18681
|
const features = this.nodeFeatures;
|
|
18562
18682
|
if (features.length > 0 && !features.includes("sql")) {
|
|
18563
|
-
throw new
|
|
18683
|
+
throw new import_sdk_core3.UnsupportedFeatureError("sql", this.config.host, features);
|
|
18564
18684
|
}
|
|
18565
18685
|
throw new Error("Not signed in. Call signIn() first.");
|
|
18566
18686
|
}
|
|
@@ -18573,7 +18693,7 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18573
18693
|
if (!this._duckdb) {
|
|
18574
18694
|
const features = this.nodeFeatures;
|
|
18575
18695
|
if (features.length > 0 && !features.includes("duckdb")) {
|
|
18576
|
-
throw new
|
|
18696
|
+
throw new import_sdk_core3.UnsupportedFeatureError("duckdb", this.config.host, features);
|
|
18577
18697
|
}
|
|
18578
18698
|
throw new Error("Not signed in. Call signIn() first.");
|
|
18579
18699
|
}
|
|
@@ -18787,9 +18907,9 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18787
18907
|
const now = /* @__PURE__ */ new Date();
|
|
18788
18908
|
const expiryMs = 60 * 60 * 1e3;
|
|
18789
18909
|
const expirationTime = new Date(now.getTime() + expiryMs);
|
|
18790
|
-
const prepared =
|
|
18910
|
+
const prepared = this.wasmBindings.prepareSession({
|
|
18791
18911
|
abilities,
|
|
18792
|
-
address:
|
|
18912
|
+
address: this.wasmBindings.ensureEip55(this.session.address),
|
|
18793
18913
|
chainId: this.session.chainId,
|
|
18794
18914
|
domain: new URL(this.config.host).hostname,
|
|
18795
18915
|
issuedAt: now.toISOString(),
|
|
@@ -18799,11 +18919,11 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18799
18919
|
parents: [this.session.delegationCid]
|
|
18800
18920
|
});
|
|
18801
18921
|
const signature2 = await this.signer.signMessage(prepared.siwe);
|
|
18802
|
-
const delegationSession =
|
|
18922
|
+
const delegationSession = this.wasmBindings.completeSessionSetup({
|
|
18803
18923
|
...prepared,
|
|
18804
18924
|
signature: signature2
|
|
18805
18925
|
});
|
|
18806
|
-
const activateResult = await (0,
|
|
18926
|
+
const activateResult = await (0, import_sdk_core3.activateSessionWithHost)(
|
|
18807
18927
|
this.config.host,
|
|
18808
18928
|
delegationSession.delegationHeader
|
|
18809
18929
|
);
|
|
@@ -18830,9 +18950,9 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18830
18950
|
}]);
|
|
18831
18951
|
}
|
|
18832
18952
|
if (this._serviceContext) {
|
|
18833
|
-
const publicKV = new
|
|
18834
|
-
const publicContext = new
|
|
18835
|
-
invoke:
|
|
18953
|
+
const publicKV = new import_sdk_core3.KVService({ prefix: "" });
|
|
18954
|
+
const publicContext = new import_sdk_core3.ServiceContext({
|
|
18955
|
+
invoke: this.wasmBindings.invoke,
|
|
18836
18956
|
fetch: this._serviceContext.fetch,
|
|
18837
18957
|
hosts: this._serviceContext.hosts
|
|
18838
18958
|
});
|
|
@@ -18933,6 +19053,11 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18933
19053
|
if (!session) {
|
|
18934
19054
|
throw new Error("Not signed in. Call signIn() first.");
|
|
18935
19055
|
}
|
|
19056
|
+
if (params.delegateDID.endsWith(".eth") && this.config.ensResolver) {
|
|
19057
|
+
const address = await this.config.ensResolver.resolveAddress(params.delegateDID);
|
|
19058
|
+
if (!address) throw new Error(`Could not resolve ENS name: ${params.delegateDID}`);
|
|
19059
|
+
params = { ...params, delegateDID: `did:pkh:eip155:1:${address}` };
|
|
19060
|
+
}
|
|
18936
19061
|
const abilities = {};
|
|
18937
19062
|
const kvActions = params.actions.filter((a) => a.startsWith("tinycloud.kv/"));
|
|
18938
19063
|
const sqlActions = params.actions.filter((a) => a.startsWith("tinycloud.sql/"));
|
|
@@ -18949,9 +19074,9 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18949
19074
|
const now = /* @__PURE__ */ new Date();
|
|
18950
19075
|
const expiryMs = params.expiryMs ?? 60 * 60 * 1e3;
|
|
18951
19076
|
const expirationTime = new Date(now.getTime() + expiryMs);
|
|
18952
|
-
const prepared =
|
|
19077
|
+
const prepared = this.wasmBindings.prepareSession({
|
|
18953
19078
|
abilities,
|
|
18954
|
-
address:
|
|
19079
|
+
address: this.wasmBindings.ensureEip55(session.address),
|
|
18955
19080
|
chainId: session.chainId,
|
|
18956
19081
|
domain: new URL(this.config.host).hostname,
|
|
18957
19082
|
issuedAt: now.toISOString(),
|
|
@@ -18961,11 +19086,11 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18961
19086
|
parents: [session.delegationCid]
|
|
18962
19087
|
});
|
|
18963
19088
|
const signature2 = await this.signer.signMessage(prepared.siwe);
|
|
18964
|
-
const delegationSession =
|
|
19089
|
+
const delegationSession = this.wasmBindings.completeSessionSetup({
|
|
18965
19090
|
...prepared,
|
|
18966
19091
|
signature: signature2
|
|
18967
19092
|
});
|
|
18968
|
-
const activateResult = await (0,
|
|
19093
|
+
const activateResult = await (0, import_sdk_core3.activateSessionWithHost)(
|
|
18969
19094
|
this.config.host,
|
|
18970
19095
|
delegationSession.delegationHeader
|
|
18971
19096
|
);
|
|
@@ -18987,16 +19112,16 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
18987
19112
|
};
|
|
18988
19113
|
const hasKvActions = params.actions.some((a) => a.startsWith("tinycloud.kv/"));
|
|
18989
19114
|
if (hasKvActions && params.includePublicSpace !== false) {
|
|
18990
|
-
const publicSpaceId = (0,
|
|
18991
|
-
|
|
19115
|
+
const publicSpaceId = (0, import_sdk_core3.makePublicSpaceId)(
|
|
19116
|
+
this.wasmBindings.ensureEip55(session.address),
|
|
18992
19117
|
session.chainId
|
|
18993
19118
|
);
|
|
18994
19119
|
const publicAbilities = {
|
|
18995
19120
|
kv: { "": ["tinycloud.kv/get", "tinycloud.kv/put", "tinycloud.kv/metadata"] }
|
|
18996
19121
|
};
|
|
18997
|
-
const publicPrepared =
|
|
19122
|
+
const publicPrepared = this.wasmBindings.prepareSession({
|
|
18998
19123
|
abilities: publicAbilities,
|
|
18999
|
-
address:
|
|
19124
|
+
address: this.wasmBindings.ensureEip55(session.address),
|
|
19000
19125
|
chainId: session.chainId,
|
|
19001
19126
|
domain: new URL(this.config.host).hostname,
|
|
19002
19127
|
issuedAt: now.toISOString(),
|
|
@@ -19006,11 +19131,11 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
19006
19131
|
parents: [session.delegationCid]
|
|
19007
19132
|
});
|
|
19008
19133
|
const publicSignature = await this.signer.signMessage(publicPrepared.siwe);
|
|
19009
|
-
const publicSession =
|
|
19134
|
+
const publicSession = this.wasmBindings.completeSessionSetup({
|
|
19010
19135
|
...publicPrepared,
|
|
19011
19136
|
signature: publicSignature
|
|
19012
19137
|
});
|
|
19013
|
-
const publicActivateResult = await (0,
|
|
19138
|
+
const publicActivateResult = await (0, import_sdk_core3.activateSessionWithHost)(
|
|
19014
19139
|
this.config.host,
|
|
19015
19140
|
publicSession.delegationHeader
|
|
19016
19141
|
);
|
|
@@ -19070,7 +19195,7 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
19070
19195
|
// Not used in session-only mode
|
|
19071
19196
|
};
|
|
19072
19197
|
this.trackReceivedDelegation(delegation, this.sessionKeyJwk);
|
|
19073
|
-
return new DelegatedAccess(session2, delegation, targetHost);
|
|
19198
|
+
return new DelegatedAccess(session2, delegation, targetHost, this.wasmBindings.invoke);
|
|
19074
19199
|
}
|
|
19075
19200
|
const mySession = this.auth?.tinyCloudSession;
|
|
19076
19201
|
if (!mySession) {
|
|
@@ -19093,9 +19218,9 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
19093
19218
|
const now = /* @__PURE__ */ new Date();
|
|
19094
19219
|
const maxExpiry = new Date(now.getTime() + 60 * 60 * 1e3);
|
|
19095
19220
|
const expirationTime = delegation.expiry < maxExpiry ? delegation.expiry : maxExpiry;
|
|
19096
|
-
const prepared =
|
|
19221
|
+
const prepared = this.wasmBindings.prepareSession({
|
|
19097
19222
|
abilities,
|
|
19098
|
-
address:
|
|
19223
|
+
address: this.wasmBindings.ensureEip55(mySession.address),
|
|
19099
19224
|
chainId: mySession.chainId,
|
|
19100
19225
|
domain: new URL(targetHost).hostname,
|
|
19101
19226
|
issuedAt: now.toISOString(),
|
|
@@ -19105,11 +19230,11 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
19105
19230
|
parents: [delegation.cid]
|
|
19106
19231
|
});
|
|
19107
19232
|
const signature2 = await this.signer.signMessage(prepared.siwe);
|
|
19108
|
-
const invokerSession =
|
|
19233
|
+
const invokerSession = this.wasmBindings.completeSessionSetup({
|
|
19109
19234
|
...prepared,
|
|
19110
19235
|
signature: signature2
|
|
19111
19236
|
});
|
|
19112
|
-
const activateResult = await (0,
|
|
19237
|
+
const activateResult = await (0, import_sdk_core3.activateSessionWithHost)(
|
|
19113
19238
|
targetHost,
|
|
19114
19239
|
invokerSession.delegationHeader
|
|
19115
19240
|
);
|
|
@@ -19129,7 +19254,7 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
19129
19254
|
signature: signature2
|
|
19130
19255
|
};
|
|
19131
19256
|
this.trackReceivedDelegation(delegation, jwk);
|
|
19132
|
-
return new DelegatedAccess(session, delegation, targetHost);
|
|
19257
|
+
return new DelegatedAccess(session, delegation, targetHost, this.wasmBindings.invoke);
|
|
19133
19258
|
}
|
|
19134
19259
|
/**
|
|
19135
19260
|
* Create a sub-delegation from a received delegation.
|
|
@@ -19182,9 +19307,9 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
19182
19307
|
abilities.duckdb = { [params.path]: duckdbActions };
|
|
19183
19308
|
}
|
|
19184
19309
|
const targetHost = parentDelegation.host ?? this.config.host;
|
|
19185
|
-
const prepared =
|
|
19310
|
+
const prepared = this.wasmBindings.prepareSession({
|
|
19186
19311
|
abilities,
|
|
19187
|
-
address:
|
|
19312
|
+
address: this.wasmBindings.ensureEip55(this._address),
|
|
19188
19313
|
chainId: this._chainId,
|
|
19189
19314
|
domain: new URL(targetHost).hostname,
|
|
19190
19315
|
issuedAt: now.toISOString(),
|
|
@@ -19194,11 +19319,11 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
19194
19319
|
parents: [parentDelegation.cid]
|
|
19195
19320
|
});
|
|
19196
19321
|
const signature2 = await this.signer.signMessage(prepared.siwe);
|
|
19197
|
-
const subDelegationSession =
|
|
19322
|
+
const subDelegationSession = this.wasmBindings.completeSessionSetup({
|
|
19198
19323
|
...prepared,
|
|
19199
19324
|
signature: signature2
|
|
19200
19325
|
});
|
|
19201
|
-
const activateResult = await (0,
|
|
19326
|
+
const activateResult = await (0, import_sdk_core3.activateSessionWithHost)(
|
|
19202
19327
|
targetHost,
|
|
19203
19328
|
subDelegationSession.delegationHeader
|
|
19204
19329
|
);
|
|
@@ -19220,9 +19345,136 @@ var _TinyCloudNode = class _TinyCloudNode {
|
|
|
19220
19345
|
};
|
|
19221
19346
|
}
|
|
19222
19347
|
};
|
|
19223
|
-
|
|
19224
|
-
|
|
19225
|
-
|
|
19348
|
+
|
|
19349
|
+
// src/nodeDefaults.ts
|
|
19350
|
+
TinyCloudNode.registerNodeDefaults({
|
|
19351
|
+
createWasmBindings: () => new NodeWasmBindings(),
|
|
19352
|
+
createSigner: (privateKey, chainId) => new PrivateKeySigner(privateKey, chainId)
|
|
19353
|
+
});
|
|
19354
|
+
|
|
19355
|
+
// src/index.ts
|
|
19356
|
+
var import_sdk_core5 = require("@tinycloud/sdk-core");
|
|
19357
|
+
var import_sdk_core6 = require("@tinycloud/sdk-core");
|
|
19358
|
+
|
|
19359
|
+
// src/storage/FileSessionStorage.ts
|
|
19360
|
+
var import_sdk_core4 = require("@tinycloud/sdk-core");
|
|
19361
|
+
var import_fs = require("fs");
|
|
19362
|
+
var import_path = require("path");
|
|
19363
|
+
var FileSessionStorage = class {
|
|
19364
|
+
/**
|
|
19365
|
+
* Create a new FileSessionStorage.
|
|
19366
|
+
*
|
|
19367
|
+
* @param baseDir - Directory to store session files (default: ~/.tinycloud/sessions)
|
|
19368
|
+
*/
|
|
19369
|
+
constructor(baseDir) {
|
|
19370
|
+
this.baseDir = baseDir || this.getDefaultDir();
|
|
19371
|
+
this.ensureDirectoryExists();
|
|
19372
|
+
}
|
|
19373
|
+
/**
|
|
19374
|
+
* Get the default session storage directory.
|
|
19375
|
+
*/
|
|
19376
|
+
getDefaultDir() {
|
|
19377
|
+
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
19378
|
+
return (0, import_path.join)(home, ".tinycloud", "sessions");
|
|
19379
|
+
}
|
|
19380
|
+
/**
|
|
19381
|
+
* Ensure the storage directory exists.
|
|
19382
|
+
*/
|
|
19383
|
+
ensureDirectoryExists() {
|
|
19384
|
+
if (!(0, import_fs.existsSync)(this.baseDir)) {
|
|
19385
|
+
(0, import_fs.mkdirSync)(this.baseDir, { recursive: true });
|
|
19386
|
+
}
|
|
19387
|
+
}
|
|
19388
|
+
/**
|
|
19389
|
+
* Get the file path for an address.
|
|
19390
|
+
*/
|
|
19391
|
+
getFilePath(address) {
|
|
19392
|
+
const normalizedAddress = address.toLowerCase();
|
|
19393
|
+
const filename = `${normalizedAddress.replace("0x", "")}.json`;
|
|
19394
|
+
return (0, import_path.join)(this.baseDir, filename);
|
|
19395
|
+
}
|
|
19396
|
+
/**
|
|
19397
|
+
* Save a session for an address.
|
|
19398
|
+
*/
|
|
19399
|
+
async save(address, session) {
|
|
19400
|
+
const filePath = this.getFilePath(address);
|
|
19401
|
+
const data = JSON.stringify(session, null, 2);
|
|
19402
|
+
(0, import_fs.writeFileSync)(filePath, data, "utf-8");
|
|
19403
|
+
}
|
|
19404
|
+
/**
|
|
19405
|
+
* Load a session for an address.
|
|
19406
|
+
*/
|
|
19407
|
+
async load(address) {
|
|
19408
|
+
const filePath = this.getFilePath(address);
|
|
19409
|
+
if (!(0, import_fs.existsSync)(filePath)) {
|
|
19410
|
+
return null;
|
|
19411
|
+
}
|
|
19412
|
+
try {
|
|
19413
|
+
const data = (0, import_fs.readFileSync)(filePath, "utf-8");
|
|
19414
|
+
const parsed = JSON.parse(data);
|
|
19415
|
+
const validation = (0, import_sdk_core4.validatePersistedSessionData)(parsed);
|
|
19416
|
+
if (!validation.ok) {
|
|
19417
|
+
console.warn(`Invalid session data for ${address}:`, validation.error.message);
|
|
19418
|
+
(0, import_fs.unlinkSync)(filePath);
|
|
19419
|
+
return null;
|
|
19420
|
+
}
|
|
19421
|
+
const session = validation.data;
|
|
19422
|
+
const expiresAt = new Date(session.expiresAt);
|
|
19423
|
+
if (expiresAt < /* @__PURE__ */ new Date()) {
|
|
19424
|
+
(0, import_fs.unlinkSync)(filePath);
|
|
19425
|
+
return null;
|
|
19426
|
+
}
|
|
19427
|
+
return session;
|
|
19428
|
+
} catch (error) {
|
|
19429
|
+
try {
|
|
19430
|
+
(0, import_fs.unlinkSync)(filePath);
|
|
19431
|
+
} catch {
|
|
19432
|
+
}
|
|
19433
|
+
return null;
|
|
19434
|
+
}
|
|
19435
|
+
}
|
|
19436
|
+
/**
|
|
19437
|
+
* Clear a session for an address.
|
|
19438
|
+
*/
|
|
19439
|
+
async clear(address) {
|
|
19440
|
+
const filePath = this.getFilePath(address);
|
|
19441
|
+
if ((0, import_fs.existsSync)(filePath)) {
|
|
19442
|
+
(0, import_fs.unlinkSync)(filePath);
|
|
19443
|
+
}
|
|
19444
|
+
}
|
|
19445
|
+
/**
|
|
19446
|
+
* Check if a session exists for an address.
|
|
19447
|
+
*/
|
|
19448
|
+
exists(address) {
|
|
19449
|
+
const filePath = this.getFilePath(address);
|
|
19450
|
+
if (!(0, import_fs.existsSync)(filePath)) {
|
|
19451
|
+
return false;
|
|
19452
|
+
}
|
|
19453
|
+
try {
|
|
19454
|
+
const data = (0, import_fs.readFileSync)(filePath, "utf-8");
|
|
19455
|
+
const session = JSON.parse(data);
|
|
19456
|
+
const expiresAt = new Date(session.expiresAt);
|
|
19457
|
+
if (expiresAt < /* @__PURE__ */ new Date()) {
|
|
19458
|
+
(0, import_fs.unlinkSync)(filePath);
|
|
19459
|
+
return false;
|
|
19460
|
+
}
|
|
19461
|
+
return true;
|
|
19462
|
+
} catch {
|
|
19463
|
+
return false;
|
|
19464
|
+
}
|
|
19465
|
+
}
|
|
19466
|
+
/**
|
|
19467
|
+
* Check if file system storage is available.
|
|
19468
|
+
*/
|
|
19469
|
+
isAvailable() {
|
|
19470
|
+
try {
|
|
19471
|
+
this.ensureDirectoryExists();
|
|
19472
|
+
return (0, import_fs.existsSync)(this.baseDir);
|
|
19473
|
+
} catch {
|
|
19474
|
+
return false;
|
|
19475
|
+
}
|
|
19476
|
+
}
|
|
19477
|
+
};
|
|
19226
19478
|
|
|
19227
19479
|
// src/delegation.ts
|
|
19228
19480
|
function serializeDelegation(delegation) {
|
|
@@ -19241,7 +19493,6 @@ function deserializeDelegation(data) {
|
|
|
19241
19493
|
}
|
|
19242
19494
|
|
|
19243
19495
|
// src/index.ts
|
|
19244
|
-
var import_sdk_core6 = require("@tinycloud/sdk-core");
|
|
19245
19496
|
var import_sdk_core7 = require("@tinycloud/sdk-core");
|
|
19246
19497
|
var import_sdk_core8 = require("@tinycloud/sdk-core");
|
|
19247
19498
|
var import_sdk_core9 = require("@tinycloud/sdk-core");
|
|
@@ -19250,8 +19501,10 @@ var import_sdk_core11 = require("@tinycloud/sdk-core");
|
|
|
19250
19501
|
var import_sdk_core12 = require("@tinycloud/sdk-core");
|
|
19251
19502
|
var import_sdk_core13 = require("@tinycloud/sdk-core");
|
|
19252
19503
|
var import_sdk_core14 = require("@tinycloud/sdk-core");
|
|
19504
|
+
var import_sdk_core15 = require("@tinycloud/sdk-core");
|
|
19253
19505
|
// Annotate the CommonJS export names for ESM import in node:
|
|
19254
19506
|
0 && (module.exports = {
|
|
19507
|
+
AutoApproveSpaceCreationHandler,
|
|
19255
19508
|
CapabilityKeyRegistry,
|
|
19256
19509
|
CapabilityKeyRegistryErrorCodes,
|
|
19257
19510
|
DataVaultService,
|
|
@@ -19266,6 +19519,7 @@ var import_sdk_core14 = require("@tinycloud/sdk-core");
|
|
|
19266
19519
|
KVService,
|
|
19267
19520
|
MemorySessionStorage,
|
|
19268
19521
|
NodeUserAuthorization,
|
|
19522
|
+
NodeWasmBindings,
|
|
19269
19523
|
PrefixedKVService,
|
|
19270
19524
|
PrivateKeySigner,
|
|
19271
19525
|
ProtocolMismatchError,
|
|
@@ -19273,6 +19527,7 @@ var import_sdk_core14 = require("@tinycloud/sdk-core");
|
|
|
19273
19527
|
SQLService,
|
|
19274
19528
|
ServiceContext,
|
|
19275
19529
|
SharingService,
|
|
19530
|
+
SilentNotificationHandler,
|
|
19276
19531
|
Space,
|
|
19277
19532
|
SpaceErrorCodes,
|
|
19278
19533
|
SpaceService,
|
|
@@ -19291,6 +19546,7 @@ var import_sdk_core14 = require("@tinycloud/sdk-core");
|
|
|
19291
19546
|
createVaultCrypto,
|
|
19292
19547
|
createWasmKeyProvider,
|
|
19293
19548
|
defaultSignStrategy,
|
|
19549
|
+
defaultSpaceCreationHandler,
|
|
19294
19550
|
deserializeDelegation,
|
|
19295
19551
|
makePublicSpaceId,
|
|
19296
19552
|
parseSpaceUri,
|