import { Mutex, constants, getOutput, setInput, derive as wasm_derive, } from './wasm'
const { KEY_BYTELENGTH, } = constants
-let privateKey = allocate(KEY_BYTELENGTH)
-let publicKey = allocate(KEY_BYTELENGTH)
+const privateKey = allocate(KEY_BYTELENGTH)
+const publicKey = allocate(KEY_BYTELENGTH)
export function derive (prv: unknown, out?: unknown): string | void {
if (typeof out !== 'undefined' && !(isBytes(out) && byteLength(out) === KEY_BYTELENGTH)) {
}
Mutex.lock()
try {
- privateKey = normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv)
+ normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv, privateKey)
setInput('prv', privateKey)
wasm_derive()
for (let i = 0; i < KEY_BYTELENGTH; i++) {
/**
* Decodes hex string to Bytes.
*/
-function hexToBytes (hex: string): Bytes {
+function hexToBytes (out: Bytes, hex: string): void {
const length = hex.length
if (length & 1) {
throw new TypeError(`Invalid hexadecimal length ${length}`)
}
- const bytes = new Bytes(length >> 1)
+ const byteLength = getByteLength(out)
+ if (byteLength !== (length >> 1)) {
+ throw new TypeError(`Invalid output byte length ${byteLength}`)
+ }
let d = 0
for (let i = 0, b = 0; i < length; i += 2, b++) {
const hi = nibble(charCodeAt(hex, i))
const lo = nibble(charCodeAt(hex, i + 1))
d |= hi | lo
- bytes[b] = (hi << 4) | lo
+ out[b] = (hi << 4) | lo
}
if (d >> 4) {
- fill(bytes, 0)
+ fill(out, 0)
throw new TypeError('Invalid hexadecimal character')
}
- return bytes
}
+
+export function normalize (name: string, byteLengthMin: number, byteLengthMax: number, value: unknown): Bytes
+export function normalize (name: string, byteLengthMin: number, byteLengthMax: number, value: unknown, out: Bytes): Bytes
/**
* Accepts an unknown input; checks that it is a representation of bytes using a
* hex string or Uint8Array<ArrayBuffer>; and validates that the byte length of
* @param {unknown} value User input to validate
* @returns User input bytes copied to a new Uint8Array for internal use.
*/
-export function normalize (name: string, byteLengthMin: number, byteLengthMax: number, value: unknown): Bytes {
+export function normalize (name: string, byteLengthMin: number, byteLengthMax: number, value: unknown, out?: Bytes): Bytes {
if (typeof value === 'string') {
if (value.length < (byteLengthMin << 1) || value.length > (byteLengthMax << 1)) {
throw new TypeError(`Invalid hexadecimal length ${value.length} for ${name}`)
}
- return hexToBytes(value)
+ if (typeof out === 'undefined') out = allocate(value.length >> 1)
+ hexToBytes(out, value)
+ return out
}
if (isBytes(value)) {
if (byteLength > byteLengthMax) {
throw new TypeError(`${name} must be no more than ${byteLengthMax} bytes`)
}
- const bytes = copy(value)
- return bytes
+ if (typeof out === 'undefined') out = allocate(byteLength)
+ copy(out, value)
+ return out
}
throw new TypeError(`${name} must be a hex string or Uint8Array<ArrayBuffer>`)
import { Mutex, constants, getOutput, setInput, sign as wasm_sign, } from './wasm'
const { KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, SIGNATURE_BYTELENGTH, } = constants
-let privateKey = allocate(KEY_BYTELENGTH)
-let publicKey = allocate(KEY_BYTELENGTH)
-let signature = allocate(SIGNATURE_BYTELENGTH)
+const privateKey = allocate(KEY_BYTELENGTH)
+const publicKey = allocate(KEY_BYTELENGTH)
+const signature = allocate(SIGNATURE_BYTELENGTH)
export function sign (msg: unknown, prv: unknown, pub: unknown, out?: unknown): string | void {
if (typeof out !== 'undefined' && !(isBytes(out) && byteLength(out) === SIGNATURE_BYTELENGTH)) {
Mutex.lock()
try {
const message = normalize('message', 0, MAX_MESSAGE_BYTELENGTH, msg)
- privateKey = normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv)
- publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub)
+ normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv, privateKey)
+ normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub, publicKey)
setInput('msg', message)
setInput('prv', privateKey)
setInput('pub', publicKey)
import { Mutex, constants, getOutput, setInput, verify as wasm_verify, verify_blocks as wasm_verify_blocks, } from './wasm'
const { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, MAX_VERIFY_BLOCKS, SIGNATURE_BYTELENGTH, } = constants
+const hash = allocate(BLOCKHASH_BYTELENGTH)
+const publicKey = allocate(KEY_BYTELENGTH)
+const signature = allocate(SIGNATURE_BYTELENGTH)
export function verify (sig: unknown, msg: unknown, pub: unknown): boolean {
Mutex.lock()
try {
const message = normalize('message', 0, MAX_MESSAGE_BYTELENGTH, msg)
- const publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub)
- const signature = normalize('signature', SIGNATURE_BYTELENGTH, SIGNATURE_BYTELENGTH, sig)
+ normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub, publicKey)
+ normalize('signature', SIGNATURE_BYTELENGTH, SIGNATURE_BYTELENGTH, sig, signature)
setInput('msg', message)
setInput('pub', publicKey)
setInput('sig', signature)
const verified: boolean[] = []
Mutex.lock()
try {
- const publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub)
+ normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub, publicKey)
setInput('pub', publicKey)
const message = allocate(count * (SIGNATURE_BYTELENGTH + BLOCKHASH_BYTELENGTH))
for (let b = 0; b < count; b++) {
if (!('hash' in block)) {
throw new TypeError('Block hash is required to verify', { cause: block })
}
- const signature = normalize('signature', SIGNATURE_BYTELENGTH, SIGNATURE_BYTELENGTH, block.signature)
- const hash = normalize('hash', BLOCKHASH_BYTELENGTH, BLOCKHASH_BYTELENGTH, block.hash)
+ normalize('signature', SIGNATURE_BYTELENGTH, SIGNATURE_BYTELENGTH, block.signature, signature)
+ normalize('hash', BLOCKHASH_BYTELENGTH, BLOCKHASH_BYTELENGTH, block.hash, hash)
const offset = b * (SIGNATURE_BYTELENGTH + BLOCKHASH_BYTELENGTH)
copy(message, signature, offset)
copy(message, hash, offset + SIGNATURE_BYTELENGTH)