|
| 1 | +import { type ContentRouting } from '@libp2p/interface/content-routing' |
| 2 | +import { CodeError } from '@libp2p/interface/errors' |
| 3 | +import { type PeerRouting } from '@libp2p/interface/peer-routing' |
| 4 | +import { peerIdFromBytes } from '@libp2p/peer-id' |
| 5 | +import { marshal, unmarshal } from 'ipns' |
| 6 | +import first from 'it-first' |
| 7 | +import map from 'it-map' |
| 8 | +import { equals as uint8ArrayEquals } from 'uint8arrays/equals' |
| 9 | +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' |
| 10 | +import type { DelegatedRoutingV1HttpApiClient } from './index.js' |
| 11 | +import type { AbortOptions } from '@libp2p/interface' |
| 12 | +import type { PeerId } from '@libp2p/interface/peer-id' |
| 13 | +import type { PeerInfo } from '@libp2p/interface/peer-info' |
| 14 | +import type { CID } from 'multiformats/cid' |
| 15 | + |
| 16 | +const IPNS_PREFIX = uint8ArrayFromString('/ipns/') |
| 17 | + |
| 18 | +function isIPNSKey (key: Uint8Array): boolean { |
| 19 | + return uint8ArrayEquals(key.subarray(0, IPNS_PREFIX.byteLength), IPNS_PREFIX) |
| 20 | +} |
| 21 | + |
| 22 | +const peerIdFromRoutingKey = (key: Uint8Array): PeerId => { |
| 23 | + return peerIdFromBytes(key.slice(IPNS_PREFIX.length)) |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Wrapper class to convert [http-routing-v1 content events](https://specs.ipfs.tech/routing/http-routing-v1/#response-body) into returned values |
| 28 | + */ |
| 29 | +export class DelegatedRoutingV1HttpApiClientContentRouting implements ContentRouting { |
| 30 | + private readonly client: DelegatedRoutingV1HttpApiClient |
| 31 | + |
| 32 | + constructor (client: DelegatedRoutingV1HttpApiClient) { |
| 33 | + this.client = client |
| 34 | + } |
| 35 | + |
| 36 | + async * findProviders (cid: CID, options: AbortOptions = {}): AsyncIterable<PeerInfo> { |
| 37 | + yield * map(this.client.getProviders(cid, options), (record) => { |
| 38 | + return { |
| 39 | + id: record.ID, |
| 40 | + multiaddrs: record.Addrs ?? [], |
| 41 | + protocols: [] |
| 42 | + } |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + async provide (): Promise<void> { |
| 47 | + // noop |
| 48 | + } |
| 49 | + |
| 50 | + async put (key: Uint8Array, value: Uint8Array, options?: AbortOptions): Promise<void> { |
| 51 | + if (!isIPNSKey(key)) { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + const peerId = peerIdFromRoutingKey(key) |
| 56 | + const record = unmarshal(value) |
| 57 | + |
| 58 | + await this.client.putIPNS(peerId, record, options) |
| 59 | + } |
| 60 | + |
| 61 | + async get (key: Uint8Array, options?: AbortOptions): Promise<Uint8Array> { |
| 62 | + if (!isIPNSKey(key)) { |
| 63 | + throw new CodeError('Not found', 'ERR_NOT_FOUND') |
| 64 | + } |
| 65 | + |
| 66 | + const peerId = peerIdFromRoutingKey(key) |
| 67 | + |
| 68 | + try { |
| 69 | + const record = await this.client.getIPNS(peerId, options) |
| 70 | + |
| 71 | + return marshal(record) |
| 72 | + } catch (err: any) { |
| 73 | + // ERR_BAD_RESPONSE is thrown when the response had no body, which means |
| 74 | + // the record couldn't be found |
| 75 | + if (err.code === 'ERR_BAD_RESPONSE') { |
| 76 | + throw new CodeError('Not found', 'ERR_NOT_FOUND') |
| 77 | + } |
| 78 | + |
| 79 | + throw err |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Wrapper class to convert [http-routing-v1](https://specs.ipfs.tech/routing/http-routing-v1/#response-body-0) events into expected libp2p values |
| 86 | + */ |
| 87 | +export class DelegatedRoutingV1HttpApiClientPeerRouting implements PeerRouting { |
| 88 | + private readonly client: DelegatedRoutingV1HttpApiClient |
| 89 | + |
| 90 | + constructor (client: DelegatedRoutingV1HttpApiClient) { |
| 91 | + this.client = client |
| 92 | + } |
| 93 | + |
| 94 | + async findPeer (peerId: PeerId, options: AbortOptions = {}): Promise<PeerInfo> { |
| 95 | + const peer = await first(this.client.getPeers(peerId, options)) |
| 96 | + |
| 97 | + if (peer != null) { |
| 98 | + return { |
| 99 | + id: peer.ID, |
| 100 | + multiaddrs: peer.Addrs, |
| 101 | + protocols: [] |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + throw new CodeError('Not found', 'ERR_NOT_FOUND') |
| 106 | + } |
| 107 | + |
| 108 | + async * getClosestPeers (key: Uint8Array, options: AbortOptions = {}): AsyncIterable<PeerInfo> { |
| 109 | + // noop |
| 110 | + } |
| 111 | +} |
0 commit comments