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