diff --git a/lib/encryption.js b/lib/encryption.js index 313fa22..9238171 100644 --- a/lib/encryption.js +++ b/lib/encryption.js @@ -1,5 +1,6 @@ -const MsgV2 = require('./msg-v2') const base58 = require('bs58') +const b4a = require('b4a') +const MsgV2 = require('./msg-v2') /** * @typedef {import('./index').Rec} Rec @@ -8,7 +9,7 @@ const base58 = require('bs58') function ciphertextStrToBuffer(str) { const dot = str.indexOf('.') - return Buffer.from(str.slice(0, dot), 'base64') + return b4a.from(str.slice(0, dot), 'base64') } /** @@ -16,8 +17,8 @@ function ciphertextStrToBuffer(str) { * @param {Keypair} keypair */ function keypairToSSBKeys(keypair) { - const public = Buffer.from(base58.decode(keypair.public)).toString('base64') - const private = Buffer.from(base58.decode(keypair.private)).toString('base64') + const public = b4a.from(base58.decode(keypair.public)).toString('base64') + const private = b4a.from(base58.decode(keypair.private)).toString('base64') return { id: `@${public}.ed25519`, curve: keypair.curve, diff --git a/lib/index.js b/lib/index.js index 4491701..664ffdb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,6 +2,7 @@ const path = require('node:path') const push = require('push-stream') const AAOL = require('async-append-only-log') const promisify = require('promisify-4loc') +const b4a = require('b4a') const base58 = require('bs58') const Obz = require('obz') const MsgV2 = require('./msg-v2') @@ -78,7 +79,7 @@ exports.init = function initDB(peer, config) { blockSize: 64 * 1024, codec: { encode(msg) { - return Buffer.from(JSON.stringify(msg), 'utf8') + return b4a.from(JSON.stringify(msg), 'utf8') }, decode(buf) { return JSON.parse(buf.toString('utf8')) @@ -141,7 +142,7 @@ exports.init = function initDB(peer, config) { log.append(rec, (err, newOffset) => { if (err) return cb(new Error('logAppend failed', { cause: err })) const offset = newOffset // latestOffset - const size = Buffer.from(JSON.stringify(rec), 'utf8').length + const size = b4a.from(JSON.stringify(rec), 'utf8').length const seq = recs.length const recExposed = decrypt(rec, peer, config) rec.misc = recExposed.misc = { offset, size, seq } @@ -326,7 +327,7 @@ exports.init = function initDB(peer, config) { recps: recps.map( (recp) => // TODO: temporary until our encryption formats are ppppp not SSB - `@${Buffer.from(base58.decode(recp)).toString('base64')}.ed25519` + `@${b4a.from(base58.decode(recp)).toString('base64')}.ed25519` ), } let ciphertextBuf diff --git a/lib/msg-v2/get-msg-id.js b/lib/msg-v2/get-msg-id.js index e3152f8..6d92036 100644 --- a/lib/msg-v2/get-msg-id.js +++ b/lib/msg-v2/get-msg-id.js @@ -1,17 +1,19 @@ +const b4a = require('b4a') const blake3 = require('blake3') const base58 = require('bs58') const stringify = require('json-canon') /** * @typedef {import('./index').Msg} Msg + * @typedef {Buffer | Uint8Array} B4A */ /** * @param {Msg} msg - * @returns {Buffer} + * @returns {B4A} */ function getMsgHashBuf(msg) { - const metadataBuf = Buffer.from(stringify(msg.metadata), 'utf8') + const metadataBuf = b4a.from(stringify(msg.metadata), 'utf8') return blake3.hash(metadataBuf).subarray(0, 16) } diff --git a/lib/msg-v2/index.js b/lib/msg-v2/index.js index f6685bc..3b88e63 100644 --- a/lib/msg-v2/index.js +++ b/lib/msg-v2/index.js @@ -1,7 +1,8 @@ -const crypto = require('crypto') +const crypto = require('node:crypto') +const base58 = require('bs58') +const b4a = require('b4a') const stringify = require('json-canon') const Keypair = require('ppppp-keypair') -const base58 = require('bs58') const union = require('set.prototype.union') const { stripGroup } = require('./strip') const isFeedRoot = require('./is-feed-root') @@ -19,6 +20,7 @@ const Tangle = require('./tangle') /** * @typedef {Iterator & {values: () => Iterator}} MsgIter * @typedef {import('ppppp-keypair').Keypair} Keypair + * @typedef {Buffer | Uint8Array} B4A */ /** @@ -74,7 +76,7 @@ function getFeedRootHash(groupId, type) { } function toPlaintextBuffer(opts) { - return Buffer.from(stringify(opts.data), 'utf8') + return b4a.from(stringify(opts.data), 'utf8') } /** @@ -123,7 +125,7 @@ function create(opts) { if ((err = validateData(msg))) throw err // TODO: add a label prefix to the metadata before signing - const metadataBuf = Buffer.from(stringify(msg.metadata), 'utf8') + const metadataBuf = b4a.from(stringify(msg.metadata), 'utf8') msg.sig = Keypair.sign(opts.keypair, metadataBuf) return msg @@ -155,7 +157,7 @@ function createRoot(group, type, keypair) { } // TODO: add a label prefix to the metadata before signing - const metadataBuf = Buffer.from(stringify(msg.metadata), 'utf8') + const metadataBuf = b4a.from(stringify(msg.metadata), 'utf8') msg.sig = Keypair.sign(keypair, metadataBuf) return msg @@ -186,7 +188,7 @@ function erase(msg) { } /** - * @param {Buffer} plaintextBuf + * @param {B4A} plaintextBuf * @param {Msg} msg * @returns {Msg} */ diff --git a/lib/msg-v2/represent-data.js b/lib/msg-v2/represent-data.js index fcba698..afc4472 100644 --- a/lib/msg-v2/represent-data.js +++ b/lib/msg-v2/represent-data.js @@ -1,4 +1,5 @@ const blake3 = require('blake3') +const b4a = require('b4a') const base58 = require('bs58') const stringify = require('json-canon') @@ -7,7 +8,7 @@ const stringify = require('json-canon') * @returns {[string, number]} */ function representData(data) { - const dataBuf = Buffer.from(stringify(data), 'utf8') + const dataBuf = b4a.from(stringify(data), 'utf8') const dataHash = base58.encode(blake3.hash(dataBuf).subarray(0, 16)) const dataSize = dataBuf.length return [dataHash, dataSize] diff --git a/lib/msg-v2/validation.js b/lib/msg-v2/validation.js index c9f4c85..4e4d8a3 100644 --- a/lib/msg-v2/validation.js +++ b/lib/msg-v2/validation.js @@ -1,3 +1,4 @@ +const b4a = require('b4a') const base58 = require('bs58') const Keypair = require('ppppp-keypair') const stringify = require('json-canon') @@ -74,7 +75,7 @@ function validateGroupPubkey(msg, pubkeys) { function validateMsgHash(str) { try { - const hashBuf = Buffer.from(base58.decode(str)) + const hashBuf = b4a.from(base58.decode(str)) if (hashBuf.length !== 16) { // prettier-ignore return `invalid message: decoded hash should be 16 bytes but was ${hashBuf.length}` @@ -102,7 +103,7 @@ function validateSignature(msg) { } let sigBuf try { - sigBuf = Buffer.from(base58.decode(sig)) + sigBuf = b4a.from(base58.decode(sig)) if (sigBuf.length !== 64) { // prettier-ignore return `invalid message: sig should be 64 bytes but was ${sigBuf.length}\n` + JSON.stringify(msg) @@ -112,7 +113,7 @@ function validateSignature(msg) { return `invalid message: sig "${sig}" should have been a base58 string\n` + JSON.stringify(msg) } - const signableBuf = Buffer.from(stringify(msg.metadata), 'utf8') + const signableBuf = b4a.from(stringify(msg.metadata), 'utf8') const keypair = {curve: 'ed25519', public: msg.pubkey} const verified = Keypair.verify(keypair, signableBuf, sig) if (!verified) { diff --git a/package.json b/package.json index 4a1560f..6dc2a2c 100644 --- a/package.json +++ b/package.json @@ -26,17 +26,19 @@ } }, "dependencies": { - "async-append-only-log": "^4.3.10", - "blake3": "^2.1.7", - "bs58": "^5.0.0", - "json-canon": "^1.0.0", - "obz": "^1.1.0", + "async-append-only-log": "~4.3.10", + "blake3": "~2.1.7", + "b4a": "~1.6.4", + "bs58": "~5.0.0", + "json-canon": "~1.0.0", + "obz": "~1.1.0", "ppppp-keypair": "github:staltz/ppppp-keypair", - "promisify-4loc": "^1.0.0", - "push-stream": "^11.2.0", - "set.prototype.union": "^1.0.2" + "promisify-4loc": "~1.0.0", + "push-stream": "~11.2.0", + "set.prototype.union": "~1.0.2" }, "devDependencies": { + "@types/b4a": "^1.6.0", "c8": "^7.11.0", "husky": "^4.3.0", "prettier": "^2.6.2",