UNPKG

@dwn-protocol/id-sdk

Version:

SDK for accessing the features and capabilities

86 lines 3.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.X25519 = void 0; const ed25519_1 = require("@noble/curves/ed25519"); /** * The `X25519` class provides an interface for X25519 (Curve25519) key pair * generation, public key computation, and shared secret computation. The class * uses the '@noble/curves/ed25519' package for the cryptographic operations. * * All methods of this class are asynchronous and return Promises. They all use * the Uint8Array type for keys and data, providing a consistent * interface for working with binary data. * * Example usage: * * ```ts * const ownKeyPair = await X25519.generateKeyPair(); * const otherPartyKeyPair = await X25519.generateKeyPair(); * const sharedSecret = await X25519.sharedSecret({ * privateKey : ownKeyPair.privateKey, * publicKey : otherPartyKeyPair.publicKey * }); * ``` */ class X25519 { /** * Generates a key pair for X25519 (private and public key). * * @returns A Promise that resolves to a BytesKeyPair object. */ static async generateKeyPair() { // Generate the private key and compute its public key. const privateKey = ed25519_1.x25519.utils.randomPrivateKey(); const publicKey = ed25519_1.x25519.getPublicKey(privateKey); const keyPair = { privateKey: privateKey, publicKey: publicKey }; return keyPair; } /** * Computes a public key given a private key. * * @param options - The options for the public key computation operation. * @param options.privateKey - The private key used to compute the public key. * @returns A Promise that resolves to the computed public key as a Uint8Array. */ static async getPublicKey(options) { let { privateKey } = options; // Compute public key. const publicKey = ed25519_1.x25519.getPublicKey(privateKey); return publicKey; } /** * Generates a RFC6090 ECDH shared secret given the private key of one party * and the public key of another party. * * @param options - The options for the shared secret computation operation. * @param options.privateKey - The private key of one party. * @param options.publicKey - The public key of the other party. * @returns A Promise that resolves to the computed shared secret as a Uint8Array. */ static async sharedSecret(options) { let { privateKey, publicKey } = options; const sharedSecret = ed25519_1.x25519.getSharedSecret(privateKey, publicKey); return sharedSecret; } /** * Note that this method is currently unimplemented because the @noble/curves * library does not yet provide a mechanism for checking whether a point * belongs to the Curve25519. Therefore, it currently throws an error whenever * it is called. * * @param options - The options for the key validation operation. * @param options.key - The key to validate. * @throws {Error} If the method is called because it is not yet implemented. * @returns A Promise that resolves to void. */ static async validatePublicKey(_options) { // TODO: add once/if @noble/curves library implements checking // proper points on the Montgomery curve. throw new Error(`Not implemented: 'validatePublicKey()'`); } } exports.X25519 = X25519; //# sourceMappingURL=x25519.js.map