replace buffer with b4a

This commit is contained in:
Andre Staltz 2023-06-15 14:55:23 +03:00
parent aa5d49d512
commit 08d3450f20
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
7 changed files with 37 additions and 27 deletions

View File

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

View File

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

View File

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

View File

@ -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<Msg> & {values: () => Iterator<Msg>}} 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}
*/

View File

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

View File

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

View File

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