NPM's signatures allow you to check that when you have a copy of the package, that it has the same contents as the package in the NPM registry. Thus, you can prove that a downloaded copy was not tampered with later. Signing packages is important in particular when mirrors or proxies are involved, and is done similarly by other package management systems.
This does not prove that it's the same contents as were uploaded by the package author, i.e. this signature does not defend against a malicious registry.
You correctly point out that the security of this approach hinges on knowing which keys are valid.
- For PGP signatures, NPM has published its key on Keybase
- For ECDSA signatures, NPM publishes its keys through the NPM API
For example, the NPM documentation lists light-cycle as an example of a package with a signature. Its metadata contains:
(I removed the PGP signature since it is very long.)
Using https://registry.npmjs.org/-/npm/v1/keys, we can fetch NPM's keys:
We can see that the keyid in the signature matches the ID of one of NPM's keys, so that we could proceed to verify the signature.