shogun-core 1.0.0 → 1.0.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/LICENSE +21 -0
- package/README.md +498 -6
- 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/browser.js +70 -24
- package/dist/config.js +5 -20
- package/dist/gun/certificates.js +90 -0
- package/dist/gun/collections.js +149 -0
- package/dist/gun/consensus.js +307 -0
- package/dist/gun/encryption.js +224 -0
- package/dist/gun/errors.js +24 -14
- package/dist/gun/example-model-repository.js +106 -0
- package/dist/gun/gun.js +700 -281
- package/dist/gun/index.js +11 -0
- package/dist/gun/repository.js +199 -0
- package/dist/gun/rxjs-integration.js +17 -11
- package/dist/gun/utils.js +53 -0
- package/dist/index.js +303 -195
- package/dist/plugins/activitypub/rsaSigner.js +158 -0
- package/dist/plugins/base.js +12 -5
- package/dist/plugins/did/DID.js +52 -46
- package/dist/plugins/did/didPlugin.js +76 -55
- package/dist/plugins/did/index.js +20 -2
- package/dist/plugins/did/types.js +14 -1
- package/dist/plugins/index.js +35 -11
- package/dist/plugins/metamask/index.js +19 -3
- package/dist/plugins/metamask/metamask.js +71 -63
- package/dist/plugins/metamask/metamaskPlugin.js +60 -60
- package/dist/plugins/metamask/types.js +2 -1
- package/dist/plugins/stealth/index.js +20 -2
- package/dist/plugins/stealth/stealth.js +145 -60
- package/dist/plugins/stealth/stealthPlugin.js +21 -14
- package/dist/plugins/stealth/types.js +2 -1
- package/dist/plugins/utils/stubs/didStub.js +5 -2
- package/dist/plugins/utils/stubs/stealthStub.js +5 -2
- package/dist/plugins/utils/stubs/webauthnStub.js +5 -2
- package/dist/plugins/wallet/index.js +20 -2
- package/dist/plugins/wallet/types.js +15 -1
- package/dist/plugins/wallet/walletManager.js +467 -221
- package/dist/plugins/wallet/walletPlugin.js +34 -27
- package/dist/plugins/webauthn/index.js +19 -3
- package/dist/plugins/webauthn/types.js +14 -1
- package/dist/plugins/webauthn/webauthn.js +32 -25
- package/dist/plugins/webauthn/webauthnPlugin.js +31 -32
- package/dist/storage/storage.js +23 -9
- package/dist/types/__mocks__/gun.d.ts +13 -0
- package/dist/types/common.js +2 -4
- package/dist/types/config.d.ts +0 -18
- package/dist/{cjs → types}/events.js +1 -1
- package/dist/types/gun/certificates.d.ts +48 -0
- package/dist/types/gun/collections.d.ts +49 -0
- package/dist/types/gun/consensus.d.ts +85 -0
- package/dist/types/gun/encryption.d.ts +105 -0
- package/dist/types/gun/errors.d.ts +7 -7
- package/dist/types/gun/example-model-repository.d.ts +81 -0
- package/dist/types/gun/gun.d.ts +314 -92
- package/dist/types/gun/index.d.ts +7 -0
- package/dist/types/gun/repository.d.ts +87 -0
- package/dist/types/gun/rxjs-integration.d.ts +3 -3
- package/dist/types/{types/gun.d.ts → gun/types.d.ts} +20 -7
- package/dist/types/gun/utils.d.ts +49 -0
- package/dist/types/index.d.ts +17 -10
- package/dist/types/plugin.js +2 -1
- package/dist/types/plugins/activitypub/rsaSigner.d.ts +32 -0
- package/dist/types/plugins/base.d.ts +2 -1
- package/dist/types/plugins/did/DID.d.ts +2 -2
- package/dist/types/plugins/did/didPlugin.d.ts +8 -2
- package/dist/types/plugins/did/index.d.ts +1 -1
- package/dist/types/plugins/did/types.d.ts +100 -1
- package/dist/types/plugins/index.d.ts +6 -6
- package/dist/types/plugins/metamask/metamask.d.ts +1 -2
- package/dist/types/plugins/metamask/metamaskPlugin.d.ts +2 -3
- package/dist/types/plugins/metamask/types.d.ts +45 -1
- package/dist/types/plugins/stealth/index.d.ts +1 -1
- package/dist/types/plugins/stealth/stealth.d.ts +30 -3
- package/dist/types/plugins/stealth/stealthPlugin.d.ts +6 -2
- package/dist/types/plugins/stealth/types.d.ts +49 -1
- package/dist/types/plugins/wallet/index.d.ts +1 -1
- package/dist/types/plugins/wallet/types.d.ts +84 -1
- package/dist/types/plugins/wallet/walletManager.d.ts +54 -3
- package/dist/types/plugins/wallet/walletPlugin.d.ts +8 -2
- package/dist/types/plugins/webauthn/types.d.ts +92 -1
- package/dist/types/plugins/webauthn/webauthn.d.ts +3 -3
- package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +2 -2
- package/dist/types/shogun.js +7 -4
- package/dist/types/storage/storage.d.ts +1 -0
- package/dist/types/types/common.d.ts +0 -3
- package/dist/types/{events.d.ts → types/events.d.ts} +1 -1
- package/dist/types/types/shogun.d.ts +8 -9
- package/dist/types/utils/errorHandler.d.ts +28 -0
- package/dist/types/utils/utility.d.ts +1 -1
- package/dist/utils/errorHandler.js +55 -16
- package/dist/utils/eventEmitter.js +8 -3
- package/dist/utils/logger.js +18 -8
- package/dist/utils/stubs/didStub.js +5 -2
- package/dist/utils/stubs/stealthStub.js +5 -2
- package/dist/utils/stubs/webauthnStub.js +5 -2
- package/dist/utils/utility.js +13 -5
- package/package.json +21 -11
- package/dist/browser/662.shogun-core.light.js +0 -1
- package/dist/cjs/browser.js +0 -109
- package/dist/cjs/config.js +0 -36
- package/dist/cjs/examples/all-plugins-usage.js +0 -171
- package/dist/cjs/examples/wallet-plugin-usage.js +0 -113
- package/dist/cjs/gun/errors.js +0 -76
- package/dist/cjs/gun/gun.js +0 -457
- package/dist/cjs/gun/rxjs-integration.js +0 -274
- package/dist/cjs/index.js +0 -882
- package/dist/cjs/plugins/base.js +0 -38
- package/dist/cjs/plugins/did/DID.js +0 -780
- package/dist/cjs/plugins/did/didPlugin.js +0 -147
- package/dist/cjs/plugins/did/index.js +0 -20
- package/dist/cjs/plugins/index.js +0 -36
- package/dist/cjs/plugins/metamask/index.js +0 -19
- package/dist/cjs/plugins/metamask/metamask.js +0 -433
- package/dist/cjs/plugins/metamask/metamaskPlugin.js +0 -298
- package/dist/cjs/plugins/metamask/types.js +0 -2
- package/dist/cjs/plugins/stealth/index.js +0 -20
- package/dist/cjs/plugins/stealth/stealth.js +0 -576
- package/dist/cjs/plugins/stealth/stealthPlugin.js +0 -80
- package/dist/cjs/plugins/stealth/types.js +0 -2
- package/dist/cjs/plugins/utils/stubs/didStub.js +0 -35
- package/dist/cjs/plugins/utils/stubs/stealthStub.js +0 -35
- package/dist/cjs/plugins/utils/stubs/webauthnStub.js +0 -29
- package/dist/cjs/plugins/wallet/index.js +0 -20
- package/dist/cjs/plugins/wallet/types.js +0 -2
- package/dist/cjs/plugins/wallet/walletManager.js +0 -1562
- package/dist/cjs/plugins/wallet/walletPlugin.js +0 -215
- package/dist/cjs/plugins/webauthn/index.js +0 -19
- package/dist/cjs/plugins/webauthn/types.js +0 -2
- package/dist/cjs/plugins/webauthn/webauthn.js +0 -465
- package/dist/cjs/plugins/webauthn/webauthnPlugin.js +0 -238
- package/dist/cjs/storage/storage.js +0 -146
- package/dist/cjs/types/common.js +0 -5
- package/dist/cjs/types/did.js +0 -14
- package/dist/cjs/types/gun.js +0 -5
- package/dist/cjs/types/metamask.js +0 -2
- package/dist/cjs/types/plugin.js +0 -2
- package/dist/cjs/types/shogun.js +0 -35
- package/dist/cjs/types/stealth.js +0 -2
- package/dist/cjs/types/wallet.js +0 -15
- package/dist/cjs/types/webauthn.js +0 -14
- package/dist/cjs/utils/errorHandler.js +0 -192
- package/dist/cjs/utils/eventEmitter.js +0 -81
- package/dist/cjs/utils/logger.js +0 -105
- package/dist/cjs/utils/stubs/didStub.js +0 -35
- package/dist/cjs/utils/stubs/stealthStub.js +0 -35
- package/dist/cjs/utils/stubs/webauthnStub.js +0 -29
- package/dist/cjs/utils/utility.js +0 -62
- package/dist/events.js +0 -36
- package/dist/examples/all-plugins-usage.js +0 -169
- package/dist/examples/wallet-plugin-usage.js +0 -111
- package/dist/types/did.js +0 -11
- package/dist/types/examples/all-plugins-usage.d.ts +0 -1
- package/dist/types/examples/wallet-plugin-usage.d.ts +0 -1
- package/dist/types/gun.js +0 -4
- package/dist/types/metamask.js +0 -1
- package/dist/types/stealth.js +0 -1
- package/dist/types/types/did.d.ts +0 -103
- package/dist/types/types/metamask.d.ts +0 -55
- package/dist/types/types/stealth.d.ts +0 -40
- package/dist/types/types/wallet.d.ts +0 -73
- package/dist/types/types/webauthn.d.ts +0 -92
- package/dist/types/wallet.js +0 -12
- package/dist/types/webauthn.js +0 -11
- /package/dist/{cjs/plugins/did → gun}/types.js +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-2024 Scobru
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
# Shogun SDK
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/shogun-core)
|
|
3
|
+
[](https://github.com/scobru/shogun-core/actions/workflows/tests.yml)
|
|
4
|
+
[](https://coveralls.io/github/scobru/shogun-core?branch=main)
|
|
5
|
+
[](https://www.npmjs.com/package/shogun-core)
|
|
8
6
|
[](https://opensource.org/licenses/MIT)
|
|
9
|
-
[](https://www.typescriptlang.org/)
|
|
10
8
|
|
|
11
9
|
## Table of Contents
|
|
12
10
|
|
|
@@ -36,6 +34,7 @@
|
|
|
36
34
|
- [Creating a Plugin](#creating-a-plugin)
|
|
37
35
|
- [Registering a Plugin](#registering-a-plugin)
|
|
38
36
|
- [Using the Plugin](#using-the-plugin)
|
|
37
|
+
- [Stealth Transactions](#stealth-transactions)
|
|
39
38
|
- [Browser Integration](#browser-integration)
|
|
40
39
|
- [Setup](#setup)
|
|
41
40
|
- [Examples](#examples)
|
|
@@ -217,6 +216,28 @@ if (walletPlugin) {
|
|
|
217
216
|
const mainWallet = walletPlugin.getMainWallet();
|
|
218
217
|
}
|
|
219
218
|
|
|
219
|
+
// Utilizzo dello Stealth Plugin
|
|
220
|
+
if (shogun.hasPlugin(CorePlugins.Stealth)) {
|
|
221
|
+
const stealthPlugin = shogun.getPlugin(CorePlugins.Stealth);
|
|
222
|
+
|
|
223
|
+
// Genera un indirizzo stealth per ricevere pagamenti in modo privato
|
|
224
|
+
const stealthAddress = await stealthPlugin.generateStealthAddress();
|
|
225
|
+
console.log("Indirizzo stealth generato:", stealthAddress);
|
|
226
|
+
|
|
227
|
+
// Crea una transazione privata verso un indirizzo stealth
|
|
228
|
+
const receiverStealthAddress = "0xEccetera..."; // Indirizzo stealth del destinatario
|
|
229
|
+
const amount = "0.01"; // Quantità in ETH
|
|
230
|
+
const txResult = await stealthPlugin.createStealthTransaction(
|
|
231
|
+
receiverStealthAddress,
|
|
232
|
+
amount
|
|
233
|
+
);
|
|
234
|
+
console.log("Transazione stealth inviata:", txResult);
|
|
235
|
+
|
|
236
|
+
// Scansiona la blockchain per trovare pagamenti stealth indirizzati a te
|
|
237
|
+
const payments = await stealthPlugin.scanStealthPayments();
|
|
238
|
+
console.log("Pagamenti stealth trovati:", payments);
|
|
239
|
+
}
|
|
240
|
+
|
|
220
241
|
// Ottenere i plugin per categoria
|
|
221
242
|
const walletPlugins = shogun.getPluginsByCategory(PluginCategory.Wallet);
|
|
222
243
|
const authPlugins = shogun.getPluginsByCategory(PluginCategory.Authentication);
|
|
@@ -356,6 +377,367 @@ if (shogun.isLoggedIn()) {
|
|
|
356
377
|
}
|
|
357
378
|
```
|
|
358
379
|
|
|
380
|
+
### Advanced GunDB Features
|
|
381
|
+
|
|
382
|
+
Shogun Core includes advanced GunDB features that allow you to build complex and robust decentralized applications.
|
|
383
|
+
|
|
384
|
+
#### Repository Pattern
|
|
385
|
+
|
|
386
|
+
The Repository Pattern provides an elegant abstraction for data access, with support for typing, transformation, and encryption.
|
|
387
|
+
|
|
388
|
+
```typescript
|
|
389
|
+
import { ShogunCore, GunRepository } from "shogun-core";
|
|
390
|
+
|
|
391
|
+
// Define an interface for your data type
|
|
392
|
+
interface User {
|
|
393
|
+
id?: string;
|
|
394
|
+
name: string;
|
|
395
|
+
email: string;
|
|
396
|
+
role: string;
|
|
397
|
+
lastLogin?: number;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// Create a typed repository class
|
|
401
|
+
class UserRepository extends GunRepository<User> {
|
|
402
|
+
constructor(shogun: ShogunCore) {
|
|
403
|
+
// Initialize with options: userScope=true, encryption=true
|
|
404
|
+
super(shogun.gun, "users", {
|
|
405
|
+
userScope: true,
|
|
406
|
+
useEncryption: true,
|
|
407
|
+
encryptionKey: shogun.gun.getCurrentUser()?.user._?.sea,
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// Implement transformation from raw data to entity
|
|
412
|
+
protected mapToEntity(data: any): User {
|
|
413
|
+
return {
|
|
414
|
+
id: data.id,
|
|
415
|
+
name: data.name || "",
|
|
416
|
+
email: data.email || "",
|
|
417
|
+
role: data.role || "user",
|
|
418
|
+
lastLogin: data.lastLogin || Date.now(),
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Implement transformation from entity to data to save
|
|
423
|
+
protected mapToData(entity: User): any {
|
|
424
|
+
// Remove computed or unnecessary fields
|
|
425
|
+
const { id, ...data } = entity;
|
|
426
|
+
return data;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Add entity-specific methods
|
|
430
|
+
async findByRole(role: string): Promise<User[]> {
|
|
431
|
+
const all = await this.findAll();
|
|
432
|
+
return all.filter((user) => user.role === role);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// Usage:
|
|
437
|
+
const shogun = new ShogunCore({
|
|
438
|
+
/* config */
|
|
439
|
+
});
|
|
440
|
+
const userRepo = new UserRepository(shogun);
|
|
441
|
+
|
|
442
|
+
// Create a new user
|
|
443
|
+
const userId = await userRepo.save({
|
|
444
|
+
name: "John Smith",
|
|
445
|
+
email: "john@example.com",
|
|
446
|
+
role: "admin",
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
// Find a specific user
|
|
450
|
+
const user = await userRepo.findById(userId);
|
|
451
|
+
|
|
452
|
+
// Update a user
|
|
453
|
+
await userRepo.update(userId, { role: "superadmin" });
|
|
454
|
+
|
|
455
|
+
// Delete a user
|
|
456
|
+
await userRepo.remove(userId);
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
#### Distributed Consensus
|
|
460
|
+
|
|
461
|
+
The Consensus module allows you to implement decentralized decision-making mechanisms based on voting.
|
|
462
|
+
|
|
463
|
+
```typescript
|
|
464
|
+
import { ShogunCore } from "shogun-core";
|
|
465
|
+
|
|
466
|
+
const shogun = new ShogunCore({
|
|
467
|
+
/* config */
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
// Consensus configuration (optional)
|
|
471
|
+
const consensusConfig = {
|
|
472
|
+
threshold: 0.66, // Require 66% of votes in favor
|
|
473
|
+
timeWindow: 3600000, // 1 hour voting window
|
|
474
|
+
minVotes: 5, // Require at least 5 votes
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
// Access the consensus system
|
|
478
|
+
const consensus = shogun.gun.consensus(consensusConfig);
|
|
479
|
+
|
|
480
|
+
// 1. Propose a change
|
|
481
|
+
async function proposeChange() {
|
|
482
|
+
const proposalId = await consensus.proposeChange(
|
|
483
|
+
"config-update",
|
|
484
|
+
{ maxUsers: 1000, featureFlag: true },
|
|
485
|
+
{ importance: "high" }
|
|
486
|
+
);
|
|
487
|
+
console.log(`Proposal created: ${proposalId}`);
|
|
488
|
+
return proposalId;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// 2. Vote on a proposal
|
|
492
|
+
async function vote(proposalId, approve) {
|
|
493
|
+
await consensus.vote(proposalId, approve, "Reason for vote");
|
|
494
|
+
console.log(`Vote recorded: ${approve ? "Approved" : "Rejected"}`);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// 3. Check proposal status
|
|
498
|
+
async function checkProposal(proposalId) {
|
|
499
|
+
const proposal = await consensus.getProposal(proposalId);
|
|
500
|
+
console.log(`Proposal status: ${proposal.status}`);
|
|
501
|
+
|
|
502
|
+
// Count votes
|
|
503
|
+
const voteCount = await consensus.countVotes(proposalId);
|
|
504
|
+
console.log(
|
|
505
|
+
`Votes: ${voteCount.approvalCount} approvals, ${voteCount.rejectionCount} rejections`
|
|
506
|
+
);
|
|
507
|
+
console.log(
|
|
508
|
+
`The proposal is ${voteCount.approved ? "approved" : "rejected or pending"}`
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
#### Immutable Data Space (Frozen Space)
|
|
514
|
+
|
|
515
|
+
The Frozen Space provides a mechanism for immutable data, ideal for logs, audit trails, and content archiving.
|
|
516
|
+
|
|
517
|
+
```typescript
|
|
518
|
+
import { ShogunCore } from "shogun-core";
|
|
519
|
+
|
|
520
|
+
const shogun = new ShogunCore({
|
|
521
|
+
/* config */
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
// Save immutable data
|
|
525
|
+
async function savePermanentData() {
|
|
526
|
+
const data = {
|
|
527
|
+
content: "This content cannot be modified",
|
|
528
|
+
timestamp: Date.now(),
|
|
529
|
+
author: "John Smith",
|
|
530
|
+
};
|
|
531
|
+
|
|
532
|
+
// Add to Frozen Space
|
|
533
|
+
await shogun.gun.addToFrozenSpace("documents", "doc1", data);
|
|
534
|
+
console.log("Document saved immutably");
|
|
535
|
+
|
|
536
|
+
// Alternative: use content hash as key
|
|
537
|
+
const contentHash = await shogun.gun.addHashedToFrozenSpace(
|
|
538
|
+
"documents",
|
|
539
|
+
data
|
|
540
|
+
);
|
|
541
|
+
console.log(`Document saved with hash: ${contentHash}`);
|
|
542
|
+
|
|
543
|
+
return contentHash;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// Retrieve immutable data
|
|
547
|
+
async function getPermanentData(hash) {
|
|
548
|
+
// Retrieve data using hash, with integrity verification
|
|
549
|
+
const data = await shogun.gun.getHashedFrozenData("documents", hash, true);
|
|
550
|
+
console.log("Document retrieved:", data);
|
|
551
|
+
return data;
|
|
552
|
+
}
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
#### Authorization Certificates
|
|
556
|
+
|
|
557
|
+
The certificate system enables decentralized and delegated authorizations.
|
|
558
|
+
|
|
559
|
+
```typescript
|
|
560
|
+
import { ShogunCore } from "shogun-core";
|
|
561
|
+
|
|
562
|
+
const shogun = new ShogunCore({
|
|
563
|
+
/* config */
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
// Generate authorization certificates
|
|
567
|
+
async function generateCertificates() {
|
|
568
|
+
// Get the key pair from the current user
|
|
569
|
+
const pair = shogun.gun.getCurrentUser()?.user._?.sea;
|
|
570
|
+
|
|
571
|
+
// Generate a single certificate
|
|
572
|
+
const cert = await shogun.gun.issueCert({
|
|
573
|
+
pair,
|
|
574
|
+
tag: "write", // Authorization type
|
|
575
|
+
dot: "tasks", // Authorized path
|
|
576
|
+
users: "*", // For all users ('*') or specific ('~pubKey')
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
// Generate multiple certificates at once
|
|
580
|
+
const certs = await shogun.gun.generateCerts({
|
|
581
|
+
pair,
|
|
582
|
+
list: [
|
|
583
|
+
{ tag: "read", dot: "profiles", users: "*" },
|
|
584
|
+
{ tag: "write", dot: "profiles", users: "~ABCDEF" }, // Specific user
|
|
585
|
+
{ tag: "admin", dot: "settings", personal: true }, // For personal use only
|
|
586
|
+
],
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
return { cert, certs };
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
// Verify a certificate
|
|
593
|
+
async function verifyCertificate(cert, pubKey) {
|
|
594
|
+
const isValid = await shogun.gun.verifyCert(cert, pubKey);
|
|
595
|
+
console.log(`Certificate is ${isValid ? "valid" : "invalid"}`);
|
|
596
|
+
|
|
597
|
+
// Extract policy from certificate
|
|
598
|
+
const policy = await shogun.gun.extractCertPolicy(cert);
|
|
599
|
+
console.log("Certificate policy:", policy);
|
|
600
|
+
|
|
601
|
+
return isValid;
|
|
602
|
+
}
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
#### Managed Collections
|
|
606
|
+
|
|
607
|
+
The Collections API simplifies managing collections of data with common operations.
|
|
608
|
+
|
|
609
|
+
```typescript
|
|
610
|
+
import { ShogunCore } from "shogun-core";
|
|
611
|
+
|
|
612
|
+
const shogun = new ShogunCore({
|
|
613
|
+
/* config */
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
// Access the Collections module
|
|
617
|
+
const collections = shogun.gun.collections();
|
|
618
|
+
|
|
619
|
+
// Add an item to a collection
|
|
620
|
+
async function addToCollection() {
|
|
621
|
+
const itemId = await collections.add("products", {
|
|
622
|
+
name: "Smartphone XYZ",
|
|
623
|
+
price: 499.99,
|
|
624
|
+
category: "electronics",
|
|
625
|
+
inStock: true,
|
|
626
|
+
});
|
|
627
|
+
console.log(`Product added with ID: ${itemId}`);
|
|
628
|
+
return itemId;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// Update an item
|
|
632
|
+
async function updateCollectionItem(id) {
|
|
633
|
+
await collections.update("products", id, {
|
|
634
|
+
price: 449.99,
|
|
635
|
+
inStock: false,
|
|
636
|
+
});
|
|
637
|
+
console.log("Product updated");
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// Retrieve all items
|
|
641
|
+
async function getAllItems() {
|
|
642
|
+
const items = await collections.findAll("products");
|
|
643
|
+
console.log(`Found ${items.length} products`);
|
|
644
|
+
return items;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// Retrieve a specific item
|
|
648
|
+
async function getItem(id) {
|
|
649
|
+
const item = await collections.findById("products", id);
|
|
650
|
+
console.log("Product found:", item);
|
|
651
|
+
return item;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// Remove an item
|
|
655
|
+
async function removeItem(id) {
|
|
656
|
+
await collections.remove("products", id);
|
|
657
|
+
console.log("Product removed");
|
|
658
|
+
}
|
|
659
|
+
```
|
|
660
|
+
|
|
661
|
+
#### Advanced Cryptographic Utilities
|
|
662
|
+
|
|
663
|
+
Shogun Core provides advanced cryptographic utilities for encryption, signing, and hash management.
|
|
664
|
+
|
|
665
|
+
```typescript
|
|
666
|
+
import { ShogunCore } from "shogun-core";
|
|
667
|
+
|
|
668
|
+
const shogun = new ShogunCore({
|
|
669
|
+
/* config */
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
// Cryptographic operations examples
|
|
673
|
+
async function cryptoExamples() {
|
|
674
|
+
// Generate key pair
|
|
675
|
+
const keyPair = await shogun.gun.generateKeyPair();
|
|
676
|
+
|
|
677
|
+
// Encryption
|
|
678
|
+
const encrypted = await shogun.gun.encrypt("Secret message", keyPair.epriv);
|
|
679
|
+
const decrypted = await shogun.gun.decrypt(encrypted, keyPair.epriv);
|
|
680
|
+
|
|
681
|
+
// Signing and verification
|
|
682
|
+
const signed = await shogun.gun.sign({ data: "Authenticated data" }, keyPair);
|
|
683
|
+
const verified = await shogun.gun.verify(signed, keyPair.pub);
|
|
684
|
+
|
|
685
|
+
// Hashing
|
|
686
|
+
const textHash = await shogun.gun.hashText("Text to hash");
|
|
687
|
+
const objHash = await shogun.gun.hashObj({
|
|
688
|
+
complex: "object",
|
|
689
|
+
with: ["arrays"],
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
// Short hashes (useful for identifiers)
|
|
693
|
+
const shortHash = await shogun.gun.getShortHash(
|
|
694
|
+
"user@example.com",
|
|
695
|
+
"salt123"
|
|
696
|
+
);
|
|
697
|
+
|
|
698
|
+
// URL-safe hashes
|
|
699
|
+
const safeHash = shogun.gun.safeHash(textHash);
|
|
700
|
+
const originalHash = shogun.gun.unsafeHash(safeHash);
|
|
701
|
+
|
|
702
|
+
return {
|
|
703
|
+
encrypted,
|
|
704
|
+
decrypted,
|
|
705
|
+
verified,
|
|
706
|
+
textHash,
|
|
707
|
+
objHash,
|
|
708
|
+
shortHash,
|
|
709
|
+
safeHash,
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// End-to-end encryption between users
|
|
714
|
+
async function encryptForUser(receiverPub) {
|
|
715
|
+
const senderPair = shogun.gun.getCurrentUser()?.user._?.sea;
|
|
716
|
+
const receiverKey = { epub: receiverPub };
|
|
717
|
+
|
|
718
|
+
// Encrypt data for a specific recipient
|
|
719
|
+
const message = "This message is only readable by the recipient";
|
|
720
|
+
const encrypted = await shogun.gun.encFor(message, senderPair, receiverKey);
|
|
721
|
+
|
|
722
|
+
return encrypted;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
// Decrypt data from a specific sender
|
|
726
|
+
async function decryptFromUser(senderPub, encryptedData) {
|
|
727
|
+
const receiverPair = shogun.gun.getCurrentUser()?.user._?.sea;
|
|
728
|
+
const senderKey = { epub: senderPub };
|
|
729
|
+
|
|
730
|
+
// Decrypt data coming from a specific sender
|
|
731
|
+
const decrypted = await shogun.gun.decFrom(
|
|
732
|
+
encryptedData,
|
|
733
|
+
senderKey,
|
|
734
|
+
receiverPair
|
|
735
|
+
);
|
|
736
|
+
|
|
737
|
+
return decrypted;
|
|
738
|
+
}
|
|
739
|
+
```
|
|
740
|
+
|
|
359
741
|
## Use Cases
|
|
360
742
|
|
|
361
743
|
Shogun is particularly suitable for:
|
|
@@ -494,6 +876,116 @@ Available categories include:
|
|
|
494
876
|
- `PluginCategory.Identity`: plugins for decentralized identity
|
|
495
877
|
- `PluginCategory.Utility`: plugins for other functionalities
|
|
496
878
|
|
|
879
|
+
## Stealth Transactions
|
|
880
|
+
|
|
881
|
+
Shogun SDK supporta transazioni stealth per maggiore privacy nelle operazioni blockchain. Il plugin Stealth permette di creare indirizzi stealth che nascondono il vero destinatario di una transazione.
|
|
882
|
+
|
|
883
|
+
```typescript
|
|
884
|
+
import { ShogunCore, CorePlugins } from "shogun-core";
|
|
885
|
+
|
|
886
|
+
// Inizializza Shogun con il plugin Stealth abilitato
|
|
887
|
+
const shogun = new ShogunCore({
|
|
888
|
+
// Configurazione...
|
|
889
|
+
stealth: { enabled: true },
|
|
890
|
+
});
|
|
891
|
+
|
|
892
|
+
// Accesso al plugin Stealth
|
|
893
|
+
const stealthPlugin = shogun.getPlugin(CorePlugins.Stealth);
|
|
894
|
+
|
|
895
|
+
// Esempio di funzionalità Stealth
|
|
896
|
+
|
|
897
|
+
// 1. Generazione di un nuovo indirizzo stealth
|
|
898
|
+
async function generateStealthAddress() {
|
|
899
|
+
const stealthResult = await stealthPlugin.generateStealthAddress();
|
|
900
|
+
console.log("Nuovo indirizzo stealth:", stealthResult.stealthAddress);
|
|
901
|
+
console.log("Chiave di scansione:", stealthResult.scanKey);
|
|
902
|
+
return stealthResult;
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
// 2. Invio di una transazione stealth
|
|
906
|
+
async function sendStealthPayment(recipientStealthAddress, amountInEth) {
|
|
907
|
+
try {
|
|
908
|
+
// Ottieni il wallet principale
|
|
909
|
+
const walletPlugin = shogun.getPlugin(CorePlugins.Wallet);
|
|
910
|
+
const senderWallet = walletPlugin.getMainWallet();
|
|
911
|
+
|
|
912
|
+
// Crea e invia la transazione stealth
|
|
913
|
+
const txResult = await stealthPlugin.createStealthTransaction(
|
|
914
|
+
recipientStealthAddress,
|
|
915
|
+
amountInEth,
|
|
916
|
+
{
|
|
917
|
+
gasLimit: 150000,
|
|
918
|
+
maxFeePerGas: "50", // in gwei
|
|
919
|
+
}
|
|
920
|
+
);
|
|
921
|
+
|
|
922
|
+
console.log("Transazione stealth inviata:", txResult);
|
|
923
|
+
console.log("Hash della transazione:", txResult.hash);
|
|
924
|
+
|
|
925
|
+
return txResult;
|
|
926
|
+
} catch (error) {
|
|
927
|
+
console.error("Errore nell'invio della transazione stealth:", error);
|
|
928
|
+
throw error;
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
// 3. Scansione per transazioni stealth in entrata
|
|
933
|
+
async function scanForPayments() {
|
|
934
|
+
try {
|
|
935
|
+
// Scansiona la blockchain per pagamenti stealth ricevuti
|
|
936
|
+
const payments = await stealthPlugin.scanStealthPayments();
|
|
937
|
+
|
|
938
|
+
console.log(`Trovati ${payments.length} pagamenti stealth`);
|
|
939
|
+
payments.forEach((payment, index) => {
|
|
940
|
+
console.log(`Pagamento ${index + 1}:`);
|
|
941
|
+
console.log(`- Indirizzo: ${payment.address}`);
|
|
942
|
+
console.log(`- Valore: ${payment.amount} ETH`);
|
|
943
|
+
console.log(`- Blocco: ${payment.blockNumber}`);
|
|
944
|
+
});
|
|
945
|
+
|
|
946
|
+
return payments;
|
|
947
|
+
} catch (error) {
|
|
948
|
+
console.error("Errore durante la scansione dei pagamenti stealth:", error);
|
|
949
|
+
throw error;
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
// 4. Reclama una transazione stealth ricevuta
|
|
954
|
+
async function claimStealthPayment(paymentInfo) {
|
|
955
|
+
try {
|
|
956
|
+
// Reclama il pagamento stealth spostando i fondi al wallet principale
|
|
957
|
+
const claimResult = await stealthPlugin.claimStealthPayment(
|
|
958
|
+
paymentInfo.address,
|
|
959
|
+
paymentInfo.privateKey
|
|
960
|
+
);
|
|
961
|
+
|
|
962
|
+
console.log("Pagamento stealth reclamato:", claimResult);
|
|
963
|
+
console.log("Hash della transazione di claim:", claimResult.hash);
|
|
964
|
+
|
|
965
|
+
return claimResult;
|
|
966
|
+
} catch (error) {
|
|
967
|
+
console.error("Errore nel reclamo del pagamento stealth:", error);
|
|
968
|
+
throw error;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
```
|
|
972
|
+
|
|
973
|
+
### Vantaggi delle Transazioni Stealth
|
|
974
|
+
|
|
975
|
+
- **Maggiore Privacy**: Gli indirizzi stealth nascondono la vera identità del destinatario
|
|
976
|
+
- **Transazioni Non Collegabili**: Ogni transazione utilizza un indirizzo diverso
|
|
977
|
+
- **Compatibilità**: Funziona con qualsiasi portafoglio Ethereum
|
|
978
|
+
- **Sicurezza**: Solo il destinatario può rilevare e riscattare i pagamenti stealth
|
|
979
|
+
|
|
980
|
+
### Come Funziona
|
|
981
|
+
|
|
982
|
+
1. **Generazione**: Il destinatario genera un indirizzo stealth utilizzando una chiave di scansione
|
|
983
|
+
2. **Pagamento**: Il mittente utilizza l'indirizzo stealth per inviare fondi
|
|
984
|
+
3. **Scansione**: Il destinatario scansiona la blockchain utilizzando la sua chiave di scansione
|
|
985
|
+
4. **Reclamo**: Il destinatario reclama i fondi trasferendoli a un indirizzo di sua scelta
|
|
986
|
+
|
|
987
|
+
Questa implementazione utilizza il protocollo Stealth Address per migliorare la privacy delle transazioni sulla blockchain Ethereum.
|
|
988
|
+
|
|
497
989
|
## Browser Integration
|
|
498
990
|
|
|
499
991
|
Shogun Core can be used directly in modern web browsers. This makes it possible to create decentralized applications that run entirely from the client's browser.
|