mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-07-04 19:42:29 +00:00
feat: Upgrade ccrypto.
This commit is contained in:
parent
a0f434c3fa
commit
e1ef3224b4
909
package-lock.json
generated
909
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -19,7 +19,7 @@
|
|||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@ipld/dag-cbor": "^9.0.6",
|
"@ipld/dag-cbor": "^9.0.6",
|
||||||
"@libp2p/crypto": "^3.0.2",
|
"@libp2p/crypto": "^5.0.5",
|
||||||
"it-pipe": "^3.0.1",
|
"it-pipe": "^3.0.1",
|
||||||
"level": "^8.0.0",
|
"level": "^8.0.0",
|
||||||
"lru": "^3.1.0",
|
"lru": "^3.1.0",
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
||||||
import { signMessage, verifyMessage } from '../../key-store.js'
|
import { signMessage, verifyMessage } from '../../key-store.js'
|
||||||
|
import { publicKeyFromRaw } from '@libp2p/crypto/keys'
|
||||||
|
|
||||||
const type = 'publickey'
|
const type = 'publickey'
|
||||||
|
|
||||||
@ -52,7 +53,7 @@ const PublicKeyIdentityProvider = ({ keystore }) => async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const key = await keystore.getKey(id) || await keystore.createKey(id)
|
const key = await keystore.getKey(id) || await keystore.createKey(id)
|
||||||
return uint8ArrayToString(key.public.marshal(), 'base16')
|
return uint8ArrayToString(key.publicKey.raw, 'base16')
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
* const storage = await MemoryStorage()
|
* const storage = await MemoryStorage()
|
||||||
* const keystore = await KeyStore({ storage })
|
* const keystore = await KeyStore({ storage })
|
||||||
*/
|
*/
|
||||||
import * as crypto from '@libp2p/crypto'
|
import { privateKeyFromRaw, publicKeyFromRaw, generateKeyPair } from '@libp2p/crypto/keys'
|
||||||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
||||||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
||||||
import { compare as uint8ArrayCompare } from 'uint8arrays/compare'
|
import { compare as uint8ArrayCompare } from 'uint8arrays/compare'
|
||||||
@ -16,9 +16,6 @@ import ComposedStorage from './storage/composed.js'
|
|||||||
import LevelStorage from './storage/level.js'
|
import LevelStorage from './storage/level.js'
|
||||||
import LRUStorage from './storage/lru.js'
|
import LRUStorage from './storage/lru.js'
|
||||||
|
|
||||||
const unmarshal = crypto.keys.supportedKeys.secp256k1.unmarshalSecp256k1PrivateKey
|
|
||||||
const unmarshalPubKey = crypto.keys.supportedKeys.secp256k1.unmarshalSecp256k1PublicKey
|
|
||||||
|
|
||||||
const verifySignature = async (signature, publicKey, data) => {
|
const verifySignature = async (signature, publicKey, data) => {
|
||||||
if (!signature) {
|
if (!signature) {
|
||||||
throw new Error('No signature given')
|
throw new Error('No signature given')
|
||||||
@ -38,7 +35,7 @@ const verifySignature = async (signature, publicKey, data) => {
|
|||||||
|
|
||||||
let res = false
|
let res = false
|
||||||
try {
|
try {
|
||||||
const pubKey = unmarshalPubKey(uint8ArrayFromString(publicKey, 'base16'))
|
const pubKey = publicKeyFromRaw(uint8ArrayFromString(publicKey, 'base16'))
|
||||||
res = await isValid(pubKey, data, uint8ArrayFromString(signature, 'base16'))
|
res = await isValid(pubKey, data, uint8ArrayFromString(signature, 'base16'))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Catch error: sig length wrong
|
// Catch error: sig length wrong
|
||||||
@ -195,7 +192,7 @@ const KeyStore = async ({ storage, path } = {}) => {
|
|||||||
const { privateKey } = key
|
const { privateKey } = key
|
||||||
await storage.put('private_' + id, privateKey)
|
await storage.put('private_' + id, privateKey)
|
||||||
// Unmarshal the key and add it to the cache
|
// Unmarshal the key and add it to the cache
|
||||||
const unmarshaledPrivateKey = unmarshal(privateKey)
|
const unmarshaledPrivateKey = privateKeyFromRaw(privateKey)
|
||||||
await keyCache.put(id, unmarshaledPrivateKey)
|
await keyCache.put(id, unmarshaledPrivateKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,17 +210,16 @@ const KeyStore = async ({ storage, path } = {}) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate a private key
|
// Generate a private key
|
||||||
const keyPair = await crypto.keys.generateKeyPair('secp256k1')
|
const keyPair = await generateKeyPair('secp256k1')
|
||||||
const keys = await crypto.keys.unmarshalPrivateKey(keyPair.bytes)
|
|
||||||
|
|
||||||
const key = {
|
const key = {
|
||||||
publicKey: keys.public.marshal(),
|
publicKey: keyPair.publicKey.raw,
|
||||||
privateKey: keys.marshal()
|
privateKey: keyPair.raw
|
||||||
}
|
}
|
||||||
|
|
||||||
await addKey(id, key)
|
await addKey(id, key)
|
||||||
|
|
||||||
return keys
|
return keyPair
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -254,7 +250,8 @@ const KeyStore = async ({ storage, path } = {}) => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
key = unmarshal(storedKey)
|
key = privateKeyFromRaw(storedKey)
|
||||||
|
|
||||||
await keyCache.put(id, key)
|
await keyCache.put(id, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -281,7 +278,7 @@ const KeyStore = async ({ storage, path } = {}) => {
|
|||||||
throw new Error('Supported formats are `hex` and `buffer`')
|
throw new Error('Supported formats are `hex` and `buffer`')
|
||||||
}
|
}
|
||||||
|
|
||||||
const pubKey = keys.public.marshal()
|
const pubKey = keys.publicKey.raw
|
||||||
|
|
||||||
return format === 'buffer' ? pubKey : uint8ArrayToString(pubKey, 'base16')
|
return format === 'buffer' ? pubKey : uint8ArrayToString(pubKey, 'base16')
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
import * as crypto from '@libp2p/crypto'
|
import { privateKeyFromRaw } from '@libp2p/crypto/keys'
|
||||||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
||||||
import { Identities, KeyStore } from '../../src/index.js'
|
import { Identities, KeyStore } from '../../src/index.js'
|
||||||
|
|
||||||
const unmarshal = crypto.keys.supportedKeys.secp256k1.unmarshalSecp256k1PrivateKey
|
|
||||||
const unmarshalPubKey = crypto.keys.supportedKeys.secp256k1.unmarshalSecp256k1PublicKey
|
|
||||||
|
|
||||||
const keysPath = './testkeys'
|
const keysPath = './testkeys'
|
||||||
|
|
||||||
const isBrowser = () => typeof window !== 'undefined'
|
const isBrowser = () => typeof window !== 'undefined'
|
||||||
@ -52,10 +49,10 @@ before(async () => {
|
|||||||
]
|
]
|
||||||
|
|
||||||
for (let user of users) {
|
for (let user of users) {
|
||||||
const privateKey1 = unmarshal(uint8ArrayFromString(user.privateKey, 'base16'))
|
const privateKey1 = privateKeyFromRaw(uint8ArrayFromString(user.privateKey, 'base16'))
|
||||||
const privateKey2 = unmarshal(uint8ArrayFromString(user.identity.privateKey, 'base16'))
|
const privateKey2 = privateKeyFromRaw(uint8ArrayFromString(user.identity.privateKey, 'base16'))
|
||||||
await keystore.addKey(user.id, { privateKey: privateKey1.marshal() })
|
await keystore.addKey(user.id, { privateKey: privateKey1.raw })
|
||||||
await keystore.addKey(user.identity.id, { privateKey: privateKey2.marshal() })
|
await keystore.addKey(user.identity.id, { privateKey: privateKey2.raw })
|
||||||
}
|
}
|
||||||
|
|
||||||
await keystore.close()
|
await keystore.close()
|
||||||
|
@ -11,7 +11,7 @@ import createHelia from '../../utils/create-helia.js'
|
|||||||
const keysPath = './testkeys'
|
const keysPath = './testkeys'
|
||||||
|
|
||||||
describe('Documents Database Replication', function () {
|
describe('Documents Database Replication', function () {
|
||||||
this.timeout(30000)
|
this.timeout(10000)
|
||||||
|
|
||||||
let ipfs1, ipfs2
|
let ipfs1, ipfs2
|
||||||
let keystore
|
let keystore
|
||||||
@ -31,6 +31,18 @@ describe('Documents Database Replication', function () {
|
|||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
[ipfs1, ipfs2] = await Promise.all([createHelia(), createHelia()])
|
[ipfs1, ipfs2] = await Promise.all([createHelia(), createHelia()])
|
||||||
|
|
||||||
|
ipfs1.libp2p.addEventListener("peer:connect", (event) => {
|
||||||
|
console.log(event.detail.toString())
|
||||||
|
})
|
||||||
|
|
||||||
|
ipfs2.libp2p.addEventListener("peer:connect", (event) => {
|
||||||
|
console.log(event.detail.toString())
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(ipfs1.libp2p.peerId.toString())
|
||||||
|
console.log(ipfs2.libp2p.peerId.toString())
|
||||||
|
|
||||||
await connectPeers(ipfs1, ipfs2)
|
await connectPeers(ipfs1, ipfs2)
|
||||||
|
|
||||||
await copy(testKeysPath, keysPath)
|
await copy(testKeysPath, keysPath)
|
||||||
|
@ -38,7 +38,7 @@ describe('Identities', function () {
|
|||||||
identities = await Identities({ path: keysPath })
|
identities = await Identities({ path: keysPath })
|
||||||
identity = await identities.createIdentity({ id })
|
identity = await identities.createIdentity({ id })
|
||||||
const key = await identities.keystore.getKey(id)
|
const key = await identities.keystore.getKey(id)
|
||||||
const externalId = uint8ArrayToString(key.public.marshal(), 'base16')
|
const externalId = uint8ArrayToString(key.publicKey.raw, 'base16')
|
||||||
assert.strictEqual(identity.id, externalId)
|
assert.strictEqual(identity.id, externalId)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -106,7 +106,7 @@ describe('Identities', function () {
|
|||||||
identity = await identities.createIdentity({ id })
|
identity = await identities.createIdentity({ id })
|
||||||
keystore = identities.keystore
|
keystore = identities.keystore
|
||||||
const key = await keystore.getKey(id)
|
const key = await keystore.getKey(id)
|
||||||
const externalId = uint8ArrayToString(key.public.marshal(), 'base16')
|
const externalId = uint8ArrayToString(key.publicKey.raw, 'base16')
|
||||||
assert.strictEqual(identity.id, externalId)
|
assert.strictEqual(identity.id, externalId)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -117,7 +117,7 @@ describe('Identities', function () {
|
|||||||
|
|
||||||
it('has the correct public key', async () => {
|
it('has the correct public key', async () => {
|
||||||
const key = await keystore.getKey(id)
|
const key = await keystore.getKey(id)
|
||||||
const externalId = uint8ArrayToString(key.public.marshal(), 'base16')
|
const externalId = uint8ArrayToString(key.publicKey.raw, 'base16')
|
||||||
const signingKey = await keystore.getKey(externalId)
|
const signingKey = await keystore.getKey(externalId)
|
||||||
assert.notStrictEqual(signingKey, undefined)
|
assert.notStrictEqual(signingKey, undefined)
|
||||||
assert.strictEqual(identity.publicKey, keystore.getPublic(signingKey))
|
assert.strictEqual(identity.publicKey, keystore.getPublic(signingKey))
|
||||||
@ -125,10 +125,10 @@ describe('Identities', function () {
|
|||||||
|
|
||||||
it('has a signature for the id', async () => {
|
it('has a signature for the id', async () => {
|
||||||
const key = await keystore.getKey(id)
|
const key = await keystore.getKey(id)
|
||||||
const externalId = uint8ArrayToString(key.public.marshal(), 'base16')
|
const externalId = uint8ArrayToString(key.publicKey.raw, 'base16')
|
||||||
const signingKey = await keystore.getKey(externalId)
|
const signingKey = await keystore.getKey(externalId)
|
||||||
const idSignature = await signMessage(signingKey, externalId)
|
const idSignature = await signMessage(signingKey, externalId)
|
||||||
const publicKey = uint8ArrayToString(signingKey.public.marshal(), 'base16')
|
const publicKey = uint8ArrayToString(signingKey.publicKey.raw, 'base16')
|
||||||
const verifies = await verifyMessage(idSignature, publicKey, externalId)
|
const verifies = await verifyMessage(idSignature, publicKey, externalId)
|
||||||
assert.strictEqual(verifies, true)
|
assert.strictEqual(verifies, true)
|
||||||
assert.strictEqual(identity.signatures.id, idSignature)
|
assert.strictEqual(identity.signatures.id, idSignature)
|
||||||
@ -136,7 +136,7 @@ describe('Identities', function () {
|
|||||||
|
|
||||||
it('has a signature for the publicKey', async () => {
|
it('has a signature for the publicKey', async () => {
|
||||||
const key = await keystore.getKey(id)
|
const key = await keystore.getKey(id)
|
||||||
const externalId = uint8ArrayToString(key.public.marshal(), 'base16')
|
const externalId = uint8ArrayToString(key.publicKey.raw, 'base16')
|
||||||
const signingKey = await keystore.getKey(externalId)
|
const signingKey = await keystore.getKey(externalId)
|
||||||
const idSignature = await signMessage(signingKey, externalId)
|
const idSignature = await signMessage(signingKey, externalId)
|
||||||
const externalKey = await keystore.getKey(id)
|
const externalKey = await keystore.getKey(id)
|
||||||
@ -171,7 +171,7 @@ describe('Identities', function () {
|
|||||||
|
|
||||||
it('has the correct id', async () => {
|
it('has the correct id', async () => {
|
||||||
const key = await savedKeysKeyStore.getKey(id)
|
const key = await savedKeysKeyStore.getKey(id)
|
||||||
assert.strictEqual(identity.id, uint8ArrayToString(key.public.marshal(), 'base16'))
|
assert.strictEqual(identity.id, uint8ArrayToString(key.publicKey.raw, 'base16'))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('has the correct public key', async () => {
|
it('has the correct public key', async () => {
|
||||||
|
@ -145,7 +145,7 @@ describe('KeyStore', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('Options', () => {
|
describe('Options', () => {
|
||||||
const unmarshal = crypto.keys.supportedKeys.secp256k1.unmarshalSecp256k1PrivateKey
|
const unmarshal = crypto.keys.privateKeyFromRaw
|
||||||
const privateKey = '198594a8de39fd97017d11996d619b3746211605a9d290964badf58bc79bdb33'
|
const privateKey = '198594a8de39fd97017d11996d619b3746211605a9d290964badf58bc79bdb33'
|
||||||
const publicKey = '0260baeaffa1de1e4135e5b395e0380563a622b9599d1b8e012a0f7603f516bdaa'
|
const publicKey = '0260baeaffa1de1e4135e5b395e0380563a622b9599d1b8e012a0f7603f516bdaa'
|
||||||
let privateKeyBuffer, publicKeyBuffer, unmarshalledPrivateKey
|
let privateKeyBuffer, publicKeyBuffer, unmarshalledPrivateKey
|
||||||
|
@ -23,6 +23,8 @@ describe('Replicating databases', function () {
|
|||||||
after(async () => {
|
after(async () => {
|
||||||
await orbitdb1.stop()
|
await orbitdb1.stop()
|
||||||
await orbitdb2.stop()
|
await orbitdb2.stop()
|
||||||
|
await ipfs1.blockstore.child.child.child.close()
|
||||||
|
await ipfs2.blockstore.child.child.child.close()
|
||||||
await ipfs1.stop()
|
await ipfs1.stop()
|
||||||
await ipfs2.stop()
|
await ipfs2.stop()
|
||||||
|
|
||||||
|
@ -72,8 +72,8 @@ describe('OrbitDB', function () {
|
|||||||
const privateKey = await orbitdb1.keystore.getKey(orbitdb1.identity.id)
|
const privateKey = await orbitdb1.keystore.getKey(orbitdb1.identity.id)
|
||||||
notStrictEqual(privateKey, undefined)
|
notStrictEqual(privateKey, undefined)
|
||||||
strictEqual(privateKey.constructor.name, 'Secp256k1PrivateKey')
|
strictEqual(privateKey.constructor.name, 'Secp256k1PrivateKey')
|
||||||
notStrictEqual(privateKey._key, undefined)
|
notStrictEqual(privateKey.raw, undefined)
|
||||||
notStrictEqual(privateKey._publicKey, undefined)
|
notStrictEqual(privateKey.publicKey, undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('has a keystore that contains a public key that matches the identity\'s public key', async () => {
|
it('has a keystore that contains a public key that matches the identity\'s public key', async () => {
|
||||||
@ -164,8 +164,8 @@ describe('OrbitDB', function () {
|
|||||||
const privateKey = await orbitdb1.keystore.getKey(orbitdb1.identity.id)
|
const privateKey = await orbitdb1.keystore.getKey(orbitdb1.identity.id)
|
||||||
notStrictEqual(privateKey, undefined)
|
notStrictEqual(privateKey, undefined)
|
||||||
strictEqual(privateKey.constructor.name, 'Secp256k1PrivateKey')
|
strictEqual(privateKey.constructor.name, 'Secp256k1PrivateKey')
|
||||||
notStrictEqual(privateKey._key, undefined)
|
notStrictEqual(privateKey.raw, undefined)
|
||||||
notStrictEqual(privateKey._publicKey, undefined)
|
notStrictEqual(privateKey.publicKey, undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('has a keystore that contains a public key that matches the identity\'s public key', async () => {
|
it('has a keystore that contains a public key that matches the identity\'s public key', async () => {
|
||||||
|
@ -54,7 +54,7 @@ const Libp2pBrowserOptions = {
|
|||||||
discoverRelays: 1
|
discoverRelays: 1
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
connectionEncryption: [noise()],
|
connectionEncrypters: [noise()],
|
||||||
streamMuxers: [yamux()],
|
streamMuxers: [yamux()],
|
||||||
connectionGater: {
|
connectionGater: {
|
||||||
denyDialMultiaddr: () => false
|
denyDialMultiaddr: () => false
|
||||||
|
Loading…
x
Reference in New Issue
Block a user