shogun-core 1.2.7 → 1.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -1
- package/dist/browser/shogun-core.js +1 -1
- package/dist/browser/shogun-core.js.LICENSE.txt +2 -0
- package/dist/browser/shogun-core.light.js +1 -1
- package/dist/browser/shogun-core.vendors.light.js +1 -1
- package/dist/core.js +31 -71
- package/dist/gundb/{instance.js → gunInstance.js} +135 -115
- package/dist/gundb/index.js +3 -20
- package/dist/plugins/index.js +23 -1
- package/dist/plugins/nostr/index.js +1 -0
- package/dist/plugins/nostr/nostrChain.js +128 -0
- package/dist/plugins/nostr/nostrConnector.js +42 -7
- package/dist/plugins/nostr/nostrConnectorPlugin.js +157 -1
- package/dist/plugins/nostr/nostrSigner.js +343 -0
- package/dist/plugins/oauth/index.js +13 -0
- package/dist/plugins/oauth/oauthChain.js +161 -0
- package/dist/plugins/oauth/oauthConnector.js +542 -0
- package/dist/plugins/oauth/oauthPlugin.js +302 -0
- package/dist/plugins/oauth/types.js +2 -0
- package/dist/plugins/web3/index.js +1 -0
- package/dist/plugins/web3/web3Chain.js +77 -2
- package/dist/plugins/web3/web3Connector.js +159 -37
- package/dist/plugins/web3/web3ConnectorPlugin.js +157 -1
- package/dist/plugins/web3/web3Signer.js +268 -0
- package/dist/plugins/webauthn/webauthnChain.js +78 -0
- package/dist/plugins/webauthn/webauthnPlugin.js +154 -1
- package/dist/plugins/webauthn/webauthnSigner.js +318 -0
- package/dist/storage/storage.js +0 -8
- package/dist/types/core.d.ts +10 -34
- package/dist/types/gundb/gun-es/gun-es.d.ts +1 -0
- package/dist/types/gundb/{instance.d.ts → gunInstance.d.ts} +2 -2
- package/dist/types/gundb/index.d.ts +1 -4
- package/dist/types/plugins/index.d.ts +4 -0
- package/dist/types/plugins/nostr/index.d.ts +1 -0
- package/dist/types/plugins/nostr/nostrConnector.d.ts +3 -2
- package/dist/types/plugins/nostr/nostrConnectorPlugin.d.ts +82 -0
- package/dist/types/plugins/nostr/nostrSigner.d.ts +104 -0
- package/dist/types/plugins/oauth/index.d.ts +4 -0
- package/dist/types/plugins/oauth/oauthChain.d.ts +2 -0
- package/dist/types/plugins/oauth/oauthConnector.d.ts +100 -0
- package/dist/types/plugins/oauth/oauthPlugin.d.ts +89 -0
- package/dist/types/plugins/oauth/types.d.ts +106 -0
- package/dist/types/plugins/web3/index.d.ts +1 -0
- package/dist/types/plugins/web3/types.d.ts +1 -0
- package/dist/types/plugins/web3/web3Connector.d.ts +8 -2
- package/dist/types/plugins/web3/web3ConnectorPlugin.d.ts +82 -0
- package/dist/types/plugins/web3/web3Signer.d.ts +93 -0
- package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +81 -0
- package/dist/types/plugins/webauthn/webauthnSigner.d.ts +90 -0
- package/dist/types/shogun.js +1 -28
- package/dist/types/types/events.d.ts +2 -2
- package/dist/types/types/shogun.d.ts +13 -49
- package/package.json +2 -1
- package/dist/browser.js +0 -107
- package/dist/contracts/base.js +0 -152
- package/dist/contracts/entryPoint.js +0 -407
- package/dist/contracts/index.js +0 -47
- package/dist/contracts/registry.js +0 -259
- package/dist/contracts/relay.js +0 -494
- package/dist/contracts/utils.js +0 -582
- package/dist/types/browser.d.ts +0 -27
- package/dist/types/contracts/base.d.ts +0 -82
- package/dist/types/contracts/entryPoint.d.ts +0 -138
- package/dist/types/contracts/index.d.ts +0 -17
- package/dist/types/contracts/registry.d.ts +0 -97
- package/dist/types/contracts/relay.d.ts +0 -165
- package/dist/types/contracts/utils.d.ts +0 -173
package/dist/contracts/utils.js
DELETED
|
@@ -1,582 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Utility functions for working with the relay system
|
|
4
|
-
*/
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.RelayVerifier = exports.RelayEventType = void 0;
|
|
7
|
-
exports.getRelayUrls = getRelayUrls;
|
|
8
|
-
exports.getRegisteredPubKeys = getRegisteredPubKeys;
|
|
9
|
-
exports.getSubscriptionHistory = getSubscriptionHistory;
|
|
10
|
-
exports.getRelayPerformance = getRelayPerformance;
|
|
11
|
-
exports.getNetworkSummary = getNetworkSummary;
|
|
12
|
-
exports.subscribeToRelayEvents = subscribeToRelayEvents;
|
|
13
|
-
exports.getUsageDataForChart = getUsageDataForChart;
|
|
14
|
-
const ethers_1 = require("ethers");
|
|
15
|
-
const relay_1 = require("./relay");
|
|
16
|
-
const events_1 = require("events");
|
|
17
|
-
const errorHandler_1 = require("../utils/errorHandler");
|
|
18
|
-
const logger_1 = require("../utils/logger");
|
|
19
|
-
var RelayEventType;
|
|
20
|
-
(function (RelayEventType) {
|
|
21
|
-
RelayEventType["NEW_SUBSCRIPTION"] = "newSubscription";
|
|
22
|
-
RelayEventType["SUBSCRIPTION_EXPIRED"] = "subscriptionExpired";
|
|
23
|
-
RelayEventType["RELAY_REGISTERED"] = "relayRegistered";
|
|
24
|
-
RelayEventType["RELAY_DEACTIVATED"] = "relayDeactivated";
|
|
25
|
-
RelayEventType["RELAY_REACTIVATED"] = "relayReactivated";
|
|
26
|
-
})(RelayEventType || (exports.RelayEventType = RelayEventType = {}));
|
|
27
|
-
// Event emitter singleton per gli eventi relay
|
|
28
|
-
const relayEventEmitter = new events_1.EventEmitter();
|
|
29
|
-
/**
|
|
30
|
-
* Fetch all relay URLs from the registry
|
|
31
|
-
* @param registry - Registry instance
|
|
32
|
-
* @param onlyActive - Whether to only fetch active relays (default: true)
|
|
33
|
-
* @returns Array of relay URLs and addresses
|
|
34
|
-
*/
|
|
35
|
-
async function getRelayUrls(registry, onlyActive = true) {
|
|
36
|
-
try {
|
|
37
|
-
const urls = [];
|
|
38
|
-
let offset = 0;
|
|
39
|
-
const limit = 100;
|
|
40
|
-
let hasMore = true;
|
|
41
|
-
// Paginate through all relays
|
|
42
|
-
while (hasMore) {
|
|
43
|
-
const page = await registry.getAllRelays(onlyActive, offset, limit);
|
|
44
|
-
if (!page || page.relays.length === 0) {
|
|
45
|
-
hasMore = false;
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
// Get info for each relay
|
|
49
|
-
for (const relayAddress of page.relays) {
|
|
50
|
-
const info = await registry.getRelayInfo(relayAddress);
|
|
51
|
-
if (info && info.url) {
|
|
52
|
-
urls.push({
|
|
53
|
-
url: info.url,
|
|
54
|
-
address: relayAddress,
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
// Check if there are more relays to fetch
|
|
59
|
-
offset += page.relays.length;
|
|
60
|
-
hasMore = offset < Number(page.total);
|
|
61
|
-
}
|
|
62
|
-
return urls;
|
|
63
|
-
}
|
|
64
|
-
catch (error) {
|
|
65
|
-
console.error("Failed to fetch relay URLs:", error);
|
|
66
|
-
return [];
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Retrieve all public keys registered in relays and group them
|
|
71
|
-
* @param registry - Registry instance
|
|
72
|
-
* @param entryPoint - EntryPoint instance
|
|
73
|
-
* @param userAddresses - Optional list of user addresses to check
|
|
74
|
-
* @param onlyActive - Whether to only fetch active relays (default: true)
|
|
75
|
-
* @returns Grouped public keys with their associated relays
|
|
76
|
-
*/
|
|
77
|
-
async function getRegisteredPubKeys(registry, entryPoint, userAddresses, onlyActive = true) {
|
|
78
|
-
try {
|
|
79
|
-
const pubKeys = [];
|
|
80
|
-
const relayUrls = await getRelayUrls(registry, onlyActive);
|
|
81
|
-
if (userAddresses && userAddresses.length > 0) {
|
|
82
|
-
// If we have specific user addresses, check their subscriptions
|
|
83
|
-
for (const userAddress of userAddresses) {
|
|
84
|
-
for (const relay of relayUrls) {
|
|
85
|
-
const hasKey = await entryPoint.hasRegisteredPubKey(userAddress, relay.address);
|
|
86
|
-
if (hasKey) {
|
|
87
|
-
const details = await entryPoint.getSubscriptionDetails(userAddress, relay.address);
|
|
88
|
-
if (details && details.pubKey) {
|
|
89
|
-
pubKeys.push({
|
|
90
|
-
relayAddress: relay.address,
|
|
91
|
-
relayUrl: relay.url,
|
|
92
|
-
pubKey: details.pubKey,
|
|
93
|
-
userAddress,
|
|
94
|
-
expires: details.expires,
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
// Without specific user addresses, we need to use an alternative approach
|
|
103
|
-
// Here we'll use batch checking for known public keys against each relay
|
|
104
|
-
// This is a placeholder implementation that needs to be adapted to your specific needs
|
|
105
|
-
// For example, you might have a way to query all public keys from a relay directly
|
|
106
|
-
// For demonstration purposes, we'll create a SimpleRelay instance for each relay
|
|
107
|
-
// and try to get information about active subscribers
|
|
108
|
-
for (const relay of relayUrls) {
|
|
109
|
-
try {
|
|
110
|
-
const simpleRelay = new relay_1.SimpleRelay({
|
|
111
|
-
relayAddress: relay.address,
|
|
112
|
-
registryAddress: registry.getAddress(),
|
|
113
|
-
providerUrl: registry.getAddress() ? undefined : undefined,
|
|
114
|
-
});
|
|
115
|
-
// Note: The SimpleRelay contract would need a method to list all subscribers
|
|
116
|
-
// or active public keys. Without such a method, this approach is limited.
|
|
117
|
-
// You might need to implement event listeners for subscription events
|
|
118
|
-
// and build a cache of public keys from those events.
|
|
119
|
-
// Example (pseudo-code) if your contract had such functionality:
|
|
120
|
-
// const activeKeys = await simpleRelay.getActivePublicKeys();
|
|
121
|
-
// for (const keyData of activeKeys) {
|
|
122
|
-
// pubKeys.push({
|
|
123
|
-
// relayAddress: relay.address,
|
|
124
|
-
// relayUrl: relay.url,
|
|
125
|
-
// pubKey: keyData.pubKey,
|
|
126
|
-
// userAddress: keyData.user,
|
|
127
|
-
// expires: keyData.expires
|
|
128
|
-
// });
|
|
129
|
-
// }
|
|
130
|
-
}
|
|
131
|
-
catch (error) {
|
|
132
|
-
console.error(`Error processing relay ${relay.address}:`, error);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
// Group public keys
|
|
137
|
-
const groupedPubKeys = {};
|
|
138
|
-
for (const entry of pubKeys) {
|
|
139
|
-
if (!groupedPubKeys[entry.pubKey]) {
|
|
140
|
-
groupedPubKeys[entry.pubKey] = {
|
|
141
|
-
pubKey: entry.pubKey,
|
|
142
|
-
relays: [],
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
groupedPubKeys[entry.pubKey].relays.push({
|
|
146
|
-
relayAddress: entry.relayAddress,
|
|
147
|
-
relayUrl: entry.relayUrl,
|
|
148
|
-
userAddress: entry.userAddress,
|
|
149
|
-
expires: entry.expires,
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
return groupedPubKeys;
|
|
153
|
-
}
|
|
154
|
-
catch (error) {
|
|
155
|
-
console.error("Failed to fetch registered public keys:", error);
|
|
156
|
-
return {};
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Ottieni la cronologia delle sottoscrizioni nel periodo specificato
|
|
161
|
-
* @param entryPoint - Istanza di EntryPoint
|
|
162
|
-
* @param timeframe - Periodo di tempo ('day', 'week', 'month')
|
|
163
|
-
* @returns Array di oggetti con date e conteggio sottoscrizioni
|
|
164
|
-
*/
|
|
165
|
-
async function getSubscriptionHistory(entryPoint, timeframe = "month") {
|
|
166
|
-
try {
|
|
167
|
-
// Questa implementazione è un mock che simula dati di sottoscrizioni nel tempo
|
|
168
|
-
// In un'implementazione reale, dovresti ottenere i dati effettivi dal contratto o da un database
|
|
169
|
-
const now = new Date();
|
|
170
|
-
const result = [];
|
|
171
|
-
let daysToGenerate;
|
|
172
|
-
switch (timeframe) {
|
|
173
|
-
case "day":
|
|
174
|
-
daysToGenerate = 24; // Per un giorno, generiamo dati orari
|
|
175
|
-
for (let i = 0; i < daysToGenerate; i++) {
|
|
176
|
-
const date = new Date(now);
|
|
177
|
-
date.setHours(date.getHours() - i);
|
|
178
|
-
result.push({
|
|
179
|
-
date: date.toISOString(),
|
|
180
|
-
// Generazione di numeri casuali per simulare i dati
|
|
181
|
-
count: Math.floor(Math.random() * 10) + 1,
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
break;
|
|
185
|
-
case "week":
|
|
186
|
-
daysToGenerate = 7;
|
|
187
|
-
for (let i = 0; i < daysToGenerate; i++) {
|
|
188
|
-
const date = new Date(now);
|
|
189
|
-
date.setDate(date.getDate() - i);
|
|
190
|
-
result.push({
|
|
191
|
-
date: date.toISOString().split("T")[0],
|
|
192
|
-
count: Math.floor(Math.random() * 25) + 5,
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
break;
|
|
196
|
-
case "month":
|
|
197
|
-
default:
|
|
198
|
-
daysToGenerate = 30;
|
|
199
|
-
for (let i = 0; i < daysToGenerate; i++) {
|
|
200
|
-
const date = new Date(now);
|
|
201
|
-
date.setDate(date.getDate() - i);
|
|
202
|
-
result.push({
|
|
203
|
-
date: date.toISOString().split("T")[0],
|
|
204
|
-
count: Math.floor(Math.random() * 50) + 10,
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
break;
|
|
208
|
-
}
|
|
209
|
-
// In un'implementazione reale, potresti utilizzare i log degli eventi di sottoscrizione dal contratto
|
|
210
|
-
// per compilare questi dati, o mantenere un database con queste informazioni aggiornate in tempo reale
|
|
211
|
-
// Ordina per data
|
|
212
|
-
return result.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime());
|
|
213
|
-
}
|
|
214
|
-
catch (error) {
|
|
215
|
-
console.error("Failed to fetch subscription history:", error);
|
|
216
|
-
return [];
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Ottieni le metriche di performance di un relay
|
|
221
|
-
* @param registry - Istanza di Registry
|
|
222
|
-
* @param relayAddress - Indirizzo del relay
|
|
223
|
-
* @returns Oggetto con metriche di performance
|
|
224
|
-
*/
|
|
225
|
-
async function getRelayPerformance(registry, relayAddress) {
|
|
226
|
-
try {
|
|
227
|
-
// Verifica che il relay esista e sia attivo
|
|
228
|
-
const isRegistered = await registry.isRegisteredRelay(relayAddress);
|
|
229
|
-
if (!isRegistered) {
|
|
230
|
-
throw new Error(`Relay ${relayAddress} non è registrato`);
|
|
231
|
-
}
|
|
232
|
-
const relayInfo = await registry.getRelayInfo(relayAddress);
|
|
233
|
-
if (!relayInfo) {
|
|
234
|
-
throw new Error(`Impossibile ottenere informazioni per il relay ${relayAddress}`);
|
|
235
|
-
}
|
|
236
|
-
// In un'implementazione reale, qui potresti fare una richiesta al relay
|
|
237
|
-
// per misurare il tempo di risposta e altri parametri
|
|
238
|
-
// Per ora, generiamo dati di esempio
|
|
239
|
-
// Mock dei dati di performance
|
|
240
|
-
const performance = {
|
|
241
|
-
uptime: relayInfo.active ? Math.random() * 5 + 95 : 0, // 95-100% se attivo, 0% se inattivo
|
|
242
|
-
responseTime: Math.floor(Math.random() * 500) + 50, // 50-550ms
|
|
243
|
-
successRate: relayInfo.active ? Math.random() * 10 + 90 : 0, // 90-100% se attivo, 0% se inattivo
|
|
244
|
-
lastChecked: new Date().toISOString(),
|
|
245
|
-
};
|
|
246
|
-
return performance;
|
|
247
|
-
}
|
|
248
|
-
catch (error) {
|
|
249
|
-
console.error(`Failed to fetch performance for relay ${relayAddress}:`, error);
|
|
250
|
-
// Restituisci valori di default in caso di errore
|
|
251
|
-
return {
|
|
252
|
-
uptime: 0,
|
|
253
|
-
responseTime: 0,
|
|
254
|
-
successRate: 0,
|
|
255
|
-
lastChecked: new Date().toISOString(),
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Ottiene un riepilogo di tutte le informazioni della rete relay
|
|
261
|
-
* @param registry - Istanza di Registry
|
|
262
|
-
* @param entryPoint - Istanza di EntryPoint
|
|
263
|
-
* @returns Oggetto con statistiche di rete
|
|
264
|
-
*/
|
|
265
|
-
async function getNetworkSummary(registry, entryPoint) {
|
|
266
|
-
try {
|
|
267
|
-
// Ottieni tutti i relay
|
|
268
|
-
const relaysPaginated = await registry.getAllRelays(false, 0, 1000);
|
|
269
|
-
if (!relaysPaginated) {
|
|
270
|
-
throw new Error("Impossibile ottenere l'elenco dei relay");
|
|
271
|
-
}
|
|
272
|
-
// Conta i relay attivi
|
|
273
|
-
let activeRelays = 0;
|
|
274
|
-
for (const relayAddress of relaysPaginated.relays) {
|
|
275
|
-
const isActive = await registry.isRelayActive(relayAddress);
|
|
276
|
-
if (isActive) {
|
|
277
|
-
activeRelays++;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
// Ottieni le statistiche dal contratto EntryPoint
|
|
281
|
-
const stats = await entryPoint.getStatistics();
|
|
282
|
-
// Calcola il prezzo medio (questo è un mock - in un'implementazione reale dovresti
|
|
283
|
-
// ottenere i prezzi effettivi dai contratti SimpleRelay)
|
|
284
|
-
let totalPrice = ethers_1.ethers.parseEther("0");
|
|
285
|
-
let relayCount = 0;
|
|
286
|
-
// In un'implementazione reale, dovresti ottenere il prezzo da ogni relay
|
|
287
|
-
// Per ora, simuliamo un prezzo medio
|
|
288
|
-
const averagePrice = ethers_1.ethers.parseEther("0.01");
|
|
289
|
-
return {
|
|
290
|
-
totalRelays: Number(relaysPaginated.total),
|
|
291
|
-
activeRelays,
|
|
292
|
-
totalSubscriptions: stats ? Number(stats.totalSubscriptions) : 0,
|
|
293
|
-
activeSubscriptions: stats ? Number(stats.totalSubscriptions) * 0.7 : 0, // Assumiamo che il 70% sia attivo
|
|
294
|
-
averagePrice: ethers_1.ethers.formatEther(averagePrice),
|
|
295
|
-
totalProcessedAmount: stats
|
|
296
|
-
? ethers_1.ethers.formatEther(stats.totalAmountProcessed)
|
|
297
|
-
: "0",
|
|
298
|
-
totalFeesCollected: stats
|
|
299
|
-
? ethers_1.ethers.formatEther(stats.totalFeesCollected)
|
|
300
|
-
: "0",
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
catch (error) {
|
|
304
|
-
console.error("Failed to fetch network summary:", error);
|
|
305
|
-
// Restituisci valori di default in caso di errore
|
|
306
|
-
return {
|
|
307
|
-
totalRelays: 0,
|
|
308
|
-
activeRelays: 0,
|
|
309
|
-
totalSubscriptions: 0,
|
|
310
|
-
activeSubscriptions: 0,
|
|
311
|
-
averagePrice: "0",
|
|
312
|
-
totalProcessedAmount: "0",
|
|
313
|
-
totalFeesCollected: "0",
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* Iscriviti agli eventi dei relay
|
|
319
|
-
* @param registry - Istanza di Registry
|
|
320
|
-
* @param callback - Funzione di callback da eseguire quando si verifica un evento
|
|
321
|
-
* @returns Funzione per annullare l'iscrizione
|
|
322
|
-
*/
|
|
323
|
-
function subscribeToRelayEvents(registry, callback) {
|
|
324
|
-
// In un'implementazione reale, dovresti ascoltare gli eventi effettivi dal contratto
|
|
325
|
-
// Questo è un mock che simula eventi periodici per scopi dimostrativi
|
|
326
|
-
// Ascolta l'emitter di eventi
|
|
327
|
-
relayEventEmitter.on("relayEvent", callback);
|
|
328
|
-
// Simula eventi casuali ogni 10 secondi
|
|
329
|
-
const interval = setInterval(() => {
|
|
330
|
-
const eventTypes = Object.values(RelayEventType);
|
|
331
|
-
const randomType = eventTypes[Math.floor(Math.random() * eventTypes.length)];
|
|
332
|
-
const event = {
|
|
333
|
-
type: randomType,
|
|
334
|
-
timestamp: Date.now(),
|
|
335
|
-
relayAddress: `0x${Math.random().toString(16).substring(2, 42)}`,
|
|
336
|
-
userAddress: randomType.includes("Subscription")
|
|
337
|
-
? `0x${Math.random().toString(16).substring(2, 42)}`
|
|
338
|
-
: undefined,
|
|
339
|
-
transactionHash: `0x${Math.random().toString(16).substring(2, 66)}`,
|
|
340
|
-
};
|
|
341
|
-
relayEventEmitter.emit("relayEvent", event);
|
|
342
|
-
}, 10000);
|
|
343
|
-
// Restituisci una funzione per annullare l'iscrizione e fermare la simulazione
|
|
344
|
-
return () => {
|
|
345
|
-
clearInterval(interval);
|
|
346
|
-
relayEventEmitter.removeListener("relayEvent", callback);
|
|
347
|
-
};
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Ottieni dati per grafici relativi all'utilizzo dei relay
|
|
351
|
-
* @param entryPoint - Istanza di EntryPoint
|
|
352
|
-
* @param metric - Metrica da visualizzare ('subscriptions', 'revenue', 'users')
|
|
353
|
-
* @param period - Periodo di tempo ('daily', 'weekly', 'monthly')
|
|
354
|
-
* @returns Dati formattati per grafici
|
|
355
|
-
*/
|
|
356
|
-
async function getUsageDataForChart(entryPoint, metric, period) {
|
|
357
|
-
try {
|
|
358
|
-
// Questa è un'implementazione mock che genera dati casuali
|
|
359
|
-
// In un'implementazione reale, dovresti ottenere i dati effettivi dal contratto o da un database
|
|
360
|
-
const now = new Date();
|
|
361
|
-
const dataPoints = [];
|
|
362
|
-
let daysToGenerate = 0;
|
|
363
|
-
let title = "";
|
|
364
|
-
let description = "";
|
|
365
|
-
// Configura il periodo
|
|
366
|
-
switch (period) {
|
|
367
|
-
case "daily":
|
|
368
|
-
daysToGenerate = 24;
|
|
369
|
-
break;
|
|
370
|
-
case "weekly":
|
|
371
|
-
daysToGenerate = 7;
|
|
372
|
-
break;
|
|
373
|
-
case "monthly":
|
|
374
|
-
default:
|
|
375
|
-
daysToGenerate = 30;
|
|
376
|
-
break;
|
|
377
|
-
}
|
|
378
|
-
// Genera dati in base alla metrica
|
|
379
|
-
for (let i = 0; i < daysToGenerate; i++) {
|
|
380
|
-
const date = new Date(now);
|
|
381
|
-
if (period === "daily") {
|
|
382
|
-
date.setHours(date.getHours() - i);
|
|
383
|
-
const label = `${date.getHours()}:00`;
|
|
384
|
-
let value = 0;
|
|
385
|
-
switch (metric) {
|
|
386
|
-
case "subscriptions":
|
|
387
|
-
value = Math.floor(Math.random() * 15) + 1;
|
|
388
|
-
break;
|
|
389
|
-
case "revenue":
|
|
390
|
-
value = Math.random() * 0.5 + 0.1; // 0.1-0.6 ETH
|
|
391
|
-
break;
|
|
392
|
-
case "users":
|
|
393
|
-
value = Math.floor(Math.random() * 20) + 5;
|
|
394
|
-
break;
|
|
395
|
-
}
|
|
396
|
-
dataPoints.push({ label, value });
|
|
397
|
-
}
|
|
398
|
-
else {
|
|
399
|
-
date.setDate(date.getDate() - i);
|
|
400
|
-
const label = date.toISOString().split("T")[0];
|
|
401
|
-
let value = 0;
|
|
402
|
-
switch (metric) {
|
|
403
|
-
case "subscriptions":
|
|
404
|
-
value = Math.floor(Math.random() * 50) + 10;
|
|
405
|
-
break;
|
|
406
|
-
case "revenue":
|
|
407
|
-
value = Math.random() * 2 + 0.5; // 0.5-2.5 ETH
|
|
408
|
-
break;
|
|
409
|
-
case "users":
|
|
410
|
-
value = Math.floor(Math.random() * 100) + 20;
|
|
411
|
-
break;
|
|
412
|
-
}
|
|
413
|
-
dataPoints.push({ label, value });
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
// Configura titolo e descrizione
|
|
417
|
-
switch (metric) {
|
|
418
|
-
case "subscriptions":
|
|
419
|
-
title = "Sottoscrizioni";
|
|
420
|
-
description = `Numero di sottoscrizioni ${period === "daily" ? "giornaliere" : period === "weekly" ? "settimanali" : "mensili"}`;
|
|
421
|
-
break;
|
|
422
|
-
case "revenue":
|
|
423
|
-
title = "Ricavi";
|
|
424
|
-
description = `Ricavi ${period === "daily" ? "giornalieri" : period === "weekly" ? "settimanali" : "mensili"} in ETH`;
|
|
425
|
-
break;
|
|
426
|
-
case "users":
|
|
427
|
-
title = "Utenti";
|
|
428
|
-
description = `Numero di utenti attivi ${period === "daily" ? "giornalieri" : period === "weekly" ? "settimanali" : "mensili"}`;
|
|
429
|
-
break;
|
|
430
|
-
}
|
|
431
|
-
// Ordina per data
|
|
432
|
-
const sortedDataPoints = dataPoints.sort((a, b) => {
|
|
433
|
-
if (period === "daily") {
|
|
434
|
-
return (parseInt(a.label.split(":")[0]) - parseInt(b.label.split(":")[0]));
|
|
435
|
-
}
|
|
436
|
-
else {
|
|
437
|
-
return new Date(a.label).getTime() - new Date(b.label).getTime();
|
|
438
|
-
}
|
|
439
|
-
});
|
|
440
|
-
return {
|
|
441
|
-
dataPoints: sortedDataPoints,
|
|
442
|
-
title,
|
|
443
|
-
description,
|
|
444
|
-
};
|
|
445
|
-
}
|
|
446
|
-
catch (error) {
|
|
447
|
-
console.error(`Failed to fetch chart data for ${metric}:`, error);
|
|
448
|
-
return {
|
|
449
|
-
dataPoints: [],
|
|
450
|
-
title: "Errore",
|
|
451
|
-
description: "Impossibile caricare i dati",
|
|
452
|
-
};
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* Create a combined relay verifier that can check public key authorization across all contract types
|
|
457
|
-
*/
|
|
458
|
-
class RelayVerifier {
|
|
459
|
-
registry = null;
|
|
460
|
-
entryPoint = null;
|
|
461
|
-
simpleRelay = null;
|
|
462
|
-
/**
|
|
463
|
-
* Create a new RelayVerifier
|
|
464
|
-
* @param registry - Optional Registry instance
|
|
465
|
-
* @param entryPoint - Optional EntryPoint instance
|
|
466
|
-
* @param simpleRelay - Optional SimpleRelay instance
|
|
467
|
-
*/
|
|
468
|
-
constructor(registry, entryPoint, simpleRelay) {
|
|
469
|
-
this.registry = registry ?? null;
|
|
470
|
-
this.entryPoint = entryPoint ?? null;
|
|
471
|
-
this.simpleRelay = simpleRelay ?? null;
|
|
472
|
-
}
|
|
473
|
-
/**
|
|
474
|
-
* Check if a public key is authorized (subscribed) to any relay
|
|
475
|
-
* @param registryAddress - The address of the registry (or any value if checking directly)
|
|
476
|
-
* @param pubKey - The public key to check (hex string or Uint8Array)
|
|
477
|
-
* @returns True if the public key is authorized, false otherwise
|
|
478
|
-
*/
|
|
479
|
-
async isPublicKeyAuthorized(registryAddress, pubKey) {
|
|
480
|
-
try {
|
|
481
|
-
// First try simpleRelay if available (most direct)
|
|
482
|
-
if (this.simpleRelay) {
|
|
483
|
-
try {
|
|
484
|
-
const isSubscribed = await this.simpleRelay.isSubscribed(pubKey);
|
|
485
|
-
if (isSubscribed)
|
|
486
|
-
return true;
|
|
487
|
-
}
|
|
488
|
-
catch (error) {
|
|
489
|
-
(0, logger_1.logError)("Error checking SimpleRelay subscription:", error);
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
// Then try entryPoint for protocol-mode relays
|
|
493
|
-
if (this.entryPoint && this.registry) {
|
|
494
|
-
try {
|
|
495
|
-
// Get all relays from registry
|
|
496
|
-
const relaysPage = await this.registry.getAllRelays(true, 0, 100);
|
|
497
|
-
if (relaysPage && relaysPage.relays && relaysPage.relays.length > 0) {
|
|
498
|
-
// Check each relay via entryPoint
|
|
499
|
-
for (const relayAddress of relaysPage.relays) {
|
|
500
|
-
const isPubKeySubscribed = await this.entryPoint.isPubKeySubscribed(relayAddress, pubKey);
|
|
501
|
-
if (isPubKeySubscribed)
|
|
502
|
-
return true;
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
catch (error) {
|
|
507
|
-
(0, logger_1.logError)("Error checking EntryPoint subscriptions:", error);
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
return false;
|
|
511
|
-
}
|
|
512
|
-
catch (error) {
|
|
513
|
-
errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.CONTRACT, "RELAY_VERIFIER_ERROR", "Error in isPublicKeyAuthorized check", error);
|
|
514
|
-
return false;
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
/**
|
|
518
|
-
* Check if a user is subscribed to a specific relay
|
|
519
|
-
* @param relayAddress - The address of the relay
|
|
520
|
-
* @param pubKey - The public key to check (hex string or Uint8Array)
|
|
521
|
-
* @returns True if the user is subscribed, false otherwise
|
|
522
|
-
*/
|
|
523
|
-
async isUserSubscribedToRelay(relayAddress, pubKey) {
|
|
524
|
-
try {
|
|
525
|
-
// If we have the individual relay, check directly
|
|
526
|
-
if (this.simpleRelay && this.simpleRelay.getAddress() === relayAddress) {
|
|
527
|
-
return await this.simpleRelay.isSubscribed(pubKey);
|
|
528
|
-
}
|
|
529
|
-
// Otherwise try via EntryPoint
|
|
530
|
-
if (this.entryPoint) {
|
|
531
|
-
return await this.entryPoint.isPubKeySubscribed(relayAddress, pubKey);
|
|
532
|
-
}
|
|
533
|
-
return false;
|
|
534
|
-
}
|
|
535
|
-
catch (error) {
|
|
536
|
-
errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.CONTRACT, "RELAY_VERIFIER_ERROR", `Error checking subscription to relay ${relayAddress}`, error);
|
|
537
|
-
return false;
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
/**
|
|
541
|
-
* Get all relays from registry
|
|
542
|
-
* @param onlyActive - If true, only return active relays
|
|
543
|
-
* @param offset - Starting index for pagination
|
|
544
|
-
* @param limit - Maximum number of items to return
|
|
545
|
-
* @returns Array of relay addresses
|
|
546
|
-
*/
|
|
547
|
-
async getAllRelays(onlyActive = true, offset = 0, limit = 100) {
|
|
548
|
-
try {
|
|
549
|
-
if (!this.registry) {
|
|
550
|
-
return [];
|
|
551
|
-
}
|
|
552
|
-
const relaysPage = await this.registry.getAllRelays(onlyActive, offset, limit);
|
|
553
|
-
return relaysPage?.relays || [];
|
|
554
|
-
}
|
|
555
|
-
catch (error) {
|
|
556
|
-
errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.CONTRACT, "RELAY_VERIFIER_ERROR", "Error getting all relays", error);
|
|
557
|
-
return [];
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
/**
|
|
561
|
-
* Set registry instance
|
|
562
|
-
* @param registry - The Registry instance
|
|
563
|
-
*/
|
|
564
|
-
setRegistry(registry) {
|
|
565
|
-
this.registry = registry;
|
|
566
|
-
}
|
|
567
|
-
/**
|
|
568
|
-
* Set entryPoint instance
|
|
569
|
-
* @param entryPoint - The EntryPoint instance
|
|
570
|
-
*/
|
|
571
|
-
setEntryPoint(entryPoint) {
|
|
572
|
-
this.entryPoint = entryPoint;
|
|
573
|
-
}
|
|
574
|
-
/**
|
|
575
|
-
* Set simpleRelay instance
|
|
576
|
-
* @param simpleRelay - The SimpleRelay instance
|
|
577
|
-
*/
|
|
578
|
-
setSimpleRelay(simpleRelay) {
|
|
579
|
-
this.simpleRelay = simpleRelay;
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
exports.RelayVerifier = RelayVerifier;
|
package/dist/types/browser.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { ShogunCore } from "./index";
|
|
2
|
-
import { ShogunSDKConfig } from "./types/shogun";
|
|
3
|
-
/**
|
|
4
|
-
* Function to initialize Shogun in a browser environment
|
|
5
|
-
*
|
|
6
|
-
* @param config - Configuration for the Shogun SDK
|
|
7
|
-
* @returns A new instance of ShogunCore
|
|
8
|
-
*
|
|
9
|
-
* @important For production use:
|
|
10
|
-
* - Always set custom GunDB peers via config.gundb.peers or config.peers
|
|
11
|
-
* - Always set a valid Ethereum RPC provider URL via config.providerUrl
|
|
12
|
-
* - Default values are provided only for development and testing
|
|
13
|
-
*/
|
|
14
|
-
export declare function initShogunBrowser(config: ShogunSDKConfig): ShogunCore;
|
|
15
|
-
export declare const modules: {
|
|
16
|
-
webauthn: {
|
|
17
|
-
loadWebAuthn: () => Promise<typeof import("./plugins/webauthn/webauthn")>;
|
|
18
|
-
};
|
|
19
|
-
web3: {
|
|
20
|
-
loadMetaMask: () => Promise<typeof import("./plugins/web3/web3ConnectorPlugin")>;
|
|
21
|
-
};
|
|
22
|
-
nostr: {
|
|
23
|
-
loadNostrConnector: () => Promise<typeof import("./plugins/nostr/nostrConnectorPlugin")>;
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
export { ShogunCore };
|
|
27
|
-
export * from "./types/shogun";
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shogun Protocol Contracts SDK
|
|
3
|
-
* This file provides interfaces and ABIs for interacting with the Shogun Protocol smart contracts
|
|
4
|
-
*/
|
|
5
|
-
import { ethers } from "ethers";
|
|
6
|
-
declare const REGISTRY_ABI: string[];
|
|
7
|
-
declare const SIMPLE_RELAY_ABI: string[];
|
|
8
|
-
declare const ENTRY_POINT_ABI: string[];
|
|
9
|
-
export interface RelayInfo {
|
|
10
|
-
owner: string;
|
|
11
|
-
url: string;
|
|
12
|
-
metadata: string;
|
|
13
|
-
registrationTime: bigint;
|
|
14
|
-
active: boolean;
|
|
15
|
-
}
|
|
16
|
-
export interface RelayPage {
|
|
17
|
-
relays: string[];
|
|
18
|
-
total: bigint;
|
|
19
|
-
offset: bigint;
|
|
20
|
-
limit: bigint;
|
|
21
|
-
}
|
|
22
|
-
export interface SubscriptionDetails {
|
|
23
|
-
expires: bigint;
|
|
24
|
-
pubKey: string;
|
|
25
|
-
}
|
|
26
|
-
export interface RelayConfig {
|
|
27
|
-
url: string;
|
|
28
|
-
price: bigint;
|
|
29
|
-
daysInMonth: bigint;
|
|
30
|
-
}
|
|
31
|
-
export declare enum RelayOperatingMode {
|
|
32
|
-
SINGLE = 0,
|
|
33
|
-
PROTOCOL = 1
|
|
34
|
-
}
|
|
35
|
-
export interface RelayModeInfo {
|
|
36
|
-
mode: RelayOperatingMode;
|
|
37
|
-
registryAddress: string;
|
|
38
|
-
entryPointAddress: string;
|
|
39
|
-
isRegistered: boolean;
|
|
40
|
-
}
|
|
41
|
-
export interface EntryPointStats {
|
|
42
|
-
userCount: bigint;
|
|
43
|
-
totalSubscriptions: bigint;
|
|
44
|
-
totalViaUrl: bigint;
|
|
45
|
-
totalViaDirect: bigint;
|
|
46
|
-
totalAmountProcessed: bigint;
|
|
47
|
-
totalFeesCollected: bigint;
|
|
48
|
-
currentBalance: bigint;
|
|
49
|
-
}
|
|
50
|
-
export interface ContractConfig {
|
|
51
|
-
providerUrl?: string;
|
|
52
|
-
provider?: ethers.Provider;
|
|
53
|
-
signer?: ethers.Signer;
|
|
54
|
-
registryAddress: string;
|
|
55
|
-
entryPointAddress?: string;
|
|
56
|
-
}
|
|
57
|
-
declare abstract class BaseContract {
|
|
58
|
-
protected provider: ethers.Provider | null;
|
|
59
|
-
protected signer: ethers.Signer | null;
|
|
60
|
-
protected contract: ethers.Contract | null;
|
|
61
|
-
protected contractAddress: string;
|
|
62
|
-
constructor(address: string, abi: string[], config: ContractConfig);
|
|
63
|
-
/**
|
|
64
|
-
* Set a new provider
|
|
65
|
-
* @param provider - The new provider
|
|
66
|
-
*/
|
|
67
|
-
setProvider(provider: ethers.Provider): void;
|
|
68
|
-
/**
|
|
69
|
-
* Set a new signer
|
|
70
|
-
* @param signer - The new signer
|
|
71
|
-
*/
|
|
72
|
-
setSigner(signer: ethers.Signer): void;
|
|
73
|
-
/**
|
|
74
|
-
* Get the contract address
|
|
75
|
-
* @returns The contract address
|
|
76
|
-
*/
|
|
77
|
-
getAddress(): string;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Export all the ABIs and interfaces for use in other modules
|
|
81
|
-
*/
|
|
82
|
-
export { REGISTRY_ABI, SIMPLE_RELAY_ABI, ENTRY_POINT_ABI, BaseContract };
|