Skip to content

Commit 0ae3145

Browse files
authored
Merge pull request #8947 from BitGo/revert-8936-WCN-723
Revert "feat: default encryptAsync to v2"
2 parents c220724 + 72a0767 commit 0ae3145

2 files changed

Lines changed: 25 additions & 38 deletions

File tree

modules/sdk-api/src/encrypt.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ export function encrypt(
4444

4545
/**
4646
* Async encrypt that dispatches to v1 (SJCL) or v2 (Argon2id + AES-256-GCM)
47-
* when `encryptionVersion` is 2. Defaults to v2.
47+
* when `encryptionVersion` is 2. Defaults to v1, matching sync `encrypt()`.
4848
*/
4949
export async function encryptAsync(
5050
password: string,
5151
plaintext: string,
5252
options?: { salt?: Buffer; iv?: Buffer; adata?: string; encryptionVersion?: 1 | 2 }
5353
): Promise<string> {
54-
if (options?.encryptionVersion === 1) {
55-
return encrypt(password, plaintext, options);
54+
if (options?.encryptionVersion === 2) {
55+
return encryptV2(password, plaintext, { adata: options.adata });
5656
}
57-
return encryptV2(password, plaintext, { adata: options?.adata });
57+
return encrypt(password, plaintext, options);
5858
}
5959

6060
/** Decrypt a v1 SJCL envelope. */

modules/sdk-api/test/unit/encrypt.ts

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,19 @@ describe('encryption methods tests', () => {
199199
const password = 'myPassword';
200200
const plaintext = 'Hello, World!';
201201

202-
it('dispatches to v2 by default and output is decryptable via decryptAsync', async () => {
202+
it('dispatches to v1 by default and output is decryptable via decrypt', async () => {
203203
const ct = await encryptAsync(password, plaintext);
204-
const envelope: V2Envelope = JSON.parse(ct);
205-
assert.strictEqual(envelope.v, 2, 'default should produce v2 envelope');
206-
assert.strictEqual(await decryptAsync(password, ct), plaintext);
204+
const envelope = JSON.parse(ct);
205+
assert.notStrictEqual(envelope.v, 2, 'default should not produce v2 envelope');
206+
assert.strictEqual(decrypt(password, ct), plaintext);
207207
});
208208

209-
it('dispatches to v2 when encryptionVersion: 2 is explicit', async () => {
209+
it('dispatches to v2 when encryptionVersion: 2', async () => {
210210
const ct = await encryptAsync(password, plaintext, { encryptionVersion: 2 });
211211
const envelope: V2Envelope = JSON.parse(ct);
212212
assert.strictEqual(envelope.v, 2);
213-
assert.strictEqual(await decryptAsync(password, ct), plaintext);
213+
const result = await decryptAsync(password, ct);
214+
assert.strictEqual(result, plaintext);
214215
});
215216

216217
it('dispatches to v1 when encryptionVersion: 1', async () => {
@@ -228,18 +229,9 @@ describe('encryption methods tests', () => {
228229
assert.strictEqual(await decryptAsync(password, ct), plaintext);
229230
});
230231

231-
it('forwards adata to v2 envelope by default', async () => {
232+
it('encrypts v1 with adata', async () => {
232233
const adata = 'additional data';
233234
const ct = await encryptAsync(password, plaintext, { adata });
234-
const envelope: V2Envelope = JSON.parse(ct);
235-
assert.strictEqual(envelope.v, 2);
236-
assert.strictEqual(envelope.adata, adata);
237-
assert.strictEqual(await decryptAsync(password, ct), plaintext);
238-
});
239-
240-
it('encrypts v1 with adata when encryptionVersion: 1', async () => {
241-
const adata = 'additional data';
242-
const ct = await encryptAsync(password, plaintext, { adata, encryptionVersion: 1 });
243235
assert.strictEqual(decrypt(password, ct), plaintext);
244236
});
245237

@@ -252,24 +244,18 @@ describe('encryption methods tests', () => {
252244
it('forwards salt and iv options to v1 encrypt for deterministic output', async () => {
253245
const salt = randomBytes(8);
254246
const iv = randomBytes(16);
255-
const ct1 = await encryptAsync(password, plaintext, { salt, iv, encryptionVersion: 1 });
256-
const ct2 = await encryptAsync(password, plaintext, { salt, iv, encryptionVersion: 1 });
247+
const ct1 = await encryptAsync(password, plaintext, { salt, iv });
248+
const ct2 = await encryptAsync(password, plaintext, { salt, iv });
257249
assert.strictEqual(ct1, ct2);
258250
assert.strictEqual(decrypt(password, ct1), plaintext);
259251
});
260252

261-
it('throws an error if the salt length is not 8 bytes for v1', async () => {
262-
await assert.rejects(
263-
() => encryptAsync(password, plaintext, { salt: randomBytes(4), encryptionVersion: 1 }),
264-
/salt must be 8 bytes/
265-
);
253+
it('throws an error if the salt length is not 8 bytes', async () => {
254+
await assert.rejects(() => encryptAsync(password, plaintext, { salt: randomBytes(4) }), /salt must be 8 bytes/);
266255
});
267256

268-
it('throws an error if the iv length is not 16 bytes for v1', async () => {
269-
await assert.rejects(
270-
() => encryptAsync(password, plaintext, { iv: randomBytes(4), encryptionVersion: 1 }),
271-
/iv must be 16 bytes/
272-
);
257+
it('throws an error if the iv length is not 16 bytes', async () => {
258+
await assert.rejects(() => encryptAsync(password, plaintext, { iv: randomBytes(4) }), /iv must be 16 bytes/);
273259
});
274260
});
275261

@@ -462,18 +448,19 @@ describe('encryption methods tests', () => {
462448
bitgo = new BitGoAPI({ env: 'test' });
463449
});
464450

465-
it('dispatches to v2 by default and output is decryptable via decryptAsync', async () => {
451+
it('dispatches to v1 by default and output is decryptable via decrypt', async () => {
466452
const ct = await bitgo.encryptAsync({ input: plaintext, password });
467-
const envelope: V2Envelope = JSON.parse(ct);
468-
assert.strictEqual(envelope.v, 2, 'default should produce v2 envelope');
469-
assert.strictEqual(await decryptAsync(password, ct), plaintext);
453+
const envelope = JSON.parse(ct);
454+
assert.notStrictEqual(envelope.v, 2, 'default should not produce v2 envelope');
455+
assert.strictEqual(decrypt(password, ct), plaintext);
470456
});
471457

472-
it('dispatches to v2 when encryptionVersion: 2 is explicit', async () => {
458+
it('dispatches to v2 when encryptionVersion: 2 and output is decryptable via decryptAsync', async () => {
473459
const ct = await bitgo.encryptAsync({ input: plaintext, password, encryptionVersion: 2 });
474460
const envelope: V2Envelope = JSON.parse(ct);
475461
assert.strictEqual(envelope.v, 2);
476-
assert.strictEqual(await decryptAsync(password, ct), plaintext);
462+
const result = await decryptAsync(password, ct);
463+
assert.strictEqual(result, plaintext);
477464
});
478465

479466
it('forwards adata to v2 envelope', async () => {

0 commit comments

Comments
 (0)