improve feed.publish input assertions

This commit is contained in:
Andre Staltz 2023-09-07 17:28:43 +03:00
parent 25eb244608
commit a6749cafa1
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 25 additions and 9 deletions

View File

@ -758,19 +758,34 @@ function initDB(peer, config) {
*/
function publishToFeed(opts, cb) {
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 (!encryptionFormats.has(opts.encryptionFormat ?? '')) {
// prettier-ignore
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) => {
// prettier-ignore
if (err) return cb(new Error('feed.publish() failed to initialize feed', { cause: err }));
@ -820,7 +835,8 @@ function initDB(peer, config) {
try {
msg = MsgV3.create(fullOpts)
} 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)

View File

@ -117,10 +117,10 @@ function validateMsgID(str) {
const hashBuf = b4a.from(base58.decode(str))
if (hashBuf.length !== 16) {
// 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) {
return `invalid msg: msgID "${str}" should have been a base58 string`
return `invalid msgID "${str}": should have been a base58 string`
}
}