mirror of
https://github.com/waku-org/js-waku.git
synced 2025-01-12 13:34:48 +00:00
Merge pull request #149 from status-im/dial-usage
This commit is contained in:
commit
27b08ff8b6
@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Enable passing `string`s to `addPeerToAddressBook`.
|
||||||
|
- Use `addPeerToAddressBook` in examples and usage doc.
|
||||||
|
|
||||||
## [0.1.0] - 2021-05-12
|
## [0.1.0] - 2021-05-12
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
12
README.md
12
README.md
@ -24,11 +24,13 @@ Connect to a new peer:
|
|||||||
import { multiaddr } from 'multiaddr';
|
import { multiaddr } from 'multiaddr';
|
||||||
import PeerId from 'peer-id';
|
import PeerId from 'peer-id';
|
||||||
|
|
||||||
waku.libp2p.peerStore.addressBook.add(
|
// Directly dial a new peer
|
||||||
PeerId.createFromB58String(
|
await waku.dial('/dns4/node-01.do-ams3.jdev.misc.statusim.net/tcp/7010/wss/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ');
|
||||||
'16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ'
|
|
||||||
),
|
// Or, add peer to address book so it auto dials in the background
|
||||||
[multiaddr('/dns4/node-01.do-ams3.jdev.misc.statusim.net/tcp/7010/wss')]
|
waku.addPeerToAddressBook(
|
||||||
|
'16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ',
|
||||||
|
['/dns4/node-01.do-ams3.jdev.misc.statusim.net/tcp/7010/wss']
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import { multiaddr } from 'multiaddr';
|
|
||||||
import PeerId from 'peer-id';
|
import PeerId from 'peer-id';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import './App.css';
|
import './App.css';
|
||||||
@ -147,17 +146,13 @@ async function initWaku(setter: (waku: Waku) => void) {
|
|||||||
|
|
||||||
setter(waku);
|
setter(waku);
|
||||||
|
|
||||||
waku.libp2p.peerStore.addressBook.add(
|
waku.addPeerToAddressBook(
|
||||||
PeerId.createFromB58String(
|
'16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ',
|
||||||
'16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ'
|
['/dns4/node-01.do-ams3.jdev.misc.statusim.net/tcp/7010/wss']
|
||||||
),
|
|
||||||
[multiaddr('/dns4/node-01.do-ams3.jdev.misc.statusim.net/tcp/7010/wss')]
|
|
||||||
);
|
);
|
||||||
waku.libp2p.peerStore.addressBook.add(
|
waku.addPeerToAddressBook(
|
||||||
PeerId.createFromB58String(
|
'16Uiu2HAmSyrYVycqBCWcHyNVQS6zYQcdQbwyov1CDijboVRsQS37',
|
||||||
'16Uiu2HAmSyrYVycqBCWcHyNVQS6zYQcdQbwyov1CDijboVRsQS37'
|
['/dns4/node-01.do-ams3.jdev.misc.statusim.net/tcp/7009/wss']
|
||||||
),
|
|
||||||
[multiaddr('/dns4/node-01.do-ams3.jdev.misc.statusim.net/tcp/7009/wss')]
|
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('Issue starting waku ', e);
|
console.log('Issue starting waku ', e);
|
||||||
|
@ -4,7 +4,7 @@ import { bytes } from 'libp2p-noise/dist/src/@types/basic';
|
|||||||
import { Noise } from 'libp2p-noise/dist/src/noise';
|
import { Noise } from 'libp2p-noise/dist/src/noise';
|
||||||
import Websockets from 'libp2p-websockets';
|
import Websockets from 'libp2p-websockets';
|
||||||
import filters from 'libp2p-websockets/src/filters';
|
import filters from 'libp2p-websockets/src/filters';
|
||||||
import { Multiaddr } from 'multiaddr';
|
import { Multiaddr, multiaddr } from 'multiaddr';
|
||||||
import PeerId from 'peer-id';
|
import PeerId from 'peer-id';
|
||||||
|
|
||||||
import { RelayCodec, WakuRelay } from './waku_relay';
|
import { RelayCodec, WakuRelay } from './waku_relay';
|
||||||
@ -34,12 +34,8 @@ export class Waku {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Create new waku node
|
* Create new waku node
|
||||||
* @param listenAddresses: Array of Multiaddrs on which the node should listen.
|
*
|
||||||
* If not present, the node is dial only.
|
* @param options Takes the same options than `Libp2p`.
|
||||||
* @param staticNoiseKey: A static key to use for noise,
|
|
||||||
* mainly used for test to reduce entropy usage.
|
|
||||||
* @throws If
|
|
||||||
* @returns {Promise<Waku>}
|
|
||||||
*/
|
*/
|
||||||
static async create(options: Partial<CreateOptions>): Promise<Waku> {
|
static async create(options: Partial<CreateOptions>): Promise<Waku> {
|
||||||
const opts = Object.assign(
|
const opts = Object.assign(
|
||||||
@ -96,6 +92,7 @@ export class Waku {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Dials to the provided peer.
|
* Dials to the provided peer.
|
||||||
|
*
|
||||||
* @param peer The peer to dial
|
* @param peer The peer to dial
|
||||||
*/
|
*/
|
||||||
async dial(
|
async dial(
|
||||||
@ -107,8 +104,27 @@ export class Waku {
|
|||||||
return this.libp2p.dialProtocol(peer, [RelayCodec, StoreCodec]);
|
return this.libp2p.dialProtocol(peer, [RelayCodec, StoreCodec]);
|
||||||
}
|
}
|
||||||
|
|
||||||
addPeerToAddressBook(peerId: PeerId, multiaddr: Multiaddr[]): void {
|
/**
|
||||||
this.libp2p.peerStore.addressBook.set(peerId, multiaddr);
|
* Add peer to address book, it will be auto-dialed in the background.
|
||||||
|
*/
|
||||||
|
addPeerToAddressBook(
|
||||||
|
peerId: PeerId | string,
|
||||||
|
multiaddrs: Multiaddr[] | string[]
|
||||||
|
): void {
|
||||||
|
let peer;
|
||||||
|
if (typeof peerId === 'string') {
|
||||||
|
peer = PeerId.createFromB58String(peerId);
|
||||||
|
} else {
|
||||||
|
peer = peerId;
|
||||||
|
}
|
||||||
|
const addresses = multiaddrs.map((addr: Multiaddr | string) => {
|
||||||
|
if (typeof addr === 'string') {
|
||||||
|
return multiaddr(addr);
|
||||||
|
} else {
|
||||||
|
return addr;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.libp2p.peerStore.addressBook.set(peer, addresses);
|
||||||
}
|
}
|
||||||
|
|
||||||
async stop(): Promise<void> {
|
async stop(): Promise<void> {
|
||||||
@ -126,8 +142,6 @@ export class Waku {
|
|||||||
if (!localMultiaddr || localMultiaddr.toString() === '') {
|
if (!localMultiaddr || localMultiaddr.toString() === '') {
|
||||||
throw 'Not listening on localhost';
|
throw 'Not listening on localhost';
|
||||||
}
|
}
|
||||||
const multiAddrWithId =
|
return localMultiaddr + '/p2p/' + this.libp2p.peerId.toB58String();
|
||||||
localMultiaddr + '/p2p/' + this.libp2p.peerId.toB58String();
|
|
||||||
return multiAddrWithId;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user