mirror of https://codeberg.org/pzp/pzp-db.git
improve feed.publish input assertions
This commit is contained in:
parent
25eb244608
commit
a6749cafa1
30
lib/index.js
30
lib/index.js
|
@ -758,19 +758,34 @@ function initDB(peer, config) {
|
||||||
*/
|
*/
|
||||||
function publishToFeed(opts, cb) {
|
function publishToFeed(opts, cb) {
|
||||||
if (!opts) return cb(new Error('feed.publish() requires an `opts`'))
|
if (!opts) return cb(new Error('feed.publish() requires an `opts`'))
|
||||||
const keypair = opts.keypair ?? config.keypair
|
// prettier-ignore
|
||||||
|
if (!opts.account) return cb(new Error('feed.publish() requires an `account`'))
|
||||||
|
if (!opts.domain) return cb(new Error('feed.publish() requires a `domain`'))
|
||||||
|
if (!opts.data) return cb(new Error('feed.publish() requires a `data`'))
|
||||||
|
if (opts.keypair) {
|
||||||
|
const keypair = opts.keypair
|
||||||
|
// prettier-ignore
|
||||||
|
if (!keypair.curve) return cb(new Error('feed.publish() requires a `keypair` with `curve`', { cause: keypair }))
|
||||||
|
// prettier-ignore
|
||||||
|
if (!keypair.public) return cb(new Error('feed.publish() requires a `keypair` with `public`', { cause: keypair }))
|
||||||
|
// prettier-ignore
|
||||||
|
if (!keypair.private) return cb(new Error('feed.publish() requires a `keypair` with `private`', { cause: keypair }))
|
||||||
|
}
|
||||||
|
if (opts.tangles) {
|
||||||
|
const tangles = opts.tangles
|
||||||
|
// prettier-ignore
|
||||||
|
if (!Array.isArray(tangles)) return cb(new Error('feed.publish() "tangles" option must be an array', { cause: tangles }))
|
||||||
|
// prettier-ignore
|
||||||
|
if (tangles.some(id => typeof id !== 'string')) return cb(new Error('feed.publish() "tangles" option should only have string IDs', { cause: tangles }))
|
||||||
|
}
|
||||||
if (opts.data.recps) {
|
if (opts.data.recps) {
|
||||||
if (!encryptionFormats.has(opts.encryptionFormat ?? '')) {
|
if (!encryptionFormats.has(opts.encryptionFormat ?? '')) {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
return cb(new Error(`feed.publish() does not support encryption format "${opts.encryptionFormat}"`))
|
return cb(new Error(`feed.publish() does not support encryption format "${opts.encryptionFormat}"`))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!opts.data) return cb(new Error('feed.publish() requires a `data`'))
|
|
||||||
if (!opts.domain) return cb(new Error('feed.publish() requires a `domain`'))
|
|
||||||
if (!opts.account)
|
|
||||||
return cb(new Error('feed.publish() requires a `account`'))
|
|
||||||
|
|
||||||
|
const keypair = opts.keypair ?? config.keypair
|
||||||
initializeFeed(opts, (err, mootID) => {
|
initializeFeed(opts, (err, mootID) => {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
if (err) return cb(new Error('feed.publish() failed to initialize feed', { cause: err }));
|
if (err) return cb(new Error('feed.publish() failed to initialize feed', { cause: err }));
|
||||||
|
@ -820,7 +835,8 @@ function initDB(peer, config) {
|
||||||
try {
|
try {
|
||||||
msg = MsgV3.create(fullOpts)
|
msg = MsgV3.create(fullOpts)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return cb(new Error('feed.publish() failed', { cause: err }))
|
// prettier-ignore
|
||||||
|
return cb(new Error('feed.publish() failed to create message', { cause: err }))
|
||||||
}
|
}
|
||||||
const msgID = MsgV3.getMsgID(msg)
|
const msgID = MsgV3.getMsgID(msg)
|
||||||
|
|
||||||
|
|
|
@ -117,10 +117,10 @@ function validateMsgID(str) {
|
||||||
const hashBuf = b4a.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 msg: decoded hash should be 16 bytes but was ${hashBuf.length}`
|
return `invalid msgID "${str}": should have 16 bytes but has ${hashBuf.length}`
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return `invalid msg: msgID "${str}" should have been a base58 string`
|
return `invalid msgID "${str}": should have been a base58 string`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue