Skip to content

Commit

Permalink
add symmetric keys to crypto controller
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrouid committed Feb 27, 2022
1 parent 00ad404 commit 5629c15
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/client/src/controllers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
encrypt,
decrypt,
sha256,
generateRandomBytes32,
} from "@walletconnect/utils";

import { CRYPTO_CONTEXT, KEYCHAIN_CONTEXT } from "../constants";
Expand Down Expand Up @@ -107,6 +108,28 @@ export class Crypto implements ICrypto {
return this.setEncryptionKeys({ sharedKey, publicKey: keyPair.publicKey }, overrideTopic);
}

public async generateSymKey(overrideTopic?: string): Promise<string> {
const symKey = generateRandomBytes32();
return this.setSymKey(symKey, overrideTopic);
}

public async setSymKey(symKey: string, overrideTopic?: string): Promise<string> {
const hash = await sha256(symKey);
return this.setEncryptionKeys({ sharedKey: symKey, publicKey: hash }, overrideTopic);
}

public async deleteKeyPair(publicKey: string): Promise<void> {
await this.keychain.del(publicKey);
}

public async deleteSharedKey(topic: string): Promise<void> {
await this.keychain.del(topic);
}

public async deleteSymKey(topic: string): Promise<void> {
await this.keychain.del(topic);
}

public async encrypt(topic: string, message: string): Promise<string> {
const { sharedKey, publicKey } = await this.getEncryptionKeys(topic);
const result = await encrypt({ message, sharedKey, publicKey });
Expand Down
10 changes: 10 additions & 0 deletions packages/types/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ export abstract class ICrypto {
overrideTopic?: string,
): Promise<string>;

public abstract generateSymKey(overrideTopic?: string): Promise<string>;

public abstract setSymKey(symKey: string, overrideTopic?: string): Promise<string>;

public abstract deleteKeyPair(publicKey: string): Promise<void>;

public abstract deleteSharedKey(topic: string): Promise<void>;

public abstract deleteSymKey(topic: string): Promise<void>;

public abstract encrypt(topic: string, message: string): Promise<string>;

public abstract decrypt(topic: string, encrypted: string): Promise<string>;
Expand Down

0 comments on commit 5629c15

Please sign in to comment.