UNPKG

1.48 kBPlain TextView Raw
1import * as cbor from 'cborg'
2import * as aes from 'keystore-idb/aes/index'
3import { SymmKeyLength } from 'keystore-idb/types'
4import { JEST_IMPLEMENTATION } from '../src/setup/jest'
5import { loadWebnativePage } from './helpers/page'
6
7
8describe("cbor encoding", () => {
9 it("works in node with encryption in between", async () => {
10 const key = await aes.makeKey({ length: SymmKeyLength.B256 })
11 const keyStr = await aes.exportKey(key)
12
13 const message = {
14 hello: "world!"
15 }
16 const encoded = cbor.encode(message)
17 const cipher = await JEST_IMPLEMENTATION.aes.encrypt(encoded, keyStr)
18 const decipher = await JEST_IMPLEMENTATION.aes.decrypt(cipher, keyStr)
19 const decoded = cbor.decode(decipher)
20
21 expect(decoded).toEqual(message)
22 })
23
24 it("works in the browser with encryption in between", async () => {
25 await loadWebnativePage()
26
27 async function runRoundTrip(message) {
28 const keyStr = await webnative.crypto.aes.genKeyStr()
29
30 const encoded = webnative.cbor.encode(message)
31 const cipher = await webnative.crypto.aes.encrypt(encoded, keyStr)
32 const decipher = await webnative.crypto.aes.decrypt(cipher, keyStr)
33 const decoded = webnative.cbor.decode(decipher)
34
35 return decoded
36 }
37
38 const message = { hello: "world!" }
39 expect(await page.evaluate(runRoundTrip, message)).toEqual(message)
40 })
41})