shogun-core 3.2.2 → 3.3.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.
Files changed (32) hide show
  1. package/README.md +12 -0
  2. package/dist/browser/shogun-core.js +108133 -43791
  3. package/dist/browser/shogun-core.js.map +1 -1
  4. package/dist/ship/examples/messenger-cli.js +173 -60
  5. package/dist/ship/examples/wallet-cli.js +767 -0
  6. package/dist/ship/implementation/SHIP_00.js +478 -0
  7. package/dist/ship/implementation/SHIP_01.js +300 -695
  8. package/dist/ship/implementation/SHIP_02.js +1366 -0
  9. package/dist/ship/implementation/SHIP_03.js +855 -0
  10. package/dist/ship/interfaces/ISHIP_00.js +135 -0
  11. package/dist/ship/interfaces/ISHIP_01.js +81 -24
  12. package/dist/ship/interfaces/ISHIP_02.js +57 -0
  13. package/dist/ship/interfaces/ISHIP_03.js +61 -0
  14. package/dist/src/gundb/db.js +55 -11
  15. package/dist/src/index.js +10 -2
  16. package/dist/src/managers/CoreInitializer.js +41 -13
  17. package/dist/src/storage/storage.js +22 -9
  18. package/dist/types/ship/examples/messenger-cli.d.ts +7 -1
  19. package/dist/types/ship/examples/wallet-cli.d.ts +131 -0
  20. package/dist/types/ship/implementation/SHIP_00.d.ts +113 -0
  21. package/dist/types/ship/implementation/SHIP_01.d.ts +47 -76
  22. package/dist/types/ship/implementation/SHIP_02.d.ts +297 -0
  23. package/dist/types/ship/implementation/SHIP_03.d.ts +127 -0
  24. package/dist/types/ship/interfaces/ISHIP_00.d.ts +410 -0
  25. package/dist/types/ship/interfaces/ISHIP_01.d.ts +157 -119
  26. package/dist/types/ship/interfaces/ISHIP_02.d.ts +470 -0
  27. package/dist/types/ship/interfaces/ISHIP_03.d.ts +295 -0
  28. package/dist/types/src/gundb/db.d.ts +10 -3
  29. package/dist/types/src/index.d.ts +7 -0
  30. package/dist/types/src/interfaces/shogun.d.ts +2 -0
  31. package/dist/types/src/storage/storage.d.ts +2 -1
  32. package/package.json +23 -9
@@ -10,28 +10,37 @@ class ShogunStorage {
10
10
  /**
11
11
  * Initializes storage and loads any existing keypair from localStorage if available
12
12
  */
13
- constructor() {
13
+ constructor(silent = false) {
14
14
  this.store = new Map();
15
+ this.silent = silent;
15
16
  this.isTestMode = process.env.NODE_ENV === "test";
16
17
  this.useLocalStorage = false;
17
- // Debug: log the environment
18
- console.log("ShogunStorage: NODE_ENV =", process.env.NODE_ENV);
19
- console.log("ShogunStorage: isTestMode =", this.isTestMode);
18
+ // Debug: log the environment (only if not silent)
19
+ if (!this.silent) {
20
+ console.log("ShogunStorage: NODE_ENV =", process.env.NODE_ENV);
21
+ console.log("ShogunStorage: isTestMode =", this.isTestMode);
22
+ }
20
23
  // In test mode, don't use localStorage to avoid test pollution
21
24
  if (this.isTestMode) {
22
25
  this.useLocalStorage = false;
23
- console.log("ShogunStorage: Test mode detected, localStorage disabled");
26
+ if (!this.silent) {
27
+ console.log("ShogunStorage: Test mode detected, localStorage disabled");
28
+ }
24
29
  return;
25
30
  }
26
31
  if (typeof localStorage !== "undefined") {
27
- console.log("ShogunStorage: localStorage is defined");
32
+ if (!this.silent) {
33
+ console.log("ShogunStorage: localStorage is defined");
34
+ }
28
35
  try {
29
36
  // Probe localStorage without polluting expectations in tests
30
37
  const testKey = "_shogun_test";
31
38
  localStorage.setItem(testKey, testKey);
32
39
  localStorage.removeItem(testKey);
33
40
  this.useLocalStorage = true;
34
- console.log("ShogunStorage: localStorage enabled");
41
+ if (!this.silent) {
42
+ console.log("ShogunStorage: localStorage enabled");
43
+ }
35
44
  const storedPair = localStorage.getItem("shogun_keypair");
36
45
  if (storedPair) {
37
46
  this.store.set("keypair", JSON.parse(storedPair));
@@ -40,11 +49,15 @@ class ShogunStorage {
40
49
  catch (error) {
41
50
  this.useLocalStorage = false;
42
51
  // Silence logs in tests; tests expect no console.error during constructor
43
- console.log("ShogunStorage: localStorage error:", error.message);
52
+ if (!this.silent) {
53
+ console.log("ShogunStorage: localStorage error:", error.message);
54
+ }
44
55
  }
45
56
  }
46
57
  else {
47
- console.log("ShogunStorage: localStorage is undefined");
58
+ if (!this.silent) {
59
+ console.log("ShogunStorage: localStorage is undefined");
60
+ }
48
61
  }
49
62
  }
50
63
  /**
@@ -6,23 +6,29 @@
6
6
  * Simple and functional CLI interface
7
7
  */
8
8
  export declare class MessengerCLI {
9
- private app;
9
+ private identity;
10
+ private messaging;
10
11
  private rl;
11
12
  private currentUser;
12
13
  private recipient;
13
14
  private derivedAddress;
14
15
  private isAuthenticated;
16
+ private channelToken;
17
+ private currentChannel;
15
18
  constructor();
16
19
  private setupHandlers;
17
20
  private withTimeout;
18
21
  login(username: string, password: string): Promise<boolean>;
19
22
  private sendMessage;
20
23
  private onMessageReceived;
24
+ private onChannelMessageReceived;
21
25
  private exportKeyPair;
22
26
  private importKeyPair;
23
27
  private loginWithPair;
24
28
  private wipeAllMessages;
25
29
  private handleCommand;
30
+ private joinChannel;
31
+ private leaveChannel;
26
32
  private showHelp;
27
33
  private showStatus;
28
34
  private showHeader;
@@ -0,0 +1,131 @@
1
+ /**
2
+ * SHIP-02 Example: Ethereum Wallet CLI
3
+ *
4
+ * Interactive CLI demonstrating SHIP-02 HD wallet derivation
5
+ * Built on top of SHIP-00 identity
6
+ *
7
+ * Usage:
8
+ * yarn wallet <username> <password>
9
+ *
10
+ * Features:
11
+ * - Derive Ethereum addresses from identity (no mnemonics!)
12
+ * - BIP-44 HD wallet support
13
+ * - Sign transactions and messages
14
+ * - Export/import address book
15
+ * - Gun persistence
16
+ *
17
+ * Note: For stealth addresses, use `yarn stealth` (SHIP-03)
18
+ */
19
+ declare class WalletCLI {
20
+ private identity;
21
+ private wallet;
22
+ private rl;
23
+ private username;
24
+ private running;
25
+ constructor(username: string, password: string);
26
+ /**
27
+ * Start the CLI
28
+ */
29
+ start(password: string): Promise<void>;
30
+ /**
31
+ * Authenticate user with SHIP-00
32
+ */
33
+ private authenticate;
34
+ /**
35
+ * Initialize SHIP-02 wallet
36
+ */
37
+ private initializeWallet;
38
+ /**
39
+ * Main menu loop
40
+ */
41
+ private mainMenu;
42
+ /**
43
+ * View all addresses
44
+ */
45
+ private viewAddresses;
46
+ /**
47
+ * Derive new address with custom index
48
+ */
49
+ private deriveNewAddress;
50
+ /**
51
+ * Derive BIP-44 address with custom parameters
52
+ */
53
+ private deriveBIP44Address;
54
+ /**
55
+ * Derive multiple addresses at once
56
+ */
57
+ private deriveMultipleAddresses;
58
+ /**
59
+ * Import address book
60
+ */
61
+ private importAddressBook;
62
+ /**
63
+ * Export encrypted wallet data
64
+ */
65
+ private exportWalletData;
66
+ /**
67
+ * Sync with Gun database
68
+ */
69
+ private syncWithGun;
70
+ /**
71
+ * View/Export mnemonic
72
+ */
73
+ private viewExportMnemonic;
74
+ /**
75
+ * Verify addresses match MetaMask
76
+ */
77
+ private verifyMetaMaskAddresses;
78
+ /**
79
+ * Sign message
80
+ */
81
+ private signMessage;
82
+ /**
83
+ * Sign transaction
84
+ */
85
+ private signTransaction;
86
+ /**
87
+ * Export address book
88
+ */
89
+ private exportAddressBook;
90
+ /**
91
+ * View master public key
92
+ */
93
+ private viewMasterPublicKey;
94
+ /**
95
+ * Create wallet using frontend API
96
+ */
97
+ private createWalletFrontendAPI;
98
+ /**
99
+ * Load all wallets using frontend API
100
+ */
101
+ private loadAllWallets;
102
+ /**
103
+ * View main wallet (derived from Gun keys)
104
+ */
105
+ private viewMainWallet;
106
+ /**
107
+ * Export all user data (complete backup)
108
+ */
109
+ private exportAllUserData;
110
+ /**
111
+ * Set RPC provider URL
112
+ */
113
+ private setRpcProvider;
114
+ /**
115
+ * Send transaction via RPC
116
+ */
117
+ private sendTransactionRPC;
118
+ /**
119
+ * Print header
120
+ */
121
+ private printHeader;
122
+ /**
123
+ * Prompt user for input
124
+ */
125
+ private prompt;
126
+ /**
127
+ * Cleanup and exit
128
+ */
129
+ private cleanup;
130
+ }
131
+ export { WalletCLI };
@@ -0,0 +1,113 @@
1
+ /**
2
+ * SHIP-00: Decentralized Identity & Authentication Implementation
3
+ *
4
+ * Foundation layer for the Shogun ecosystem.
5
+ * Provides identity and authentication services for all other SHIPs.
6
+ *
7
+ * Based on:
8
+ * - Shogun Core API (see ../API.md)
9
+ * - GunDB for P2P identity storage
10
+ * - SEA for cryptographic operations
11
+ * - shogun-derive for address derivation
12
+ *
13
+ * Features:
14
+ * ✅ Username/password authentication
15
+ * ✅ SEA key pair management
16
+ * ✅ Public key publication and discovery
17
+ * ✅ User registry and lookup
18
+ * ✅ Blockchain address derivation
19
+ * ✅ Multi-device support (export/import)
20
+ */
21
+ import type { ISHIP_00, SignupResult, AuthResult, OperationResult, SEAPair, UserIdentity, UserData, PublicKeyData } from "../interfaces/ISHIP_00";
22
+ import { ShogunCoreConfig } from "../../src/interfaces/shogun";
23
+ /**
24
+ * SHIP-00 Reference Implementation
25
+ *
26
+ * Uses Shogun Core as the underlying implementation.
27
+ * This class wraps Shogun Core APIs to provide the ISHIP_00 standard interface.
28
+ */
29
+ declare class SHIP_00 implements ISHIP_00 {
30
+ private shogun;
31
+ static readonly NODES: {
32
+ readonly USERS: "users";
33
+ readonly PUBLIC_KEYS: "publicKeys";
34
+ readonly REGISTRY: "registry";
35
+ };
36
+ constructor(shogunConfig: ShogunCoreConfig);
37
+ /**
38
+ * Register new user
39
+ * Uses Shogun Core signUp method (see API.md)
40
+ */
41
+ signup(username: string, password: string): Promise<SignupResult>;
42
+ /**
43
+ * Login with username and password
44
+ * Uses Shogun Core login method (see API.md)
45
+ */
46
+ login(username: string, password: string): Promise<AuthResult>;
47
+ /**
48
+ * Login with SEA Key Pair
49
+ * Uses Shogun Core loginWithPair method (see API.md)
50
+ */
51
+ loginWithPair(seaPair: SEAPair): Promise<AuthResult>;
52
+ /**
53
+ * Logout
54
+ * Uses Shogun Core logout method (see API.md line 325)
55
+ */
56
+ logout(): void;
57
+ /**
58
+ * Check if user is logged in
59
+ * Uses Shogun Core isLoggedIn method (see API.md line 326)
60
+ */
61
+ isLoggedIn(): boolean;
62
+ /**
63
+ * Get underlying ShogunCore instance
64
+ * Provides access to Gun, SEA, and crypto utilities
65
+ */
66
+ getShogun(): any;
67
+ /**
68
+ * Publish public key on GunDB
69
+ * Makes user's public key discoverable by others
70
+ */
71
+ publishPublicKey(): Promise<OperationResult>;
72
+ /**
73
+ * Export current user's SEA key pair
74
+ * For backup and multi-device usage
75
+ * Uses Shogun Core exportPair method (see API.md line 354)
76
+ */
77
+ exportKeyPair(): SEAPair | null;
78
+ /**
79
+ * Get current user's key pair
80
+ * Alias for exportKeyPair()
81
+ */
82
+ getKeyPair(): SEAPair | null;
83
+ /**
84
+ * Get user information by username
85
+ * Uses Shogun Core getUserByAlias method (see API.md line 1141-1148)
86
+ */
87
+ getUserByAlias(username: string): Promise<UserData | null>;
88
+ /**
89
+ * Get user information by public key
90
+ * Uses Shogun Core getUserDataByPub method (see API.md line 1150-1152)
91
+ */
92
+ getUserByPub(userPub: string): Promise<UserData | null>;
93
+ /**
94
+ * Check if user exists
95
+ * Uses SimpleGunAPI userExists method (see API.md line 188)
96
+ */
97
+ userExists(username: string): Promise<boolean>;
98
+ /**
99
+ * Get public key by username
100
+ */
101
+ getPublicKey(username: string): Promise<PublicKeyData | null>;
102
+ /**
103
+ * Get current authenticated user info
104
+ * Uses Shogun Core getCurrentUser method (see API.md line 429)
105
+ */
106
+ getCurrentUser(): UserIdentity | null;
107
+ /**
108
+ * Derive Ethereum address from SEA keypair
109
+ * Uses shogun-derive package for deterministic derivation
110
+ */
111
+ deriveEthereumAddress(publicKey?: string): Promise<string>;
112
+ }
113
+ export { SHIP_00 };
@@ -1,109 +1,80 @@
1
1
  /**
2
- * Esempio Pratico: Messaggistica Decentralizzata con Shogun Core
2
+ * SHIP-01: Decentralized Encrypted Messaging Implementation
3
3
  *
4
- * Questo esempio mostra come creare un sistema di messaggistica sicuro usando:
5
- * - Shogun Core per autenticazione (username/password)
6
- * - GunDB per storage decentralizzato P2P
7
- * - SEA (Security, Encryption, Authorization) per crittografia
4
+ * Messaggistica decentralizzata E2E che dipende da SHIP-00 per l'identità.
8
5
  *
9
- * Vantaggi:
10
- * Completamente decentralizzato (no server centrale)
11
- * Zero costi (no blockchain, no gas fees)
12
- * Real-time messaging
13
- * ✅ Offline-first
14
- * ✅ End-to-end encryption
6
+ * Dipendenze:
7
+ * - SHIP-00 (Identity & Authentication) - per gestione utenti e chiavi
8
+ * - GunDB - per storage decentralizzato P2P
9
+ * - SEA - per crittografia ECDH + AES-GCM
15
10
  *
16
- * Note sull'ERC7627:
17
- * L'EIP era un template concettuale. Questo esempio implementa
18
- * solo la parte GunDB/Shogun Core senza interazione blockchain.
19
- * La funzione deriveEthereumAddress() rimane come utility per derivare
20
- * un address Ethereum dalla chiave GunDB se necessario in futuro.
11
+ * Vantaggi dell'architettura modulare:
12
+ * Separazione delle responsabilità (Identity vs Messaging)
13
+ * Riusabilità di SHIP-00 in altre applicazioni
14
+ * Testing più semplice e isolato
15
+ * Manutenibilità migliorata
21
16
  */
22
- import type { ISHIP_01, SignupResult, AuthResult, SendMessageResult, DecryptedMessage, OperationResult } from "../interfaces/ISHIP_01";
23
- import { ShogunCoreConfig } from "../../src/interfaces/shogun";
24
- import { ISEAPair } from "../../src/types/shogun";
17
+ import type { ISHIP_00 } from "../interfaces/ISHIP_00";
18
+ import type { ISHIP_01, SendMessageResult, DecryptedMessage, MessageHistoryEntry } from "../interfaces/ISHIP_01";
25
19
  /**
26
- * Classe per messaggistica sicura con Shogun Core
27
- * Implementa l'interfaccia ISHIP_01
28
- * Usa solo GunDB per storage decentralizzato (no blockchain)
20
+ * SHIP-01 Reference Implementation
21
+ *
22
+ * Questa implementazione dipende da ISHIP_00 per tutte le operazioni di identità.
23
+ * Si concentra esclusivamente sulla logica di messaggistica.
29
24
  */
30
25
  declare class SHIP_01 implements ISHIP_01 {
31
- private shogun;
32
- private static readonly NODES;
33
- constructor(shogunConfig: ShogunCoreConfig);
34
- /**
35
- * Registra un nuovo utente
36
- */
37
- signup(username: string, password: string): Promise<SignupResult>;
38
- /**
39
- * Login con username e password
40
- */
41
- login(username: string, password: string): Promise<AuthResult>;
42
- /**
43
- * Login con SEA Key Pair
44
- *
45
- * Autenticazione diretta usando un key pair esportato.
46
- * Utile per:
47
- * - Recupero account senza password
48
- * - Portabilità tra dispositivi
49
- * - Backup dell'identità
50
- */
51
- loginWithPair(seaPair: ISEAPair): Promise<AuthResult>;
26
+ private identity;
27
+ static readonly NODES: {
28
+ readonly MESSAGES: "messages";
29
+ readonly TOKEN_MESSAGES: "token_messages";
30
+ };
52
31
  /**
53
- * Logout
32
+ * Constructor
33
+ * @param identity ISHIP_00 instance for identity operations
54
34
  */
55
- logout(): void;
35
+ constructor(identity: ISHIP_00);
56
36
  /**
57
- * Verifica se l'utente è autenticato
37
+ * Get identity provider
58
38
  */
59
- isLoggedIn(): boolean;
39
+ getIdentity(): ISHIP_00;
60
40
  /**
61
- * Salva la chiave pubblica dell'utente su GunDB
62
- * Questo permette ad altri di trovare la tua chiave per criptare messaggi
63
- */
64
- publishPublicKey(): Promise<OperationResult>;
65
- /**
66
- * Invia un messaggio crittografato a un altro utente
41
+ * Send encrypted message to a user
42
+ * Uses identity provider (SHIP-00) to get keys
67
43
  */
68
44
  sendMessage(recipientUsername: string, message: string): Promise<SendMessageResult>;
69
45
  /**
70
- * Ottiene la chiave pubblica di un utente da GunDB
46
+ * Listen for incoming encrypted messages
47
+ * Uses identity provider (SHIP-00) to decrypt messages
71
48
  */
72
- private getRecipientPublicKey;
49
+ listenForMessages(onMessage: (message: DecryptedMessage) => void): Promise<void>;
73
50
  /**
74
- * Ascolta messaggi crittografati in arrivo su GunDB
51
+ * Get message history with a user
52
+ * Uses identity provider (SHIP-00) to decrypt messages
75
53
  */
76
- listenForMessages(onMessage: (message: DecryptedMessage) => void): Promise<void>;
54
+ getMessageHistory(withUsername: string): Promise<MessageHistoryEntry[]>;
77
55
  /**
78
- * Decripta un messaggio usando SEA.secret + SEA.decrypt (ECDH)
56
+ * Decrypt a message using ECDH
57
+ * Uses identity provider (SHIP-00) to get keys
79
58
  */
80
59
  private decryptMessage;
81
60
  /**
82
- * Ottiene la chiave pubblica di un utente dalla sua pub key
61
+ * Get public key by pub key
62
+ * Uses identity provider internally
83
63
  */
84
64
  private getPublicKeyByPub;
85
65
  /**
86
- * Recupera lo storico dei messaggi crittografati con un utente
66
+ * Generate unique message ID
87
67
  */
88
- getMessageHistory(withUsername: string): Promise<Array<{
89
- from: string;
90
- to: string;
91
- content: string;
92
- timestamp: number;
93
- }>>;
68
+ private generateMessageId;
94
69
  /**
95
- * Converte la chiave pubblica di GunDB in address Ethereum
96
- * Usa la chiave privata SEA come seed per derivazione deterministica
97
- *
98
- * Questo garantisce che:
99
- * - Stesso SEA pair → stesso address Ethereum
100
- * - Derivazione cryptografica sicura
101
- * - Identità unificata tra GunDB e blockchain
70
+ * Send message encrypted with shared token/password
71
+ * Useful for group chats, channels, broadcast messages
102
72
  */
103
- deriveEthereumAddress(publicKey?: string): Promise<string>;
73
+ sendMessageWithToken(token: string, message: string, channel?: string): Promise<SendMessageResult>;
104
74
  /**
105
- * Genera ID messaggio univoco
75
+ * Listen for token-encrypted messages
76
+ * Automatically decrypts with provided token
106
77
  */
107
- private generateMessageId;
78
+ listenForTokenMessages(token: string, onMessage: (message: any) => void, channel?: string): Promise<void>;
108
79
  }
109
80
  export { SHIP_01 };