From 2d08532ce046ac7a29cbecb6b89674b9c1a84737 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 30 Nov 2024 00:15:17 -0800 Subject: [PATCH] We are only ever using this to get public keys if interacting with keys are all, so remove unrelated functionality and update references. --- src/lib/account.ts | 2 +- src/lib/workers/nano-nacl.ts | 55 ++++++++---------------------------- 2 files changed, 12 insertions(+), 45 deletions(-) diff --git a/src/lib/account.ts b/src/lib/account.ts index f083e7f..8d12132 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -109,7 +109,7 @@ export class Account { static fromPrivateKey (privateKey: string, index?: number): Account { Account.#isInternal = true Account.#validateKey(privateKey) - const { publicKey } = nacl.keyPair.fromSeed(hex.toBytes(privateKey)) + const { publicKey } = nacl.keyPair(hex.toBytes(privateKey)) const account = Account.fromPublicKey(bytes.toHex(publicKey), index) account.#prv = privateKey.toUpperCase() return account diff --git a/src/lib/workers/nano-nacl.ts b/src/lib/workers/nano-nacl.ts index d63cef4..d2c5d98 100644 --- a/src/lib/workers/nano-nacl.ts +++ b/src/lib/workers/nano-nacl.ts @@ -21,7 +21,7 @@ const NanoNaCl = () => { */ addEventListener('message', (message) => { const { privateKey, index } = message.data ?? message - const { publicKey } = keyPair.fromSeed(privateKey) + const { publicKey } = keyPair(privateKey) postMessage({ publicKey, privateKey, index }) }) @@ -596,21 +596,11 @@ const NanoNaCl = () => { scalarmult(p, q, s) } - function crypto_sign_keypair (pk: Uint8Array, sk: Uint8Array, seeded?: boolean) { + function crypto_sign_keypair (pk: Uint8Array, sk: Uint8Array) { var d = new Uint8Array(64) var p = [gf(), gf(), gf(), gf()] var i - if (!seeded) { - var QUOTA = 65536 // https://w3c.github.io/webcrypto/#Crypto-method-getRandomValues - var i, n = 32, v = new Uint8Array(n) - for (i = 0; i < n; i += QUOTA) { - crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA))) - } - for (i = 0; i < n; i++) sk[i] = v[i] - cleanup(v) - } - crypto_hash(d, sk, 32) d[0] &= 248 d[31] &= 127 @@ -821,40 +811,17 @@ const NanoNaCl = () => { return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0) } - const keyPair = { - create () { - var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES) - var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES) - crypto_sign_keypair(pk, sk) - return { publicKey: pk, secretKey: sk } - }, - - fromSecretKey (secretKey: Uint8Array) { - checkArrayTypes(secretKey) - if (secretKey.length !== crypto_sign_SECRETKEYBYTES) - throw new Error('bad secret key size') - var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES) - for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32 + i] - return { publicKey: pk, secretKey: new Uint8Array(secretKey) } - }, - - fromSeed (seed: Uint8Array) { - checkArrayTypes(seed) - if (seed.length !== crypto_sign_SEEDBYTES) - throw new Error('bad seed size') - var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES) - var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES) - for (var i = 0; i < 32; i++) sk[i] = seed[i] - crypto_sign_keypair(pk, sk, true) - return { publicKey: pk, secretKey: sk } - } + const keyPair = function (seed: Uint8Array) { + checkArrayTypes(seed) + if (seed.length !== crypto_sign_SEEDBYTES) + throw new Error('bad seed size') + var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES) + var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES) + for (var i = 0; i < 32; i++) sk[i] = seed[i] + crypto_sign_keypair(pk, sk) + return { publicKey: pk, secretKey: sk } } - const publicKeyLength = crypto_sign_PUBLICKEYBYTES - const secretKeyLength = crypto_sign_SECRETKEYBYTES - const seedLength = crypto_sign_SEEDBYTES - const signatureLength = crypto_sign_BYTES - return { sign, open, detached, verify, keyPair } } -- 2.34.1