@towns-labs/encryption 2.0.1

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 (98) hide show
  1. package/README.md +3 -0
  2. package/dist/CryptoStoreInMemory.d.ts +42 -0
  3. package/dist/CryptoStoreInMemory.d.ts.map +1 -0
  4. package/dist/CryptoStoreInMemory.js +172 -0
  5. package/dist/CryptoStoreInMemory.js.map +1 -0
  6. package/dist/CryptoStoreIndexedDb.d.ts +55 -0
  7. package/dist/CryptoStoreIndexedDb.d.ts.map +1 -0
  8. package/dist/CryptoStoreIndexedDb.js +139 -0
  9. package/dist/CryptoStoreIndexedDb.js.map +1 -0
  10. package/dist/base.d.ts +69 -0
  11. package/dist/base.d.ts.map +1 -0
  12. package/dist/base.js +44 -0
  13. package/dist/base.js.map +1 -0
  14. package/dist/cryptoAesGcm.d.ts +9 -0
  15. package/dist/cryptoAesGcm.d.ts.map +1 -0
  16. package/dist/cryptoAesGcm.js +30 -0
  17. package/dist/cryptoAesGcm.js.map +1 -0
  18. package/dist/cryptoStore.d.ts +34 -0
  19. package/dist/cryptoStore.d.ts.map +1 -0
  20. package/dist/cryptoStore.js +17 -0
  21. package/dist/cryptoStore.js.map +1 -0
  22. package/dist/derivedEncryption.d.ts +2 -0
  23. package/dist/derivedEncryption.d.ts.map +1 -0
  24. package/dist/derivedEncryption.js +2 -0
  25. package/dist/derivedEncryption.js.map +1 -0
  26. package/dist/encryptionDelegate.d.ts +16 -0
  27. package/dist/encryptionDelegate.d.ts.map +1 -0
  28. package/dist/encryptionDelegate.js +64 -0
  29. package/dist/encryptionDelegate.js.map +1 -0
  30. package/dist/encryptionDevice.d.ts +264 -0
  31. package/dist/encryptionDevice.d.ts.map +1 -0
  32. package/dist/encryptionDevice.js +745 -0
  33. package/dist/encryptionDevice.js.map +1 -0
  34. package/dist/encryptionTypes.d.ts +21 -0
  35. package/dist/encryptionTypes.d.ts.map +1 -0
  36. package/dist/encryptionTypes.js +2 -0
  37. package/dist/encryptionTypes.js.map +1 -0
  38. package/dist/groupDecryption.d.ts +34 -0
  39. package/dist/groupDecryption.d.ts.map +1 -0
  40. package/dist/groupDecryption.js +84 -0
  41. package/dist/groupDecryption.js.map +1 -0
  42. package/dist/groupEncryption.d.ts +35 -0
  43. package/dist/groupEncryption.d.ts.map +1 -0
  44. package/dist/groupEncryption.js +99 -0
  45. package/dist/groupEncryption.js.map +1 -0
  46. package/dist/groupEncryptionCrypto.d.ts +125 -0
  47. package/dist/groupEncryptionCrypto.d.ts.map +1 -0
  48. package/dist/groupEncryptionCrypto.js +268 -0
  49. package/dist/groupEncryptionCrypto.js.map +1 -0
  50. package/dist/hybridGroupDecryption.d.ts +33 -0
  51. package/dist/hybridGroupDecryption.d.ts.map +1 -0
  52. package/dist/hybridGroupDecryption.js +84 -0
  53. package/dist/hybridGroupDecryption.js.map +1 -0
  54. package/dist/hybridGroupEncryption.d.ts +27 -0
  55. package/dist/hybridGroupEncryption.d.ts.map +1 -0
  56. package/dist/hybridGroupEncryption.js +101 -0
  57. package/dist/hybridGroupEncryption.js.map +1 -0
  58. package/dist/index.d.ts +20 -0
  59. package/dist/index.d.ts.map +1 -0
  60. package/dist/index.js +20 -0
  61. package/dist/index.js.map +1 -0
  62. package/dist/olmLib.d.ts +35 -0
  63. package/dist/olmLib.d.ts.map +1 -0
  64. package/dist/olmLib.js +37 -0
  65. package/dist/olmLib.js.map +1 -0
  66. package/dist/storeTypes.d.ts +27 -0
  67. package/dist/storeTypes.d.ts.map +1 -0
  68. package/dist/storeTypes.js +2 -0
  69. package/dist/storeTypes.js.map +1 -0
  70. package/dist/tests/cryptoAesGcm.test.d.ts +2 -0
  71. package/dist/tests/cryptoAesGcm.test.d.ts.map +1 -0
  72. package/dist/tests/cryptoAesGcm.test.js +71 -0
  73. package/dist/tests/cryptoAesGcm.test.js.map +1 -0
  74. package/dist/tests/cryptoStore.test.d.ts +5 -0
  75. package/dist/tests/cryptoStore.test.d.ts.map +1 -0
  76. package/dist/tests/cryptoStore.test.js +114 -0
  77. package/dist/tests/cryptoStore.test.js.map +1 -0
  78. package/dist/tests/encryption-protocol.test.d.ts +2 -0
  79. package/dist/tests/encryption-protocol.test.d.ts.map +1 -0
  80. package/dist/tests/encryption-protocol.test.js +150 -0
  81. package/dist/tests/encryption-protocol.test.js.map +1 -0
  82. package/dist/tests/encryptionDelegate.test.d.ts +2 -0
  83. package/dist/tests/encryptionDelegate.test.d.ts.map +1 -0
  84. package/dist/tests/encryptionDelegate.test.js +78 -0
  85. package/dist/tests/encryptionDelegate.test.js.map +1 -0
  86. package/dist/tests/group-encryption-protocol.test.d.ts +2 -0
  87. package/dist/tests/group-encryption-protocol.test.d.ts.map +1 -0
  88. package/dist/tests/group-encryption-protocol.test.js +103 -0
  89. package/dist/tests/group-encryption-protocol.test.js.map +1 -0
  90. package/dist/tests/group-encryptionDelegate.test.d.ts +2 -0
  91. package/dist/tests/group-encryptionDelegate.test.d.ts.map +1 -0
  92. package/dist/tests/group-encryptionDelegate.test.js +23 -0
  93. package/dist/tests/group-encryptionDelegate.test.js.map +1 -0
  94. package/dist/tests/pk.test.d.ts +2 -0
  95. package/dist/tests/pk.test.d.ts.map +1 -0
  96. package/dist/tests/pk.test.js +103 -0
  97. package/dist/tests/pk.test.js.map +1 -0
  98. package/package.json +51 -0
@@ -0,0 +1,34 @@
1
+ import { ExtendedInboundGroupSessionData, GroupSessionRecord, HybridGroupSessionRecord } from './storeTypes';
2
+ import { InboundGroupSessionData } from './encryptionDevice';
3
+ import { UserDevice } from './olmLib';
4
+ export declare const DEFAULT_USER_DEVICE_EXPIRATION_TIME_MS: number;
5
+ export declare function createCryptoStore(databaseName: string, userId: string, maxEntries?: number): CryptoStore;
6
+ export interface CryptoStore {
7
+ userId: string;
8
+ initialize(): Promise<void>;
9
+ deleteAllData(): Promise<void>;
10
+ deleteInboundGroupSessions(streamId: string, sessionId: string): Promise<void>;
11
+ deleteOutboundGrounpSessions(streamId: string): Promise<void>;
12
+ deleteAccount(userId: string): Promise<void>;
13
+ getAccount(): Promise<string>;
14
+ storeAccount(accountPickle: string): Promise<void>;
15
+ storeEndToEndOutboundGroupSession(sessionId: string, sessionData: string, streamId: string): Promise<void>;
16
+ getEndToEndOutboundGroupSession(streamId: string): Promise<string>;
17
+ getAllEndToEndOutboundGroupSessions(): Promise<GroupSessionRecord[]>;
18
+ getEndToEndInboundGroupSession(streamId: string, sessionId: string): Promise<InboundGroupSessionData | undefined>;
19
+ getHybridGroupSession(streamId: string, sessionId: string): Promise<HybridGroupSessionRecord | undefined>;
20
+ getHybridGroupSessionsForStream(streamId: string): Promise<HybridGroupSessionRecord[]>;
21
+ getAllEndToEndInboundGroupSessions(): Promise<ExtendedInboundGroupSessionData[]>;
22
+ getAllHybridGroupSessions(): Promise<HybridGroupSessionRecord[]>;
23
+ deleteHybridGroupSessions(streamId: string): Promise<void>;
24
+ storeEndToEndInboundGroupSession(streamId: string, sessionId: string, sessionData: InboundGroupSessionData): Promise<void>;
25
+ storeHybridGroupSession(sessionData: HybridGroupSessionRecord): Promise<void>;
26
+ getInboundGroupSessionIds(streamId: string): Promise<string[]>;
27
+ getHybridGroupSessionIds(streamId: string): Promise<string[]>;
28
+ withAccountTx<T>(fn: () => Promise<T>): Promise<T>;
29
+ withGroupSessions<T>(fn: () => Promise<T>): Promise<T>;
30
+ deviceRecordCount(): Promise<number>;
31
+ saveUserDevices(userId: string, devices: UserDevice[], expirationMs?: number): Promise<void>;
32
+ getUserDevices(userId: string): Promise<UserDevice[]>;
33
+ }
34
+ //# sourceMappingURL=cryptoStore.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cryptoStore.d.ts","sourceRoot":"","sources":["../src/cryptoStore.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,+BAA+B,EAC/B,kBAAkB,EAClB,wBAAwB,EAC3B,MAAM,cAAc,CAAA;AACrB,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAA;AAC5D,OAAO,EA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rowser } from '@towns-labs/utils';
4
+ const ONE_SECOND_MS = 1000;
5
+ const ONE_MINUTE_MS = 60 * ONE_SECOND_MS;
6
+ const ONE_HOUR_MS = 60 * ONE_MINUTE_MS;
7
+ const ONE_DAY_MS = 24 * ONE_HOUR_MS;
8
+ export const DEFAULT_USER_DEVICE_EXPIRATION_TIME_MS = 5 * ONE_DAY_MS;
9
+ export function createCryptoStore(databaseName, userId, maxEntries) {
10
+ if (isBrowser) {
11
+ return new CryptoStoreIndexedDb(databaseName, userId);
12
+ }
13
+ else {
14
+ return new CryptoStoreInMemory(userId, maxEntries);
15
+ }
16
+ }
17
+ //# sourceMappingURL=cryptoStore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cryptoStore.js","sourceRoot":"","sources":["../src/cryptoStore.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAA;AAC7D,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAE7C,MAAM,aAAa,GAAG,IAAI,CAAA;AAC1B,MAAM,aAAa,GAAG,EAAE,GAAG,aAAa,CAAA;AACxC,MAAM,WAAW,GAAG,EAAE,GAAG,aAAa,CAAA;AACtC,MAAM,UAAU,GAAG,EAAE,GAAG,WAAW,CAAA;AAEnC,MAAM,CAAC,MAAM,sCAAsC,GAAG,CAAC,GAAG,UAAU,CAAA;AAEpE,MAAM,UAAU,iBAAiB,CAC7B,YAAoB,EACpB,MAAc,EACd,UAAmB;IAEnB,IAAI,SAAS,EAAE,CAAC;QACZ,OAAO,IAAI,oBAAoB,CAAC,YAAY,EAAE,MAAM,CAAC,CAAA;IACzD,CAAC;SAAM,CAAC;QACJ,OAAO,IAAI,mBAAmB,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;IACtD,CAAC;AACL,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const AES_GCM_DERIVED_ALGORITHM = "r.aes-256-gcm.derived";
2
+ //# sourceMappingURL=derivedEncryption.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"derivedEncryption.d.ts","sourceRoot":"","sources":["../src/derivedEncryption.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,yBAAyB,0BAA0B,CAAA"}
@@ -0,0 +1,2 @@
1
+ export const AES_GCM_DERIVED_ALGORITHM = 'r.aes-256-gcm.derived';
2
+ //# sourceMappingURL=derivedEncryption.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"derivedEncryption.js","sourceRoot":"","sources":["../src/derivedEncryption.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,yBAAyB,GAAG,uBAAuB,CAAA"}
@@ -0,0 +1,16 @@
1
+ import { Account, InboundGroupSession, OutboundGroupSession, PkDecryption, PkEncryption, PkSigning, Session, Utility } from './encryptionTypes';
2
+ export declare class EncryptionDelegate {
3
+ private delegate;
4
+ isInitialized: boolean;
5
+ constructor();
6
+ init(): Promise<void>;
7
+ createAccount(): Account;
8
+ createSession(): Session;
9
+ createInboundGroupSession(): InboundGroupSession;
10
+ createOutboundGroupSession(): OutboundGroupSession;
11
+ createPkEncryption(): PkEncryption;
12
+ createPkDecryption(): PkDecryption;
13
+ createPkSigning(): PkSigning;
14
+ createUtility(): Utility;
15
+ }
16
+ //# sourceMappingURL=encryptionDelegate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encryptionDelegate.d.ts","sourceRoot":"","sources":["../src/encryptionDelegate.ts"],"names":[],"mappings":"AACA,OAAO,EACH,OAAO,EACP,mBAAmB,EACnB,oBAAoB,EACpB,YAAY,EACZ,YAAY,EACZ,SAAS,EACT,OAAO,EACP,OAAO,EACV,MAAM,mBAAmB,CAAA;AAE1B,qBAAa,kBAAkB;IAC3B,OAAO,CAAC,QAAQ,CAAqB;IAC9B,aAAa,UAAQ;;IAKf,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAS3B,aAAa,IAAI,OAAO;IAOxB,aAAa,IAAI,OAAO;IAOxB,yBAAyB,IAAI,mBAAmB;IAOhD,0BAA0B,IAAI,oBAAoB;IAOlD,kBAAkB,IAAI,YAAY;IAOlC,kBAAkB,IAAI,YAAY;IAOlC,eAAe,IAAI,SAAS;IAO5B,aAAa,IAAI,OAAO;CAMlC"}
@@ -0,0 +1,64 @@
1
+ import Olm from '@towns-protocol/olm';
2
+ export class EncryptionDelegate {
3
+ delegate;
4
+ isInitialized = false;
5
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
6
+ constructor() { }
7
+ async init() {
8
+ // initializes Olm library. This should run before using any Olm classes.
9
+ if (this.delegate) {
10
+ return;
11
+ }
12
+ this.delegate = await Olm.initAsync();
13
+ this.isInitialized = this.delegate !== undefined;
14
+ }
15
+ createAccount() {
16
+ if (!this.delegate) {
17
+ throw new Error('olm not initialized');
18
+ }
19
+ return new this.delegate.Account();
20
+ }
21
+ createSession() {
22
+ if (!this.delegate) {
23
+ throw new Error('olm not initialized');
24
+ }
25
+ return new this.delegate.Session();
26
+ }
27
+ createInboundGroupSession() {
28
+ if (!this.delegate) {
29
+ throw new Error('olm not initialized');
30
+ }
31
+ return new this.delegate.InboundGroupSession();
32
+ }
33
+ createOutboundGroupSession() {
34
+ if (!this.delegate) {
35
+ throw new Error('olm not initialized');
36
+ }
37
+ return new this.delegate.OutboundGroupSession();
38
+ }
39
+ createPkEncryption() {
40
+ if (!this.delegate) {
41
+ throw new Error('olm not initialized');
42
+ }
43
+ return new this.delegate.PkEncryption();
44
+ }
45
+ createPkDecryption() {
46
+ if (!this.delegate) {
47
+ throw new Error('olm not initialized');
48
+ }
49
+ return new this.delegate.PkDecryption();
50
+ }
51
+ createPkSigning() {
52
+ if (!this.delegate) {
53
+ throw new Error('olm not initialized');
54
+ }
55
+ return new this.delegate.PkSigning();
56
+ }
57
+ createUtility() {
58
+ if (!this.delegate) {
59
+ throw new Error('olm not initialized');
60
+ }
61
+ return new this.delegate.Utility();
62
+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C,qBAAqB,CAAC,CAAA;QAC1C,CAAC;QACD,OAAO,IAAI,IAAI,CAAC,QAAQ,CAAC,oBAAoB,EAAE,CAAA;IACnD,CAAC;IAEM,kBAAkB;QACrB,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;QAC1C,CAAC;QACD,OAAO,IAAI,IAAI,CAAC,QAAQ,CAAC,YAAY,EAAE,CAAA;IAC3C,CAAC;IAEM,kBAAkB;QACrB,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;QAC1C,CAAC;QACD,OAAO,IAAI,IAAI,CAAC,QAAQ,CAAC,YAAY,EAAE,CAAA;IAC3C,CAAC;IAEM,eAAe;QAClB,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;QAC1C,CAAC;QACD,OAAO,IAAI,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAA;IACxC,CAAC;IAEM,aAAa;QAChB,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;QAC1C,CAAC;QACD,OAAO,IAAI,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAA;IACtC,CAAC;CACJ"}
@@ -0,0 +1,264 @@
1
+ import type { CryptoStore } from './cryptoStore';
2
+ import { InboundGroupSession, IOutboundGroupSessionKey } from './encryptionTypes';
3
+ import { EncryptionDelegate } from './encryptionDelegate';
4
+ import { GroupEncryptionSession } from './olmLib';
5
+ import type { HybridGroupSessionRecord } from './storeTypes';
6
+ import { ExportedDevice, HybridGroupSessionKey } from '@towns-labs/proto';
7
+ /** data stored in the session store about an inbound group session */
8
+ export interface InboundGroupSessionData {
9
+ stream_id: string;
10
+ /** pickled InboundGroupSession */
11
+ session: string;
12
+ keysClaimed: Record<string, string>;
13
+ /** whether this session is untrusted. */
14
+ untrusted?: boolean;
15
+ }
16
+ export type EncryptionDeviceInitOpts = {
17
+ fromExportedDevice?: ExportedDevice;
18
+ pickleKey?: string;
19
+ };
20
+ export interface IDecryptedGroupMessage {
21
+ result: string;
22
+ keysClaimed: Record<string, string>;
23
+ streamId: string;
24
+ untrusted: boolean;
25
+ }
26
+ export type GroupSessionExtraData = {
27
+ untrusted?: boolean;
28
+ };
29
+ export declare class EncryptionDevice {
30
+ private delegate;
31
+ private readonly cryptoStore;
32
+ pickleKey: string;
33
+ /** Curve25519 key for the account, unknown until we load the account from storage in init() */
34
+ deviceCurve25519Key: string | null;
35
+ /** Ed25519 key for the account, unknown until we load the account from storage in init() */
36
+ deviceDoNotUseKey: string | null;
37
+ fallbackKey: {
38
+ keyId: string;
39
+ key: string;
40
+ };
41
+ sessionsInProgress: Record<string, Promise<void>>;
42
+ olmPrekeyPromise: Promise<any>;
43
+ private inboundGroupSessionMessageIndexes;
44
+ constructor(delegate: EncryptionDelegate, cryptoStore: CryptoStore);
45
+ /**
46
+ * Iniitialize the Account. Must be called prior to any other operation
47
+ * on the device.
48
+ *
49
+ * Data from an exported device can be provided in order to recreate this device.
50
+ *
51
+ * Attempts to load the Account from the crypto store, or create one otherwise
52
+ * storing the account in storage.
53
+ *
54
+ * Reads the device keys from the Account object.
55
+ *
56
+ * @param fromExportedDevice - data from exported device
57
+ * that must be re-created.
58
+ * If present, opts.pickleKey is ignored
59
+ * (exported data already provides a pickle key)
60
+ * @param pickleKey - pickle key to set instead of default one
61
+ *
62
+ *
63
+ */
64
+ init(opts?: EncryptionDeviceInitOpts): Promise<void>;
65
+ private initializeFromExportedDevice;
66
+ private initializeAccount;
67
+ /**
68
+ * Export the current device state
69
+ * @returns ExportedDevice object containing the device state
70
+ */
71
+ exportDevice(): Promise<ExportedDevice>;
72
+ /**
73
+ * Extract our Account from the crypto store and call the given function
74
+ * with the account object
75
+ * The `account` object is usable only within the callback passed to this
76
+ * function and will be freed as soon the callback returns. It is *not*
77
+ * usable for the rest of the lifetime of the transaction.
78
+ * This function requires a live transaction object from cryptoStore.doTxn()
79
+ * and therefore may only be called in a doTxn() callback.
80
+ *
81
+ * @param txn - Opaque transaction object from cryptoStore.doTxn()
82
+ * @internal
83
+ */
84
+ private getAccount;
85
+ /**
86
+ * Saves an account to the crypto store.
87
+ * This function requires a live transaction object from cryptoStore.doTxn()
88
+ * and therefore may only be called in a doTxn() callback.
89
+ *
90
+ * @param txn - Opaque transaction object from cryptoStore.doTxn()
91
+ * @param Account object
92
+ * @internal
93
+ */
94
+ private storeAccount;
95
+ /**
96
+ * get an OlmUtility and call the given function
97
+ *
98
+ * @returns result of func
99
+ * @internal
100
+ */
101
+ private getUtility;
102
+ /**
103
+ * Signs a message with the ed25519 key for this account.
104
+ *
105
+ * @param message - message to be signed
106
+ * @returns base64-encoded signature
107
+ */
108
+ sign(message: string): Promise<string>;
109
+ /**
110
+ * Marks all of the fallback keys as published.
111
+ */
112
+ markKeysAsPublished(): Promise<void>;
113
+ /**
114
+ * Generate a new fallback keys
115
+ *
116
+ * @returns Resolved once the account is saved back having generated the key
117
+ */
118
+ generateFallbackKeyIfNeeded(): Promise<void>;
119
+ getFallbackKey(): Promise<{
120
+ keyId: string;
121
+ key: string;
122
+ }>;
123
+ forgetOldFallbackKey(): Promise<void>;
124
+ /**
125
+ * Store an OutboundGroupSession in outboundSessionStore
126
+ *
127
+ */
128
+ private saveOutboundGroupSession;
129
+ /**
130
+ * Extract OutboundGroupSession from the session store and call given fn.
131
+ */
132
+ private getOutboundGroupSession;
133
+ /**
134
+ * Get the session keys for an outbound group session
135
+ *
136
+ * @param sessionId - the id of the outbound group session
137
+ *
138
+ * @returns current chain index, and
139
+ * base64-encoded secret key.
140
+ */
141
+ getOutboundGroupSessionKey(streamId: string): Promise<IOutboundGroupSessionKey>;
142
+ /** */
143
+ getHybridGroupSessionKeyForStream(streamId: string): Promise<HybridGroupSessionKey>;
144
+ /** */
145
+ getHybridGroupSessionKey(streamId: string, sessionId: string): Promise<HybridGroupSessionKey>;
146
+ /**
147
+ * Generate a new outbound group session
148
+ *
149
+ */
150
+ createOutboundGroupSession(streamId: string): Promise<string>;
151
+ /** */
152
+ createHybridGroupSession(streamId: string, miniblockNum: bigint, miniblockHash: Uint8Array): Promise<{
153
+ sessionId: string;
154
+ sessionRecord: HybridGroupSessionRecord;
155
+ sessionKey: HybridGroupSessionKey;
156
+ }>;
157
+ /**
158
+ * Unpickle a session from a sessionData object and invoke the given function.
159
+ * The session is valid only until func returns.
160
+ *
161
+ * @param sessionData - Object describing the session.
162
+ * @param func - Invoked with the unpickled session
163
+ * @returns result of func
164
+ */
165
+ private unpickleInboundGroupSession;
166
+ /**
167
+ * Extract an InboundGroupSession from the crypto store and call the given function
168
+ *
169
+ * @param streamId - The stream ID to extract the session for, or null to fetch
170
+ * sessions for any room.
171
+ * @param txn - Opaque transaction object from cryptoStore.doTxn()
172
+ * @param func - function to call.
173
+ *
174
+ * @internal
175
+ */
176
+ getInboundGroupSession(streamId: string, sessionId: string): Promise<{
177
+ session: InboundGroupSession | undefined;
178
+ data: InboundGroupSessionData | undefined;
179
+ }>;
180
+ /**
181
+ * Add an inbound group session to the session store
182
+ *
183
+ * @param streamId - room in which this session will be used
184
+ * @param senderKey - base64-encoded curve25519 key of the sender
185
+ * @param sessionId - session identifier
186
+ * @param sessionKey - base64-encoded secret key
187
+ * @param keysClaimed - Other keys the sender claims.
188
+ * @param exportFormat - true if the group keys are in export format
189
+ * (ie, they lack an ed25519 signature)
190
+ * @param extraSessionData - any other data to be include with the session
191
+ */
192
+ addInboundGroupSession(streamId: string, sessionId: string, sessionKey: string, keysClaimed: Record<string, string>, _exportFormat: boolean, extraSessionData?: GroupSessionExtraData): Promise<void>;
193
+ /** */
194
+ addHybridGroupSession(streamId: string, sessionId: string, sessionKey: string): Promise<void>;
195
+ /**
196
+ * Encrypt an outgoing message with an outbound group session
197
+ *
198
+ * @param sessionId - this id of the session
199
+ * @param payloadString - payload to be encrypted
200
+ *
201
+ * @returns ciphertext
202
+ */
203
+ encryptGroupMessage(payloadString: string, streamId: string): Promise<{
204
+ ciphertext: string;
205
+ sessionId: string;
206
+ }>;
207
+ encryptUsingFallbackKey(theirIdentityKey: string, fallbackKey: string, payload: string): Promise<{
208
+ type: 0 | 1;
209
+ body: string;
210
+ }>;
211
+ /**
212
+ * Decrypt an incoming message using an existing session
213
+ *
214
+ * @param theirDeviceIdentityKey - Curve25519 identity key for the
215
+ * remote device
216
+ * @param messageType - messageType field from the received message
217
+ * @param ciphertext - base64-encoded body from the received message
218
+ *
219
+ * @returns decrypted payload.
220
+ */
221
+ decryptMessage(ciphertext: string, theirDeviceIdentityKey: string, messageType?: number): Promise<string>;
222
+ /**
223
+ * Verify an ed25519 signature.
224
+ *
225
+ * @param key - ed25519 key
226
+ * @param message - message which was signed
227
+ * @param signature - base64-encoded signature to be checked
228
+ *
229
+ * @throws Error if there is a problem with the verification. If the key was
230
+ * too small then the message will be "OLM.INVALID_BASE64". If the signature
231
+ * was invalid then the message will be "OLM.BAD_MESSAGE_MAC".
232
+ */
233
+ verifySignature(key: string, message: string, signature: string): void;
234
+ getInboundGroupSessionIds(streamId: string): Promise<string[]>;
235
+ getHybridGroupSessionIds(streamId: string): Promise<string[]>;
236
+ /**
237
+ * Determine if we have the keys for a given group session
238
+ *
239
+ * @param streamId - stream in which the message was received
240
+ * @param senderKey - base64-encoded curve25519 key of the sender
241
+ * @param sessionId - session identifier
242
+ */
243
+ hasInboundSessionKeys(streamId: string, sessionId: string): Promise<boolean>;
244
+ /** */
245
+ hasHybridGroupSessionKey(streamId: string, sessionId: string): Promise<boolean>;
246
+ /**
247
+ * Export an inbound group session
248
+ *
249
+ * @param streamId - streamId of session
250
+ * @param sessionId - session identifier
251
+ */
252
+ exportInboundGroupSession(streamId: string, sessionId: string): Promise<GroupEncryptionSession | undefined>;
253
+ /** */
254
+ exportHybridGroupSession(streamId: string, sessionId: string): Promise<GroupEncryptionSession | undefined>;
255
+ /**
256
+ * Get a list containing all of the room keys
257
+ *
258
+ * @returns a list of session export objects
259
+ */
260
+ exportInboundGroupSessions(): Promise<GroupEncryptionSession[]>;
261
+ exportHybridGroupSessions(): Promise<GroupEncryptionSession[]>;
262
+ }
263
+ export declare function hybridSessionKeyHash(streamId: Uint8Array, key: Uint8Array, miniblockNum: bigint, miniblockHash: Uint8Array): Promise<Uint8Array>;
264
+ //# sourceMappingURL=encryptionDevice.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encryptionDevice.d.ts","sourceRoot":"","sources":["../src/encryptionDevice.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAChD,OAAO,EAEH,mBAAmB,EACnB,wBAAwB,EAG3B,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAA8B,sBAAsB,EAAE,MAAM,UAAU,CAAA;AAE7E,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAA;AAC5D,OAAO,EACH,cAAc,EAMd,qBAAqB,EAGxB,MAAM,mBAAmB,CAAA;AAW1B,sEAAsE;AACtE,MAAM,WAAW,uBAAuB;IACpC,SAAS,EAAE,MAAM,CAAA;IACjB,kCAAkC;IAClC,OAAO,EAAE,MAAM,CAAA;IACf,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACnC,yCAAyC;IACzC,SAAS,CAAC,EAAE,OAAO,CAAA;CACtB;AAED,MAAM,MAAM,wBAAwB,GAAG;IACnC,kBAAkB,CAAC,EAAE,cAAc,CAAA;IACnC,SAAS,CAAC,EAAE,MAAM,CAAA;CACrB,CAAA;AAqBD,MAAM,WAAW,sBAAsB;IACnC,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,MAAM,CAAC,MAAM,EA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