-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for libp2p ContentRouting and PeerRouting #44
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7311bc7
feat: add support for libp2p ContentRouting and PeerRouting
achingbrain bb8e8b8
Merge remote-tracking branch 'origin/main' into feat/add-libp2p-routings
achingbrain c81a4db
chore: apply suggestions from code review
achingbrain 859da87
chore: add assertions and fix linting
achingbrain 88eb8d9
chore: apply suggestions from code review
achingbrain d399c7e
chore: update docs and deps
achingbrain 236b0a0
chore: update readme
achingbrain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { type ContentRouting } from '@libp2p/interface/content-routing' | ||
import { CodeError } from '@libp2p/interface/errors' | ||
import { type PeerRouting } from '@libp2p/interface/peer-routing' | ||
import { peerIdFromBytes } from '@libp2p/peer-id' | ||
import { marshal, unmarshal } from 'ipns' | ||
import first from 'it-first' | ||
import map from 'it-map' | ||
import { equals as uint8ArrayEquals } from 'uint8arrays/equals' | ||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
import type { DelegatedRoutingV1HttpApiClient } from './index.js' | ||
import type { AbortOptions } from '@libp2p/interface' | ||
import type { PeerId } from '@libp2p/interface/peer-id' | ||
import type { PeerInfo } from '@libp2p/interface/peer-info' | ||
import type { CID } from 'multiformats/cid' | ||
|
||
const IPNS_PREFIX = uint8ArrayFromString('/ipns/') | ||
|
||
function isIPNSKey (key: Uint8Array): boolean { | ||
return uint8ArrayEquals(key.subarray(0, IPNS_PREFIX.byteLength), IPNS_PREFIX) | ||
} | ||
|
||
const peerIdFromRoutingKey = (key: Uint8Array): PeerId => { | ||
return peerIdFromBytes(key.slice(IPNS_PREFIX.length)) | ||
} | ||
|
||
/** | ||
* Wrapper class to convert [http-routing-v1 content events](https://specs.ipfs.tech/routing/http-routing-v1/#response-body) into returned values | ||
*/ | ||
export class DelegatedRoutingV1HttpApiClientContentRouting implements ContentRouting { | ||
private readonly client: DelegatedRoutingV1HttpApiClient | ||
|
||
constructor (client: DelegatedRoutingV1HttpApiClient) { | ||
this.client = client | ||
} | ||
|
||
async * findProviders (cid: CID, options: AbortOptions = {}): AsyncIterable<PeerInfo> { | ||
yield * map(this.client.getProviders(cid, options), (record) => { | ||
return { | ||
id: record.ID, | ||
multiaddrs: record.Addrs ?? [], | ||
protocols: [] | ||
} | ||
}) | ||
} | ||
|
||
async provide (): Promise<void> { | ||
// noop | ||
} | ||
|
||
async put (key: Uint8Array, value: Uint8Array, options?: AbortOptions): Promise<void> { | ||
if (!isIPNSKey(key)) { | ||
return | ||
} | ||
|
||
const peerId = peerIdFromRoutingKey(key) | ||
const record = unmarshal(value) | ||
|
||
await this.client.putIPNS(peerId, record, options) | ||
} | ||
|
||
async get (key: Uint8Array, options?: AbortOptions): Promise<Uint8Array> { | ||
if (!isIPNSKey(key)) { | ||
throw new CodeError('Not found', 'ERR_NOT_FOUND') | ||
} | ||
|
||
const peerId = peerIdFromRoutingKey(key) | ||
|
||
try { | ||
const record = await this.client.getIPNS(peerId, options) | ||
|
||
return marshal(record) | ||
} catch (err: any) { | ||
// ERR_BAD_RESPONSE is thrown when the response had no body, which means | ||
// the record couldn't be found | ||
if (err.code === 'ERR_BAD_RESPONSE') { | ||
throw new CodeError('Not found', 'ERR_NOT_FOUND') | ||
} | ||
|
||
throw err | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Wrapper class to convert [http-routing-v1](https://specs.ipfs.tech/routing/http-routing-v1/#response-body-0) events into expected libp2p values | ||
*/ | ||
export class DelegatedRoutingV1HttpApiClientPeerRouting implements PeerRouting { | ||
private readonly client: DelegatedRoutingV1HttpApiClient | ||
|
||
constructor (client: DelegatedRoutingV1HttpApiClient) { | ||
this.client = client | ||
} | ||
|
||
async findPeer (peerId: PeerId, options: AbortOptions = {}): Promise<PeerInfo> { | ||
const peer = await first(this.client.getPeers(peerId, options)) | ||
|
||
if (peer != null) { | ||
return { | ||
id: peer.ID, | ||
multiaddrs: peer.Addrs, | ||
protocols: [] | ||
} | ||
} | ||
|
||
throw new CodeError('Not found', 'ERR_NOT_FOUND') | ||
} | ||
|
||
async * getClosestPeers (key: Uint8Array, options: AbortOptions = {}): AsyncIterable<PeerInfo> { | ||
// noop | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for supporting strings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general if we can push validation of an argument type out of a consumer it's a good thing. Otherwise this can snowball quickly and we have to guess what the contents of a string are, usually at several points in a module if we have multiple entry points.
We suffered from this previously with "what's a CID?" is it "QmFoo", is it "/ipfs/QMFoo", etc. Pushing all that into the
CID
class vastly simplified the codebase.Of course for a user it's nicer to be able to pass
'http://example.org'
instead ofnew URL('http://example.org')
so it's probably worth the tradeoff for thecreate...
factory functions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be a straightforward one where it will fail quickly if an invalid string is passed.