Compare commits

..

3 Commits

Author SHA1 Message Date
d168c7d531 chore: release version v0.6.0 2020-10-05 16:40:42 +02:00
349c1174db chore: update contributors 2020-10-05 16:40:41 +02:00
e14844315b feat: update pubsub getMsgId return type to Uint8Array (#65)
BREAKING CHANGE:
new getMsgId return type is not backwards compatible with prior `string`
return type.
2020-10-05 16:36:29 +02:00
7 changed files with 29 additions and 12 deletions

View File

@ -1,3 +1,19 @@
<a name="0.6.0"></a>
# [0.6.0](https://github.com/libp2p/js-interfaces/compare/v0.5.2...v0.6.0) (2020-10-05)
### Features
* update pubsub getMsgId return type to Uint8Array ([#65](https://github.com/libp2p/js-interfaces/issues/65)) ([e148443](https://github.com/libp2p/js-interfaces/commit/e148443))
### BREAKING CHANGES
* new getMsgId return type is not backwards compatible with prior `string`
return type.
<a name="0.5.2"></a>
## [0.5.2](https://github.com/libp2p/js-interfaces/compare/v0.3.1...v0.5.2) (2020-09-30)

View File

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

View File

@ -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

View File

@ -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)

View File

@ -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<any>, b: any[] | Set<any>): boolean;
export function ensureArray(maybeArray: any): any[];
export function normalizeInRpcMessage(message: any, peerId: string): any;

View File

@ -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
}
/**

View File

@ -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', () => {