@unicitylabs/sphere-sdk 0.4.0 → 0.4.2
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/connect/index.cjs +78 -2
- package/dist/connect/index.cjs.map +1 -1
- package/dist/connect/index.d.cts +16 -0
- package/dist/connect/index.d.ts +16 -0
- package/dist/connect/index.js +78 -2
- package/dist/connect/index.js.map +1 -1
- package/dist/core/index.cjs +63 -41
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +7 -0
- package/dist/core/index.d.ts +7 -0
- package/dist/core/index.js +63 -41
- package/dist/core/index.js.map +1 -1
- package/dist/impl/browser/connect/index.cjs +11 -2
- package/dist/impl/browser/connect/index.cjs.map +1 -1
- package/dist/impl/browser/connect/index.js +11 -2
- package/dist/impl/browser/connect/index.js.map +1 -1
- package/dist/impl/browser/index.cjs +45 -28
- package/dist/impl/browser/index.cjs.map +1 -1
- package/dist/impl/browser/index.js +45 -28
- package/dist/impl/browser/index.js.map +1 -1
- package/dist/impl/nodejs/connect/index.cjs +11 -2
- package/dist/impl/nodejs/connect/index.cjs.map +1 -1
- package/dist/impl/nodejs/connect/index.js +11 -2
- package/dist/impl/nodejs/connect/index.js.map +1 -1
- package/dist/index.cjs +63 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +63 -41
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -6736,6 +6736,29 @@ var PaymentsModule = class _PaymentsModule {
|
|
|
6736
6736
|
this.nametags = [];
|
|
6737
6737
|
await this.save();
|
|
6738
6738
|
}
|
|
6739
|
+
/**
|
|
6740
|
+
* Reload nametag data from storage providers into memory.
|
|
6741
|
+
*
|
|
6742
|
+
* Used as a recovery mechanism when `this.nametags` is unexpectedly empty
|
|
6743
|
+
* (e.g., wiped by sync or race condition) but nametag data exists in storage.
|
|
6744
|
+
*/
|
|
6745
|
+
async reloadNametagsFromStorage() {
|
|
6746
|
+
const providers = this.getTokenStorageProviders();
|
|
6747
|
+
for (const [, provider] of providers) {
|
|
6748
|
+
try {
|
|
6749
|
+
const result = await provider.load();
|
|
6750
|
+
if (result.success && result.data) {
|
|
6751
|
+
const parsed = parseTxfStorageData(result.data);
|
|
6752
|
+
if (parsed.nametags.length > 0) {
|
|
6753
|
+
this.nametags = parsed.nametags;
|
|
6754
|
+
this.log(`Reloaded ${parsed.nametags.length} nametag(s) from storage`);
|
|
6755
|
+
return;
|
|
6756
|
+
}
|
|
6757
|
+
}
|
|
6758
|
+
} catch {
|
|
6759
|
+
}
|
|
6760
|
+
}
|
|
6761
|
+
}
|
|
6739
6762
|
/**
|
|
6740
6763
|
* Mint a nametag token on-chain (like Sphere wallet and lottery)
|
|
6741
6764
|
* This creates the nametag token required for receiving tokens via PROXY addresses
|
|
@@ -6860,11 +6883,15 @@ var PaymentsModule = class _PaymentsModule {
|
|
|
6860
6883
|
const localData = await this.createStorageData();
|
|
6861
6884
|
let totalAdded = 0;
|
|
6862
6885
|
let totalRemoved = 0;
|
|
6886
|
+
const savedNametags = [...this.nametags];
|
|
6863
6887
|
for (const [providerId, provider] of providers) {
|
|
6864
6888
|
try {
|
|
6865
6889
|
const result = await provider.sync(localData);
|
|
6866
6890
|
if (result.success && result.merged) {
|
|
6867
6891
|
this.loadFromStorageData(result.merged);
|
|
6892
|
+
if (this.nametags.length === 0 && savedNametags.length > 0) {
|
|
6893
|
+
this.nametags = savedNametags;
|
|
6894
|
+
}
|
|
6868
6895
|
totalAdded += result.added;
|
|
6869
6896
|
totalRemoved += result.removed;
|
|
6870
6897
|
}
|
|
@@ -7252,7 +7279,12 @@ var PaymentsModule = class _PaymentsModule {
|
|
|
7252
7279
|
let nametagTokens = [];
|
|
7253
7280
|
if (addressScheme === import_AddressScheme.AddressScheme.PROXY) {
|
|
7254
7281
|
const { ProxyAddress } = await import("@unicitylabs/state-transition-sdk/lib/address/ProxyAddress");
|
|
7255
|
-
|
|
7282
|
+
let proxyNametag = this.getNametag();
|
|
7283
|
+
if (!proxyNametag?.token) {
|
|
7284
|
+
this.log("Nametag missing in memory, attempting reload from storage...");
|
|
7285
|
+
await this.reloadNametagsFromStorage();
|
|
7286
|
+
proxyNametag = this.getNametag();
|
|
7287
|
+
}
|
|
7256
7288
|
if (!proxyNametag?.token) {
|
|
7257
7289
|
throw new Error("Cannot finalize PROXY transfer - no nametag token");
|
|
7258
7290
|
}
|
|
@@ -7857,13 +7889,14 @@ var CommunicationsModule = class {
|
|
|
7857
7889
|
*/
|
|
7858
7890
|
async sendDM(recipient, content) {
|
|
7859
7891
|
this.ensureInitialized();
|
|
7860
|
-
const
|
|
7861
|
-
const eventId = await this.deps.transport.sendMessage(
|
|
7892
|
+
const resolved = await this.resolveRecipient(recipient);
|
|
7893
|
+
const eventId = await this.deps.transport.sendMessage(resolved.pubkey, content);
|
|
7862
7894
|
const message = {
|
|
7863
7895
|
id: eventId,
|
|
7864
7896
|
senderPubkey: this.deps.identity.chainPubkey,
|
|
7865
7897
|
senderNametag: this.deps.identity.nametag,
|
|
7866
|
-
recipientPubkey,
|
|
7898
|
+
recipientPubkey: resolved.pubkey,
|
|
7899
|
+
...resolved.nametag ? { recipientNametag: resolved.nametag } : {},
|
|
7867
7900
|
content,
|
|
7868
7901
|
timestamp: Date.now(),
|
|
7869
7902
|
isRead: false
|
|
@@ -7981,12 +8014,12 @@ var CommunicationsModule = class {
|
|
|
7981
8014
|
*/
|
|
7982
8015
|
async sendComposingIndicator(recipientPubkeyOrNametag) {
|
|
7983
8016
|
this.ensureInitialized();
|
|
7984
|
-
const
|
|
8017
|
+
const resolved = await this.resolveRecipient(recipientPubkeyOrNametag);
|
|
7985
8018
|
const content = JSON.stringify({
|
|
7986
8019
|
senderNametag: this.deps.identity.nametag,
|
|
7987
8020
|
expiresIn: 3e4
|
|
7988
8021
|
});
|
|
7989
|
-
await this.deps.transport.sendComposingIndicator?.(
|
|
8022
|
+
await this.deps.transport.sendComposingIndicator?.(resolved.pubkey, content);
|
|
7990
8023
|
}
|
|
7991
8024
|
/**
|
|
7992
8025
|
* Subscribe to incoming composing indicators
|
|
@@ -8174,13 +8207,14 @@ var CommunicationsModule = class {
|
|
|
8174
8207
|
// ===========================================================================
|
|
8175
8208
|
async resolveRecipient(recipient) {
|
|
8176
8209
|
if (recipient.startsWith("@")) {
|
|
8177
|
-
const
|
|
8210
|
+
const nametag = recipient.slice(1);
|
|
8211
|
+
const pubkey = await this.deps.transport.resolveNametag?.(nametag);
|
|
8178
8212
|
if (!pubkey) {
|
|
8179
8213
|
throw new Error(`Nametag not found: ${recipient}`);
|
|
8180
8214
|
}
|
|
8181
|
-
return pubkey;
|
|
8215
|
+
return { pubkey, nametag };
|
|
8182
8216
|
}
|
|
8183
|
-
return recipient;
|
|
8217
|
+
return { pubkey: recipient };
|
|
8184
8218
|
}
|
|
8185
8219
|
ensureInitialized() {
|
|
8186
8220
|
if (!this.deps) {
|
|
@@ -13191,45 +13225,22 @@ var Sphere = class _Sphere {
|
|
|
13191
13225
|
await _Sphere.instance.destroy();
|
|
13192
13226
|
console.log("[Sphere.clear] Sphere instance destroyed");
|
|
13193
13227
|
}
|
|
13228
|
+
console.log("[Sphere.clear] Clearing L1 vesting cache...");
|
|
13194
13229
|
await vestingClassifier.destroy();
|
|
13195
|
-
|
|
13196
|
-
|
|
13197
|
-
try {
|
|
13198
|
-
const dbs = await Promise.race([
|
|
13199
|
-
indexedDB.databases(),
|
|
13200
|
-
new Promise(
|
|
13201
|
-
(_, reject) => setTimeout(() => reject(new Error("timeout")), 2e3)
|
|
13202
|
-
)
|
|
13203
|
-
]);
|
|
13204
|
-
const sphereDbs = dbs.filter((db) => db.name?.startsWith("sphere"));
|
|
13205
|
-
if (sphereDbs.length > 0) {
|
|
13206
|
-
await Promise.all(sphereDbs.map(
|
|
13207
|
-
(db) => new Promise((resolve) => {
|
|
13208
|
-
const req = indexedDB.deleteDatabase(db.name);
|
|
13209
|
-
req.onsuccess = () => {
|
|
13210
|
-
console.log(`[Sphere.clear] Deleted ${db.name}`);
|
|
13211
|
-
resolve();
|
|
13212
|
-
};
|
|
13213
|
-
req.onerror = () => resolve();
|
|
13214
|
-
req.onblocked = () => {
|
|
13215
|
-
console.warn(`[Sphere.clear] deleteDatabase blocked: ${db.name}`);
|
|
13216
|
-
resolve();
|
|
13217
|
-
};
|
|
13218
|
-
})
|
|
13219
|
-
));
|
|
13220
|
-
}
|
|
13221
|
-
console.log("[Sphere.clear] IndexedDB cleanup done");
|
|
13222
|
-
} catch {
|
|
13223
|
-
console.warn("[Sphere.clear] IndexedDB enumeration failed");
|
|
13224
|
-
}
|
|
13225
|
-
}
|
|
13230
|
+
console.log("[Sphere.clear] Yielding 50ms for IDB transaction settlement...");
|
|
13231
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
13226
13232
|
if (tokenStorage?.clear) {
|
|
13233
|
+
console.log("[Sphere.clear] Clearing token storage...");
|
|
13227
13234
|
try {
|
|
13228
13235
|
await tokenStorage.clear();
|
|
13236
|
+
console.log("[Sphere.clear] Token storage cleared");
|
|
13229
13237
|
} catch (err) {
|
|
13230
13238
|
console.warn("[Sphere.clear] Token storage clear failed:", err);
|
|
13231
13239
|
}
|
|
13240
|
+
} else {
|
|
13241
|
+
console.log("[Sphere.clear] No token storage provider to clear");
|
|
13232
13242
|
}
|
|
13243
|
+
console.log("[Sphere.clear] Clearing KV storage...");
|
|
13233
13244
|
if (!storage.isConnected()) {
|
|
13234
13245
|
try {
|
|
13235
13246
|
await storage.connect();
|
|
@@ -13238,7 +13249,11 @@ var Sphere = class _Sphere {
|
|
|
13238
13249
|
}
|
|
13239
13250
|
if (storage.isConnected()) {
|
|
13240
13251
|
await storage.clear();
|
|
13252
|
+
console.log("[Sphere.clear] KV storage cleared");
|
|
13253
|
+
} else {
|
|
13254
|
+
console.log("[Sphere.clear] KV storage not connected, skipping");
|
|
13241
13255
|
}
|
|
13256
|
+
console.log("[Sphere.clear] Done");
|
|
13242
13257
|
}
|
|
13243
13258
|
/**
|
|
13244
13259
|
* Get current instance
|
|
@@ -14086,8 +14101,12 @@ var Sphere = class _Sphere {
|
|
|
14086
14101
|
await this._storage.set(STORAGE_KEYS_GLOBAL.CURRENT_ADDRESS_INDEX, index.toString());
|
|
14087
14102
|
this._storage.setIdentity(this._identity);
|
|
14088
14103
|
await this._transport.setIdentity(this._identity);
|
|
14089
|
-
|
|
14104
|
+
console.log(`[Sphere] switchToAddress(${index}): re-initializing ${this._tokenStorageProviders.size} token storage provider(s)`);
|
|
14105
|
+
for (const [providerId, provider] of this._tokenStorageProviders.entries()) {
|
|
14106
|
+
console.log(`[Sphere] switchToAddress(${index}): shutdown provider=${providerId}`);
|
|
14107
|
+
await provider.shutdown();
|
|
14090
14108
|
provider.setIdentity(this._identity);
|
|
14109
|
+
console.log(`[Sphere] switchToAddress(${index}): initialize provider=${providerId}`);
|
|
14091
14110
|
await provider.initialize();
|
|
14092
14111
|
}
|
|
14093
14112
|
await this.reinitializeModulesForNewAddress();
|
|
@@ -14169,6 +14188,9 @@ var Sphere = class _Sphere {
|
|
|
14169
14188
|
await this._communications.load();
|
|
14170
14189
|
await this._groupChat?.load();
|
|
14171
14190
|
await this._market?.load();
|
|
14191
|
+
this._payments.sync().catch((err) => {
|
|
14192
|
+
console.warn("[Sphere] Post-switch sync failed:", err);
|
|
14193
|
+
});
|
|
14172
14194
|
}
|
|
14173
14195
|
/**
|
|
14174
14196
|
* Derive address at a specific index
|