shogun-core 3.3.1 → 3.3.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/ship/examples/ephemeral-cli.js +234 -0
- package/dist/ship/examples/identity-cli.js +503 -0
- package/dist/ship/examples/messenger-cli.js +745 -0
- package/dist/ship/examples/stealth-cli.js +433 -0
- package/dist/ship/examples/storage-cli.js +615 -0
- package/dist/ship/examples/vault-cli.js +444 -0
- package/dist/ship/examples/wallet-cli.js +767 -0
- package/dist/ship/implementation/SHIP_00.js +478 -0
- package/dist/ship/implementation/SHIP_01.js +433 -0
- package/dist/ship/implementation/SHIP_02.js +1366 -0
- package/dist/ship/implementation/SHIP_03.js +855 -0
- package/dist/ship/implementation/SHIP_04.js +589 -0
- package/dist/ship/implementation/SHIP_05.js +1064 -0
- package/dist/ship/implementation/SHIP_06.js +350 -0
- package/dist/ship/implementation/SHIP_07.js +635 -0
- package/dist/ship/index.js +17 -0
- package/dist/ship/interfaces/ISHIP_00.js +135 -0
- package/dist/ship/interfaces/ISHIP_01.js +128 -0
- package/dist/ship/interfaces/ISHIP_02.js +57 -0
- package/dist/ship/interfaces/ISHIP_03.js +61 -0
- package/dist/ship/interfaces/ISHIP_04.js +62 -0
- package/dist/ship/interfaces/ISHIP_05.js +59 -0
- package/dist/ship/interfaces/ISHIP_06.js +144 -0
- package/dist/ship/interfaces/ISHIP_07.js +194 -0
- package/dist/types/ship/examples/ephemeral-cli.d.ts +13 -0
- package/dist/types/ship/examples/identity-cli.d.ts +40 -0
- package/dist/types/ship/examples/messenger-cli.d.ts +37 -0
- package/dist/types/ship/examples/stealth-cli.d.ts +31 -0
- package/dist/types/ship/examples/storage-cli.d.ts +48 -0
- package/dist/types/ship/examples/vault-cli.d.ts +13 -0
- package/dist/types/ship/examples/wallet-cli.d.ts +131 -0
- package/dist/types/ship/implementation/SHIP_00.d.ts +113 -0
- package/dist/types/ship/implementation/SHIP_01.d.ts +80 -0
- package/dist/types/ship/implementation/SHIP_02.d.ts +297 -0
- package/dist/types/ship/implementation/SHIP_03.d.ts +127 -0
- package/dist/types/ship/implementation/SHIP_04.d.ts +76 -0
- package/dist/types/ship/implementation/SHIP_05.d.ts +70 -0
- package/dist/types/ship/implementation/SHIP_06.d.ts +66 -0
- package/dist/types/ship/implementation/SHIP_07.d.ts +101 -0
- package/dist/types/ship/index.d.ts +14 -0
- package/dist/types/ship/interfaces/ISHIP_00.d.ts +410 -0
- package/dist/types/ship/interfaces/ISHIP_01.d.ts +343 -0
- package/dist/types/ship/interfaces/ISHIP_02.d.ts +470 -0
- package/dist/types/ship/interfaces/ISHIP_03.d.ts +295 -0
- package/dist/types/ship/interfaces/ISHIP_04.d.ts +245 -0
- package/dist/types/ship/interfaces/ISHIP_05.d.ts +234 -0
- package/dist/types/ship/interfaces/ISHIP_06.d.ts +370 -0
- package/dist/types/ship/interfaces/ISHIP_07.d.ts +522 -0
- package/package.json +1 -1
- /package/dist/{config → src/config}/simplified-config.js +0 -0
- /package/dist/{core.js → src/core.js} +0 -0
- /package/dist/{examples → src/examples}/api-test.js +0 -0
- /package/dist/{examples → src/examples}/simple-api-test.js +0 -0
- /package/dist/{gundb → src/gundb}/api.js +0 -0
- /package/dist/{gundb → src/gundb}/crypto.js +0 -0
- /package/dist/{gundb → src/gundb}/db.js +0 -0
- /package/dist/{gundb → src/gundb}/derive.js +0 -0
- /package/dist/{gundb → src/gundb}/errors.js +0 -0
- /package/dist/{gundb → src/gundb}/index.js +0 -0
- /package/dist/{gundb → src/gundb}/rxjs.js +0 -0
- /package/dist/{gundb → src/gundb}/types.js +0 -0
- /package/dist/{index.js → src/index.js} +0 -0
- /package/dist/{interfaces → src/interfaces}/common.js +0 -0
- /package/dist/{interfaces → src/interfaces}/events.js +0 -0
- /package/dist/{interfaces → src/interfaces}/plugin.js +0 -0
- /package/dist/{interfaces → src/interfaces}/shogun.js +0 -0
- /package/dist/{managers → src/managers}/AuthManager.js +0 -0
- /package/dist/{managers → src/managers}/CoreInitializer.js +0 -0
- /package/dist/{managers → src/managers}/EventManager.js +0 -0
- /package/dist/{managers → src/managers}/PluginManager.js +0 -0
- /package/dist/{migration-test.js → src/migration-test.js} +0 -0
- /package/dist/{plugins → src/plugins}/base.js +0 -0
- /package/dist/{plugins → src/plugins}/index.js +0 -0
- /package/dist/{plugins → src/plugins}/nostr/index.js +0 -0
- /package/dist/{plugins → src/plugins}/nostr/nostrConnector.js +0 -0
- /package/dist/{plugins → src/plugins}/nostr/nostrConnectorPlugin.js +0 -0
- /package/dist/{plugins → src/plugins}/nostr/nostrSigner.js +0 -0
- /package/dist/{plugins → src/plugins}/nostr/types.js +0 -0
- /package/dist/{plugins → src/plugins}/oauth/index.js +0 -0
- /package/dist/{plugins → src/plugins}/oauth/oauthConnector.js +0 -0
- /package/dist/{plugins → src/plugins}/oauth/oauthPlugin.js +0 -0
- /package/dist/{plugins → src/plugins}/oauth/types.js +0 -0
- /package/dist/{plugins → src/plugins}/web3/index.js +0 -0
- /package/dist/{plugins → src/plugins}/web3/types.js +0 -0
- /package/dist/{plugins → src/plugins}/web3/web3Connector.js +0 -0
- /package/dist/{plugins → src/plugins}/web3/web3ConnectorPlugin.js +0 -0
- /package/dist/{plugins → src/plugins}/web3/web3Signer.js +0 -0
- /package/dist/{plugins → src/plugins}/webauthn/index.js +0 -0
- /package/dist/{plugins → src/plugins}/webauthn/types.js +0 -0
- /package/dist/{plugins → src/plugins}/webauthn/webauthn.js +0 -0
- /package/dist/{plugins → src/plugins}/webauthn/webauthnPlugin.js +0 -0
- /package/dist/{plugins → src/plugins}/webauthn/webauthnSigner.js +0 -0
- /package/dist/{storage → src/storage}/storage.js +0 -0
- /package/dist/{types → src/types}/events.js +0 -0
- /package/dist/{types → src/types}/shogun.js +0 -0
- /package/dist/{utils → src/utils}/errorHandler.js +0 -0
- /package/dist/{utils → src/utils}/eventEmitter.js +0 -0
- /package/dist/{utils → src/utils}/validation.js +0 -0
- /package/dist/types/{config → src/config}/simplified-config.d.ts +0 -0
- /package/dist/types/{core.d.ts → src/core.d.ts} +0 -0
- /package/dist/types/{examples → src/examples}/api-test.d.ts +0 -0
- /package/dist/types/{examples → src/examples}/simple-api-test.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/api.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/crypto.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/db.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/derive.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/errors.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/index.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/rxjs.d.ts +0 -0
- /package/dist/types/{gundb → src/gundb}/types.d.ts +0 -0
- /package/dist/types/{index.d.ts → src/index.d.ts} +0 -0
- /package/dist/types/{interfaces → src/interfaces}/common.d.ts +0 -0
- /package/dist/types/{interfaces → src/interfaces}/events.d.ts +0 -0
- /package/dist/types/{interfaces → src/interfaces}/plugin.d.ts +0 -0
- /package/dist/types/{interfaces → src/interfaces}/shogun.d.ts +0 -0
- /package/dist/types/{managers → src/managers}/AuthManager.d.ts +0 -0
- /package/dist/types/{managers → src/managers}/CoreInitializer.d.ts +0 -0
- /package/dist/types/{managers → src/managers}/EventManager.d.ts +0 -0
- /package/dist/types/{managers → src/managers}/PluginManager.d.ts +0 -0
- /package/dist/types/{migration-test.d.ts → src/migration-test.d.ts} +0 -0
- /package/dist/types/{plugins → src/plugins}/base.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/nostrConnector.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/nostrConnectorPlugin.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/nostrSigner.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/nostr/types.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/oauth/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/oauth/oauthConnector.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/oauth/oauthPlugin.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/oauth/types.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/types.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/web3Connector.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/web3ConnectorPlugin.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/web3/web3Signer.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/index.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/types.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/webauthn.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/webauthnPlugin.d.ts +0 -0
- /package/dist/types/{plugins → src/plugins}/webauthn/webauthnSigner.d.ts +0 -0
- /package/dist/types/{storage → src/storage}/storage.d.ts +0 -0
- /package/dist/types/{types → src/types}/events.d.ts +0 -0
- /package/dist/types/{types → src/types}/shogun.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/errorHandler.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/eventEmitter.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/validation.d.ts +0 -0
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shogun Messaging Protocol Interface
|
|
3
|
+
*
|
|
4
|
+
* @title ISHIP_01 - Decentralized Encrypted Messaging
|
|
5
|
+
* @notice Interface for decentralized encrypted messaging on GunDB
|
|
6
|
+
* @dev This interface depends on ISHIP_00 for identity and authentication
|
|
7
|
+
*
|
|
8
|
+
* ## Abstract
|
|
9
|
+
*
|
|
10
|
+
* This standard defines an interface for decentralized messaging that allows:
|
|
11
|
+
* - End-to-end encrypted message sending (ECDH)
|
|
12
|
+
* - Real-time message reception
|
|
13
|
+
* - Decentralized message history
|
|
14
|
+
*
|
|
15
|
+
* ## Dependencies
|
|
16
|
+
*
|
|
17
|
+
* - ISHIP_00: Identity and authentication layer
|
|
18
|
+
* - GunDB: P2P storage
|
|
19
|
+
* - SEA: Cryptography (ECDH + AES-GCM)
|
|
20
|
+
*
|
|
21
|
+
* ## Specification
|
|
22
|
+
*
|
|
23
|
+
* Based on:
|
|
24
|
+
* - SHIP-00 for identity management
|
|
25
|
+
* - GunDB for P2P storage
|
|
26
|
+
* - SEA (Security, Encryption, Authorization) for cryptography
|
|
27
|
+
* - ECDH (Elliptic Curve Diffie-Hellman) for key agreement
|
|
28
|
+
*/
|
|
29
|
+
import type { ISHIP_00 } from "./ISHIP_00";
|
|
30
|
+
/**
|
|
31
|
+
* @notice Decrypted message structure (for UI)
|
|
32
|
+
*/
|
|
33
|
+
export interface DecryptedMessage {
|
|
34
|
+
from: string;
|
|
35
|
+
content: string;
|
|
36
|
+
timestamp: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* @notice Message history entry
|
|
40
|
+
*/
|
|
41
|
+
export interface MessageHistoryEntry {
|
|
42
|
+
from: string;
|
|
43
|
+
to: string;
|
|
44
|
+
content: string;
|
|
45
|
+
timestamp: number;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* @notice Send message result
|
|
49
|
+
*/
|
|
50
|
+
export interface SendMessageResult {
|
|
51
|
+
success: boolean;
|
|
52
|
+
messageId?: string;
|
|
53
|
+
error?: string;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* @notice Token-encrypted message (for channels/groups)
|
|
57
|
+
*/
|
|
58
|
+
export interface TokenMessage {
|
|
59
|
+
content: string;
|
|
60
|
+
from: string;
|
|
61
|
+
channel?: string;
|
|
62
|
+
timestamp: number;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* @title ISHIP_01 - Decentralized Messaging
|
|
66
|
+
* @notice Main interface for the messaging system
|
|
67
|
+
* @dev Depends on ISHIP_00 for all identity operations
|
|
68
|
+
*
|
|
69
|
+
* Constructor pattern:
|
|
70
|
+
* ```typescript
|
|
71
|
+
* class MessagingApp implements ISHIP_01 {
|
|
72
|
+
* constructor(private identity: ISHIP_00) {}
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export interface ISHIP_01 {
|
|
77
|
+
/**
|
|
78
|
+
* @notice Get the identity provider
|
|
79
|
+
* @dev Returns the ISHIP_00 instance used for identity operations
|
|
80
|
+
* @return Identity provider instance
|
|
81
|
+
*/
|
|
82
|
+
getIdentity(): ISHIP_00;
|
|
83
|
+
/**
|
|
84
|
+
* @notice Send encrypted message
|
|
85
|
+
* @dev Uses ECDH to derive shared secret, then AES-GCM for encryption
|
|
86
|
+
* @param recipientUsername Recipient's username
|
|
87
|
+
* @param message Plain text message content
|
|
88
|
+
* @return Result with messageId
|
|
89
|
+
*
|
|
90
|
+
* Prerequisites:
|
|
91
|
+
* - User must be authenticated (via ISHIP_00)
|
|
92
|
+
* - Recipient must have published their public key
|
|
93
|
+
*
|
|
94
|
+
* Flow:
|
|
95
|
+
* 1. Get recipient's epub from identity provider
|
|
96
|
+
* 2. SEA.secret(recipient.epub, sender.pair) → shared_secret
|
|
97
|
+
* 3. SEA.encrypt(message, shared_secret) → encrypted
|
|
98
|
+
* 4. Save encrypted on GunDB
|
|
99
|
+
*/
|
|
100
|
+
sendMessage(recipientUsername: string, message: string): Promise<SendMessageResult>;
|
|
101
|
+
/**
|
|
102
|
+
* @notice Listen for incoming messages in real-time
|
|
103
|
+
* @dev Automatically decrypts received messages
|
|
104
|
+
* @param onMessage Callback called for each message
|
|
105
|
+
*
|
|
106
|
+
* Prerequisites:
|
|
107
|
+
* - User must be authenticated (via ISHIP_00)
|
|
108
|
+
*
|
|
109
|
+
* Decryption flow:
|
|
110
|
+
* 1. Receive encrypted message from GunDB
|
|
111
|
+
* 2. Retrieve sender's epub from identity provider
|
|
112
|
+
* 3. SEA.secret(sender.epub, receiver.pair) → shared_secret
|
|
113
|
+
* 4. SEA.decrypt(encrypted, shared_secret) → message
|
|
114
|
+
* 5. Call callback with decrypted message
|
|
115
|
+
*/
|
|
116
|
+
listenForMessages(onMessage: (message: DecryptedMessage) => void): Promise<void>;
|
|
117
|
+
/**
|
|
118
|
+
* @notice Retrieve message history with a user
|
|
119
|
+
* @dev Decrypts all messages in history
|
|
120
|
+
* @param withUsername Username of the other user
|
|
121
|
+
* @return Array of decrypted messages sorted by timestamp
|
|
122
|
+
*
|
|
123
|
+
* Prerequisites:
|
|
124
|
+
* - User must be authenticated (via ISHIP_00)
|
|
125
|
+
*/
|
|
126
|
+
getMessageHistory(withUsername: string): Promise<MessageHistoryEntry[]>;
|
|
127
|
+
/**
|
|
128
|
+
* @notice Send message encrypted with a shared token/password
|
|
129
|
+
* @dev Uses symmetric encryption with provided token
|
|
130
|
+
* @param token Shared secret/password for encryption
|
|
131
|
+
* @param message Plain text message content
|
|
132
|
+
* @param channel Optional channel name for organization
|
|
133
|
+
* @return Result with messageId
|
|
134
|
+
*
|
|
135
|
+
* Use cases:
|
|
136
|
+
* - Group chats with shared password
|
|
137
|
+
* - Broadcast channels
|
|
138
|
+
* - Private communities
|
|
139
|
+
*
|
|
140
|
+
* Flow:
|
|
141
|
+
* 1. Hash token for key derivation
|
|
142
|
+
* 2. SEA.encrypt(message, hashedToken) → encrypted
|
|
143
|
+
* 3. Save encrypted on GunDB with channel tag
|
|
144
|
+
*/
|
|
145
|
+
sendMessageWithToken(token: string, message: string, channel?: string): Promise<SendMessageResult>;
|
|
146
|
+
/**
|
|
147
|
+
* @notice Listen for token-encrypted messages
|
|
148
|
+
* @dev Automatically decrypts received messages with provided token
|
|
149
|
+
* @param token Shared secret/password for decryption
|
|
150
|
+
* @param onMessage Callback called for each message
|
|
151
|
+
* @param channel Optional channel filter
|
|
152
|
+
*
|
|
153
|
+
* Prerequisites:
|
|
154
|
+
* - User must be authenticated (via ISHIP_00)
|
|
155
|
+
*
|
|
156
|
+
* Decryption flow:
|
|
157
|
+
* 1. Receive encrypted message from GunDB
|
|
158
|
+
* 2. Hash token
|
|
159
|
+
* 3. SEA.decrypt(encrypted, hashedToken) → message
|
|
160
|
+
* 4. Call callback with decrypted message
|
|
161
|
+
*/
|
|
162
|
+
listenForTokenMessages(token: string, onMessage: (message: TokenMessage) => void, channel?: string): Promise<void>;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* @notice Messaging system configuration
|
|
166
|
+
*/
|
|
167
|
+
export interface MessagingConfig {
|
|
168
|
+
/**
|
|
169
|
+
* @notice Identity provider (SHIP-00 instance)
|
|
170
|
+
*/
|
|
171
|
+
identity: ISHIP_00;
|
|
172
|
+
/**
|
|
173
|
+
* @notice Operation timeout (ms)
|
|
174
|
+
*/
|
|
175
|
+
timeout?: number;
|
|
176
|
+
/**
|
|
177
|
+
* @notice Enable debug logging
|
|
178
|
+
*/
|
|
179
|
+
debug?: boolean;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* @notice Cryptographic functions provided by Shogun Core
|
|
183
|
+
* @dev Wrapper for GunDB SEA
|
|
184
|
+
*/
|
|
185
|
+
export interface ISEACrypto {
|
|
186
|
+
/**
|
|
187
|
+
* @notice Encrypt data for a recipient (ECDH)
|
|
188
|
+
* @param data Data to encrypt
|
|
189
|
+
* @param senderPair Sender's key pair
|
|
190
|
+
* @param receiverEpub Recipient's encryption public key
|
|
191
|
+
* @return Encrypted data
|
|
192
|
+
*/
|
|
193
|
+
encFor(data: any, senderPair: {
|
|
194
|
+
pub: string;
|
|
195
|
+
priv: string;
|
|
196
|
+
epub: string;
|
|
197
|
+
epriv: string;
|
|
198
|
+
}, receiverEpub: {
|
|
199
|
+
epub: string;
|
|
200
|
+
}): Promise<string>;
|
|
201
|
+
/**
|
|
202
|
+
* @notice Decrypt data from a sender (ECDH)
|
|
203
|
+
* @param encryptedData Encrypted data
|
|
204
|
+
* @param senderEpub Sender's encryption public key
|
|
205
|
+
* @param receiverPair Recipient's key pair
|
|
206
|
+
* @return Decrypted data
|
|
207
|
+
*/
|
|
208
|
+
decFrom(encryptedData: string, senderEpub: {
|
|
209
|
+
epub: string;
|
|
210
|
+
}, receiverPair: {
|
|
211
|
+
pub: string;
|
|
212
|
+
priv: string;
|
|
213
|
+
epub: string;
|
|
214
|
+
epriv: string;
|
|
215
|
+
}): Promise<any>;
|
|
216
|
+
/**
|
|
217
|
+
* @notice Derive shared secret between two parties (ECDH)
|
|
218
|
+
* @param epub Other party's encryption public key
|
|
219
|
+
* @param pair Own key pair
|
|
220
|
+
* @return Shared secret
|
|
221
|
+
*/
|
|
222
|
+
secret(epub: string, pair: {
|
|
223
|
+
pub: string;
|
|
224
|
+
priv: string;
|
|
225
|
+
epub: string;
|
|
226
|
+
epriv: string;
|
|
227
|
+
}): Promise<string>;
|
|
228
|
+
/**
|
|
229
|
+
* @notice Encrypt with symmetric key
|
|
230
|
+
* @param data Data to encrypt
|
|
231
|
+
* @param key Symmetric key
|
|
232
|
+
* @return Encrypted data
|
|
233
|
+
*/
|
|
234
|
+
encrypt(data: any, key: string): Promise<string>;
|
|
235
|
+
/**
|
|
236
|
+
* @notice Decrypt with symmetric key
|
|
237
|
+
* @param encryptedData Encrypted data
|
|
238
|
+
* @param key Symmetric key
|
|
239
|
+
* @return Decrypted data
|
|
240
|
+
*/
|
|
241
|
+
decrypt(encryptedData: string, key: string): Promise<any>;
|
|
242
|
+
/**
|
|
243
|
+
* @notice Hash text with SHA-256
|
|
244
|
+
* @param text Text to hash
|
|
245
|
+
* @return Hash
|
|
246
|
+
*/
|
|
247
|
+
hashText(text: string): Promise<string>;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Example of how to implement ISHIP_01 with ISHIP_00 dependency
|
|
251
|
+
*
|
|
252
|
+
* ```typescript
|
|
253
|
+
* import { ISHIP_00 } from './ISHIP_00';
|
|
254
|
+
* import { ISHIP_01, DecryptedMessage, SendMessageResult } from './ISHIP_01';
|
|
255
|
+
*
|
|
256
|
+
* class SecureMessagingApp implements ISHIP_01 {
|
|
257
|
+
* constructor(private identity: ISHIP_00) {
|
|
258
|
+
* // Verify identity is authenticated
|
|
259
|
+
* if (!identity.isLoggedIn()) {
|
|
260
|
+
* throw new Error('User must be authenticated');
|
|
261
|
+
* }
|
|
262
|
+
* }
|
|
263
|
+
*
|
|
264
|
+
* getIdentity(): ISHIP_00 {
|
|
265
|
+
* return this.identity;
|
|
266
|
+
* }
|
|
267
|
+
*
|
|
268
|
+
* async sendMessage(recipientUsername: string, message: string): Promise<SendMessageResult> {
|
|
269
|
+
* // 1. Get recipient's public key from identity provider
|
|
270
|
+
* const recipientKey = await this.identity.getPublicKey(recipientUsername);
|
|
271
|
+
* if (!recipientKey) {
|
|
272
|
+
* return { success: false, error: 'Recipient not found' };
|
|
273
|
+
* }
|
|
274
|
+
*
|
|
275
|
+
* // 2. Get sender's key pair from identity provider
|
|
276
|
+
* const senderPair = this.identity.getKeyPair();
|
|
277
|
+
* if (!senderPair) {
|
|
278
|
+
* return { success: false, error: 'Not authenticated' };
|
|
279
|
+
* }
|
|
280
|
+
*
|
|
281
|
+
* // 3. Encrypt with ECDH
|
|
282
|
+
* const encrypted = await crypto.encFor(
|
|
283
|
+
* message,
|
|
284
|
+
* senderPair,
|
|
285
|
+
* { epub: recipientKey.epub }
|
|
286
|
+
* );
|
|
287
|
+
*
|
|
288
|
+
* // 4. Save to GunDB
|
|
289
|
+
* const messageId = generateId();
|
|
290
|
+
* await gun.get('messages').get(messageId).put({
|
|
291
|
+
* content: encrypted,
|
|
292
|
+
* from: senderPair.pub,
|
|
293
|
+
* to: recipientUsername,
|
|
294
|
+
* timestamp: Date.now().toString()
|
|
295
|
+
* });
|
|
296
|
+
*
|
|
297
|
+
* return { success: true, messageId };
|
|
298
|
+
* }
|
|
299
|
+
*
|
|
300
|
+
* async listenForMessages(onMessage: (message: DecryptedMessage) => void): Promise<void> {
|
|
301
|
+
* const currentUser = this.identity.getCurrentUser();
|
|
302
|
+
* if (!currentUser) {
|
|
303
|
+
* throw new Error('Not authenticated');
|
|
304
|
+
* }
|
|
305
|
+
*
|
|
306
|
+
* gun.get('messages').map().on(async (data, key) => {
|
|
307
|
+
* if (data && data.to === currentUser.alias) {
|
|
308
|
+
* // Decrypt message
|
|
309
|
+
* const senderKey = await this.identity.getPublicKey(data.from);
|
|
310
|
+
* const receiverPair = this.identity.getKeyPair();
|
|
311
|
+
*
|
|
312
|
+
* if (senderKey && receiverPair) {
|
|
313
|
+
* const decrypted = await crypto.decFrom(
|
|
314
|
+
* data.content,
|
|
315
|
+
* { epub: senderKey.epub },
|
|
316
|
+
* receiverPair
|
|
317
|
+
* );
|
|
318
|
+
*
|
|
319
|
+
* onMessage({
|
|
320
|
+
* from: data.from,
|
|
321
|
+
* content: decrypted,
|
|
322
|
+
* timestamp: parseInt(data.timestamp)
|
|
323
|
+
* });
|
|
324
|
+
* }
|
|
325
|
+
* }
|
|
326
|
+
* });
|
|
327
|
+
* }
|
|
328
|
+
*
|
|
329
|
+
* async getMessageHistory(withUsername: string): Promise<MessageHistoryEntry[]> {
|
|
330
|
+
* // Implementation here
|
|
331
|
+
* return [];
|
|
332
|
+
* }
|
|
333
|
+
* }
|
|
334
|
+
*
|
|
335
|
+
* // Usage
|
|
336
|
+
* const identity = new SHIP_00(config);
|
|
337
|
+
* await identity.login('alice', 'password123');
|
|
338
|
+
* await identity.publishPublicKey();
|
|
339
|
+
*
|
|
340
|
+
* const messaging = new SecureMessagingApp(identity);
|
|
341
|
+
* await messaging.sendMessage('bob', 'Hello Bob!');
|
|
342
|
+
* ```
|
|
343
|
+
*/
|