diff --git a/packages/client/src/controllers/crypto.ts b/packages/client/src/controllers/crypto.ts index 543dd7e30..1e6e873a3 100644 --- a/packages/client/src/controllers/crypto.ts +++ b/packages/client/src/controllers/crypto.ts @@ -11,6 +11,7 @@ import { encrypt, decrypt, sha256, + generateRandomBytes32, } from "@walletconnect/utils"; import { CRYPTO_CONTEXT, KEYCHAIN_CONTEXT } from "../constants"; @@ -107,6 +108,28 @@ export class Crypto implements ICrypto { return this.setEncryptionKeys({ sharedKey, publicKey: keyPair.publicKey }, overrideTopic); } + public async generateSymKey(overrideTopic?: string): Promise { + const symKey = generateRandomBytes32(); + return this.setSymKey(symKey, overrideTopic); + } + + public async setSymKey(symKey: string, overrideTopic?: string): Promise { + const hash = await sha256(symKey); + return this.setEncryptionKeys({ sharedKey: symKey, publicKey: hash }, overrideTopic); + } + + public async deleteKeyPair(publicKey: string): Promise { + await this.keychain.del(publicKey); + } + + public async deleteSharedKey(topic: string): Promise { + await this.keychain.del(topic); + } + + public async deleteSymKey(topic: string): Promise { + await this.keychain.del(topic); + } + public async encrypt(topic: string, message: string): Promise { const { sharedKey, publicKey } = await this.getEncryptionKeys(topic); const result = await encrypt({ message, sharedKey, publicKey }); diff --git a/packages/types/src/crypto.ts b/packages/types/src/crypto.ts index 12ec71963..b513788d5 100644 --- a/packages/types/src/crypto.ts +++ b/packages/types/src/crypto.ts @@ -70,6 +70,16 @@ export abstract class ICrypto { overrideTopic?: string, ): Promise; + public abstract generateSymKey(overrideTopic?: string): Promise; + + public abstract setSymKey(symKey: string, overrideTopic?: string): Promise; + + public abstract deleteKeyPair(publicKey: string): Promise; + + public abstract deleteSharedKey(topic: string): Promise; + + public abstract deleteSymKey(topic: string): Promise; + public abstract encrypt(topic: string, message: string): Promise; public abstract decrypt(topic: string, encrypted: string): Promise;