2020-08-25 13:05:58 +02:00
|
|
|
'use strict'
|
2020-11-30 20:46:04 -08:00
|
|
|
/* eslint-disable valid-jsdoc */
|
2020-08-25 13:05:58 +02:00
|
|
|
|
|
|
|
const randomBytes = require('libp2p-crypto/src/random-bytes')
|
|
|
|
const uint8ArrayToString = require('uint8arrays/to-string')
|
|
|
|
const uint8ArrayFromString = require('uint8arrays/from-string')
|
2020-10-05 08:36:29 -06:00
|
|
|
const PeerId = require('peer-id')
|
2020-11-03 10:22:03 -07:00
|
|
|
const multihash = require('multihashes')
|
2020-08-25 13:05:58 +02:00
|
|
|
exports = module.exports
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generatea random sequence number.
|
|
|
|
*
|
|
|
|
* @returns {Uint8Array}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
exports.randomSeqno = () => {
|
|
|
|
return randomBytes(8)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a message id, based on the `from` and `seqno`.
|
|
|
|
*
|
|
|
|
* @param {string} from
|
|
|
|
* @param {Uint8Array} seqno
|
2020-10-05 08:36:29 -06:00
|
|
|
* @returns {Uint8Array}
|
2020-08-25 13:05:58 +02:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
exports.msgId = (from, seqno) => {
|
2020-10-05 08:36:29 -06:00
|
|
|
const fromBytes = PeerId.createFromB58String(from).id
|
|
|
|
const msgId = new Uint8Array(fromBytes.length + seqno.length)
|
|
|
|
msgId.set(fromBytes, 0)
|
|
|
|
msgId.set(seqno, fromBytes.length)
|
|
|
|
return msgId
|
2020-08-25 13:05:58 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 10:22:03 -07:00
|
|
|
/**
|
|
|
|
* Generate a message id, based on message `data`.
|
|
|
|
*
|
|
|
|
* @param {Uint8Array} data
|
|
|
|
* @returns {Uint8Array}
|
|
|
|
* @private
|
|
|
|
*/
|
2020-11-11 09:16:49 -07:00
|
|
|
exports.noSignMsgId = (data) => multihash.encode(data, 'sha2-256')
|
2020-11-03 10:22:03 -07:00
|
|
|
|
2020-08-25 13:05:58 +02:00
|
|
|
/**
|
|
|
|
* Check if any member of the first set is also a member
|
|
|
|
* of the second set.
|
|
|
|
*
|
|
|
|
* @param {Set|Array} a
|
|
|
|
* @param {Set|Array} b
|
|
|
|
* @returns {boolean}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
exports.anyMatch = (a, b) => {
|
|
|
|
let bHas
|
|
|
|
if (Array.isArray(b)) {
|
|
|
|
bHas = (val) => b.indexOf(val) > -1
|
|
|
|
} else {
|
|
|
|
bHas = (val) => b.has(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const val of a) {
|
|
|
|
if (bHas(val)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make everything an array.
|
|
|
|
*
|
2020-11-30 13:59:09 -08:00
|
|
|
* @template T
|
|
|
|
* @param {T|T[]} maybeArray
|
|
|
|
* @returns {T[]}
|
2020-08-25 13:05:58 +02:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
exports.ensureArray = (maybeArray) => {
|
|
|
|
if (!Array.isArray(maybeArray)) {
|
|
|
|
return [maybeArray]
|
|
|
|
}
|
|
|
|
|
|
|
|
return maybeArray
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures `message.from` is base58 encoded
|
2020-11-30 13:59:09 -08:00
|
|
|
*
|
|
|
|
* @template {Object} T
|
|
|
|
* @param {T} message
|
|
|
|
* @param {string} [peerId]
|
|
|
|
* @return {T & {from?: string, peerId?: string }}
|
2020-08-25 13:05:58 +02:00
|
|
|
*/
|
|
|
|
exports.normalizeInRpcMessage = (message, peerId) => {
|
|
|
|
const m = Object.assign({}, message)
|
|
|
|
if (message.from instanceof Uint8Array) {
|
|
|
|
m.from = uint8ArrayToString(message.from, 'base58btc')
|
|
|
|
}
|
|
|
|
if (peerId) {
|
|
|
|
m.receivedFrom = peerId
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:21:11 +02:00
|
|
|
/**
|
2020-11-30 13:59:09 -08:00
|
|
|
* @template {Object} T
|
2020-11-30 20:46:04 -08:00
|
|
|
*
|
2020-11-30 13:59:09 -08:00
|
|
|
* @param {T} message
|
|
|
|
* @return {T & {from?: Uint8Array, data?: Uint8Array}}
|
2020-09-30 11:21:11 +02:00
|
|
|
*/
|
2020-08-25 13:05:58 +02:00
|
|
|
exports.normalizeOutRpcMessage = (message) => {
|
|
|
|
const m = Object.assign({}, message)
|
|
|
|
if (typeof message.from === 'string' || message.from instanceof String) {
|
|
|
|
m.from = uint8ArrayFromString(message.from, 'base58btc')
|
|
|
|
}
|
|
|
|
if (typeof message.data === 'string' || message.data instanceof String) {
|
|
|
|
m.data = uint8ArrayFromString(message.data)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|