pzp-db/lib/msg-v3/get-msg-id.js

56 lines
1.2 KiB
JavaScript

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 {B4A}
*/
function getMsgHashBuf(msg) {
const metadataBuf = b4a.from(stringify(msg.metadata), 'utf8')
return blake3.hash(metadataBuf).subarray(0, 16)
}
/**
* @param {Msg | string} x
* @returns {string}
*/
function getMsgHash(x) {
if (typeof x === 'string') {
if (x.startsWith('ppppp:message/v3/')) {
const msgUri = x
const parts = msgUri.split('/')
return parts[parts.length - 1]
} else {
const msgHash = x
return msgHash
}
} else {
const msg = x
const msgHashBuf = getMsgHashBuf(msg)
return base58.encode(msgHashBuf)
}
}
/**
* @param {Msg} msg
* @returns {string}
*/
function getMsgId(msg) {
const { identity, domain } = msg.metadata
const msgHash = getMsgHash(msg)
if (domain) {
return `ppppp:message/v3/${identity}/${domain}/${msgHash}`
} else {
return `ppppp:message/v3/${identity}/${msgHash}`
}
}
module.exports = { getMsgId, getMsgHash }