orbitdb/test/utils/encrypt.js
2024-05-17 03:30:56 +01:00

20 lines
587 B
JavaScript

import EthCrypto from 'eth-crypto'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
const encrypt = ({ publicKey }) => async (value) => {
const encryptedObj = await EthCrypto.encryptWithPublicKey(publicKey, value)
return EthCrypto.cipher.stringify(encryptedObj)
}
const decrypt = ({ privateKey }) => async (value) => {
const privateKeyStr = uint8ArrayToString(privateKey.marshal(), 'base16')
const encryptedObj = EthCrypto.cipher.parse(value)
return await EthCrypto.decryptWithPrivateKey(privateKeyStr, encryptedObj)
}
export {
encrypt,
decrypt
}