generated from orbitdb/repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from orbitdb/tests
Add more tests
- Loading branch information
Showing
7 changed files
with
228 additions
and
11 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { multiaddr } from '@multiformats/multiaddr' | ||
import { WebRTC } from '@multiformats/multiaddr-matcher' | ||
import waitFor from './wait-for.js' | ||
|
||
const defaultFilter = () => true | ||
|
||
const isBrowser = () => typeof window !== 'undefined' | ||
|
||
const connectIpfsNodes = async (ipfs1, ipfs2, options = { | ||
filter: defaultFilter | ||
}) => { | ||
if (isBrowser()) { | ||
const relayId = '12D3KooWAJjbRkp8FPF5MKgMU53aUTxWkqvDrs4zc1VMbwRwfsbE' | ||
|
||
await ipfs1.libp2p.dial(multiaddr(`/ip4/127.0.0.1/tcp/12345/ws/p2p/${relayId}`)) | ||
|
||
let address1 | ||
|
||
await waitFor(() => { | ||
address1 = ipfs1.libp2p.getMultiaddrs().filter(ma => WebRTC.matches(ma)).pop() | ||
return address1 != null | ||
}, () => true) | ||
|
||
await ipfs2.libp2p.dial(address1) | ||
} else { | ||
await ipfs2.libp2p.peerStore.save(ipfs1.libp2p.peerId, { multiaddrs: ipfs1.libp2p.getMultiaddrs().filter(options.filter) }) | ||
await ipfs2.libp2p.dial(ipfs1.libp2p.peerId) | ||
} | ||
} | ||
|
||
export default connectIpfsNodes |
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,56 @@ | ||
import { yamux } from '@chainsafe/libp2p-yamux' | ||
import { createLibp2p } from 'libp2p' | ||
import { noise } from '@chainsafe/libp2p-noise' | ||
import { circuitRelayServer } from '@libp2p/circuit-relay-v2' | ||
import { webSockets } from '@libp2p/websockets' | ||
import * as filters from '@libp2p/websockets/filters' | ||
import { identify } from '@libp2p/identify' | ||
import { createFromPrivKey } from '@libp2p/peer-id-factory' | ||
import { unmarshalPrivateKey } from '@libp2p/crypto/keys' | ||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
|
||
// output of: console.log(server.peerId.privateKey.toString('hex')) | ||
const relayPrivKey = '08011240821cb6bc3d4547fcccb513e82e4d718089f8a166b23ffcd4a436754b6b0774cf07447d1693cd10ce11ef950d7517bad6e9472b41a927cd17fc3fb23f8c70cd99' | ||
// the peer id of the above key | ||
// const relayId = '12D3KooWAJjbRkp8FPF5MKgMU53aUTxWkqvDrs4zc1VMbwRwfsbE' | ||
|
||
const encoded = uint8ArrayFromString(relayPrivKey, 'hex') | ||
const privateKey = await unmarshalPrivateKey(encoded) | ||
const peerId = await createFromPrivKey(privateKey) | ||
|
||
const server = await createLibp2p({ | ||
peerId, | ||
addresses: { | ||
listen: ['/ip4/0.0.0.0/tcp/12345/ws'] | ||
}, | ||
transports: [ | ||
webSockets({ | ||
filter: filters.all | ||
}) | ||
], | ||
connectionEncryption: [noise()], | ||
streamMuxers: [yamux()], | ||
services: { | ||
identify: identify(), | ||
relay: circuitRelayServer({ | ||
reservations: { | ||
maxReservations: 5000, | ||
reservationTtl: 1000, | ||
defaultDataLimit: BigInt(1024 * 1024 * 1024) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
server.addEventListener('peer:connect', async event => { | ||
console.log('peer:connect', event.detail) | ||
}) | ||
|
||
server.addEventListener('peer:disconnect', async event => { | ||
console.log('peer:disconnect', event.detail) | ||
server.peerStore.delete(event.detail) | ||
}) | ||
|
||
console.log(server.peerId.toString()) | ||
console.log('p2p addr: ', server.getMultiaddrs().map((ma) => ma.toString())) | ||
// generates a deterministic address: /ip4/127.0.0.1/tcp/33519/ws/p2p/12D3KooWAJjbRkp8FPF5MKgMU53aUTxWkqvDrs4zc1VMbwRwfsbE |
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,12 @@ | ||
const waitFor = async (valueA, toBeValueB, pollInterval = 100) => { | ||
return new Promise((resolve) => { | ||
const interval = setInterval(async () => { | ||
if (await valueA() === await toBeValueB()) { | ||
clearInterval(interval) | ||
resolve() | ||
} | ||
}, pollInterval) | ||
}) | ||
} | ||
|
||
export default waitFor |