@dwn-protocol/id-sdk 0.2.5 → 0.2.6
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/package.json +2 -3
- package/src/agent/app-data-store.ts +0 -365
- package/src/agent/did-manager.ts +0 -393
- package/src/agent/dwn-manager.ts +0 -548
- package/src/agent/identity-manager.ts +0 -165
- package/src/agent/index.ts +0 -19
- package/src/agent/json-rpc.ts +0 -107
- package/src/agent/key-manager.ts +0 -302
- package/src/agent/kms-local.ts +0 -412
- package/src/agent/outbox.ts +0 -128
- package/src/agent/rpc-client.ts +0 -223
- package/src/agent/store-managed-did.ts +0 -295
- package/src/agent/store-managed-identity.ts +0 -243
- package/src/agent/store-managed-key.ts +0 -754
- package/src/agent/sync-manager.ts +0 -631
- package/src/agent/test-managed-agent.ts +0 -299
- package/src/agent/types/agent.ts +0 -145
- package/src/agent/types/managed-key.ts +0 -442
- package/src/agent/utils.ts +0 -190
- package/src/common/convert.ts +0 -424
- package/src/common/index.ts +0 -9
- package/src/common/multicodec.ts +0 -176
- package/src/common/object.ts +0 -43
- package/src/common/stores.ts +0 -125
- package/src/common/stream-node.ts +0 -381
- package/src/common/stream.ts +0 -406
- package/src/common/type-utils.ts +0 -117
- package/src/common/types.ts +0 -48
- package/src/credentials/credential-bbs.ts +0 -419
- package/src/credentials/credential.ts +0 -324
- package/src/credentials/index.ts +0 -5
- package/src/credentials/presentation.ts +0 -182
- package/src/credentials/status-list.ts +0 -365
- package/src/credentials/utils.ts +0 -58
- package/src/credentials/validators.ts +0 -52
- package/src/crypto/algorithms-api/aes/base.ts +0 -49
- package/src/crypto/algorithms-api/aes/ctr.ts +0 -51
- package/src/crypto/algorithms-api/aes/index.ts +0 -2
- package/src/crypto/algorithms-api/crypto-algorithm.ts +0 -127
- package/src/crypto/algorithms-api/crypto-key.ts +0 -56
- package/src/crypto/algorithms-api/ec/base.ts +0 -39
- package/src/crypto/algorithms-api/ec/ecdh.ts +0 -53
- package/src/crypto/algorithms-api/ec/ecdsa.ts +0 -37
- package/src/crypto/algorithms-api/ec/eddsa.ts +0 -30
- package/src/crypto/algorithms-api/ec/index.ts +0 -4
- package/src/crypto/algorithms-api/errors.ts +0 -29
- package/src/crypto/algorithms-api/index.ts +0 -6
- package/src/crypto/algorithms-api/pbkdf/index.ts +0 -1
- package/src/crypto/algorithms-api/pbkdf/pbkdf2.ts +0 -91
- package/src/crypto/crypto-algorithms/aes-ctr.ts +0 -70
- package/src/crypto/crypto-algorithms/bbs.ts +0 -110
- package/src/crypto/crypto-algorithms/ecdh.ts +0 -115
- package/src/crypto/crypto-algorithms/ecdsa.ts +0 -111
- package/src/crypto/crypto-algorithms/eddsa.ts +0 -110
- package/src/crypto/crypto-algorithms/index.ts +0 -6
- package/src/crypto/crypto-algorithms/pbkdf2.ts +0 -54
- package/src/crypto/crypto-primitives/aes-ctr.ts +0 -131
- package/src/crypto/crypto-primitives/aes-gcm.ts +0 -138
- package/src/crypto/crypto-primitives/bbs.ts +0 -183
- package/src/crypto/crypto-primitives/concat-kdf.ts +0 -207
- package/src/crypto/crypto-primitives/ed25519.ts +0 -201
- package/src/crypto/crypto-primitives/index.ts +0 -10
- package/src/crypto/crypto-primitives/pbkdf2.ts +0 -78
- package/src/crypto/crypto-primitives/secp256k1.ts +0 -322
- package/src/crypto/crypto-primitives/x25519.ts +0 -101
- package/src/crypto/crypto-primitives/xchacha20-poly1305.ts +0 -46
- package/src/crypto/crypto-primitives/xchacha20.ts +0 -34
- package/src/crypto/index.ts +0 -8
- package/src/crypto/jose.ts +0 -948
- package/src/crypto/types/crypto-key.ts +0 -4
- package/src/crypto/types/iddwn-crypto.ts +0 -119
- package/src/crypto/utils.ts +0 -200
- package/src/did-api.ts +0 -72
- package/src/dids/dht.ts +0 -412
- package/src/dids/did-dht.ts +0 -436
- package/src/dids/did-ion.ts +0 -613
- package/src/dids/did-key.ts +0 -791
- package/src/dids/did-resolver.ts +0 -107
- package/src/dids/index.ts +0 -9
- package/src/dids/resolver-cache-level.ts +0 -82
- package/src/dids/resolver-cache-noop.ts +0 -25
- package/src/dids/types.ts +0 -278
- package/src/dids/utils.ts +0 -129
- package/src/dwn-api.ts +0 -584
- package/src/iddwn.ts +0 -241
- package/src/identity-agent/index.ts +0 -270
- package/src/index.ts +0 -26
- package/src/interfaces/metadata.ts +0 -163
- package/src/interfaces/queue.ts +0 -108
- package/src/interfaces/services.ts +0 -122
- package/src/interfaces/transactions.ts +0 -220
- package/src/protocol.ts +0 -68
- package/src/proxy-agent/index.ts +0 -255
- package/src/record.ts +0 -521
- package/src/service-options.ts +0 -62
- package/src/typings/decentralized-identity__ion-pow-sdk.d.ts +0 -7
- package/src/user-agent/index.ts +0 -295
- package/src/utils.ts +0 -29
- package/src/vc-api.ts +0 -505
|
@@ -1,322 +0,0 @@
|
|
|
1
|
-
import type { BytesKeyPair } from '../types/crypto-key.js';
|
|
2
|
-
|
|
3
|
-
import { sha256 } from '@noble/hashes/sha256';
|
|
4
|
-
import { secp256k1 } from '@noble/curves/secp256k1';
|
|
5
|
-
import { numberToBytesBE } from '@noble/curves/abstract/utils';
|
|
6
|
-
|
|
7
|
-
export type HashFunction = (data: Uint8Array) => Uint8Array;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* The `Secp256k1` class provides an interface for generating secp256k1 key pairs,
|
|
11
|
-
* computing public keys from private keys, generating shaerd secrets, and
|
|
12
|
-
* signing and verifying messages.
|
|
13
|
-
*
|
|
14
|
-
* The class uses the '@noble/secp256k1' package for the cryptographic operations,
|
|
15
|
-
* and the '@noble/hashes/sha256' package for generating the hash digests needed
|
|
16
|
-
* for the signing and verification operations.
|
|
17
|
-
*
|
|
18
|
-
* The methods of this class are all asynchronous and return Promises. They all use
|
|
19
|
-
* the Uint8Array type for keys, signatures, and data, providing a consistent
|
|
20
|
-
* interface for working with binary data.
|
|
21
|
-
*
|
|
22
|
-
* Example usage:
|
|
23
|
-
*
|
|
24
|
-
* ```ts
|
|
25
|
-
* const keyPair = await Secp256k1.generateKeyPair();
|
|
26
|
-
* const message = new TextEncoder().encode('Hello, world!');
|
|
27
|
-
* const signature = await Secp256k1.sign({
|
|
28
|
-
* algorithm: { hash: 'SHA-256' },
|
|
29
|
-
* key: keyPair.privateKey,
|
|
30
|
-
* data: message
|
|
31
|
-
* });
|
|
32
|
-
* const isValid = await Secp256k1.verify({
|
|
33
|
-
* algorithm: { hash: 'SHA-256' },
|
|
34
|
-
* key: keyPair.publicKey,
|
|
35
|
-
* signature,
|
|
36
|
-
* data: message
|
|
37
|
-
* });
|
|
38
|
-
* console.log(isValid); // true
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
export class Secp256k1 {
|
|
42
|
-
/**
|
|
43
|
-
* A private static field containing a map of hash algorithm names to their
|
|
44
|
-
* corresponding hash functions. The map is used in the 'sign' and 'verify'
|
|
45
|
-
* methods to get the specified hash function.
|
|
46
|
-
*/
|
|
47
|
-
private static hashAlgorithms: Record<string, HashFunction> = {
|
|
48
|
-
'SHA-256': sha256
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Converts a public key between its compressed and uncompressed forms.
|
|
53
|
-
*
|
|
54
|
-
* Given a public key, this method can either compress or decompress it
|
|
55
|
-
* depending on the provided `compressedPublicKey` option. The conversion
|
|
56
|
-
* process involves decoding the Weierstrass points from the key bytes
|
|
57
|
-
* and then returning the key in the desired format.
|
|
58
|
-
*
|
|
59
|
-
* This is useful in scenarios where space is a consideration or when
|
|
60
|
-
* interfacing with systems that expect a specific public key format.
|
|
61
|
-
*
|
|
62
|
-
* @param options - The options for the public key conversion.
|
|
63
|
-
* @param options.publicKey - The original public key, represented as a Uint8Array.
|
|
64
|
-
* @param options.compressedPublicKey - A boolean indicating whether the output
|
|
65
|
-
* should be in compressed form. If true, the
|
|
66
|
-
* method returns the compressed form of the
|
|
67
|
-
* provided public key. If false, it returns
|
|
68
|
-
* the uncompressed form.
|
|
69
|
-
*
|
|
70
|
-
* @returns A Promise that resolves to the converted public key as a Uint8Array.
|
|
71
|
-
*/
|
|
72
|
-
public static async convertPublicKey(options: {
|
|
73
|
-
publicKey: Uint8Array,
|
|
74
|
-
compressedPublicKey: boolean
|
|
75
|
-
}): Promise<Uint8Array> {
|
|
76
|
-
let { publicKey, compressedPublicKey } = options;
|
|
77
|
-
|
|
78
|
-
// Decode Weierstrass points from key bytes.
|
|
79
|
-
const point = secp256k1.ProjectivePoint.fromHex(publicKey);
|
|
80
|
-
|
|
81
|
-
// Return either the compressed or uncompressed form of hte public key.
|
|
82
|
-
return point.toRawBytes(compressedPublicKey);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Generates a secp256k1 key pair.
|
|
87
|
-
*
|
|
88
|
-
* @param options - Optional parameters for the key generation.
|
|
89
|
-
* @param options.compressedPublicKey - If true, generates a compressed public key. Defaults to true.
|
|
90
|
-
* @returns A Promise that resolves to an object containing the private and public keys as Uint8Array.
|
|
91
|
-
*/
|
|
92
|
-
public static async generateKeyPair(options?: {
|
|
93
|
-
compressedPublicKey?: boolean
|
|
94
|
-
}): Promise<BytesKeyPair> {
|
|
95
|
-
let { compressedPublicKey } = options ?? { };
|
|
96
|
-
|
|
97
|
-
compressedPublicKey ??= true; // Default to compressed public key, matching the default of @noble/secp256k1.
|
|
98
|
-
|
|
99
|
-
// Generate the private key and compute its public key.
|
|
100
|
-
const privateKey = secp256k1.utils.randomPrivateKey();
|
|
101
|
-
const publicKey = secp256k1.getPublicKey(privateKey, compressedPublicKey);
|
|
102
|
-
|
|
103
|
-
const keyPair = {
|
|
104
|
-
privateKey : privateKey,
|
|
105
|
-
publicKey : publicKey
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
return keyPair;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Returns the elliptic curve points (x and y coordinates) for a given secp256k1 key.
|
|
113
|
-
*
|
|
114
|
-
* In the case of a private key, the public key is first computed from the private key,
|
|
115
|
-
* then the x and y coordinates of the public key point on the elliptic curve are returned.
|
|
116
|
-
*
|
|
117
|
-
* In the case of a public key, the x and y coordinates of the key point on the elliptic
|
|
118
|
-
* curve are returned directly.
|
|
119
|
-
*
|
|
120
|
-
* The returned coordinates can be used to perform various operations on the elliptic curve,
|
|
121
|
-
* such as addition and multiplication of points, which can be used in various cryptographic
|
|
122
|
-
* schemes and protocols.
|
|
123
|
-
*
|
|
124
|
-
* @param options - The options for the operation.
|
|
125
|
-
* @param options.key - The key for which to get the elliptic curve points.
|
|
126
|
-
* Can be either a private key or a public key.
|
|
127
|
-
* The key should be passed as a Uint8Array.
|
|
128
|
-
* @returns A Promise that resolves to an object with properties 'x' and 'y',
|
|
129
|
-
* each being a Uint8Array representing the x and y coordinates of the key point on the elliptic curve.
|
|
130
|
-
*/
|
|
131
|
-
public static async getCurvePoints(options: {
|
|
132
|
-
key: Uint8Array
|
|
133
|
-
}): Promise<{ x: Uint8Array, y: Uint8Array }> {
|
|
134
|
-
let { key } = options;
|
|
135
|
-
|
|
136
|
-
// If key is a private key, first compute the public key.
|
|
137
|
-
if (key.byteLength === 32) {
|
|
138
|
-
key = await Secp256k1.getPublicKey({ privateKey: key });
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Decode Weierstrass points from key bytes.
|
|
142
|
-
const point = secp256k1.ProjectivePoint.fromHex(key);
|
|
143
|
-
|
|
144
|
-
// Get x- and y-coordinate values and convert to Uint8Array.
|
|
145
|
-
const x = numberToBytesBE(point.x, 32);
|
|
146
|
-
const y = numberToBytesBE(point.y, 32);
|
|
147
|
-
|
|
148
|
-
return { x, y };
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Computes the public key from a given private key.
|
|
153
|
-
* If compressedPublicKey=true then the output is a 33-byte public key.
|
|
154
|
-
* If compressedPublicKey=false then the output is a 65-byte public key.
|
|
155
|
-
*
|
|
156
|
-
* @param options - The options for the public key computation.
|
|
157
|
-
* @param options.privateKey - The 32-byte private key from which to compute the public key.
|
|
158
|
-
* @param options.compressedPublicKey - If true, returns a compressed public key. Defaults to true.
|
|
159
|
-
* @returns A Promise that resolves to the computed public key as a Uint8Array.
|
|
160
|
-
*/
|
|
161
|
-
public static async getPublicKey(options: {
|
|
162
|
-
privateKey: Uint8Array,
|
|
163
|
-
compressedPublicKey?: boolean
|
|
164
|
-
}): Promise<Uint8Array> {
|
|
165
|
-
let { privateKey, compressedPublicKey } = options;
|
|
166
|
-
|
|
167
|
-
compressedPublicKey ??= true; // Default to compressed public key, matching the default of @noble/secp256k1.
|
|
168
|
-
|
|
169
|
-
// Compute public key.
|
|
170
|
-
const publicKey = secp256k1.getPublicKey(privateKey, compressedPublicKey);
|
|
171
|
-
|
|
172
|
-
return publicKey;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Generates a RFC6090 ECDH shared secret given the private key of one party
|
|
177
|
-
* and the public key another party.
|
|
178
|
-
*
|
|
179
|
-
* Note: When performing Elliptic Curve Diffie-Hellman (ECDH) key agreement,
|
|
180
|
-
* the resulting shared secret is a point on the elliptic curve, which
|
|
181
|
-
* consists of an x-coordinate and a y-coordinate. With a 256-bit curve like
|
|
182
|
-
* secp256k1, each of these coordinates is 32 bytes (256 bits) long. However,
|
|
183
|
-
* in the ECDH process, it's standard practice to use only the x-coordinate
|
|
184
|
-
* of the shared secret point as the resulting shared key. This is because
|
|
185
|
-
* the y-coordinate does not add to the entropy of the key, and both parties
|
|
186
|
-
* can independently compute the x-coordinate, so using just the x-coordinate
|
|
187
|
-
* simplifies matters.
|
|
188
|
-
*/
|
|
189
|
-
public static async sharedSecret(options: {
|
|
190
|
-
compressedSecret?: boolean,
|
|
191
|
-
privateKey: Uint8Array,
|
|
192
|
-
publicKey: Uint8Array
|
|
193
|
-
}): Promise<Uint8Array> {
|
|
194
|
-
let { privateKey, publicKey } = options;
|
|
195
|
-
|
|
196
|
-
// Compute the shared secret between the public and private keys.
|
|
197
|
-
const sharedSecret = secp256k1.getSharedSecret(privateKey, publicKey);
|
|
198
|
-
|
|
199
|
-
// Remove the leading byte that indicates the sign of the y-coordinate
|
|
200
|
-
// of the point on the elliptic curve. See note above.
|
|
201
|
-
return sharedSecret.slice(1);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Generates a RFC6979 ECDSA signature of given data with a given private key and hash algorithm.
|
|
206
|
-
*
|
|
207
|
-
* @param options - The options for the signing operation.
|
|
208
|
-
* @param options.data - The data to sign.
|
|
209
|
-
* @param options.hash - The hash algorithm to use to generate a digest of the data.
|
|
210
|
-
* @param options.key - The private key to use for signing.
|
|
211
|
-
* @returns A Promise that resolves to the signature as a Uint8Array.
|
|
212
|
-
*/
|
|
213
|
-
public static async sign(options: {
|
|
214
|
-
data: Uint8Array,
|
|
215
|
-
hash: string,
|
|
216
|
-
key: Uint8Array
|
|
217
|
-
}): Promise<Uint8Array> {
|
|
218
|
-
const { data, hash, key } = options;
|
|
219
|
-
|
|
220
|
-
// Generate a digest of the data using the specified hash function.
|
|
221
|
-
const hashFunction = this.hashAlgorithms[hash];
|
|
222
|
-
const digest = hashFunction(data);
|
|
223
|
-
|
|
224
|
-
// Signature operation returns a Signature instance with { r, s, recovery } properties.
|
|
225
|
-
const signatureObject = secp256k1.sign(digest, key);
|
|
226
|
-
|
|
227
|
-
// Convert Signature object to Uint8Array.
|
|
228
|
-
const signature = signatureObject.toCompactRawBytes();
|
|
229
|
-
|
|
230
|
-
return signature;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Validates a given private key to ensure that it's a valid 32-byte number
|
|
235
|
-
* that is less than the secp256k1 curve's order.
|
|
236
|
-
*
|
|
237
|
-
* This method checks the byte length of the key and its numerical validity
|
|
238
|
-
* according to the secp256k1 curve's parameters. It doesn't verify whether
|
|
239
|
-
* the key corresponds to a known or authorized entity or whether it has
|
|
240
|
-
* been compromised.
|
|
241
|
-
*
|
|
242
|
-
* @param options - The options for the key validation.
|
|
243
|
-
* @param options.key - The private key to validate, represented as a Uint8Array.
|
|
244
|
-
* @returns A Promise that resolves to a boolean indicating whether the private
|
|
245
|
-
* key is a valid 32-byte number less than the secp256k1 curve's order.
|
|
246
|
-
*/
|
|
247
|
-
public static async validatePrivateKey(options: {
|
|
248
|
-
key: Uint8Array
|
|
249
|
-
}): Promise<boolean> {
|
|
250
|
-
const { key } = options;
|
|
251
|
-
|
|
252
|
-
return secp256k1.utils.isValidPrivateKey(key);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Validates a given public key to ensure that it corresponds to a
|
|
257
|
-
* valid point on the secp256k1 elliptic curve.
|
|
258
|
-
*
|
|
259
|
-
* This method decodes the Weierstrass points from the key bytes and
|
|
260
|
-
* asserts their validity on the curve. If the points are not valid,
|
|
261
|
-
* the method returns false. If the points are valid, the method
|
|
262
|
-
* returns true.
|
|
263
|
-
*
|
|
264
|
-
* Note: This method does not check whether the key corresponds to a
|
|
265
|
-
* known or authorized entity, or whether it has been compromised.
|
|
266
|
-
* It only checks the mathematical validity of the key.
|
|
267
|
-
*
|
|
268
|
-
* @param options - The options for the key validation.
|
|
269
|
-
* @param options.key - The key to validate, represented as a Uint8Array.
|
|
270
|
-
* @returns A Promise that resolves to a boolean indicating whether the key
|
|
271
|
-
* corresponds to a valid point on the secp256k1 elliptic curve.
|
|
272
|
-
*/
|
|
273
|
-
public static async validatePublicKey(options: {
|
|
274
|
-
key: Uint8Array
|
|
275
|
-
}): Promise<boolean> {
|
|
276
|
-
const { key } = options;
|
|
277
|
-
|
|
278
|
-
try {
|
|
279
|
-
// Decode Weierstrass points from key bytes.
|
|
280
|
-
const point = secp256k1.ProjectivePoint.fromHex(key);
|
|
281
|
-
|
|
282
|
-
// Check if points are on the Short Weierstrass curve.
|
|
283
|
-
point.assertValidity();
|
|
284
|
-
|
|
285
|
-
} catch(error: any) {
|
|
286
|
-
return false;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
return true;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* Verifies a RFC6979 ECDSA signature of given data with a given public key and hash algorithm.
|
|
294
|
-
*
|
|
295
|
-
* @param options - The options for the verification operation.
|
|
296
|
-
* @param options.data - The data that was signed.
|
|
297
|
-
* @param options.hash - The hash algorithm to use to generate a digest of the data.
|
|
298
|
-
* @param options.key - The public key to use for verification.
|
|
299
|
-
* @param options.signature - The signature to verify.
|
|
300
|
-
* @returns A Promise that resolves to a boolean indicating whether the signature is valid.
|
|
301
|
-
*/
|
|
302
|
-
public static async verify(options: {
|
|
303
|
-
data: Uint8Array,
|
|
304
|
-
hash: string,
|
|
305
|
-
key: Uint8Array,
|
|
306
|
-
signature: Uint8Array
|
|
307
|
-
}): Promise<boolean> {
|
|
308
|
-
const { data, hash, key, signature } = options;
|
|
309
|
-
|
|
310
|
-
// Generate a digest of the data using the specified hash function.
|
|
311
|
-
const hashFunction = this.hashAlgorithms[hash];
|
|
312
|
-
const digest = hashFunction(data);
|
|
313
|
-
|
|
314
|
-
// Verify operation with malleability check disabled. Guaranteed support
|
|
315
|
-
// for low-s signatures across languages.
|
|
316
|
-
// Notable Cloud KMS providers do not natively support it however,
|
|
317
|
-
// low-s signatures are a requirement for Bitcoin.
|
|
318
|
-
const isValid = secp256k1.verify(signature, digest, key, { lowS: false });
|
|
319
|
-
|
|
320
|
-
return isValid;
|
|
321
|
-
}
|
|
322
|
-
}
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import type { BytesKeyPair } from '../types/crypto-key.js';
|
|
2
|
-
|
|
3
|
-
import { x25519 } from '@noble/curves/ed25519';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* The `X25519` class provides an interface for X25519 (Curve25519) key pair
|
|
7
|
-
* generation, public key computation, and shared secret computation. The class
|
|
8
|
-
* uses the '@noble/curves/ed25519' package for the cryptographic operations.
|
|
9
|
-
*
|
|
10
|
-
* All methods of this class are asynchronous and return Promises. They all use
|
|
11
|
-
* the Uint8Array type for keys and data, providing a consistent
|
|
12
|
-
* interface for working with binary data.
|
|
13
|
-
*
|
|
14
|
-
* Example usage:
|
|
15
|
-
*
|
|
16
|
-
* ```ts
|
|
17
|
-
* const ownKeyPair = await X25519.generateKeyPair();
|
|
18
|
-
* const otherPartyKeyPair = await X25519.generateKeyPair();
|
|
19
|
-
* const sharedSecret = await X25519.sharedSecret({
|
|
20
|
-
* privateKey : ownKeyPair.privateKey,
|
|
21
|
-
* publicKey : otherPartyKeyPair.publicKey
|
|
22
|
-
* });
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
export class X25519 {
|
|
26
|
-
/**
|
|
27
|
-
* Generates a key pair for X25519 (private and public key).
|
|
28
|
-
*
|
|
29
|
-
* @returns A Promise that resolves to a BytesKeyPair object.
|
|
30
|
-
*/
|
|
31
|
-
public static async generateKeyPair(): Promise<BytesKeyPair> {
|
|
32
|
-
// Generate the private key and compute its public key.
|
|
33
|
-
const privateKey = x25519.utils.randomPrivateKey();
|
|
34
|
-
const publicKey = x25519.getPublicKey(privateKey);
|
|
35
|
-
|
|
36
|
-
const keyPair = {
|
|
37
|
-
privateKey : privateKey,
|
|
38
|
-
publicKey : publicKey
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
return keyPair;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Computes a public key given a private key.
|
|
46
|
-
*
|
|
47
|
-
* @param options - The options for the public key computation operation.
|
|
48
|
-
* @param options.privateKey - The private key used to compute the public key.
|
|
49
|
-
* @returns A Promise that resolves to the computed public key as a Uint8Array.
|
|
50
|
-
*/
|
|
51
|
-
public static async getPublicKey(options: {
|
|
52
|
-
privateKey: Uint8Array
|
|
53
|
-
}): Promise<Uint8Array> {
|
|
54
|
-
let { privateKey } = options;
|
|
55
|
-
|
|
56
|
-
// Compute public key.
|
|
57
|
-
const publicKey = x25519.getPublicKey(privateKey);
|
|
58
|
-
|
|
59
|
-
return publicKey;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Generates a RFC6090 ECDH shared secret given the private key of one party
|
|
64
|
-
* and the public key of another party.
|
|
65
|
-
*
|
|
66
|
-
* @param options - The options for the shared secret computation operation.
|
|
67
|
-
* @param options.privateKey - The private key of one party.
|
|
68
|
-
* @param options.publicKey - The public key of the other party.
|
|
69
|
-
* @returns A Promise that resolves to the computed shared secret as a Uint8Array.
|
|
70
|
-
*/
|
|
71
|
-
public static async sharedSecret(options: {
|
|
72
|
-
privateKey: Uint8Array,
|
|
73
|
-
publicKey: Uint8Array
|
|
74
|
-
}): Promise<Uint8Array> {
|
|
75
|
-
let { privateKey, publicKey } = options;
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const sharedSecret = x25519.getSharedSecret(privateKey, publicKey);
|
|
79
|
-
|
|
80
|
-
return sharedSecret;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Note that this method is currently unimplemented because the @noble/curves
|
|
85
|
-
* library does not yet provide a mechanism for checking whether a point
|
|
86
|
-
* belongs to the Curve25519. Therefore, it currently throws an error whenever
|
|
87
|
-
* it is called.
|
|
88
|
-
*
|
|
89
|
-
* @param options - The options for the key validation operation.
|
|
90
|
-
* @param options.key - The key to validate.
|
|
91
|
-
* @throws {Error} If the method is called because it is not yet implemented.
|
|
92
|
-
* @returns A Promise that resolves to void.
|
|
93
|
-
*/
|
|
94
|
-
public static async validatePublicKey(_options: {
|
|
95
|
-
key: Uint8Array
|
|
96
|
-
}): Promise<void> {
|
|
97
|
-
// TODO: add once/if @noble/curves library implements checking
|
|
98
|
-
// proper points on the Montgomery curve.
|
|
99
|
-
throw new Error(`Not implemented: 'validatePublicKey()'`);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { xchacha20poly1305 as xchacha20_poly1305 } from '@noble/ciphers/chacha';
|
|
2
|
-
|
|
3
|
-
const TAG_LENGTH = 16;
|
|
4
|
-
|
|
5
|
-
export class XChaCha20Poly1305 {
|
|
6
|
-
|
|
7
|
-
public static async decrypt(options: {
|
|
8
|
-
additionalData?: Uint8Array,
|
|
9
|
-
data: Uint8Array,
|
|
10
|
-
key: Uint8Array,
|
|
11
|
-
nonce: Uint8Array,
|
|
12
|
-
tag: Uint8Array
|
|
13
|
-
}): Promise<Uint8Array> {
|
|
14
|
-
const { additionalData, data, key, nonce, tag } = options;
|
|
15
|
-
// console.log(additionalData, data, key, nonce, tag);
|
|
16
|
-
const xc20p = xchacha20_poly1305(key, nonce, additionalData);
|
|
17
|
-
const ciphertext = new Uint8Array([...data, ...tag]);
|
|
18
|
-
const plaintext = xc20p.decrypt(ciphertext);
|
|
19
|
-
|
|
20
|
-
return plaintext;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
public static async encrypt(options: {
|
|
24
|
-
additionalData?: Uint8Array,
|
|
25
|
-
data: Uint8Array,
|
|
26
|
-
key: Uint8Array,
|
|
27
|
-
nonce: Uint8Array
|
|
28
|
-
}): Promise<{ ciphertext: Uint8Array, tag: Uint8Array }> {
|
|
29
|
-
const { additionalData, data, key, nonce } = options;
|
|
30
|
-
|
|
31
|
-
const xc20p = xchacha20_poly1305(key, nonce, additionalData);
|
|
32
|
-
const cipherOutput = xc20p.encrypt(data);
|
|
33
|
-
|
|
34
|
-
const ciphertext = cipherOutput.subarray(0, -TAG_LENGTH);
|
|
35
|
-
const tag = cipherOutput.subarray(-TAG_LENGTH);
|
|
36
|
-
|
|
37
|
-
return { ciphertext, tag };
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public static async generateKey(): Promise<Uint8Array> {
|
|
41
|
-
// Generate the secret key.
|
|
42
|
-
const secretKey = crypto.getRandomValues(new Uint8Array(32));
|
|
43
|
-
|
|
44
|
-
return secretKey;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { xchacha20 } from '@noble/ciphers/chacha';
|
|
2
|
-
|
|
3
|
-
export class XChaCha20 {
|
|
4
|
-
public static async decrypt(options: {
|
|
5
|
-
data: Uint8Array,
|
|
6
|
-
key: Uint8Array,
|
|
7
|
-
nonce: Uint8Array
|
|
8
|
-
}): Promise<Uint8Array> {
|
|
9
|
-
const { data, key, nonce } = options;
|
|
10
|
-
|
|
11
|
-
const ciphertext = xchacha20(key, nonce, data);
|
|
12
|
-
|
|
13
|
-
return ciphertext;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
public static async encrypt(options: {
|
|
17
|
-
data: Uint8Array,
|
|
18
|
-
key: Uint8Array,
|
|
19
|
-
nonce: Uint8Array
|
|
20
|
-
}): Promise<Uint8Array> {
|
|
21
|
-
const { data, key, nonce } = options;
|
|
22
|
-
|
|
23
|
-
const plaintext = xchacha20(key, nonce, data);
|
|
24
|
-
|
|
25
|
-
return plaintext;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
public static async generateKey(): Promise<Uint8Array> {
|
|
29
|
-
// Generate the secret key.
|
|
30
|
-
const secretKey = crypto.getRandomValues(new Uint8Array(32));
|
|
31
|
-
|
|
32
|
-
return secretKey;
|
|
33
|
-
}
|
|
34
|
-
}
|
package/src/crypto/index.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export type * from './types/crypto-key.js';
|
|
2
|
-
export type * from './types/iddwn-crypto.js';
|
|
3
|
-
|
|
4
|
-
export * from './algorithms-api/index.js';
|
|
5
|
-
export * from './crypto-algorithms/index.js';
|
|
6
|
-
export * from './crypto-primitives/index.js';
|
|
7
|
-
export * from './jose.js';
|
|
8
|
-
export * as utils from './utils.js';
|