From e14844315bc76f3a4057783ea4c608ac577aa52a Mon Sep 17 00:00:00 2001 From: Cayman Date: Mon, 5 Oct 2020 08:36:29 -0600 Subject: [PATCH] feat: update pubsub getMsgId return type to Uint8Array (#65) BREAKING CHANGE: new getMsgId return type is not backwards compatible with prior `string` return type. --- src/pubsub/index.d.ts | 4 ++-- src/pubsub/index.js | 2 +- src/pubsub/utils.d.ts | 2 +- src/pubsub/utils.js | 9 +++++++-- test/pubsub/utils.spec.js | 6 +----- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/pubsub/index.d.ts b/src/pubsub/index.d.ts index d4f7dbd..cb79e2c 100644 --- a/src/pubsub/index.d.ts +++ b/src/pubsub/index.d.ts @@ -184,9 +184,9 @@ declare class PubsubBaseProtocol { * The default msgID implementation * Child class can override this. * @param {RPC.Message} msg the message object - * @returns {string} message id as string + * @returns {Uint8Array} message id as bytes */ - getMsgId(msg: any): string; + getMsgId(msg: any): Uint8Array; /** * Whether to accept a message from a peer * Override to create a graylist diff --git a/src/pubsub/index.js b/src/pubsub/index.js index f7d1af6..8728369 100644 --- a/src/pubsub/index.js +++ b/src/pubsub/index.js @@ -437,7 +437,7 @@ class PubsubBaseProtocol extends EventEmitter { * The default msgID implementation * Child class can override this. * @param {RPC.Message} msg the message object - * @returns {string} message id as string + * @returns {Uint8Array} message id as bytes */ getMsgId (msg) { return utils.msgId(msg.from, msg.seqno) diff --git a/src/pubsub/utils.d.ts b/src/pubsub/utils.d.ts index b237b3f..6a8c1f6 100644 --- a/src/pubsub/utils.d.ts +++ b/src/pubsub/utils.d.ts @@ -1,5 +1,5 @@ export function randomSeqno(): Uint8Array; -export function msgId(from: string, seqno: Uint8Array): string; +export function msgId(from: string, seqno: Uint8Array): Uint8Array; export function anyMatch(a: any[] | Set, b: any[] | Set): boolean; export function ensureArray(maybeArray: any): any[]; export function normalizeInRpcMessage(message: any, peerId: string): any; diff --git a/src/pubsub/utils.js b/src/pubsub/utils.js index 3663676..bc51005 100644 --- a/src/pubsub/utils.js +++ b/src/pubsub/utils.js @@ -3,6 +3,7 @@ const randomBytes = require('libp2p-crypto/src/random-bytes') const uint8ArrayToString = require('uint8arrays/to-string') const uint8ArrayFromString = require('uint8arrays/from-string') +const PeerId = require('peer-id') exports = module.exports /** @@ -20,11 +21,15 @@ exports.randomSeqno = () => { * * @param {string} from * @param {Uint8Array} seqno - * @returns {string} + * @returns {Uint8Array} * @private */ exports.msgId = (from, seqno) => { - return from + uint8ArrayToString(seqno, 'base16') + 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 } /** diff --git a/test/pubsub/utils.spec.js b/test/pubsub/utils.spec.js index 439e0da..810a2cb 100644 --- a/test/pubsub/utils.spec.js +++ b/test/pubsub/utils.spec.js @@ -15,15 +15,11 @@ describe('utils', () => { expect(first).to.not.eql(second) }) - it('msgId', () => { - expect(utils.msgId('hello', uint8ArrayFromString('world'))).to.be.eql('hello776f726c64') - }) - it('msgId should not generate same ID for two different Uint8Arrays', () => { const peerId = 'QmPNdSYk5Rfpo5euNqwtyizzmKXMNHdXeLjTQhcN4yfX22' const msgId0 = utils.msgId(peerId, uint8ArrayFromString('15603533e990dfde', 'base16')) const msgId1 = utils.msgId(peerId, uint8ArrayFromString('15603533e990dfe0', 'base16')) - expect(msgId0).to.not.eql(msgId1) + expect(msgId0).to.not.deep.equal(msgId1) }) it('anyMatch', () => {