@substrate-system/keys 0.2.3 → 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/README.md +279 -160
- package/dist/_base.d.ts +183 -0
- package/dist/_base.d.ts.map +1 -0
- package/dist/aes/index.cjs +123 -0
- package/dist/aes/index.cjs.map +7 -0
- package/dist/aes/index.d.ts +20 -0
- package/dist/aes/index.d.ts.map +1 -0
- package/dist/aes/index.js +119 -0
- package/dist/aes/index.js.map +7 -0
- package/dist/aes/index.min.cjs +2 -0
- package/dist/aes/index.min.cjs.map +7 -0
- package/dist/aes/index.min.js +4 -0
- package/dist/aes/index.min.js.map +7 -0
- package/dist/constants.d.ts +15 -4
- package/dist/constants.d.ts.map +1 -1
- package/dist/ecc/index.cjs +412 -0
- package/dist/ecc/index.cjs.map +7 -0
- package/dist/ecc/index.d.ts +111 -0
- package/dist/ecc/index.d.ts.map +1 -0
- package/dist/ecc/index.js +414 -0
- package/dist/ecc/index.js.map +7 -0
- package/dist/ecc/index.min.cjs +2 -0
- package/dist/ecc/index.min.cjs.map +7 -0
- package/dist/ecc/index.min.js +4 -0
- package/dist/ecc/index.min.js.map +7 -0
- package/dist/errors.d.ts +27 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/index.d.ts +3 -190
- package/dist/index.d.ts.map +1 -1
- package/dist/meta.json +91 -118
- package/dist/rsa/index.cjs +283 -0
- package/dist/rsa/index.cjs.map +7 -0
- package/dist/rsa/index.d.ts +118 -0
- package/dist/rsa/index.d.ts.map +1 -0
- package/dist/rsa/index.js +287 -0
- package/dist/rsa/index.js.map +7 -0
- package/dist/rsa/index.min.cjs +2 -0
- package/dist/rsa/index.min.cjs.map +7 -0
- package/dist/rsa/index.min.js +4 -0
- package/dist/rsa/index.min.js.map +7 -0
- package/dist/types.d.ts +20 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/util.d.ts +15 -14
- package/dist/util.d.ts.map +1 -1
- package/package.json +38 -12
- package/dist/constants.cjs +0 -64
- package/dist/constants.cjs.map +0 -7
- package/dist/constants.js +0 -44
- package/dist/constants.js.map +0 -7
- package/dist/constants.min.cjs +0 -2
- package/dist/constants.min.cjs.map +0 -7
- package/dist/constants.min.js +0 -2
- package/dist/constants.min.js.map +0 -7
- package/dist/index.cjs +0 -480
- package/dist/index.cjs.map +0 -7
- package/dist/index.js +0 -493
- package/dist/index.js.map +0 -7
- package/dist/index.min.cjs +0 -2
- package/dist/index.min.cjs.map +0 -7
- package/dist/index.min.js +0 -4
- package/dist/index.min.js.map +0 -7
- package/dist/types.cjs +0 -57
- package/dist/types.cjs.map +0 -7
- package/dist/types.js +0 -37
- package/dist/types.js.map +0 -7
- package/dist/types.min.cjs +0 -2
- package/dist/types.min.cjs.map +0 -7
- package/dist/types.min.js +0 -2
- package/dist/types.min.js.map +0 -7
- package/dist/util.cjs +0 -375
- package/dist/util.cjs.map +0 -7
- package/dist/util.js +0 -373
- package/dist/util.js.map +0 -7
- package/dist/util.min.cjs +0 -4
- package/dist/util.min.cjs.map +0 -7
- package/dist/util.min.js +0 -4
- package/dist/util.min.js.map +0 -7
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
21
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
23
|
+
var rsa_exports = {};
|
|
24
|
+
__export(rsa_exports, {
|
|
25
|
+
RsaKeys: () => RsaKeys,
|
|
26
|
+
encrypt: () => encrypt,
|
|
27
|
+
encryptKeyTo: () => encryptKeyTo,
|
|
28
|
+
encryptTo: () => encryptTo,
|
|
29
|
+
getPublicKeyAsArrayBuffer: () => import_util.getPublicKeyAsArrayBuffer,
|
|
30
|
+
getPublicKeyAsUint8Array: () => import_util2.getPublicKeyAsUint8Array,
|
|
31
|
+
publicKeyToDid: () => import_util.publicKeyToDid,
|
|
32
|
+
verify: () => verify
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(rsa_exports);
|
|
35
|
+
var import_one_webcrypto = require("@substrate-system/one-webcrypto");
|
|
36
|
+
var import_uint8arrays = require("uint8arrays");
|
|
37
|
+
var import_aes = require("../aes/index.js");
|
|
38
|
+
var import_constants = require("../constants.js");
|
|
39
|
+
var import_base = require("../_base.js");
|
|
40
|
+
var import_types = require("../types.js");
|
|
41
|
+
var import_util = require("../util.js");
|
|
42
|
+
__reExport(rsa_exports, require("../constants.js"), module.exports);
|
|
43
|
+
var import_util2 = require("../util.js");
|
|
44
|
+
function toArrayBuffer(data) {
|
|
45
|
+
return new Uint8Array(data).buffer;
|
|
46
|
+
}
|
|
47
|
+
__name(toArrayBuffer, "toArrayBuffer");
|
|
48
|
+
const _RsaKeys = class _RsaKeys extends import_base.AbstractKeys {
|
|
49
|
+
constructor(opts) {
|
|
50
|
+
super(opts);
|
|
51
|
+
_RsaKeys.EXCHANGE_KEY_NAME = opts.exchangeKeyName || import_constants.DEFAULT_RSA_EXCHANGE;
|
|
52
|
+
_RsaKeys.WRITE_KEY_NAME = opts.writeKeyName || import_constants.DEFAULT_RSA_WRITE;
|
|
53
|
+
}
|
|
54
|
+
static async _createExchangeKeys() {
|
|
55
|
+
const exchangeKeys = await (0, import_util.makeRSAKeypair)(
|
|
56
|
+
import_constants.DEFAULT_RSA_SIZE,
|
|
57
|
+
import_constants.DEFAULT_HASH_ALGORITHM,
|
|
58
|
+
import_types.KeyUse.Exchange
|
|
59
|
+
);
|
|
60
|
+
return exchangeKeys;
|
|
61
|
+
}
|
|
62
|
+
static async _createWriteKeys() {
|
|
63
|
+
const writeKeys = await (0, import_util.makeRSAKeypair)(
|
|
64
|
+
import_constants.DEFAULT_RSA_SIZE,
|
|
65
|
+
import_constants.DEFAULT_HASH_ALGORITHM,
|
|
66
|
+
import_types.KeyUse.Write
|
|
67
|
+
);
|
|
68
|
+
return writeKeys;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Serialize this keys instance. Will return an object of
|
|
72
|
+
* { DID, publicExchangeKey }, where DID is the public signature key,
|
|
73
|
+
* and `publicExchangeKey` is the encryption key, `base64` encoded.
|
|
74
|
+
* @returns {Promise<{ DID:DID, publicExchangeKey:string }>}
|
|
75
|
+
*/
|
|
76
|
+
async toJson(format) {
|
|
77
|
+
const pubEnc = this.publicExchangeKey;
|
|
78
|
+
const did = this.DID;
|
|
79
|
+
const spki = await import_one_webcrypto.webcrypto.subtle.exportKey(
|
|
80
|
+
"spki",
|
|
81
|
+
pubEnc
|
|
82
|
+
);
|
|
83
|
+
const keyString = format ? (0, import_uint8arrays.toString)(new Uint8Array(spki), format) : (0, import_util.toBase64)(spki);
|
|
84
|
+
return {
|
|
85
|
+
publicExchangeKey: keyString,
|
|
86
|
+
DID: did
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Sign the message, and return the signature as a `Uint8Array`.
|
|
91
|
+
*/
|
|
92
|
+
async sign(msg, charsize = import_constants.DEFAULT_CHAR_SIZE) {
|
|
93
|
+
const key = this.writeKey;
|
|
94
|
+
const sig = await import_util.rsaOperations.sign(
|
|
95
|
+
msg,
|
|
96
|
+
key.privateKey,
|
|
97
|
+
charsize
|
|
98
|
+
);
|
|
99
|
+
return new Uint8Array(sig);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Sign a message, return the signature as a base64 encoded string.
|
|
103
|
+
*/
|
|
104
|
+
async signAsString(msg, charsize) {
|
|
105
|
+
const sig = await this.sign(msg, charsize);
|
|
106
|
+
return (0, import_util.toBase64)(sig);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Encrypt content using hybrid encryption (RSA + AES).
|
|
110
|
+
*/
|
|
111
|
+
async encrypt(content, recipient, aesKeyOrInfo, keysizeOrAesKey, keysize) {
|
|
112
|
+
const publicKey = recipient || this.exchangeKey.publicKey;
|
|
113
|
+
const aesKey = aesKeyOrInfo;
|
|
114
|
+
const keysizeValue = keysizeOrAesKey;
|
|
115
|
+
const key = aesKey || await import_aes.AES.create({ length: keysizeValue || keysize });
|
|
116
|
+
const encryptedContent = await import_aes.AES.encrypt(
|
|
117
|
+
typeof content === "string" ? (0, import_uint8arrays.fromString)(content) : content,
|
|
118
|
+
typeof key === "string" ? await import_aes.AES.import(key) : key
|
|
119
|
+
);
|
|
120
|
+
const encryptedKey = await encryptKeyTo({ key, publicKey });
|
|
121
|
+
const result = (0, import_util.joinBufs)(encryptedKey, encryptedContent);
|
|
122
|
+
return new Uint8Array(result);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Encrypt and return as base64 string.
|
|
126
|
+
*/
|
|
127
|
+
async encryptAsString(content, recipient, aesKeyOrInfo, keysizeOrAesKey, keysize) {
|
|
128
|
+
const encrypted = await this.encrypt(content, recipient, aesKeyOrInfo, keysizeOrAesKey, keysize);
|
|
129
|
+
return (0, import_uint8arrays.toString)(encrypted, "base64pad");
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Decrypt the given message. Expects encrypted AES key + encrypted content.
|
|
133
|
+
*/
|
|
134
|
+
async decrypt(msg, keysize, _aesAlgorithm) {
|
|
135
|
+
const keysizeValue = (typeof keysize === "number" ? keysize : void 0) || import_constants.DEFAULT_SYMM_LENGTH;
|
|
136
|
+
const cipherText = (0, import_util.normalizeToBuf)(msg, import_util.base64ToArrBuf);
|
|
137
|
+
const key = cipherText.slice(0, keysizeValue);
|
|
138
|
+
const data = cipherText.slice(keysizeValue);
|
|
139
|
+
const decryptedKey = await this.decryptKey(key);
|
|
140
|
+
const decryptedContent = await import_aes.AES.decrypt(data, decryptedKey);
|
|
141
|
+
return decryptedContent;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Decrypt and return as string.
|
|
145
|
+
*/
|
|
146
|
+
async decryptAsString(msg, keysize, _aesAlgorithm) {
|
|
147
|
+
const dec = await this.decrypt(msg, keysize, _aesAlgorithm);
|
|
148
|
+
return (0, import_uint8arrays.toString)(dec);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Decrypt the given encrypted AES key.
|
|
152
|
+
* Return the key as `Uint8Array`.
|
|
153
|
+
*/
|
|
154
|
+
async decryptKey(key) {
|
|
155
|
+
const decrypted = await import_util.rsaOperations.decrypt(
|
|
156
|
+
key,
|
|
157
|
+
this.exchangeKey.privateKey
|
|
158
|
+
);
|
|
159
|
+
return decrypted;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Decrypt the given AES key, return the result as a string.
|
|
163
|
+
*/
|
|
164
|
+
async decryptKeyAsString(msg, format) {
|
|
165
|
+
const decrypted = await import_util.rsaOperations.decrypt(
|
|
166
|
+
msg,
|
|
167
|
+
this.exchangeKey.privateKey
|
|
168
|
+
);
|
|
169
|
+
return (0, import_uint8arrays.toString)(decrypted, format);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Get the relevant AES key for RSA - decrypt the given encrypted key.
|
|
173
|
+
*/
|
|
174
|
+
async getAesKey(encryptedKey, _info) {
|
|
175
|
+
if (!encryptedKey) {
|
|
176
|
+
throw new Error("RSA requires an encrypted AES key to decrypt");
|
|
177
|
+
}
|
|
178
|
+
if (typeof encryptedKey === "string") {
|
|
179
|
+
const decryptedKeyData = await this.decryptKey(encryptedKey);
|
|
180
|
+
return await import_aes.AES.import(decryptedKeyData);
|
|
181
|
+
} else {
|
|
182
|
+
return encryptedKey;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
__name(_RsaKeys, "RsaKeys");
|
|
187
|
+
__publicField(_RsaKeys, "TYPE", "rsa");
|
|
188
|
+
__publicField(_RsaKeys, "EXCHANGE_KEY_NAME", import_constants.DEFAULT_RSA_EXCHANGE);
|
|
189
|
+
__publicField(_RsaKeys, "WRITE_KEY_NAME", import_constants.DEFAULT_RSA_WRITE);
|
|
190
|
+
let RsaKeys = _RsaKeys;
|
|
191
|
+
Object.assign(RsaKeys.prototype.sign, {
|
|
192
|
+
asString: /* @__PURE__ */ __name(async function(msg, charsize) {
|
|
193
|
+
return this.signAsString(msg, charsize);
|
|
194
|
+
}, "asString")
|
|
195
|
+
});
|
|
196
|
+
Object.assign(RsaKeys.prototype.encrypt, {
|
|
197
|
+
asString: /* @__PURE__ */ __name(async function(content, recipient, aesKey, keysize) {
|
|
198
|
+
return this.encryptAsString(content, recipient, aesKey, keysize);
|
|
199
|
+
}, "asString")
|
|
200
|
+
});
|
|
201
|
+
Object.assign(RsaKeys.prototype.decrypt, {
|
|
202
|
+
asString: /* @__PURE__ */ __name(async function(msg, keysize) {
|
|
203
|
+
return this.decryptAsString(msg, keysize);
|
|
204
|
+
}, "asString")
|
|
205
|
+
});
|
|
206
|
+
async function verify(msg, sig, signingDid) {
|
|
207
|
+
const _key = (0, import_util.didToPublicKey)(signingDid);
|
|
208
|
+
const key = await (0, import_util.importPublicKey)(
|
|
209
|
+
toArrayBuffer(_key.publicKey),
|
|
210
|
+
import_types.HashAlg.SHA_256,
|
|
211
|
+
import_types.KeyUse.Sign
|
|
212
|
+
);
|
|
213
|
+
try {
|
|
214
|
+
const isOk = import_util.rsaOperations.verify(msg, sig, key);
|
|
215
|
+
return isOk;
|
|
216
|
+
} catch (_err) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
__name(verify, "verify");
|
|
221
|
+
Object.assign(RsaKeys.prototype.decryptKey, {
|
|
222
|
+
asString: /* @__PURE__ */ __name(async function(msg, format) {
|
|
223
|
+
return this.decryptKeyAsString(msg, format);
|
|
224
|
+
}, "asString")
|
|
225
|
+
});
|
|
226
|
+
async function encryptTo(opts, aesKey) {
|
|
227
|
+
const { content, publicKey } = opts;
|
|
228
|
+
const key = aesKey || await import_aes.AES.create();
|
|
229
|
+
const encryptedContent = await import_aes.AES.encrypt(
|
|
230
|
+
typeof content === "string" ? (0, import_uint8arrays.fromString)(content) : content,
|
|
231
|
+
typeof key === "string" ? await import_aes.AES.import(key) : key
|
|
232
|
+
);
|
|
233
|
+
const encryptedKey = await encryptKeyTo({ key, publicKey });
|
|
234
|
+
return (0, import_util.joinBufs)(encryptedKey, encryptedContent);
|
|
235
|
+
}
|
|
236
|
+
__name(encryptTo, "encryptTo");
|
|
237
|
+
encryptTo.asString = async function(opts, aesKey) {
|
|
238
|
+
const { content, publicKey } = opts;
|
|
239
|
+
const key = aesKey || await import_aes.AES.create();
|
|
240
|
+
const encryptedContent = await import_aes.AES.encrypt(
|
|
241
|
+
typeof content === "string" ? (0, import_uint8arrays.fromString)(content) : content,
|
|
242
|
+
typeof key === "string" ? await import_aes.AES.import(key) : key,
|
|
243
|
+
"arraybuffer"
|
|
244
|
+
);
|
|
245
|
+
const encryptedKey = await encryptKeyTo({ key, publicKey });
|
|
246
|
+
const joined = (0, import_util.joinBufs)(encryptedKey, encryptedContent);
|
|
247
|
+
return (0, import_uint8arrays.toString)(new Uint8Array(joined), "base64pad");
|
|
248
|
+
};
|
|
249
|
+
async function encryptKeyTo({ key, publicKey }, format) {
|
|
250
|
+
let _key;
|
|
251
|
+
if (key instanceof CryptoKey) {
|
|
252
|
+
_key = await import_aes.AES.export(key);
|
|
253
|
+
} else {
|
|
254
|
+
_key = key;
|
|
255
|
+
}
|
|
256
|
+
const buf = await import_util.rsaOperations.encrypt(_key, publicKey);
|
|
257
|
+
if (format && format === "arraybuffer") return buf;
|
|
258
|
+
return new Uint8Array(buf);
|
|
259
|
+
}
|
|
260
|
+
__name(encryptKeyTo, "encryptKeyTo");
|
|
261
|
+
encryptKeyTo.asString = async function({ key, publicKey }, format) {
|
|
262
|
+
const asArr = await encryptKeyTo({ key, publicKey });
|
|
263
|
+
return format ? (0, import_uint8arrays.toString)(asArr, format) : (0, import_util.toBase64)(asArr);
|
|
264
|
+
};
|
|
265
|
+
async function encrypt(data, cryptoKey, format, iv) {
|
|
266
|
+
const key = (0, import_util.isCryptoKey)(cryptoKey) ? cryptoKey : await (0, import_aes.importAesKey)(cryptoKey);
|
|
267
|
+
const encrypted = iv ? await import_one_webcrypto.webcrypto.subtle.encrypt({ name: import_constants.AES_GCM, iv: toArrayBuffer(iv) }, key, toArrayBuffer(data)) : await encryptBytes(data, key);
|
|
268
|
+
if (format && format === "arraybuffer") return encrypted;
|
|
269
|
+
return new Uint8Array(encrypted);
|
|
270
|
+
}
|
|
271
|
+
__name(encrypt, "encrypt");
|
|
272
|
+
async function encryptBytes(msg, key, opts) {
|
|
273
|
+
const data = (0, import_util.normalizeUnicodeToBuf)(msg, opts?.charsize ?? import_constants.DEFAULT_CHAR_SIZE);
|
|
274
|
+
const importedKey = typeof key === "string" ? await (0, import_util.importKey)(key, opts) : key;
|
|
275
|
+
const iv = opts?.iv || (0, import_util.randomBuf)(import_constants.IV_LENGTH);
|
|
276
|
+
const cipherBuf = await import_one_webcrypto.webcrypto.subtle.encrypt({
|
|
277
|
+
name: import_constants.AES_GCM,
|
|
278
|
+
iv
|
|
279
|
+
}, importedKey, data);
|
|
280
|
+
return (0, import_util.joinBufs)(iv, cipherBuf);
|
|
281
|
+
}
|
|
282
|
+
__name(encryptBytes, "encryptBytes");
|
|
283
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/rsa/index.ts"],
|
|
4
|
+
"sourcesContent": ["import { webcrypto } from '@substrate-system/one-webcrypto'\nimport { fromString, toString, type SupportedEncodings } from 'uint8arrays'\nimport { AES, importAesKey } from '../aes/index.js'\nimport {\n DEFAULT_CHAR_SIZE,\n DEFAULT_SYMM_LENGTH,\n AES_GCM,\n IV_LENGTH,\n DEFAULT_RSA_EXCHANGE,\n DEFAULT_RSA_WRITE,\n DEFAULT_RSA_SIZE,\n DEFAULT_HASH_ALGORITHM,\n} from '../constants.js'\nimport { AbstractKeys, type KeyArgs } from '../_base.js'\nimport type {\n DID,\n Msg,\n SymmKeyLength,\n SymmKey,\n CharSize,\n} from '../types.js'\nimport { HashAlg, KeyUse } from '../types.js'\nimport {\n publicKeyToDid,\n getPublicKeyAsArrayBuffer,\n isCryptoKey,\n normalizeUnicodeToBuf,\n importKey,\n randomBuf,\n joinBufs,\n normalizeToBuf,\n rsaOperations,\n base64ToArrBuf,\n toBase64,\n didToPublicKey,\n importPublicKey,\n makeRSAKeypair\n} from '../util.js'\n\nexport { publicKeyToDid, getPublicKeyAsArrayBuffer }\nexport * from '../constants.js'\n\n// Helper function to ensure proper ArrayBuffer type\nfunction toArrayBuffer (data: Uint8Array): ArrayBuffer {\n return new Uint8Array(data).buffer\n}\nexport type { DID }\nexport { getPublicKeyAsUint8Array } from '../util.js'\nexport type SerializedKeys = {\n DID:DID;\n publicExchangeKey:string;\n}\n\nexport class RsaKeys extends AbstractKeys {\n static TYPE = 'rsa' as const\n static EXCHANGE_KEY_NAME:string = DEFAULT_RSA_EXCHANGE\n static WRITE_KEY_NAME:string = DEFAULT_RSA_WRITE\n\n constructor (opts:KeyArgs) {\n super(opts)\n RsaKeys.EXCHANGE_KEY_NAME = opts.exchangeKeyName || DEFAULT_RSA_EXCHANGE\n RsaKeys.WRITE_KEY_NAME = opts.writeKeyName || DEFAULT_RSA_WRITE\n }\n\n static async _createExchangeKeys ():Promise<CryptoKeyPair> {\n const exchangeKeys = await makeRSAKeypair(\n DEFAULT_RSA_SIZE,\n DEFAULT_HASH_ALGORITHM,\n KeyUse.Exchange\n )\n\n return exchangeKeys\n }\n\n static async _createWriteKeys ():Promise<CryptoKeyPair> {\n const writeKeys = await makeRSAKeypair(\n DEFAULT_RSA_SIZE,\n DEFAULT_HASH_ALGORITHM,\n KeyUse.Write\n )\n\n return writeKeys\n }\n\n /**\n * Serialize this keys instance. Will return an object of\n * { DID, publicExchangeKey }, where DID is the public signature key,\n * and `publicExchangeKey` is the encryption key, `base64` encoded.\n * @returns {Promise<{ DID:DID, publicExchangeKey:string }>}\n */\n async toJson (\n format?:SupportedEncodings\n ):Promise<{ DID:DID; publicExchangeKey:string; }> {\n const pubEnc = this.publicExchangeKey\n const did = this.DID\n\n const spki = await webcrypto.subtle.exportKey(\n 'spki',\n pubEnc\n )\n\n const keyString = (format ?\n toString(new Uint8Array(spki), format) :\n toBase64(spki))\n\n return {\n publicExchangeKey: keyString,\n DID: did\n }\n }\n\n /**\n * Sign the message, and return the signature as a `Uint8Array`.\n */\n async sign (\n msg:Msg,\n charsize:CharSize = DEFAULT_CHAR_SIZE\n ):Promise<Uint8Array> {\n const key = this.writeKey\n const sig = await rsaOperations.sign(\n msg,\n key.privateKey,\n charsize\n )\n\n return new Uint8Array(sig)\n }\n\n /**\n * Sign a message, return the signature as a base64 encoded string.\n */\n async signAsString (msg:Msg, charsize?:CharSize):Promise<string> {\n const sig = await this.sign(msg, charsize)\n return toBase64(sig)\n }\n\n /**\n * Encrypt content using hybrid encryption (RSA + AES).\n */\n async encrypt (\n content:string|Uint8Array,\n recipient?:CryptoKey|string,\n aesKeyOrInfo?:SymmKey|Uint8Array|string, // For RSA, this is aesKey\n // For RSA, this is keysize\n keysizeOrAesKey?:SymmKeyLength|SymmKey|Uint8Array|string,\n keysize?:SymmKeyLength\n ):Promise<Uint8Array> {\n const publicKey = recipient || this.exchangeKey.publicKey\n // For RSA: aesKeyOrInfo is aesKey, keysizeOrAesKey is keysize\n const aesKey = aesKeyOrInfo\n const keysizeValue = keysizeOrAesKey as SymmKeyLength\n const key = aesKey || await AES.create({ length: keysizeValue || keysize })\n const encryptedContent = await AES.encrypt(\n typeof content === 'string' ? fromString(content) : content,\n typeof key === 'string' ? await AES.import(key) : key,\n )\n const encryptedKey = await encryptKeyTo({ key, publicKey })\n\n const result = joinBufs(encryptedKey, encryptedContent)\n return new Uint8Array(result)\n }\n\n /**\n * Encrypt and return as base64 string.\n */\n async encryptAsString (\n content:string|Uint8Array,\n recipient?:CryptoKey|string,\n aesKeyOrInfo?:SymmKey|Uint8Array|string, // For RSA, this is aesKey\n keysizeOrAesKey?:SymmKeyLength|SymmKey|Uint8Array|string, // For RSA, this is keysize\n keysize?:SymmKeyLength\n ):Promise<string> {\n const encrypted = await this.encrypt(content, recipient, aesKeyOrInfo, keysizeOrAesKey, keysize)\n return toString(encrypted, 'base64pad')\n }\n\n /**\n * Decrypt the given message. Expects encrypted AES key + encrypted content.\n */\n async decrypt (\n msg:string|Uint8Array|ArrayBuffer,\n keysize?:CryptoKey|string|SymmKeyLength,\n _aesAlgorithm?:string,\n ):Promise<Uint8Array> {\n // For RSA, the second parameter is keysize, not publicKey\n const keysizeValue = (typeof keysize === 'number' ? keysize : undefined) || DEFAULT_SYMM_LENGTH\n const cipherText = normalizeToBuf(msg, base64ToArrBuf)\n const key = cipherText.slice(0, keysizeValue)\n const data = cipherText.slice(keysizeValue)\n const decryptedKey = await this.decryptKey(key)\n const decryptedContent = await AES.decrypt(data, decryptedKey)\n return decryptedContent\n }\n\n /**\n * Decrypt and return as string.\n */\n async decryptAsString (\n msg:string|Uint8Array|ArrayBuffer,\n keysize?:CryptoKey|string|SymmKeyLength,\n _aesAlgorithm?:string,\n ):Promise<string> {\n const dec = await this.decrypt(msg, keysize, _aesAlgorithm)\n return toString(dec)\n }\n\n /**\n * Decrypt the given encrypted AES key.\n * Return the key as `Uint8Array`.\n */\n async decryptKey (key:string|Uint8Array|ArrayBuffer):Promise<Uint8Array> {\n const decrypted = await rsaOperations.decrypt(\n key,\n this.exchangeKey.privateKey\n )\n return decrypted\n }\n\n /**\n * Decrypt the given AES key, return the result as a string.\n */\n async decryptKeyAsString (\n msg:string|Uint8Array,\n format?:SupportedEncodings\n ):Promise<string> {\n const decrypted = await rsaOperations.decrypt(\n msg,\n this.exchangeKey.privateKey\n )\n\n return toString(decrypted, format)\n }\n\n /**\n * Get the relevant AES key for RSA - decrypt the given encrypted key.\n */\n async getAesKey (\n encryptedKey?:CryptoKey|string|null,\n _info?:string|null\n ):Promise<CryptoKey> {\n if (!encryptedKey) {\n // For RSA, we need an encrypted key to decrypt\n throw new Error('RSA requires an encrypted AES key to decrypt')\n }\n\n if (typeof encryptedKey === 'string') {\n const decryptedKeyData = await this.decryptKey(encryptedKey)\n return await AES.import(decryptedKeyData)\n } else {\n // If it's already a CryptoKey, return it\n return encryptedKey\n }\n }\n}\n\n// Add backward compatibility by attaching asString methods to prototypes\nObject.assign(RsaKeys.prototype.sign, {\n asString: async function (\n this: RsaKeys,\n msg: Msg,\n charsize?: CharSize\n ): Promise<string> {\n return this.signAsString(msg, charsize)\n }\n})\n\nObject.assign(RsaKeys.prototype.encrypt, {\n asString: async function (\n this: RsaKeys,\n content: string | Uint8Array,\n recipient?: CryptoKey | string,\n aesKey?: SymmKey | Uint8Array | string,\n keysize?: SymmKeyLength\n ): Promise<string> {\n return this.encryptAsString(content, recipient, aesKey, keysize)\n }\n})\n\nObject.assign(RsaKeys.prototype.decrypt, {\n asString: async function (\n this: RsaKeys,\n msg: string | Uint8Array | ArrayBuffer,\n keysize?: SymmKeyLength\n ): Promise<string> {\n return this.decryptAsString(msg, keysize)\n }\n})\n\n/**\n * Check that the given signature is valid with the given message.\n */\nexport async function verify (\n msg:string|Uint8Array,\n sig:string|Uint8Array,\n signingDid:DID\n):Promise<boolean> {\n const _key = didToPublicKey(signingDid)\n const key = await importPublicKey(\n toArrayBuffer(_key.publicKey),\n HashAlg.SHA_256,\n KeyUse.Sign\n )\n\n try {\n const isOk = rsaOperations.verify(msg, sig, key)\n return isOk\n } catch (_err) {\n return false\n }\n}\n\nObject.assign(RsaKeys.prototype.decryptKey, {\n asString: async function (\n this: RsaKeys,\n msg: string | Uint8Array,\n format?: SupportedEncodings\n ): Promise<string> {\n return this.decryptKeyAsString(msg, format)\n }\n})\n\n/**\n * Encrypt the given message to the given public key. If an AES key is not\n * provided, one will be created. Use an AES key to encrypt the given\n * content, then we encrypt the AES key to the given public key.\n *\n * @param {{ content, publicKey }} opts The content to encrypt and\n * public key to encrypt to\n * @param {SymmKey|Uint8Array|string} [aesKey] An optional AES key to encrypt\n * to the given public key\n * @returns {Promise<ArrayBuffer>} The encrypted AES key, concattenated with\n * the encrypted content.\n */\nexport async function encryptTo (\n opts:{\n content:string|Uint8Array;\n publicKey:CryptoKey|string;\n },\n aesKey?:SymmKey|Uint8Array|string,\n):Promise<ArrayBuffer> {\n const { content, publicKey } = opts\n const key = aesKey || await AES.create()\n const encryptedContent = await AES.encrypt(\n typeof content === 'string' ? fromString(content) : content,\n typeof key === 'string' ? await AES.import(key) : key,\n )\n const encryptedKey = await encryptKeyTo({ key, publicKey })\n\n return joinBufs(encryptedKey, encryptedContent)\n}\n\n/**\n * Encrypt the given AES key to the given public key. Return the encrypted AES\n * key concattenated with the cipher text.\n *\n * @param { content, publicKey } opts The content to encrypt and key to\n * encrypt to.\n * @param {SymmKey|Uint8Array|string} [aesKey] Optional -- the AES key. One will\n * be created if not passed in.\n * @returns {Promise<string>} The encrypted AES key concattenated with the\n * cipher text.\n */\nencryptTo.asString = async function (\n opts:{ content:string|Uint8Array; publicKey:CryptoKey|string },\n aesKey?:SymmKey|Uint8Array|string\n):Promise<string> {\n const { content, publicKey } = opts\n const key = aesKey || await AES.create()\n const encryptedContent = await AES.encrypt(\n typeof content === 'string' ? fromString(content) : content,\n typeof key === 'string' ? await AES.import(key) : key,\n 'arraybuffer'\n )\n\n const encryptedKey = await encryptKeyTo({ key, publicKey })\n const joined = joinBufs(encryptedKey, encryptedContent)\n\n return toString(new Uint8Array(joined), 'base64pad')\n}\n\nexport async function encryptKeyTo ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|Uint8Array|string;\n}, format:'arraybuffer'):Promise<ArrayBuffer>\n\nexport async function encryptKeyTo ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|Uint8Array|string;\n}, format:'uint8array'):Promise<Uint8Array>\n\nexport async function encryptKeyTo ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|Uint8Array|string;\n}, format?:undefined):Promise<Uint8Array>\n\n/**\n * Encrypt the given content to the given public key. This is RSA encryption,\n * and should be used only to encrypt AES keys.\n *\n * @param {{ content, publicKey }} params The content to encrypt, and public key\n * to encrypt it to.\n * @returns {Promise<Uint8Array>}\n */\nexport async function encryptKeyTo ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|Uint8Array|string;\n}, format?:'uint8array'|'arraybuffer'):Promise<Uint8Array|ArrayBuffer> {\n let _key:Uint8Array|string\n if (key instanceof CryptoKey) {\n _key = await AES.export(key)\n } else {\n _key = key\n }\n\n const buf = await rsaOperations.encrypt(_key, publicKey)\n if (format && format === 'arraybuffer') return buf\n return new Uint8Array(buf)\n}\n\nencryptKeyTo.asString = async function ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|string|Uint8Array;\n}, format?:SupportedEncodings):Promise<string> {\n const asArr = await encryptKeyTo({ key, publicKey })\n return format ? toString(asArr, format) : toBase64(asArr)\n}\n\nexport async function encrypt (\n data:Uint8Array,\n cryptoKey:CryptoKey|Uint8Array,\n format?:undefined,\n iv?:Uint8Array\n):Promise<Uint8Array>\n\nexport async function encrypt (\n data:Uint8Array,\n cryptoKey:CryptoKey|Uint8Array,\n format:'uint8array',\n iv?:Uint8Array\n):Promise<Uint8Array>\n\nexport async function encrypt (\n data:Uint8Array,\n cryptoKey:CryptoKey|Uint8Array,\n format:'arraybuffer',\n iv?:Uint8Array\n):Promise<ArrayBuffer>\n\n/**\n * Encrypt the given data\n */\nexport async function encrypt (\n data:Uint8Array,\n cryptoKey:CryptoKey|Uint8Array,\n format?:'uint8array'|'arraybuffer',\n iv?:Uint8Array\n):Promise<Uint8Array|ArrayBuffer> {\n // get a crypto key\n const key = (isCryptoKey(cryptoKey) ?\n cryptoKey :\n await importAesKey(cryptoKey)\n )\n\n // prefix the `iv` into the cipher text\n const encrypted = (iv ?\n await webcrypto.subtle.encrypt({ name: AES_GCM, iv: toArrayBuffer(iv) }, key, toArrayBuffer(data)) :\n await encryptBytes(data, key)\n )\n\n if (format && format === 'arraybuffer') return encrypted\n\n return new Uint8Array(encrypted)\n}\n\nasync function encryptBytes (\n msg:Msg,\n key:CryptoKey|string,\n opts?:Partial<{ iv:ArrayBuffer, charsize:number }>\n):Promise<ArrayBuffer> {\n const data = normalizeUnicodeToBuf(msg, opts?.charsize ?? DEFAULT_CHAR_SIZE)\n const importedKey = typeof key === 'string' ?\n await importKey(key, opts) :\n key\n const iv:ArrayBuffer = opts?.iv || randomBuf(IV_LENGTH)\n const cipherBuf = await webcrypto.subtle.encrypt({\n name: AES_GCM,\n iv\n }, importedKey, data)\n\n // prefix the `iv` to the ciphertext\n return joinBufs(iv, cipherBuf)\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;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AAS,IAAI,SAAS;AACjC;AAjBe;",
|
|
6
|
+
"names": ["import_util"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { type SupportedEncodings } from 'uint8arrays';
|
|
2
|
+
import { AbstractKeys, type KeyArgs } from '../_base.js';
|
|
3
|
+
import type { DID, Msg, SymmKeyLength, SymmKey, CharSize } from '../types.js';
|
|
4
|
+
import { publicKeyToDid, getPublicKeyAsArrayBuffer } from '../util.js';
|
|
5
|
+
export { publicKeyToDid, getPublicKeyAsArrayBuffer };
|
|
6
|
+
export * from '../constants.js';
|
|
7
|
+
export type { DID };
|
|
8
|
+
export { getPublicKeyAsUint8Array } from '../util.js';
|
|
9
|
+
export type SerializedKeys = {
|
|
10
|
+
DID: DID;
|
|
11
|
+
publicExchangeKey: string;
|
|
12
|
+
};
|
|
13
|
+
export declare class RsaKeys extends AbstractKeys {
|
|
14
|
+
static TYPE: "rsa";
|
|
15
|
+
static EXCHANGE_KEY_NAME: string;
|
|
16
|
+
static WRITE_KEY_NAME: string;
|
|
17
|
+
constructor(opts: KeyArgs);
|
|
18
|
+
static _createExchangeKeys(): Promise<CryptoKeyPair>;
|
|
19
|
+
static _createWriteKeys(): Promise<CryptoKeyPair>;
|
|
20
|
+
/**
|
|
21
|
+
* Serialize this keys instance. Will return an object of
|
|
22
|
+
* { DID, publicExchangeKey }, where DID is the public signature key,
|
|
23
|
+
* and `publicExchangeKey` is the encryption key, `base64` encoded.
|
|
24
|
+
* @returns {Promise<{ DID:DID, publicExchangeKey:string }>}
|
|
25
|
+
*/
|
|
26
|
+
toJson(format?: SupportedEncodings): Promise<{
|
|
27
|
+
DID: DID;
|
|
28
|
+
publicExchangeKey: string;
|
|
29
|
+
}>;
|
|
30
|
+
/**
|
|
31
|
+
* Sign the message, and return the signature as a `Uint8Array`.
|
|
32
|
+
*/
|
|
33
|
+
sign(msg: Msg, charsize?: CharSize): Promise<Uint8Array>;
|
|
34
|
+
/**
|
|
35
|
+
* Sign a message, return the signature as a base64 encoded string.
|
|
36
|
+
*/
|
|
37
|
+
signAsString(msg: Msg, charsize?: CharSize): Promise<string>;
|
|
38
|
+
/**
|
|
39
|
+
* Encrypt content using hybrid encryption (RSA + AES).
|
|
40
|
+
*/
|
|
41
|
+
encrypt(content: string | Uint8Array, recipient?: CryptoKey | string, aesKeyOrInfo?: SymmKey | Uint8Array | string, // For RSA, this is aesKey
|
|
42
|
+
keysizeOrAesKey?: SymmKeyLength | SymmKey | Uint8Array | string, keysize?: SymmKeyLength): Promise<Uint8Array>;
|
|
43
|
+
/**
|
|
44
|
+
* Encrypt and return as base64 string.
|
|
45
|
+
*/
|
|
46
|
+
encryptAsString(content: string | Uint8Array, recipient?: CryptoKey | string, aesKeyOrInfo?: SymmKey | Uint8Array | string, // For RSA, this is aesKey
|
|
47
|
+
keysizeOrAesKey?: SymmKeyLength | SymmKey | Uint8Array | string, // For RSA, this is keysize
|
|
48
|
+
keysize?: SymmKeyLength): Promise<string>;
|
|
49
|
+
/**
|
|
50
|
+
* Decrypt the given message. Expects encrypted AES key + encrypted content.
|
|
51
|
+
*/
|
|
52
|
+
decrypt(msg: string | Uint8Array | ArrayBuffer, keysize?: CryptoKey | string | SymmKeyLength, _aesAlgorithm?: string): Promise<Uint8Array>;
|
|
53
|
+
/**
|
|
54
|
+
* Decrypt and return as string.
|
|
55
|
+
*/
|
|
56
|
+
decryptAsString(msg: string | Uint8Array | ArrayBuffer, keysize?: CryptoKey | string | SymmKeyLength, _aesAlgorithm?: string): Promise<string>;
|
|
57
|
+
/**
|
|
58
|
+
* Decrypt the given encrypted AES key.
|
|
59
|
+
* Return the key as `Uint8Array`.
|
|
60
|
+
*/
|
|
61
|
+
decryptKey(key: string | Uint8Array | ArrayBuffer): Promise<Uint8Array>;
|
|
62
|
+
/**
|
|
63
|
+
* Decrypt the given AES key, return the result as a string.
|
|
64
|
+
*/
|
|
65
|
+
decryptKeyAsString(msg: string | Uint8Array, format?: SupportedEncodings): Promise<string>;
|
|
66
|
+
/**
|
|
67
|
+
* Get the relevant AES key for RSA - decrypt the given encrypted key.
|
|
68
|
+
*/
|
|
69
|
+
getAesKey(encryptedKey?: CryptoKey | string | null, _info?: string | null): Promise<CryptoKey>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Check that the given signature is valid with the given message.
|
|
73
|
+
*/
|
|
74
|
+
export declare function verify(msg: string | Uint8Array, sig: string | Uint8Array, signingDid: DID): Promise<boolean>;
|
|
75
|
+
/**
|
|
76
|
+
* Encrypt the given message to the given public key. If an AES key is not
|
|
77
|
+
* provided, one will be created. Use an AES key to encrypt the given
|
|
78
|
+
* content, then we encrypt the AES key to the given public key.
|
|
79
|
+
*
|
|
80
|
+
* @param {{ content, publicKey }} opts The content to encrypt and
|
|
81
|
+
* public key to encrypt to
|
|
82
|
+
* @param {SymmKey|Uint8Array|string} [aesKey] An optional AES key to encrypt
|
|
83
|
+
* to the given public key
|
|
84
|
+
* @returns {Promise<ArrayBuffer>} The encrypted AES key, concattenated with
|
|
85
|
+
* the encrypted content.
|
|
86
|
+
*/
|
|
87
|
+
export declare function encryptTo(opts: {
|
|
88
|
+
content: string | Uint8Array;
|
|
89
|
+
publicKey: CryptoKey | string;
|
|
90
|
+
}, aesKey?: SymmKey | Uint8Array | string): Promise<ArrayBuffer>;
|
|
91
|
+
export declare namespace encryptTo {
|
|
92
|
+
var asString: (opts: {
|
|
93
|
+
content: string | Uint8Array;
|
|
94
|
+
publicKey: CryptoKey | string;
|
|
95
|
+
}, aesKey?: SymmKey | Uint8Array | string) => Promise<string>;
|
|
96
|
+
}
|
|
97
|
+
export declare function encryptKeyTo({ key, publicKey }: {
|
|
98
|
+
key: string | Uint8Array | CryptoKey;
|
|
99
|
+
publicKey: CryptoKey | Uint8Array | string;
|
|
100
|
+
}, format: 'arraybuffer'): Promise<ArrayBuffer>;
|
|
101
|
+
export declare function encryptKeyTo({ key, publicKey }: {
|
|
102
|
+
key: string | Uint8Array | CryptoKey;
|
|
103
|
+
publicKey: CryptoKey | Uint8Array | string;
|
|
104
|
+
}, format: 'uint8array'): Promise<Uint8Array>;
|
|
105
|
+
export declare function encryptKeyTo({ key, publicKey }: {
|
|
106
|
+
key: string | Uint8Array | CryptoKey;
|
|
107
|
+
publicKey: CryptoKey | Uint8Array | string;
|
|
108
|
+
}, format?: undefined): Promise<Uint8Array>;
|
|
109
|
+
export declare namespace encryptKeyTo {
|
|
110
|
+
var asString: ({ key, publicKey }: {
|
|
111
|
+
key: string | Uint8Array | CryptoKey;
|
|
112
|
+
publicKey: CryptoKey | string | Uint8Array;
|
|
113
|
+
}, format?: SupportedEncodings) => Promise<string>;
|
|
114
|
+
}
|
|
115
|
+
export declare function encrypt(data: Uint8Array, cryptoKey: CryptoKey | Uint8Array, format?: undefined, iv?: Uint8Array): Promise<Uint8Array>;
|
|
116
|
+
export declare function encrypt(data: Uint8Array, cryptoKey: CryptoKey | Uint8Array, format: 'uint8array', iv?: Uint8Array): Promise<Uint8Array>;
|
|
117
|
+
export declare function encrypt(data: Uint8Array, cryptoKey: CryptoKey | Uint8Array, format: 'arraybuffer', iv?: Uint8Array): Promise<ArrayBuffer>;
|
|
118
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rsa/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAwB,KAAK,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAY3E,OAAO,EAAE,YAAY,EAAE,KAAK,OAAO,EAAE,MAAM,aAAa,CAAA;AACxD,OAAO,KAAK,EACR,GAAG,EACH,GAAG,EACH,aAAa,EACb,OAAO,EACP,QAAQ,EACX,MAAM,aAAa,CAAA;AAEpB,OAAO,EACH,cAAc,EACd,yBAAyB,EAa5B,MAAM,YAAY,CAAA;AAEnB,OAAO,EAAE,cAAc,EAAE,yBAAyB,EAAE,CAAA;AACpD,cAAc,iBAAiB,CAAA;AAM/B,YAAY,E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