Skip to content

Commit

Permalink
feat: expose sodium encrypt/decrypt box in keychain api (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtoth authored Nov 15, 2024
1 parent 96c59c5 commit 618bc35
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
23 changes: 23 additions & 0 deletions features/keychain/api/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
3 changes: 3 additions & 0 deletions features/keychain/api/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ export interface KeychainApi {
): Promise<{ box: { publicKey: Buffer }; sign: { publicKey: Buffer } }>
encryptSecretBox(params: { data: Buffer } & KeySource): Promise<Buffer>
decryptSecretBox(params: { data: Buffer } & KeySource): Promise<Buffer>
encryptBox(params: { data: Buffer; toPublicKey: Buffer } & KeySource): Promise<Buffer>
decryptBox(params: { data: Buffer; fromPublicKey: Buffer } & KeySource): Promise<Buffer>
}
ed25519: {
signBuffer(params: { data: Buffer } & KeySource): Promise<Buffer>
}
secp256k1: {
signBuffer(params: { data: Buffer } & KeySource): Promise<Buffer>
signBuffer(params: { data: Buffer; enc: 'der' } & KeySource): Promise<Buffer>
signSchnorr(params: { data: Buffer; tweak?: Buffer } & KeySource): Promise<Buffer>
}
}

Expand Down
2 changes: 2 additions & 0 deletions features/keychain/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })),
},
Expand Down

0 comments on commit 618bc35

Please sign in to comment.