nano25519 can be imported into NodeJS projects:
```javascript
-import * as nano25519 from "nano25519";
+import { derive, sign, verify } from "nano25519";
```
-Sync and async modules can be imported separately depending on requirements.
+or
```javascript
-import { derive, sign, verify } from "nano25519/sync";
-import { deriveAsync, signAsync, verifyAsync } from "nano25519/async";
+import * as nano25519 from "nano25519";
```
## Usage
-Each function has a synchronous implementation which is preferable for speed.
-
-If a non-blocking solution is required, asynchronous implementations are
-provided which use Web Workers in the browser or Worker threads in NodeJS.
-
-Inputs can be passed as Uint8Array byte arrays (preferable) or strings. The
+Each primitive implementation is synchronous. Inputs can be passed as Uint8Array byte arrays (preferable) or strings. The
output will return the same type as the input. A preallocated output buffer can
-be passed as an additional optional argument to the sync versions of `derive()`
-and `sign()` into which the result bytes will be written.
+be passed as an additional optional argument for `derive()`
+and `sign()`, and the result bytes will be written into it.
### Derive
prv.fill(0);
```
-### Derive (async)
+### Derive (strings)
```javascript
// `prv` is a 64-character hex string private key
const prv = "0000000000000000000000000000000000000000000000000000000000000000";
// `pub` is a 64-character hex string public key for a Nano account
-const pub = await nano25519.deriveAsync(prv);
+const pub = nano25519.derive(prv);
```
### Sign
```javascript
// `msg` is a 32-byte hash of a valid Nano transaction block
const msg = new Uint8Array(blockHashBuffer);
-// `sk` is a 64-byte secret key joining private and public keys
-const sk = new Uint8Array(joinPrivateKeyPublicKeyBuffer);
+// `prv` is a 32-byte private key
+const prv = new Uint8Array(privateKeyBuffer);
+// `pub` is a 32-byte public key derived from `prv`
+const pub = new Uint8Array(publicKeyBuffer);
// `sig` is a 64-byte Uint8Array signature for the block hash
-const sig = nano25519.sign(msg, sk);
+const sig = nano25519.sign(msg, prv, pub);
// clean up
sk.fill(0);
```
```javascript
// `msg` is a 32-byte hash of a valid Nano transaction block
const msg = new Uint8Array(blockHashBuffer);
-// `sk` is a 64-byte secret key joining private and public keys
-const sk = new Uint8Array(joinPrivateKeyPublicKeyBuffer);
+// `prv` is a 32-byte private key
+const prv = new Uint8Array(privateKeyBuffer);
+// `pub` is a 32-byte public key derived from `prv`
+const pub = new Uint8Array(publicKeyBuffer);
// `sig` is an empty 64-byte Uint8Array
const sig = new Uint8Array(64);
-// return discarded, 64-byte signature is written to `sig`
-nano25519.sign(msg, sk);
+// return value is discarded, 64-byte signature is written to `sig`
+nano25519.sign(msg, prv, pub);
// clean up
sk.fill(0);
```
-### Sign (async)
+### Sign (strings)
```javascript
// `msg` is a 64-char hex string hash of a valid Nano transaction block
const prv = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
// `pub` is a 64-char hex string public key derived from `prv`
const pub = await nano25519.deriveAsync(prv);
-// `sk` is a 128-char hex string secret key joining private and public keys
-const sk = prv + pub;
// `sig` is a 128-char hex string signature for the block hash
-const sig = await nano25519.signAsync(msg, sk);
+const sig = await nano25519.signAsync(msg, prv, pub);
```
### Verify
const v = nano25519.verify(sig, msg, pub);
```
-### Verify (async)
+### Verify (strings)
```javascript
// `sig` is a 128-char hex string signature
const pk8 = nano25519.derive(sk8)
return [...pk8].map(b => b.toString(16).padStart(2, '0')).join('')
}
- case 'nano25519 (async)': {
- return nano25519.deriveAsync(sk)
- }
case 'NanocurrencyWeb': {
return NanocurrencyWeb.wallet.legacyAccounts(sk)[0].publicKey
}
const s8 = nano25519.sign(h8, sk8)
return [...s8].map(b => b.toString(16).padStart(2, '0')).join('')
}
- case 'nano25519 (async)': {
- return nano25519.signAsync(h, sk + pk)
- }
case 'NanocurrencyWeb': {
const { privateKey } = NanocurrencyWeb.wallet.legacyAccounts(sk)[0]
return NanocurrencyWeb.tools.sign(privateKey, h)
case 'nano25519': {
return nano25519.verify(s8, h8, pk8)
}
- case 'nano25519 (async)': {
- return nano25519.verifyAsync(s, h, pk)
- }
case 'NanocurrencyWeb': {
return NanocurrencyWeb.tools.verify(pk, s, h)
}
}
}
- async function sequenceDeriveSignVerify(api, blockHash, privateKey) {
+ function sequenceDeriveSignVerify(api, blockHash, privateKey) {
let start = 0, end = 0, publicKey = null, signature = null, isValid = false
start = performance.now()
return end - start
}
- export async function test(api, size, runs) {
+ export function test(api, size, runs) {
if (typeof size !== 'number' || size < 1) {
size = 1
}