From: Chris Duncan Date: Sun, 24 Nov 2024 19:40:52 +0000 (-0800) Subject: Got account to work with basically vanilla TweetNaCl, so save work and start pruning... X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=67781b953343591cc7529c5fd94234dcd351f946;p=libnemo.git Got account to work with basically vanilla TweetNaCl, so save work and start pruning in subsequent commits. --- diff --git a/src/lib/account.ts b/src/lib/account.ts index 3e9be66..b2b1910 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -4,7 +4,7 @@ import blake2b from 'blake2b' import { ACCOUNT_KEY_LENGTH, ALPHABET, PREFIX, PREFIX_LEGACY } from './constants.js' import { base32, bytes, hex } from './convert.js' -import { keyPair, secretKeyLength } from './workers/nano25519.js' +import { getPublicKey, keyPair } from './workers/nano25519.js' import { Rpc } from './rpc.js' import { Safe } from './safe.js' @@ -87,7 +87,8 @@ export class Account { */ static async fromPrivateKey (key: string, index?: number): Promise { Account.#validateKey(key) - const { publicKey } = keyPair.fromSecretKey(hex.toBytes(key)) + // const publicKey = getPublicKey(hex.toBytes(key)) + const { publicKey } = keyPair.fromSeed(hex.toBytes(key)) const account = await Account.fromPublicKey(bytes.toHex(publicKey), index) account.#prv = key.toUpperCase() return account diff --git a/src/lib/workers/nano25519.ts b/src/lib/workers/nano25519.ts index de2536c..f97313e 100644 --- a/src/lib/workers/nano25519.ts +++ b/src/lib/workers/nano25519.ts @@ -17,6 +17,29 @@ import blake2b from 'blake2b' // See for details: https://docs.nano.org/integration-guides/the-basics/ // Original source commit: https://github.com/dchest/tweetnacl-js/blob/71df1d6a1d78236ca3e9f6c788786e21f5a651a6/nacl-fast.js +/** +* Generate a public key from a private key using the Ed25519 algorithm. The key +* should be a cryptographically strong random value. +* +* @param {string} privateKey - 32-byte private key +* @returns {string} 32-byte public key +*/ +function getPublicKey (privateKey: Uint8Array): Uint8Array { + const h = blake2b(64).update(privateKey).digest().slice(0, 32) + return scalarMult.base(h) + // const pk = new Uint8Array(32) + // const p = [gf(),gf(),gf(),gf()] + // const h = blake2b(64).update(privateKey).digest().slice(0, 32) + + // h[0] &= 0xf8 + // h[31] &= 0x7f + // h[31] |= 0x40 + + // scalarbase(p, h) + // pack(pk, p) + + // return pk +} var gf = function(init?) { var i, r = new Float64Array(16); if (init) for (i = 0; i < init.length; i++) r[i] = init[i]; @@ -2177,7 +2200,37 @@ import blake2b from 'blake2b' scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES; scalarMult.groupElementLength = crypto_scalarmult_BYTES; - + + const box = { + before: function(publicKey, secretKey) { + checkArrayTypes(publicKey, secretKey); + checkBoxLengths(publicKey, secretKey); + var k = new Uint8Array(crypto_box_BEFORENMBYTES); + crypto_box_beforenm(k, publicKey, secretKey); + return k; + }, + keyPair: { + create: () => { + var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES); + var sk = new Uint8Array(crypto_box_SECRETKEYBYTES); + crypto_box_keypair(pk, sk); + return {publicKey: pk, secretKey: sk}; + }, + fromSecretKey: (secretKey) => { + checkArrayTypes(secretKey); + if (secretKey.length !== crypto_box_SECRETKEYBYTES) + throw new Error('bad secret key size'); + var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES); + crypto_scalarmult_base(pk, secretKey); + return {publicKey: pk, secretKey: new Uint8Array(secretKey)}; + } + }, + publicKeyLength: crypto_box_PUBLICKEYBYTES, + secretKeyLength: crypto_box_SECRETKEYBYTES, + sharedKeyLength: crypto_box_BEFORENMBYTES, + nonceLength: crypto_box_NONCEBYTES + } + const message = { sign: (msg, secretKey) => { checkArrayTypes(msg, secretKey); @@ -2233,7 +2286,7 @@ import blake2b from 'blake2b' fromSecretKey: (secretKey) => { checkArrayTypes(secretKey); if (secretKey.length !== crypto_sign_SECRETKEYBYTES) - throw new Error('bad secret key size'); + throw new Error(`bad secret key size ${secretKey.length}`); 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)}; @@ -2305,4 +2358,4 @@ import blake2b from 'blake2b' } })(); - export { keyPair, hash, message, randomBytes, scalarMult } \ No newline at end of file + export { box, keyPair, getPublicKey, hash, message, randomBytes, scalarMult } \ No newline at end of file