Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove remaining uses of Buffer #4

Merged
merged 2 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ module.exports = class NoiseSecretStream extends Duplex {
this._message = b4a.allocUnsafe(this._len)
}

data.copy(this._message, this._tmp, offset)
b4a.copy(data, this._message, this._tmp, offset)
this._tmp += unprocessed

if (end <= data.length) {
Expand Down Expand Up @@ -208,7 +208,7 @@ module.exports = class NoiseSecretStream extends Duplex {
const expectedId = streamId(this.handshakeHash, !this.isInitiator)
const header = message.subarray(32)

if (!expectedId.equals(remoteId)) {
if (!b4a.equals(expectedId, remoteId)) {
this.destroy(new Error('Invalid header received'))
return
}
Expand Down
21 changes: 9 additions & 12 deletions lib/handshake.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const sodium = require('sodium-universal')
const curve = require('noise-curve-ed')
const Noise = require('noise-handshake')
const b4a = require('b4a')

const EMPTY = Buffer.alloc(0)
const EMPTY = b4a.alloc(0)

module.exports = class Handshake {
constructor (isInitiator, keyPair, remotePublicKey, pattern) {
Expand All @@ -14,8 +15,8 @@ module.exports = class Handshake {
}

static keyPair (seed) {
const publicKey = Buffer.alloc(32)
const secretKey = Buffer.alloc(64)
const publicKey = b4a.alloc(32)
const secretKey = b4a.alloc(64)
if (seed) sodium.crypto_sign_seed_keypair(publicKey, secretKey, seed)
else sodium.crypto_sign_keypair(publicKey, secretKey)
return { publicKey, secretKey }
Expand All @@ -37,7 +38,7 @@ module.exports = class Handshake {
send () {
try {
const data = this.noise.send()
const wrap = Buffer.allocUnsafe(data.byteLength + 3)
const wrap = b4a.allocUnsafe(data.byteLength + 3)

writeUint24le(data.byteLength, wrap)
wrap.set(data, 3)
Expand All @@ -55,10 +56,10 @@ module.exports = class Handshake {
}

_return (data) {
const tx = this.noise.complete ? toBuffer(this.noise.tx) : null
const rx = this.noise.complete ? toBuffer(this.noise.rx) : null
const hash = this.noise.complete ? toBuffer(this.noise.hash) : null
const remotePublicKey = this.noise.complete ? toBuffer(this.noise.rs) : null
const tx = this.noise.complete ? b4a.toBuffer(this.noise.tx) : null
const rx = this.noise.complete ? b4a.toBuffer(this.noise.rx) : null
const hash = this.noise.complete ? b4a.toBuffer(this.noise.hash) : null
const remotePublicKey = this.noise.complete ? b4a.toBuffer(this.noise.rs) : null

return {
data,
Expand All @@ -75,7 +76,3 @@ function writeUint24le (n, buf) {
buf[1] = (n >>> 8) & 255
buf[2] = (n >>> 16) & 255
}

function toBuffer (uint) {
return Buffer.from(uint.buffer, uint.byteOffset, uint.byteLength)
}