Skip to content

Commit 6e31631

Browse files
chore(ts): use transaction() and findCredentials()
1 parent 2ae167a commit 6e31631

25 files changed

+181
-181
lines changed

crypto-ffi/bindings/js/benches/CoreCrypto.bench.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function measureDecryption(
3131
);
3232
const messageBytes = encoder.encode(message).buffer;
3333

34-
const encryptedMessages = await cc1.newTransaction(async (ctx) => {
34+
const encryptedMessages = await cc1.transaction(async (ctx) => {
3535
const encryptedMessages: ArrayBuffer[] = [];
3636
for (let i = 0; i < messageCount; i++) {
3737
const encryptedMessage = await ctx.encryptMessage(
@@ -46,7 +46,7 @@ async function measureDecryption(
4646

4747
// measure decryption performance
4848
const cc2 = window.ensureCcDefined(client2);
49-
return await cc2.newTransaction(async (ctx) => {
49+
return await cc2.transaction(async (ctx) => {
5050
const start = performance.now();
5151

5252
const decryptedMessages: (Uint8Array | undefined)[] = [];

crypto-ffi/bindings/js/benches/createMessage.bench.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe("benchmark", () => {
3131
new TextEncoder().encode(conversationIdStr).buffer
3232
);
3333

34-
await cc.newTransaction(async (ctx) => {
34+
await cc.transaction(async (ctx) => {
3535
const [credentialRef] = await ctx.getCredentials();
3636
await ctx.createConversation(
3737
conversationId,
@@ -42,7 +42,7 @@ describe("benchmark", () => {
4242
window.bench.add(
4343
`cipherSuite=${window.ccModule.Ciphersuite[cipherSuite]} size=${size}B count=${count}`,
4444
async () => {
45-
await cc.newTransaction(async (ctx) => {
45+
await cc.transaction(async (ctx) => {
4646
for (let i = 0; i < count; i++) {
4747
await ctx.encryptMessage(
4848
conversationId,

crypto-ffi/bindings/js/benches/processMessage.bench.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ describe("benchmark", () => {
2929
new TextEncoder().encode(conversationIdStr).buffer
3030
);
3131

32-
await aliceCc.newTransaction(async (ctx) => {
32+
await aliceCc.transaction(async (ctx) => {
3333
const [credentialRef] = await ctx.getCredentials();
3434
await ctx.createConversation(
3535
conversationId,
3636
credentialRef!
3737
);
3838
});
3939

40-
const kp = await bobCc.newTransaction(async (ctx) => {
40+
const kp = await bobCc.transaction(async (ctx) => {
4141
const [credentialRef] =
42-
await ctx.getFilteredCredentials({
42+
await ctx.findCredentials({
4343
ciphersuite: cipherSuite,
4444
credentialType:
4545
window.ccModule.CredentialType.Basic,
4646
});
4747
return await ctx.generateKeypackage(credentialRef!);
4848
});
4949

50-
await aliceCc.newTransaction(
50+
await aliceCc.transaction(
5151
async (ctx) =>
5252
await ctx.addClientsToConversation(conversationId, [
5353
kp,
@@ -56,7 +56,7 @@ describe("benchmark", () => {
5656
const commitBundle =
5757
await window.deliveryService.getLatestCommitBundle();
5858

59-
await bobCc.newTransaction(
59+
await bobCc.transaction(
6060
async (ctx) =>
6161
await ctx.processWelcomeMessage(
6262
commitBundle.welcome!
@@ -70,7 +70,7 @@ describe("benchmark", () => {
7070
`cipherSuite=${window.ccModule.Ciphersuite[cipherSuite]} size=${size}B count=${count}`,
7171
async () => {
7272
const encryptedMessages =
73-
await aliceCc.newTransaction(async (ctx) => {
73+
await aliceCc.transaction(async (ctx) => {
7474
const encryptedMessages: ArrayBuffer[] = [];
7575
for (let i = 0; i < count; i++) {
7676
const encryptedMessage =
@@ -86,7 +86,7 @@ describe("benchmark", () => {
8686
return encryptedMessages;
8787
});
8888
const start = window.bench.now();
89-
await bobCc.newTransaction(async (ctx) => {
89+
await bobCc.transaction(async (ctx) => {
9090
for (const message of encryptedMessages) {
9191
await ctx.decryptMessage(
9292
conversationId,

crypto-ffi/bindings/js/benches/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export async function setup() {
101101

102102
const cc = window.ccModule.CoreCrypto.new(db);
103103

104-
await cc.newTransaction(async (ctx) => {
104+
await cc.transaction(async (ctx) => {
105105
await ctx.mlsInit(id, window.deliveryService);
106106
await ctx.addCredential(
107107
window.ccModule.Credential.basic(cipherSuite, id)

crypto-ffi/bindings/js/src/shared/CoreCryptoContext.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ export class CoreCryptoContext extends CoreCryptoContextFfi {
3030
*
3131
* @param findFilters a set of filters defining which credentials are of interest.
3232
*/
33-
async getFilteredCredentials(
33+
async findCredentials(
3434
findFilters: CredentialFindFilters
3535
): Promise<CredentialRef[]> {
36-
return await super.findCredentials(
36+
return await super.findCredentialsFfi(
3737
findFilters.clientId,
3838
findFilters.publicKey,
3939
findFilters.ciphersuite,
@@ -42,18 +42,18 @@ export class CoreCryptoContext extends CoreCryptoContextFfi {
4242
);
4343
}
4444

45-
/**
46-
* You should use {@link CoreCryptoContext.getFilteredCredentials} instead, since it provides a nicer interface.
45+
/** @internal
46+
* We're overriding this just to hide it from the docs
4747
*/
48-
async findCredentials(
48+
async findCredentialsFfi(
4949
clientId?: ClientId,
5050
publicKey?: ArrayBuffer,
5151
ciphersuite?: Ciphersuite,
5252
credentialType?: CredentialType,
5353
earliestValidity?: bigint,
5454
asyncOpts_?: { signal: AbortSignal }
5555
): Promise<Array<CredentialRef>> {
56-
return await super.findCredentials(
56+
return await super.findCredentialsFfi(
5757
clientId,
5858
publicKey,
5959
ciphersuite,

crypto-ffi/bindings/js/test/bun/napi/credential.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("credentials", () => {
3434

3535
const cc = await ccInit(clientId);
3636

37-
const ref = await cc.newTransaction(async (ctx) => {
37+
const ref = await cc.transaction(async (ctx) => {
3838
return await ctx.addCredential(credential);
3939
});
4040

@@ -43,7 +43,7 @@ describe("credentials", () => {
4343
// saving causes the earliest validity to be updated
4444
expect(ref.earliestValidity()).not.toEqual(0n);
4545

46-
const allCredentials = await cc.newTransaction(async (ctx) => {
46+
const allCredentials = await cc.transaction(async (ctx) => {
4747
return await ctx.getCredentials();
4848
});
4949
expect(allCredentials.length).toBe(1);
@@ -57,15 +57,15 @@ describe("credentials", () => {
5757

5858
const cc = await ccInit(clientId);
5959

60-
const ref = await cc.newTransaction(async (ctx) => {
60+
const ref = await cc.transaction(async (ctx) => {
6161
return await ctx.addCredential(credential);
6262
});
6363

64-
await cc.newTransaction(async (ctx) => {
64+
await cc.transaction(async (ctx) => {
6565
return await ctx.removeCredential(ref);
6666
});
6767

68-
const allCredentials = await cc.newTransaction(async (ctx) => {
68+
const allCredentials = await cc.transaction(async (ctx) => {
6969
return await ctx.getCredentials();
7070
});
7171
expect(allCredentials.length).toBe(0);
@@ -84,18 +84,18 @@ describe("credentials", () => {
8484

8585
const cc = await ccInit(clientId);
8686

87-
await cc.newTransaction(async (ctx) => {
87+
await cc.transaction(async (ctx) => {
8888
await ctx.addCredential(credential1);
8989
await ctx.addCredential(credential2);
9090
});
9191

92-
const results1 = await cc.newTransaction(async (ctx) => {
93-
return await ctx.getFilteredCredentials({
92+
const results1 = await cc.transaction(async (ctx) => {
93+
return await ctx.findCredentials({
9494
ciphersuite: ciphersuite1,
9595
});
9696
});
97-
const results2 = await cc.newTransaction(async (ctx) => {
98-
return await ctx.getFilteredCredentials({
97+
const results2 = await cc.transaction(async (ctx) => {
98+
return await ctx.findCredentials({
9999
ciphersuite: ciphersuite2,
100100
});
101101
});

crypto-ffi/bindings/js/test/bun/napi/keyPackage.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ describe("key package", () => {
2424

2525
const cc = await ccInit(clientId);
2626

27-
const credentialRef = await cc.newTransaction(async (ctx) => {
27+
const credentialRef = await cc.transaction(async (ctx) => {
2828
return await ctx.addCredential(credential);
2929
});
3030

31-
const keyPackage = await cc.newTransaction(async (ctx) => {
31+
const keyPackage = await cc.transaction(async (ctx) => {
3232
return await ctx.generateKeypackage(credentialRef);
3333
});
3434

@@ -43,11 +43,11 @@ describe("key package", () => {
4343

4444
const cc = await ccInit(clientId);
4545

46-
const credentialRef = await cc.newTransaction(async (ctx) => {
46+
const credentialRef = await cc.transaction(async (ctx) => {
4747
return await ctx.addCredential(credential);
4848
});
4949

50-
const keyPackage = await cc.newTransaction(async (ctx) => {
50+
const keyPackage = await cc.transaction(async (ctx) => {
5151
return await ctx.generateKeypackage(credentialRef);
5252
});
5353

@@ -71,15 +71,15 @@ describe("key package", () => {
7171

7272
const cc = await ccInit(clientId);
7373

74-
const credentialRef = await cc.newTransaction(async (ctx) => {
74+
const credentialRef = await cc.transaction(async (ctx) => {
7575
return await ctx.addCredential(credential);
7676
});
7777

78-
await cc.newTransaction(async (ctx) => {
78+
await cc.transaction(async (ctx) => {
7979
await ctx.generateKeypackage(credentialRef);
8080
});
8181

82-
const keyPackages = await cc.newTransaction(async (ctx) => {
82+
const keyPackages = await cc.transaction(async (ctx) => {
8383
return await ctx.getKeypackages();
8484
});
8585

@@ -96,26 +96,26 @@ describe("key package", () => {
9696

9797
const cc = await ccInit(clientId);
9898

99-
const credentialRef = await cc.newTransaction(async (ctx) => {
99+
const credentialRef = await cc.transaction(async (ctx) => {
100100
return await ctx.addCredential(credential);
101101
});
102102

103103
// add a kp which will not be removed, so we have one left over
104-
await cc.newTransaction(async (ctx) => {
104+
await cc.transaction(async (ctx) => {
105105
await ctx.generateKeypackage(credentialRef);
106106
});
107107

108108
// add a kp which will be removed
109-
const keyPackage = await cc.newTransaction(async (ctx) => {
109+
const keyPackage = await cc.transaction(async (ctx) => {
110110
return await ctx.generateKeypackage(credentialRef);
111111
});
112112

113113
// now remove the keypackage
114-
await cc.newTransaction(async (ctx) => {
114+
await cc.transaction(async (ctx) => {
115115
await ctx.removeKeypackage(keyPackage.ref());
116116
});
117117

118-
const keyPackages = await cc.newTransaction(async (ctx) => {
118+
const keyPackages = await cc.transaction(async (ctx) => {
119119
return await ctx.getKeypackages();
120120
});
121121

@@ -137,7 +137,7 @@ describe("key package", () => {
137137
);
138138
const cc = await ccInit(clientId);
139139

140-
await cc.newTransaction(async (ctx) => {
140+
await cc.transaction(async (ctx) => {
141141
const cref1 = await ctx.addCredential(credential1);
142142
const cref2 = await ctx.addCredential(credential2);
143143

crypto-ffi/bindings/js/test/bun/napi/utils.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export async function ccInit(
122122
const cc = CoreCrypto.new(database);
123123

124124
if (clientId) {
125-
await cc.newTransaction(async (ctx) => {
125+
await cc.transaction(async (ctx) => {
126126
await ctx.mlsInit(clientId, DELIVERY_SERVICE);
127127
});
128128
}
@@ -154,7 +154,7 @@ export async function createConversation(
154154
cc: CoreCrypto,
155155
conversationId: ConversationId
156156
): Promise<void> {
157-
await cc.newTransaction(async (ctx) => {
157+
await cc.transaction(async (ctx) => {
158158
const credential = Credential.basic(
159159
ciphersuiteDefault(),
160160
randomClientId()
@@ -183,20 +183,20 @@ export async function invite(
183183
cc2: CoreCrypto,
184184
conversationId: ConversationId
185185
): Promise<GroupInfoBundle> {
186-
const kp = await cc2.newTransaction(async (ctx) => {
187-
const [credentialRef] = await ctx.getFilteredCredentials({
186+
const kp = await cc2.transaction(async (ctx) => {
187+
const [credentialRef] = await ctx.findCredentials({
188188
ciphersuite: DEFAULT_CIPHERSUITE,
189189
credentialType: CredentialType.Basic,
190190
});
191191
return await ctx.generateKeypackage(credentialRef!);
192192
});
193-
await cc1.newTransaction((ctx) =>
193+
await cc1.transaction((ctx) =>
194194
ctx.addClientsToConversation(conversationId, [kp])
195195
);
196196
const { groupInfo, welcome } =
197197
await DELIVERY_SERVICE.getLatestCommitBundle();
198198

199-
await cc2.newTransaction((ctx) =>
199+
await cc2.transaction((ctx) =>
200200
ctx.processWelcomeMessage(new Welcome(welcome!.copyBytes()))
201201
);
202202

@@ -223,17 +223,17 @@ export async function roundTripMessage(
223223
conversationId: ConversationId,
224224
message: ArrayBuffer
225225
): Promise<(ArrayBuffer | null)[]> {
226-
const encryptedByClient1 = await cc1.newTransaction(async (ctx) => {
226+
const encryptedByClient1 = await cc1.transaction(async (ctx) => {
227227
return await ctx.encryptMessage(conversationId, message);
228228
});
229-
const decryptedByClient2 = await cc2.newTransaction(async (ctx) => {
229+
const decryptedByClient2 = await cc2.transaction(async (ctx) => {
230230
return await ctx.decryptMessage(conversationId, encryptedByClient1);
231231
});
232232

233-
const encryptedByClient2 = await cc2.newTransaction(async (ctx) => {
233+
const encryptedByClient2 = await cc2.transaction(async (ctx) => {
234234
return await ctx.encryptMessage(conversationId, message);
235235
});
236-
const decryptedByClient1 = await cc1.newTransaction(async (ctx) => {
236+
const decryptedByClient1 = await cc1.transaction(async (ctx) => {
237237
return await ctx.decryptMessage(conversationId, encryptedByClient2);
238238
});
239239

@@ -263,7 +263,7 @@ export async function proteusInit(
263263
const database = await openTestDatabase(databaseName ?? clientName);
264264

265265
const instance = CoreCrypto.new(database);
266-
await instance.newTransaction(async (ctx) => {
266+
await instance.transaction(async (ctx) => {
267267
await ctx.proteusInit();
268268
});
269269

@@ -287,11 +287,11 @@ export async function newProteusSessionFromPrekey(
287287
cc2: CoreCrypto,
288288
sessionId: string
289289
): Promise<void> {
290-
const cc2Prekey = await cc2.newTransaction(async (ctx) => {
290+
const cc2Prekey = await cc2.transaction(async (ctx) => {
291291
return await ctx.proteusNewPrekey(10);
292292
});
293293

294-
await cc1.newTransaction(async (ctx) => {
294+
await cc1.transaction(async (ctx) => {
295295
return await ctx.proteusSessionFromPrekey(sessionId, cc2Prekey);
296296
});
297297
}
@@ -317,11 +317,11 @@ export async function newProteusSessionFromMessage(
317317
sessionId: string,
318318
messageBytes: ArrayBuffer
319319
): Promise<ArrayBuffer> {
320-
const encrypted = await cc1.newTransaction(async (ctx) => {
320+
const encrypted = await cc1.transaction(async (ctx) => {
321321
return await ctx.proteusEncrypt(sessionId, messageBytes);
322322
});
323323

324-
const decrypted = await cc2.newTransaction(async (ctx) => {
324+
const decrypted = await cc2.transaction(async (ctx) => {
325325
return await ctx.proteusSessionFromMessage(sessionId, encrypted);
326326
});
327327

0 commit comments

Comments
 (0)