Compare commits

..

6 Commits

Author SHA1 Message Date
Vasco Santos
83d7d52d7e chore: release version v0.7.2 2020-11-11 17:20:34 +01:00
Vasco Santos
1a3ea82776 chore: update contributors 2020-11-11 17:20:34 +01:00
Cayman
ad2dfa42dc chore: pubsub conformance test updates (#70) 2020-11-11 17:16:49 +01:00
Vasco Santos
b75f2cab48 chore: release version v0.7.1 2020-11-03 22:43:57 +01:00
Vasco Santos
8512997e76 chore: update contributors 2020-11-03 22:43:57 +01:00
Cayman
269a6f5e0a fix: typescript types (#69) 2020-11-03 22:35:18 +01:00
11 changed files with 56 additions and 41 deletions

View File

@@ -1,3 +1,18 @@
<a name="0.7.2"></a>
## [0.7.2](https://github.com/libp2p/js-interfaces/compare/v0.7.1...v0.7.2) (2020-11-11)
<a name="0.7.1"></a>
## [0.7.1](https://github.com/libp2p/js-interfaces/compare/v0.7.0...v0.7.1) (2020-11-03)
### Bug Fixes
* typescript types ([#69](https://github.com/libp2p/js-interfaces/issues/69)) ([269a6f5](https://github.com/libp2p/js-interfaces/commit/269a6f5))
<a name="0.7.0"></a> <a name="0.7.0"></a>
# [0.7.0](https://github.com/libp2p/js-interfaces/compare/v0.5.2...v0.7.0) (2020-11-03) # [0.7.0](https://github.com/libp2p/js-interfaces/compare/v0.5.2...v0.7.0) (2020-11-03)

View File

@@ -1,6 +1,6 @@
{ {
"name": "libp2p-interfaces", "name": "libp2p-interfaces",
"version": "0.7.0", "version": "0.7.2",
"description": "Interfaces for JS Libp2p", "description": "Interfaces for JS Libp2p",
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>", "leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
"main": "src/index.js", "main": "src/index.js",

View File

@@ -1,4 +1,11 @@
export namespace codes { export namespace codes {
export const ERR_INVALID_SIGNATURE_POLICY: string;
export const ERR_UNHANDLED_SIGNATURE_POLICY: string;
export const ERR_MISSING_SIGNATURE: string; export const ERR_MISSING_SIGNATURE: string;
export const ERR_MISSING_SEQNO: string;
export const ERR_INVALID_SIGNATURE: string; export const ERR_INVALID_SIGNATURE: string;
export const ERR_UNEXPECTED_FROM: string;
export const ERR_UNEXPECTED_SIGNATURE: string;
export const ERR_UNEXPECTED_KEY: string;
export const ERR_UNEXPECTED_SEQNO: string;
} }

22
src/pubsub/index.d.ts vendored
View File

@@ -22,18 +22,16 @@ declare class PubsubBaseProtocol {
* @param {String} props.debugName log namespace * @param {String} props.debugName log namespace
* @param {Array<string>|string} props.multicodecs protocol identificers to connect * @param {Array<string>|string} props.multicodecs protocol identificers to connect
* @param {Libp2p} props.libp2p * @param {Libp2p} props.libp2p
* @param {boolean} [props.signMessages = true] if messages should be signed * @param {SignaturePolicy} [props.globalSignaturePolicy = SignaturePolicy.StrictSign] defines how signatures should be handled
* @param {boolean} [props.strictSigning = true] if message signing should be required
* @param {boolean} [props.canRelayMessage = false] if can relay messages not subscribed * @param {boolean} [props.canRelayMessage = false] if can relay messages not subscribed
* @param {boolean} [props.emitSelf = false] if publish should emit to self, if subscribed * @param {boolean} [props.emitSelf = false] if publish should emit to self, if subscribed
* @abstract * @abstract
*/ */
constructor({ debugName, multicodecs, libp2p, signMessages, strictSigning, canRelayMessage, emitSelf }: { constructor({ debugName, multicodecs, libp2p, globalSignaturePolicy, canRelayMessage, emitSelf }: {
debugName: string; debugName: string;
multicodecs: string | string[]; multicodecs: string | string[];
libp2p: any; libp2p: any;
signMessages?: boolean; globalSignaturePolicy?: any;
strictSigning?: boolean;
canRelayMessage?: boolean; canRelayMessage?: boolean;
emitSelf?: boolean; emitSelf?: boolean;
}); });
@@ -66,12 +64,12 @@ declare class PubsubBaseProtocol {
* @type {Map<string, import('./peer-streams')>} * @type {Map<string, import('./peer-streams')>}
*/ */
peers: Map<string, import('./peer-streams')>; peers: Map<string, import('./peer-streams')>;
signMessages: boolean;
/** /**
* If message signing should be required for incoming messages * The signature policy to follow by default
* @type {boolean} *
* @type {string}
*/ */
strictSigning: boolean; globalSignaturePolicy: string;
/** /**
* If router can relay received messages, even if not subscribed * If router can relay received messages, even if not subscribed
* @type {boolean} * @type {boolean}
@@ -284,7 +282,7 @@ declare class PubsubBaseProtocol {
getTopics(): string[]; getTopics(): string[];
} }
declare namespace PubsubBaseProtocol { declare namespace PubsubBaseProtocol {
export { message, utils, InMessage, PeerId }; export { message, utils, SignaturePolicy, InMessage, PeerId };
} }
type PeerId = import("peer-id"); type PeerId = import("peer-id");
/** /**
@@ -305,3 +303,7 @@ type InMessage = {
*/ */
declare const message: typeof import('./message'); declare const message: typeof import('./message');
declare const utils: typeof import("./utils"); declare const utils: typeof import("./utils");
declare const SignaturePolicy: {
StrictSign: string;
StrictNoSign: string;
};

View File

@@ -116,7 +116,7 @@ class PubsubBaseProtocol extends EventEmitter {
/** /**
* The signature policy to follow by default * The signature policy to follow by default
* *
* @type {SignaturePolicy} * @type {string}
*/ */
this.globalSignaturePolicy = globalSignaturePolicy this.globalSignaturePolicy = globalSignaturePolicy

4
src/pubsub/signature-policy.d.ts vendored Normal file
View File

@@ -0,0 +1,4 @@
export namespace SignaturePolicy {
export const StrictSign: string;
export const StrictNoSign: string;
}

View File

@@ -76,7 +76,7 @@ module.exports = (common) => {
const defer = pDefer() const defer = pDefer()
const handler = (msg) => { const handler = (msg) => {
expect(msg).to.exist() expect(msg).to.not.eql(undefined)
defer.resolve() defer.resolve()
} }

View File

@@ -10,6 +10,7 @@ const uint8ArrayFromString = require('uint8arrays/from-string')
const { utils } = require('..') const { utils } = require('..')
const PeerStreams = require('../peer-streams') const PeerStreams = require('../peer-streams')
const { SignaturePolicy } = require('../signature-policy')
const topic = 'foo' const topic = 'foo'
const data = uint8ArrayFromString('bar') const data = uint8ArrayFromString('bar')
@@ -31,24 +32,17 @@ module.exports = (common) => {
}) })
it('should emit normalized signed messages on publish', async () => { it('should emit normalized signed messages on publish', async () => {
pubsub.globalSignaturePolicy = SignaturePolicy.StrictSign
sinon.spy(pubsub, '_emitMessage') sinon.spy(pubsub, '_emitMessage')
sinon.spy(utils, 'randomSeqno')
await pubsub.publish(topic, data) await pubsub.publish(topic, data)
expect(pubsub._emitMessage.callCount).to.eql(1) expect(pubsub._emitMessage.callCount).to.eql(1)
const [messageToEmit] = pubsub._emitMessage.getCall(0).args const [messageToEmit] = pubsub._emitMessage.getCall(0).args
const expected = utils.normalizeInRpcMessage( expect(messageToEmit.seqno).to.not.eql(undefined)
await pubsub._buildMessage({ expect(messageToEmit.key).to.not.eql(undefined)
receivedFrom: pubsub.peerId.toB58String(), expect(messageToEmit.signature).to.not.eql(undefined)
from: pubsub.peerId.toB58String(),
data,
seqno: utils.randomSeqno.getCall(0).returnValue,
topicIDs: [topic]
}))
expect(messageToEmit).to.eql(expected)
}) })
it('should drop unsigned messages', async () => { it('should drop unsigned messages', async () => {
@@ -83,18 +77,16 @@ module.exports = (common) => {
}) })
it('should not drop unsigned messages if strict signing is disabled', async () => { it('should not drop unsigned messages if strict signing is disabled', async () => {
pubsub.globalSignaturePolicy = SignaturePolicy.StrictNoSign
sinon.spy(pubsub, '_emitMessage') sinon.spy(pubsub, '_emitMessage')
sinon.spy(pubsub, '_publish') sinon.spy(pubsub, '_publish')
sinon.spy(pubsub, 'validate') sinon.spy(pubsub, 'validate')
sinon.stub(pubsub, 'strictSigning').value(false)
const peerStream = new PeerStreams({ id: await PeerId.create() }) const peerStream = new PeerStreams({ id: await PeerId.create() })
const rpc = { const rpc = {
subscriptions: [], subscriptions: [],
msgs: [{ msgs: [{
from: peerStream.id.toBytes(),
data, data,
seqno: utils.randomSeqno(),
topicIDs: [topic] topicIDs: [topic]
}] }]
} }

View File

@@ -52,26 +52,20 @@ module.exports = (common) => {
await common.teardown() await common.teardown()
}) })
it('subscribe to the topic on node a', () => { it('subscribe to the topic on node a', async () => {
const topic = 'Z' const topic = 'Z'
const defer = pDefer()
psA.subscribe(topic) psA.subscribe(topic)
expectSet(psA.subscriptions, [topic]) expectSet(psA.subscriptions, [topic])
psB.once('pubsub:subscription-change', () => { await new Promise((resolve) => psB.once('pubsub:subscription-change', resolve))
expect(psB.peers.size).to.equal(2) expect(psB.peers.size).to.equal(2)
const aPeerId = psA.peerId.toB58String() const aPeerId = psA.peerId.toB58String()
expectSet(psB.topics.get(topic), [aPeerId]) expectSet(psB.topics.get(topic), [aPeerId])
expect(psC.peers.size).to.equal(1) expect(psC.peers.size).to.equal(1)
expect(psC.topics.get(topic)).to.not.exist() expect(psC.topics.get(topic)).to.eql(undefined)
defer.resolve()
})
return defer.promise
}) })
it('subscribe to the topic on node b', async () => { it('subscribe to the topic on node b', async () => {

View File

@@ -1,5 +1,6 @@
export function randomSeqno(): Uint8Array; export function randomSeqno(): Uint8Array;
export function msgId(from: string, seqno: Uint8Array): Uint8Array; export function msgId(from: string, seqno: Uint8Array): Uint8Array;
export function noSignMsgId(data: Uint8Array): Uint8Array;
export function anyMatch(a: any[] | Set<any>, b: any[] | Set<any>): boolean; export function anyMatch(a: any[] | Set<any>, b: any[] | Set<any>): boolean;
export function ensureArray(maybeArray: any): any[]; export function ensureArray(maybeArray: any): any[];
export function normalizeInRpcMessage(message: any, peerId: string): any; export function normalizeInRpcMessage(message: any, peerId: string): any;

View File

@@ -40,7 +40,7 @@ exports.msgId = (from, seqno) => {
* @returns {Uint8Array} * @returns {Uint8Array}
* @private * @private
*/ */
exports.noSignMsgId = (data) => multihash.encode(data, 'sha2') exports.noSignMsgId = (data) => multihash.encode(data, 'sha2-256')
/** /**
* Check if any member of the first set is also a member * Check if any member of the first set is also a member