@enbox/crypto 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -102
- package/dist/browser.js +6 -10
- package/dist/browser.js.map +4 -4
- package/dist/browser.mjs +6 -10
- package/dist/browser.mjs.map +4 -4
- package/dist/esm/algorithms/aes-gcm.js +1 -1
- package/dist/esm/algorithms/aes-gcm.js.map +1 -1
- package/dist/esm/algorithms/ecdsa.js +9 -5
- package/dist/esm/algorithms/ecdsa.js.map +1 -1
- package/dist/esm/algorithms/eddsa.js +9 -5
- package/dist/esm/algorithms/eddsa.js.map +1 -1
- package/dist/esm/algorithms/sha-2.js +1 -1
- package/dist/esm/algorithms/sha-2.js.map +1 -1
- package/dist/esm/crypto-error.js +41 -0
- package/dist/esm/crypto-error.js.map +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/jose/jwk.js +52 -26
- package/dist/esm/jose/jwk.js.map +1 -1
- package/dist/esm/local-key-manager.js +3 -2
- package/dist/esm/local-key-manager.js.map +1 -1
- package/dist/esm/primitives/aes-ctr.js.map +1 -1
- package/dist/esm/primitives/aes-gcm.js.map +1 -1
- package/dist/esm/primitives/aes-kw.js +246 -0
- package/dist/esm/primitives/aes-kw.js.map +1 -0
- package/dist/esm/primitives/concat-kdf.js +1 -1
- package/dist/esm/primitives/concat-kdf.js.map +1 -1
- package/dist/esm/primitives/ed25519.js +3 -3
- package/dist/esm/primitives/ed25519.js.map +1 -1
- package/dist/esm/primitives/hkdf.js +79 -0
- package/dist/esm/primitives/hkdf.js.map +1 -0
- package/dist/esm/primitives/pbkdf2.js +49 -0
- package/dist/esm/primitives/pbkdf2.js.map +1 -1
- package/dist/esm/primitives/secp256k1.js +4 -4
- package/dist/esm/primitives/secp256k1.js.map +1 -1
- package/dist/esm/primitives/secp256r1.js +4 -4
- package/dist/esm/primitives/secp256r1.js.map +1 -1
- package/dist/esm/primitives/x25519.js +1 -1
- package/dist/esm/primitives/x25519.js.map +1 -1
- package/dist/esm/primitives/xchacha20-poly1305.js +48 -3
- package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -1
- package/dist/esm/primitives/xchacha20.js +1 -1
- package/dist/esm/primitives/xchacha20.js.map +1 -1
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/algorithms/aes-ctr.d.ts +1 -1
- package/dist/types/algorithms/aes-ctr.d.ts.map +1 -1
- package/dist/types/algorithms/aes-gcm.d.ts +2 -2
- package/dist/types/algorithms/aes-gcm.d.ts.map +1 -1
- package/dist/types/algorithms/ecdsa.d.ts +1 -1
- package/dist/types/algorithms/ecdsa.d.ts.map +1 -1
- package/dist/types/algorithms/eddsa.d.ts +2 -2
- package/dist/types/algorithms/eddsa.d.ts.map +1 -1
- package/dist/types/algorithms/sha-2.d.ts +1 -1
- package/dist/types/algorithms/sha-2.d.ts.map +1 -1
- package/dist/types/crypto-error.d.ts +29 -0
- package/dist/types/crypto-error.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/jose/jwk.d.ts.map +1 -1
- package/dist/types/local-key-manager.d.ts +3 -3
- package/dist/types/local-key-manager.d.ts.map +1 -1
- package/dist/types/primitives/aes-kw.d.ts +103 -0
- package/dist/types/primitives/aes-kw.d.ts.map +1 -0
- package/dist/types/primitives/concat-kdf.d.ts +1 -1
- package/dist/types/primitives/concat-kdf.d.ts.map +1 -1
- package/dist/types/primitives/hkdf.d.ts +90 -0
- package/dist/types/primitives/hkdf.d.ts.map +1 -0
- package/dist/types/primitives/pbkdf2.d.ts +58 -0
- package/dist/types/primitives/pbkdf2.d.ts.map +1 -1
- package/dist/types/primitives/xchacha20-poly1305.d.ts +47 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -1
- package/dist/types/types/cipher.d.ts +1 -1
- package/dist/types/types/crypto-api.d.ts +3 -3
- package/dist/types/types/crypto-api.d.ts.map +1 -1
- package/dist/types/types/params-direct.d.ts +79 -1
- package/dist/types/types/params-direct.d.ts.map +1 -1
- package/dist/utils.js.map +2 -2
- package/package.json +26 -39
- package/src/algorithms/aes-ctr.ts +1 -1
- package/src/algorithms/aes-gcm.ts +3 -2
- package/src/algorithms/ecdsa.ts +13 -7
- package/src/algorithms/eddsa.ts +9 -9
- package/src/algorithms/sha-2.ts +2 -2
- package/src/crypto-error.ts +45 -0
- package/src/index.ts +3 -0
- package/src/jose/jwk.ts +32 -32
- package/src/local-key-manager.ts +14 -13
- package/src/primitives/aes-ctr.ts +1 -1
- package/src/primitives/aes-gcm.ts +5 -5
- package/src/primitives/aes-kw.ts +269 -0
- package/src/primitives/concat-kdf.ts +4 -2
- package/src/primitives/ed25519.ts +6 -6
- package/src/primitives/hkdf.ts +121 -0
- package/src/primitives/pbkdf2.ts +91 -0
- package/src/primitives/secp256k1.ts +6 -6
- package/src/primitives/secp256r1.ts +6 -6
- package/src/primitives/x25519.ts +3 -3
- package/src/primitives/xchacha20-poly1305.ts +57 -4
- package/src/primitives/xchacha20.ts +1 -1
- package/src/types/cipher.ts +1 -1
- package/src/types/crypto-api.ts +5 -5
- package/src/types/params-direct.ts +97 -1
- package/src/utils.ts +2 -2
- package/dist/cjs/algorithms/aes-ctr.js +0 -188
- package/dist/cjs/algorithms/aes-ctr.js.map +0 -1
- package/dist/cjs/algorithms/aes-gcm.js +0 -196
- package/dist/cjs/algorithms/aes-gcm.js.map +0 -1
- package/dist/cjs/algorithms/crypto-algorithm.js +0 -13
- package/dist/cjs/algorithms/crypto-algorithm.js.map +0 -1
- package/dist/cjs/algorithms/ecdsa.js +0 -352
- package/dist/cjs/algorithms/ecdsa.js.map +0 -1
- package/dist/cjs/algorithms/eddsa.js +0 -325
- package/dist/cjs/algorithms/eddsa.js.map +0 -1
- package/dist/cjs/algorithms/sha-2.js +0 -119
- package/dist/cjs/algorithms/sha-2.js.map +0 -1
- package/dist/cjs/index.js +0 -41
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/jose/jwe.js +0 -3
- package/dist/cjs/jose/jwe.js.map +0 -1
- package/dist/cjs/jose/jwk.js +0 -278
- package/dist/cjs/jose/jwk.js.map +0 -1
- package/dist/cjs/jose/jws.js +0 -3
- package/dist/cjs/jose/jws.js.map +0 -1
- package/dist/cjs/jose/jwt.js +0 -3
- package/dist/cjs/jose/jwt.js.map +0 -1
- package/dist/cjs/jose/utils.js +0 -60
- package/dist/cjs/jose/utils.js.map +0 -1
- package/dist/cjs/local-key-manager.js +0 -521
- package/dist/cjs/local-key-manager.js.map +0 -1
- package/dist/cjs/package.json +0 -1
- package/dist/cjs/primitives/aes-ctr.js +0 -398
- package/dist/cjs/primitives/aes-ctr.js.map +0 -1
- package/dist/cjs/primitives/aes-gcm.js +0 -425
- package/dist/cjs/primitives/aes-gcm.js.map +0 -1
- package/dist/cjs/primitives/concat-kdf.js +0 -215
- package/dist/cjs/primitives/concat-kdf.js.map +0 -1
- package/dist/cjs/primitives/ed25519.js +0 -651
- package/dist/cjs/primitives/ed25519.js.map +0 -1
- package/dist/cjs/primitives/pbkdf2.js +0 -120
- package/dist/cjs/primitives/pbkdf2.js.map +0 -1
- package/dist/cjs/primitives/secp256k1.js +0 -958
- package/dist/cjs/primitives/secp256k1.js.map +0 -1
- package/dist/cjs/primitives/secp256r1.js +0 -959
- package/dist/cjs/primitives/secp256r1.js.map +0 -1
- package/dist/cjs/primitives/sha256.js +0 -93
- package/dist/cjs/primitives/sha256.js.map +0 -1
- package/dist/cjs/primitives/x25519.js +0 -498
- package/dist/cjs/primitives/x25519.js.map +0 -1
- package/dist/cjs/primitives/xchacha20-poly1305.js +0 -340
- package/dist/cjs/primitives/xchacha20-poly1305.js.map +0 -1
- package/dist/cjs/primitives/xchacha20.js +0 -316
- package/dist/cjs/primitives/xchacha20.js.map +0 -1
- package/dist/cjs/types/cipher.js +0 -3
- package/dist/cjs/types/cipher.js.map +0 -1
- package/dist/cjs/types/crypto-api.js +0 -3
- package/dist/cjs/types/crypto-api.js.map +0 -1
- package/dist/cjs/types/hasher.js +0 -3
- package/dist/cjs/types/hasher.js.map +0 -1
- package/dist/cjs/types/identifier.js +0 -3
- package/dist/cjs/types/identifier.js.map +0 -1
- package/dist/cjs/types/key-compressor.js +0 -3
- package/dist/cjs/types/key-compressor.js.map +0 -1
- package/dist/cjs/types/key-converter.js +0 -3
- package/dist/cjs/types/key-converter.js.map +0 -1
- package/dist/cjs/types/key-deriver.js +0 -3
- package/dist/cjs/types/key-deriver.js.map +0 -1
- package/dist/cjs/types/key-generator.js +0 -3
- package/dist/cjs/types/key-generator.js.map +0 -1
- package/dist/cjs/types/key-io.js +0 -3
- package/dist/cjs/types/key-io.js.map +0 -1
- package/dist/cjs/types/key-wrapper.js +0 -3
- package/dist/cjs/types/key-wrapper.js.map +0 -1
- package/dist/cjs/types/params-direct.js +0 -3
- package/dist/cjs/types/params-direct.js.map +0 -1
- package/dist/cjs/types/params-enclosed.js +0 -3
- package/dist/cjs/types/params-enclosed.js.map +0 -1
- package/dist/cjs/types/params-kms.js +0 -3
- package/dist/cjs/types/params-kms.js.map +0 -1
- package/dist/cjs/types/signer.js +0 -3
- package/dist/cjs/types/signer.js.map +0 -1
- package/dist/cjs/utils.js +0 -173
- package/dist/cjs/utils.js.map +0 -1
|
@@ -1,651 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
39
|
-
var t = {};
|
|
40
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
41
|
-
t[p] = s[p];
|
|
42
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
43
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
44
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
45
|
-
t[p[i]] = s[p[i]];
|
|
46
|
-
}
|
|
47
|
-
return t;
|
|
48
|
-
};
|
|
49
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
exports.Ed25519 = void 0;
|
|
51
|
-
var common_1 = require("@enbox/common");
|
|
52
|
-
var ed25519_1 = require("@noble/curves/ed25519");
|
|
53
|
-
var jwk_js_1 = require("../jose/jwk.js");
|
|
54
|
-
/**
|
|
55
|
-
* The `Ed25519` class provides a comprehensive suite of utilities for working with the Ed25519
|
|
56
|
-
* elliptic curve, widely used in modern cryptographic applications. This class includes methods for
|
|
57
|
-
* key generation, conversion, signing, verification, and public key derivation.
|
|
58
|
-
*
|
|
59
|
-
* The class supports conversions between raw byte formats and JSON Web Key (JWK) formats. It
|
|
60
|
-
* follows the guidelines and specifications outlined in RFC8032 for EdDSA (Edwards-curve Digital
|
|
61
|
-
* Signature Algorithm) operations.
|
|
62
|
-
*
|
|
63
|
-
* Key Features:
|
|
64
|
-
* - Key Generation: Generate Ed25519 private keys in JWK format.
|
|
65
|
-
* - Key Conversion: Transform keys between raw byte arrays and JWK formats.
|
|
66
|
-
* - Public Key Derivation: Derive public keys from private keys.
|
|
67
|
-
* - Signing and Verification: Sign data and verify signatures with Ed25519 keys.
|
|
68
|
-
* - Key Validation: Validate the mathematical correctness of Ed25519 keys.
|
|
69
|
-
*
|
|
70
|
-
* The methods in this class are asynchronous, returning Promises to accommodate various
|
|
71
|
-
* JavaScript environments, and use `Uint8Array` for binary data handling.
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* ```ts
|
|
75
|
-
* // Key Generation
|
|
76
|
-
* const privateKey = await Ed25519.generateKey();
|
|
77
|
-
*
|
|
78
|
-
* // Public Key Derivation
|
|
79
|
-
* const publicKey = await Ed25519.computePublicKey({ key: privateKey });
|
|
80
|
-
* console.log(publicKey === await Ed25519.getPublicKey({ key: privateKey })); // Output: true
|
|
81
|
-
*
|
|
82
|
-
* // EdDSA Signing
|
|
83
|
-
* const signature = await Ed25519.sign({
|
|
84
|
-
* key: privateKey,
|
|
85
|
-
* data: new TextEncoder().encode('Message')
|
|
86
|
-
* });
|
|
87
|
-
*
|
|
88
|
-
* // EdDSA Signature Verification
|
|
89
|
-
* const isValid = await Ed25519.verify({
|
|
90
|
-
* key: publicKey,
|
|
91
|
-
* signature: signature,
|
|
92
|
-
* data: new TextEncoder().encode('Message')
|
|
93
|
-
* });
|
|
94
|
-
*
|
|
95
|
-
* // Key Conversion
|
|
96
|
-
* const privateKeyBytes = await Ed25519.privateKeyToBytes({ privateKey });
|
|
97
|
-
* const publicKeyBytes = await Ed25519.publicKeyToBytes({ publicKey });
|
|
98
|
-
*
|
|
99
|
-
* // Key Validation
|
|
100
|
-
* const isPublicKeyValid = await Ed25519.validatePublicKey({ publicKeyBytes });
|
|
101
|
-
* ```
|
|
102
|
-
*/
|
|
103
|
-
var Ed25519 = /** @class */ (function () {
|
|
104
|
-
function Ed25519() {
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
|
|
108
|
-
*
|
|
109
|
-
* @remarks
|
|
110
|
-
* This method accepts a private key as a byte array (Uint8Array) for the Curve25519 curve in
|
|
111
|
-
* Twisted Edwards form and transforms it into a JWK object. The process involves first deriving
|
|
112
|
-
* the public key from the private key, then encoding both the private and public keys into
|
|
113
|
-
* base64url format.
|
|
114
|
-
*
|
|
115
|
-
* The resulting JWK object includes the following properties:
|
|
116
|
-
* - `kty`: Key Type, set to 'OKP' for Octet Key Pair.
|
|
117
|
-
* - `crv`: Curve Name, set to 'Ed25519'.
|
|
118
|
-
* - `d`: The private key component, base64url-encoded.
|
|
119
|
-
* - `x`: The computed public key, base64url-encoded.
|
|
120
|
-
*
|
|
121
|
-
* @example
|
|
122
|
-
* ```ts
|
|
123
|
-
* const privateKeyBytes = new Uint8Array([...]); // Replace with actual private key bytes
|
|
124
|
-
* const privateKey = await Ed25519.bytesToPrivateKey({ privateKeyBytes });
|
|
125
|
-
* ```
|
|
126
|
-
*
|
|
127
|
-
* @param params - The parameters for the private key conversion.
|
|
128
|
-
* @param params.privateKeyBytes - The raw private key as a Uint8Array.
|
|
129
|
-
*
|
|
130
|
-
* @returns A Promise that resolves to the private key in JWK format.
|
|
131
|
-
*/
|
|
132
|
-
Ed25519.bytesToPrivateKey = function (_a) {
|
|
133
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
134
|
-
var publicKeyBytes, privateKey, _c;
|
|
135
|
-
var privateKeyBytes = _b.privateKeyBytes;
|
|
136
|
-
return __generator(this, function (_d) {
|
|
137
|
-
switch (_d.label) {
|
|
138
|
-
case 0:
|
|
139
|
-
publicKeyBytes = ed25519_1.ed25519.getPublicKey(privateKeyBytes);
|
|
140
|
-
privateKey = {
|
|
141
|
-
crv: 'Ed25519',
|
|
142
|
-
d: common_1.Convert.uint8Array(privateKeyBytes).toBase64Url(),
|
|
143
|
-
kty: 'OKP',
|
|
144
|
-
x: common_1.Convert.uint8Array(publicKeyBytes).toBase64Url(),
|
|
145
|
-
};
|
|
146
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
147
|
-
_c = privateKey;
|
|
148
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: privateKey })];
|
|
149
|
-
case 1:
|
|
150
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
151
|
-
_c.kid = _d.sent();
|
|
152
|
-
return [2 /*return*/, privateKey];
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
};
|
|
157
|
-
/**
|
|
158
|
-
* Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
|
|
159
|
-
*
|
|
160
|
-
* @remarks
|
|
161
|
-
* This method accepts a public key as a byte array (Uint8Array) for the Curve25519 curve in
|
|
162
|
-
* Twisted Edwards form and transforms it into a JWK object. The process involves encoding the
|
|
163
|
-
* public key bytes into base64url format.
|
|
164
|
-
*
|
|
165
|
-
* The resulting JWK object includes the following properties:
|
|
166
|
-
* - `kty`: Key Type, set to 'OKP' for Octet Key Pair.
|
|
167
|
-
* - `crv`: Curve Name, set to 'X25519'.
|
|
168
|
-
* - `x`: The public key, base64url-encoded.
|
|
169
|
-
*
|
|
170
|
-
* @example
|
|
171
|
-
* ```ts
|
|
172
|
-
* const publicKeyBytes = new Uint8Array([...]); // Replace with actual public key bytes
|
|
173
|
-
* const publicKey = await X25519.bytesToPublicKey({ publicKeyBytes });
|
|
174
|
-
* ```
|
|
175
|
-
*
|
|
176
|
-
* @param params - The parameters for the public key conversion.
|
|
177
|
-
* @param params.publicKeyBytes - The raw public key as a `Uint8Array`.
|
|
178
|
-
*
|
|
179
|
-
* @returns A Promise that resolves to the public key in JWK format.
|
|
180
|
-
*/
|
|
181
|
-
Ed25519.bytesToPublicKey = function (_a) {
|
|
182
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
183
|
-
var publicKey, _c;
|
|
184
|
-
var publicKeyBytes = _b.publicKeyBytes;
|
|
185
|
-
return __generator(this, function (_d) {
|
|
186
|
-
switch (_d.label) {
|
|
187
|
-
case 0:
|
|
188
|
-
publicKey = {
|
|
189
|
-
kty: 'OKP',
|
|
190
|
-
crv: 'Ed25519',
|
|
191
|
-
x: common_1.Convert.uint8Array(publicKeyBytes).toBase64Url(),
|
|
192
|
-
};
|
|
193
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
194
|
-
_c = publicKey;
|
|
195
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: publicKey })];
|
|
196
|
-
case 1:
|
|
197
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
198
|
-
_c.kid = _d.sent();
|
|
199
|
-
return [2 /*return*/, publicKey];
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
};
|
|
204
|
-
/**
|
|
205
|
-
* Derives the public key in JWK format from a given Ed25519 private key.
|
|
206
|
-
*
|
|
207
|
-
* @remarks
|
|
208
|
-
* This method takes a private key in JWK format and derives its corresponding public key,
|
|
209
|
-
* also in JWK format. The derivation process involves converting the private key to a
|
|
210
|
-
* raw byte array and then computing the corresponding public key on the Curve25519 curve in
|
|
211
|
-
* Twisted Edwards form. The public key is then encoded into base64url format to construct
|
|
212
|
-
* a JWK representation.
|
|
213
|
-
*
|
|
214
|
-
* @example
|
|
215
|
-
* ```ts
|
|
216
|
-
* const privateKey = { ... }; // A Jwk object representing an Ed25519 private key
|
|
217
|
-
* const publicKey = await Ed25519.computePublicKey({ key: privateKey });
|
|
218
|
-
* ```
|
|
219
|
-
*
|
|
220
|
-
* @param params - The parameters for the public key derivation.
|
|
221
|
-
* @param params.key - The private key in JWK format from which to derive the public key.
|
|
222
|
-
*
|
|
223
|
-
* @returns A Promise that resolves to the computed public key in JWK format.
|
|
224
|
-
*/
|
|
225
|
-
Ed25519.computePublicKey = function (_a) {
|
|
226
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
227
|
-
var privateKeyBytes, publicKeyBytes, publicKey, _c;
|
|
228
|
-
var key = _b.key;
|
|
229
|
-
return __generator(this, function (_d) {
|
|
230
|
-
switch (_d.label) {
|
|
231
|
-
case 0: return [4 /*yield*/, Ed25519.privateKeyToBytes({ privateKey: key })];
|
|
232
|
-
case 1:
|
|
233
|
-
privateKeyBytes = _d.sent();
|
|
234
|
-
publicKeyBytes = ed25519_1.ed25519.getPublicKey(privateKeyBytes);
|
|
235
|
-
publicKey = {
|
|
236
|
-
kty: 'OKP',
|
|
237
|
-
crv: 'Ed25519',
|
|
238
|
-
x: common_1.Convert.uint8Array(publicKeyBytes).toBase64Url()
|
|
239
|
-
};
|
|
240
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
241
|
-
_c = publicKey;
|
|
242
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: publicKey })];
|
|
243
|
-
case 2:
|
|
244
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
245
|
-
_c.kid = _d.sent();
|
|
246
|
-
return [2 /*return*/, publicKey];
|
|
247
|
-
}
|
|
248
|
-
});
|
|
249
|
-
});
|
|
250
|
-
};
|
|
251
|
-
/**
|
|
252
|
-
* Converts an Ed25519 private key to its X25519 counterpart.
|
|
253
|
-
*
|
|
254
|
-
* @remarks
|
|
255
|
-
* This method enables the use of the same key pair for both digital signature (Ed25519)
|
|
256
|
-
* and key exchange (X25519) operations. It takes an Ed25519 private key and converts it
|
|
257
|
-
* to the corresponding X25519 format, facilitating interoperability between signing
|
|
258
|
-
* and encryption protocols.
|
|
259
|
-
*
|
|
260
|
-
* @example
|
|
261
|
-
* ```ts
|
|
262
|
-
* const ed25519PrivateKey = { ... }; // An Ed25519 private key in JWK format
|
|
263
|
-
* const x25519PrivateKey = await Ed25519.convertPrivateKeyToX25519({
|
|
264
|
-
* privateKey: ed25519PrivateKey
|
|
265
|
-
* });
|
|
266
|
-
* ```
|
|
267
|
-
*
|
|
268
|
-
* @param params - The parameters for the private key conversion.
|
|
269
|
-
* @param params.privateKey - The Ed25519 private key to convert, in JWK format.
|
|
270
|
-
*
|
|
271
|
-
* @returns A Promise that resolves to the X25519 private key in JWK format.
|
|
272
|
-
*/
|
|
273
|
-
Ed25519.convertPrivateKeyToX25519 = function (_a) {
|
|
274
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
275
|
-
var ed25519PrivateKeyBytes, x25519PrivateKeyBytes, x25519PublicKeyBytes, x25519PrivateKey, _c;
|
|
276
|
-
var privateKey = _b.privateKey;
|
|
277
|
-
return __generator(this, function (_d) {
|
|
278
|
-
switch (_d.label) {
|
|
279
|
-
case 0: return [4 /*yield*/, Ed25519.privateKeyToBytes({ privateKey: privateKey })];
|
|
280
|
-
case 1:
|
|
281
|
-
ed25519PrivateKeyBytes = _d.sent();
|
|
282
|
-
x25519PrivateKeyBytes = (0, ed25519_1.edwardsToMontgomeryPriv)(ed25519PrivateKeyBytes);
|
|
283
|
-
x25519PublicKeyBytes = ed25519_1.x25519.getPublicKey(x25519PrivateKeyBytes);
|
|
284
|
-
x25519PrivateKey = {
|
|
285
|
-
kty: 'OKP',
|
|
286
|
-
crv: 'X25519',
|
|
287
|
-
d: common_1.Convert.uint8Array(x25519PrivateKeyBytes).toBase64Url(),
|
|
288
|
-
x: common_1.Convert.uint8Array(x25519PublicKeyBytes).toBase64Url(),
|
|
289
|
-
};
|
|
290
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
291
|
-
_c = x25519PrivateKey;
|
|
292
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: x25519PrivateKey })];
|
|
293
|
-
case 2:
|
|
294
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
295
|
-
_c.kid = _d.sent();
|
|
296
|
-
return [2 /*return*/, x25519PrivateKey];
|
|
297
|
-
}
|
|
298
|
-
});
|
|
299
|
-
});
|
|
300
|
-
};
|
|
301
|
-
/**
|
|
302
|
-
* Converts an Ed25519 public key to its X25519 counterpart.
|
|
303
|
-
*
|
|
304
|
-
* @remarks
|
|
305
|
-
* This method enables the use of the same key pair for both digital signature (Ed25519)
|
|
306
|
-
* and key exchange (X25519) operations. It takes an Ed25519 public key and converts it
|
|
307
|
-
* to the corresponding X25519 format, facilitating interoperability between signing
|
|
308
|
-
* and encryption protocols.
|
|
309
|
-
*
|
|
310
|
-
* @example
|
|
311
|
-
* ```ts
|
|
312
|
-
* const ed25519PublicKey = { ... }; // An Ed25519 public key in JWK format
|
|
313
|
-
* const x25519PublicKey = await Ed25519.convertPublicKeyToX25519({
|
|
314
|
-
* publicKey: ed25519PublicKey
|
|
315
|
-
* });
|
|
316
|
-
* ```
|
|
317
|
-
*
|
|
318
|
-
* @param params - The parameters for the public key conversion.
|
|
319
|
-
* @param params.publicKey - The Ed25519 public key to convert, in JWK format.
|
|
320
|
-
*
|
|
321
|
-
* @returns A Promise that resolves to the X25519 public key in JWK format.
|
|
322
|
-
*/
|
|
323
|
-
Ed25519.convertPublicKeyToX25519 = function (_a) {
|
|
324
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
325
|
-
var ed25519PublicKeyBytes, isValid, x25519PublicKeyBytes, x25519PublicKey, _c;
|
|
326
|
-
var publicKey = _b.publicKey;
|
|
327
|
-
return __generator(this, function (_d) {
|
|
328
|
-
switch (_d.label) {
|
|
329
|
-
case 0: return [4 /*yield*/, Ed25519.publicKeyToBytes({ publicKey: publicKey })];
|
|
330
|
-
case 1:
|
|
331
|
-
ed25519PublicKeyBytes = _d.sent();
|
|
332
|
-
return [4 /*yield*/, Ed25519.validatePublicKey({ publicKeyBytes: ed25519PublicKeyBytes })];
|
|
333
|
-
case 2:
|
|
334
|
-
isValid = _d.sent();
|
|
335
|
-
if (!isValid) {
|
|
336
|
-
throw new Error('Ed25519: Invalid public key.');
|
|
337
|
-
}
|
|
338
|
-
x25519PublicKeyBytes = (0, ed25519_1.edwardsToMontgomeryPub)(ed25519PublicKeyBytes);
|
|
339
|
-
x25519PublicKey = {
|
|
340
|
-
kty: 'OKP',
|
|
341
|
-
crv: 'X25519',
|
|
342
|
-
x: common_1.Convert.uint8Array(x25519PublicKeyBytes).toBase64Url(),
|
|
343
|
-
};
|
|
344
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
345
|
-
_c = x25519PublicKey;
|
|
346
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: x25519PublicKey })];
|
|
347
|
-
case 3:
|
|
348
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
349
|
-
_c.kid = _d.sent();
|
|
350
|
-
return [2 /*return*/, x25519PublicKey];
|
|
351
|
-
}
|
|
352
|
-
});
|
|
353
|
-
});
|
|
354
|
-
};
|
|
355
|
-
/**
|
|
356
|
-
* Generates an Ed25519 private key in JSON Web Key (JWK) format.
|
|
357
|
-
*
|
|
358
|
-
* @remarks
|
|
359
|
-
* This method creates a new private key suitable for use with the Curve25519 elliptic curve in
|
|
360
|
-
* Twisted Edwards form. The key generation process involves using cryptographically secure
|
|
361
|
-
* random number generation to ensure the uniqueness and security of the key. The resulting
|
|
362
|
-
* private key adheres to the JWK format making it compatible with common cryptographic
|
|
363
|
-
* standards and easy to use in various cryptographic processes.
|
|
364
|
-
*
|
|
365
|
-
* The generated private key in JWK format includes the following components:
|
|
366
|
-
* - `kty`: Key Type, set to 'OKP' for Octet Key Pair.
|
|
367
|
-
* - `crv`: Curve Name, set to 'Ed25519'.
|
|
368
|
-
* - `d`: The private key component, base64url-encoded.
|
|
369
|
-
* - `x`: The derived public key, base64url-encoded.
|
|
370
|
-
*
|
|
371
|
-
* @example
|
|
372
|
-
* ```ts
|
|
373
|
-
* const privateKey = await Ed25519.generateKey();
|
|
374
|
-
* ```
|
|
375
|
-
*
|
|
376
|
-
* @returns A Promise that resolves to the generated private key in JWK format.
|
|
377
|
-
*/
|
|
378
|
-
Ed25519.generateKey = function () {
|
|
379
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
380
|
-
var privateKeyBytes, privateKey, _a;
|
|
381
|
-
return __generator(this, function (_b) {
|
|
382
|
-
switch (_b.label) {
|
|
383
|
-
case 0:
|
|
384
|
-
privateKeyBytes = ed25519_1.ed25519.utils.randomPrivateKey();
|
|
385
|
-
return [4 /*yield*/, Ed25519.bytesToPrivateKey({ privateKeyBytes: privateKeyBytes })];
|
|
386
|
-
case 1:
|
|
387
|
-
privateKey = _b.sent();
|
|
388
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
389
|
-
_a = privateKey;
|
|
390
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: privateKey })];
|
|
391
|
-
case 2:
|
|
392
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
393
|
-
_a.kid = _b.sent();
|
|
394
|
-
return [2 /*return*/, privateKey];
|
|
395
|
-
}
|
|
396
|
-
});
|
|
397
|
-
});
|
|
398
|
-
};
|
|
399
|
-
/**
|
|
400
|
-
* Retrieves the public key properties from a given private key in JWK format.
|
|
401
|
-
*
|
|
402
|
-
* @remarks
|
|
403
|
-
* This method extracts the public key portion from an Ed25519 private key in JWK format. It does
|
|
404
|
-
* so by removing the private key property 'd' and making a shallow copy, effectively yielding the
|
|
405
|
-
* public key. The method sets the 'kid' (key ID) property using the JWK thumbprint if it is not
|
|
406
|
-
* already defined. This approach is used under the assumption that a private key in JWK format
|
|
407
|
-
* always contains the corresponding public key properties.
|
|
408
|
-
*
|
|
409
|
-
* Note: This method offers a significant performance advantage, being about 100 times faster
|
|
410
|
-
* than `computePublicKey()`. However, it does not mathematically validate the private key, nor
|
|
411
|
-
* does it derive the public key from the private key. It simply extracts existing public key
|
|
412
|
-
* properties from the private key object. This makes it suitable for scenarios where speed is
|
|
413
|
-
* critical and the private key's integrity is already assured.
|
|
414
|
-
*
|
|
415
|
-
* @example
|
|
416
|
-
* ```ts
|
|
417
|
-
* const privateKey = { ... }; // A Jwk object representing an Ed25519 private key
|
|
418
|
-
* const publicKey = await Ed25519.getPublicKey({ key: privateKey });
|
|
419
|
-
* ```
|
|
420
|
-
*
|
|
421
|
-
* @param params - The parameters for retrieving the public key properties.
|
|
422
|
-
* @param params.key - The private key in JWK format.
|
|
423
|
-
*
|
|
424
|
-
* @returns A Promise that resolves to the public key in JWK format.
|
|
425
|
-
*/
|
|
426
|
-
Ed25519.getPublicKey = function (_a) {
|
|
427
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
428
|
-
var d, publicKey, _c, _d;
|
|
429
|
-
var _e;
|
|
430
|
-
var key = _b.key;
|
|
431
|
-
return __generator(this, function (_f) {
|
|
432
|
-
switch (_f.label) {
|
|
433
|
-
case 0:
|
|
434
|
-
// Verify the provided JWK represents an octet key pair (OKP) Ed25519 private key.
|
|
435
|
-
if (!((0, jwk_js_1.isOkpPrivateJwk)(key) && key.crv === 'Ed25519')) {
|
|
436
|
-
throw new Error("Ed25519: The provided key is not an Ed25519 private JWK.");
|
|
437
|
-
}
|
|
438
|
-
d = key.d, publicKey = __rest(key, ["d"]);
|
|
439
|
-
if (!((_e =
|
|
440
|
-
// If the key ID is undefined, set it to the JWK thumbprint.
|
|
441
|
-
publicKey.kid) !== null && _e !== void 0)) return [3 /*break*/, 1];
|
|
442
|
-
_c = _e;
|
|
443
|
-
return [3 /*break*/, 3];
|
|
444
|
-
case 1:
|
|
445
|
-
// If the key ID is undefined, set it to the JWK thumbprint.
|
|
446
|
-
_d = publicKey;
|
|
447
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: publicKey })];
|
|
448
|
-
case 2:
|
|
449
|
-
_c = (_d.kid = _f.sent());
|
|
450
|
-
_f.label = 3;
|
|
451
|
-
case 3:
|
|
452
|
-
// If the key ID is undefined, set it to the JWK thumbprint.
|
|
453
|
-
_c;
|
|
454
|
-
return [2 /*return*/, publicKey];
|
|
455
|
-
}
|
|
456
|
-
});
|
|
457
|
-
});
|
|
458
|
-
};
|
|
459
|
-
/**
|
|
460
|
-
* Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
|
|
461
|
-
*
|
|
462
|
-
* @remarks
|
|
463
|
-
* This method accepts a private key in JWK format and extracts its raw byte representation.
|
|
464
|
-
*
|
|
465
|
-
* This method accepts a public key in JWK format and converts it into its raw binary
|
|
466
|
-
* form. The conversion process involves decoding the 'd' parameter of the JWK
|
|
467
|
-
* from base64url format into a byte array.
|
|
468
|
-
*
|
|
469
|
-
* @example
|
|
470
|
-
* ```ts
|
|
471
|
-
* const privateKey = { ... }; // An Ed25519 private key in JWK format
|
|
472
|
-
* const privateKeyBytes = await Ed25519.privateKeyToBytes({ privateKey });
|
|
473
|
-
* ```
|
|
474
|
-
*
|
|
475
|
-
* @param params - The parameters for the private key conversion.
|
|
476
|
-
* @param params.privateKey - The private key in JWK format.
|
|
477
|
-
*
|
|
478
|
-
* @returns A Promise that resolves to the private key as a Uint8Array.
|
|
479
|
-
*/
|
|
480
|
-
Ed25519.privateKeyToBytes = function (_a) {
|
|
481
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
482
|
-
var privateKeyBytes;
|
|
483
|
-
var privateKey = _b.privateKey;
|
|
484
|
-
return __generator(this, function (_c) {
|
|
485
|
-
// Verify the provided JWK represents a valid OKP private key.
|
|
486
|
-
if (!(0, jwk_js_1.isOkpPrivateJwk)(privateKey)) {
|
|
487
|
-
throw new Error("Ed25519: The provided key is not a valid OKP private key.");
|
|
488
|
-
}
|
|
489
|
-
privateKeyBytes = common_1.Convert.base64Url(privateKey.d).toUint8Array();
|
|
490
|
-
return [2 /*return*/, privateKeyBytes];
|
|
491
|
-
});
|
|
492
|
-
});
|
|
493
|
-
};
|
|
494
|
-
/**
|
|
495
|
-
* Converts a public key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
|
|
496
|
-
*
|
|
497
|
-
* @remarks
|
|
498
|
-
* This method accepts a public key in JWK format and converts it into its raw binary form.
|
|
499
|
-
* The conversion process involves decoding the 'x' parameter of the JWK (which represent the
|
|
500
|
-
* x coordinate of the elliptic curve point) from base64url format into a byte array.
|
|
501
|
-
*
|
|
502
|
-
* @example
|
|
503
|
-
* ```ts
|
|
504
|
-
* const publicKey = { ... }; // An Ed25519 public key in JWK format
|
|
505
|
-
* const publicKeyBytes = await Ed25519.publicKeyToBytes({ publicKey });
|
|
506
|
-
* ```
|
|
507
|
-
*
|
|
508
|
-
* @param params - The parameters for the public key conversion.
|
|
509
|
-
* @param params.publicKey - The public key in JWK format.
|
|
510
|
-
*
|
|
511
|
-
* @returns A Promise that resolves to the public key as a Uint8Array.
|
|
512
|
-
*/
|
|
513
|
-
Ed25519.publicKeyToBytes = function (_a) {
|
|
514
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
515
|
-
var publicKeyBytes;
|
|
516
|
-
var publicKey = _b.publicKey;
|
|
517
|
-
return __generator(this, function (_c) {
|
|
518
|
-
// Verify the provided JWK represents a valid OKP public key.
|
|
519
|
-
if (!(0, jwk_js_1.isOkpPublicJwk)(publicKey)) {
|
|
520
|
-
throw new Error("Ed25519: The provided key is not a valid OKP public key.");
|
|
521
|
-
}
|
|
522
|
-
publicKeyBytes = common_1.Convert.base64Url(publicKey.x).toUint8Array();
|
|
523
|
-
return [2 /*return*/, publicKeyBytes];
|
|
524
|
-
});
|
|
525
|
-
});
|
|
526
|
-
};
|
|
527
|
-
/**
|
|
528
|
-
* Generates an RFC8032-compliant EdDSA signature of given data using an Ed25519 private key.
|
|
529
|
-
*
|
|
530
|
-
* @remarks
|
|
531
|
-
* This method signs the provided data with a specified private key using the EdDSA
|
|
532
|
-
* (Edwards-curve Digital Signature Algorithm) as defined in RFC8032. It
|
|
533
|
-
* involves converting the private key from JWK format to a byte array and then employing
|
|
534
|
-
* the Ed25519 algorithm to sign the data. The output is a digital signature in the form
|
|
535
|
-
* of a Uint8Array, uniquely corresponding to both the data and the private key used for
|
|
536
|
-
* signing.
|
|
537
|
-
*
|
|
538
|
-
* @example
|
|
539
|
-
* ```ts
|
|
540
|
-
* const data = new TextEncoder().encode('Messsage'); // Data to be signed
|
|
541
|
-
* const privateKey = { ... }; // A Jwk object representing an Ed25519 private key
|
|
542
|
-
* const signature = await Ed25519.sign({ key: privateKey, data });
|
|
543
|
-
* ```
|
|
544
|
-
*
|
|
545
|
-
* @param params - The parameters for the signing operation.
|
|
546
|
-
* @param params.key - The private key to use for signing, represented in JWK format.
|
|
547
|
-
* @param params.data - The data to sign, represented as a Uint8Array.
|
|
548
|
-
*
|
|
549
|
-
* @returns A Promise that resolves to the signature as a Uint8Array.
|
|
550
|
-
*/
|
|
551
|
-
Ed25519.sign = function (_a) {
|
|
552
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
553
|
-
var privateKeyBytes, signature;
|
|
554
|
-
var key = _b.key, data = _b.data;
|
|
555
|
-
return __generator(this, function (_c) {
|
|
556
|
-
switch (_c.label) {
|
|
557
|
-
case 0: return [4 /*yield*/, Ed25519.privateKeyToBytes({ privateKey: key })];
|
|
558
|
-
case 1:
|
|
559
|
-
privateKeyBytes = _c.sent();
|
|
560
|
-
signature = ed25519_1.ed25519.sign(data, privateKeyBytes);
|
|
561
|
-
return [2 /*return*/, signature];
|
|
562
|
-
}
|
|
563
|
-
});
|
|
564
|
-
});
|
|
565
|
-
};
|
|
566
|
-
/**
|
|
567
|
-
* Validates a given public key to confirm its mathematical correctness on the Edwards curve.
|
|
568
|
-
*
|
|
569
|
-
* @remarks
|
|
570
|
-
* This method decodes the Edwards points from the key bytes and asserts their validity on the
|
|
571
|
-
* Curve25519 curve in Twisted Edwards form. If the points are not valid, the method returns
|
|
572
|
-
* false. If the points are valid, the method returns true.
|
|
573
|
-
*
|
|
574
|
-
* Note that this validation strictly pertains to the key's format and numerical validity; it does
|
|
575
|
-
* not assess whether the key corresponds to a known entity or its security status (e.g., whether
|
|
576
|
-
* it has been compromised).
|
|
577
|
-
*
|
|
578
|
-
* @example
|
|
579
|
-
* ```ts
|
|
580
|
-
* const publicKeyBytes = new Uint8Array([...]); // A public key in byte format
|
|
581
|
-
* const isValid = await Ed25519.validatePublicKey({ publicKeyBytes });
|
|
582
|
-
* console.log(isValid); // true if the key is valid on the Edwards curve, false otherwise
|
|
583
|
-
* ```
|
|
584
|
-
*
|
|
585
|
-
* @param params - The parameters for the public key validation.
|
|
586
|
-
* @param params.publicKeyBytes - The public key to validate, represented as a Uint8Array.
|
|
587
|
-
*
|
|
588
|
-
* @returns A Promise that resolves to a boolean indicating whether the key
|
|
589
|
-
* corresponds to a valid point on the Edwards curve.
|
|
590
|
-
*/
|
|
591
|
-
Ed25519.validatePublicKey = function (_a) {
|
|
592
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
593
|
-
var point;
|
|
594
|
-
var publicKeyBytes = _b.publicKeyBytes;
|
|
595
|
-
return __generator(this, function (_c) {
|
|
596
|
-
try {
|
|
597
|
-
point = ed25519_1.ed25519.ExtendedPoint.fromHex(publicKeyBytes);
|
|
598
|
-
// Check if points are on the Twisted Edwards curve.
|
|
599
|
-
point.assertValidity();
|
|
600
|
-
}
|
|
601
|
-
catch (error) {
|
|
602
|
-
return [2 /*return*/, false];
|
|
603
|
-
}
|
|
604
|
-
return [2 /*return*/, true];
|
|
605
|
-
});
|
|
606
|
-
});
|
|
607
|
-
};
|
|
608
|
-
/**
|
|
609
|
-
* Verifies an RFC8032-compliant EdDSA signature against given data using an Ed25519 public key.
|
|
610
|
-
*
|
|
611
|
-
* @remarks
|
|
612
|
-
* This method validates a digital signature to ensure its authenticity and integrity.
|
|
613
|
-
* It uses the EdDSA (Edwards-curve Digital Signature Algorithm) as specified in RFC8032.
|
|
614
|
-
* The verification process involves converting the public key from JWK format to a raw
|
|
615
|
-
* byte array and using the Ed25519 algorithm to validate the signature against the provided data.
|
|
616
|
-
*
|
|
617
|
-
* @example
|
|
618
|
-
* ```ts
|
|
619
|
-
* const data = new TextEncoder().encode('Messsage'); // Data that was signed
|
|
620
|
-
* const publicKey = { ... }; // A Jwk object representing an Ed25519 public key
|
|
621
|
-
* const signature = new Uint8Array([...]); // Signature to verify
|
|
622
|
-
* const isValid = await Ed25519.verify({ key: publicKey, signature, data });
|
|
623
|
-
* console.log(isValid); // true if the signature is valid, false otherwise
|
|
624
|
-
* ```
|
|
625
|
-
*
|
|
626
|
-
* @param params - The parameters for the signature verification.
|
|
627
|
-
* @param params.key - The public key in JWK format used for verification.
|
|
628
|
-
* @param params.signature - The signature to verify, represented as a Uint8Array.
|
|
629
|
-
* @param params.data - The data that was signed, represented as a Uint8Array.
|
|
630
|
-
*
|
|
631
|
-
* @returns A Promise that resolves to a boolean indicating whether the signature is valid.
|
|
632
|
-
*/
|
|
633
|
-
Ed25519.verify = function (_a) {
|
|
634
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
635
|
-
var publicKeyBytes, isValid;
|
|
636
|
-
var key = _b.key, signature = _b.signature, data = _b.data;
|
|
637
|
-
return __generator(this, function (_c) {
|
|
638
|
-
switch (_c.label) {
|
|
639
|
-
case 0: return [4 /*yield*/, Ed25519.publicKeyToBytes({ publicKey: key })];
|
|
640
|
-
case 1:
|
|
641
|
-
publicKeyBytes = _c.sent();
|
|
642
|
-
isValid = ed25519_1.ed25519.verify(signature, data, publicKeyBytes);
|
|
643
|
-
return [2 /*return*/, isValid];
|
|
644
|
-
}
|
|
645
|
-
});
|
|
646
|
-
});
|
|
647
|
-
};
|
|
648
|
-
return Ed25519;
|
|
649
|
-
}());
|
|
650
|
-
exports.Ed25519 = Ed25519;
|
|
651
|
-
//# sourceMappingURL=ed25519.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ed25519.js","sourceRoot":"","sources":["../../../src/primitives/ed25519.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wCAAwC;AACxC,iDAAyG;AAKzG,yCAAuF;AAEvF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH;IAAA;IA0eA,CAAC;IAzeC;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACiB,yBAAiB,GAArC;4DAAsC,EAErC;;gBAFuC,eAAe,qBAAA;;;;wBAI/C,cAAc,GAAI,iBAAO,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC;wBAGxD,UAAU,GAAQ;4BACtB,GAAG,EAAG,SAAS;4BACf,CAAC,EAAK,gBAAO,CAAC,UAAU,CAAC,eAAe,CAAC,CAAC,WAAW,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