paulmillr / noble-secp256k1

Fastest 4KB JS implementation of secp256k1 signatures and ECDH
https://paulmillr.com/noble
MIT License
757 stars 114 forks source link

Add isValidPublicKey? #71

Closed paulmillr closed 1 year ago

paulmillr commented 2 years ago

Need to understand if it'll be used anywhere in scure/bip32, micro-signers, etc

  isValidPublicKey(publicKey: Hex, type: 'ecdsa' | 'schnorr') {
    const arr = ensureBytes(publicKey);
    const len = arr.length;
    if (type === 'ecdsa') {
      if (len === 32) throw new Error('Expected non-Schnorr key');
    } else if (type === 'schnorr') {
      if (len !== 32) throw new Error('Expected 32-byte Schnorr key');
    } else {
      throw new Error('Unknown key type')
    }
    Point.fromHex(publicKey); // does assertValidity
    return true;
  },
paulmillr commented 2 years ago

or maybe ban schnorr in fromHex and add new method for fromHexSchnorr

tre-dev commented 1 year ago

I'm migrating a project to noble ones and this function is missing, so it would be great for it to be added. In the meantime, will the code you linked above work?

paulmillr commented 1 year ago

Point.fromHex does all the validation you need yeah