-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCoreCryptoInstance.ts
More file actions
120 lines (108 loc) · 4.24 KB
/
CoreCryptoInstance.ts
File metadata and controls
120 lines (108 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Wire
// Copyright (C) 2022 Wire Swiss GmbH
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
import * as CoreCryptoFfiTypes from "../browser/autogenerated/core_crypto_ffi";
import {
CoreCryptoFfi,
coreCryptoHistoryClient,
coreCryptoNew,
Database,
HistorySecret,
} from "./autogenerated/core_crypto_ffi";
import { CoreCryptoError } from "../browser/autogenerated/core_crypto_ffi";
import { CoreCryptoContext } from "./CoreCryptoContext";
/**
* Wrapper for the WASM-compiled version of CoreCrypto
*/
export class CoreCrypto extends CoreCryptoFfi {
/** @internal */
static instanceOf(obj: unknown): obj is CoreCryptoFfi {
return super.instanceOf(obj);
}
private constructor(ffiInstance: CoreCryptoFfi) {
super(ffiInstance);
}
/**
* Construct a new {@link CoreCrypto} instance.
* MLS or proteus can be initialized with {@link CoreCryptoContext.mlsInit} or
* {@link CoreCryptoContext.proteusInit}, respectively.
*/
public static new(database: Database): CoreCrypto {
const ffiInstance = coreCryptoNew(database);
return new this(ffiInstance);
}
/**
* Instantiate a history client.
*
* This client exposes the full interface of {@link CoreCrypto}, but it should only be used to decrypt messages.
* Other use is a logic error.
*/
public static async historyClient(
historySecret: HistorySecret
): Promise<CoreCrypto> {
const historyClient = await coreCryptoHistoryClient(historySecret);
return new this(historyClient);
}
/**
* Starts a new transaction in Core Crypto. If the callback succeeds, it will be committed,
* otherwise, every operation performed with the context will be discarded.
*
* @param callback - The callback to execute within the transaction
*
* @returns the result of the callback will be returned from this call
*/
async newTransaction<R>(
callback: (ctx: CoreCryptoContext) => Promise<R>
): Promise<R> {
let result!: R;
let error: CoreCryptoError | Error | null = null;
let needOuterRethrow = false;
try {
await super.transaction({
execute: async (ctx: CoreCryptoFfiTypes.CoreCryptoContext) => {
try {
result = await callback(new CoreCryptoContext(ctx));
} catch (e) {
// We want to catch the error before it gets wrapped by core crypto.
if (CoreCryptoError.instanceOf(e)) {
error = e;
} else if (Error.isError(e)) {
error = e;
} else {
// Something unexpected was thrown
needOuterRethrow = true;
}
// This is to tell core crypto that there was an error inside the transaction.
throw e;
}
},
});
} catch (e) {
// We prefer the closure error if it's available since the transaction will just wrap and re-throw it.
// If we caught an object of an unexpected type, we're rethrowing it as a wrapped `CoreCryptoError`.
if (needOuterRethrow) {
throw e;
}
}
if (error !== null) {
throw error;
}
return result;
}
/** @internal */
async transaction(
command: CoreCryptoFfiTypes.CoreCryptoCommand,
asyncOpts_?: { signal: AbortSignal }
): Promise<void> {
super.transaction(command, asyncOpts_);
}
}