multiformats / js-cid

CID implementation in JavaScript
MIT License
97 stars 39 forks source link

Improve isCID? #53

Closed alanshaw closed 5 years ago

alanshaw commented 6 years ago

isCID checks version, codec and multihash properties are present and validates them. This proves that the passed object is an object that looks like { version, codec, multihash } and each property is valid, but doesn't actually prove that the passed object is an instance of CID.

So you could get back a true from isCID but there's a possibility that toBaseEncodedString (and the other instance methods) don't exist!

I know it's not the perfect solution, but would using https://github.com/moxystudio/js-class-is be better?

It's still possible to "fake" a CID instance, but it's much harder to do it by accident and we might even get a performance win if we make version, codec, and multihash readonly, because we can safely assume they are valid if we can determine the passed object is an instance of CID.

mikeal commented 5 years ago

I actually assumed this was already the case because I'd seen is-class used in other related modules. Big +1.