// SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>\r
// SPDX-License-Identifier: GPL-3.0-or-later\r
-//@ts-nocheck\r
\r
'use strict';\r
\r
};\r
\r
// Pluggable, initialized in high-level API below.\r
-var randombytes = function(/* x, n */) { throw new Error('no PRNG'); };\r
+var randombytes = function(x, n) { throw new Error('no PRNG'); };\r
\r
var gf0 = gf(),\r
gf1 = gf([1]),\r
scalarmult(p, q, s);\r
}\r
\r
-function crypto_sign_keypair (pk, sk, seeded?) {\r
+function crypto_sign_keypair (pk, sk, seeded?: boolean) {\r
var d = new Uint8Array(64);\r
var p = [gf(), gf(), gf(), gf()];\r
var i;\r
\r
/* High-level API */\r
\r
-function checkArrayTypes() {\r
- for (var i = 0; i < arguments.length; i++) {\r
- if (!(arguments[i] instanceof Uint8Array))\r
- throw new TypeError('unexpected type, use Uint8Array');\r
+function checkArrayTypes (...args: Uint8Array[]) {\r
+ for (var i = 0; i < args.length; i++) {\r
+ if (!(args[i] instanceof Uint8Array))\r
+ throw new TypeError(`expected Uint8Array; received ${args[i].constructor?.name ?? typeof args[i]}`);\r
}\r
}\r
\r
-function cleanup(arr) {\r
+function cleanup(arr: Uint8Array | any[]) {\r
for (var i = 0; i < arr.length; i++) arr[i] = 0;\r
}\r
\r
-const sign = function(msg, secretKey) {\r
+ const sign = function (msg: Uint8Array, secretKey: Uint8Array) {\r
checkArrayTypes(msg, secretKey);\r
if (secretKey.length !== crypto_sign_SECRETKEYBYTES)\r
throw new Error('bad secret key size');\r
return signedMsg;\r
};\r
\r
-const open = function(signedMsg, publicKey) {\r
+const open = function(signedMsg: Uint8Array, publicKey: Uint8Array) {\r
checkArrayTypes(signedMsg, publicKey);\r
if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)\r
throw new Error('bad public key size');\r
return m;\r
};\r
\r
-const detached = function(msg, secretKey) {\r
+const detached = function (msg: Uint8Array, secretKey: Uint8Array) {\r
var signedMsg = sign(msg, secretKey);\r
var sig = new Uint8Array(crypto_sign_BYTES);\r
for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i];\r
return sig;\r
};\r
\r
-const verify = function(msg, sig, publicKey) {\r
+const verify = function(msg: Uint8Array, sig: Uint8Array, publicKey: Uint8Array) {\r
checkArrayTypes(msg, sig, publicKey);\r
if (sig.length !== crypto_sign_BYTES)\r
throw new Error('bad signature size');\r
return {publicKey: pk, secretKey: sk};\r
},\r
\r
- fromSecretKey(secretKey) {\r
+ fromSecretKey(secretKey: Uint8Array) {\r
checkArrayTypes(secretKey);\r
if (secretKey.length !== crypto_sign_SECRETKEYBYTES)\r
throw new Error('bad secret key size');\r
return {publicKey: pk, secretKey: new Uint8Array(secretKey)};\r
},\r
\r
- fromSeed (seed) {\r
+ fromSeed (seed: Uint8Array) {\r
checkArrayTypes(seed);\r
if (seed.length !== crypto_sign_SEEDBYTES)\r
throw new Error('bad seed size');\r
(function() {\r
// Initialize PRNG if environment provides CSPRNG.\r
// If not, methods calling randombytes will throw.\r
- var crypto = typeof self !== 'undefined' ? (self.crypto || self.msCrypto) : null;\r
+ var crypto = self?.crypto ?? null;\r
if (crypto && crypto.getRandomValues) {\r
// Browsers.\r
var QUOTA = 65536;\r