paulmillr / noble-curves

Audited & minimal JS implementation of elliptic curve cryptography.
https://paulmillr.com/noble
MIT License
623 stars 56 forks source link

hashToPrivateScalar Error: hex string is invalid: unpadded 63 #35

Closed 0xc0de4c0ffee closed 1 year ago

0xc0de4c0ffee commented 1 year ago
        let caip10 = `eip155:1:${App.ADDR}`
        let domain = "domain.eth"
        let msg = `Generate Deterministic IPNS Keys for '${domain}'\n\nSigned By: ${caip10}`;
        let sig = await App.SIGNER.signMessage(msg);
        let password = "pass12#$"
        let inputKey = sha256(
            hexToBytes(
                sig.toLowerCase().startsWith('0x') ? sig.slice(2) : sig
            ) 
        )
        let info = `${caip10}:${domain}`
        let salt = sha256(`${info}:${password ? password : ''}:${sig.slice(-64)}`)
        let hashKey = hkdf(sha256, inputKey, salt, info, 42)
        let privKey = hashToPrivateScalar(hashKey, ed25519.CURVE.n).toString(16)
        let pubKey = await ed25519.getPublicKey(privKey)

utils.js:86 Uncaught (in promise) Error: private key must be valid hex string, got "d06c76f87d42fe0e683b454318543ba25463b34a4c17594f32c05a3051ae608". Cause: Error: hex string is invalid: unpadded 63

paulmillr commented 1 year ago

The error is pretty clear: you need padded hex. d06 bad, 0d06 good.

0xc0de4c0ffee commented 1 year ago

The error is pretty clear: you need padded hex. d06 bad, 0d06 good.

I've used let privateKey = hashToPrivateScalar(hashKey, ed25519.CURVE.n).toString(16).padStart(64, "0"). it's always 63 char long so privkey always starts with "0x0...".