@digitalcredentials/ed25519-verification-key-2020
Version:
Javascript library for generating and working with Ed25519VerificationKey2020 key pairs, for use with crypto-ld.
33 lines • 1.24 kB
JavaScript
/*!
* Copyright (c) 2022 Digital Credentials Consortium. (Conversion to Typescript)
* Copyright (c) 2022 Digital Bazaar, Inc. All rights reserved.
*/
/**
* Asserts that key bytes have a type of Uint8Array and a specific length.
*
* @throws {TypeError|SyntaxError} - Throws a Type or Syntax error.
*
* @param {object} options - Options to use.
* @param {Uint8Array} options.bytes - The bytes being checked.
* @param {number} [options.expectedLength=32] - The expected bytes length.
* @param {string} [options.code] - An optional code for the error.
*
* @returns {undefined} Returns on success throws on error.
*/
export function assertKeyBytes({ bytes, expectedLength = 32, code }) {
if (!(bytes instanceof Uint8Array)) {
throw new TypeError('"bytes" must be a Uint8Array.');
}
if (bytes.length !== expectedLength) {
const error = new Error(`"bytes" must be a ${expectedLength}-byte Uint8Array.`);
// we need DataError for invalid byte length
error.name = 'DataError';
// add the error code from the did:key spec if provided
if (code) {
// @ts-ignore
error.code = code;
}
throw error;
}
}
//# sourceMappingURL=validators.js.map