mirror of https://codeberg.org/pzp/pzp-db.git
minor refactoring and rename message=>msg
This commit is contained in:
parent
3a9df124f6
commit
222f54ea52
|
@ -350,7 +350,10 @@ function initDB(peer, config) {
|
|||
* @returns {string | undefined}
|
||||
*/
|
||||
function validateAccountMsg(msg, accountTangle) {
|
||||
if (accountTangle && !MsgV3.isRoot(msg)) {
|
||||
if (!accountTangle) {
|
||||
return 'invalid account msg: account tangle is unknown'
|
||||
}
|
||||
if (!MsgV3.isRoot(msg)) {
|
||||
/** @type {AccountData} */
|
||||
const data = msg.data
|
||||
if (data.action === 'add') {
|
||||
|
@ -359,7 +362,7 @@ function initDB(peer, config) {
|
|||
const powers = getAccountPowers(accountTangle, keypair)
|
||||
if (!powers.has('add')) {
|
||||
// prettier-ignore
|
||||
return `invalid message: pubkey "${msg.pubkey}" does not have "add" power`
|
||||
return `invalid account msg: pubkey "${msg.pubkey}" does not have "add" power`
|
||||
}
|
||||
}
|
||||
// TODO validate 'del'
|
||||
|
|
|
@ -23,40 +23,37 @@ const {
|
|||
*/
|
||||
function validateShape(msg) {
|
||||
if (!msg || typeof msg !== 'object') {
|
||||
return 'invalid message: not an object\n' + JSON.stringify(msg)
|
||||
return 'invalid msg: not an object\n' + JSON.stringify(msg)
|
||||
}
|
||||
if (!('data' in msg)) {
|
||||
return 'invalid message: must have data\n' + JSON.stringify(msg)
|
||||
return 'invalid msg: must have data\n' + JSON.stringify(msg)
|
||||
}
|
||||
if (!msg.metadata || typeof msg.metadata !== 'object') {
|
||||
return 'invalid message: must have metadata\n' + JSON.stringify(msg)
|
||||
return 'invalid msg: must have metadata\n' + JSON.stringify(msg)
|
||||
}
|
||||
if (!('dataHash' in msg.metadata)) {
|
||||
// prettier-ignore
|
||||
return 'invalid message: must have metadata.dataHash\n' + JSON.stringify(msg)
|
||||
return 'invalid msg: must have metadata.dataHash\n' + JSON.stringify(msg)
|
||||
}
|
||||
if (!('dataSize' in msg.metadata)) {
|
||||
// prettier-ignore
|
||||
return 'invalid message: must have metadata.dataSize\n' + JSON.stringify(msg)
|
||||
return 'invalid msg: must have metadata.dataSize\n' + JSON.stringify(msg)
|
||||
}
|
||||
if (!('account' in msg.metadata)) {
|
||||
return 'invalid message: must have metadata.account\n' + JSON.stringify(msg)
|
||||
return 'invalid msg: must have metadata.account\n' + JSON.stringify(msg)
|
||||
}
|
||||
if (!('accountTips' in msg.metadata)) {
|
||||
// prettier-ignore
|
||||
return 'invalid message: must have metadata.accountTips\n' + JSON.stringify(msg)
|
||||
return 'invalid msg: must have metadata.accountTips\n' + JSON.stringify(msg)
|
||||
}
|
||||
if (!('tangles' in msg.metadata)) {
|
||||
return 'invalid message: must have metadata.tangles\n' + JSON.stringify(msg)
|
||||
return 'invalid msg: must have metadata.tangles\n' + JSON.stringify(msg)
|
||||
}
|
||||
if (!('domain' in msg.metadata)) {
|
||||
return 'invalid message: must have metadata.domain\n' + JSON.stringify(msg)
|
||||
return 'invalid msg: must have metadata.domain\n' + JSON.stringify(msg)
|
||||
}
|
||||
if (msg.metadata.v !== 3) {
|
||||
return 'invalid message: must have metadata.v=3\n' + JSON.stringify(msg)
|
||||
return 'invalid msg: must have metadata.v=3\n' + JSON.stringify(msg)
|
||||
}
|
||||
if (typeof msg.sig !== 'string') {
|
||||
return 'invalid message: must have sig\n' + JSON.stringify(msg)
|
||||
return 'invalid msg: must have sig\n' + JSON.stringify(msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -68,17 +65,17 @@ function validatePubkey(msg) {
|
|||
const { pubkey } = msg
|
||||
if (typeof pubkey !== 'string') {
|
||||
// prettier-ignore
|
||||
return `invalid message: pubkey "${pubkey}" should have been a string\n` + JSON.stringify(msg)
|
||||
return `invalid msg: pubkey "${pubkey}" should have been a string\n` + JSON.stringify(msg)
|
||||
}
|
||||
try {
|
||||
const pubkeyBuf = base58.decode(pubkey)
|
||||
if (pubkeyBuf.length !== 32) {
|
||||
// prettier-ignore
|
||||
return `invalid message: decoded "pubkey" should be 32 bytes but was ${pubkeyBuf.length}\n` + JSON.stringify(msg)
|
||||
return `invalid msg: decoded "pubkey" should be 32 bytes but was ${pubkeyBuf.length}\n` + JSON.stringify(msg)
|
||||
}
|
||||
} catch (err) {
|
||||
// prettier-ignore
|
||||
return `invalid message: pubkey "${pubkey}" should have been a base58 string\n` + JSON.stringify(msg)
|
||||
return `invalid msg: pubkey "${pubkey}" should have been a base58 string\n` + JSON.stringify(msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -93,20 +90,20 @@ function validatePubkeyAndAccount(msg, tangle, pubkeys) {
|
|||
if (tangle.type === 'feed' || tangle.type === 'weave') {
|
||||
if (msg.metadata.account === ACCOUNT_SELF) {
|
||||
// prettier-ignore
|
||||
return `invalid message: account "${msg.metadata.account}" cannot be "self" in a feed tangle\n` + JSON.stringify(msg)
|
||||
return `invalid msg: account "${msg.metadata.account}" cannot be "self" in a feed tangle\n` + JSON.stringify(msg)
|
||||
}
|
||||
if (msg.metadata.account !== ACCOUNT_ANY && !pubkeys.has(msg.pubkey)) {
|
||||
// prettier-ignore
|
||||
return `invalid message: pubkey "${msg.pubkey}" should have been one of "${[...pubkeys]}" from the account "${msg.metadata.account}"\n` + JSON.stringify(msg)
|
||||
return `invalid msg: pubkey "${msg.pubkey}" should have been one of "${[...pubkeys]}" from the account "${msg.metadata.account}"\n` + JSON.stringify(msg)
|
||||
}
|
||||
} else if (tangle.type === 'account') {
|
||||
if (msg.metadata.account !== ACCOUNT_SELF) {
|
||||
// prettier-ignore
|
||||
return `invalid message: account "${msg.metadata.account}" should have been "self" in an account tangle\n` + JSON.stringify(msg)
|
||||
return `invalid msg: account "${msg.metadata.account}" should have been "self" in an account tangle\n` + JSON.stringify(msg)
|
||||
}
|
||||
if (msg.metadata.accountTips !== null) {
|
||||
// prettier-ignore
|
||||
return `invalid message: accountTips "${msg.metadata.accountTips}" should have been null in an account tangle\n` + JSON.stringify(msg)
|
||||
return `invalid msg: accountTips "${msg.metadata.accountTips}" should have been null in an account tangle\n` + JSON.stringify(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -120,10 +117,10 @@ function validateMsgID(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}`
|
||||
return `invalid msg: decoded hash should be 16 bytes but was ${hashBuf.length}`
|
||||
}
|
||||
} catch (err) {
|
||||
return `invalid message: msgID "${str}" should have been a base58 string`
|
||||
return `invalid msg: msgID "${str}" should have been a base58 string`
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -134,21 +131,19 @@ function validateMsgID(str) {
|
|||
function validateSignature(msg) {
|
||||
const { sig } = msg
|
||||
if (typeof sig !== 'string') {
|
||||
return (
|
||||
`invalid message: sig "${sig}" should have been a string\n` +
|
||||
JSON.stringify(msg)
|
||||
)
|
||||
// prettier-ignore
|
||||
return `invalid msg: sig "${sig}" should have been a string\n` + JSON.stringify(msg)
|
||||
}
|
||||
let sigBuf
|
||||
try {
|
||||
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)
|
||||
return `invalid msg: sig should be 64 bytes but was ${sigBuf.length}\n` + JSON.stringify(msg)
|
||||
}
|
||||
} catch (err) {
|
||||
// prettier-ignore
|
||||
return `invalid message: sig "${sig}" should have been a base58 string\n` + JSON.stringify(msg)
|
||||
return `invalid msg: sig "${sig}" should have been a base58 string\n` + JSON.stringify(msg)
|
||||
}
|
||||
|
||||
const signableBuf = b4a.from(
|
||||
|
@ -158,7 +153,7 @@ function validateSignature(msg) {
|
|||
const keypair = { curve: 'ed25519', public: msg.pubkey }
|
||||
const verified = Keypair.verify(keypair, signableBuf, sig)
|
||||
if (!verified) {
|
||||
return 'invalid message: sig is invalid\n' + JSON.stringify(msg)
|
||||
return 'invalid msg: sig is invalid\n' + JSON.stringify(msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -175,26 +170,26 @@ function validateSignature(msg) {
|
|||
function validateTangle(msg, tangle, tangleID) {
|
||||
if (!msg.metadata.tangles[tangleID]) {
|
||||
// prettier-ignore
|
||||
return `invalid message: must have metadata.tangles.${tangleID}\n` + JSON.stringify(msg)
|
||||
return `invalid msg: must have metadata.tangles.${tangleID}\n` + JSON.stringify(msg)
|
||||
}
|
||||
const { depth, prev } = msg.metadata.tangles[tangleID]
|
||||
if (!prev || !Array.isArray(prev)) {
|
||||
// prettier-ignore
|
||||
return `invalid message: prev "${prev}" should have been an array\n` + JSON.stringify(msg)
|
||||
return `invalid msg: prev "${prev}" should have been an array\n` + JSON.stringify(msg)
|
||||
}
|
||||
if (!Number.isSafeInteger(depth) || depth <= 0) {
|
||||
// prettier-ignore
|
||||
return `invalid message: depth "${depth}" should have been a positive integer\n` + JSON.stringify(msg)
|
||||
return `invalid msg: depth "${depth}" should have been a positive integer\n` + JSON.stringify(msg)
|
||||
}
|
||||
if (tangle.type === 'feed') {
|
||||
const { account, domain } = /** @type {MootDetails} */ (tangle.mootDetails)
|
||||
if (domain !== msg.metadata.domain) {
|
||||
// prettier-ignore
|
||||
return `invalid message: domain "${msg.metadata.domain}" should have been feed domain "${domain}"\n` + JSON.stringify(msg)
|
||||
return `invalid msg: domain "${msg.metadata.domain}" should have been feed domain "${domain}"\n` + JSON.stringify(msg)
|
||||
}
|
||||
if (account !== msg.metadata.account) {
|
||||
// prettier-ignore
|
||||
return `invalid message: account "${msg.metadata.account}" should have been feed account "${account}"\n` + JSON.stringify(msg)
|
||||
return `invalid msg: account "${msg.metadata.account}" should have been feed account "${account}"\n` + JSON.stringify(msg)
|
||||
}
|
||||
}
|
||||
let lastPrev = null
|
||||
|
@ -203,20 +198,20 @@ function validateTangle(msg, tangle, tangleID) {
|
|||
for (const p of prev) {
|
||||
if (typeof p !== 'string') {
|
||||
// prettier-ignore
|
||||
return `invalid message: prev item "${p}" should have been a string\n` + JSON.stringify(msg)
|
||||
return `invalid msg: prev item "${p}" should have been a string\n` + JSON.stringify(msg)
|
||||
}
|
||||
if (p.startsWith('ppppp:')) {
|
||||
// prettier-ignore
|
||||
return `invalid message: prev item "${p}" is a URI, but should have been a hash\n` + JSON.stringify(msg)
|
||||
return `invalid msg: prev item "${p}" is a URI, but should have been a hash\n` + JSON.stringify(msg)
|
||||
}
|
||||
if (lastPrev !== null) {
|
||||
if (p === lastPrev) {
|
||||
// prettier-ignore
|
||||
return `invalid message: prev "${prev}" contains duplicates\n` + JSON.stringify(msg)
|
||||
return `invalid msg: prev "${prev}" contains duplicates\n` + JSON.stringify(msg)
|
||||
}
|
||||
if (p < lastPrev) {
|
||||
// prettier-ignore
|
||||
return `invalid message: prev "${prev}" should have been alphabetically sorted\n` + JSON.stringify(msg)
|
||||
return `invalid msg: prev "${prev}" should have been alphabetically sorted\n` + JSON.stringify(msg)
|
||||
}
|
||||
}
|
||||
lastPrev = p
|
||||
|
@ -230,19 +225,18 @@ function validateTangle(msg, tangle, tangleID) {
|
|||
const diff = depth - prevDepth
|
||||
if (diff <= 0) {
|
||||
// prettier-ignore
|
||||
return `invalid message: depth of prev "${p}" should have been lower than this message's depth\n` + JSON.stringify(msg)
|
||||
return `invalid msg: depth of prev "${p}" should have been lower than this message's depth\n` + JSON.stringify(msg)
|
||||
}
|
||||
if (diff < minDiff) minDiff = diff
|
||||
}
|
||||
|
||||
if (countPrevUnknown === prev.length) {
|
||||
// prettier-ignore
|
||||
return 'invalid message: all prev are locally unknown\n' + JSON.stringify(msg)
|
||||
return 'invalid msg: all prev are locally unknown\n' + JSON.stringify(msg)
|
||||
}
|
||||
|
||||
if (countPrevUnknown === 0 && minDiff !== 1) {
|
||||
// prettier-ignore
|
||||
return `invalid message: depth must be the largest prev depth plus one\n` + JSON.stringify(msg)
|
||||
return `invalid msg: depth must be the largest prev depth plus one\n` + JSON.stringify(msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -254,11 +248,11 @@ function validateTangle(msg, tangle, tangleID) {
|
|||
function validateTangleRoot(msg, msgID, tangleID) {
|
||||
if (msgID !== tangleID) {
|
||||
// prettier-ignore
|
||||
return `invalid message: tangle root "${msgID}" must match tangleID "${tangleID}"\n` + JSON.stringify(msg)
|
||||
return `invalid msg: tangle root "${msgID}" must match tangleID "${tangleID}"\n` + JSON.stringify(msg)
|
||||
}
|
||||
if (msg.metadata.tangles[tangleID]) {
|
||||
// prettier-ignore
|
||||
return `invalid message: tangle root "${tangleID}" must not have self tangle data\n` + JSON.stringify(msg)
|
||||
return `invalid msg: tangle root "${tangleID}" must not have self tangle data\n` + JSON.stringify(msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -271,11 +265,9 @@ function validateDomain(domain) {
|
|||
return `invalid domain: "${domain}" (${typeof domain}) should have been a string`
|
||||
}
|
||||
if (domain.length > 100) {
|
||||
// prettier-ignore
|
||||
return `invalid domain: "${domain}" is 100+ characters long`
|
||||
}
|
||||
if (domain.length < 3) {
|
||||
// prettier-ignore
|
||||
return `invalid domain: "${domain}" is shorter than 3 characters`
|
||||
}
|
||||
if (/[^a-zA-Z0-9_]/.test(domain)) {
|
||||
|
@ -293,16 +285,12 @@ function validateData(msg) {
|
|||
return
|
||||
}
|
||||
if (Array.isArray(data)) {
|
||||
return (
|
||||
`invalid message: data "${data}" must not be an array\n` +
|
||||
JSON.stringify(msg)
|
||||
)
|
||||
// prettier-ignore
|
||||
return `invalid msg: data "${data}" must not be an array\n` + JSON.stringify(msg)
|
||||
}
|
||||
if (typeof data !== 'object' && typeof data !== 'string') {
|
||||
return (
|
||||
`invalid message: data "${data}" must be an object or a string` +
|
||||
JSON.stringify(msg)
|
||||
)
|
||||
// prettier-ignore
|
||||
return `invalid msg: data "${data}" must be an object or a string` + JSON.stringify(msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -313,15 +301,15 @@ function validateDataSizeHash(msg) {
|
|||
const [dataHash, dataSize] = representData(msg.data)
|
||||
if (!Number.isSafeInteger(dataSize) || dataSize < 0) {
|
||||
// prettier-ignore
|
||||
return `invalid message: dataSize ${dataSize} should have been an unsigned integer\n` + JSON.stringify(msg)
|
||||
return `invalid msg: dataSize ${dataSize} should have been an unsigned integer\n` + JSON.stringify(msg)
|
||||
}
|
||||
if (dataHash !== msg.metadata.dataHash) {
|
||||
// prettier-ignore
|
||||
return `invalid message: data hash "${dataHash}" does not match metadata.dataHash "${msg.metadata.dataHash}"\n` + JSON.stringify(msg)
|
||||
return `invalid msg: data hash "${dataHash}" does not match metadata.dataHash "${msg.metadata.dataHash}"\n` + JSON.stringify(msg)
|
||||
}
|
||||
if (dataSize !== msg.metadata.dataSize) {
|
||||
// prettier-ignore
|
||||
return `invalid message: data size "${dataSize}" does not match metadata.dataSize "${msg.metadata.dataSize}"\n` + JSON.stringify(msg)
|
||||
return `invalid msg: data size "${dataSize}" does not match metadata.dataSize "${msg.metadata.dataSize}"\n` + JSON.stringify(msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -222,7 +222,7 @@ test('publish with a key in the account', async (t) => {
|
|||
await p(carol.db.add)(accountRec1.msg, account)
|
||||
await p(carol.db.add)(postsRoot.msg, postsID)
|
||||
await p(carol.db.add)(postRec.msg, postsID)
|
||||
// t.pass('carol added all messages successfully')
|
||||
// t.pass('carol added all msgs successfully')
|
||||
|
||||
await p(carol.close)()
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue