Skip to content
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

add browser to browser #22

Merged
merged 9 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3,399 changes: 2,450 additions & 949 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
"@libp2p/delegated-peer-routing": "^4.0.0",
"@libp2p/kad-dht": "^7.0.0",
"@libp2p/peer-id": "^2.0.1",
"@libp2p/webrtc": "^1.0.4",
"@libp2p/webrtc": "^1.1.0" ,
"@libp2p/websockets": "^5.0.3",
"@libp2p/webtransport": "^1.0.7",
"@multiformats/mafmt": "^12.0.0",
"@multiformats/multiaddr": "^12.0.0",
"@types/node": "18.14.6",
"@types/react": "18.0.28",
"@types/react-dom": "18.0.11",
Expand All @@ -30,7 +32,7 @@
"eslint": "8.35.0",
"eslint-config-next": "13.2.3",
"kubo-rpc-client": "^3.0.1",
"libp2p": "^0.42.2",
"libp2p": "^0.43.0",
"next": "13.2.3",
"private-ip": "^3.0.0",
"react": "18.2.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/frontend/src/context/ctx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ export const libp2pContext = createContext<Libp2pContextInterface>({
interface WrapperProps {
children?: ReactNode
}

let loaded = false
export function AppWrapper({ children }: WrapperProps) {
const [libp2p, setLibp2p] = useState<Libp2p>()

useEffect(() => {
const init = async () => {
if (loaded) return
try {
loaded = true
const libp2p = await startLibp2p()

// @ts-ignore
Expand Down
3 changes: 3 additions & 0 deletions packages/frontend/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ export const CHAT_TOPIC = "universal-connectivity"

// export const DEFAULT_APP_PEER = '12D3KooWBdmLJjhpgJ9KZgLM3f894ff9xyBfPvPjFNn7MKJpyrC2'
export const DEFAULT_APP_PEER = '12D3KooWRBy97UB99e3J6hiPesre1MZeuNQvfan4gBziswrRJsNK'


export const CIRCUIT_RELAY_CODE = 290
48 changes: 43 additions & 5 deletions packages/frontend/src/lib/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ import { create as KuboClient } from 'kubo-rpc-client'
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
import { webSockets } from '@libp2p/websockets'
import { webTransport } from '@libp2p/webtransport'
import { webRTC } from '@libp2p/webrtc'
import { webRTC, webRTCDirect } from '@libp2p/webrtc'
import { PeerId } from 'kubo-rpc-client/dist/src/types'
import { CHAT_TOPIC } from './constants'
import { CHAT_TOPIC, CIRCUIT_RELAY_CODE } from './constants'
import * as filters from "@libp2p/websockets/filters"

// @ts-ignore
import { circuitRelayTransport } from 'libp2p/circuit-relay'


export async function startLibp2p(options: {} = {}) {
// localStorage.debug = 'libp2p*,-*:trace'
// application-specific data lives in the datastore
// const datastore = new MemoryDatastore()
const datastore = new LevelDatastore('js-libp2p-nextjs-example')
// const datastore = new LevelDatastore('js-libp2p-nextjs-example')

// default is to use ipfs.io
const client = KuboClient({
Expand All @@ -42,8 +47,23 @@ export async function startLibp2p(options: {} = {}) {
// libp2p is the networking layer that underpins Helia
const libp2p = await createLibp2p({
// dht: kadDHT(),
datastore,
transports: [webTransport(), webSockets(), webRTC()],
// datastore,
transports: [webTransport(), webSockets({
filter: filters.all,
}), webRTC({
rtcConfiguration: {
iceServers:[
{
urls: [
'stun:stun.l.google.com:19302',
'stun:global.stun.twilio.com:3478'
]
}
]
}
}), webRTCDirect(), circuitRelayTransport({
discoverRelays: 10,
}),],
// transports: [webRTC()],
connectionEncryption: [noise()],
streamMuxers: [yamux()],
Expand Down Expand Up @@ -83,6 +103,12 @@ export async function startLibp2p(options: {} = {}) {

libp2p.pubsub.subscribe(CHAT_TOPIC)

libp2p.peerStore.addEventListener('change:multiaddrs', ({detail: {peerId, multiaddrs}}) => {

console.log(`changed multiaddrs: peer ${peerId.toString()} multiaddrs: ${multiaddrs}`)
setWebRTCRelayAddress(multiaddrs, libp2p.peerId.toString())
})

console.log(`this nodes peerID: ${libp2p.peerId.toString()}`)

return libp2p
Expand Down Expand Up @@ -244,3 +270,15 @@ export class Libp2pDialError extends Error {
this.error = error
}
}

export const setWebRTCRelayAddress = (maddrs: Multiaddr[], peerId: string) => {
maddrs.forEach((maddr) => {
if (maddr.protoCodes().includes(CIRCUIT_RELAY_CODE)) {

const webRTCrelayAddress = multiaddr(maddr.toString() + '/webrtc/p2p/' + peerId)

console.log(`Listening on '${webRTCrelayAddress.toString()}'`)
}
})
}

26 changes: 14 additions & 12 deletions packages/frontend/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ export default function Home() {
return await libp2p.getConnections()
}

const ping = async () => {
if (maddr) {
return libp2p.ping(multiaddr(maddr))
}
}
// const ping = async () => {
// if (maddr) {
// return libp2p.ping(multiaddr(maddr))
// }
// }

ping()
.then((lat) => {
setLatency(lat)
})
.catch((e) => {
console.error(e, e?.error)
})
// ping()
// .then((lat) => {
// setLatency(lat)
// })
// .catch((e) => {
// console.error(e, e?.error)
// })

getConnectedPeers().then((peers) => {
// If one of the connected peers matches the one in input we're connected
Expand Down Expand Up @@ -93,6 +93,7 @@ export default function Home() {
const addrs = await getPeerMultiaddrs(libp2p)(peerID)

setMultiaddrs(addrs)

} catch (e) {
console.error(e)
}
Expand Down Expand Up @@ -143,6 +144,7 @@ export default function Home() {
try {
const connection = await connectToMultiaddr(libp2p)(multiaddr(maddr))
console.log('connection: ', connection)

return connection
} catch (e) {
console.error(e)
Expand Down
Loading