msg hash is only applied on msg.metadata

This commit is contained in:
Andre Staltz 2023-04-17 21:49:17 +03:00
parent 3686f5de24
commit 3f7d299559
4 changed files with 30 additions and 12 deletions

View File

@ -11,10 +11,8 @@ const stringify = require('fast-json-stable-stringify')
* @returns {Buffer}
*/
function getMsgHashBuf(msg) {
const { metadata, sig } = msg
const metadataBuf = Buffer.from(stringify(metadata), 'utf8')
const sigBuf = base58.decode(sig)
return blake3.hash(Buffer.concat([metadataBuf, sigBuf])).subarray(0, 16)
const metadataBuf = Buffer.from(stringify(msg.metadata), 'utf8')
return blake3.hash(metadataBuf).subarray(0, 16)
}
/**

View File

@ -103,6 +103,25 @@ function isFeedRoot(msg, authorId, findType) {
return who === findWho && type === findType && isEmptyObject(tangles)
}
function getFeedRootHash(authorId, type) {
const who = stripAuthor(authorId)
const msg = {
content: null,
metadata: {
hash: null,
size: 0,
tangles: {},
type,
v: 1,
who,
},
sig: '',
}
return getMsgHash(msg)
}
function toPlaintextBuffer(opts) {
return Buffer.from(stringify(opts.content), 'utf8')
}
@ -195,7 +214,7 @@ function createRoot(keys, type) {
* @returns {Msg}
*/
function erase(msg) {
return {...msg, content: null}
return { ...msg, content: null }
}
/**
@ -211,6 +230,7 @@ module.exports = {
getMsgHash,
getMsgId,
isFeedRoot,
getFeedRootHash,
// getFeedId,
// isFeedId,
// isMsg,

View File

@ -20,7 +20,6 @@ test('erase', async (t) => {
await peer.db.loaded()
const rootHash = 'Nf2kuXAYsLBHEgU9eonYdn'
const msgHashes = []
for (let i = 0; i < 5; i++) {
const rec = await p(peer.db.create)({
@ -48,8 +47,8 @@ test('erase', async (t) => {
const after2 = []
for (const msg of peer.db.msgs()) {
if (msg.metadata.tangles[rootHash]) {
after2.push(msg.metadata.tangles[rootHash].depth)
for (const tangleId in msg.metadata.tangles) {
after2.push(msg.metadata.tangles[tangleId].depth)
}
}

View File

@ -14,8 +14,9 @@ tape('FeedV1.createRoot()', (t) => {
t.equals(rootMsg.metadata.who, FeedV1.stripAuthor(keys.id), 'who')
t.deepEquals(rootMsg.metadata.tangles, {}, 'tangles')
console.log(rootMsg);
rootHash = FeedV1.getMsgHash(rootMsg)
t.equals(rootHash, 'Nf2kuXAYsLBHEgU9eonYdn', 'root hash')
t.equals(rootHash, '3F26EgnwbMHm1EEeeVM1Eb', 'root hash')
t.end()
})
@ -54,7 +55,7 @@ tape('FeedV1.create()', (t) => {
console.log(msg1)
const msgHash1 = 'SktCiaHrUxz2mXS1SRSDmj'
const msgHash1 = 'MTYQM89hvHuiVKaw8Ze7kc'
t.equals(
FeedV1.getMsgId(msg1),
@ -99,7 +100,7 @@ tape('FeedV1.create()', (t) => {
t.deepEqual(
FeedV1.getMsgId(msg2),
'ppppp:message/v1/4mjQ5aJu378cEu6TksRG3uXAiKFiwGjYQtWAjfVjDAJW/post/Nej4ibHrxryTduWqDeCJE4',
'ppppp:message/v1/4mjQ5aJu378cEu6TksRG3uXAiKFiwGjYQtWAjfVjDAJW/post/T7juKvDH2bqEUhJB9Dxctr',
'getMsgId'
)
@ -122,7 +123,7 @@ tape('create() handles DAG tips correctly', (t) => {
const msgHash1 = FeedV1.getMsgHash(msg1)
t.deepEquals(
msg1.metadata.tangles[rootHash].prev,
['Nf2kuXAYsLBHEgU9eonYdn'],
[FeedV1.getFeedRootHash(keys.id, 'post')],
'msg1.prev is root'
)