Skip to content

Commit

Permalink
fix: replace node buffers with uint8arrays (#70)
Browse files Browse the repository at this point in the history
All uses of node Buffers have been replaced with Uint8Arrays

BREAKING CHANGES:

- The `.data`, `.from` and `.seq` properties of messages used to be
  node Buffers, now they are Uint8Arrays
- All deps of this module now use Uint8Arrays instead of Buffers
  • Loading branch information
achingbrain authored Aug 11, 2020
1 parent 1297ca1 commit 92632b5
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 33 deletions.
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
},
"homepage": "https://github.com/libp2p/js-libp2p-pubsub#readme",
"devDependencies": {
"aegir": "^22.0.0",
"aegir": "^25.0.0",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"chai-spies": "^1.0.0",
"dirty-chai": "^2.0.1",
"it-pair": "^1.0.0",
"multiaddr": "^7.2.1",
"multiaddr": "^8.0.0",
"sinon": "^9.0.0"
},
"dependencies": {
Expand All @@ -57,11 +57,12 @@
"it-length-prefixed": "^3.0.0",
"it-pipe": "^1.0.1",
"it-pushable": "^1.3.2",
"libp2p-crypto": "~0.17.0",
"libp2p-interfaces": "^0.3.0",
"multibase": "^0.7.0",
"peer-id": "~0.13.3",
"protons": "^1.0.1"
"libp2p-crypto": "^0.18.0",
"libp2p-interfaces": "^0.4.0",
"multibase": "^3.0.0",
"peer-id": "^0.14.0",
"protons": "^2.0.0",
"uint8arrays": "^1.1.0"
},
"contributors": [
"Vasco Santos <[email protected]>",
Expand Down
10 changes: 6 additions & 4 deletions src/message/sign.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict'
const { Buffer } = require('buffer')

const PeerId = require('peer-id')
const { Message } = require('./index')
const SignPrefix = Buffer.from('libp2p-pubsub:')
const uint8ArrayConcat = require('uint8arrays/concat')
const uint8ArrayFromString = require('uint8arrays/from-string')
const SignPrefix = uint8ArrayFromString('libp2p-pubsub:')

/**
* Signs the provided message with the given `peerId`
Expand All @@ -13,7 +15,7 @@ const SignPrefix = Buffer.from('libp2p-pubsub:')
*/
async function signMessage (peerId, message) {
// Get the message in bytes, and prepend with the pubsub prefix
const bytes = Buffer.concat([
const bytes = uint8ArrayConcat([
SignPrefix,
Message.encode(message)
])
Expand All @@ -37,7 +39,7 @@ async function verifySignature (message) {
const baseMessage = { ...message }
delete baseMessage.signature
delete baseMessage.key
const bytes = Buffer.concat([
const bytes = uint8ArrayConcat([
SignPrefix,
Message.encode(baseMessage)
])
Expand Down
2 changes: 1 addition & 1 deletion src/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Peer extends EventEmitter {
* Send a message to this peer.
* Throws if there is no `stream` to write to available.
*
* @param {Buffer} msg
* @param {Uint8Array} msg
* @returns {undefined}
*/
write (msg) {
Expand Down
18 changes: 11 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
'use strict'

const { Buffer } = require('buffer')
const crypto = require('libp2p-crypto')
const multibase = require('multibase')
const uint8ArrayToString = require('uint8arrays/to-string')
const uint8ArrayFromString = require('uint8arrays/from-string')

exports = module.exports

/**
* Generatea random sequence number.
*
* @returns {Buffer}
* @returns {Uint8Array}
* @private
*/
exports.randomSeqno = () => {
Expand All @@ -20,12 +21,12 @@ exports.randomSeqno = () => {
* Generate a message id, based on the `from` and `seqno`.
*
* @param {string} from
* @param {Buffer} seqno
* @param {Uint8Array} seqno
* @returns {string}
* @private
*/
exports.msgId = (from, seqno) => {
return from + seqno.toString('hex')
return from + uint8ArrayToString(seqno, 'base16')
}

/**
Expand Down Expand Up @@ -72,13 +73,13 @@ exports.ensureArray = (maybeArray) => {
/**
* Ensures `message.from` is base58 encoded
* @param {Object} message
* @param {Buffer|String} message.from
* @param {Uint8Array|String} message.from
* @return {Object}
*/
exports.normalizeInRpcMessage = (message) => {
const m = Object.assign({}, message)
if (Buffer.isBuffer(message.from)) {
m.from = multibase.encode('base58btc', message.from).toString().slice(1)
if (message.from instanceof Uint8Array) {
m.from = uint8ArrayToString(message.from, 'base58btc')
}
return m
}
Expand All @@ -100,6 +101,9 @@ exports.normalizeOutRpcMessage = (message) => {
if (typeof message.from === 'string' || message.from instanceof String) {
m.from = multibase.decode('z' + message.from)
}
if (typeof message.data === 'string' || message.data instanceof String) {
m.data = uint8ArrayFromString(message.data)
}
return m
}

Expand Down
15 changes: 8 additions & 7 deletions test/sign.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
/* eslint max-nested-callbacks: ["error", 5] */
'use strict'

const { Buffer } = require('buffer')
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const uint8ArrayConcat = require('uint8arrays/concat')
const uint8ArrayFromString = require('uint8arrays/from-string')

const { Message } = require('../src/message')
const {
Expand All @@ -27,12 +28,12 @@ describe('message signing', () => {
it('should be able to sign and verify a message', async () => {
const message = {
from: peerId.id,
data: 'hello',
data: uint8ArrayFromString('hello'),
seqno: randomSeqno(),
topicIDs: ['test-topic']
}

const bytesToSign = Buffer.concat([SignPrefix, Message.encode(message)])
const bytesToSign = uint8ArrayConcat([SignPrefix, Message.encode(message)])
const expectedSignature = await peerId.privKey.sign(bytesToSign)

const signedMessage = await signMessage(peerId, message)
Expand All @@ -51,12 +52,12 @@ describe('message signing', () => {

const message = {
from: secPeerId.id,
data: 'hello',
data: uint8ArrayFromString('hello'),
seqno: randomSeqno(),
topicIDs: ['test-topic']
}

const bytesToSign = Buffer.concat([SignPrefix, Message.encode(message)])
const bytesToSign = uint8ArrayConcat([SignPrefix, Message.encode(message)])
const expectedSignature = await secPeerId.privKey.sign(bytesToSign)

const signedMessage = await signMessage(secPeerId, message)
Expand All @@ -73,12 +74,12 @@ describe('message signing', () => {
it('should be able to extract the public key from the message', async () => {
const message = {
from: peerId.id,
data: 'hello',
data: uint8ArrayFromString('hello'),
seqno: randomSeqno(),
topicIDs: ['test-topic']
}

const bytesToSign = Buffer.concat([SignPrefix, Message.encode(message)])
const bytesToSign = uint8ArrayConcat([SignPrefix, Message.encode(message)])
const expectedSignature = await peerId.privKey.sign(bytesToSign)

const signedMessage = await signMessage(peerId, message)
Expand Down
14 changes: 7 additions & 7 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
'use strict'

const { expect } = require('chai')
const { Buffer } = require('buffer')
const utils = require('../src/utils')
const uint8ArrayFromString = require('uint8arrays/from-string')

describe('utils', () => {
it('randomSeqno', () => {
Expand All @@ -16,13 +16,13 @@ describe('utils', () => {
})

it('msgId', () => {
expect(utils.msgId('hello', Buffer.from('world'))).to.be.eql('hello776f726c64')
expect(utils.msgId('hello', uint8ArrayFromString('world'))).to.be.eql('hello776f726c64')
})

it('msgId should not generate same ID for two different buffers', () => {
it('msgId should not generate same ID for two different Uint8Arrays', () => {
const peerId = 'QmPNdSYk5Rfpo5euNqwtyizzmKXMNHdXeLjTQhcN4yfX22'
const msgId0 = utils.msgId(peerId, Buffer.from('15603533e990dfde', 'hex'))
const msgId1 = utils.msgId(peerId, Buffer.from('15603533e990dfe0', 'hex'))
const msgId0 = utils.msgId(peerId, uint8ArrayFromString('15603533e990dfde', 'base16'))
const msgId1 = utils.msgId(peerId, uint8ArrayFromString('15603533e990dfe0', 'base16'))
expect(msgId0).to.not.eql(msgId1)
})

Expand All @@ -49,7 +49,7 @@ describe('utils', () => {
})

it('converts an IN msg.from to b58', () => {
const binaryId = Buffer.from('1220e2187eb3e6c4fb3e7ff9ad4658610624a6315e0240fc6f37130eedb661e939cc', 'hex')
const binaryId = uint8ArrayFromString('1220e2187eb3e6c4fb3e7ff9ad4658610624a6315e0240fc6f37130eedb661e939cc', 'base16')
const stringId = 'QmdZEWgtaWAxBh93fELFT298La1rsZfhiC2pqwMVwy3jZM'
const m = [
{ from: binaryId },
Expand All @@ -63,7 +63,7 @@ describe('utils', () => {
})

it('converts an OUT msg.from to binary', () => {
const binaryId = Buffer.from('1220e2187eb3e6c4fb3e7ff9ad4658610624a6315e0240fc6f37130eedb661e939cc', 'hex')
const binaryId = uint8ArrayFromString('1220e2187eb3e6c4fb3e7ff9ad4658610624a6315e0240fc6f37130eedb661e939cc', 'base16')
const stringId = 'QmdZEWgtaWAxBh93fELFT298La1rsZfhiC2pqwMVwy3jZM'
const m = [
{ from: binaryId },
Expand Down

0 comments on commit 92632b5

Please sign in to comment.