Compare commits
7 Commits
f0c19a7bb4
...
ff754773e6
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | ff754773e6 | |
Derrick Hammer | 3f3e581ce2 | |
Derrick Hammer | 861a8229d4 | |
Derrick Hammer | c29f2422a2 | |
Derrick Hammer | 2b3b6dd0f3 | |
Derrick Hammer | 483ac62f96 | |
Derrick Hammer | 1a544d7c2d |
File diff suppressed because it is too large
Load Diff
16
package.json
16
package.json
|
@ -31,8 +31,9 @@
|
|||
"blockstore-idb": "^1.1.1",
|
||||
"datastore-core": "^9.2.0",
|
||||
"datastore-idb": "^2.1.2",
|
||||
"helia": "1.3.5",
|
||||
"helia": "^2.0.3-43932a5",
|
||||
"ipfs-http-client": "^60.0.1",
|
||||
"ipns": "^7.0.1",
|
||||
"multiformats": "^12.0.1",
|
||||
"p-defer": "^4.0.0",
|
||||
"path-browserify": "^1.0.1",
|
||||
|
@ -46,12 +47,13 @@
|
|||
"rollup-plugin-ignore-import": "^1.3.2"
|
||||
},
|
||||
"overrides": {
|
||||
"libp2p": "^0.45",
|
||||
"@libp2p/kad-dht": "^9.3.8",
|
||||
"libp2p": "^0.46.6",
|
||||
"@libp2p/kad-dht": "^10.0.2",
|
||||
"@chainsafe/libp2p-noise": "^12.0.0",
|
||||
"@libp2p/mplex": "^8.0.3",
|
||||
"@libp2p/bootstrap": "^8.0.0",
|
||||
"@chainsafe/libp2p-gossipsub": "^8.0.0",
|
||||
"@libp2p/interface-datastore": "^8.0.0"
|
||||
"@libp2p/mplex": "^9.0.2",
|
||||
"@libp2p/bootstrap": "^9.0.2",
|
||||
"@chainsafe/libp2p-gossipsub": "^10.0.0",
|
||||
"@libp2p/interface-datastore": "^8.0.0",
|
||||
"@libp2p/interface-blockstore": "^5.0.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import type { BootstrapInit } from "@libp2p/bootstrap";
|
||||
|
||||
export const bootstrapConfig = {
|
||||
list: [
|
||||
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
|
||||
|
@ -54,4 +56,4 @@ export const bootstrapConfig = {
|
|||
"/ip6/2a01:4ff:f0:3764::1/tcp/4001/p2p/12D3KooWSDj6JM2JmoHwE9AUUwqAFUEg9ndd3pMA8aF2bkYckZfo",
|
||||
"/ip6/2a01:4ff:f0:3764::1/udp/4001/quic/p2p/12D3KooWSDj6JM2JmoHwE9AUUwqAFUEg9ndd3pMA8aF2bkYckZfo",
|
||||
],
|
||||
};
|
||||
} as BootstrapInit;
|
||||
|
|
|
@ -18,7 +18,14 @@ import { reframeContentRouting } from "@libp2p/reframe-content-routing";
|
|||
import { multiaddr } from "@multiformats/multiaddr";
|
||||
import { create as createIpfsHttpClient } from "ipfs-http-client";
|
||||
import { delegatedContentRouting } from "@libp2p/delegated-content-routing";
|
||||
import { delegatedPeerRouting } from "@libp2p/delegated-peer-routing";
|
||||
import { circuitRelayTransport } from "libp2p/circuit-relay";
|
||||
import { DELEGATE_LIST } from "./constants.js";
|
||||
import { webSockets } from "@libp2p/websockets";
|
||||
import { webTransport } from "@libp2p/webtransport";
|
||||
import { dcutrService } from "libp2p/dcutr";
|
||||
// @ts-ignore
|
||||
import type { DefaultLibp2pServices } from "helia/dist/src/utils/libp2p-defaults.browser.js";
|
||||
|
||||
function getDelegateConfig(): any {
|
||||
const delegateString =
|
||||
|
@ -34,24 +41,27 @@ function getDelegateConfig(): any {
|
|||
};
|
||||
}
|
||||
|
||||
export function libp2pConfig(proxy: MultiSocketProxy): Libp2pOptions<{
|
||||
dht: DualKadDHT;
|
||||
pubsub: PubSub;
|
||||
identify: unknown;
|
||||
autoNAT: unknown;
|
||||
}> {
|
||||
export function libp2pConfig(
|
||||
proxy: MultiSocketProxy,
|
||||
): Libp2pOptions<DefaultLibp2pServices> {
|
||||
const client = createIpfsHttpClient(getDelegateConfig());
|
||||
|
||||
return {
|
||||
addresses: {
|
||||
listen: [],
|
||||
},
|
||||
transports: [hypercoreTransport({ proxy })],
|
||||
transports: [
|
||||
hypercoreTransport({ proxy }),
|
||||
circuitRelayTransport({
|
||||
discoverRelays: 1,
|
||||
}),
|
||||
webTransport(),
|
||||
webSockets(),
|
||||
],
|
||||
connectionEncryption: [noise()],
|
||||
streamMuxers: [yamux(), mplex()],
|
||||
peerDiscovery: [bootstrap(bootstrapConfig)],
|
||||
contentRouters: [
|
||||
delegatedContentRouting(client),
|
||||
ipniContentRouting("https://cid.contact"),
|
||||
reframeContentRouting("https://cid.contact/reframe"),
|
||||
],
|
||||
|
@ -59,6 +69,7 @@ export function libp2pConfig(proxy: MultiSocketProxy): Libp2pOptions<{
|
|||
identify: identifyService(),
|
||||
autoNAT: autoNATService(),
|
||||
pubsub: gossipsub(),
|
||||
dcutr: dcutrService(),
|
||||
dht: kadDHT({
|
||||
clientMode: true,
|
||||
validators: {
|
||||
|
|
48
src/index.ts
48
src/index.ts
|
@ -1,6 +1,4 @@
|
|||
import { createHelia } from "helia";
|
||||
// @ts-ignore
|
||||
import Hyperswarm from "hyperswarm";
|
||||
import { MultiSocketProxy } from "@lumeweb/libhyperproxy";
|
||||
import { UnixFS, unixfs } from "@helia/unixfs";
|
||||
import { PROTOCOL } from "./constants.js";
|
||||
|
@ -11,22 +9,22 @@ import {
|
|||
} from "@lumeweb/libkernel/module";
|
||||
import { createClient } from "@lumeweb/kernel-swarm-client";
|
||||
import { ipns, IPNS } from "@helia/ipns";
|
||||
import { dht, pubsub } from "@helia/ipns/routing";
|
||||
import { dht } from "@helia/ipns/routing";
|
||||
// @ts-ignore
|
||||
import { gossipsub } from "@chainsafe/libp2p-gossipsub";
|
||||
import { CID } from "multiformats/cid";
|
||||
import { bases } from "multiformats/basics";
|
||||
import { substr } from "runes2";
|
||||
import { MultibaseDecoder } from "multiformats";
|
||||
import { peerIdFromCID } from "@libp2p/peer-id";
|
||||
import { IDBBlockstore } from "blockstore-idb";
|
||||
import { IDBDatastore } from "datastore-idb";
|
||||
import defer from "p-defer";
|
||||
import { Helia } from "@helia/interface";
|
||||
// @ts-ignore
|
||||
import type { Components } from "libp2p/src/components.js";
|
||||
import { libp2pConfig } from "./config.js";
|
||||
import { createClient as createNetworkRegistryClient } from "@lumeweb/kernel-network-registry-client";
|
||||
import { Libp2p } from "@libp2p/interface";
|
||||
import { unmarshal as ipnsUnmarshal } from "ipns";
|
||||
|
||||
const IPFS_GATEWAY = "https://ipfs.lumeweb.com";
|
||||
|
||||
const basesByPrefix: { [prefix: string]: MultibaseDecoder<any> } = Object.keys(
|
||||
bases,
|
||||
|
@ -50,7 +48,7 @@ let swarm;
|
|||
let proxy: MultiSocketProxy;
|
||||
let fs: UnixFS;
|
||||
let IPNS: IPNS;
|
||||
let ipfs: Helia;
|
||||
let ipfs: Helia<Libp2p<any>>;
|
||||
|
||||
// @ts-ignore
|
||||
BigInt.prototype.toJSON = function () {
|
||||
|
@ -85,6 +83,7 @@ async function handlePresentKey() {
|
|||
|
||||
await blockstore.open();
|
||||
await datastore.open();
|
||||
// @ts-ignore
|
||||
ipfs = await createHelia({
|
||||
blockstore,
|
||||
// @ts-ignore
|
||||
|
@ -106,7 +105,7 @@ async function handlePresentKey() {
|
|||
await swarm.ready();
|
||||
// @ts-ignore
|
||||
fs = unixfs(ipfs);
|
||||
IPNS = ipns(ipfs as any, [dht(ipfs), pubsub(ipfs as any)]);
|
||||
IPNS = ipns(ipfs as any, [dht(ipfs)]);
|
||||
|
||||
ipfs.libp2p.addEventListener("peer:connect", () => {
|
||||
if (ipfs.libp2p.getPeers().length > 0) {
|
||||
|
@ -238,9 +237,22 @@ async function handleCat(aq: ActiveQuery) {
|
|||
aq.respond();
|
||||
}
|
||||
|
||||
function toDNSPrefix(cid: CID) {
|
||||
const cidb32 = cid.toV1().toString(bases.base32);
|
||||
if (cidb32.length <= 63) {
|
||||
return cidb32;
|
||||
}
|
||||
const cidb36 = cid.toV1().toString(bases.base36);
|
||||
if (cidb36.length <= 63) {
|
||||
return cidb36;
|
||||
}
|
||||
throw new Error("CID incompatible with DNS label length limit of 63");
|
||||
}
|
||||
|
||||
async function handleIpnsResolve(aq: ActiveQuery) {
|
||||
await ready();
|
||||
|
||||
/*
|
||||
await activeIpfsPeersDefer.promise;
|
||||
|
||||
if (ipfs.libp2p.getPeers().length === 0) {
|
||||
|
@ -248,6 +260,7 @@ async function handleIpnsResolve(aq: ActiveQuery) {
|
|||
}
|
||||
|
||||
await activeIpfsPeersDefer.promise;
|
||||
*/
|
||||
|
||||
if (!aq.callerInput || !("cid" in aq.callerInput)) {
|
||||
aq.reject("cid required");
|
||||
|
@ -255,14 +268,17 @@ async function handleIpnsResolve(aq: ActiveQuery) {
|
|||
}
|
||||
|
||||
try {
|
||||
return aq.respond(
|
||||
(
|
||||
await IPNS.resolve(
|
||||
peerIdFromCID(getCID(aq.callerInput.cid)),
|
||||
aq.callerInput?.options,
|
||||
)
|
||||
).asCID.toString(),
|
||||
const cid = getCID(aq.callerInput.cid);
|
||||
|
||||
const ret = await fetch(
|
||||
`${IPFS_GATEWAY}/ipns/${toDNSPrefix(cid)}?format=ipns-record`,
|
||||
);
|
||||
|
||||
const buf = await ret.arrayBuffer();
|
||||
|
||||
const record = ipnsUnmarshal(new Uint8Array(buf));
|
||||
|
||||
return aq.respond(record.value);
|
||||
} catch (e: any) {
|
||||
aq.reject((e as Error).message);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue