mirror of https://codeberg.org/pzp/pzp-db.git
msg hash is only applied on msg.metadata
This commit is contained in:
parent
3686f5de24
commit
3f7d299559
|
@ -11,10 +11,8 @@ const stringify = require('fast-json-stable-stringify')
|
||||||
* @returns {Buffer}
|
* @returns {Buffer}
|
||||||
*/
|
*/
|
||||||
function getMsgHashBuf(msg) {
|
function getMsgHashBuf(msg) {
|
||||||
const { metadata, sig } = msg
|
const metadataBuf = Buffer.from(stringify(msg.metadata), 'utf8')
|
||||||
const metadataBuf = Buffer.from(stringify(metadata), 'utf8')
|
return blake3.hash(metadataBuf).subarray(0, 16)
|
||||||
const sigBuf = base58.decode(sig)
|
|
||||||
return blake3.hash(Buffer.concat([metadataBuf, sigBuf])).subarray(0, 16)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -103,6 +103,25 @@ function isFeedRoot(msg, authorId, findType) {
|
||||||
return who === findWho && type === findType && isEmptyObject(tangles)
|
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) {
|
function toPlaintextBuffer(opts) {
|
||||||
return Buffer.from(stringify(opts.content), 'utf8')
|
return Buffer.from(stringify(opts.content), 'utf8')
|
||||||
}
|
}
|
||||||
|
@ -195,7 +214,7 @@ function createRoot(keys, type) {
|
||||||
* @returns {Msg}
|
* @returns {Msg}
|
||||||
*/
|
*/
|
||||||
function erase(msg) {
|
function erase(msg) {
|
||||||
return {...msg, content: null}
|
return { ...msg, content: null }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -211,6 +230,7 @@ module.exports = {
|
||||||
getMsgHash,
|
getMsgHash,
|
||||||
getMsgId,
|
getMsgId,
|
||||||
isFeedRoot,
|
isFeedRoot,
|
||||||
|
getFeedRootHash,
|
||||||
// getFeedId,
|
// getFeedId,
|
||||||
// isFeedId,
|
// isFeedId,
|
||||||
// isMsg,
|
// isMsg,
|
||||||
|
|
|
@ -20,7 +20,6 @@ test('erase', async (t) => {
|
||||||
|
|
||||||
await peer.db.loaded()
|
await peer.db.loaded()
|
||||||
|
|
||||||
const rootHash = 'Nf2kuXAYsLBHEgU9eonYdn'
|
|
||||||
const msgHashes = []
|
const msgHashes = []
|
||||||
for (let i = 0; i < 5; i++) {
|
for (let i = 0; i < 5; i++) {
|
||||||
const rec = await p(peer.db.create)({
|
const rec = await p(peer.db.create)({
|
||||||
|
@ -48,8 +47,8 @@ test('erase', async (t) => {
|
||||||
|
|
||||||
const after2 = []
|
const after2 = []
|
||||||
for (const msg of peer.db.msgs()) {
|
for (const msg of peer.db.msgs()) {
|
||||||
if (msg.metadata.tangles[rootHash]) {
|
for (const tangleId in msg.metadata.tangles) {
|
||||||
after2.push(msg.metadata.tangles[rootHash].depth)
|
after2.push(msg.metadata.tangles[tangleId].depth)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,8 +14,9 @@ tape('FeedV1.createRoot()', (t) => {
|
||||||
t.equals(rootMsg.metadata.who, FeedV1.stripAuthor(keys.id), 'who')
|
t.equals(rootMsg.metadata.who, FeedV1.stripAuthor(keys.id), 'who')
|
||||||
t.deepEquals(rootMsg.metadata.tangles, {}, 'tangles')
|
t.deepEquals(rootMsg.metadata.tangles, {}, 'tangles')
|
||||||
|
|
||||||
|
console.log(rootMsg);
|
||||||
rootHash = FeedV1.getMsgHash(rootMsg)
|
rootHash = FeedV1.getMsgHash(rootMsg)
|
||||||
t.equals(rootHash, 'Nf2kuXAYsLBHEgU9eonYdn', 'root hash')
|
t.equals(rootHash, '3F26EgnwbMHm1EEeeVM1Eb', 'root hash')
|
||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -54,7 +55,7 @@ tape('FeedV1.create()', (t) => {
|
||||||
|
|
||||||
console.log(msg1)
|
console.log(msg1)
|
||||||
|
|
||||||
const msgHash1 = 'SktCiaHrUxz2mXS1SRSDmj'
|
const msgHash1 = 'MTYQM89hvHuiVKaw8Ze7kc'
|
||||||
|
|
||||||
t.equals(
|
t.equals(
|
||||||
FeedV1.getMsgId(msg1),
|
FeedV1.getMsgId(msg1),
|
||||||
|
@ -99,7 +100,7 @@ tape('FeedV1.create()', (t) => {
|
||||||
|
|
||||||
t.deepEqual(
|
t.deepEqual(
|
||||||
FeedV1.getMsgId(msg2),
|
FeedV1.getMsgId(msg2),
|
||||||
'ppppp:message/v1/4mjQ5aJu378cEu6TksRG3uXAiKFiwGjYQtWAjfVjDAJW/post/Nej4ibHrxryTduWqDeCJE4',
|
'ppppp:message/v1/4mjQ5aJu378cEu6TksRG3uXAiKFiwGjYQtWAjfVjDAJW/post/T7juKvDH2bqEUhJB9Dxctr',
|
||||||
'getMsgId'
|
'getMsgId'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -122,7 +123,7 @@ tape('create() handles DAG tips correctly', (t) => {
|
||||||
const msgHash1 = FeedV1.getMsgHash(msg1)
|
const msgHash1 = FeedV1.getMsgHash(msg1)
|
||||||
t.deepEquals(
|
t.deepEquals(
|
||||||
msg1.metadata.tangles[rootHash].prev,
|
msg1.metadata.tangles[rootHash].prev,
|
||||||
['Nf2kuXAYsLBHEgU9eonYdn'],
|
[FeedV1.getFeedRootHash(keys.id, 'post')],
|
||||||
'msg1.prev is root'
|
'msg1.prev is root'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue