From 7d19a5649fbbbb7fba9d3ec0df42b837c95053e2 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 24 Nov 2024 15:22:47 -0800 Subject: [PATCH] Remove self reference in nacl since we're using it as a module. Replace imports of old implementation. --- src/lib/account.ts | 4 +- src/lib/block.ts | 2 +- src/lib/tools.ts | 2 +- src/lib/workers/nano25519.ts | 92 ++++++++++++++++++------------------ 4 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/lib/account.ts b/src/lib/account.ts index e9fb73e..a98a5be 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -4,9 +4,9 @@ import blake2b from 'blake2b' import { ACCOUNT_KEY_LENGTH, ALPHABET, PREFIX, PREFIX_LEGACY } from './constants.js' import { base32, bytes, hex } from './convert.js' -import { keyPair } from './workers/nano25519.js' import { Rpc } from './rpc.js' import { Safe } from './safe.js' +import * as Ed25519 from './workers/nano25519.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 } = keyPair.fromSeed(hex.toBytes(key)) + const { publicKey } = Ed25519.sign.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 577e6c9..cd4b5c2 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -4,9 +4,9 @@ import { BURN_ADDRESS, PREAMBLE } from './constants.js' import { Account } from './account.js' import { bytes, dec, hex } from './convert.js' -import Ed25519 from './ed25519.js' import { Rpc } from './rpc.js' import Tools from './tools.js' +import * as Ed25519 from './workers/nano25519.js' /** * Represents a block as defined by the Nano cryptocurrency protocol. The Block diff --git a/src/lib/tools.ts b/src/lib/tools.ts index 6763413..c6bdbe2 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -5,10 +5,10 @@ import blake2b from 'blake2b' import { Account } from './account.js' import { UNITS } from './constants.js' import { bytes, hex } from './convert.js' -import Ed25519 from './ed25519.js' import { Rpc } from './rpc.js' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from './wallet.js' import { SendBlock } from './block.js' +import * as Ed25519 from './workers/nano25519.js' /** * Converts a decimal amount of nano from one unit divider to another. diff --git a/src/lib/workers/nano25519.ts b/src/lib/workers/nano25519.ts index 0a4351d..754b50e 100644 --- a/src/lib/workers/nano25519.ts +++ b/src/lib/workers/nano25519.ts @@ -2110,7 +2110,7 @@ import blake2b from 'blake2b' crypto_sign_SEEDBYTES = 32, crypto_hash_BYTES = 64; - nacl.lowlevel = { + lowlevel = { crypto_core_hsalsa20: crypto_core_hsalsa20, crypto_stream_xor: crypto_stream_xor, crypto_stream: crypto_stream, @@ -2192,13 +2192,13 @@ import blake2b from 'blake2b' for (var i = 0; i < arr.length; i++) arr[i] = 0; } - nacl.randomBytes = function(n) { + randomBytes = function(n) { var b = new Uint8Array(n); randombytes(b, n); return b; }; - nacl.secretbox = function(msg, nonce, key) { + secretbox = function(msg, nonce, key) { checkArrayTypes(msg, nonce, key); checkLengths(key, nonce); var m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length); @@ -2208,7 +2208,7 @@ import blake2b from 'blake2b' return c.subarray(crypto_secretbox_BOXZEROBYTES); }; - nacl.secretbox.open = function(box, nonce, key) { + secretbox.open = function(box, nonce, key) { checkArrayTypes(box, nonce, key); checkLengths(key, nonce); var c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length); @@ -2219,11 +2219,11 @@ import blake2b from 'blake2b' return m.subarray(crypto_secretbox_ZEROBYTES); }; - nacl.secretbox.keyLength = crypto_secretbox_KEYBYTES; - nacl.secretbox.nonceLength = crypto_secretbox_NONCEBYTES; - nacl.secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES; + secretbox.keyLength = crypto_secretbox_KEYBYTES; + secretbox.nonceLength = crypto_secretbox_NONCEBYTES; + secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES; - nacl.scalarMult = function(n, p) { + scalarMult = function(n, p) { checkArrayTypes(n, p); if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size'); if (p.length !== crypto_scalarmult_BYTES) throw new Error('bad p size'); @@ -2232,7 +2232,7 @@ import blake2b from 'blake2b' return q; }; - nacl.scalarMult.base = function(n) { + scalarMult.base = function(n) { checkArrayTypes(n); if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size'); var q = new Uint8Array(crypto_scalarmult_BYTES); @@ -2240,15 +2240,15 @@ import blake2b from 'blake2b' return q; }; - nacl.scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES; - nacl.scalarMult.groupElementLength = crypto_scalarmult_BYTES; + scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES; + scalarMult.groupElementLength = crypto_scalarmult_BYTES; - nacl.box = function(msg, nonce, publicKey, secretKey) { - var k = nacl.box.before(publicKey, secretKey); - return nacl.secretbox(msg, nonce, k); + box = function(msg, nonce, publicKey, secretKey) { + var k = box.before(publicKey, secretKey); + return secretbox(msg, nonce, k); }; - nacl.box.before = function(publicKey, secretKey) { + box.before = function(publicKey, secretKey) { checkArrayTypes(publicKey, secretKey); checkBoxLengths(publicKey, secretKey); var k = new Uint8Array(crypto_box_BEFORENMBYTES); @@ -2256,23 +2256,23 @@ import blake2b from 'blake2b' return k; }; - nacl.box.after = nacl.secretbox; + box.after = secretbox; - nacl.box.open = function(msg, nonce, publicKey, secretKey) { - var k = nacl.box.before(publicKey, secretKey); - return nacl.secretbox.open(msg, nonce, k); + box.open = function(msg, nonce, publicKey, secretKey) { + var k = box.before(publicKey, secretKey); + return secretbox.open(msg, nonce, k); }; - nacl.box.open.after = nacl.secretbox.open; + box.open.after = secretbox.open; - nacl.box.keyPair = function() { + box.keyPair = function() { var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES); var sk = new Uint8Array(crypto_box_SECRETKEYBYTES); crypto_box_keypair(pk, sk); return {publicKey: pk, secretKey: sk}; }; - nacl.box.keyPair.fromSecretKey = function(secretKey) { + box.keyPair.fromSecretKey = function(secretKey) { checkArrayTypes(secretKey); if (secretKey.length !== crypto_box_SECRETKEYBYTES) throw new Error('bad secret key size'); @@ -2281,13 +2281,13 @@ import blake2b from 'blake2b' return {publicKey: pk, secretKey: new Uint8Array(secretKey)}; }; - nacl.box.publicKeyLength = crypto_box_PUBLICKEYBYTES; - nacl.box.secretKeyLength = crypto_box_SECRETKEYBYTES; - nacl.box.sharedKeyLength = crypto_box_BEFORENMBYTES; - nacl.box.nonceLength = crypto_box_NONCEBYTES; - nacl.box.overheadLength = nacl.secretbox.overheadLength; + box.publicKeyLength = crypto_box_PUBLICKEYBYTES; + box.secretKeyLength = crypto_box_SECRETKEYBYTES; + box.sharedKeyLength = crypto_box_BEFORENMBYTES; + box.nonceLength = crypto_box_NONCEBYTES; + box.overheadLength = secretbox.overheadLength; - nacl.sign = function(msg, secretKey) { + sign = function(msg, secretKey) { checkArrayTypes(msg, secretKey); if (secretKey.length !== crypto_sign_SECRETKEYBYTES) throw new Error('bad secret key size'); @@ -2296,7 +2296,7 @@ import blake2b from 'blake2b' return signedMsg; }; - nacl.sign.open = function(signedMsg, publicKey) { + sign.open = function(signedMsg, publicKey) { checkArrayTypes(signedMsg, publicKey); if (publicKey.length !== crypto_sign_PUBLICKEYBYTES) throw new Error('bad public key size'); @@ -2308,14 +2308,14 @@ import blake2b from 'blake2b' return m; }; - nacl.sign.detached = function(msg, secretKey) { - var signedMsg = nacl.sign(msg, secretKey); + sign.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; }; - nacl.sign.detached.verify = function(msg, sig, publicKey) { + sign.detached.verify = function(msg, sig, publicKey) { checkArrayTypes(msg, sig, publicKey); if (sig.length !== crypto_sign_BYTES) throw new Error('bad signature size'); @@ -2329,14 +2329,14 @@ import blake2b from 'blake2b' return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0); }; - nacl.sign.keyPair = function() { + sign.keyPair = function() { var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES); var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES); crypto_sign_keypair(pk, sk); return {publicKey: pk, secretKey: sk}; }; - nacl.sign.keyPair.fromSecretKey = function(secretKey) { + sign.keyPair.fromSecretKey = function(secretKey) { checkArrayTypes(secretKey); if (secretKey.length !== crypto_sign_SECRETKEYBYTES) throw new Error('bad secret key size'); @@ -2345,7 +2345,7 @@ import blake2b from 'blake2b' return {publicKey: pk, secretKey: new Uint8Array(secretKey)}; }; - nacl.sign.keyPair.fromSeed = function(seed) { + sign.keyPair.fromSeed = function(seed) { checkArrayTypes(seed); if (seed.length !== crypto_sign_SEEDBYTES) throw new Error('bad seed size'); @@ -2356,21 +2356,21 @@ import blake2b from 'blake2b' return {publicKey: pk, secretKey: sk}; }; - nacl.sign.publicKeyLength = crypto_sign_PUBLICKEYBYTES; - nacl.sign.secretKeyLength = crypto_sign_SECRETKEYBYTES; - nacl.sign.seedLength = crypto_sign_SEEDBYTES; - nacl.sign.signatureLength = crypto_sign_BYTES; + sign.publicKeyLength = crypto_sign_PUBLICKEYBYTES; + sign.secretKeyLength = crypto_sign_SECRETKEYBYTES; + sign.seedLength = crypto_sign_SEEDBYTES; + sign.signatureLength = crypto_sign_BYTES; - nacl.hash = function(msg) { + hash = function(msg) { checkArrayTypes(msg); var h = new Uint8Array(crypto_hash_BYTES); crypto_hash(h, msg, msg.length); return h; }; - nacl.hash.hashLength = crypto_hash_BYTES; + hash.hashLength = crypto_hash_BYTES; - nacl.verify = function(x, y) { + verify = function(x, y) { checkArrayTypes(x, y); // Zero length arguments are considered not equal. if (x.length === 0 || y.length === 0) return false; @@ -2378,7 +2378,7 @@ import blake2b from 'blake2b' return (vn(x, 0, y, 0, x.length) === 0) ? true : false; }; - nacl.setPRNG = function(fn) { + setPRNG = function(fn) { randombytes = fn; }; @@ -2389,7 +2389,7 @@ import blake2b from 'blake2b' if (crypto && crypto.getRandomValues) { // Browsers. var QUOTA = 65536; - nacl.setPRNG(function(x, n) { + setPRNG(function(x, n) { var i, v = new Uint8Array(n); for (i = 0; i < n; i += QUOTA) { crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA))); @@ -2401,7 +2401,7 @@ import blake2b from 'blake2b' // Node.js. crypto = require('crypto'); if (crypto && crypto.randomBytes) { - nacl.setPRNG(function(x, n) { + setPRNG(function(x, n) { var i, v = crypto.randomBytes(n); for (i = 0; i < n; i++) x[i] = v[i]; cleanup(v); @@ -2410,4 +2410,4 @@ import blake2b from 'blake2b' } })(); - export { box, keyPair, getPublicKey, hash, message, randomBytes, scalarMult } + export { box, hash, randomBytes, scalarMult, secretbox, sign, verify } -- 2.34.1