diff --git a/features/keychain/api/__tests__/index.test.js b/features/keychain/api/__tests__/index.test.js index bc73c60..aceeb11 100644 --- a/features/keychain/api/__tests__/index.test.js +++ b/features/keychain/api/__tests__/index.test.js @@ -159,6 +159,29 @@ describe('keychain api', () => { expect(data.compare(decrypted)).toBe(0) }) + test('should encrypt and decrypt data with external keypair', async () => { + const { + box: { publicKey: toPublicKey }, + } = await api.sodium.getKeysFromSeed({ seedId: otherSeedId, keyId }) + + const data = Buffer.from("Harvey Dent's identity was revealed as the Riddler") + const encrypted = await api.sodium.encryptBox({ seedId, keyId, data, toPublicKey }) + + expect(encrypted).toBeInstanceOf(Buffer) + + const { + box: { publicKey: fromPublicKey }, + } = await api.sodium.getKeysFromSeed({ seedId, keyId }) + + const decrypted = await api.sodium.decryptBox({ + seedId: otherSeedId, + keyId, + data: encrypted, + fromPublicKey, + }) + expect(data.compare(decrypted)).toBe(0) + }) + test('sign signs data', async () => { const data = Buffer.from("Batman's identity was revealed as Harvey Dent") const signed = await api.sodium.sign({ seedId, keyId, data }) diff --git a/features/keychain/api/index.d.ts b/features/keychain/api/index.d.ts index f2e0653..79d1519 100644 --- a/features/keychain/api/index.d.ts +++ b/features/keychain/api/index.d.ts @@ -27,6 +27,8 @@ export interface KeychainApi { ): Promise<{ box: { publicKey: Buffer }; sign: { publicKey: Buffer } }> encryptSecretBox(params: { data: Buffer } & KeySource): Promise decryptSecretBox(params: { data: Buffer } & KeySource): Promise + encryptBox(params: { data: Buffer; toPublicKey: Buffer } & KeySource): Promise + decryptBox(params: { data: Buffer; fromPublicKey: Buffer } & KeySource): Promise } ed25519: { signBuffer(params: { data: Buffer } & KeySource): Promise @@ -34,6 +36,7 @@ export interface KeychainApi { secp256k1: { signBuffer(params: { data: Buffer } & KeySource): Promise signBuffer(params: { data: Buffer; enc: 'der' } & KeySource): Promise + signSchnorr(params: { data: Buffer; tweak?: Buffer } & KeySource): Promise } } diff --git a/features/keychain/api/index.js b/features/keychain/api/index.js index 2661b0d..ce82827 100644 --- a/features/keychain/api/index.js +++ b/features/keychain/api/index.js @@ -8,6 +8,8 @@ const createKeychainApi = ({ keychain }) => { signDetached: keychain.sodium.signDetached, encryptSecretBox: keychain.sodium.encryptSecretBox, decryptSecretBox: keychain.sodium.decryptSecretBox, + encryptBox: keychain.sodium.encryptBox, + decryptBox: keychain.sodium.decryptBox, getKeysFromSeed: (...args) => keychain.sodium.getKeysFromSeed(...args).then(({ box, sign }) => ({ box, sign })), },