mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
fix: replace node buffers with uint8arrays (#730)
* fix: replace node buffers with uint8arrays Upgrades all deps and replaces all use of node Buffers with Uint8Arrays BREAKING CHANGES: - All deps used by this module now use Uint8Arrays in place of node Buffers * chore: browser fixes * chore: remove .only * chore: stringify uint8array before parsing * chore: update interop suite * chore: remove ts from build command * chore: update deps * fix: update records to use uint8array * chore: fix lint * chore: update deps Co-authored-by: Jacob Heun <jacobheun@gmail.com>
This commit is contained in:
parent
9107efe121
commit
1e869717ff
@ -49,5 +49,11 @@ module.exports = {
|
||||
hooks: {
|
||||
pre: before,
|
||||
post: after
|
||||
},
|
||||
webpack: {
|
||||
node: {
|
||||
// needed by bcrypto
|
||||
Buffer: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
40
doc/API.md
40
doc/API.md
@ -585,7 +585,7 @@ Writes a value to a key in the DHT.
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| key | `string` | key to add to the dht |
|
||||
| value | `Buffer` | value to add to the dht |
|
||||
| value | `Uint8Array` | value to add to the dht |
|
||||
| [options] | `object` | put options |
|
||||
| [options.minPeers] | `number` | minimum number of peers required to successfully put (default: closestPeers.length) |
|
||||
|
||||
@ -600,7 +600,7 @@ Writes a value to a key in the DHT.
|
||||
```js
|
||||
// ...
|
||||
const key = '/key'
|
||||
const value = Buffer.from('oh hello there')
|
||||
const value = uint8ArrayFromString('oh hello there')
|
||||
|
||||
await libp2p.contentRouting.put(key, value)
|
||||
```
|
||||
@ -623,7 +623,7 @@ Queries the DHT for a value stored for a given key.
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise<Buffer>` | Value obtained from the DHT |
|
||||
| `Promise<Uint8Array>` | Value obtained from the DHT |
|
||||
|
||||
#### Example
|
||||
|
||||
@ -653,7 +653,7 @@ Queries the DHT for the n values stored for the given key (without sorting).
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise<Array<{from: PeerId, val: Buffer}>>` | Array of records obtained from the DHT |
|
||||
| `Promise<Array<{from: PeerId, val: Uint8Array}>>` | Array of records obtained from the DHT |
|
||||
|
||||
#### Example
|
||||
|
||||
@ -970,7 +970,7 @@ Delete the provided peer from the book.
|
||||
```js
|
||||
peerStore.metadataBook.delete(peerId)
|
||||
// false
|
||||
peerStore.metadataBook.set(peerId, 'nickname', Buffer.from('homePeer'))
|
||||
peerStore.metadataBook.set(peerId, 'nickname', uint8ArrayFromString('homePeer'))
|
||||
peerStore.metadataBook.delete(peerId)
|
||||
// true
|
||||
```
|
||||
@ -999,7 +999,7 @@ Deletes the provided peer metadata key-value pair from the book.
|
||||
```js
|
||||
peerStore.metadataBook.deleteValue(peerId, 'location')
|
||||
// false
|
||||
peerStore.metadataBook.set(peerId, 'location', Buffer.from('Berlin'))
|
||||
peerStore.metadataBook.set(peerId, 'location', uint8ArrayFromString('Berlin'))
|
||||
peerStore.metadataBook.deleteValue(peerId, 'location')
|
||||
// true
|
||||
```
|
||||
@ -1020,14 +1020,14 @@ Get the known metadata of a provided peer.
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Map<string, Buffer>` | Peer Metadata |
|
||||
| `Map<string, Uint8Array>` | Peer Metadata |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
peerStore.metadataBook.get(peerId)
|
||||
// undefined
|
||||
peerStore.metadataBook.set(peerId, 'location', Buffer.from('Berlin'))
|
||||
peerStore.metadataBook.set(peerId, 'location', uint8ArrayFromString('Berlin'))
|
||||
peerStore.metadataBook.get(peerId)
|
||||
// Metadata Map
|
||||
```
|
||||
@ -1049,14 +1049,14 @@ Get specific metadata of a provided peer.
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Map<string, Buffer>` | Peer Metadata |
|
||||
| `Map<string, Uint8Array>` | Peer Metadata |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
peerStore.metadataBook.getValue(peerId, 'location')
|
||||
// undefined
|
||||
peerStore.metadataBook.set(peerId, 'location', Buffer.from('Berlin'))
|
||||
peerStore.metadataBook.set(peerId, 'location', uint8ArrayFromString('Berlin'))
|
||||
peerStore.metadataBook.getValue(peerId, 'location')
|
||||
// Metadata Map
|
||||
```
|
||||
@ -1073,7 +1073,7 @@ Set known metadata of a given `peerId`.
|
||||
|------|------|-------------|
|
||||
| peerId | [`PeerId`][peer-id] | peerId to set |
|
||||
| key | `string` | key of the metadata value to store |
|
||||
| value | `Buffer` | metadata value to store |
|
||||
| value | `Uint8Array` | metadata value to store |
|
||||
|
||||
#### Returns
|
||||
|
||||
@ -1084,7 +1084,7 @@ Set known metadata of a given `peerId`.
|
||||
#### Example
|
||||
|
||||
```js
|
||||
peerStore.metadataBook.set(peerId, 'location', Buffer.from('Berlin'))
|
||||
peerStore.metadataBook.set(peerId, 'location', uint8ArrayFromString('Berlin'))
|
||||
```
|
||||
|
||||
### peerStore.protoBook.delete
|
||||
@ -1308,7 +1308,7 @@ Publishes messages to the given topics.
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| topic | `string` | topic to publish |
|
||||
| data | `Buffer` | data to publish |
|
||||
| data | `Uint8Array` | data to publish |
|
||||
|
||||
#### Returns
|
||||
|
||||
@ -1320,7 +1320,7 @@ Publishes messages to the given topics.
|
||||
|
||||
```js
|
||||
const topic = 'topic'
|
||||
const data = Buffer.from('data')
|
||||
const data = uint8ArrayFromString('data')
|
||||
|
||||
await libp2p.pubsub.publish(topic, data)
|
||||
```
|
||||
@ -1336,7 +1336,7 @@ Subscribes the given handler to a pubsub topic.
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| topic | `string` | topic to subscribe |
|
||||
| handler | `function({ from: string, data: Buffer, seqno: Buffer, topicIDs: Array<string>, signature: Buffer, key: Buffer })` | handler for new data on topic |
|
||||
| handler | `function({ from: string, data: Uint8Array, seqno: Uint8Array, topicIDs: Array<string>, signature: Uint8Array, key: Uint8Array })` | handler for new data on topic |
|
||||
|
||||
#### Returns
|
||||
|
||||
@ -1679,19 +1679,19 @@ Encrypt protected data using the Cryptographic Message Syntax (CMS).
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| name | `string` | The local key name. |
|
||||
| data | `Buffer` | The data to encrypt. |
|
||||
| data | `Uint8Array` | The data to encrypt. |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise<Buffer>` | Encrypted data as a PKCS #7 message in DER. |
|
||||
| `Promise<Uint8Array>` | Encrypted data as a PKCS #7 message in DER. |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const keyInfo = await libp2p.keychain.createKey('keyTest', 'rsa', 4096)
|
||||
const enc = await libp2p.keychain.cms.encrypt('keyTest', Buffer.from('data'))
|
||||
const enc = await libp2p.keychain.cms.encrypt('keyTest', uint8ArrayFromString('data'))
|
||||
```
|
||||
|
||||
### keychain.cms.decrypt
|
||||
@ -1711,13 +1711,13 @@ The keychain must contain one of the keys used to encrypt the data. If none of
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise<Buffer>` | Decrypted data. |
|
||||
| `Promise<Uint8Array>` | Decrypted data. |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const keyInfo = await libp2p.keychain.createKey('keyTest', 'rsa', 4096)
|
||||
const enc = await libp2p.keychain.cms.encrypt('keyTest', Buffer.from('data'))
|
||||
const enc = await libp2p.keychain.cms.encrypt('keyTest', uint8ArrayFromString('data'))
|
||||
const decData = await libp2p.keychain.cms.decrypt(enc)
|
||||
```
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
const pipe = require('it-pipe')
|
||||
const lp = require('it-length-prefixed')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
function stdinToStream(stream) {
|
||||
// Read utf-8 from stdin
|
||||
@ -28,7 +29,7 @@ function streamToConsole(stream) {
|
||||
// For each chunk of data
|
||||
for await (const msg of source) {
|
||||
// Output the data as a utf8 string
|
||||
console.log('> ' + msg.toString('utf8').replace('\n', ''))
|
||||
console.log('> ' + uint8ArrayToString(msg).replace('\n', ''))
|
||||
}
|
||||
}
|
||||
)
|
||||
|
@ -1,18 +1,17 @@
|
||||
/* eslint no-console: ["off"] */
|
||||
'use strict'
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const { generate } = require('libp2p/src/pnet')
|
||||
const privateLibp2pNode = require('./libp2p-node')
|
||||
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
// Create a buffer and write the swarm key to it
|
||||
const swarmKey = Buffer.alloc(95)
|
||||
// Create a Uint8Array and write the swarm key to it
|
||||
const swarmKey = new Uint8Array(95)
|
||||
generate(swarmKey)
|
||||
|
||||
// This key is for testing a different key not working
|
||||
const otherSwarmKey = Buffer.alloc(95)
|
||||
const otherSwarmKey = new Uint8Array(95)
|
||||
generate(otherSwarmKey)
|
||||
|
||||
;(async () => {
|
||||
|
@ -11,7 +11,7 @@ const Protector = require('libp2p/src/pnet')
|
||||
* privateLibp2pNode returns a libp2p node function that will use the swarm
|
||||
* key with the given `swarmKey` to create the Protector
|
||||
*
|
||||
* @param {Buffer} swarmKey
|
||||
* @param {Uint8Array} swarmKey
|
||||
* @returns {Promise<libp2p>} Returns a libp2pNode function for use in IPFS creation
|
||||
*/
|
||||
const privateLibp2pNode = async (swarmKey) => {
|
||||
|
@ -1,13 +1,13 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
@ -48,6 +48,6 @@ const createNode = async () => {
|
||||
|
||||
// node2 publishes "news" every second
|
||||
setInterval(() => {
|
||||
node2.pubsub.publish(topic, Buffer.from('Bird bird bird, bird is the word!'))
|
||||
node2.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!'))
|
||||
}, 1000)
|
||||
})()
|
||||
|
@ -57,7 +57,7 @@ await node2.pubsub.subscribe(topic, (msg) => {
|
||||
|
||||
// node2 publishes "news" every second
|
||||
setInterval(() => {
|
||||
node2.pubsub.publish(topic, Buffer.from('Bird bird bird, bird is the word!'))
|
||||
node2.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!'))
|
||||
}, 1000)
|
||||
```
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const Libp2p = require('../../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
@ -73,7 +73,7 @@ const createNode = async () => {
|
||||
// car is not a fruit !
|
||||
setInterval(() => {
|
||||
console.log('############## fruit ' + myFruits[count] + ' ##############')
|
||||
node1.pubsub.publish(topic, Buffer.from(myFruits[count]))
|
||||
node1.pubsub.publish(topic, uint8ArrayFromString(myFruits[count]))
|
||||
count++
|
||||
if (count == myFruits.length) {
|
||||
count = 0
|
||||
|
@ -79,7 +79,7 @@ const myFruits = ['banana', 'apple', 'car', 'orange'];
|
||||
|
||||
setInterval(() => {
|
||||
console.log('############## fruit ' + myFruits[count] + ' ##############')
|
||||
node1.pubsub.publish(topic, Buffer.from(myFruits[count]))
|
||||
node1.pubsub.publish(topic, new TextEncoder().encode(myFruits[count]))
|
||||
count++
|
||||
if (count == myFruits.length) {
|
||||
count = 0
|
||||
|
66
package.json
66
package.json
@ -50,7 +50,7 @@
|
||||
"err-code": "^2.0.0",
|
||||
"events": "^3.1.0",
|
||||
"hashlru": "^2.3.0",
|
||||
"interface-datastore": "^1.0.4",
|
||||
"interface-datastore": "^2.0.0",
|
||||
"ipfs-utils": "^2.2.0",
|
||||
"it-all": "^1.0.1",
|
||||
"it-buffer": "^0.1.2",
|
||||
@ -58,69 +58,67 @@
|
||||
"it-length-prefixed": "^3.0.1",
|
||||
"it-pipe": "^1.1.0",
|
||||
"it-protocol-buffers": "^0.2.0",
|
||||
"libp2p-crypto": "^0.17.9",
|
||||
"libp2p-interfaces": "^0.3.1",
|
||||
"libp2p-utils": "^0.1.2",
|
||||
"mafmt": "^7.0.0",
|
||||
"libp2p-crypto": "^0.18.0",
|
||||
"libp2p-interfaces": "^0.5.0",
|
||||
"libp2p-utils": "^0.2.0",
|
||||
"mafmt": "^8.0.0",
|
||||
"merge-options": "^2.0.0",
|
||||
"moving-average": "^1.0.0",
|
||||
"multiaddr": "^7.4.3",
|
||||
"multiaddr": "^8.0.0",
|
||||
"multicodec": "^1.0.2",
|
||||
"multistream-select": "^0.15.0",
|
||||
"multistream-select": "^1.0.0",
|
||||
"mutable-proxy": "^1.0.0",
|
||||
"node-forge": "^0.9.1",
|
||||
"p-any": "^3.0.0",
|
||||
"p-fifo": "^1.0.0",
|
||||
"p-settle": "^4.0.1",
|
||||
"peer-id": "^0.13.11",
|
||||
"protons": "^1.0.1",
|
||||
"peer-id": "^0.14.0",
|
||||
"protons": "^2.0.0",
|
||||
"retimer": "^2.0.0",
|
||||
"sanitize-filename": "^1.6.3",
|
||||
"streaming-iterables": "^4.1.0",
|
||||
"timeout-abort-controller": "^1.0.0",
|
||||
"streaming-iterables": "^5.0.2",
|
||||
"timeout-abort-controller": "^1.1.1",
|
||||
"varint": "^5.0.0",
|
||||
"xsalsa20": "^1.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nodeutils/defaults-deep": "^1.1.0",
|
||||
"abortable-iterator": "^3.0.0",
|
||||
"aegir": "^22.0.0",
|
||||
"aegir": "^26.0.0",
|
||||
"chai": "^4.2.0",
|
||||
"chai-as-promised": "^7.1.1",
|
||||
"chai-bytes": "^0.1.2",
|
||||
"chai-string": "^1.5.0",
|
||||
"cids": "^0.8.0",
|
||||
"datastore-fs": "^1.1.0",
|
||||
"datastore-level": "^1.1.0",
|
||||
"cids": "^1.0.0",
|
||||
"delay": "^4.3.0",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"interop-libp2p": "^0.1.0",
|
||||
"ipfs-http-client": "^44.0.0",
|
||||
"interop-libp2p": "^0.2.0",
|
||||
"ipfs-http-client": "^46.0.0",
|
||||
"it-concat": "^1.0.0",
|
||||
"it-pair": "^1.0.0",
|
||||
"it-pushable": "^1.4.0",
|
||||
"level": "^6.0.1",
|
||||
"libp2p-bootstrap": "^0.11.0",
|
||||
"libp2p-delegated-content-routing": "^0.5.0",
|
||||
"libp2p-delegated-peer-routing": "^0.5.0",
|
||||
"libp2p-floodsub": "^0.21.0",
|
||||
"libp2p-gossipsub": "^0.4.6",
|
||||
"libp2p-kad-dht": "^0.19.1",
|
||||
"libp2p-mdns": "^0.14.1",
|
||||
"libp2p-mplex": "^0.9.5",
|
||||
"libp2p-noise": "^1.1.1",
|
||||
"libp2p-secio": "^0.12.4",
|
||||
"libp2p-tcp": "^0.14.1",
|
||||
"libp2p-webrtc-star": "^0.18.0",
|
||||
"libp2p-websockets": "^0.13.1",
|
||||
"multihashes": "^0.4.19",
|
||||
"nock": "^12.0.3",
|
||||
"libp2p-bootstrap": "^0.12.0",
|
||||
"libp2p-delegated-content-routing": "^0.6.0",
|
||||
"libp2p-delegated-peer-routing": "^0.6.0",
|
||||
"libp2p-floodsub": "^0.22.0",
|
||||
"libp2p-gossipsub": "^0.5.0",
|
||||
"libp2p-kad-dht": "^0.20.0",
|
||||
"libp2p-mdns": "^0.15.0",
|
||||
"libp2p-mplex": "^0.10.0",
|
||||
"libp2p-noise": "^2.0.0",
|
||||
"libp2p-secio": "^0.13.1",
|
||||
"libp2p-tcp": "^0.15.1",
|
||||
"libp2p-webrtc-star": "^0.19.0",
|
||||
"libp2p-websockets": "^0.14.0",
|
||||
"multihashes": "^3.0.1",
|
||||
"nock": "^13.0.3",
|
||||
"p-defer": "^3.0.0",
|
||||
"p-times": "^3.0.0",
|
||||
"p-wait-for": "^3.1.0",
|
||||
"promisify-es6": "^1.0.3",
|
||||
"rimraf": "^3.0.2",
|
||||
"sinon": "^9.0.2"
|
||||
"sinon": "^9.0.2",
|
||||
"uint8arrays": "^1.1.0"
|
||||
},
|
||||
"contributors": [
|
||||
"David Dias <daviddias.p@gmail.com>",
|
||||
|
@ -122,11 +122,11 @@ class Circuit {
|
||||
type: CircuitPB.Type.HOP,
|
||||
srcPeer: {
|
||||
id: this.peerId.toBytes(),
|
||||
addrs: this._libp2p.multiaddrs.map(addr => addr.buffer)
|
||||
addrs: this._libp2p.multiaddrs.map(addr => addr.bytes)
|
||||
},
|
||||
dstPeer: {
|
||||
id: destinationPeer.toBytes(),
|
||||
addrs: [multiaddr(destinationAddr).buffer]
|
||||
addrs: [multiaddr(destinationAddr).bytes]
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -64,8 +64,8 @@ module.exports = (node) => {
|
||||
|
||||
/**
|
||||
* Store the given key/value pair in the DHT.
|
||||
* @param {Buffer} key
|
||||
* @param {Buffer} value
|
||||
* @param {Uint8Array} key
|
||||
* @param {Uint8Array} value
|
||||
* @param {Object} [options] - put options
|
||||
* @param {number} [options.minPeers] - minimum number of peers required to successfully put
|
||||
* @returns {Promise<void>}
|
||||
@ -81,10 +81,10 @@ module.exports = (node) => {
|
||||
/**
|
||||
* Get the value to the given key.
|
||||
* Times out after 1 minute by default.
|
||||
* @param {Buffer} key
|
||||
* @param {Uint8Array} key
|
||||
* @param {Object} [options] - get options
|
||||
* @param {number} [options.timeout] - optional timeout (default: 60000)
|
||||
* @returns {Promise<{from: PeerId, val: Buffer}>}
|
||||
* @returns {Promise<{from: PeerId, val: Uint8Array}>}
|
||||
*/
|
||||
async get (key, options) { // eslint-disable-line require-await
|
||||
if (!node.isStarted() || !dht.isStarted) {
|
||||
@ -96,11 +96,11 @@ module.exports = (node) => {
|
||||
|
||||
/**
|
||||
* Get the `n` values to the given key without sorting.
|
||||
* @param {Buffer} key
|
||||
* @param {Uint8Array} key
|
||||
* @param {number} nVals
|
||||
* @param {Object} [options] - get options
|
||||
* @param {number} [options.timeout] - optional timeout (default: 60000)
|
||||
* @returns {Promise<Array<{from: PeerId, val: Buffer}>>}
|
||||
* @returns {Promise<Array<{from: PeerId, val: Uint8Array}>>}
|
||||
*/
|
||||
async getMany (key, nVals, options) { // eslint-disable-line require-await
|
||||
if (!node.isStarted() || !dht.isStarted) {
|
||||
|
@ -5,11 +5,11 @@ const log = debug('libp2p:identify')
|
||||
log.error = debug('libp2p:identify:error')
|
||||
|
||||
const errCode = require('err-code')
|
||||
const { Buffer } = require('buffer')
|
||||
const pb = require('it-protocol-buffers')
|
||||
const lp = require('it-length-prefixed')
|
||||
const pipe = require('it-pipe')
|
||||
const { collect, take, consume } = require('streaming-iterables')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const multiaddr = require('multiaddr')
|
||||
@ -32,7 +32,7 @@ const { codes } = require('../errors')
|
||||
class IdentifyService {
|
||||
/**
|
||||
* Takes the `addr` and converts it to a Multiaddr if possible
|
||||
* @param {Buffer|String} addr
|
||||
* @param {Uint8Array|String} addr
|
||||
* @returns {Multiaddr|null}
|
||||
*/
|
||||
static getCleanMultiaddr (addr) {
|
||||
@ -91,7 +91,7 @@ class IdentifyService {
|
||||
*/
|
||||
async push (connections) {
|
||||
const signedPeerRecord = await this._getSelfPeerRecord()
|
||||
const listenAddrs = this._libp2p.multiaddrs.map((ma) => ma.buffer)
|
||||
const listenAddrs = this._libp2p.multiaddrs.map((ma) => ma.bytes)
|
||||
const protocols = Array.from(this._protocols.keys())
|
||||
|
||||
const pushes = connections.map(async connection => {
|
||||
@ -199,7 +199,7 @@ class IdentifyService {
|
||||
}
|
||||
|
||||
this.peerStore.protoBook.set(id, protocols)
|
||||
this.peerStore.metadataBook.set(id, 'AgentVersion', Buffer.from(message.agentVersion))
|
||||
this.peerStore.metadataBook.set(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
|
||||
|
||||
// TODO: Track our observed address so that we can score it
|
||||
log('received observed address of %s', observedAddr)
|
||||
@ -234,7 +234,7 @@ class IdentifyService {
|
||||
* @param {Connection} options.connection
|
||||
*/
|
||||
async _handleIdentify ({ connection, stream }) {
|
||||
let publicKey = Buffer.alloc(0)
|
||||
let publicKey = new Uint8Array(0)
|
||||
if (this.peerId.pubKey) {
|
||||
publicKey = this.peerId.pubKey.bytes
|
||||
}
|
||||
@ -245,9 +245,9 @@ class IdentifyService {
|
||||
protocolVersion: PROTOCOL_VERSION,
|
||||
agentVersion: AGENT_VERSION,
|
||||
publicKey,
|
||||
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.buffer),
|
||||
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
|
||||
signedPeerRecord,
|
||||
observedAddr: connection.remoteAddr.buffer,
|
||||
observedAddr: connection.remoteAddr.bytes,
|
||||
protocols: Array.from(this._protocols.keys())
|
||||
})
|
||||
|
||||
@ -311,7 +311,7 @@ class IdentifyService {
|
||||
|
||||
/**
|
||||
* Get self signed peer record raw envelope.
|
||||
* @return {Buffer}
|
||||
* @return {Uint8Array}
|
||||
*/
|
||||
async _getSelfPeerRecord () {
|
||||
const selfSignedPeerRecord = this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
||||
|
@ -5,6 +5,8 @@ require('node-forge/lib/pbe')
|
||||
const forge = require('node-forge/lib/forge')
|
||||
const { certificateForKey, findAsync } = require('./util')
|
||||
const errcode = require('err-code')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
/**
|
||||
* Cryptographic Message Syntax (aka PKCS #7)
|
||||
@ -32,15 +34,15 @@ class CMS {
|
||||
/**
|
||||
* Creates some protected data.
|
||||
*
|
||||
* The output Buffer contains the PKCS #7 message in DER.
|
||||
* The output Uint8Array contains the PKCS #7 message in DER.
|
||||
*
|
||||
* @param {string} name - The local key name.
|
||||
* @param {Buffer} plain - The data to encrypt.
|
||||
* @param {Uint8Array} plain - The data to encrypt.
|
||||
* @returns {undefined}
|
||||
*/
|
||||
async encrypt (name, plain) {
|
||||
if (!Buffer.isBuffer(plain)) {
|
||||
throw errcode(new Error('Plain data must be a Buffer'), 'ERR_INVALID_PARAMS')
|
||||
if (!(plain instanceof Uint8Array)) {
|
||||
throw errcode(new Error('Plain data must be a Uint8Array'), 'ERR_INVALID_PARAMS')
|
||||
}
|
||||
|
||||
const key = await this.keychain.findKeyByName(name)
|
||||
@ -56,7 +58,7 @@ class CMS {
|
||||
|
||||
// convert message to DER
|
||||
const der = forge.asn1.toDer(p7.toAsn1()).getBytes()
|
||||
return Buffer.from(der, 'binary')
|
||||
return uint8ArrayFromString(der, 'ascii')
|
||||
}
|
||||
|
||||
/**
|
||||
@ -65,17 +67,17 @@ class CMS {
|
||||
* The keychain must contain one of the keys used to encrypt the data. If none of the keys
|
||||
* exists, an Error is returned with the property 'missingKeys'. It is array of key ids.
|
||||
*
|
||||
* @param {Buffer} cmsData - The CMS encrypted data to decrypt.
|
||||
* @param {Uint8Array} cmsData - The CMS encrypted data to decrypt.
|
||||
* @returns {undefined}
|
||||
*/
|
||||
async decrypt (cmsData) {
|
||||
if (!Buffer.isBuffer(cmsData)) {
|
||||
if (!(cmsData instanceof Uint8Array)) {
|
||||
throw errcode(new Error('CMS data is required'), 'ERR_INVALID_PARAMS')
|
||||
}
|
||||
|
||||
let cms
|
||||
try {
|
||||
const buf = forge.util.createBuffer(cmsData.toString('binary'))
|
||||
const buf = forge.util.createBuffer(uint8ArrayToString(cmsData, 'ascii'))
|
||||
const obj = forge.asn1.fromDer(buf)
|
||||
cms = forge.pkcs7.messageFromAsn1(obj)
|
||||
} catch (err) {
|
||||
@ -115,7 +117,7 @@ class CMS {
|
||||
const pem = await this.keychain._getPrivateKey(key.name)
|
||||
const privateKey = forge.pki.decryptRsaPrivateKey(pem, this.keychain._())
|
||||
cms.decrypt(r.recipient, privateKey)
|
||||
return Buffer.from(cms.content.getBytes(), 'binary')
|
||||
return uint8ArrayFromString(cms.content.getBytes(), 'ascii')
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,8 @@ const DS = require('interface-datastore')
|
||||
const CMS = require('./cms')
|
||||
const errcode = require('err-code')
|
||||
const { Number } = require('ipfs-utils/src/globalthis')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
require('node-forge/lib/sha512')
|
||||
|
||||
@ -155,7 +157,7 @@ class Keychain {
|
||||
static generateOptions () {
|
||||
const options = Object.assign({}, defaultOptions)
|
||||
const saltLength = Math.ceil(NIST.minSaltLength / 3) * 3 // no base64 padding
|
||||
options.dek.salt = crypto.randomBytes(saltLength).toString('base64')
|
||||
options.dek.salt = uint8ArrayToString(crypto.randomBytes(saltLength), 'base64')
|
||||
return options
|
||||
}
|
||||
|
||||
@ -212,8 +214,8 @@ class Keychain {
|
||||
id: kid
|
||||
}
|
||||
const batch = self.store.batch()
|
||||
batch.put(dsname, pem)
|
||||
batch.put(DsInfoName(name), JSON.stringify(keyInfo))
|
||||
batch.put(dsname, uint8ArrayFromString(pem))
|
||||
batch.put(DsInfoName(name), uint8ArrayFromString(JSON.stringify(keyInfo)))
|
||||
|
||||
await batch.commit()
|
||||
} catch (err) {
|
||||
@ -236,7 +238,7 @@ class Keychain {
|
||||
|
||||
const info = []
|
||||
for await (const value of self.store.query(query)) {
|
||||
info.push(JSON.parse(value.value))
|
||||
info.push(JSON.parse(uint8ArrayToString(value.value)))
|
||||
}
|
||||
|
||||
return info
|
||||
@ -271,7 +273,7 @@ class Keychain {
|
||||
const dsname = DsInfoName(name)
|
||||
try {
|
||||
const res = await this.store.get(dsname)
|
||||
return JSON.parse(res.toString())
|
||||
return JSON.parse(uint8ArrayToString(res))
|
||||
} catch (err) {
|
||||
return throwDelayed(errcode(new Error(`Key '${name}' does not exist. ${err.message}`), 'ERR_KEY_NOT_FOUND'))
|
||||
}
|
||||
@ -321,15 +323,14 @@ class Keychain {
|
||||
if (exists) return throwDelayed(errcode(new Error(`Key '${newName}' already exists`), 'ERR_KEY_ALREADY_EXISTS'))
|
||||
|
||||
try {
|
||||
let res = await this.store.get(oldDsname)
|
||||
const pem = res.toString()
|
||||
res = await self.store.get(oldInfoName)
|
||||
const pem = await self.store.get(oldDsname)
|
||||
const res = await self.store.get(oldInfoName)
|
||||
|
||||
const keyInfo = JSON.parse(res.toString())
|
||||
const keyInfo = JSON.parse(uint8ArrayToString(res))
|
||||
keyInfo.name = newName
|
||||
const batch = self.store.batch()
|
||||
batch.put(newDsname, pem)
|
||||
batch.put(newInfoName, JSON.stringify(keyInfo))
|
||||
batch.put(newInfoName, uint8ArrayFromString(JSON.stringify(keyInfo)))
|
||||
batch.delete(oldDsname)
|
||||
batch.delete(oldInfoName)
|
||||
await batch.commit()
|
||||
@ -357,7 +358,7 @@ class Keychain {
|
||||
const dsname = DsName(name)
|
||||
try {
|
||||
const res = await this.store.get(dsname)
|
||||
const pem = res.toString()
|
||||
const pem = uint8ArrayToString(res)
|
||||
const privateKey = await crypto.keys.import(pem, this._())
|
||||
return privateKey.export(password)
|
||||
} catch (err) {
|
||||
@ -405,8 +406,8 @@ class Keychain {
|
||||
id: kid
|
||||
}
|
||||
const batch = self.store.batch()
|
||||
batch.put(dsname, pem)
|
||||
batch.put(DsInfoName(name), JSON.stringify(keyInfo))
|
||||
batch.put(dsname, uint8ArrayFromString(pem))
|
||||
batch.put(DsInfoName(name), uint8ArrayFromString(JSON.stringify(keyInfo)))
|
||||
await batch.commit()
|
||||
|
||||
return keyInfo
|
||||
@ -434,8 +435,8 @@ class Keychain {
|
||||
id: kid
|
||||
}
|
||||
const batch = self.store.batch()
|
||||
batch.put(dsname, pem)
|
||||
batch.put(DsInfoName(name), JSON.stringify(keyInfo))
|
||||
batch.put(dsname, uint8ArrayFromString(pem))
|
||||
batch.put(DsInfoName(name), uint8ArrayFromString(JSON.stringify(keyInfo)))
|
||||
await batch.commit()
|
||||
return keyInfo
|
||||
} catch (err) {
|
||||
@ -458,7 +459,7 @@ class Keychain {
|
||||
try {
|
||||
const dsname = DsName(name)
|
||||
const res = await this.store.get(dsname)
|
||||
return res.toString()
|
||||
return uint8ArrayToString(res)
|
||||
} catch (err) {
|
||||
return throwDelayed(errcode(new Error(`Key '${name}' does not exist. ${err.message}`), 'ERR_KEY_NOT_FOUND'))
|
||||
}
|
||||
|
@ -8,13 +8,13 @@ exports = module.exports
|
||||
/**
|
||||
* Gets a self-signed X.509 certificate for the key.
|
||||
*
|
||||
* The output Buffer contains the PKCS #7 message in DER.
|
||||
* The output Uint8Array contains the PKCS #7 message in DER.
|
||||
*
|
||||
* TODO: move to libp2p-crypto package
|
||||
*
|
||||
* @param {KeyInfo} key - The id and name of the key
|
||||
* @param {RsaPrivateKey} privateKey - The naked key
|
||||
* @returns {undefined}
|
||||
* @returns {Uint8Array}
|
||||
*/
|
||||
exports.certificateForKey = (key, privateKey) => {
|
||||
const publicKey = pki.setRsaPublicKey(privateKey.n, privateKey.e)
|
||||
|
@ -75,9 +75,9 @@ A `peerId.toB58String()` identifier mapping to a `Set` of protocol identifier st
|
||||
|
||||
#### Metadata Book
|
||||
|
||||
The `metadataBook` keeps track of the known metadata of a peer. Its metadata is stored in a key value fashion, where a key identifier (`string`) represents a metadata value (`Buffer`).
|
||||
The `metadataBook` keeps track of the known metadata of a peer. Its metadata is stored in a key value fashion, where a key identifier (`string`) represents a metadata value (`Uint8Array`).
|
||||
|
||||
`Map<string, Map<string, Buffer>>`
|
||||
`Map<string, Map<string, Uint8Array>>`
|
||||
|
||||
A `peerId.toB58String()` identifier mapping to the peer metadata Map.
|
||||
|
||||
|
@ -31,7 +31,7 @@ class AddressBook extends Book {
|
||||
/**
|
||||
* CertifiedRecord object
|
||||
* @typedef {Object} CertifiedRecord
|
||||
* @property {Buffer} raw raw envelope.
|
||||
* @property {Uint8Array} raw raw envelope.
|
||||
* @property {number} seqNumber seq counter.
|
||||
*/
|
||||
|
||||
@ -128,7 +128,7 @@ class AddressBook extends Book {
|
||||
* Get the raw Envelope for a peer. Returns
|
||||
* undefined if no Envelope is found.
|
||||
* @param {PeerId} peerId
|
||||
* @return {Buffer|undefined}
|
||||
* @return {Uint8Array|undefined}
|
||||
*/
|
||||
getRawEnvelope (peerId) {
|
||||
const entry = this.data.get(peerId.toB58String())
|
||||
|
@ -4,8 +4,7 @@ const errcode = require('err-code')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:peer-store:proto-book')
|
||||
log.error = debug('libp2p:peer-store:proto-book:error')
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const uint8ArrayEquals = require('uint8arrays/equals')
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
@ -38,7 +37,7 @@ class MetadataBook extends Book {
|
||||
|
||||
/**
|
||||
* Map known peers to their known protocols.
|
||||
* @type {Map<string, Map<string, Buffer>>}
|
||||
* @type {Map<string, Map<string, Uint8Array>>}
|
||||
*/
|
||||
this.data = new Map()
|
||||
}
|
||||
@ -48,7 +47,7 @@ class MetadataBook extends Book {
|
||||
* @override
|
||||
* @param {PeerId} peerId
|
||||
* @param {string} key metadata key
|
||||
* @param {Buffer} value metadata value
|
||||
* @param {Uint8Array} value metadata value
|
||||
* @returns {ProtoBook}
|
||||
*/
|
||||
set (peerId, key, value) {
|
||||
@ -57,7 +56,7 @@ class MetadataBook extends Book {
|
||||
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
|
||||
}
|
||||
|
||||
if (typeof key !== 'string' || !Buffer.isBuffer(value)) {
|
||||
if (typeof key !== 'string' || !(value instanceof Uint8Array)) {
|
||||
log.error('valid key and value must be provided to store data')
|
||||
throw errcode(new Error('valid key and value must be provided'), ERR_INVALID_PARAMETERS)
|
||||
}
|
||||
@ -77,7 +76,7 @@ class MetadataBook extends Book {
|
||||
const recMap = rec.get(key)
|
||||
|
||||
// Already exists and is equal
|
||||
if (recMap && value.equals(recMap)) {
|
||||
if (recMap && uint8ArrayEquals(value, recMap)) {
|
||||
log(`the metadata provided to store is equal to the already stored for ${id} on ${key}`)
|
||||
return
|
||||
}
|
||||
@ -91,7 +90,7 @@ class MetadataBook extends Book {
|
||||
/**
|
||||
* Get the known data of a provided peer.
|
||||
* @param {PeerId} peerId
|
||||
* @returns {Map<string, Buffer>}
|
||||
* @returns {Map<string, Uint8Array>}
|
||||
*/
|
||||
get (peerId) {
|
||||
if (!PeerId.isPeerId(peerId)) {
|
||||
@ -105,7 +104,7 @@ class MetadataBook extends Book {
|
||||
* Get specific metadata value, if it exists
|
||||
* @param {PeerId} peerId
|
||||
* @param {string} key
|
||||
* @returns {Buffer}
|
||||
* @returns {Uint8Array}
|
||||
*/
|
||||
getValue (peerId, key) {
|
||||
if (!PeerId.isPeerId(peerId)) {
|
||||
|
@ -189,7 +189,7 @@ class PersistentPeerStore extends PeerStore {
|
||||
|
||||
const encodedData = Addresses.encode({
|
||||
addrs: entry.addresses.map((address) => ({
|
||||
multiaddr: address.multiaddr.buffer,
|
||||
multiaddr: address.multiaddr.bytes,
|
||||
isCertified: address.isCertified
|
||||
})),
|
||||
certified_record: entry.record ? {
|
||||
@ -287,7 +287,7 @@ class PersistentPeerStore extends PeerStore {
|
||||
* @private
|
||||
* @param {Object} params
|
||||
* @param {Key} params.key datastore key
|
||||
* @param {Buffer} params.value datastore value stored
|
||||
* @param {Uint8Array} params.value datastore value stored
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async _processDatastoreEntry ({ key, value }) {
|
||||
|
@ -64,7 +64,7 @@ node -e "require('libp2p/src/pnet').generate(process.stdout)" > swarm.key
|
||||
|
||||
```js
|
||||
const writeKey = require('libp2p/src/pnet').generate
|
||||
const swarmKey = Buffer.alloc(95)
|
||||
const swarmKey = new Uint8Array(95)
|
||||
writeKey(swarmKey)
|
||||
fs.writeFileSync('swarm.key', swarmKey)
|
||||
```
|
||||
|
@ -1,10 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const debug = require('debug')
|
||||
const Errors = require('./errors')
|
||||
const xsalsa20 = require('xsalsa20')
|
||||
const KEY_LENGTH = require('./key-generator').KEY_LENGTH
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const log = debug('libp2p:pnet')
|
||||
log.trace = debug('libp2p:pnet:trace')
|
||||
@ -13,15 +14,15 @@ log.error = debug('libp2p:pnet:err')
|
||||
/**
|
||||
* Creates a stream iterable to encrypt messages in a private network
|
||||
*
|
||||
* @param {Buffer} nonce The nonce to use in encryption
|
||||
* @param {Buffer} psk The private shared key to use in encryption
|
||||
* @param {Uint8Array} nonce The nonce to use in encryption
|
||||
* @param {Uint8Array} psk The private shared key to use in encryption
|
||||
* @returns {*} a through iterable
|
||||
*/
|
||||
module.exports.createBoxStream = (nonce, psk) => {
|
||||
const xor = xsalsa20(nonce, psk)
|
||||
return (source) => (async function * () {
|
||||
for await (const chunk of source) {
|
||||
yield Buffer.from(xor.update(chunk.slice()))
|
||||
yield Uint8Array.from(xor.update(chunk.slice()))
|
||||
}
|
||||
})()
|
||||
}
|
||||
@ -29,8 +30,8 @@ module.exports.createBoxStream = (nonce, psk) => {
|
||||
/**
|
||||
* Creates a stream iterable to decrypt messages in a private network
|
||||
*
|
||||
* @param {Buffer} nonce The nonce of the remote peer
|
||||
* @param {Buffer} psk The private shared key to use in decryption
|
||||
* @param {Uint8Array} nonce The nonce of the remote peer
|
||||
* @param {Uint8Array} psk The private shared key to use in decryption
|
||||
* @returns {*} a through iterable
|
||||
*/
|
||||
module.exports.createUnboxStream = (nonce, psk) => {
|
||||
@ -39,15 +40,15 @@ module.exports.createUnboxStream = (nonce, psk) => {
|
||||
log.trace('Decryption enabled')
|
||||
|
||||
for await (const chunk of source) {
|
||||
yield Buffer.from(xor.update(chunk.slice()))
|
||||
yield Uint8Array.from(xor.update(chunk.slice()))
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode the version 1 psk from the given Buffer
|
||||
* Decode the version 1 psk from the given Uint8Array
|
||||
*
|
||||
* @param {Buffer} pskBuffer
|
||||
* @param {Uint8Array} pskBuffer
|
||||
* @throws {INVALID_PSK}
|
||||
* @returns {Object} The PSK metadata (tag, codecName, psk)
|
||||
*/
|
||||
@ -58,10 +59,10 @@ module.exports.decodeV1PSK = (pskBuffer) => {
|
||||
// from the buffer line by line to evaluate the next line
|
||||
// programmatically instead of making assumptions about the
|
||||
// encodings of each line.
|
||||
const metadata = pskBuffer.toString().split(/(?:\r\n|\r|\n)/g)
|
||||
const metadata = uint8ArrayToString(pskBuffer).split(/(?:\r\n|\r|\n)/g)
|
||||
const pskTag = metadata.shift()
|
||||
const codec = metadata.shift()
|
||||
const psk = Buffer.from(metadata.shift(), 'hex')
|
||||
const psk = uint8ArrayFromString(metadata.shift(), 'base16')
|
||||
|
||||
if (psk.byteLength !== KEY_LENGTH) {
|
||||
throw new Error(Errors.INVALID_PSK)
|
||||
|
@ -25,7 +25,7 @@ log.error = debug('libp2p:pnet:err')
|
||||
*/
|
||||
class Protector {
|
||||
/**
|
||||
* @param {Buffer} keyBuffer The private shared key buffer
|
||||
* @param {Uint8Array} keyBuffer The private shared key buffer
|
||||
* @constructor
|
||||
*/
|
||||
constructor (keyBuffer) {
|
||||
|
@ -2,15 +2,19 @@
|
||||
|
||||
const crypto = require('libp2p-crypto')
|
||||
const KEY_LENGTH = 32
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
/**
|
||||
* Generates a PSK that can be used in a libp2p-pnet private network
|
||||
* @param {Writer} writer An object containing a `write` method
|
||||
* @param {Uint8Array} bytes An object to write the psk into
|
||||
* @returns {void}
|
||||
*/
|
||||
function generate (writer) {
|
||||
const psk = crypto.randomBytes(KEY_LENGTH).toString('hex')
|
||||
writer.write('/key/swarm/psk/1.0.0/\n/base16/\n' + psk)
|
||||
function generate (bytes) {
|
||||
const psk = uint8ArrayToString(crypto.randomBytes(KEY_LENGTH), 'base16')
|
||||
const key = uint8ArrayFromString('/key/swarm/psk/1.0.0/\n/base16/\n' + psk)
|
||||
|
||||
bytes.set(key)
|
||||
}
|
||||
|
||||
module.exports = generate
|
||||
|
@ -1,8 +1,8 @@
|
||||
'use strict'
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const errCode = require('err-code')
|
||||
const { messages, codes } = require('./errors')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
module.exports = (node, Pubsub, config) => {
|
||||
const pubsub = new Pubsub(node.peerId, node.registrar, config)
|
||||
@ -50,7 +50,7 @@ module.exports = (node, Pubsub, config) => {
|
||||
/**
|
||||
* Publish messages to the given topics.
|
||||
* @param {Array<string>|string} topic
|
||||
* @param {Buffer} data
|
||||
* @param {Uint8Array} data
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
publish: (topic, data) => {
|
||||
@ -58,10 +58,14 @@ module.exports = (node, Pubsub, config) => {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
||||
}
|
||||
|
||||
if (typeof data === 'string' || data instanceof String) {
|
||||
data = uint8ArrayFromString(data)
|
||||
}
|
||||
|
||||
try {
|
||||
data = Buffer.from(data)
|
||||
data = Uint8Array.from(data)
|
||||
} catch (err) {
|
||||
throw errCode(new Error('data must be convertible to a Buffer'), 'ERR_DATA_IS_NOT_VALID')
|
||||
throw errCode(new Error('data must be convertible to a Uint8Array'), 'ERR_DATA_IS_NOT_VALID')
|
||||
}
|
||||
|
||||
return pubsub.publish(topic, data)
|
||||
|
@ -17,10 +17,11 @@ You can read further about the envelope in [libp2p/specs#217](https://github.com
|
||||
```js
|
||||
// interface-record implementation example with the "libp2p-example" namespace
|
||||
const Record = require('libp2p-interfaces/src/record')
|
||||
const fromString = require('uint8arrays/from-string')
|
||||
|
||||
class ExampleRecord extends Record {
|
||||
constructor () {
|
||||
super ('libp2p-example', Buffer.from('0302', 'hex'))
|
||||
super ('libp2p-example', fromString('0302', 'hex'))
|
||||
}
|
||||
|
||||
marshal () {}
|
||||
|
@ -4,12 +4,12 @@ const debug = require('debug')
|
||||
const log = debug('libp2p:envelope')
|
||||
log.error = debug('libp2p:envelope:error')
|
||||
const errCode = require('err-code')
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
|
||||
const crypto = require('libp2p-crypto')
|
||||
const uint8arraysConcat = require('uint8arrays/concat')
|
||||
const uint8arraysFromString = require('uint8arrays/from-string')
|
||||
const cryptoKeys = require('libp2p-crypto/src/keys')
|
||||
const PeerId = require('peer-id')
|
||||
const varint = require('varint')
|
||||
const uint8arraysEquals = require('uint8arrays/equals')
|
||||
|
||||
const { codes } = require('../../errors')
|
||||
const Protobuf = require('./envelope.proto')
|
||||
@ -23,9 +23,9 @@ class Envelope {
|
||||
* @constructor
|
||||
* @param {object} params
|
||||
* @param {PeerId} params.peerId
|
||||
* @param {Buffer} params.payloadType
|
||||
* @param {Buffer} params.payload marshaled record
|
||||
* @param {Buffer} params.signature signature of the domain string :: type hint :: payload.
|
||||
* @param {Uint8Array} params.payloadType
|
||||
* @param {Uint8Array} params.payload marshaled record
|
||||
* @param {Uint8Array} params.signature signature of the domain string :: type hint :: payload.
|
||||
*/
|
||||
constructor ({ peerId, payloadType, payload, signature }) {
|
||||
this.peerId = peerId
|
||||
@ -39,14 +39,14 @@ class Envelope {
|
||||
|
||||
/**
|
||||
* Marshal the envelope content.
|
||||
* @return {Buffer}
|
||||
* @return {Uint8Array}
|
||||
*/
|
||||
marshal () {
|
||||
if (this._marshal) {
|
||||
return this._marshal
|
||||
}
|
||||
|
||||
const publicKey = crypto.keys.marshalPublicKey(this.peerId.pubKey)
|
||||
const publicKey = cryptoKeys.marshalPublicKey(this.peerId.pubKey)
|
||||
|
||||
this._marshal = Protobuf.encode({
|
||||
public_key: publicKey,
|
||||
@ -64,10 +64,10 @@ class Envelope {
|
||||
* @return {boolean}
|
||||
*/
|
||||
equals (other) {
|
||||
return this.peerId.pubKey.bytes.equals(other.peerId.pubKey.bytes) &&
|
||||
this.payloadType.equals(other.payloadType) &&
|
||||
this.payload.equals(other.payload) &&
|
||||
this.signature.equals(other.signature)
|
||||
return uint8arraysEquals(this.peerId.pubKey.bytes, other.peerId.pubKey.bytes) &&
|
||||
uint8arraysEquals(this.payloadType, other.payloadType) &&
|
||||
uint8arraysEquals(this.payload, other.payload) &&
|
||||
uint8arraysEquals(this.signature, other.signature)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -83,14 +83,14 @@ class Envelope {
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that prepares a buffer to sign or verify a signature.
|
||||
* Helper function that prepares a Uint8Array to sign or verify a signature.
|
||||
* @param {string} domain
|
||||
* @param {Buffer} payloadType
|
||||
* @param {Buffer} payload
|
||||
* @return {Buffer}
|
||||
* @param {Uint8Array} payloadType
|
||||
* @param {Uint8Array} payload
|
||||
* @return {Uint8Array}
|
||||
*/
|
||||
const formatSignaturePayload = (domain, payloadType, payload) => {
|
||||
// When signing, a peer will prepare a buffer by concatenating the following:
|
||||
// When signing, a peer will prepare a Uint8Array by concatenating the following:
|
||||
// - The length of the domain separation string string in bytes
|
||||
// - The domain separation string, encoded as UTF-8
|
||||
// - The length of the payload_type field in bytes
|
||||
@ -98,23 +98,24 @@ const formatSignaturePayload = (domain, payloadType, payload) => {
|
||||
// - The length of the payload field in bytes
|
||||
// - The value of the payload field
|
||||
|
||||
const domainLength = varint.encode(Buffer.byteLength(domain))
|
||||
domain = uint8arraysFromString(domain)
|
||||
const domainLength = varint.encode(domain.byteLength)
|
||||
const payloadTypeLength = varint.encode(payloadType.length)
|
||||
const payloadLength = varint.encode(payload.length)
|
||||
|
||||
return Buffer.concat([
|
||||
Buffer.from(domainLength),
|
||||
Buffer.from(domain),
|
||||
Buffer.from(payloadTypeLength),
|
||||
return uint8arraysConcat([
|
||||
new Uint8Array(domainLength),
|
||||
domain,
|
||||
new Uint8Array(payloadTypeLength),
|
||||
payloadType,
|
||||
Buffer.from(payloadLength),
|
||||
new Uint8Array(payloadLength),
|
||||
payload
|
||||
])
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmarshal a serialized Envelope protobuf message.
|
||||
* @param {Buffer} data
|
||||
* @param {Uint8Array} data
|
||||
* @return {Promise<Envelope>}
|
||||
*/
|
||||
Envelope.createFromProtobuf = async (data) => {
|
||||
@ -139,7 +140,7 @@ Envelope.createFromProtobuf = async (data) => {
|
||||
*/
|
||||
Envelope.seal = async (record, peerId) => {
|
||||
const domain = record.domain
|
||||
const payloadType = Buffer.from(record.codec)
|
||||
const payloadType = uint8arraysFromString(record.codec)
|
||||
const payload = record.marshal()
|
||||
|
||||
const signData = formatSignaturePayload(domain, payloadType, payload)
|
||||
@ -156,7 +157,7 @@ Envelope.seal = async (record, peerId) => {
|
||||
/**
|
||||
* Open and certify a given marshalled envelope.
|
||||
* Data is unmarshalled and the signature validated for the given domain.
|
||||
* @param {Buffer} data
|
||||
* @param {Uint8Array} data
|
||||
* @param {string} domain
|
||||
* @return {Envelope}
|
||||
*/
|
||||
|
@ -36,7 +36,7 @@ class PeerRecord extends Record {
|
||||
|
||||
/**
|
||||
* Marshal a record to be used in an envelope.
|
||||
* @return {Buffer}
|
||||
* @return {Uint8Array}
|
||||
*/
|
||||
marshal () {
|
||||
if (this._marshal) {
|
||||
@ -47,7 +47,7 @@ class PeerRecord extends Record {
|
||||
peer_id: this.peerId.toBytes(),
|
||||
seq: this.seqNumber,
|
||||
addresses: this.multiaddrs.map((m) => ({
|
||||
multiaddr: m.buffer
|
||||
multiaddr: m.bytes
|
||||
}))
|
||||
})
|
||||
|
||||
@ -81,7 +81,7 @@ class PeerRecord extends Record {
|
||||
|
||||
/**
|
||||
* Unmarshal Peer Record Protobuf.
|
||||
* @param {Buffer} buf marshaled peer record.
|
||||
* @param {Uint8Array} buf marshaled peer record.
|
||||
* @return {PeerRecord}
|
||||
*/
|
||||
PeerRecord.createFromProtobuf = (buf) => {
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
chai.use(require('chai-as-promised'))
|
||||
const { expect } = chai
|
||||
const nock = require('nock')
|
||||
const sinon = require('sinon')
|
||||
@ -96,7 +97,7 @@ describe('content-routing', () => {
|
||||
let delegate
|
||||
|
||||
beforeEach(async () => {
|
||||
const [peerId] = await peerUtils.createPeerId({ fixture: false })
|
||||
const [peerId] = await peerUtils.createPeerId({ fixture: true })
|
||||
|
||||
delegate = new DelegatedContentRouter(peerId, {
|
||||
host: '0.0.0.0',
|
||||
@ -227,7 +228,7 @@ describe('content-routing', () => {
|
||||
let delegate
|
||||
|
||||
beforeEach(async () => {
|
||||
const [peerId] = await peerUtils.createPeerId({ fixture: false })
|
||||
const [peerId] = await peerUtils.createPeerId({ fixture: true })
|
||||
|
||||
delegate = new DelegatedContentRouter(peerId, {
|
||||
host: '0.0.0.0',
|
||||
|
@ -1,14 +1,12 @@
|
||||
'use strict'
|
||||
/* eslint-env mocha */
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const { expect } = chai
|
||||
const { expect } = require('aegir/utils/chai')
|
||||
|
||||
const multiaddr = require('multiaddr')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const mergeOptions = require('merge-options')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
const { create } = require('../../../src')
|
||||
const { subsystemOptions, subsystemMulticodecs } = require('./utils')
|
||||
@ -68,8 +66,8 @@ describe('DHT subsystem operates correctly', () => {
|
||||
})
|
||||
|
||||
it('should put on a peer and get from the other', async () => {
|
||||
const key = Buffer.from('hello')
|
||||
const value = Buffer.from('world')
|
||||
const key = uint8ArrayFromString('hello')
|
||||
const value = uint8ArrayFromString('world')
|
||||
|
||||
await libp2p.dialProtocol(remAddr, subsystemMulticodecs)
|
||||
await Promise.all([
|
||||
@ -131,8 +129,8 @@ describe('DHT subsystem operates correctly', () => {
|
||||
it('should put on a peer and get from the other', async () => {
|
||||
await libp2p.dial(remAddr)
|
||||
|
||||
const key = Buffer.from('hello')
|
||||
const value = Buffer.from('world')
|
||||
const key = uint8ArrayFromString('hello')
|
||||
const value = uint8ArrayFromString('world')
|
||||
|
||||
await remoteLibp2p._dht.start()
|
||||
await pWaitFor(() => libp2p._dht.routingTable.size === 1)
|
||||
|
@ -1,11 +1,7 @@
|
||||
'use strict'
|
||||
/* eslint-env mocha */
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
chai.use(require('chai-as-promised'))
|
||||
const { expect } = chai
|
||||
const { expect } = require('aegir/utils/chai')
|
||||
const sinon = require('sinon')
|
||||
const Transport = require('libp2p-tcp')
|
||||
const Muxer = require('libp2p-mplex')
|
||||
@ -19,6 +15,7 @@ const pipe = require('it-pipe')
|
||||
const AggregateError = require('aggregate-error')
|
||||
const { Connection } = require('libp2p-interfaces/src/connection')
|
||||
const { AbortError } = require('libp2p-interfaces/src/transport/errors')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
const Libp2p = require('../../src')
|
||||
const Dialer = require('../../src/dialer')
|
||||
@ -27,7 +24,7 @@ const PeerStore = require('../../src/peer-store')
|
||||
const TransportManager = require('../../src/transport-manager')
|
||||
const { codes: ErrorCodes } = require('../../src/errors')
|
||||
const Protector = require('../../src/pnet')
|
||||
const swarmKeyBuffer = Buffer.from(require('../fixtures/swarm.key'))
|
||||
const swarmKeyBuffer = uint8ArrayFromString(require('../fixtures/swarm.key'))
|
||||
|
||||
const mockUpgrader = require('../utils/mockUpgrader')
|
||||
const createMockConnection = require('../utils/mockConnection')
|
||||
|
@ -1,7 +1,6 @@
|
||||
'use strict'
|
||||
/* eslint-env mocha */
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
chai.use(require('chai-as-promised'))
|
||||
@ -13,6 +12,7 @@ const { collect } = require('streaming-iterables')
|
||||
const pipe = require('it-pipe')
|
||||
const AggregateError = require('aggregate-error')
|
||||
const PeerId = require('peer-id')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
const { createPeerId } = require('../utils/creators/peer')
|
||||
const baseOptions = require('../utils/base-options')
|
||||
@ -88,7 +88,7 @@ describe('Dialing (via relay, TCP)', () => {
|
||||
)
|
||||
|
||||
const { stream: echoStream } = await connection.newStream('/echo/1.0.0')
|
||||
const input = Buffer.from('hello')
|
||||
const input = uint8ArrayFromString('hello')
|
||||
const [output] = await pipe(
|
||||
[input],
|
||||
echoStream,
|
||||
@ -162,7 +162,7 @@ describe('Dialing (via relay, TCP)', () => {
|
||||
|
||||
// Tamper with the our multiaddrs for the circuit message
|
||||
sinon.stub(srcLibp2p, 'multiaddrs').value([{
|
||||
buffer: Buffer.from('an invalid multiaddr')
|
||||
bytes: uint8ArrayFromString('an invalid multiaddr')
|
||||
}])
|
||||
|
||||
await expect(srcLibp2p.dial(dialAddr))
|
||||
|
@ -13,6 +13,7 @@ const PeerId = require('peer-id')
|
||||
const duplexPair = require('it-pair/duplex')
|
||||
const multiaddr = require('multiaddr')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const unit8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const { codes: Errors } = require('../../src/errors')
|
||||
const { IdentifyService, multicodecs } = require('../../src/identify')
|
||||
@ -148,7 +149,7 @@ describe('Identify', () => {
|
||||
const metadataArgs = localIdentify.peerStore.metadataBook.set.firstCall.args
|
||||
expect(metadataArgs[0].id.bytes).to.equal(remotePeer.bytes)
|
||||
expect(metadataArgs[1]).to.equal('AgentVersion')
|
||||
expect(metadataArgs[2].toString()).to.equal(`js-libp2p/${pkg.version}`)
|
||||
expect(unit8ArrayToString(metadataArgs[2])).to.equal(`js-libp2p/${pkg.version}`)
|
||||
|
||||
// Validate the remote peer gets updated in the peer store
|
||||
const call = localIdentify.peerStore.addressBook.set.firstCall
|
||||
|
@ -1,7 +1,6 @@
|
||||
'use strict'
|
||||
/* eslint-env mocha */
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const { expect } = chai
|
||||
@ -56,7 +55,7 @@ describe('plaintext', () => {
|
||||
|
||||
// When we attempt to get the remote peer key, return the wrong peers pub key
|
||||
sinon.stub(remotePeer, 'marshalPubKey').callsFake(() => {
|
||||
return Buffer.alloc(0)
|
||||
return new Uint8Array(0)
|
||||
})
|
||||
|
||||
return Promise.all([
|
||||
|
@ -7,13 +7,9 @@ const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
chai.use(require('chai-string'))
|
||||
|
||||
const os = require('os')
|
||||
const path = require('path')
|
||||
const { isNode } = require('ipfs-utils/src/env')
|
||||
const FsStore = require('datastore-fs')
|
||||
const LevelStore = require('datastore-level')
|
||||
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
const { MemoryDatastore } = require('interface-datastore')
|
||||
const Keychain = require('../../src/keychain')
|
||||
|
||||
describe('cms interop', () => {
|
||||
@ -22,13 +18,11 @@ describe('cms interop', () => {
|
||||
let ks
|
||||
|
||||
before(() => {
|
||||
const datastore = isNode
|
||||
? new FsStore(path.join(os.tmpdir(), 'test-keystore-1-' + Date.now()))
|
||||
: new LevelStore('test-keystore-1', { db: require('level') })
|
||||
const datastore = new MemoryDatastore()
|
||||
ks = new Keychain(datastore, { passPhrase: passPhrase })
|
||||
})
|
||||
|
||||
const plainData = Buffer.from('This is a message from Alice to Bob')
|
||||
const plainData = uint8ArrayFromString('This is a message from Alice to Bob')
|
||||
|
||||
it('imports openssl key', async function () {
|
||||
this.timeout(10 * 1000)
|
||||
@ -67,8 +61,8 @@ knU1yykWGkdlbclCuu0NaAfmb8o0OX50CbEKZB7xmsv8tnqn0H0jMF4GCSqGSIb3
|
||||
DQEHATAdBglghkgBZQMEASoEEP/PW1JWehQx6/dsLkp/Mf+gMgQwFM9liLTqC56B
|
||||
nHILFmhac/+a/StQOKuf9dx5qXeGvt9LnwKuGGSfNX4g+dTkoa6N
|
||||
`
|
||||
const plain = await ks.cms.decrypt(Buffer.from(example, 'base64'))
|
||||
const plain = await ks.cms.decrypt(uint8ArrayFromString(example.replace(/\s/g, ''), 'base64'))
|
||||
expect(plain).to.exist()
|
||||
expect(plain.toString()).to.equal(plainData.toString())
|
||||
expect(uint8ArrayToString(plain)).to.equal(uint8ArrayToString(plainData))
|
||||
})
|
||||
})
|
||||
|
@ -5,15 +5,12 @@
|
||||
const { chai, expect } = require('aegir/utils/chai')
|
||||
const fail = expect.fail
|
||||
chai.use(require('chai-string'))
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const peerUtils = require('../utils/creators/peer')
|
||||
|
||||
const os = require('os')
|
||||
const path = require('path')
|
||||
const { isNode } = require('ipfs-utils/src/env')
|
||||
const { MemoryDatastore } = require('interface-datastore')
|
||||
const FsStore = require('datastore-fs')
|
||||
const LevelStore = require('datastore-level')
|
||||
const Keychain = require('../../src/keychain')
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
@ -26,16 +23,20 @@ describe('keychain', () => {
|
||||
let ks
|
||||
let datastore1, datastore2
|
||||
|
||||
before(() => {
|
||||
datastore1 = isNode
|
||||
? new FsStore(path.join(os.tmpdir(), 'test-keystore-1-' + Date.now()))
|
||||
: new LevelStore('test-keystore-1', { db: require('level') })
|
||||
datastore2 = isNode
|
||||
? new FsStore(path.join(os.tmpdir(), 'test-keystore-2-' + Date.now()))
|
||||
: new LevelStore('test-keystore-2', { db: require('level') })
|
||||
before(async () => {
|
||||
datastore1 = new MemoryDatastore()
|
||||
datastore2 = new MemoryDatastore()
|
||||
|
||||
ks = new Keychain(datastore2, { passPhrase: passPhrase })
|
||||
emptyKeystore = new Keychain(datastore1, { passPhrase: passPhrase })
|
||||
|
||||
await datastore1.open()
|
||||
await datastore2.open()
|
||||
})
|
||||
|
||||
after(async () => {
|
||||
await datastore2.close()
|
||||
await datastore2.close()
|
||||
})
|
||||
|
||||
it('can start without a password', () => {
|
||||
@ -74,7 +75,7 @@ describe('keychain', () => {
|
||||
const keychainWithPassword = new Keychain(datastore2, { passPhrase: `hello-${Date.now()}-${Date.now()}` })
|
||||
const id = `key-${Math.random()}`
|
||||
|
||||
await keychainWithPassword.createKey(id, 'rsa', 2048)
|
||||
await keychainWithPassword.createKey(id, 'ed25519')
|
||||
|
||||
await expect(keychain.findKeyById(id)).to.eventually.be.ok()
|
||||
})
|
||||
@ -84,7 +85,7 @@ describe('keychain', () => {
|
||||
const keychainWithPassword = new Keychain(datastore2, { passPhrase: `hello-${Date.now()}-${Date.now()}` })
|
||||
const name = `key-${Math.random()}`
|
||||
|
||||
expect(await keychainWithPassword.createKey(name, 'rsa', 2048)).to.have.property('name', name)
|
||||
expect(await keychainWithPassword.createKey(name, 'ed25519')).to.have.property('name', name)
|
||||
expect(await keychainWithoutPassword.findKeyByName(name)).to.have.property('name', name)
|
||||
await keychainWithoutPassword.removeKey(name)
|
||||
await expect(keychainWithoutPassword.findKeyByName(name)).to.be.rejectedWith(/does not exist/)
|
||||
@ -278,7 +279,7 @@ describe('keychain', () => {
|
||||
})
|
||||
|
||||
describe('CMS protected data', () => {
|
||||
const plainData = Buffer.from('This is a message from Alice to Bob')
|
||||
const plainData = uint8ArrayFromString('This is a message from Alice to Bob')
|
||||
let cms
|
||||
|
||||
it('service is available', () => {
|
||||
@ -291,7 +292,7 @@ describe('keychain', () => {
|
||||
expect(err).to.have.property('code', 'ERR_KEY_NOT_FOUND')
|
||||
})
|
||||
|
||||
it('requires plain data as a Buffer', async () => {
|
||||
it('requires plain data as a Uint8Array', async () => {
|
||||
const err = await ks.cms.encrypt(rsaKeyName, 'plain data').then(fail, err => err)
|
||||
expect(err).to.exist()
|
||||
expect(err).to.have.property('code', 'ERR_INVALID_PARAMS')
|
||||
@ -300,7 +301,7 @@ describe('keychain', () => {
|
||||
it('encrypts', async () => {
|
||||
cms = await ks.cms.encrypt(rsaKeyName, plainData)
|
||||
expect(cms).to.exist()
|
||||
expect(cms).to.be.instanceOf(Buffer)
|
||||
expect(cms).to.be.instanceOf(Uint8Array)
|
||||
})
|
||||
|
||||
it('is a PKCS #7 message', async () => {
|
||||
@ -326,7 +327,7 @@ describe('keychain', () => {
|
||||
it('can be read with the key', async () => {
|
||||
const plain = await ks.cms.decrypt(cms)
|
||||
expect(plain).to.exist()
|
||||
expect(plain.toString()).to.equal(plainData.toString())
|
||||
expect(uint8ArrayToString(plain)).to.equal(uint8ArrayToString(plainData))
|
||||
})
|
||||
})
|
||||
|
||||
@ -380,7 +381,7 @@ describe('keychain', () => {
|
||||
let alice
|
||||
|
||||
before(async function () {
|
||||
const encoded = Buffer.from(alicePrivKey, 'base64')
|
||||
const encoded = uint8ArrayFromString(alicePrivKey, 'base64pad')
|
||||
alice = await PeerId.createFromPrivKey(encoded)
|
||||
})
|
||||
|
||||
@ -522,7 +523,7 @@ describe('libp2p.keychain', () => {
|
||||
|
||||
await libp2p.loadKeychain()
|
||||
|
||||
const kInfo = await libp2p.keychain.createKey('keyName', 'rsa', 2048)
|
||||
const kInfo = await libp2p.keychain.createKey('keyName', 'ed25519')
|
||||
expect(kInfo).to.exist()
|
||||
})
|
||||
|
||||
@ -555,7 +556,7 @@ describe('libp2p.keychain', () => {
|
||||
})
|
||||
await libp2p.loadKeychain()
|
||||
|
||||
const kInfo = await libp2p.keychain.createKey('keyName', 'rsa', 2048)
|
||||
const kInfo = await libp2p.keychain.createKey('keyName', 'ed25519')
|
||||
expect(kInfo).to.exist()
|
||||
|
||||
const [libp2p2] = await peerUtils.createPeer({
|
||||
|
@ -10,6 +10,7 @@ const multihash = require('multihashes')
|
||||
const crypto = require('libp2p-crypto')
|
||||
const rsaUtils = require('libp2p-crypto/src/keys/rsa-utils')
|
||||
const rsaClass = require('libp2p-crypto/src/keys/rsa-class')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
const sample = {
|
||||
id: '122019318b6e5e0cf93a2314bf01269a2cc23cd3dcd452d742cdb9379d8646f6e4a9',
|
||||
@ -22,7 +23,7 @@ describe('peer ID', () => {
|
||||
let publicKeyDer // a buffer
|
||||
|
||||
before(async () => {
|
||||
const encoded = Buffer.from(sample.privKey, 'base64')
|
||||
const encoded = uint8ArrayFromString(sample.privKey, 'base64pad')
|
||||
peer = await PeerId.createFromPrivKey(encoded)
|
||||
})
|
||||
|
||||
|
@ -13,6 +13,7 @@ const crypto = require('libp2p-crypto')
|
||||
const KadDht = require('libp2p-kad-dht')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const multiaddr = require('multiaddr')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const Libp2p = require('../../src')
|
||||
const baseOptions = require('../utils/base-options')
|
||||
@ -110,7 +111,7 @@ describe('peer discovery scenarios', () => {
|
||||
enabled: true,
|
||||
interval: 200, // discover quickly
|
||||
// use a random tag to prevent CI collision
|
||||
serviceTag: crypto.randomBytes(10).toString('hex')
|
||||
serviceTag: uint8ArrayToString(crypto.randomBytes(10), 'base16')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
chai.use(require('chai-bytes'))
|
||||
const { expect } = chai
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
const pDefer = require('p-defer')
|
||||
const PeerStore = require('../../src/peer-store')
|
||||
@ -76,7 +77,7 @@ describe('metadataBook', () => {
|
||||
it('stores the content and emit change event', () => {
|
||||
const defer = pDefer()
|
||||
const metadataKey = 'location'
|
||||
const metadataValue = Buffer.from('mars')
|
||||
const metadataValue = uint8ArrayFromString('mars')
|
||||
|
||||
peerStore.once('change:metadata', ({ peerId, metadata }) => {
|
||||
expect(peerId).to.exist()
|
||||
@ -99,8 +100,8 @@ describe('metadataBook', () => {
|
||||
it('emits on set if not storing the exact same content', () => {
|
||||
const defer = pDefer()
|
||||
const metadataKey = 'location'
|
||||
const metadataValue1 = Buffer.from('mars')
|
||||
const metadataValue2 = Buffer.from('saturn')
|
||||
const metadataValue1 = uint8ArrayFromString('mars')
|
||||
const metadataValue2 = uint8ArrayFromString('saturn')
|
||||
|
||||
let changeCounter = 0
|
||||
peerStore.on('change:metadata', () => {
|
||||
@ -129,7 +130,7 @@ describe('metadataBook', () => {
|
||||
it('does not emit on set if it is storing the exact same content', () => {
|
||||
const defer = pDefer()
|
||||
const metadataKey = 'location'
|
||||
const metadataValue = Buffer.from('mars')
|
||||
const metadataValue = uint8ArrayFromString('mars')
|
||||
|
||||
let changeCounter = 0
|
||||
peerStore.on('change:metadata', () => {
|
||||
@ -180,7 +181,7 @@ describe('metadataBook', () => {
|
||||
|
||||
it('returns the metadata stored', () => {
|
||||
const metadataKey = 'location'
|
||||
const metadataValue = Buffer.from('mars')
|
||||
const metadataValue = uint8ArrayFromString('mars')
|
||||
|
||||
mb.set(peerId, metadataKey, metadataValue)
|
||||
|
||||
@ -217,7 +218,7 @@ describe('metadataBook', () => {
|
||||
|
||||
it('returns the metadata value stored for the given key', () => {
|
||||
const metadataKey = 'location'
|
||||
const metadataValue = Buffer.from('mars')
|
||||
const metadataValue = uint8ArrayFromString('mars')
|
||||
|
||||
mb.set(peerId, metadataKey, metadataValue)
|
||||
|
||||
@ -229,7 +230,7 @@ describe('metadataBook', () => {
|
||||
it('returns undefined if no metadata is known for the provided peer and key', () => {
|
||||
const metadataKey = 'location'
|
||||
const metadataBadKey = 'nickname'
|
||||
const metadataValue = Buffer.from('mars')
|
||||
const metadataValue = uint8ArrayFromString('mars')
|
||||
|
||||
mb.set(peerId, metadataKey, metadataValue)
|
||||
|
||||
@ -279,7 +280,7 @@ describe('metadataBook', () => {
|
||||
it('returns true if the record exists and an event is emitted', () => {
|
||||
const defer = pDefer()
|
||||
const metadataKey = 'location'
|
||||
const metadataValue = Buffer.from('mars')
|
||||
const metadataValue = uint8ArrayFromString('mars')
|
||||
|
||||
mb.set(peerId, metadataKey, metadataValue)
|
||||
|
||||
@ -337,7 +338,7 @@ describe('metadataBook', () => {
|
||||
it('returns true if the record exists and an event is emitted', () => {
|
||||
const defer = pDefer()
|
||||
const metadataKey = 'location'
|
||||
const metadataValue = Buffer.from('mars')
|
||||
const metadataValue = uint8ArrayFromString('mars')
|
||||
|
||||
mb.set(peerId, metadataKey, metadataValue)
|
||||
|
||||
@ -357,7 +358,7 @@ describe('metadataBook', () => {
|
||||
const defer = pDefer()
|
||||
const metadataKey = 'location'
|
||||
const metadataBadKey = 'nickname'
|
||||
const metadataValue = Buffer.from('mars')
|
||||
const metadataValue = uint8ArrayFromString('mars')
|
||||
|
||||
mb.set(peerId, metadataKey, metadataValue)
|
||||
|
||||
|
@ -7,6 +7,7 @@ const { expect } = chai
|
||||
|
||||
const PeerStore = require('../../src/peer-store')
|
||||
const multiaddr = require('multiaddr')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
const peerUtils = require('../utils/creators/peer')
|
||||
|
||||
@ -196,7 +197,7 @@ describe('peer-store', () => {
|
||||
|
||||
it('returns peers if only metadata is known', () => {
|
||||
const metadataKey = 'location'
|
||||
const metadataValue = Buffer.from('earth')
|
||||
const metadataValue = uint8ArrayFromString('earth')
|
||||
peerStore.metadataBook.set(peerIds[0], metadataKey, metadataValue)
|
||||
|
||||
const peers = peerStore.peers
|
||||
|
@ -12,6 +12,7 @@ const PeerStore = require('../../src/peer-store/persistent')
|
||||
|
||||
const multiaddr = require('multiaddr')
|
||||
const { MemoryDatastore } = require('interface-datastore')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
const peerUtils = require('../utils/creators/peer')
|
||||
|
||||
@ -145,7 +146,7 @@ describe('Persisted PeerStore', () => {
|
||||
await Promise.all(commitSpy.returnValues)
|
||||
|
||||
// MetadataBook
|
||||
peerStore.metadataBook.set(peers[0], 'location', Buffer.from('earth'))
|
||||
peerStore.metadataBook.set(peers[0], 'location', uint8ArrayFromString('earth'))
|
||||
|
||||
// let batch commit complete
|
||||
await Promise.all(commitSpy.returnValues)
|
||||
@ -186,7 +187,7 @@ describe('Persisted PeerStore', () => {
|
||||
// ProtoBook
|
||||
peerStore.protoBook.set(peer, protocols)
|
||||
// MetadataBook
|
||||
peerStore.metadataBook.set(peer, 'location', Buffer.from('earth'))
|
||||
peerStore.metadataBook.set(peer, 'location', uint8ArrayFromString('earth'))
|
||||
|
||||
// let batch commit complete
|
||||
await Promise.all(commitSpy.returnValues)
|
||||
@ -408,7 +409,7 @@ describe('Persisted PeerStore', () => {
|
||||
// Add Peer0 data in multiple books
|
||||
peerStore.addressBook.set(peers[0], multiaddrs)
|
||||
peerStore.protoBook.set(peers[0], protocols)
|
||||
peerStore.metadataBook.set(peers[0], 'location', Buffer.from('earth'))
|
||||
peerStore.metadataBook.set(peers[0], 'location', uint8ArrayFromString('earth'))
|
||||
|
||||
// let batch commit complete
|
||||
await Promise.all(commitSpy.returnValues)
|
||||
|
@ -1,7 +1,6 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
chai.use(dirtyChai)
|
||||
@ -9,13 +8,14 @@ const expect = chai.expect
|
||||
const duplexPair = require('it-pair/duplex')
|
||||
const pipe = require('it-pipe')
|
||||
const { collect } = require('streaming-iterables')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
const Protector = require('../../src/pnet')
|
||||
const Errors = Protector.errors
|
||||
const generate = Protector.generate
|
||||
|
||||
const swarmKeyBuffer = Buffer.alloc(95)
|
||||
const wrongSwarmKeyBuffer = Buffer.alloc(95)
|
||||
const swarmKeyBuffer = new Uint8Array(95)
|
||||
const wrongSwarmKeyBuffer = new Uint8Array(95)
|
||||
|
||||
// Write new psk files to the buffers
|
||||
generate(swarmKeyBuffer)
|
||||
@ -39,7 +39,7 @@ describe('private network', () => {
|
||||
])
|
||||
|
||||
pipe(
|
||||
[Buffer.from('hello world'), Buffer.from('doo dah')],
|
||||
[uint8ArrayFromString('hello world'), uint8ArrayFromString('doo dah')],
|
||||
aToB
|
||||
)
|
||||
|
||||
@ -53,7 +53,7 @@ describe('private network', () => {
|
||||
collect
|
||||
)
|
||||
|
||||
expect(output).to.eql([Buffer.from('hello world'), Buffer.from('doo dah')])
|
||||
expect(output).to.eql([uint8ArrayFromString('hello world'), uint8ArrayFromString('doo dah')])
|
||||
})
|
||||
|
||||
it('should not be able to share correct data with different keys', async () => {
|
||||
@ -67,7 +67,7 @@ describe('private network', () => {
|
||||
])
|
||||
|
||||
pipe(
|
||||
[Buffer.from('hello world'), Buffer.from('doo dah')],
|
||||
[uint8ArrayFromString('hello world'), uint8ArrayFromString('doo dah')],
|
||||
aToB
|
||||
)
|
||||
|
||||
@ -76,19 +76,19 @@ describe('private network', () => {
|
||||
collect
|
||||
)
|
||||
|
||||
expect(output).to.not.eql([Buffer.from('hello world'), Buffer.from('doo dah')])
|
||||
expect(output).to.not.eql([uint8ArrayFromString('hello world'), uint8ArrayFromString('doo dah')])
|
||||
})
|
||||
|
||||
describe('invalid psks', () => {
|
||||
it('should not accept a bad psk', () => {
|
||||
expect(() => {
|
||||
return new Protector(Buffer.from('not-a-key'))
|
||||
return new Protector(uint8ArrayFromString('not-a-key'))
|
||||
}).to.throw(Errors.INVALID_PSK)
|
||||
})
|
||||
|
||||
it('should not accept a psk of incorrect length', () => {
|
||||
expect(() => {
|
||||
return new Protector(Buffer.from('/key/swarm/psk/1.0.0/\n/base16/\ndffb7e'))
|
||||
return new Protector(uint8ArrayFromString('/key/swarm/psk/1.0.0/\n/base16/\ndffb7e'))
|
||||
}).to.throw(Errors.INVALID_PSK)
|
||||
})
|
||||
})
|
||||
|
@ -13,6 +13,7 @@ const Floodsub = require('libp2p-floodsub')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const { multicodec: floodsubMulticodec } = require('libp2p-floodsub')
|
||||
const { multicodec: gossipsubMulticodec } = require('libp2p-gossipsub')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
@ -81,7 +82,7 @@ describe('Pubsub subsystem is able to use different implementations', () => {
|
||||
expect(connection).to.exist()
|
||||
|
||||
libp2p.pubsub.subscribe(topic, (msg) => {
|
||||
expect(msg.data.toString()).to.equal(data)
|
||||
expect(uint8ArrayToString(msg.data)).to.equal(data)
|
||||
defer.resolve()
|
||||
})
|
||||
|
||||
|
@ -10,6 +10,7 @@ const pWaitFor = require('p-wait-for')
|
||||
const pDefer = require('p-defer')
|
||||
const mergeOptions = require('merge-options')
|
||||
const multiaddr = require('multiaddr')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const { create } = require('../../src')
|
||||
const { subsystemOptions, subsystemMulticodecs } = require('./utils')
|
||||
@ -82,7 +83,7 @@ describe('Pubsub subsystem operates correctly', () => {
|
||||
expect(subscribedTopics).to.not.include(topic)
|
||||
|
||||
libp2p.pubsub.subscribe(topic, (msg) => {
|
||||
expect(msg.data.toString()).to.equal(data)
|
||||
expect(uint8ArrayToString(msg.data)).to.equal(data)
|
||||
defer.resolve()
|
||||
})
|
||||
|
||||
@ -171,7 +172,7 @@ describe('Pubsub subsystem operates correctly', () => {
|
||||
expect(subscribedTopics).to.not.include(topic)
|
||||
|
||||
libp2p.pubsub.subscribe(topic, (msg) => {
|
||||
expect(msg.data.toString()).to.equal(data)
|
||||
expect(uint8ArrayToString(msg.data)).to.equal(data)
|
||||
defer.resolve()
|
||||
})
|
||||
|
||||
@ -242,7 +243,7 @@ describe('Pubsub subsystem operates correctly', () => {
|
||||
const defer1 = pDefer()
|
||||
const defer2 = pDefer()
|
||||
const handler = (msg) => {
|
||||
expect(msg.data.toString()).to.equal(data)
|
||||
expect(uint8ArrayToString(msg.data)).to.equal(data)
|
||||
counter++
|
||||
counter === 1 ? defer1.resolve() : defer2.resolve()
|
||||
}
|
||||
@ -307,7 +308,7 @@ describe('Pubsub subsystem operates correctly', () => {
|
||||
libp2p.pubsub.publish(topic, 'message1')
|
||||
remoteLibp2p.pubsub.publish(topic, 'message2')
|
||||
await pWaitFor(() => handlerSpy.callCount === 2)
|
||||
expect(handlerSpy.args.map(([message]) => message.data.toString())).to.include.members(['message1', 'message2'])
|
||||
expect(handlerSpy.args.map(([message]) => uint8ArrayToString(message.data))).to.include.members(['message1', 'message2'])
|
||||
|
||||
// Disconnect the first connection (this acts as a delayed reconnect)
|
||||
await originalConnection.close()
|
||||
@ -317,7 +318,7 @@ describe('Pubsub subsystem operates correctly', () => {
|
||||
libp2p.pubsub.publish(topic, 'message3')
|
||||
remoteLibp2p.pubsub.publish(topic, 'message4')
|
||||
await pWaitFor(() => handlerSpy.callCount === 2)
|
||||
expect(handlerSpy.args.map(([message]) => message.data.toString())).to.include.members(['message3', 'message4'])
|
||||
expect(handlerSpy.args.map(([message]) => uint8ArrayToString(message.data))).to.include.members(['message3', 'message4'])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -4,6 +4,7 @@
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
chai.use(require('chai-bytes'))
|
||||
chai.use(require('chai-as-promised'))
|
||||
const { expect } = chai
|
||||
|
||||
const Envelope = require('../../src/record/envelope')
|
||||
|
@ -1,11 +1,7 @@
|
||||
'use strict'
|
||||
/* eslint-env mocha */
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
chai.use(require('chai-as-promised'))
|
||||
const { expect } = chai
|
||||
const { expect } = require('aegir/utils/chai')
|
||||
const sinon = require('sinon')
|
||||
const Muxer = require('libp2p-mplex')
|
||||
const multiaddr = require('multiaddr')
|
||||
@ -16,7 +12,8 @@ const pSettle = require('p-settle')
|
||||
const Transport = require('libp2p-websockets')
|
||||
const { NOISE: Crypto } = require('libp2p-noise')
|
||||
const Protector = require('../../src/pnet')
|
||||
const swarmKeyBuffer = Buffer.from(require('../fixtures/swarm.key'))
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
const swarmKeyBuffer = uint8ArrayFromString(require('../fixtures/swarm.key'))
|
||||
|
||||
const Libp2p = require('../../src')
|
||||
const Upgrader = require('../../src/upgrader')
|
||||
@ -102,7 +99,7 @@ describe('Upgrader', () => {
|
||||
const { stream, protocol } = await connections[0].newStream('/echo/1.0.0')
|
||||
expect(protocol).to.equal('/echo/1.0.0')
|
||||
|
||||
const hello = Buffer.from('hello there!')
|
||||
const hello = uint8ArrayFromString('hello there!')
|
||||
const result = await pipe(
|
||||
[hello],
|
||||
stream,
|
||||
@ -172,7 +169,7 @@ describe('Upgrader', () => {
|
||||
const { stream, protocol } = await connections[0].newStream('/echo/1.0.0')
|
||||
expect(protocol).to.equal('/echo/1.0.0')
|
||||
|
||||
const hello = Buffer.from('hello there!')
|
||||
const hello = uint8ArrayFromString('hello there!')
|
||||
const result = await pipe(
|
||||
[hello],
|
||||
stream,
|
||||
|
Loading…
x
Reference in New Issue
Block a user