Skip to content

Commit 92632b5

Browse files
authored
fix: replace node buffers with uint8arrays (#70)
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
1 parent 1297ca1 commit 92632b5

File tree

6 files changed

+41
-33
lines changed

6 files changed

+41
-33
lines changed

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@
4242
},
4343
"homepage": "https://github.com/libp2p/js-libp2p-pubsub#readme",
4444
"devDependencies": {
45-
"aegir": "^22.0.0",
45+
"aegir": "^25.0.0",
4646
"benchmark": "^2.1.4",
4747
"chai": "^4.2.0",
4848
"chai-spies": "^1.0.0",
4949
"dirty-chai": "^2.0.1",
5050
"it-pair": "^1.0.0",
51-
"multiaddr": "^7.2.1",
51+
"multiaddr": "^8.0.0",
5252
"sinon": "^9.0.0"
5353
},
5454
"dependencies": {
@@ -57,11 +57,12 @@
5757
"it-length-prefixed": "^3.0.0",
5858
"it-pipe": "^1.0.1",
5959
"it-pushable": "^1.3.2",
60-
"libp2p-crypto": "~0.17.0",
61-
"libp2p-interfaces": "^0.3.0",
62-
"multibase": "^0.7.0",
63-
"peer-id": "~0.13.3",
64-
"protons": "^1.0.1"
60+
"libp2p-crypto": "^0.18.0",
61+
"libp2p-interfaces": "^0.4.0",
62+
"multibase": "^3.0.0",
63+
"peer-id": "^0.14.0",
64+
"protons": "^2.0.0",
65+
"uint8arrays": "^1.1.0"
6566
},
6667
"contributors": [
6768
"Vasco Santos <[email protected]>",

src/message/sign.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
'use strict'
2-
const { Buffer } = require('buffer')
2+
33
const PeerId = require('peer-id')
44
const { Message } = require('./index')
5-
const SignPrefix = Buffer.from('libp2p-pubsub:')
5+
const uint8ArrayConcat = require('uint8arrays/concat')
6+
const uint8ArrayFromString = require('uint8arrays/from-string')
7+
const SignPrefix = uint8ArrayFromString('libp2p-pubsub:')
68

79
/**
810
* Signs the provided message with the given `peerId`
@@ -13,7 +15,7 @@ const SignPrefix = Buffer.from('libp2p-pubsub:')
1315
*/
1416
async function signMessage (peerId, message) {
1517
// Get the message in bytes, and prepend with the pubsub prefix
16-
const bytes = Buffer.concat([
18+
const bytes = uint8ArrayConcat([
1719
SignPrefix,
1820
Message.encode(message)
1921
])
@@ -37,7 +39,7 @@ async function verifySignature (message) {
3739
const baseMessage = { ...message }
3840
delete baseMessage.signature
3941
delete baseMessage.key
40-
const bytes = Buffer.concat([
42+
const bytes = uint8ArrayConcat([
4143
SignPrefix,
4244
Message.encode(baseMessage)
4345
])

src/peer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Peer extends EventEmitter {
6767
* Send a message to this peer.
6868
* Throws if there is no `stream` to write to available.
6969
*
70-
* @param {Buffer} msg
70+
* @param {Uint8Array} msg
7171
* @returns {undefined}
7272
*/
7373
write (msg) {

src/utils.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const crypto = require('libp2p-crypto')
54
const multibase = require('multibase')
5+
const uint8ArrayToString = require('uint8arrays/to-string')
6+
const uint8ArrayFromString = require('uint8arrays/from-string')
67

78
exports = module.exports
89

910
/**
1011
* Generatea random sequence number.
1112
*
12-
* @returns {Buffer}
13+
* @returns {Uint8Array}
1314
* @private
1415
*/
1516
exports.randomSeqno = () => {
@@ -20,12 +21,12 @@ exports.randomSeqno = () => {
2021
* Generate a message id, based on the `from` and `seqno`.
2122
*
2223
* @param {string} from
23-
* @param {Buffer} seqno
24+
* @param {Uint8Array} seqno
2425
* @returns {string}
2526
* @private
2627
*/
2728
exports.msgId = (from, seqno) => {
28-
return from + seqno.toString('hex')
29+
return from + uint8ArrayToString(seqno, 'base16')
2930
}
3031

3132
/**
@@ -72,13 +73,13 @@ exports.ensureArray = (maybeArray) => {
7273
/**
7374
* Ensures `message.from` is base58 encoded
7475
* @param {Object} message
75-
* @param {Buffer|String} message.from
76+
* @param {Uint8Array|String} message.from
7677
* @return {Object}
7778
*/
7879
exports.normalizeInRpcMessage = (message) => {
7980
const m = Object.assign({}, message)
80-
if (Buffer.isBuffer(message.from)) {
81-
m.from = multibase.encode('base58btc', message.from).toString().slice(1)
81+
if (message.from instanceof Uint8Array) {
82+
m.from = uint8ArrayToString(message.from, 'base58btc')
8283
}
8384
return m
8485
}
@@ -100,6 +101,9 @@ exports.normalizeOutRpcMessage = (message) => {
100101
if (typeof message.from === 'string' || message.from instanceof String) {
101102
m.from = multibase.decode('z' + message.from)
102103
}
104+
if (typeof message.data === 'string' || message.data instanceof String) {
105+
m.data = uint8ArrayFromString(message.data)
106+
}
103107
return m
104108
}
105109

test/sign.spec.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
/* eslint max-nested-callbacks: ["error", 5] */
33
'use strict'
44

5-
const { Buffer } = require('buffer')
65
const chai = require('chai')
76
chai.use(require('dirty-chai'))
87
const expect = chai.expect
8+
const uint8ArrayConcat = require('uint8arrays/concat')
9+
const uint8ArrayFromString = require('uint8arrays/from-string')
910

1011
const { Message } = require('../src/message')
1112
const {
@@ -27,12 +28,12 @@ describe('message signing', () => {
2728
it('should be able to sign and verify a message', async () => {
2829
const message = {
2930
from: peerId.id,
30-
data: 'hello',
31+
data: uint8ArrayFromString('hello'),
3132
seqno: randomSeqno(),
3233
topicIDs: ['test-topic']
3334
}
3435

35-
const bytesToSign = Buffer.concat([SignPrefix, Message.encode(message)])
36+
const bytesToSign = uint8ArrayConcat([SignPrefix, Message.encode(message)])
3637
const expectedSignature = await peerId.privKey.sign(bytesToSign)
3738

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

5253
const message = {
5354
from: secPeerId.id,
54-
data: 'hello',
55+
data: uint8ArrayFromString('hello'),
5556
seqno: randomSeqno(),
5657
topicIDs: ['test-topic']
5758
}
5859

59-
const bytesToSign = Buffer.concat([SignPrefix, Message.encode(message)])
60+
const bytesToSign = uint8ArrayConcat([SignPrefix, Message.encode(message)])
6061
const expectedSignature = await secPeerId.privKey.sign(bytesToSign)
6162

6263
const signedMessage = await signMessage(secPeerId, message)
@@ -73,12 +74,12 @@ describe('message signing', () => {
7374
it('should be able to extract the public key from the message', async () => {
7475
const message = {
7576
from: peerId.id,
76-
data: 'hello',
77+
data: uint8ArrayFromString('hello'),
7778
seqno: randomSeqno(),
7879
topicIDs: ['test-topic']
7980
}
8081

81-
const bytesToSign = Buffer.concat([SignPrefix, Message.encode(message)])
82+
const bytesToSign = uint8ArrayConcat([SignPrefix, Message.encode(message)])
8283
const expectedSignature = await peerId.privKey.sign(bytesToSign)
8384

8485
const signedMessage = await signMessage(peerId, message)

test/utils.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
'use strict'
33

44
const { expect } = require('chai')
5-
const { Buffer } = require('buffer')
65
const utils = require('../src/utils')
6+
const uint8ArrayFromString = require('uint8arrays/from-string')
77

88
describe('utils', () => {
99
it('randomSeqno', () => {
@@ -16,13 +16,13 @@ describe('utils', () => {
1616
})
1717

1818
it('msgId', () => {
19-
expect(utils.msgId('hello', Buffer.from('world'))).to.be.eql('hello776f726c64')
19+
expect(utils.msgId('hello', uint8ArrayFromString('world'))).to.be.eql('hello776f726c64')
2020
})
2121

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

@@ -49,7 +49,7 @@ describe('utils', () => {
4949
})
5050

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

6565
it('converts an OUT msg.from to binary', () => {
66-
const binaryId = Buffer.from('1220e2187eb3e6c4fb3e7ff9ad4658610624a6315e0240fc6f37130eedb661e939cc', 'hex')
66+
const binaryId = uint8ArrayFromString('1220e2187eb3e6c4fb3e7ff9ad4658610624a6315e0240fc6f37130eedb661e939cc', 'base16')
6767
const stringId = 'QmdZEWgtaWAxBh93fELFT298La1rsZfhiC2pqwMVwy3jZM'
6868
const m = [
6969
{ from: binaryId },

0 commit comments

Comments
 (0)