test: Enc/dec using simple encryption module.

This commit is contained in:
Hayden Young 2025-05-26 10:35:47 +02:00
parent b539877a39
commit 231976998f
4 changed files with 25 additions and 32 deletions

19
package-lock.json generated
View File

@ -22,6 +22,7 @@
"devDependencies": { "devDependencies": {
"@chainsafe/libp2p-gossipsub": "^14.1.0", "@chainsafe/libp2p-gossipsub": "^14.1.0",
"@libp2p/circuit-relay-v2": "^3.1.0", "@libp2p/circuit-relay-v2": "^3.1.0",
"@orbitdb/simple-encryption": "github:orbitdb/simple-encryption",
"blockstore-level": "^2.0.1", "blockstore-level": "^2.0.1",
"c8": "^8.0.1", "c8": "^8.0.1",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
@ -2758,6 +2759,24 @@
"node": ">= 8" "node": ">= 8"
} }
}, },
"node_modules/@orbitdb/simple-encryption": {
"version": "0.0.1",
"resolved": "git+ssh://git@github.com/orbitdb/simple-encryption.git#1aef41220a24fdad067ccb34cb14456b68bbdaf5",
"dev": true,
"license": "MIT",
"dependencies": {
"multiformats": "^13.3.6"
},
"engines": {
"node": ">=20.0.0"
}
},
"node_modules/@orbitdb/simple-encryption/node_modules/multiformats": {
"version": "13.3.6",
"resolved": "https://registry.npmjs.org/multiformats/-/multiformats-13.3.6.tgz",
"integrity": "sha512-yakbt9cPYj8d3vi/8o/XWm61MrOILo7fsTL0qxNx6zS0Nso6K5JqqS2WV7vK/KSuDBvrW3KfCwAdAgarAgOmww==",
"dev": true
},
"node_modules/@peculiar/asn1-cms": { "node_modules/@peculiar/asn1-cms": {
"version": "2.3.15", "version": "2.3.15",
"resolved": "https://registry.npmjs.org/@peculiar/asn1-cms/-/asn1-cms-2.3.15.tgz", "resolved": "https://registry.npmjs.org/@peculiar/asn1-cms/-/asn1-cms-2.3.15.tgz",

View File

@ -31,6 +31,7 @@
"devDependencies": { "devDependencies": {
"@chainsafe/libp2p-gossipsub": "^14.1.0", "@chainsafe/libp2p-gossipsub": "^14.1.0",
"@libp2p/circuit-relay-v2": "^3.1.0", "@libp2p/circuit-relay-v2": "^3.1.0",
"@orbitdb/simple-encryption": "github:orbitdb/simple-encryption",
"blockstore-level": "^2.0.1", "blockstore-level": "^2.0.1",
"c8": "^8.0.1", "c8": "^8.0.1",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",

View File

@ -1,27 +0,0 @@
const CustomEncryption = async ({ fail } = {}) => {
fail = fail || false
const key = 'encrypted'
const bufferedKey = new Uint8Array(Buffer.from(key))
const encrypt = (value) => {
return new Uint8Array([...bufferedKey, ...value])
}
const decrypt = (value) => {
const detectedBufferedKey = new Uint8Array(value.slice(0, bufferedKey.length))
const rawValue = new Uint8Array(value.slice(bufferedKey.length))
if (detectedBufferedKey.length == bufferedKey.length && !fail) {
return rawValue
} else {
throw new Exception('invalid encryption')
}
}
return {
encrypt,
decrypt
}
}
export default CustomEncryption

View File

@ -10,7 +10,7 @@ import * as Block from 'multiformats/block'
import * as dagCbor from '@ipld/dag-cbor' import * as dagCbor from '@ipld/dag-cbor'
import { sha256 } from 'multiformats/hashes/sha2' import { sha256 } from 'multiformats/hashes/sha2'
import CustomEncryption from './fixtures/encryption/custom.js' import SimpleEncryption from '@orbitdb/simple-encryption'
const codec = dagCbor const codec = dagCbor
const hasher = sha256 const hasher = sha256
@ -36,8 +36,8 @@ describe('Encryption', function () {
orbitdb1 = await createOrbitDB({ ipfs: ipfs1, id: 'user1', directory: path.join(dbPath, '1') }) orbitdb1 = await createOrbitDB({ ipfs: ipfs1, id: 'user1', directory: path.join(dbPath, '1') })
orbitdb2 = await createOrbitDB({ ipfs: ipfs2, id: 'user2', directory: path.join(dbPath, '2') }) orbitdb2 = await createOrbitDB({ ipfs: ipfs2, id: 'user2', directory: path.join(dbPath, '2') })
replicationEncryption = await CustomEncryption() replicationEncryption = await SimpleEncryption({ password: 'hello' })
dataEncryption = await CustomEncryption() dataEncryption = await SimpleEncryption({ password: 'world' })
}) })
after(async () => { after(async () => {
@ -218,7 +218,7 @@ describe('Encryption', function () {
let hasError = false let hasError = false
let error let error
const replicationEncryptionWithFailure = await CustomEncryption({ fail: true }) const replicationEncryptionWithFailure = await SimpleEncryption({ password: 'goodbye' })
const encryption = { const encryption = {
replication: replicationEncryption replication: replicationEncryption
@ -261,7 +261,7 @@ describe('Encryption', function () {
let hasError = false let hasError = false
let error let error
const dataEncryptionWithFailure = await CustomEncryption({ fail: true }) const dataEncryptionWithFailure = await SimpleEncryption({ password: 'goodbye' })
const encryption = { const encryption = {
data: dataEncryption data: dataEncryption