Is there a way to detect if a hash is a IPNS or IPFS hash from API command?

Is there a method in the API that can tell me if a link is a IPNS hash or a IPFS hash?

I’m building an immutable data storage system and I can’t allow for clients to submit IPNS links. How do I detect if a link is IPNS vs IPFS?

Thanks for any help that can be provided.

Depends on how the data is given to you, if it’s just a hash I don’t think there’s anyway you can tell. However, you can submit paths instead of standalone hashes, so for example you could submit /ipns/Qm...A in which case you know it’s an IPNS hash, or you can submit /ipfs/Qm...A in which case you know its an IPFS hash. Although technically I guess CID (Content Identifier) is a more valid word to use than hash.

I appreciate the reply. So it sounds like the best way to handle this is require the user to enter a full “link/CID” and reject any that have /ipns/ in the beginning. Thanks!

you may be able to work off of

ipfs ping QmZJBQBXX98AuTcoR1HBGdbe5Gph74ZBWSgNemBcqPNv1W

If it is a PeerID you get one of two responses:

ipfs ping QmQcZypJxHQa5ERoEknDQ7CYG1rBTn5XZz32PgeL2VqiBF
PING QmQcZypJxHQa5ERoEknDQ7CYG1rBTn5XZz32PgeL2VqiBF.
Average latency: 0.00ms

or

ipfs ping QmQcZypJxHQa5ERoEknDQ7CYG1rBTn5XZz32PgeL2VqiBF
PING QmQcZypJxHQa5ERoEknDQ7CYG1rBTn5XZz32PgeL2VqiBF.
Pong received: time=44.42 ms
Pong received: time=42.02 ms
Pong received: time=44.31 ms
Pong received: time=43.43 ms
Pong received: time=42.53 ms
Pong received: time=42.37 ms
Pong received: time=44.71 ms
Pong received: time=47.92 ms
Pong received: time=42.55 ms
Pong received: time=48.92 ms
Average latency: 44.32ms

Next I altered the ID to contrive something that is highly unlikely to be a PeerID:

ipfs ping QmQcZypJxHQa5ERoEknDQ7CYG1rBTn5XZz32PgeL2VqiB1
Looking up peer QmQcZypJxHQa5ERoEknDQ7CYG1rBTn5XZz32PgeL2VqiB1
Peer lookup error: context deadline exceeded

I noticed that it immediately started with “looking up peer” so that is a clue that it’s not a PeerID.

1 Like