mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-10-07 22:57:07 +00:00
21 lines
672 B
JavaScript
21 lines
672 B
JavaScript
import EthCrypto from 'eth-crypto'
|
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
|
|
const encrypt = ({ identity }) => async (value) => {
|
|
const encryptedObj = await EthCrypto.encryptWithPublicKey(identity.publicKey, value)
|
|
return EthCrypto.cipher.stringify(encryptedObj)
|
|
}
|
|
|
|
const decrypt = ({ identities, identity }) => async (value) => {
|
|
const privateKey = await identities.keystore.getKey(identity.id)
|
|
const privateKeyStr = uint8ArrayToString(privateKey.marshal(), 'base16')
|
|
|
|
const encryptedObj = EthCrypto.cipher.parse(value)
|
|
return await EthCrypto.decryptWithPrivateKey(privateKeyStr, encryptedObj)
|
|
}
|
|
|
|
export {
|
|
encrypt,
|
|
decrypt
|
|
}
|