Skip to content

Commit

Permalink
AES256GCM decrypt: replace Observable with Promise (#778)
Browse files Browse the repository at this point in the history
* AES256GCM decrypt: replace Observable with Promise

* WalletSDKRelay

* fix linter

* prettier

* hmmmmm

* rejects.toThrowError

* rejects.toThrow

* update comment

* return promise

* use async / await instead

* prettier
  • Loading branch information
bangtoven committed Jan 10, 2023
1 parent cb0d710 commit 8fc922d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
5 changes: 2 additions & 3 deletions packages/wallet-sdk/src/relay/WalletSDKRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import bind from "bind-decorator";
import { ethErrors } from "eth-rpc-errors";
import { BehaviorSubject, Observable, of, Subscription, zip } from "rxjs";
import { BehaviorSubject, from, Observable, of, Subscription, zip } from "rxjs";
import {
catchError,
distinctUntilChanged,
Expand Down Expand Up @@ -777,8 +777,7 @@ export class WalletSDKRelay extends WalletSDKRelayAbstract {
private handleIncomingEvent(event: ServerMessageEvent): void {
try {
this.subscriptions.add(
aes256gcm
.decrypt(event.data, this.session.secret)
from(aes256gcm.decrypt(event.data, this.session.secret))
.pipe(map(c => JSON.parse(c)))
.subscribe({
next: json => {
Expand Down
29 changes: 8 additions & 21 deletions packages/wallet-sdk/src/relay/aes256gcm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ describe("aes256gcm", () => {
expect(encrypted.length).toEqual(90);

// decrypted output matches original input
decrypt(encrypted, randSecret).subscribe({
next: decrypted => {
expect(decrypted).toBe("plain text string");
},
});
const decrypted = await decrypt(encrypted, randSecret);

expect(decrypted).toBe("plain text string");
});

test("decrypt", () => {
test("decrypt", async () => {
const cipherText =
"06593325a922a928913b5c6ea26f848c4545bcea4e26c4f5ee745316ff22b2780aeccc565730514b2820a94b03f5f89fe7542a35bbdd87a1d52a4352f49482781113db09266c668696778e0a94bc9f866f1e92e7262fd0bb811838284cc64cbc4552b33e9c6fb2582cea4f49471d6d46a16a5c8ac83ee8483ed4dc01f1fde3bfd7a2f173715e0a8d09dd4907483f096a845bff698831ea277c1ca4223d3f6073174cb35119d0a795c1a9cb4f32ee1dcc254d8931";
const sampleDataResult = {
Expand All @@ -31,11 +29,9 @@ describe("aes256gcm", () => {
},
};

decrypt(cipherText, secret).subscribe({
next: value => {
expect(sampleDataResult).toEqual(value);
},
});
const decrypted = await decrypt(cipherText, secret);

expect(sampleDataResult).toEqual(JSON.parse(decrypted));
});

test("errors", async () => {
Expand All @@ -47,15 +43,6 @@ describe("aes256gcm", () => {
"secret must be 256 bits",
);

await expect(
decrypt("text", secret).subscribe(
() => {
fail("expected error");
},
error => {
expect(error).toBe("expected error");
},
),
);
await expect(decrypt("text", secret)).rejects.toThrow();
});
});
16 changes: 4 additions & 12 deletions packages/wallet-sdk/src/relay/aes256gcm.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) 2018-2022 Coinbase, Inc. <https://www.coinbase.com/>
// Licensed under the Apache License, version 2.0

import { Observable } from "rxjs";

import { hexStringToUint8Array, uint8ArrayToHex } from "../util";

/**
Expand Down Expand Up @@ -63,15 +61,10 @@ export async function encrypt(
* @param cipherText hex string representation of bytes in the order: initialization vector (iv),
* auth tag, encrypted plaintext. IV is 12 bytes. Auth tag is 16 bytes.
* @param secret hex string representation of 32-byte secret
*
* TODO: Update rxjs for promises
*/
export function decrypt(
cipherText: string,
secret: string,
): Observable<string> {
export function decrypt(cipherText: string, secret: string): Promise<string> {
if (secret.length !== 64) throw Error(`secret must be 256 bits`);
return new Observable<string>(subscriber => {
return new Promise<string>((resolve, reject) => {
void (async function () {
const secretKey: CryptoKey = await crypto.subtle.importKey(
"raw",
Expand Down Expand Up @@ -101,10 +94,9 @@ export function decrypt(
concattedBytes,
);
const decoder = new TextDecoder();
subscriber.next(decoder.decode(decrypted));
subscriber.complete();
resolve(decoder.decode(decrypted));
} catch (err) {
subscriber.error(err);
reject(err);
}
})();
});
Expand Down

0 comments on commit 8fc922d

Please sign in to comment.