Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
test: add tests for different key types, where possible (#52)
Browse files Browse the repository at this point in the history
Expands the tests to test using `secp256k1` and `RSA` keys as well
as `Ed25519` where possible.

Kubo doesn't support generating or importing `secp256k1` keys so
those tests are skipped, as are `RSA` keys for pubsub resolution since
the DHT needs to be enabled to resolve the public key component.
  • Loading branch information
achingbrain authored May 25, 2023
1 parent 25b38fd commit 312381c
Show file tree
Hide file tree
Showing 3 changed files with 284 additions and 257 deletions.
271 changes: 143 additions & 128 deletions packages/interop/test/dht.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { ipns } from '@helia/ipns'
import { dht } from '@helia/ipns/routing'
import { type KadDHT, kadDHT } from '@libp2p/kad-dht'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { createEd25519PeerId, createRSAPeerId, createSecp256k1PeerId } from '@libp2p/peer-id-factory'
import { expect } from 'aegir/chai'
import { ipnsSelector } from 'ipns/selector'
import { ipnsValidator } from 'ipns/validator'
Expand All @@ -19,170 +19,185 @@ import { connect } from './fixtures/connect.js'
import { createHeliaNode } from './fixtures/create-helia.js'
import { createKuboNode } from './fixtures/create-kubo.js'
import { sortClosestPeers } from './fixtures/create-peer-ids.js'
import { keyTypes } from './fixtures/key-types.js'
import { waitFor } from './fixtures/wait-for.js'
import type { Helia } from '@helia/interface'
import type { IPNS } from '@helia/ipns'
import type { Libp2p } from '@libp2p/interface-libp2p'
import type { Controller } from 'ipfsd-ctl'
import type { PeerId } from 'kubo-rpc-client/dist/src/types.js'

describe('dht routing', () => {
let helia: Helia<Libp2p<{ dht: KadDHT }>>
let kubo: Controller
let name: IPNS

// the CID we are going to publish
let value: CID

// the public key we will use to publish the value
let key: PeerId

/**
* Ensure that for the CID we are going to publish, the resolver has a peer ID that
* is KAD-closer to the routing key so we can predict the the resolver will receive
* the DHT record containing the IPNS record
*/
async function createNodes (resolver: 'kubo' | 'helia'): Promise<void> {
const input = Uint8Array.from([0, 1, 2, 3, 4])
const digest = await sha256.digest(input)
value = CID.createV1(raw.code, digest)

helia = await createHeliaNode({
services: {
identify: identifyService(),
dht: kadDHT({
validators: {
ipns: ipnsValidator
},
selectors: {
ipns: ipnsSelector
},
// skips waiting for the initial self-query to find peers
allowQueryWithZeroPeers: true
})
}
})
kubo = await createKuboNode()

// find a PeerId that is KAD-closer to the resolver than the publisher when used as an IPNS key
while (true) {
key = await createEd25519PeerId()
const routingKey = uint8ArrayConcat([
uint8ArrayFromString('/ipns/'),
key.toBytes()
])
keyTypes.forEach(type => {
describe(`dht routing with ${type} keys`, () => {
let helia: Helia<Libp2p<{ dht: KadDHT }>>
let kubo: Controller
let name: IPNS

// the CID we are going to publish
let value: CID

// the public key we will use to publish the value
let key: PeerId

/**
* Ensure that for the CID we are going to publish, the resolver has a peer ID that
* is KAD-closer to the routing key so we can predict the the resolver will receive
* the DHT record containing the IPNS record
*/
async function createNodes (resolver: 'kubo' | 'helia'): Promise<void> {
const input = Uint8Array.from([0, 1, 2, 3, 4])
const digest = await sha256.digest(input)
value = CID.createV1(raw.code, digest)

helia = await createHeliaNode({
services: {
identify: identifyService(),
dht: kadDHT({
validators: {
ipns: ipnsValidator
},
selectors: {
ipns: ipnsSelector
},
// skips waiting for the initial self-query to find peers
allowQueryWithZeroPeers: true
})
}
})
kubo = await createKuboNode()

// find a PeerId that is KAD-closer to the resolver than the publisher when used as an IPNS key
while (true) {
if (type === 'Ed25519') {
key = await createEd25519PeerId()
} else if (type === 'secp256k1') {
key = await createSecp256k1PeerId()
} else {
key = await createRSAPeerId()
}

const [closest] = await sortClosestPeers(routingKey, [
helia.libp2p.peerId,
kubo.peer.id
])
const routingKey = uint8ArrayConcat([
uint8ArrayFromString('/ipns/'),
key.toBytes()
])

if (resolver === 'kubo' && closest.equals(kubo.peer.id)) {
break
}
const [closest] = await sortClosestPeers(routingKey, [
helia.libp2p.peerId,
kubo.peer.id
])

if (resolver === 'kubo' && closest.equals(kubo.peer.id)) {
break
}

if (resolver === 'helia' && closest.equals(helia.libp2p.peerId)) {
break
if (resolver === 'helia' && closest.equals(helia.libp2p.peerId)) {
break
}
}
}

// connect the two nodes over the KAD-DHT protocol, this should ensure
// both nodes have each other in their KAD buckets
await connect(helia, kubo, '/ipfs/lan/kad/1.0.0')
// connect the two nodes over the KAD-DHT protocol, this should ensure
// both nodes have each other in their KAD buckets
await connect(helia, kubo, '/ipfs/lan/kad/1.0.0')

await waitFor(async () => {
let found = false
await waitFor(async () => {
let found = false

for await (const event of helia.libp2p.services.dht.findPeer(kubo.peer.id)) {
if (event.name === 'FINAL_PEER') {
found = true
for await (const event of helia.libp2p.services.dht.findPeer(kubo.peer.id)) {
if (event.name === 'FINAL_PEER') {
found = true
}
}
}

return found
}, {
timeout: 30000,
delay: 1000,
message: 'Helia could not find Kubo on the DHT'
})
return found
}, {
timeout: 30000,
delay: 1000,
message: 'Helia could not find Kubo on the DHT'
})

await waitFor(async () => {
let found = false
await waitFor(async () => {
let found = false

for await (const event of kubo.api.dht.findPeer(helia.libp2p.peerId)) {
if (event.name === 'FINAL_PEER') {
found = true
for await (const event of kubo.api.dht.findPeer(helia.libp2p.peerId)) {
if (event.name === 'FINAL_PEER') {
found = true
}
}

return found
}, {
timeout: 30000,
delay: 1000,
message: 'Kubo could not find Helia on the DHT'
})

name = ipns(helia, [
dht(helia)
])
}

afterEach(async () => {
if (helia != null) {
await helia.stop()
}

return found
}, {
timeout: 30000,
delay: 1000,
message: 'Kubo could not find Helia on the DHT'
if (kubo != null) {
await kubo.stop()
}
})

name = ipns(helia, [
dht(helia)
])
}

afterEach(async () => {
if (helia != null) {
await helia.stop()
}
it(`should publish on helia and resolve on kubo using a ${type} key`, async () => {
await createNodes('kubo')

if (kubo != null) {
await kubo.stop()
}
})
const keyName = 'my-ipns-key'
await helia.libp2p.keychain.importPeer(keyName, key)

it('should publish on helia and resolve on kubo', async () => {
await createNodes('kubo')
await name.publish(key, value)

const keyName = 'my-ipns-key'
await helia.libp2p.keychain.importPeer(keyName, key)
const resolved = await last(kubo.api.name.resolve(key))

await name.publish(key, value)
if (resolved == null) {
throw new Error('kubo failed to resolve name')
}

const resolved = await last(kubo.api.name.resolve(key))
expect(resolved).to.equal(`/ipfs/${value.toString()}`)
})

if (resolved == null) {
throw new Error('kubo failed to resolve name')
}
it('should publish on kubo and resolve on helia', async function () {
if (isElectronMain) {
// electron main does not have fetch, FormData or Blob APIs
// can revisit when kubo-rpc-client supports the key.import API
return this.skip()
}

expect(resolved).to.equal(`/ipfs/${value.toString()}`)
})
if (type === 'secp256k1') {
// Kubo cannot import secp256k1 keys
return this.skip()
}

it('should publish on kubo and resolve on helia', async function () {
if (isElectronMain) {
// electron main does not have fetch, FormData or Blob APIs
// can revisit when kubo-rpc-client supports the key.import API
return this.skip()
}
await createNodes('helia')

await createNodes('helia')
const keyName = 'my-ipns-key'
const { cid } = await kubo.api.add(Uint8Array.from([0, 1, 2, 3, 4]))

const keyName = 'my-ipns-key'
const { cid } = await kubo.api.add(Uint8Array.from([0, 1, 2, 3, 4]))
// ensure the key is in the kubo keychain so we can use it to publish the IPNS record
const body = new FormData()
body.append('key', new Blob([key.privateKey ?? new Uint8Array(0)]))

// ensure the key is in the kubo keychain so we can use it to publish the IPNS record
const body = new FormData()
body.append('key', new Blob([key.privateKey ?? new Uint8Array(0)]))
// can't use the kubo-rpc-api for this call yet
const response = await fetch(`http://${kubo.api.apiHost}:${kubo.api.apiPort}/api/v0/key/import?arg=${keyName}`, {
method: 'POST',
body
})

// can't use the kubo-rpc-api for this call yet
const response = await fetch(`http://${kubo.api.apiHost}:${kubo.api.apiPort}/api/v0/key/import?arg=${keyName}`, {
method: 'POST',
body
})
expect(response).to.have.property('status', 200)

expect(response).to.have.property('status', 200)
await kubo.api.name.publish(cid, {
key: keyName
})

await kubo.api.name.publish(cid, {
key: keyName
const resolvedCid = await name.resolve(key)
expect(resolvedCid.toString()).to.equal(cid.toString())
})

const resolvedCid = await name.resolve(key)
expect(resolvedCid.toString()).to.equal(cid.toString())
})
})
7 changes: 7 additions & 0 deletions packages/interop/test/fixtures/key-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { PeerIdType } from '@libp2p/interface-peer-id'

export const keyTypes: PeerIdType[] = [
'Ed25519',
'secp256k1',
'RSA'
]
Loading

0 comments on commit 312381c

Please sign in to comment.