diff --git a/lib/index.js b/lib/index.js index 768c7d4..e65e69c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) diff --git a/lib/msg-v3/validation.js b/lib/msg-v3/validation.js index a1d0de5..100fc7a 100644 --- a/lib/msg-v3/validation.js +++ b/lib/msg-v3/validation.js @@ -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` } }