ssbc / ssb-bfe

Binary Field Encodings (BFE) for Secure Scuttlebutt (SSB)
3 stars 1 forks source link

add is() functions #35

Closed staltz closed 2 years ago

staltz commented 2 years ago

Problem

There are a couple other modules that use ssb-bfe like this:

const FEED = bfe.bfeNamedTypes['feed']
const CLASSIC_FEED_TF = Buffer.from([FEED.code, FEED.formats['classic'].code])
if (feedId.slice(0, 2).equals(CLASSIC_FEED_TF))

That's kind of sad, we have to build a bunch of Buffer and slice buffers and stuff just to know if feedId is an encoded BFE of the type "feed". It's also error prone to fiddle with buffers like that, untested.

Solution

Similar to ssb-uri2 has isGabbyGroveV1MessageSSBURI(input) => boolean, there are now:

such that the input is a buffer and you get a boolean answering you that the type/format is the thing.

Details

I started implementing this manually, like isEncodedFeedClassic implemented by hand, etc. Half the way I figured this is stupid. There is way too many cases, and when we update ssb-bfe-spec we will have to manually update ssb-bfe.

So instead I found a way of using bfe.json from ssb-bfe-spec to automatically create these functions. Should fit the style of this library which doesn't manually list all the types/formats, but instead uses TYPES and NAMED_TYPES.