Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/brow-2-brow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@
"start:relay": "node dist/src/relay.mjs"
},
"dependencies": {
"@chainsafe/libp2p-noise": "^15.1.0",
"@chainsafe/libp2p-noise": "^16.1.3",
"@chainsafe/libp2p-yamux": "^6.0.2",
"@libp2p/autonat": "^1.1.1",
"@libp2p/bootstrap": "^10.1.1",
"@libp2p/circuit-relay-v2": "^1.1.1",
"@libp2p/autonat": "^2.0.30",
"@libp2p/bootstrap": "^11.0.35",
"@libp2p/circuit-relay-v2": "^3.2.11",
"@libp2p/crypto": "^5.1.1",
"@libp2p/identify": "^2.1.1",
"@libp2p/identify": "^3.0.29",
"@libp2p/interface": "^2.9.0",
"@libp2p/peer-id": "^5.1.2",
"@libp2p/tcp": "^9.1.1",
"@libp2p/webrtc": "^4.1.1",
"@libp2p/websockets": "^8.1.1",
"@libp2p/webtransport": "^4.1.1",
"@libp2p/tcp": "^10.1.10",
"@libp2p/webrtc": "^5.2.12",
"@libp2p/websockets": "^9.2.10",
"@libp2p/webtransport": "^5.0.40",
"@multiformats/multiaddr": "^12.3.0",
"@multiformats/multiaddr-matcher": "^1.2.4",
"@ts-bridge/cli": "^0.6.3",
"@ts-bridge/shims": "^0.1.1",
"@types/web": "^0",
"it-byte-stream": "^2.0.1",
"libp2p": "^1.8.0",
"libp2p": "^2.8.5",
"uint8arrays": "^5.1.0"
},
"devDependencies": {
Expand Down
37 changes: 22 additions & 15 deletions packages/brow-2-brow/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import { yamux } from '@chainsafe/libp2p-yamux';
import { bootstrap } from '@libp2p/bootstrap';
import { circuitRelayTransport } from '@libp2p/circuit-relay-v2';
import { identify } from '@libp2p/identify';
import type { PeerId, Connection, Libp2pEvents } from '@libp2p/interface';
import type {
PeerId,
Connection,
Libp2p,
Libp2pEvents,
} from '@libp2p/interface';
import { peerIdFromPrivateKey } from '@libp2p/peer-id';
import { webRTC } from '@libp2p/webrtc';
import { webSockets } from '@libp2p/websockets';
import * as filters from '@libp2p/websockets/filters';
import { webTransport } from '@libp2p/webtransport';
import { multiaddr } from '@multiformats/multiaddr';
import '@types/web';
Expand All @@ -15,7 +20,7 @@ import { byteStream } from 'it-byte-stream';
import { createLibp2p } from 'libp2p';
import { toString as bufToString, fromString } from 'uint8arrays';

import { generatePeerId } from './key-manglage.ts';
import { generateKeyPair } from './key-manglage.ts';
import { update, getPeerTypes, getAddresses, getPeerDetails } from './utils.ts';

/* eslint-disable n/no-unsupported-features/node-builtins */
Expand All @@ -29,15 +34,18 @@ type Channel = {
};

declare global {
let libp2p: unknown;
// TypeScript requires you to use `var` here for this to work.
// eslint-disable-next-line no-var
var libp2p: Libp2p;
}

const App = async (): Promise<void> => {
const peerIdList: (PeerId | undefined)[] = []; // id -> peerID
const idMap = new Map<string, number>(); // peerID string -> id
peerIdList[0] = undefined;
for (let i = 1; i < 256; ++i) {
const peerId = await generatePeerId(i);
const keyPair = await generateKeyPair(i);
const peerId = peerIdFromPrivateKey(keyPair);
peerIdList[i] = peerId;
idMap.set(peerId.toString(), i);
}
Expand All @@ -57,30 +65,29 @@ const App = async (): Promise<void> => {
console.log(`I am id:${localId} peerId:${peerId.toString()}`);

const relayPeerId = peerIdList[RELAY_ID];
if (!relayPeerId) {
throw Error(`relay peer ID is undefined`);
}
const privateKey = await generateKeyPair(RELAY_ID);
const relayAddr = `${RELAY_HOST}/tcp/9001/ws/p2p/${relayPeerId.toString()}`;
// const relayAddr = `${RELAY_HOST}/tcp/9001`;

const libp2p = await createLibp2p({
peerId,
privateKey,
addresses: {
listen: [
// 👇 Listen for webRTC connection
'/webrtc',
],
},
transports: [
webSockets({
// Allow all WebSocket connections inclusing without TLS
filter: filters.all,
}),
webSockets(),
webTransport(),
webRTC(),
// 👇 Required to create circuit relay reservations in order to hole punch browser-to-browser WebRTC connections
circuitRelayTransport({
discoverRelays: 1,
}),
circuitRelayTransport(),
],
connectionEncryption: [noise()],
connectionEncrypters: [noise()],
streamMuxers: [yamux()],
connectionGater: {
// Allow private addresses for local testing
Expand Down Expand Up @@ -292,7 +299,7 @@ const App = async (): Promise<void> => {
await libp2p.dial(maddr);
} catch (problem) {
outputLine(
`error connecting to ${maddr.toString()}: ${problem.toString()}`,
`error connecting to ${maddr.toString()}: ${(problem as Error).toString()}`,
);
}
};
Expand Down
24 changes: 8 additions & 16 deletions packages/brow-2-brow/src/key-manglage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {
generateKeyPairFromSeed,
marshalPrivateKey,
marshalPublicKey,
} from '@libp2p/crypto/keys';
import type { PeerId } from '@libp2p/interface';
import { peerIdFromKeys } from '@libp2p/peer-id';
import { generateKeyPairFromSeed } from '@libp2p/crypto/keys';
import type { PrivateKey } from '@libp2p/interface';

/**
* Convert a Uint8Array into a hex string.
Expand Down Expand Up @@ -51,13 +46,13 @@ export function fromHex(str: string): Uint8Array {
// seed: 5 peerId: 12D3KooWSHj3RRbBjD15g6wekV8y3mm57Pobmps2g2WJm6F67Lay

/**
* Generate the peerID for a given localID.
* Generate the private key for a given localID.
*
* @param localId - The localID whose peerID is sought.
*
* @returns the peerID for `localID`.
* @returns the private key for `localID`.
*/
export async function generatePeerId(localId: number): Promise<PeerId> {
export async function generateKeyPair(localId: number): Promise<PrivateKey> {
let seed;
if (localId > 0 && localId < 256) {
seed = new Uint8Array(32);
Expand All @@ -66,10 +61,7 @@ export async function generatePeerId(localId: number): Promise<PeerId> {
// eslint-disable-next-line n/no-unsupported-features/node-builtins
seed = globalThis.crypto.getRandomValues(new Uint8Array(32));
}
const keyPair = await generateKeyPairFromSeed('Ed25519', seed);
const peerId = peerIdFromKeys(
marshalPublicKey(keyPair.public),
marshalPrivateKey(keyPair),
);
return peerId;
return await generateKeyPairFromSeed('Ed25519', seed);
// const peerId = peerIdFromPrivateKey(keyPair);
// return peerId;
}
11 changes: 4 additions & 7 deletions packages/brow-2-brow/src/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@ import { yamux } from '@chainsafe/libp2p-yamux';
import { autoNAT } from '@libp2p/autonat';
import { circuitRelayServer } from '@libp2p/circuit-relay-v2';
import { identify } from '@libp2p/identify';
import type { PeerId } from '@libp2p/interface';
import { tcp } from '@libp2p/tcp';
import { webSockets } from '@libp2p/websockets';
import { createLibp2p } from 'libp2p';

import { generatePeerId } from './key-manglage.ts';
import { generateKeyPair } from './key-manglage.ts';

const RELAY_LOCAL_ID = 200;

/**
* Main.
*/
async function main(): Promise<void> {
const peerId: PeerId = (await generatePeerId(
RELAY_LOCAL_ID,
)) as unknown as PeerId;
const privateKey = await generateKeyPair(RELAY_LOCAL_ID);
const libp2p = await createLibp2p({
peerId,
privateKey,
addresses: {
listen: ['/ip4/0.0.0.0/tcp/9001/ws', '/ip4/0.0.0.0/tcp/9002'],
},
transports: [webSockets(), tcp()],
connectionEncryption: [noise()],
connectionEncrypters: [noise()],
streamMuxers: [yamux()],
connectionGater: {
// Allow private addresses for local testing
Expand Down
Loading
Loading