From 4043b9668a956dbc5bc63da765e2af4df1d0993a Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 25 Nov 2024 14:37:21 -0800 Subject: [PATCH] Export keyPair as proper object and update its original implementation as a function to a new create function. Create grouped export to make all functions available in one variable. Update relevant import statements. Fix tabs. --- src/lib/account.ts | 4 +- src/lib/block.ts | 6 +-- src/lib/tools.ts | 6 +-- src/lib/workers/nacl-nano.ts | 76 +++++++++++++++++++----------------- 4 files changed, 48 insertions(+), 44 deletions(-) diff --git a/src/lib/account.ts b/src/lib/account.ts index 0fde3e8..9ad147b 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -6,7 +6,7 @@ import { ACCOUNT_KEY_LENGTH, ALPHABET, PREFIX, PREFIX_LEGACY } from './constants import { base32, bytes, hex } from './convert.js' import { Rpc } from './rpc.js' import { Safe } from './safe.js' -import * as Ed25519 from './workers/nacl-nano.js' +import { nacl } from './workers/nacl-nano.js' /** * Represents a single Nano address and the associated public key. To include the @@ -87,7 +87,7 @@ export class Account { */ static async fromPrivateKey (key: string, index?: number): Promise { Account.#validateKey(key) - const { publicKey } = Ed25519.keyPair.fromSeed(hex.toBytes(key)) + const { publicKey } = nacl.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/block.ts b/src/lib/block.ts index af96d8a..7084c76 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -6,7 +6,7 @@ import { Account } from './account.js' import { bytes, dec, hex } from './convert.js' import { Rpc } from './rpc.js' import Tools from './tools.js' -import * as Ed25519 from './workers/nacl-nano.js' +import { nacl } from './workers/nacl-nano.js' /** * Represents a block as defined by the Nano cryptocurrency protocol. The Block @@ -141,7 +141,7 @@ abstract class Block { } const account = await Account.fromPrivateKey(key) try { - const signature = Ed25519.sign( + const signature = nacl.sign( hex.toBytes(await this.hash()), hex.toBytes(`${account.privateKey}${account.publicKey}`) ) @@ -201,7 +201,7 @@ abstract class Block { this.link ] const hash = await Tools.hash(data, 'hex') - return Ed25519.verify( + return nacl.verify( hex.toBytes(hash), hex.toBytes(this.signature ?? ''), hex.toBytes(key) diff --git a/src/lib/tools.ts b/src/lib/tools.ts index 854e353..b70fad1 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -8,7 +8,7 @@ import { bytes, hex } from './convert.js' import { Rpc } from './rpc.js' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from './wallet.js' import { SendBlock } from './block.js' -import * as Ed25519 from './workers/nacl-nano.js' +import { nacl } from './workers/nacl-nano.js' /** * Converts a decimal amount of nano from one unit divider to another. @@ -88,7 +88,7 @@ export async function hash (data: string | string[], encoding?: 'hex'): Promise< export async function sign (key: string, ...input: string[]): Promise { const account = await Account.fromPrivateKey(key) const data = await hash(input) - const signature = Ed25519.sign( + const signature = nacl.sign( hex.toBytes(data), hex.toBytes(`${account.privateKey}${account.publicKey}`)) return bytes.toHex(signature.subarray(0, 64)) @@ -159,7 +159,7 @@ export async function sweep (rpc: Rpc | string | URL, wallet: Blake2bWallet | Bi */ export async function verify (key: string, signature: string, ...input: string[]): Promise { const data = await hash(input) - return Ed25519.verify( + return nacl.verify( hex.toBytes(data), hex.toBytes(signature), hex.toBytes(key)) diff --git a/src/lib/workers/nacl-nano.ts b/src/lib/workers/nacl-nano.ts index 15e13db..7bac240 100644 --- a/src/lib/workers/nacl-nano.ts +++ b/src/lib/workers/nacl-nano.ts @@ -770,64 +770,65 @@ function cleanup(arr) { } export const sign = function(msg, secretKey) { - checkArrayTypes(msg, secretKey); - if (secretKey.length !== crypto_sign_SECRETKEYBYTES) - throw new Error('bad secret key size'); - var signedMsg = new Uint8Array(crypto_sign_BYTES+msg.length); - crypto_sign(signedMsg, msg, msg.length, secretKey); - return signedMsg; + checkArrayTypes(msg, secretKey); + if (secretKey.length !== crypto_sign_SECRETKEYBYTES) + throw new Error('bad secret key size'); + var signedMsg = new Uint8Array(crypto_sign_BYTES+msg.length); + crypto_sign(signedMsg, msg, msg.length, secretKey); + return signedMsg; }; export const open = function(signedMsg, publicKey) { - checkArrayTypes(signedMsg, publicKey); - if (publicKey.length !== crypto_sign_PUBLICKEYBYTES) - throw new Error('bad public key size'); - var tmp = new Uint8Array(signedMsg.length); - var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey); - if (mlen < 0) return null; - var m = new Uint8Array(mlen); - for (var i = 0; i < m.length; i++) m[i] = tmp[i]; - return m; + checkArrayTypes(signedMsg, publicKey); + if (publicKey.length !== crypto_sign_PUBLICKEYBYTES) + throw new Error('bad public key size'); + var tmp = new Uint8Array(signedMsg.length); + var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey); + if (mlen < 0) return null; + var m = new Uint8Array(mlen); + for (var i = 0; i < m.length; i++) m[i] = tmp[i]; + return m; }; export const detached = function(msg, secretKey) { - var signedMsg = sign(msg, secretKey); - var sig = new Uint8Array(crypto_sign_BYTES); - for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i]; - return sig; + var signedMsg = sign(msg, secretKey); + var sig = new Uint8Array(crypto_sign_BYTES); + for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i]; + return sig; }; export const verify = function(msg, sig, publicKey) { - checkArrayTypes(msg, sig, publicKey); - if (sig.length !== crypto_sign_BYTES) - throw new Error('bad signature size'); - if (publicKey.length !== crypto_sign_PUBLICKEYBYTES) - throw new Error('bad public key size'); - var sm = new Uint8Array(crypto_sign_BYTES + msg.length); - var m = new Uint8Array(crypto_sign_BYTES + msg.length); - var i; - for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i]; - for (i = 0; i < msg.length; i++) sm[i+crypto_sign_BYTES] = msg[i]; - return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0); + checkArrayTypes(msg, sig, publicKey); + if (sig.length !== crypto_sign_BYTES) + throw new Error('bad signature size'); + if (publicKey.length !== crypto_sign_PUBLICKEYBYTES) + throw new Error('bad public key size'); + var sm = new Uint8Array(crypto_sign_BYTES + msg.length); + var m = new Uint8Array(crypto_sign_BYTES + msg.length); + var i; + for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i]; + for (i = 0; i < msg.length; i++) sm[i+crypto_sign_BYTES] = msg[i]; + return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0); }; -export const keyPair = function() { +export 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}; -}; + }, -keyPair.fromSecretKey = function(secretKey) { + fromSecretKey(secretKey) { 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)}; -}; + }, -keyPair.fromSeed = function(seed) { + fromSeed(seed) { checkArrayTypes(seed); if (seed.length !== crypto_sign_SEEDBYTES) throw new Error('bad seed size'); @@ -836,7 +837,8 @@ keyPair.fromSeed = function(seed) { for (var i = 0; i < 32; i++) sk[i] = seed[i]; crypto_sign_keypair(pk, sk, true); return {publicKey: pk, secretKey: sk}; -}; + } +} export const publicKeyLength = crypto_sign_PUBLICKEYBYTES; export const secretKeyLength = crypto_sign_SECRETKEYBYTES; @@ -874,3 +876,5 @@ const setPRNG = function(fn) { } } })(); + +export const nacl = { sign, open, detached, verify, keyPair, publicKeyLength, secretKeyLength, seedLength, signatureLength } \ No newline at end of file -- 2.34.1