@substrate-system/keys 0.2.43 → 0.2.44
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 +68 -1
- package/dist/_base.cjs +92 -22
- package/dist/_base.cjs.map +2 -2
- package/dist/_base.d.ts +44 -3
- package/dist/_base.d.ts.map +1 -1
- package/dist/_base.js +92 -22
- package/dist/_base.js.map +2 -2
- package/dist/_base.min.cjs +1 -1
- package/dist/_base.min.cjs.map +3 -3
- package/dist/_base.min.js +2 -2
- package/dist/_base.min.js.map +4 -4
- package/dist/browser/index.js +120 -62
- package/dist/browser/index.min.js +2 -2
- package/dist/ecc/index.cjs +1 -13
- package/dist/ecc/index.cjs.map +2 -2
- package/dist/ecc/index.d.ts +1 -2
- package/dist/ecc/index.d.ts.map +1 -1
- package/dist/ecc/index.js +1 -13
- package/dist/ecc/index.js.map +2 -2
- package/dist/ecc/index.min.cjs +1 -1
- package/dist/ecc/index.min.cjs.map +3 -3
- package/dist/ecc/index.min.js +2 -2
- package/dist/ecc/index.min.js.map +3 -3
- package/dist/errors.cjs +10 -0
- package/dist/errors.cjs.map +2 -2
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +10 -0
- package/dist/errors.js.map +2 -2
- package/dist/errors.min.cjs +1 -1
- package/dist/errors.min.cjs.map +3 -3
- package/dist/errors.min.js +1 -1
- package/dist/errors.min.js.map +3 -3
- package/dist/index.min.js +2 -2
- package/dist/index.min.js.map +4 -4
- package/dist/meta.json +23 -16
- package/dist/rsa/index.cjs +0 -5
- package/dist/rsa/index.cjs.map +2 -2
- package/dist/rsa/index.d.ts +1 -2
- package/dist/rsa/index.d.ts.map +1 -1
- package/dist/rsa/index.js +0 -5
- package/dist/rsa/index.js.map +2 -2
- package/dist/rsa/index.min.cjs +1 -1
- package/dist/rsa/index.min.cjs.map +3 -3
- package/dist/rsa/index.min.js +2 -2
- package/dist/rsa/index.min.js.map +4 -4
- package/package.json +1 -1
package/dist/browser/index.js
CHANGED
|
@@ -2019,29 +2019,83 @@
|
|
|
2019
2019
|
}
|
|
2020
2020
|
__name(delMany, "delMany");
|
|
2021
2021
|
|
|
2022
|
+
// src/errors.ts
|
|
2023
|
+
var KeyDoesNotExist = new Error("Key does not exist. Make sure you properly instantiated the keystore.");
|
|
2024
|
+
var NotKeyPair = new Error("Retrieved a symmetric key when an asymmetric keypair was expected. Please use a different key name.");
|
|
2025
|
+
var NotKey = new Error("Retrieved an asymmetric keypair when an symmetric key was expected. Please use a different key name.");
|
|
2026
|
+
var ECCNotEnabled = new Error("ECC is not enabled for this browser. Please use RSA instead.");
|
|
2027
|
+
var UnsupportedCrypto = new Error("Cryptosystem not supported. Please use ECC or RSA");
|
|
2028
|
+
var InvalidKeyUse2 = new Error("Invalid key use. Please use 'exchange' or 'write'");
|
|
2029
|
+
var InvalidMaxValue2 = new Error("Max must be less than 256 and greater than 0");
|
|
2030
|
+
var KeysDeletedError = class extends Error {
|
|
2031
|
+
static {
|
|
2032
|
+
__name(this, "KeysDeletedError");
|
|
2033
|
+
}
|
|
2034
|
+
constructor(msg) {
|
|
2035
|
+
super(msg || "These keys were deleted");
|
|
2036
|
+
this.name = "KeysDeletedError";
|
|
2037
|
+
}
|
|
2038
|
+
};
|
|
2039
|
+
function checkValidKeyUse2(use) {
|
|
2040
|
+
checkValid2(use, ["exchange" /* Exchange */, "write" /* Write */, "signing" /* Sign */], InvalidKeyUse2);
|
|
2041
|
+
}
|
|
2042
|
+
__name(checkValidKeyUse2, "checkValidKeyUse");
|
|
2043
|
+
function checkValid2(toCheck, opts, error) {
|
|
2044
|
+
const match = opts.some((opt) => opt === toCheck);
|
|
2045
|
+
if (!match) {
|
|
2046
|
+
throw error;
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
__name(checkValid2, "checkValid");
|
|
2050
|
+
|
|
2022
2051
|
// src/_base.ts
|
|
2023
2052
|
var AbstractKeys = class _AbstractKeys {
|
|
2024
2053
|
static {
|
|
2025
2054
|
__name(this, "AbstractKeys");
|
|
2026
2055
|
}
|
|
2027
2056
|
DID;
|
|
2028
|
-
exchangeKey;
|
|
2029
|
-
writeKey;
|
|
2030
2057
|
hasPersisted;
|
|
2031
2058
|
isSessionOnly;
|
|
2059
|
+
/**
|
|
2060
|
+
* Have these keys been deleted? A deleted keypair is terminal -- the key
|
|
2061
|
+
* material is gone, and any use of it throws.
|
|
2062
|
+
*/
|
|
2063
|
+
destroyed;
|
|
2064
|
+
/**
|
|
2065
|
+
* The `indexedDB` names this keypair was loaded from, and the names
|
|
2066
|
+
* `persist` and `delete` will use.
|
|
2067
|
+
*/
|
|
2068
|
+
exchangeKeyName;
|
|
2069
|
+
writeKeyName;
|
|
2032
2070
|
// type:'ecc'|'rsa'
|
|
2033
2071
|
static EXCHANGE_KEY_NAME;
|
|
2034
2072
|
// needs to be defined by child class
|
|
2035
2073
|
static WRITE_KEY_NAME;
|
|
2036
2074
|
static _instance;
|
|
2037
2075
|
// a cache for indexedDB
|
|
2076
|
+
// the key material -- read it via the `exchangeKey`/`writeKey` getters,
|
|
2077
|
+
// which throw if this instance was deleted
|
|
2078
|
+
_exchangeKey;
|
|
2079
|
+
_writeKey;
|
|
2038
2080
|
constructor(opts) {
|
|
2039
2081
|
const { keys } = opts;
|
|
2082
|
+
const ctor = this.constructor;
|
|
2040
2083
|
this.DID = opts.did;
|
|
2041
|
-
this.
|
|
2042
|
-
this.
|
|
2084
|
+
this._exchangeKey = keys.exchange;
|
|
2085
|
+
this._writeKey = keys.write;
|
|
2043
2086
|
this.hasPersisted = opts.hasPersisted;
|
|
2044
2087
|
this.isSessionOnly = !!opts.isSessionOnly;
|
|
2088
|
+
this.destroyed = false;
|
|
2089
|
+
this.exchangeKeyName = opts.exchangeKeyName || ctor.EXCHANGE_KEY_NAME;
|
|
2090
|
+
this.writeKeyName = opts.writeKeyName || ctor.WRITE_KEY_NAME;
|
|
2091
|
+
}
|
|
2092
|
+
get exchangeKey() {
|
|
2093
|
+
if (this.destroyed || !this._exchangeKey) throw new KeysDeletedError();
|
|
2094
|
+
return this._exchangeKey;
|
|
2095
|
+
}
|
|
2096
|
+
get writeKey() {
|
|
2097
|
+
if (this.destroyed || !this._writeKey) throw new KeysDeletedError();
|
|
2098
|
+
return this._writeKey;
|
|
2045
2099
|
}
|
|
2046
2100
|
publicExchangeKeyAsString(format2) {
|
|
2047
2101
|
return this.publicExchangeKey.asString(format2);
|
|
@@ -2094,24 +2148,64 @@
|
|
|
2094
2148
|
* Save this keys instance to `indexedDB`.
|
|
2095
2149
|
*/
|
|
2096
2150
|
async persist() {
|
|
2151
|
+
if (this.destroyed) throw new KeysDeletedError();
|
|
2097
2152
|
if (this.isSessionOnly) return;
|
|
2098
|
-
const exchange = this.constructor.EXCHANGE_KEY_NAME;
|
|
2099
|
-
const write = this.constructor.WRITE_KEY_NAME;
|
|
2100
2153
|
await Promise.all([
|
|
2101
|
-
set(
|
|
2102
|
-
set(
|
|
2154
|
+
set(this.exchangeKeyName, this.exchangeKey),
|
|
2155
|
+
set(this.writeKeyName, this.writeKey)
|
|
2103
2156
|
]);
|
|
2104
2157
|
this.hasPersisted = true;
|
|
2105
2158
|
}
|
|
2106
2159
|
/**
|
|
2107
|
-
* Delete the keys
|
|
2160
|
+
* Delete this keypair. This deletes the keys from indexedDB, drops the
|
|
2161
|
+
* in-memory key material, and clears the instance cache, so `load` will
|
|
2162
|
+
* not hand out these keys again.
|
|
2163
|
+
*
|
|
2164
|
+
* After this, the instance is `destroyed` -- using it throws
|
|
2165
|
+
* a `KeysDeletedError`.
|
|
2108
2166
|
*/
|
|
2109
2167
|
async delete() {
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2168
|
+
const exchangeName = this.exchangeKeyName;
|
|
2169
|
+
const writeName = this.writeKeyName;
|
|
2170
|
+
await delMany([exchangeName, writeName]);
|
|
2171
|
+
const ctor = this.constructor;
|
|
2172
|
+
ctor._invalidate(exchangeName, writeName);
|
|
2173
|
+
this._destroy();
|
|
2174
|
+
}
|
|
2175
|
+
/**
|
|
2176
|
+
* Drop the in-memory key material. Internal -- call `delete` instead.
|
|
2177
|
+
*/
|
|
2178
|
+
_destroy() {
|
|
2179
|
+
this.destroyed = true;
|
|
2114
2180
|
this.hasPersisted = false;
|
|
2181
|
+
this._exchangeKey = null;
|
|
2182
|
+
this._writeKey = null;
|
|
2183
|
+
}
|
|
2184
|
+
/**
|
|
2185
|
+
* If the cached instance was loaded from the given names, then its keys
|
|
2186
|
+
* are gone too. Destroy it, and clear the cache. Internal.
|
|
2187
|
+
*/
|
|
2188
|
+
static _invalidate(exchangeName, writeName) {
|
|
2189
|
+
const cached = this._instance;
|
|
2190
|
+
if (!cached) return;
|
|
2191
|
+
if (cached.exchangeKeyName !== exchangeName) return;
|
|
2192
|
+
if (cached.writeKeyName !== writeName) return;
|
|
2193
|
+
cached._destroy();
|
|
2194
|
+
this._instance = null;
|
|
2195
|
+
}
|
|
2196
|
+
/**
|
|
2197
|
+
* Delete a stored keypair without loading it first. Deletes the keys from
|
|
2198
|
+
* indexedDB, and destroys the cached instance, if there is one for
|
|
2199
|
+
* these names.
|
|
2200
|
+
*
|
|
2201
|
+
* @param {{ encryptionKeyName, writeKeyName }} [opts] The indexedDB names
|
|
2202
|
+
* to delete. Defaults to this class's names.
|
|
2203
|
+
*/
|
|
2204
|
+
static async delete(opts = {}) {
|
|
2205
|
+
const exchangeName = opts.encryptionKeyName || this.EXCHANGE_KEY_NAME;
|
|
2206
|
+
const writeName = opts.writeKeyName || this.WRITE_KEY_NAME;
|
|
2207
|
+
await delMany([exchangeName, writeName]);
|
|
2208
|
+
this._invalidate(exchangeName, writeName);
|
|
2115
2209
|
}
|
|
2116
2210
|
/**
|
|
2117
2211
|
* Return a 32-character, DNS friendly hash of the public signing key.
|
|
@@ -2164,14 +2258,15 @@
|
|
|
2164
2258
|
static async load(opts = {
|
|
2165
2259
|
session: false
|
|
2166
2260
|
}) {
|
|
2167
|
-
|
|
2261
|
+
const exchangeKeyName = opts.encryptionKeyName || this.EXCHANGE_KEY_NAME;
|
|
2262
|
+
const writeKeyName = opts.writeKeyName || this.WRITE_KEY_NAME;
|
|
2263
|
+
const cached = this._instance;
|
|
2264
|
+
if (cached && !cached.destroyed && cached.exchangeKeyName === exchangeKeyName && cached.writeKeyName === writeKeyName) {
|
|
2265
|
+
return cached;
|
|
2266
|
+
}
|
|
2168
2267
|
let hasPersisted = true;
|
|
2169
|
-
let exchangeKeys = await get(
|
|
2170
|
-
|
|
2171
|
-
);
|
|
2172
|
-
let writeKeys = await get(
|
|
2173
|
-
opts.writeKeyName || this.WRITE_KEY_NAME
|
|
2174
|
-
);
|
|
2268
|
+
let exchangeKeys = await get(exchangeKeyName);
|
|
2269
|
+
let writeKeys = await get(writeKeyName);
|
|
2175
2270
|
if (!exchangeKeys) {
|
|
2176
2271
|
hasPersisted = false;
|
|
2177
2272
|
exchangeKeys = await this._createExchangeKeys(opts.extractable);
|
|
@@ -2189,7 +2284,9 @@
|
|
|
2189
2284
|
keys: { exchange: exchangeKeys, write: writeKeys },
|
|
2190
2285
|
did: did2,
|
|
2191
2286
|
hasPersisted,
|
|
2192
|
-
isSessionOnly: !!opts.session
|
|
2287
|
+
isSessionOnly: !!opts.session,
|
|
2288
|
+
exchangeKeyName,
|
|
2289
|
+
writeKeyName
|
|
2193
2290
|
});
|
|
2194
2291
|
this._instance = keys;
|
|
2195
2292
|
return keys;
|
|
@@ -2240,7 +2337,7 @@
|
|
|
2240
2337
|
return new Uint8Array(data).buffer;
|
|
2241
2338
|
}
|
|
2242
2339
|
__name(toArrayBuffer2, "toArrayBuffer");
|
|
2243
|
-
var RsaKeys = class
|
|
2340
|
+
var RsaKeys = class extends AbstractKeys {
|
|
2244
2341
|
static {
|
|
2245
2342
|
__name(this, "RsaKeys");
|
|
2246
2343
|
}
|
|
@@ -2260,11 +2357,6 @@
|
|
|
2260
2357
|
static async create(session, extractable, keys) {
|
|
2261
2358
|
return await super.create(session, extractable, keys);
|
|
2262
2359
|
}
|
|
2263
|
-
constructor(opts) {
|
|
2264
|
-
super(opts);
|
|
2265
|
-
_RsaKeys.EXCHANGE_KEY_NAME = opts.exchangeKeyName || DEFAULT_RSA_EXCHANGE;
|
|
2266
|
-
_RsaKeys.WRITE_KEY_NAME = opts.writeKeyName || DEFAULT_RSA_WRITE;
|
|
2267
|
-
}
|
|
2268
2360
|
static async _createExchangeKeys(extractable = false) {
|
|
2269
2361
|
const exchangeKeys = await makeRSAKeypair(
|
|
2270
2362
|
DEFAULT_RSA_SIZE,
|
|
@@ -2501,28 +2593,6 @@
|
|
|
2501
2593
|
importPublicKey: () => importPublicKey,
|
|
2502
2594
|
verify: () => verify3
|
|
2503
2595
|
});
|
|
2504
|
-
|
|
2505
|
-
// src/errors.ts
|
|
2506
|
-
var KeyDoesNotExist = new Error("Key does not exist. Make sure you properly instantiated the keystore.");
|
|
2507
|
-
var NotKeyPair = new Error("Retrieved a symmetric key when an asymmetric keypair was expected. Please use a different key name.");
|
|
2508
|
-
var NotKey = new Error("Retrieved an asymmetric keypair when an symmetric key was expected. Please use a different key name.");
|
|
2509
|
-
var ECCNotEnabled = new Error("ECC is not enabled for this browser. Please use RSA instead.");
|
|
2510
|
-
var UnsupportedCrypto = new Error("Cryptosystem not supported. Please use ECC or RSA");
|
|
2511
|
-
var InvalidKeyUse2 = new Error("Invalid key use. Please use 'exchange' or 'write'");
|
|
2512
|
-
var InvalidMaxValue2 = new Error("Max must be less than 256 and greater than 0");
|
|
2513
|
-
function checkValidKeyUse2(use) {
|
|
2514
|
-
checkValid2(use, ["exchange" /* Exchange */, "write" /* Write */, "signing" /* Sign */], InvalidKeyUse2);
|
|
2515
|
-
}
|
|
2516
|
-
__name(checkValidKeyUse2, "checkValidKeyUse");
|
|
2517
|
-
function checkValid2(toCheck, opts, error) {
|
|
2518
|
-
const match = opts.some((opt) => opt === toCheck);
|
|
2519
|
-
if (!match) {
|
|
2520
|
-
throw error;
|
|
2521
|
-
}
|
|
2522
|
-
}
|
|
2523
|
-
__name(checkValid2, "checkValid");
|
|
2524
|
-
|
|
2525
|
-
// src/ecc/index.ts
|
|
2526
2596
|
var webcrypto5 = globalThis.crypto;
|
|
2527
2597
|
function toArrayBuffer3(data) {
|
|
2528
2598
|
return new Uint8Array(data).buffer;
|
|
@@ -2548,11 +2618,6 @@
|
|
|
2548
2618
|
static async create(session, extractable, keys) {
|
|
2549
2619
|
return await super.create(session, extractable, keys);
|
|
2550
2620
|
}
|
|
2551
|
-
constructor(opts) {
|
|
2552
|
-
super(opts);
|
|
2553
|
-
_EccKeys.EXCHANGE_KEY_NAME = opts.exchangeKeyName || DEFAULT_ECC_EXCHANGE;
|
|
2554
|
-
_EccKeys.WRITE_KEY_NAME = opts.writeKeyName || DEFAULT_ECC_WRITE;
|
|
2555
|
-
}
|
|
2556
2621
|
get publicExchangeKey() {
|
|
2557
2622
|
const publicKey = this.exchangeKey.publicKey;
|
|
2558
2623
|
return Object.assign(publicKey, {
|
|
@@ -2599,14 +2664,7 @@
|
|
|
2599
2664
|
static async load(opts = {
|
|
2600
2665
|
session: false
|
|
2601
2666
|
}) {
|
|
2602
|
-
|
|
2603
|
-
const exchangeKeyName = opts.encryptionKeyName || DEFAULT_ECC_EXCHANGE;
|
|
2604
|
-
const writeKeyName = opts.writeKeyName || DEFAULT_ECC_WRITE;
|
|
2605
|
-
return super.load({
|
|
2606
|
-
...opts,
|
|
2607
|
-
encryptionKeyName: exchangeKeyName,
|
|
2608
|
-
writeKeyName
|
|
2609
|
-
});
|
|
2667
|
+
return super.load(opts);
|
|
2610
2668
|
}
|
|
2611
2669
|
/**
|
|
2612
2670
|
* Serialize this keys instance. Will return an object of
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";(()=>{var Ir=Object.defineProperty;var s=(r,e)=>Ir(r,"name",{value:e,configurable:!0});var E=(r,e)=>{for(var t in e)Ir(r,t,{get:e[t],enumerable:!0})};var kr=(n=>(n[n.B1024=1024]="B1024",n[n.B2048=2048]="B2048",n[n.B4096=4096]="B4096",n))(kr||{}),Lr=(n=>(n[n.B128=128]="B128",n[n.B192=192]="B192",n[n.B256=256]="B256",n))(Lr||{}),Mr=(t=>(t.ECC="ecc",t.RSA="rsa",t))(Mr||{}),Qe=(i=>(i.SHA_1="SHA-1",i.SHA_256="SHA-256",i.SHA_384="SHA-384",i.SHA_512="SHA-512",i))(Qe||{}),er=(i=>(i.P_256="P-256",i.P_384="P-384",i.P_521="P-521",i.X25519="X25519",i))(er||{}),Z=(n=>(n.Sign="signing",n.Exchange="exchange",n.Write="write",n))(Z||{}),rr=(t=>(t[t.B8=8]="B8",t[t.B16=16]="B16",t))(rr||{});var dt={};E(dt,{AES_GCM:()=>N,BASE58_DID_PREFIX:()=>H,BLS_DID_PREFIX:()=>Fe,DEFAULT_CHAR_SIZE:()=>M,DEFAULT_ECC_EXCHANGE:()=>Be,DEFAULT_ECC_EXCHANGE_CURVE:()=>Bn,DEFAULT_ECC_WRITE:()=>ve,DEFAULT_HASH_ALGORITHM:()=>re,DEFAULT_RSA_EXCHANGE:()=>je,DEFAULT_RSA_SIZE:()=>Ge,DEFAULT_RSA_WRITE:()=>We,DEFAULT_SYMM_ALGORITHM:()=>B,DEFAULT_SYMM_LENGTH:()=>R,ECC_EXCHANGE_ALGORITHM:()=>Ce,ECC_WRITE_ALGORITHM:()=>ee,EDWARDS_DID_PREFIX:()=>Ve,IV_LENGTH:()=>S,KEY_TYPE:()=>Pe,KEY_USE:()=>it,RSA_ALGORITHM:()=>X,RSA_DID_PREFIX:()=>He,RSA_HASHING_ALGORITHM:()=>ze,RSA_SIGN_ALGORITHM:()=>Y,RsaKeys:()=>ie,SALT_LENGTH:()=>x,X25519_PUBLIC_KEY_LENGTH:()=>W,encrypt:()=>jn,encryptKeyTo:()=>he,encryptTo:()=>mt,getPublicKeyAsArrayBuffer:()=>J,getPublicKeyAsUint8Array:()=>Pr,publicKeyToDid:()=>Ie,verify:()=>Vn});function be(r=0){return new Uint8Array(r)}s(be,"allocUnsafe");function tr(r,e){e==null&&(e=r.reduce((i,o)=>i+o.length,0));let t=be(e),n=0;for(let i of r)t.set(i,n),n+=i.length;return t}s(tr,"concat");var ar={};E(ar,{base10:()=>St});var mi=new Uint8Array(0);function Nr(r,e){if(r===e)return!0;if(r.byteLength!==e.byteLength)return!1;for(let t=0;t<r.byteLength;t++)if(r[t]!==e[t])return!1;return!0}s(Nr,"equals");function z(r){if(r instanceof Uint8Array&&r.constructor.name==="Uint8Array")return r;if(r instanceof ArrayBuffer)return new Uint8Array(r);if(ArrayBuffer.isView(r))return new Uint8Array(r.buffer,r.byteOffset,r.byteLength);throw new Error("Unknown type, must be binary type")}s(z,"coerce");function Rr(r){return new TextEncoder().encode(r)}s(Rr,"fromString");function Or(r){return new TextDecoder().decode(r)}s(Or,"toString");function bt(r,e){if(r.length>=255)throw new TypeError("Alphabet too long");for(var t=new Uint8Array(256),n=0;n<t.length;n++)t[n]=255;for(var i=0;i<r.length;i++){var o=r.charAt(i),a=o.charCodeAt(0);if(t[a]!==255)throw new TypeError(o+" is ambiguous");t[a]=i}var y=r.length,c=r.charAt(0),g=Math.log(y)/Math.log(256),f=Math.log(256)/Math.log(y);function K(p){if(p instanceof Uint8Array||(ArrayBuffer.isView(p)?p=new Uint8Array(p.buffer,p.byteOffset,p.byteLength):Array.isArray(p)&&(p=Uint8Array.from(p))),!(p instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(p.length===0)return"";for(var l=0,P=0,U=0,T=p.length;U!==T&&p[U]===0;)U++,l++;for(var w=(T-U)*f+1>>>0,D=new Uint8Array(w);U!==T;){for(var k=p[U],V=0,I=w-1;(k!==0||V<P)&&I!==-1;I--,V++)k+=256*D[I]>>>0,D[I]=k%y>>>0,k=k/y>>>0;if(k!==0)throw new Error("Non-zero carry");P=V,U++}for(var O=w-P;O!==w&&D[O]===0;)O++;for(var Me=c.repeat(l);O<w;++O)Me+=r.charAt(D[O]);return Me}s(K,"encode");function C(p){if(typeof p!="string")throw new TypeError("Expected String");if(p.length===0)return new Uint8Array;var l=0;if(p[l]!==" "){for(var P=0,U=0;p[l]===c;)P++,l++;for(var T=(p.length-l)*g+1>>>0,w=new Uint8Array(T);p[l];){var D=t[p.charCodeAt(l)];if(D===255)return;for(var k=0,V=T-1;(D!==0||k<U)&&V!==-1;V--,k++)D+=y*w[V]>>>0,w[V]=D%256>>>0,D=D/256>>>0;if(D!==0)throw new Error("Non-zero carry");U=k,l++}if(p[l]!==" "){for(var I=T-U;I!==T&&w[I]===0;)I++;for(var O=new Uint8Array(P+(T-I)),Me=P;I!==T;)O[Me++]=w[I++];return O}}}s(C,"decodeUnsafe");function ae(p){var l=C(p);if(l)return l;throw new Error(`Non-${e} character`)}return s(ae,"decode"),{encode:K,decodeUnsafe:C,decode:ae}}s(bt,"base");var wt=bt,xt=wt,Hr=xt;var nr=class{static{s(this,"Encoder")}name;prefix;baseEncode;constructor(e,t,n){this.name=e,this.prefix=t,this.baseEncode=n}encode(e){if(e instanceof Uint8Array)return`${this.prefix}${this.baseEncode(e)}`;throw Error("Unknown type, must be binary type")}},ir=class{static{s(this,"Decoder")}name;prefix;baseDecode;prefixCodePoint;constructor(e,t,n){this.name=e,this.prefix=t;let i=t.codePointAt(0);if(i===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=i,this.baseDecode=n}decode(e){if(typeof e=="string"){if(e.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(e)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(e.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(e){return Gr(this,e)}},sr=class{static{s(this,"ComposedDecoder")}decoders;constructor(e){this.decoders=e}or(e){return Gr(this,e)}decode(e){let t=e[0],n=this.decoders[t];if(n!=null)return n.decode(e);throw RangeError(`Unable to decode multibase string ${JSON.stringify(e)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}};function Gr(r,e){return new sr({...r.decoders??{[r.prefix]:r},...e.decoders??{[e.prefix]:e}})}s(Gr,"or");var or=class{static{s(this,"Codec")}name;prefix;baseEncode;baseDecode;encoder;decoder;constructor(e,t,n,i){this.name=e,this.prefix=t,this.baseEncode=n,this.baseDecode=i,this.encoder=new nr(e,t,n),this.decoder=new ir(e,t,i)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}};function ye({name:r,prefix:e,encode:t,decode:n}){return new or(r,e,t,n)}s(ye,"from");function j({name:r,prefix:e,alphabet:t}){let{encode:n,decode:i}=Hr(t,r);return ye({prefix:e,name:r,encode:n,decode:s(o=>z(i(o)),"decode")})}s(j,"baseX");function Et(r,e,t,n){let i={};for(let f=0;f<e.length;++f)i[e[f]]=f;let o=r.length;for(;r[o-1]==="=";)--o;let a=new Uint8Array(o*t/8|0),y=0,c=0,g=0;for(let f=0;f<o;++f){let K=i[r[f]];if(K===void 0)throw new SyntaxError(`Non-${n} character`);c=c<<t|K,y+=t,y>=8&&(y-=8,a[g++]=255&c>>y)}if(y>=t||(255&c<<8-y)!==0)throw new SyntaxError("Unexpected end of data");return a}s(Et,"decode");function Ut(r,e,t){let n=e[e.length-1]==="=",i=(1<<t)-1,o="",a=0,y=0;for(let c=0;c<r.length;++c)for(y=y<<8|r[c],a+=8;a>t;)a-=t,o+=e[i&y>>a];if(a!==0&&(o+=e[i&y<<t-a]),n)for(;(o.length*t&7)!==0;)o+="=";return o}s(Ut,"encode");function d({name:r,prefix:e,bitsPerChar:t,alphabet:n}){return ye({prefix:e,name:r,encode(i){return Ut(i,n,t)},decode(i){return Et(i,n,t,r)}})}s(d,"rfc4648");var St=j({prefix:"9",name:"base10",alphabet:"0123456789"});var yr={};E(yr,{base16:()=>Ct,base16upper:()=>Pt});var Ct=d({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),Pt=d({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var cr={};E(cr,{base2:()=>Bt});var Bt=d({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var pr={};E(pr,{base256emoji:()=>It});var Fr=Array.from("\u{1F680}\u{1FA90}\u2604\u{1F6F0}\u{1F30C}\u{1F311}\u{1F312}\u{1F313}\u{1F314}\u{1F315}\u{1F316}\u{1F317}\u{1F318}\u{1F30D}\u{1F30F}\u{1F30E}\u{1F409}\u2600\u{1F4BB}\u{1F5A5}\u{1F4BE}\u{1F4BF}\u{1F602}\u2764\u{1F60D}\u{1F923}\u{1F60A}\u{1F64F}\u{1F495}\u{1F62D}\u{1F618}\u{1F44D}\u{1F605}\u{1F44F}\u{1F601}\u{1F525}\u{1F970}\u{1F494}\u{1F496}\u{1F499}\u{1F622}\u{1F914}\u{1F606}\u{1F644}\u{1F4AA}\u{1F609}\u263A\u{1F44C}\u{1F917}\u{1F49C}\u{1F614}\u{1F60E}\u{1F607}\u{1F339}\u{1F926}\u{1F389}\u{1F49E}\u270C\u2728\u{1F937}\u{1F631}\u{1F60C}\u{1F338}\u{1F64C}\u{1F60B}\u{1F497}\u{1F49A}\u{1F60F}\u{1F49B}\u{1F642}\u{1F493}\u{1F929}\u{1F604}\u{1F600}\u{1F5A4}\u{1F603}\u{1F4AF}\u{1F648}\u{1F447}\u{1F3B6}\u{1F612}\u{1F92D}\u2763\u{1F61C}\u{1F48B}\u{1F440}\u{1F62A}\u{1F611}\u{1F4A5}\u{1F64B}\u{1F61E}\u{1F629}\u{1F621}\u{1F92A}\u{1F44A}\u{1F973}\u{1F625}\u{1F924}\u{1F449}\u{1F483}\u{1F633}\u270B\u{1F61A}\u{1F61D}\u{1F634}\u{1F31F}\u{1F62C}\u{1F643}\u{1F340}\u{1F337}\u{1F63B}\u{1F613}\u2B50\u2705\u{1F97A}\u{1F308}\u{1F608}\u{1F918}\u{1F4A6}\u2714\u{1F623}\u{1F3C3}\u{1F490}\u2639\u{1F38A}\u{1F498}\u{1F620}\u261D\u{1F615}\u{1F33A}\u{1F382}\u{1F33B}\u{1F610}\u{1F595}\u{1F49D}\u{1F64A}\u{1F639}\u{1F5E3}\u{1F4AB}\u{1F480}\u{1F451}\u{1F3B5}\u{1F91E}\u{1F61B}\u{1F534}\u{1F624}\u{1F33C}\u{1F62B}\u26BD\u{1F919}\u2615\u{1F3C6}\u{1F92B}\u{1F448}\u{1F62E}\u{1F646}\u{1F37B}\u{1F343}\u{1F436}\u{1F481}\u{1F632}\u{1F33F}\u{1F9E1}\u{1F381}\u26A1\u{1F31E}\u{1F388}\u274C\u270A\u{1F44B}\u{1F630}\u{1F928}\u{1F636}\u{1F91D}\u{1F6B6}\u{1F4B0}\u{1F353}\u{1F4A2}\u{1F91F}\u{1F641}\u{1F6A8}\u{1F4A8}\u{1F92C}\u2708\u{1F380}\u{1F37A}\u{1F913}\u{1F619}\u{1F49F}\u{1F331}\u{1F616}\u{1F476}\u{1F974}\u25B6\u27A1\u2753\u{1F48E}\u{1F4B8}\u2B07\u{1F628}\u{1F31A}\u{1F98B}\u{1F637}\u{1F57A}\u26A0\u{1F645}\u{1F61F}\u{1F635}\u{1F44E}\u{1F932}\u{1F920}\u{1F927}\u{1F4CC}\u{1F535}\u{1F485}\u{1F9D0}\u{1F43E}\u{1F352}\u{1F617}\u{1F911}\u{1F30A}\u{1F92F}\u{1F437}\u260E\u{1F4A7}\u{1F62F}\u{1F486}\u{1F446}\u{1F3A4}\u{1F647}\u{1F351}\u2744\u{1F334}\u{1F4A3}\u{1F438}\u{1F48C}\u{1F4CD}\u{1F940}\u{1F922}\u{1F445}\u{1F4A1}\u{1F4A9}\u{1F450}\u{1F4F8}\u{1F47B}\u{1F910}\u{1F92E}\u{1F3BC}\u{1F975}\u{1F6A9}\u{1F34E}\u{1F34A}\u{1F47C}\u{1F48D}\u{1F4E3}\u{1F942}"),vt=Fr.reduce((r,e,t)=>(r[t]=e,r),[]),Tt=Fr.reduce((r,e,t)=>{let n=e.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${e}`);return r[n]=t,r},[]);function Dt(r){return r.reduce((e,t)=>(e+=vt[t],e),"")}s(Dt,"encode");function _t(r){let e=[];for(let t of r){let n=t.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${t}`);let i=Tt[n];if(i==null)throw new Error(`Non-base256emoji character: ${t}`);e.push(i)}return new Uint8Array(e)}s(_t,"decode");var It=ye({prefix:"\u{1F680}",name:"base256emoji",encode:Dt,decode:_t});var ur={};E(ur,{base32:()=>ce,base32hex:()=>Nt,base32hexpad:()=>Ot,base32hexpadupper:()=>zt,base32hexupper:()=>Rt,base32pad:()=>Lt,base32padupper:()=>Mt,base32upper:()=>kt,base32z:()=>Ht});var ce=d({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),kt=d({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Lt=d({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Mt=d({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Nt=d({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Rt=d({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Ot=d({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),zt=d({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Ht=d({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var fr={};E(fr,{base36:()=>we,base36upper:()=>Gt});var we=j({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),Gt=j({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var lr={};E(lr,{base58btc:()=>L,base58flickr:()=>Ft});var L=j({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),Ft=j({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var gr={};E(gr,{base64:()=>Vt,base64pad:()=>jt,base64url:()=>Wt,base64urlpad:()=>Xt});var Vt=d({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),jt=d({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),Wt=d({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),Xt=d({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var mr={};E(mr,{base8:()=>Yt});var Yt=d({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var dr={};E(dr,{identity:()=>$t});var $t=ye({prefix:"\0",name:"identity",encode:s(r=>Or(r),"encode"),decode:s(r=>Rr(r),"decode")});var Mi=new TextEncoder,Ni=new TextDecoder;var Ar={};E(Ar,{identity:()=>Kn});var Zt=Wr,Vr=128,Qt=127,en=~Qt,rn=Math.pow(2,31);function Wr(r,e,t){e=e||[],t=t||0;for(var n=t;r>=rn;)e[t++]=r&255|Vr,r/=128;for(;r&en;)e[t++]=r&255|Vr,r>>>=7;return e[t]=r|0,Wr.bytes=t-n+1,e}s(Wr,"encode");var tn=hr,nn=128,jr=127;function hr(r,n){var t=0,n=n||0,i=0,o=n,a,y=r.length;do{if(o>=y)throw hr.bytes=0,new RangeError("Could not decode varint");a=r[o++],t+=i<28?(a&jr)<<i:(a&jr)*Math.pow(2,i),i+=7}while(a>=nn);return hr.bytes=o-n,t}s(hr,"read");var sn=Math.pow(2,7),on=Math.pow(2,14),an=Math.pow(2,21),yn=Math.pow(2,28),cn=Math.pow(2,35),pn=Math.pow(2,42),un=Math.pow(2,49),fn=Math.pow(2,56),ln=Math.pow(2,63),gn=s(function(r){return r<sn?1:r<on?2:r<an?3:r<yn?4:r<cn?5:r<pn?6:r<un?7:r<fn?8:r<ln?9:10},"length"),mn={encode:Zt,decode:tn,encodingLength:gn},dn=mn,xe=dn;function Ee(r,e=0){return[xe.decode(r,e),xe.decode.bytes]}s(Ee,"decode");function pe(r,e,t=0){return xe.encode(r,e,t),e}s(pe,"encodeTo");function ue(r){return xe.encodingLength(r)}s(ue,"encodingLength");function Q(r,e){let t=e.byteLength,n=ue(r),i=n+ue(t),o=new Uint8Array(i+t);return pe(r,o,0),pe(t,o,n),o.set(e,i),new fe(r,t,e,o)}s(Q,"create");function Xr(r){let e=z(r),[t,n]=Ee(e),[i,o]=Ee(e.subarray(n)),a=e.subarray(n+o);if(a.byteLength!==i)throw new Error("Incorrect length");return new fe(t,i,a,e)}s(Xr,"decode");function Yr(r,e){if(r===e)return!0;{let t=e;return r.code===t.code&&r.size===t.size&&t.bytes instanceof Uint8Array&&Nr(r.bytes,t.bytes)}}s(Yr,"equals");var fe=class{static{s(this,"Digest")}code;size;digest;bytes;constructor(e,t,n,i){this.code=e,this.size=t,this.digest=n,this.bytes=i}};var $r=0,hn="identity",Jr=z;function An(r){return Q($r,Jr(r))}s(An,"digest");var Kn={code:$r,name:hn,encode:Jr,digest:An};var wr={};E(wr,{sha256:()=>bn,sha512:()=>wn});function br({name:r,code:e,encode:t}){return new Kr(r,e,t)}s(br,"from");var Kr=class{static{s(this,"Hasher")}name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){let t=this.encode(e);return t instanceof Uint8Array?Q(this.code,t):t.then(n=>Q(this.code,n))}else throw Error("Unknown type, must be binary type")}};function Zr(r){return async e=>new Uint8Array(await crypto.subtle.digest(r,e))}s(Zr,"sha");var bn=br({name:"sha2-256",code:18,encode:Zr("SHA-256")}),wn=br({name:"sha2-512",code:19,encode:Zr("SHA-512")});function Qr(r,e){let{bytes:t,version:n}=r;return n===0?En(t,xr(r),e??L.encoder):Un(t,xr(r),e??ce.encoder)}s(Qr,"format");var et=new WeakMap;function xr(r){let e=et.get(r);if(e==null){let t=new Map;return et.set(r,t),t}return e}s(xr,"baseCache");var Re=class r{static{s(this,"CID")}code;version;multihash;bytes;"/";constructor(e,t,n,i){this.code=t,this.version=e,this.multihash=n,this.bytes=i,this["/"]=i}get asCID(){return this}get byteOffset(){return this.bytes.byteOffset}get byteLength(){return this.bytes.byteLength}toV0(){switch(this.version){case 0:return this;case 1:{let{code:e,multihash:t}=this;if(e!==Se)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(t.code!==Sn)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return r.createV0(t)}default:throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)}}toV1(){switch(this.version){case 0:{let{code:e,digest:t}=this.multihash,n=Q(e,t);return r.createV1(this.code,n)}case 1:return this;default:throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`)}}equals(e){return r.equals(this,e)}static equals(e,t){let n=t;return n!=null&&e.code===n.code&&e.version===n.version&&Yr(e.multihash,n.multihash)}toString(e){return Qr(this,e)}toJSON(){return{"/":Qr(this)}}link(){return this}[Symbol.toStringTag]="CID";[Symbol.for("nodejs.util.inspect.custom")](){return`CID(${this.toString()})`}static asCID(e){if(e==null)return null;let t=e;if(t instanceof r)return t;if(t["/"]!=null&&t["/"]===t.bytes||t.asCID===t){let{version:n,code:i,multihash:o,bytes:a}=t;return new r(n,i,o,a??rt(n,i,o.bytes))}else if(t[Cn]===!0){let{version:n,multihash:i,code:o}=t,a=Xr(i);return r.create(n,o,a)}else return null}static create(e,t,n){if(typeof t!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(e){case 0:{if(t!==Se)throw new Error(`Version 0 CID must use dag-pb (code: ${Se}) block encoding`);return new r(e,t,n,n.bytes)}case 1:{let i=rt(e,t,n.bytes);return new r(e,t,n,i)}default:throw new Error("Invalid version")}}static createV0(e){return r.create(0,Se,e)}static createV1(e,t){return r.create(1,e,t)}static decode(e){let[t,n]=r.decodeFirst(e);if(n.length!==0)throw new Error("Incorrect length");return t}static decodeFirst(e){let t=r.inspectBytes(e),n=t.size-t.multihashSize,i=z(e.subarray(n,n+t.multihashSize));if(i.byteLength!==t.multihashSize)throw new Error("Incorrect length");let o=i.subarray(t.multihashSize-t.digestSize),a=new fe(t.multihashCode,t.digestSize,o,i);return[t.version===0?r.createV0(a):r.createV1(t.codec,a),e.subarray(t.size)]}static inspectBytes(e){let t=0,n=s(()=>{let[K,C]=Ee(e.subarray(t));return t+=C,K},"next"),i=n(),o=Se;if(i===18?(i=0,t=0):o=n(),i!==0&&i!==1)throw new RangeError(`Invalid CID version ${i}`);let a=t,y=n(),c=n(),g=t+c,f=g-a;return{version:i,codec:o,multihashCode:y,digestSize:c,multihashSize:f,size:g}}static parse(e,t){let[n,i]=xn(e,t),o=r.decode(i);if(o.version===0&&e[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return xr(o).set(n,e),o}};function xn(r,e){switch(r[0]){case"Q":{let t=e??L;return[L.prefix,t.decode(`${L.prefix}${r}`)]}case L.prefix:{let t=e??L;return[L.prefix,t.decode(r)]}case ce.prefix:{let t=e??ce;return[ce.prefix,t.decode(r)]}case we.prefix:{let t=e??we;return[we.prefix,t.decode(r)]}default:{if(e==null)throw Error("To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided");return[r[0],e.decode(r)]}}}s(xn,"parseCIDtoBytes");function En(r,e,t){let{prefix:n}=t;if(n!==L.prefix)throw Error(`Cannot string encode V0 in ${t.name} encoding`);let i=e.get(n);if(i==null){let o=t.encode(r).slice(1);return e.set(n,o),o}else return i}s(En,"toStringV0");function Un(r,e,t){let{prefix:n}=t,i=e.get(n);if(i==null){let o=t.encode(r);return e.set(n,o),o}else return i}s(Un,"toStringV1");var Se=112,Sn=18;function rt(r,e,t){let n=ue(r),i=n+ue(e),o=new Uint8Array(i+t.byteLength);return pe(r,o,0),pe(e,o,n),o.set(t,i),o}s(rt,"encodeCID");var Cn=Symbol.for("@ipld/js-cid/CID");var Er={...dr,...cr,...mr,...ar,...yr,...ur,...fr,...lr,...gr,...pr},gs={...wr,...Ar};function nt(r,e,t,n){return{name:r,prefix:e,encoder:{name:r,prefix:e,encode:t},decoder:{decode:n}}}s(nt,"createCodec");var tt=nt("utf8","u",r=>"u"+new TextDecoder("utf8").decode(r),r=>new TextEncoder().encode(r.substring(1))),Ur=nt("ascii","a",r=>{let e="a";for(let t=0;t<r.length;t++)e+=String.fromCharCode(r[t]);return e},r=>{r=r.substring(1);let e=be(r.length);for(let t=0;t<r.length;t++)e[t]=r.charCodeAt(t);return e}),Pn={utf8:tt,"utf-8":tt,hex:Er.base16,latin1:Ur,ascii:Ur,binary:Ur,...Er},Oe=Pn;function h(r,e="utf8"){let t=Oe[e];if(t==null)throw new Error(`Unsupported encoding "${e}"`);return t.decoder.decode(`${t.prefix}${r}`)}s(h,"fromString");function u(r,e="utf8"){let t=Oe[e];if(t==null)throw new Error(`Unsupported encoding "${e}"`);return t.encoder.encode(r).substring(1)}s(u,"toString");var ut={};E(ut,{AES:()=>A,decryptBytes:()=>pt,importAesKey:()=>me});var H="did:key:z",ee="Ed25519",Ce="X25519",Bn="X25519",W=32,it={sign:["sign","verify"],encrypt:["deriveKey","deriveBits"]},X="RSA-OAEP",Y="RSASSA-PKCS1-v1_5",ze="SHA-256",He=new Uint8Array([0,245,2]),Ge=2048,re="SHA-256",M=8,N="AES-GCM",B=N,R=256,S=12,Fe=new Uint8Array([234,1]),Ve=new Uint8Array([237,1]),Pe={RSA:"rsa",Edwards:"ed25519",BLS:"bls12-381"},je="rsa-exchange-key",We="rsa-write-key",Be="ecc-exchange",ve="ecc-write",x=16;var Te=globalThis.crypto,st=new Error("Invalid key use. Please use 'encryption' or 'signing"),vn=new Error("Max must be less than 256 and greater than 0");function m(r){return new Uint8Array(r).buffer}s(m,"toArrayBuffer");async function ot(r){return new Uint8Array(await Te.subtle.digest("sha-256",r.buffer))}s(ot,"sha256");function v(r){let e=h(r,"base64pad");return m(e)}s(v,"base64ToArrBuf");var te=s((r,e)=>{if(typeof r=="string")return e(r);if(typeof r=="object"&&r.byteLength!==void 0)return new Uint8Array(r).buffer;throw new Error("Improper value. Must be a string, ArrayBuffer, Uint8Array")},"normalizeToBuf");function Xe(r){return te(r,v)}s(Xe,"normalizeBase64ToBuf");var Tn=s(r=>te(r,e=>at(e,8)),"normalizeUtf8ToBuf");function at(r,e){let t=e===8?new Uint8Array(r.length):new Uint16Array(r.length);for(let n=0,i=r.length;n<i;n++)t[n]=r.charCodeAt(n);return t.buffer}s(at,"strToArrBuf");var Dn=s(r=>te(r,e=>at(e,16)),"normalizeUtf16ToBuf");function $(r,e){return e===8?Tn(r):Dn(r)}s($,"normalizeUnicodeToBuf");function Ye(r){if(!r.startsWith(H))throw new Error("Please use a base58-encoded DID formatted `did:key:z...`");let e=""+r.substring(H.length),t=h(e,"base58btc"),{keyBuffer:n,type:i}=_n(m(t));return{publicKey:new Uint8Array(n),type:i}}s(Ye,"didToPublicKey");function _n(r){if(le(r,m(He)))return{keyBuffer:r.slice(He.byteLength),type:Pe.RSA};if(le(r,m(Ve)))return{keyBuffer:r.slice(Ve.byteLength),type:Pe.Edwards};if(le(r,m(Fe)))return{keyBuffer:r.slice(Fe.byteLength),type:Pe.BLS};throw new Error("Unsupported key algorithm. Try using RSA.")}s(_n,"parseMagicBytes");var In={equal:s((r,e)=>{let t=new Uint8Array(r),n=new Uint8Array(e);if(t.length!==n.length)return!1;for(let i=0;i<t.length;i++)if(t[i]!==n[i])return!1;return!0},"equal")};function le(r,e){return In.equal(e,r.slice(0,e.byteLength))}s(le,"hasPrefix");function b(r){return u(r instanceof ArrayBuffer?new Uint8Array(r):r,"base64pad")}s(b,"toBase64");async function De(r,e){let t=typeof r=="string"?v(r):m(r);return Te.subtle.importKey("raw",t,{name:e?.alg||B,length:e?.length||R},!0,["encrypt","decrypt"])}s(De,"importKey");function $e(r,{max:e}={max:255}){if(e<1||e>255)throw vn;let t=new Uint8Array(r);if(e===255)return Te.getRandomValues(t),t.buffer;let n=0,i=e+1,o=Math.floor(256/i)*i,a=new Uint8Array(1);for(;n<t.length;)Te.getRandomValues(a),a[0]<o&&(t[n]=a[0]%i,n++);return t.buffer}s($e,"randomBuf");function G(r,e){let t=r instanceof ArrayBuffer?new Uint8Array(r):new Uint8Array(m(r)),n=e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(m(e)),i=new Uint8Array(t.length+n.length);return i.set(t),i.set(n,t.length),i.buffer}s(G,"joinBufs");async function Sr(r,e,t,n=!1){let i=n;if(!Object.values(Z).includes(t))throw new Error("invalid key use");let o=t==="exchange"?X:Y,a=t==="exchange"?["encrypt","decrypt"]:["sign","verify"];return Te.subtle.generateKey({name:o,modulusLength:r,publicExponent:kn(),hash:{name:e}},i,a)}s(Sr,"makeRSAKeypair");function kn(){return new Uint8Array([1,0,1])}s(kn,"publicExponent");var _=globalThis.crypto,Je={keyTypes:{"bls12-381":{magicBytes:new Uint8Array([234,1]),verify:s(()=>{throw new Error("Not implemented")},"verify")},rsa:{magicBytes:new Uint8Array([0,245,2]),verify:ct},ed25519:{magicBytes:new Uint8Array([237,1]),verify:yt}}};async function Xs({message:r,did:e,signature:t}){let n=Ln(e),i=qe(e).publicKey;if(n==="ed25519")return yt({message:r,publicKey:i,signature:t});if(n==="rsa")return ct({message:r,publicKey:i,signature:t});throw new Error("Unknown key format")}s(Xs,"verify");function Ln(r){try{let{type:e}=qe(r);return e}catch{return"unknown"}}s(Ln,"getAlgorithm");async function yt({message:r,publicKey:e,signature:t}){let n=r;typeof r=="string"&&(n=h(r,"utf8"));let i=t;typeof t=="string"&&(i=h(t,"base64pad"));let o=e;if(typeof e=="string"&&(o=qe(e).publicKey),o.length!==32)throw new Error(`Invalid Ed25519 public key size: ${o.length} bytes, expected 32`);if(i.length!==64)throw new Error(`Invalid Ed25519 signature size: ${i.length} bytes, expected 64`);let a=await _.subtle.importKey("raw",m(o),{name:"Ed25519"},!1,["verify"]);return _.subtle.verify({name:"Ed25519"},a,m(i),m(n))}s(yt,"ed25519Verify");async function ct({message:r,publicKey:e,signature:t}){let n=e;return typeof e=="string"&&(n=qe(e).publicKey),F.verify(r,t,await _.subtle.importKey("spki",m(n),{name:Y,hash:ze},!1,["verify"]),8)}s(ct,"rsaVerify");var F={verify:s(async function(e,t,n,i=M,o=re){return _.subtle.verify({name:Y,saltLength:x},typeof n=="string"?await _e(n,o,"signing"):n,Xe(t),$(e,i))},"rsaVerify"),sign:s(async function(e,t,n=M){return _.subtle.sign({name:Y,saltLength:x},t,$(e,n))},"sign"),encrypt:s(async function(e,t,n=M,i=re){let o;return typeof t=="string"?o=await _e(t,i,"exchange"):o=t instanceof Uint8Array?await _e(m(t),i,"exchange"):t,_.subtle.encrypt({name:X},o,$(e,n))},"rsaEncrypt"),decrypt:s(async function(e,t){let n=ge(t)?t:await On(t,["decrypt"]),i;typeof e=="string"?i=h(e,"base64pad"):i=e;let o=await _.subtle.decrypt({name:X},n,i instanceof Uint8Array?m(i):i);return new Uint8Array(o)},"rsaDecrypt")};async function _e(r,e,t){Mn(t);let n=t==="exchange"?X:Y,i=t==="exchange"?["encrypt"]:["verify"],o=typeof r=="string"?v(Rn(r)):r;return _.subtle.importKey("spki",o,{name:n,hash:{name:e}},!0,i)}s(_e,"rsaImportPublicKey");function Mn(r){Nn(r,["signing","exchange"],st)}s(Mn,"checkValidKeyUse");function Nn(r,e,t){if(!e.some(i=>i===r))throw t}s(Nn,"checkValid");function Rn(r){return r.replace(`-----BEGIN PUBLIC KEY-----
|
|
1
|
+
"use strict";(()=>{var kr=Object.defineProperty;var s=(t,e)=>kr(t,"name",{value:e,configurable:!0});var E=(t,e)=>{for(var r in e)kr(t,r,{get:e[r],enumerable:!0})};var Lr=(n=>(n[n.B1024=1024]="B1024",n[n.B2048=2048]="B2048",n[n.B4096=4096]="B4096",n))(Lr||{}),Mr=(n=>(n[n.B128=128]="B128",n[n.B192=192]="B192",n[n.B256=256]="B256",n))(Mr||{}),Rr=(r=>(r.ECC="ecc",r.RSA="rsa",r))(Rr||{}),Je=(i=>(i.SHA_1="SHA-1",i.SHA_256="SHA-256",i.SHA_384="SHA-384",i.SHA_512="SHA-512",i))(Je||{}),qe=(i=>(i.P_256="P-256",i.P_384="P-384",i.P_521="P-521",i.X25519="X25519",i))(qe||{}),Z=(n=>(n.Sign="signing",n.Exchange="exchange",n.Write="write",n))(Z||{}),Ze=(r=>(r[r.B8=8]="B8",r[r.B16=16]="B16",r))(Ze||{});var At={};E(At,{AES_GCM:()=>M,BASE58_DID_PREFIX:()=>H,BLS_DID_PREFIX:()=>Fe,DEFAULT_CHAR_SIZE:()=>L,DEFAULT_ECC_EXCHANGE:()=>Ur,DEFAULT_ECC_EXCHANGE_CURVE:()=>vn,DEFAULT_ECC_WRITE:()=>Sr,DEFAULT_HASH_ALGORITHM:()=>re,DEFAULT_RSA_EXCHANGE:()=>xr,DEFAULT_RSA_SIZE:()=>He,DEFAULT_RSA_WRITE:()=>Er,DEFAULT_SYMM_ALGORITHM:()=>B,DEFAULT_SYMM_LENGTH:()=>R,ECC_EXCHANGE_ALGORITHM:()=>Pe,ECC_WRITE_ALGORITHM:()=>ee,EDWARDS_DID_PREFIX:()=>Ge,IV_LENGTH:()=>S,KEY_TYPE:()=>Be,KEY_USE:()=>ot,RSA_ALGORITHM:()=>X,RSA_DID_PREFIX:()=>ze,RSA_HASHING_ALGORITHM:()=>Oe,RSA_SIGN_ALGORITHM:()=>Y,RsaKeys:()=>ie,SALT_LENGTH:()=>x,X25519_PUBLIC_KEY_LENGTH:()=>W,encrypt:()=>Yn,encryptKeyTo:()=>Ae,encryptTo:()=>ht,getPublicKeyAsArrayBuffer:()=>J,getPublicKeyAsUint8Array:()=>Br,publicKeyToDid:()=>_e,verify:()=>Xn});function we(t=0){return new Uint8Array(t)}s(we,"allocUnsafe");function Qe(t,e){e==null&&(e=t.reduce((i,o)=>i+o.length,0));let r=we(e),n=0;for(let i of t)r.set(i,n),n+=i.length;return r}s(Qe,"concat");var ir={};E(ir,{base10:()=>Ct});var di=new Uint8Array(0);function Or(t,e){if(t===e)return!0;if(t.byteLength!==e.byteLength)return!1;for(let r=0;r<t.byteLength;r++)if(t[r]!==e[r])return!1;return!0}s(Or,"equals");function z(t){if(t instanceof Uint8Array&&t.constructor.name==="Uint8Array")return t;if(t instanceof ArrayBuffer)return new Uint8Array(t);if(ArrayBuffer.isView(t))return new Uint8Array(t.buffer,t.byteOffset,t.byteLength);throw new Error("Unknown type, must be binary type")}s(z,"coerce");function zr(t){return new TextEncoder().encode(t)}s(zr,"fromString");function Hr(t){return new TextDecoder().decode(t)}s(Hr,"toString");function wt(t,e){if(t.length>=255)throw new TypeError("Alphabet too long");for(var r=new Uint8Array(256),n=0;n<r.length;n++)r[n]=255;for(var i=0;i<t.length;i++){var o=t.charAt(i),a=o.charCodeAt(0);if(r[a]!==255)throw new TypeError(o+" is ambiguous");r[a]=i}var y=t.length,c=t.charAt(0),l=Math.log(y)/Math.log(256),f=Math.log(256)/Math.log(y);function K(p){if(p instanceof Uint8Array||(ArrayBuffer.isView(p)?p=new Uint8Array(p.buffer,p.byteOffset,p.byteLength):Array.isArray(p)&&(p=Uint8Array.from(p))),!(p instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(p.length===0)return"";for(var g=0,P=0,U=0,T=p.length;U!==T&&p[U]===0;)U++,g++;for(var w=(T-U)*f+1>>>0,D=new Uint8Array(w);U!==T;){for(var N=p[U],V=0,I=w-1;(N!==0||V<P)&&I!==-1;I--,V++)N+=256*D[I]>>>0,D[I]=N%y>>>0,N=N/y>>>0;if(N!==0)throw new Error("Non-zero carry");P=V,U++}for(var O=w-P;O!==w&&D[O]===0;)O++;for(var ke=c.repeat(g);O<w;++O)ke+=t.charAt(D[O]);return ke}s(K,"encode");function C(p){if(typeof p!="string")throw new TypeError("Expected String");if(p.length===0)return new Uint8Array;var g=0;if(p[g]!==" "){for(var P=0,U=0;p[g]===c;)P++,g++;for(var T=(p.length-g)*l+1>>>0,w=new Uint8Array(T);p[g];){var D=r[p.charCodeAt(g)];if(D===255)return;for(var N=0,V=T-1;(D!==0||N<U)&&V!==-1;V--,N++)D+=y*w[V]>>>0,w[V]=D%256>>>0,D=D/256>>>0;if(D!==0)throw new Error("Non-zero carry");U=N,g++}if(p[g]!==" "){for(var I=T-U;I!==T&&w[I]===0;)I++;for(var O=new Uint8Array(P+(T-I)),ke=P;I!==T;)O[ke++]=w[I++];return O}}}s(C,"decodeUnsafe");function ae(p){var g=C(p);if(g)return g;throw new Error(`Non-${e} character`)}return s(ae,"decode"),{encode:K,decodeUnsafe:C,decode:ae}}s(wt,"base");var xt=wt,Et=xt,Gr=Et;var er=class{static{s(this,"Encoder")}name;prefix;baseEncode;constructor(e,r,n){this.name=e,this.prefix=r,this.baseEncode=n}encode(e){if(e instanceof Uint8Array)return`${this.prefix}${this.baseEncode(e)}`;throw Error("Unknown type, must be binary type")}},rr=class{static{s(this,"Decoder")}name;prefix;baseDecode;prefixCodePoint;constructor(e,r,n){this.name=e,this.prefix=r;let i=r.codePointAt(0);if(i===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=i,this.baseDecode=n}decode(e){if(typeof e=="string"){if(e.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(e)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(e.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(e){return Vr(this,e)}},tr=class{static{s(this,"ComposedDecoder")}decoders;constructor(e){this.decoders=e}or(e){return Vr(this,e)}decode(e){let r=e[0],n=this.decoders[r];if(n!=null)return n.decode(e);throw RangeError(`Unable to decode multibase string ${JSON.stringify(e)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}};function Vr(t,e){return new tr({...t.decoders??{[t.prefix]:t},...e.decoders??{[e.prefix]:e}})}s(Vr,"or");var nr=class{static{s(this,"Codec")}name;prefix;baseEncode;baseDecode;encoder;decoder;constructor(e,r,n,i){this.name=e,this.prefix=r,this.baseEncode=n,this.baseDecode=i,this.encoder=new er(e,r,n),this.decoder=new rr(e,r,i)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}};function ye({name:t,prefix:e,encode:r,decode:n}){return new nr(t,e,r,n)}s(ye,"from");function j({name:t,prefix:e,alphabet:r}){let{encode:n,decode:i}=Gr(r,t);return ye({prefix:e,name:t,encode:n,decode:s(o=>z(i(o)),"decode")})}s(j,"baseX");function Ut(t,e,r,n){let i={};for(let f=0;f<e.length;++f)i[e[f]]=f;let o=t.length;for(;t[o-1]==="=";)--o;let a=new Uint8Array(o*r/8|0),y=0,c=0,l=0;for(let f=0;f<o;++f){let K=i[t[f]];if(K===void 0)throw new SyntaxError(`Non-${n} character`);c=c<<r|K,y+=r,y>=8&&(y-=8,a[l++]=255&c>>y)}if(y>=r||(255&c<<8-y)!==0)throw new SyntaxError("Unexpected end of data");return a}s(Ut,"decode");function St(t,e,r){let n=e[e.length-1]==="=",i=(1<<r)-1,o="",a=0,y=0;for(let c=0;c<t.length;++c)for(y=y<<8|t[c],a+=8;a>r;)a-=r,o+=e[i&y>>a];if(a!==0&&(o+=e[i&y<<r-a]),n)for(;(o.length*r&7)!==0;)o+="=";return o}s(St,"encode");function d({name:t,prefix:e,bitsPerChar:r,alphabet:n}){return ye({prefix:e,name:t,encode(i){return St(i,n,r)},decode(i){return Ut(i,n,r,t)}})}s(d,"rfc4648");var Ct=j({prefix:"9",name:"base10",alphabet:"0123456789"});var sr={};E(sr,{base16:()=>Pt,base16upper:()=>Bt});var Pt=d({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),Bt=d({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var or={};E(or,{base2:()=>vt});var vt=d({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var ar={};E(ar,{base256emoji:()=>Nt});var jr=Array.from("\u{1F680}\u{1FA90}\u2604\u{1F6F0}\u{1F30C}\u{1F311}\u{1F312}\u{1F313}\u{1F314}\u{1F315}\u{1F316}\u{1F317}\u{1F318}\u{1F30D}\u{1F30F}\u{1F30E}\u{1F409}\u2600\u{1F4BB}\u{1F5A5}\u{1F4BE}\u{1F4BF}\u{1F602}\u2764\u{1F60D}\u{1F923}\u{1F60A}\u{1F64F}\u{1F495}\u{1F62D}\u{1F618}\u{1F44D}\u{1F605}\u{1F44F}\u{1F601}\u{1F525}\u{1F970}\u{1F494}\u{1F496}\u{1F499}\u{1F622}\u{1F914}\u{1F606}\u{1F644}\u{1F4AA}\u{1F609}\u263A\u{1F44C}\u{1F917}\u{1F49C}\u{1F614}\u{1F60E}\u{1F607}\u{1F339}\u{1F926}\u{1F389}\u{1F49E}\u270C\u2728\u{1F937}\u{1F631}\u{1F60C}\u{1F338}\u{1F64C}\u{1F60B}\u{1F497}\u{1F49A}\u{1F60F}\u{1F49B}\u{1F642}\u{1F493}\u{1F929}\u{1F604}\u{1F600}\u{1F5A4}\u{1F603}\u{1F4AF}\u{1F648}\u{1F447}\u{1F3B6}\u{1F612}\u{1F92D}\u2763\u{1F61C}\u{1F48B}\u{1F440}\u{1F62A}\u{1F611}\u{1F4A5}\u{1F64B}\u{1F61E}\u{1F629}\u{1F621}\u{1F92A}\u{1F44A}\u{1F973}\u{1F625}\u{1F924}\u{1F449}\u{1F483}\u{1F633}\u270B\u{1F61A}\u{1F61D}\u{1F634}\u{1F31F}\u{1F62C}\u{1F643}\u{1F340}\u{1F337}\u{1F63B}\u{1F613}\u2B50\u2705\u{1F97A}\u{1F308}\u{1F608}\u{1F918}\u{1F4A6}\u2714\u{1F623}\u{1F3C3}\u{1F490}\u2639\u{1F38A}\u{1F498}\u{1F620}\u261D\u{1F615}\u{1F33A}\u{1F382}\u{1F33B}\u{1F610}\u{1F595}\u{1F49D}\u{1F64A}\u{1F639}\u{1F5E3}\u{1F4AB}\u{1F480}\u{1F451}\u{1F3B5}\u{1F91E}\u{1F61B}\u{1F534}\u{1F624}\u{1F33C}\u{1F62B}\u26BD\u{1F919}\u2615\u{1F3C6}\u{1F92B}\u{1F448}\u{1F62E}\u{1F646}\u{1F37B}\u{1F343}\u{1F436}\u{1F481}\u{1F632}\u{1F33F}\u{1F9E1}\u{1F381}\u26A1\u{1F31E}\u{1F388}\u274C\u270A\u{1F44B}\u{1F630}\u{1F928}\u{1F636}\u{1F91D}\u{1F6B6}\u{1F4B0}\u{1F353}\u{1F4A2}\u{1F91F}\u{1F641}\u{1F6A8}\u{1F4A8}\u{1F92C}\u2708\u{1F380}\u{1F37A}\u{1F913}\u{1F619}\u{1F49F}\u{1F331}\u{1F616}\u{1F476}\u{1F974}\u25B6\u27A1\u2753\u{1F48E}\u{1F4B8}\u2B07\u{1F628}\u{1F31A}\u{1F98B}\u{1F637}\u{1F57A}\u26A0\u{1F645}\u{1F61F}\u{1F635}\u{1F44E}\u{1F932}\u{1F920}\u{1F927}\u{1F4CC}\u{1F535}\u{1F485}\u{1F9D0}\u{1F43E}\u{1F352}\u{1F617}\u{1F911}\u{1F30A}\u{1F92F}\u{1F437}\u260E\u{1F4A7}\u{1F62F}\u{1F486}\u{1F446}\u{1F3A4}\u{1F647}\u{1F351}\u2744\u{1F334}\u{1F4A3}\u{1F438}\u{1F48C}\u{1F4CD}\u{1F940}\u{1F922}\u{1F445}\u{1F4A1}\u{1F4A9}\u{1F450}\u{1F4F8}\u{1F47B}\u{1F910}\u{1F92E}\u{1F3BC}\u{1F975}\u{1F6A9}\u{1F34E}\u{1F34A}\u{1F47C}\u{1F48D}\u{1F4E3}\u{1F942}"),Tt=jr.reduce((t,e,r)=>(t[r]=e,t),[]),Dt=jr.reduce((t,e,r)=>{let n=e.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${e}`);return t[n]=r,t},[]);function _t(t){return t.reduce((e,r)=>(e+=Tt[r],e),"")}s(_t,"encode");function It(t){let e=[];for(let r of t){let n=r.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${r}`);let i=Dt[n];if(i==null)throw new Error(`Non-base256emoji character: ${r}`);e.push(i)}return new Uint8Array(e)}s(It,"decode");var Nt=ye({prefix:"\u{1F680}",name:"base256emoji",encode:_t,decode:It});var yr={};E(yr,{base32:()=>ce,base32hex:()=>Rt,base32hexpad:()=>zt,base32hexpadupper:()=>Ht,base32hexupper:()=>Ot,base32pad:()=>Lt,base32padupper:()=>Mt,base32upper:()=>kt,base32z:()=>Ft});var ce=d({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),kt=d({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Lt=d({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Mt=d({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Rt=d({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Ot=d({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),zt=d({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Ht=d({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Ft=d({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var cr={};E(cr,{base36:()=>xe,base36upper:()=>Gt});var xe=j({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),Gt=j({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var pr={};E(pr,{base58btc:()=>k,base58flickr:()=>Vt});var k=j({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),Vt=j({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var ur={};E(ur,{base64:()=>jt,base64pad:()=>Wt,base64url:()=>Xt,base64urlpad:()=>Yt});var jt=d({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),Wt=d({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),Xt=d({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),Yt=d({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var fr={};E(fr,{base8:()=>$t});var $t=d({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var lr={};E(lr,{identity:()=>Jt});var Jt=ye({prefix:"\0",name:"identity",encode:s(t=>Hr(t),"encode"),decode:s(t=>zr(t),"decode")});var Mi=new TextEncoder,Ri=new TextDecoder;var mr={};E(mr,{identity:()=>bn});var Qt=Yr,Wr=128,en=127,rn=~en,tn=Math.pow(2,31);function Yr(t,e,r){e=e||[],r=r||0;for(var n=r;t>=tn;)e[r++]=t&255|Wr,t/=128;for(;t&rn;)e[r++]=t&255|Wr,t>>>=7;return e[r]=t|0,Yr.bytes=r-n+1,e}s(Yr,"encode");var nn=gr,sn=128,Xr=127;function gr(t,n){var r=0,n=n||0,i=0,o=n,a,y=t.length;do{if(o>=y)throw gr.bytes=0,new RangeError("Could not decode varint");a=t[o++],r+=i<28?(a&Xr)<<i:(a&Xr)*Math.pow(2,i),i+=7}while(a>=sn);return gr.bytes=o-n,r}s(gr,"read");var on=Math.pow(2,7),an=Math.pow(2,14),yn=Math.pow(2,21),cn=Math.pow(2,28),pn=Math.pow(2,35),un=Math.pow(2,42),fn=Math.pow(2,49),ln=Math.pow(2,56),gn=Math.pow(2,63),mn=s(function(t){return t<on?1:t<an?2:t<yn?3:t<cn?4:t<pn?5:t<un?6:t<fn?7:t<ln?8:t<gn?9:10},"length"),dn={encode:Qt,decode:nn,encodingLength:mn},hn=dn,Ee=hn;function Ue(t,e=0){return[Ee.decode(t,e),Ee.decode.bytes]}s(Ue,"decode");function pe(t,e,r=0){return Ee.encode(t,e,r),e}s(pe,"encodeTo");function ue(t){return Ee.encodingLength(t)}s(ue,"encodingLength");function Q(t,e){let r=e.byteLength,n=ue(t),i=n+ue(r),o=new Uint8Array(i+r);return pe(t,o,0),pe(r,o,n),o.set(e,i),new fe(t,r,e,o)}s(Q,"create");function $r(t){let e=z(t),[r,n]=Ue(e),[i,o]=Ue(e.subarray(n)),a=e.subarray(n+o);if(a.byteLength!==i)throw new Error("Incorrect length");return new fe(r,i,a,e)}s($r,"decode");function Jr(t,e){if(t===e)return!0;{let r=e;return t.code===r.code&&t.size===r.size&&r.bytes instanceof Uint8Array&&Or(t.bytes,r.bytes)}}s(Jr,"equals");var fe=class{static{s(this,"Digest")}code;size;digest;bytes;constructor(e,r,n,i){this.code=e,this.size=r,this.digest=n,this.bytes=i}};var qr=0,An="identity",Zr=z;function Kn(t){return Q(qr,Zr(t))}s(Kn,"digest");var bn={code:qr,name:An,encode:Zr,digest:Kn};var Ar={};E(Ar,{sha256:()=>wn,sha512:()=>xn});function hr({name:t,code:e,encode:r}){return new dr(t,e,r)}s(hr,"from");var dr=class{static{s(this,"Hasher")}name;code;encode;constructor(e,r,n){this.name=e,this.code=r,this.encode=n}digest(e){if(e instanceof Uint8Array){let r=this.encode(e);return r instanceof Uint8Array?Q(this.code,r):r.then(n=>Q(this.code,n))}else throw Error("Unknown type, must be binary type")}};function et(t){return async e=>new Uint8Array(await crypto.subtle.digest(t,e))}s(et,"sha");var wn=hr({name:"sha2-256",code:18,encode:et("SHA-256")}),xn=hr({name:"sha2-512",code:19,encode:et("SHA-512")});function rt(t,e){let{bytes:r,version:n}=t;return n===0?Un(r,Kr(t),e??k.encoder):Sn(r,Kr(t),e??ce.encoder)}s(rt,"format");var tt=new WeakMap;function Kr(t){let e=tt.get(t);if(e==null){let r=new Map;return tt.set(t,r),r}return e}s(Kr,"baseCache");var Me=class t{static{s(this,"CID")}code;version;multihash;bytes;"/";constructor(e,r,n,i){this.code=r,this.version=e,this.multihash=n,this.bytes=i,this["/"]=i}get asCID(){return this}get byteOffset(){return this.bytes.byteOffset}get byteLength(){return this.bytes.byteLength}toV0(){switch(this.version){case 0:return this;case 1:{let{code:e,multihash:r}=this;if(e!==Ce)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(r.code!==Cn)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return t.createV0(r)}default:throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)}}toV1(){switch(this.version){case 0:{let{code:e,digest:r}=this.multihash,n=Q(e,r);return t.createV1(this.code,n)}case 1:return this;default:throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`)}}equals(e){return t.equals(this,e)}static equals(e,r){let n=r;return n!=null&&e.code===n.code&&e.version===n.version&&Jr(e.multihash,n.multihash)}toString(e){return rt(this,e)}toJSON(){return{"/":rt(this)}}link(){return this}[Symbol.toStringTag]="CID";[Symbol.for("nodejs.util.inspect.custom")](){return`CID(${this.toString()})`}static asCID(e){if(e==null)return null;let r=e;if(r instanceof t)return r;if(r["/"]!=null&&r["/"]===r.bytes||r.asCID===r){let{version:n,code:i,multihash:o,bytes:a}=r;return new t(n,i,o,a??nt(n,i,o.bytes))}else if(r[Pn]===!0){let{version:n,multihash:i,code:o}=r,a=$r(i);return t.create(n,o,a)}else return null}static create(e,r,n){if(typeof r!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(e){case 0:{if(r!==Ce)throw new Error(`Version 0 CID must use dag-pb (code: ${Ce}) block encoding`);return new t(e,r,n,n.bytes)}case 1:{let i=nt(e,r,n.bytes);return new t(e,r,n,i)}default:throw new Error("Invalid version")}}static createV0(e){return t.create(0,Ce,e)}static createV1(e,r){return t.create(1,e,r)}static decode(e){let[r,n]=t.decodeFirst(e);if(n.length!==0)throw new Error("Incorrect length");return r}static decodeFirst(e){let r=t.inspectBytes(e),n=r.size-r.multihashSize,i=z(e.subarray(n,n+r.multihashSize));if(i.byteLength!==r.multihashSize)throw new Error("Incorrect length");let o=i.subarray(r.multihashSize-r.digestSize),a=new fe(r.multihashCode,r.digestSize,o,i);return[r.version===0?t.createV0(a):t.createV1(r.codec,a),e.subarray(r.size)]}static inspectBytes(e){let r=0,n=s(()=>{let[K,C]=Ue(e.subarray(r));return r+=C,K},"next"),i=n(),o=Ce;if(i===18?(i=0,r=0):o=n(),i!==0&&i!==1)throw new RangeError(`Invalid CID version ${i}`);let a=r,y=n(),c=n(),l=r+c,f=l-a;return{version:i,codec:o,multihashCode:y,digestSize:c,multihashSize:f,size:l}}static parse(e,r){let[n,i]=En(e,r),o=t.decode(i);if(o.version===0&&e[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return Kr(o).set(n,e),o}};function En(t,e){switch(t[0]){case"Q":{let r=e??k;return[k.prefix,r.decode(`${k.prefix}${t}`)]}case k.prefix:{let r=e??k;return[k.prefix,r.decode(t)]}case ce.prefix:{let r=e??ce;return[ce.prefix,r.decode(t)]}case xe.prefix:{let r=e??xe;return[xe.prefix,r.decode(t)]}default:{if(e==null)throw Error("To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided");return[t[0],e.decode(t)]}}}s(En,"parseCIDtoBytes");function Un(t,e,r){let{prefix:n}=r;if(n!==k.prefix)throw Error(`Cannot string encode V0 in ${r.name} encoding`);let i=e.get(n);if(i==null){let o=r.encode(t).slice(1);return e.set(n,o),o}else return i}s(Un,"toStringV0");function Sn(t,e,r){let{prefix:n}=r,i=e.get(n);if(i==null){let o=r.encode(t);return e.set(n,o),o}else return i}s(Sn,"toStringV1");var Ce=112,Cn=18;function nt(t,e,r){let n=ue(t),i=n+ue(e),o=new Uint8Array(i+r.byteLength);return pe(t,o,0),pe(e,o,n),o.set(r,i),o}s(nt,"encodeCID");var Pn=Symbol.for("@ipld/js-cid/CID");var br={...lr,...or,...fr,...ir,...sr,...yr,...cr,...pr,...ur,...ar},ms={...Ar,...mr};function st(t,e,r,n){return{name:t,prefix:e,encoder:{name:t,prefix:e,encode:r},decoder:{decode:n}}}s(st,"createCodec");var it=st("utf8","u",t=>"u"+new TextDecoder("utf8").decode(t),t=>new TextEncoder().encode(t.substring(1))),wr=st("ascii","a",t=>{let e="a";for(let r=0;r<t.length;r++)e+=String.fromCharCode(t[r]);return e},t=>{t=t.substring(1);let e=we(t.length);for(let r=0;r<t.length;r++)e[r]=t.charCodeAt(r);return e}),Bn={utf8:it,"utf-8":it,hex:br.base16,latin1:wr,ascii:wr,binary:wr,...br},Re=Bn;function h(t,e="utf8"){let r=Re[e];if(r==null)throw new Error(`Unsupported encoding "${e}"`);return r.decoder.decode(`${r.prefix}${t}`)}s(h,"fromString");function u(t,e="utf8"){let r=Re[e];if(r==null)throw new Error(`Unsupported encoding "${e}"`);return r.encoder.encode(t).substring(1)}s(u,"toString");var lt={};E(lt,{AES:()=>A,decryptBytes:()=>ft,importAesKey:()=>me});var H="did:key:z",ee="Ed25519",Pe="X25519",vn="X25519",W=32,ot={sign:["sign","verify"],encrypt:["deriveKey","deriveBits"]},X="RSA-OAEP",Y="RSASSA-PKCS1-v1_5",Oe="SHA-256",ze=new Uint8Array([0,245,2]),He=2048,re="SHA-256",L=8,M="AES-GCM",B=M,R=256,S=12,Fe=new Uint8Array([234,1]),Ge=new Uint8Array([237,1]),Be={RSA:"rsa",Edwards:"ed25519",BLS:"bls12-381"},xr="rsa-exchange-key",Er="rsa-write-key",Ur="ecc-exchange",Sr="ecc-write",x=16;var ve=globalThis.crypto,at=new Error("Invalid key use. Please use 'encryption' or 'signing"),Tn=new Error("Max must be less than 256 and greater than 0");function m(t){return new Uint8Array(t).buffer}s(m,"toArrayBuffer");async function yt(t){return new Uint8Array(await ve.subtle.digest("sha-256",t.buffer))}s(yt,"sha256");function v(t){let e=h(t,"base64pad");return m(e)}s(v,"base64ToArrBuf");var te=s((t,e)=>{if(typeof t=="string")return e(t);if(typeof t=="object"&&t.byteLength!==void 0)return new Uint8Array(t).buffer;throw new Error("Improper value. Must be a string, ArrayBuffer, Uint8Array")},"normalizeToBuf");function Ve(t){return te(t,v)}s(Ve,"normalizeBase64ToBuf");var Dn=s(t=>te(t,e=>ct(e,8)),"normalizeUtf8ToBuf");function ct(t,e){let r=e===8?new Uint8Array(t.length):new Uint16Array(t.length);for(let n=0,i=t.length;n<i;n++)r[n]=t.charCodeAt(n);return r.buffer}s(ct,"strToArrBuf");var _n=s(t=>te(t,e=>ct(e,16)),"normalizeUtf16ToBuf");function $(t,e){return e===8?Dn(t):_n(t)}s($,"normalizeUnicodeToBuf");function je(t){if(!t.startsWith(H))throw new Error("Please use a base58-encoded DID formatted `did:key:z...`");let e=""+t.substring(H.length),r=h(e,"base58btc"),{keyBuffer:n,type:i}=In(m(r));return{publicKey:new Uint8Array(n),type:i}}s(je,"didToPublicKey");function In(t){if(le(t,m(ze)))return{keyBuffer:t.slice(ze.byteLength),type:Be.RSA};if(le(t,m(Ge)))return{keyBuffer:t.slice(Ge.byteLength),type:Be.Edwards};if(le(t,m(Fe)))return{keyBuffer:t.slice(Fe.byteLength),type:Be.BLS};throw new Error("Unsupported key algorithm. Try using RSA.")}s(In,"parseMagicBytes");var Nn={equal:s((t,e)=>{let r=new Uint8Array(t),n=new Uint8Array(e);if(r.length!==n.length)return!1;for(let i=0;i<r.length;i++)if(r[i]!==n[i])return!1;return!0},"equal")};function le(t,e){return Nn.equal(e,t.slice(0,e.byteLength))}s(le,"hasPrefix");function b(t){return u(t instanceof ArrayBuffer?new Uint8Array(t):t,"base64pad")}s(b,"toBase64");async function Te(t,e){let r=typeof t=="string"?v(t):m(t);return ve.subtle.importKey("raw",r,{name:e?.alg||B,length:e?.length||R},!0,["encrypt","decrypt"])}s(Te,"importKey");function We(t,{max:e}={max:255}){if(e<1||e>255)throw Tn;let r=new Uint8Array(t);if(e===255)return ve.getRandomValues(r),r.buffer;let n=0,i=e+1,o=Math.floor(256/i)*i,a=new Uint8Array(1);for(;n<r.length;)ve.getRandomValues(a),a[0]<o&&(r[n]=a[0]%i,n++);return r.buffer}s(We,"randomBuf");function F(t,e){let r=t instanceof ArrayBuffer?new Uint8Array(t):new Uint8Array(m(t)),n=e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(m(e)),i=new Uint8Array(r.length+n.length);return i.set(r),i.set(n,r.length),i.buffer}s(F,"joinBufs");async function Cr(t,e,r,n=!1){let i=n;if(!Object.values(Z).includes(r))throw new Error("invalid key use");let o=r==="exchange"?X:Y,a=r==="exchange"?["encrypt","decrypt"]:["sign","verify"];return ve.subtle.generateKey({name:o,modulusLength:t,publicExponent:kn(),hash:{name:e}},i,a)}s(Cr,"makeRSAKeypair");function kn(){return new Uint8Array([1,0,1])}s(kn,"publicExponent");var _=globalThis.crypto,Xe={keyTypes:{"bls12-381":{magicBytes:new Uint8Array([234,1]),verify:s(()=>{throw new Error("Not implemented")},"verify")},rsa:{magicBytes:new Uint8Array([0,245,2]),verify:ut},ed25519:{magicBytes:new Uint8Array([237,1]),verify:pt}}};async function Ys({message:t,did:e,signature:r}){let n=Ln(e),i=Ye(e).publicKey;if(n==="ed25519")return pt({message:t,publicKey:i,signature:r});if(n==="rsa")return ut({message:t,publicKey:i,signature:r});throw new Error("Unknown key format")}s(Ys,"verify");function Ln(t){try{let{type:e}=Ye(t);return e}catch{return"unknown"}}s(Ln,"getAlgorithm");async function pt({message:t,publicKey:e,signature:r}){let n=t;typeof t=="string"&&(n=h(t,"utf8"));let i=r;typeof r=="string"&&(i=h(r,"base64pad"));let o=e;if(typeof e=="string"&&(o=Ye(e).publicKey),o.length!==32)throw new Error(`Invalid Ed25519 public key size: ${o.length} bytes, expected 32`);if(i.length!==64)throw new Error(`Invalid Ed25519 signature size: ${i.length} bytes, expected 64`);let a=await _.subtle.importKey("raw",m(o),{name:"Ed25519"},!1,["verify"]);return _.subtle.verify({name:"Ed25519"},a,m(i),m(n))}s(pt,"ed25519Verify");async function ut({message:t,publicKey:e,signature:r}){let n=e;return typeof e=="string"&&(n=Ye(e).publicKey),G.verify(t,r,await _.subtle.importKey("spki",m(n),{name:Y,hash:Oe},!1,["verify"]),8)}s(ut,"rsaVerify");var G={verify:s(async function(e,r,n,i=L,o=re){return _.subtle.verify({name:Y,saltLength:x},typeof n=="string"?await De(n,o,"signing"):n,Ve(r),$(e,i))},"rsaVerify"),sign:s(async function(e,r,n=L){return _.subtle.sign({name:Y,saltLength:x},r,$(e,n))},"sign"),encrypt:s(async function(e,r,n=L,i=re){let o;return typeof r=="string"?o=await De(r,i,"exchange"):o=r instanceof Uint8Array?await De(m(r),i,"exchange"):r,_.subtle.encrypt({name:X},o,$(e,n))},"rsaEncrypt"),decrypt:s(async function(e,r){let n=ge(r)?r:await zn(r,["decrypt"]),i;typeof e=="string"?i=h(e,"base64pad"):i=e;let o=await _.subtle.decrypt({name:X},n,i instanceof Uint8Array?m(i):i);return new Uint8Array(o)},"rsaDecrypt")};async function De(t,e,r){Mn(r);let n=r==="exchange"?X:Y,i=r==="exchange"?["encrypt"]:["verify"],o=typeof t=="string"?v(On(t)):t;return _.subtle.importKey("spki",o,{name:n,hash:{name:e}},!0,i)}s(De,"rsaImportPublicKey");function Mn(t){Rn(t,["signing","exchange"],at)}s(Mn,"checkValidKeyUse");function Rn(t,e,r){if(!e.some(i=>i===t))throw r}s(Rn,"checkValid");function On(t){return t.replace(`-----BEGIN PUBLIC KEY-----
|
|
2
2
|
`,"").replace(`
|
|
3
|
-
-----END PUBLIC KEY-----`,"")}s(Rn,"stripKeyHeader");function ge(r){return Cr(r,"algorithm")&&Cr(r,"extractable")&&Cr(r,"type")}s(ge,"isCryptoKey");function Cr(r,e){return typeof r=="object"&&r!=null&&e in r}s(Cr,"hasProp");function On(r,e){return _.subtle.importKey("spki",r instanceof Uint8Array?m(r):r,{name:X,hash:ze},!1,e)}s(On,"importRsaKey");async function Ie(r,e="rsa"){let t;if(r instanceof CryptoKey){e=r.algorithm.name.includes("RSA")?"rsa":"ed25519";let o=e==="ed25519"?"raw":"spki",a=await _.subtle.exportKey(o,r);t=new Uint8Array(a)}else t=r;let n=Je.keyTypes[e]?.magicBytes;if(!n)throw new Error(`Key type '${e}' not supported, available types: ${Object.keys(Je.keyTypes).join(", ")}`);let i=tr([n,t]);return H+u(i,"base58btc")}s(Ie,"publicKeyToDid");function Ys(r){let e=r.substring(H.length),t=h(e,"base58btc"),n=Object.entries(Je.keyTypes).find(([o,a])=>le(t.buffer,a.magicBytes.buffer));if(!n)throw new Error("Invalid key type");let i=n[0];if(i!=="ed25519"&&i!=="rsa")throw new Error(`Unsupported key type: ${i}`);return i}s(Ys,"keyTypeFromDid");async function J(r){return r instanceof CryptoKey?await _.subtle.exportKey("spki",r):await _.subtle.exportKey("spki",r.publicKey)}s(J,"getPublicKeyAsArrayBuffer");async function Pr(r){let e=await J(r);return new Uint8Array(e)}s(Pr,"getPublicKeyAsUint8Array");function qe(r){if(!r.startsWith(H))throw new Error("Please use a base58-encoded DID formatted `did:key:z...`");let e=r.substring(H.length),t=h(e,"base58btc"),n=Object.entries(Je.keyTypes).find(([o,a])=>le(t.buffer,a.magicBytes.buffer));if(!n)throw new Error("Unsupported key algorithm.");let i=t.slice(n[1].magicBytes.length);return n[0]==="ed25519"&&i.length===44?{publicKey:i.slice(-32),type:n[0]}:{publicKey:i,type:n[0]}}s(qe,"didToPublicKey");var ne=globalThis.crypto,A={create(r={alg:B,length:R}){let e=r.length||R,t=r.alg||B;return ne.subtle.generateKey({name:t,length:e},!0,["encrypt","decrypt"])},export:Object.assign(async r=>{let e=await ne.subtle.exportKey("raw",r);return new Uint8Array(e)},{asString:s(async(r,e)=>{let t=await A.export(r);return e?u(t,e):b(t)},"asString")}),import(r){return me(typeof r=="string"?v(r):r)},async exportAsString(r){let e=await A.export(r);return b(e)},encrypt:zn,async decrypt(r,e,t){let n=ge(e)?e:await me(e),i=t?await ne.subtle.decrypt({name:N,iv:m(t)},n,typeof r=="string"?m(h(r)):r instanceof Uint8Array?m(r):r):await pt(r,n);return new Uint8Array(i)}};function me(r,e){return ne.subtle.importKey("raw",r instanceof Uint8Array?m(r):r,{name:N,length:e||256},!0,["encrypt","decrypt"])}s(me,"importAesKey");async function zn(r,e,t,n){let i=ge(e)?e:await me(e),o=n?await ne.subtle.encrypt({name:N,iv:m(n)},i,r instanceof ArrayBuffer?r:m(typeof r=="string"?h(r):r)):await Hn(r,i);return t&&t==="arraybuffer"?o:new Uint8Array(o)}s(zn,"encrypt");async function Hn(r,e,t){let n=$(r,t?.charsize??M),i=typeof e=="string"?await De(e,t):e,o=t?.iv||$e(S),a=await ne.subtle.encrypt({name:N,iv:o},i,n);return G(o,a)}s(Hn,"encryptBytes");async function pt(r,e,t){let n=Xe(r),i=typeof e=="string"?await De(e,t):e,o=n.slice(0,S),a=n.slice(S);return await ne.subtle.decrypt({name:B,iv:o},i,a)}s(pt,"decryptBytes");function Ze(r){return new Promise((e,t)=>{r.oncomplete=r.onsuccess=()=>e(r.result),r.onabort=r.onerror=()=>t(r.error)})}s(Ze,"promisifyRequest");function Gn(r,e){let t,n=s(()=>{if(t)return t;let i=indexedDB.open(r);return i.onupgradeneeded=()=>i.result.createObjectStore(e),t=Ze(i),t.then(o=>{o.onclose=()=>t=void 0},()=>{}),t},"getDB");return(i,o)=>n().then(a=>o(a.transaction(e,i).objectStore(e)))}s(Gn,"createStore");var Br;function vr(){return Br||(Br=Gn("keyval-store","keyval")),Br}s(vr,"defaultGetStore");function ke(r,e=vr()){return e("readonly",t=>Ze(t.get(r)))}s(ke,"get");function Tr(r,e,t=vr()){return t("readwrite",n=>(n.put(e,r),Ze(n.transaction)))}s(Tr,"set");function ft(r,e=vr()){return e("readwrite",t=>(r.forEach(n=>t.delete(n)),Ze(t.transaction)))}s(ft,"delMany");var de=class r{static{s(this,"AbstractKeys")}DID;exchangeKey;writeKey;hasPersisted;isSessionOnly;static EXCHANGE_KEY_NAME;static WRITE_KEY_NAME;static _instance;constructor(e){let{keys:t}=e;this.DID=e.did,this.exchangeKey=t.exchange,this.writeKey=t.write,this.hasPersisted=e.hasPersisted,this.isSessionOnly=!!e.isSessionOnly}publicExchangeKeyAsString(e){return this.publicExchangeKey.asString(e)}publicWriteKeyAsString(e){return this.publicWriteKey.asString(e)}get publicWriteKey(){let e=this.writeKey.publicKey;return Object.assign(e,{asString:s(async t=>{let n=await J(this.writeKey),i=new Uint8Array(n);return t?u(i,t):b(i)},"asString")})}get publicExchangeKey(){let e=this.exchangeKey.publicKey;return Object.assign(e,{asString:s(async t=>{let n=await J(this.exchangeKey),i=new Uint8Array(n);return t?u(i,t):b(i)},"asString")})}get privateWriteKey(){return this.writeKey.privateKey}get privateExchangeKey(){return this.exchangeKey.privateKey}get deviceName(){return r.deviceName(this.DID)}static deviceName(e){return Fn(e)}async persist(){if(this.isSessionOnly)return;let e=this.constructor.EXCHANGE_KEY_NAME,t=this.constructor.WRITE_KEY_NAME;await Promise.all([Tr(e,this.exchangeKey),Tr(t,this.writeKey)]),this.hasPersisted=!0}async delete(){await ft([this.constructor.EXCHANGE_KEY_NAME,this.constructor.WRITE_KEY_NAME]),this.hasPersisted=!1}async getDeviceName(){return r.deviceName(this.DID)}static _createExchangeKeys(e){throw new Error("The child should implement this")}static _createWriteKeys(e){throw new Error("The child should implement this")}static async create(e,t,n){let i=n?.exchangeKeys||await this._createExchangeKeys(t),o=n?.writeKeys||await this._createWriteKeys(t),a=await J(o),y=await Ie(new Uint8Array(a),this.TYPE==="ecc"?"ed25519":"rsa"),c=new this({keys:{exchange:i,write:o},did:y,hasPersisted:!1,isSessionOnly:!!e});return this._instance=c,c}static async exist(e={}){let t=await ke(e.writeKeyName||this.WRITE_KEY_NAME),n=await ke(e.encryptionKeyName||this.EXCHANGE_KEY_NAME);return!(!t||!n)}static async load(e={session:!1}){if(this._instance)return this._instance;let t=!0,n=await ke(e.encryptionKeyName||this.EXCHANGE_KEY_NAME),i=await ke(e.writeKeyName||this.WRITE_KEY_NAME);n||(t=!1,n=await this._createExchangeKeys(e.extractable)),i||(t=!1,i=await this._createWriteKeys(e.extractable));let o=await J(i),a=await Ie(new Uint8Array(o),this.TYPE==="ecc"?"ed25519":"rsa"),y=new this({keys:{exchange:n,write:i},did:a,hasPersisted:t,isSessionOnly:!!e.session});return this._instance=y,y}};async function lt(r,e){let{content:t,publicKey:n}=r,i=e||await A.create(),o=await A.encrypt(typeof t=="string"?h(t):t,typeof i=="string"?await A.import(i):i),a=await Dr({key:i,publicKey:n});return G(a,o)}s(lt,"encryptTo");lt.asString=async function(r,e){let t=await lt(r,e);return u(new Uint8Array(t),"base64pad")};async function Dr({key:r,publicKey:e},t){let n;r instanceof CryptoKey?n=await A.export(r):n=r;let i=await F.encrypt(n,e);return t&&t==="arraybuffer"?i:new Uint8Array(i)}s(Dr,"encryptKeyTo");Dr.asString=async function({key:r,publicKey:e},t){let n=await Dr({key:r,publicKey:e});return t?u(n,t):b(n)};async function Fn(r){let e=await ot(new TextEncoder().encode(r.normalize("NFD")));return u(e,"base32").slice(0,32)}s(Fn,"getDeviceName");var _r=globalThis.crypto;function gt(r){return new Uint8Array(r).buffer}s(gt,"toArrayBuffer");var ie=class r extends de{static{s(this,"RsaKeys")}static TYPE="rsa";static INFO="keys";static EXCHANGE_KEY_NAME=je;static WRITE_KEY_NAME=We;static async create(e,t,n){return await super.create(e,t,n)}constructor(e){super(e),r.EXCHANGE_KEY_NAME=e.exchangeKeyName||je,r.WRITE_KEY_NAME=e.writeKeyName||We}static async _createExchangeKeys(e=!1){return await Sr(Ge,re,"exchange",e)}static async _createWriteKeys(e=!1){return await Sr(Ge,re,"write",e)}async toJson(e){let t=this.publicExchangeKey,n=this.DID,i=await _r.subtle.exportKey("spki",t);return{publicExchangeKey:e?u(new Uint8Array(i),e):b(i),DID:n}}async sign(e,t=M){let n=this.writeKey,i=await F.sign(e,n.privateKey,t);return new Uint8Array(i)}async signAsString(e,t){let n=await this.sign(e,t);return b(n)}async encrypt(e,t,n,i,o){let a=t||this.exchangeKey.publicKey,y=n,c=i,g=y||await A.create({length:c||o}),f=await A.encrypt(typeof e=="string"?h(e):e,typeof g=="string"?await A.import(g):g),K=await he({key:g,publicKey:a}),C=G(K,f);return new Uint8Array(C)}async encryptAsString(e,t,n,i,o){let a=await this.encrypt(e,t,n,i,o);return u(a,"base64pad")}async decrypt(e,t,n){let i=(typeof t=="number"?t:void 0)||R,o=te(e,v),a=o.slice(0,i),y=o.slice(i),c=await this.decryptKey(a);return await A.decrypt(y,c)}async decryptAsString(e,t,n){let i=await this.decrypt(e,t,n);return u(i)}async decryptKey(e){return await F.decrypt(e,this.exchangeKey.privateKey)}async decryptKeyAsString(e,t){let n=await F.decrypt(e,this.exchangeKey.privateKey);return u(n,t)}async getAesKey(e,t){if(!e)throw new Error("RSA requires an encrypted AES key to decrypt");if(typeof e=="string"){let n=await this.decryptKey(e);return await A.import(n)}else return e}};Object.assign(ie.prototype.sign,{asString:s(async function(r,e){return this.signAsString(r,e)},"asString")});Object.assign(ie.prototype.encrypt,{asString:s(async function(r,e,t,n){return this.encryptAsString(r,e,t,n)},"asString")});Object.assign(ie.prototype.decrypt,{asString:s(async function(r,e){return this.decryptAsString(r,e)},"asString")});async function Vn(r,e,t){let n=Ye(t),i=await _e(n.publicKey.buffer,"SHA-256","signing");try{return F.verify(r,e,i)}catch{return!1}}s(Vn,"verify");Object.assign(ie.prototype.decryptKey,{asString:s(async function(r,e){return this.decryptKeyAsString(r,e)},"asString")});async function mt(r,e){let{content:t,publicKey:n}=r,i=e||await A.create(),o=await A.encrypt(typeof t=="string"?h(t):t,typeof i=="string"?await A.import(i):i),a=await he({key:i,publicKey:n});return G(a,o)}s(mt,"encryptTo");mt.asString=async function(r,e){let{content:t,publicKey:n}=r,i=e||await A.create(),o=await A.encrypt(typeof t=="string"?h(t):t,typeof i=="string"?await A.import(i):i,"arraybuffer"),a=await he({key:i,publicKey:n}),y=G(a,o);return u(new Uint8Array(y),"base64pad")};async function he({key:r,publicKey:e},t){let n;r instanceof CryptoKey?n=await A.export(r):n=r;let i=await F.encrypt(n,e);return t&&t==="arraybuffer"?i:new Uint8Array(i)}s(he,"encryptKeyTo");he.asString=async function({key:r,publicKey:e},t){let n=await he({key:r,publicKey:e});return t?u(n,t):b(n)};async function jn(r,e,t,n){let i=ge(e)?e:await me(e),o=n?await _r.subtle.encrypt({name:N,iv:gt(n)},i,gt(r)):await Wn(r,i);return t&&t==="arraybuffer"?o:new Uint8Array(o)}s(jn,"encrypt");async function Wn(r,e,t){let n=$(r,t?.charsize??M),i=typeof e=="string"?await De(e,t):e,o=t?.iv||$e(S),a=await _r.subtle.encrypt({name:N,iv:o},i,n);return G(o,a)}s(Wn,"encryptBytes");var At={};E(At,{EccKeys:()=>q,default:()=>$n,exportPublicKey:()=>Ke,importPublicKey:()=>Le,verify:()=>Jn});var Co=new Error("Key does not exist. Make sure you properly instantiated the keystore."),Po=new Error("Retrieved a symmetric key when an asymmetric keypair was expected. Please use a different key name."),Bo=new Error("Retrieved an asymmetric keypair when an symmetric key was expected. Please use a different key name."),vo=new Error("ECC is not enabled for this browser. Please use RSA instead."),To=new Error("Cryptosystem not supported. Please use ECC or RSA"),Xn=new Error("Invalid key use. Please use 'exchange' or 'write'"),Do=new Error("Max must be less than 256 and greater than 0");function ht(r){Yn(r,["exchange","write","signing"],Xn)}s(ht,"checkValidKeyUse");function Yn(r,e,t){if(!e.some(i=>i===r))throw t}s(Yn,"checkValid");var se=globalThis.crypto;function oe(r){return new Uint8Array(r).buffer}s(oe,"toArrayBuffer");var q=class r extends de{static{s(this,"EccKeys")}static TYPE="ecc";static EXCHANGE_KEY_NAME=Be;static WRITE_KEY_NAME=ve;static INFO="keys";static async create(e,t,n){return await super.create(e,t,n)}constructor(e){super(e),r.EXCHANGE_KEY_NAME=e.exchangeKeyName||Be,r.WRITE_KEY_NAME=e.writeKeyName||ve}get publicExchangeKey(){let e=this.exchangeKey.publicKey;return Object.assign(e,{asString:s(async t=>{let n=await Ke(this.exchangeKey),i=new Uint8Array(n);return t?u(i,t):b(n)},"asString")})}get publicWriteKey(){let e=this.writeKey.publicKey;return Object.assign(e,{asString:s(async t=>{let n=await Ke(this.writeKey),i=new Uint8Array(n);return t?u(i,t):b(n)},"asString")})}static async _createExchangeKeys(e=!1){return await se.subtle.generateKey({name:Ce},e,["deriveKey","deriveBits"])}static async _createWriteKeys(e=!1){return await se.subtle.generateKey({name:ee},e,["sign","verify"])}static async load(e={session:!1}){if(this._instance)return this._instance;let t=e.encryptionKeyName||Be,n=e.writeKeyName||ve;return super.load({...e,encryptionKeyName:t,writeKeyName:n})}async toJson(e){let t=this.DID,n=await Ke(this.exchangeKey);return{publicExchangeKey:e?u(new Uint8Array(n),e):b(n),DID:t}}async wrap(e,t,n){let i=await r._createExchangeKeys(!0),o=crypto.getRandomValues(new Uint8Array(x)),a=await Ae(i.privateKey,t,o,n||"key-wrap"),y;if(e instanceof CryptoKey){let P=await crypto.subtle.exportKey("raw",e);y=new Uint8Array(P)}else typeof e=="string"?y=h(e,"base64pad"):y=e;let c=crypto.getRandomValues(new Uint8Array(S)),g=await crypto.subtle.encrypt({name:B,iv:c},a,oe(y)),f=await Ke(i),K=new Uint8Array(o),C=new Uint8Array(c),ae=new Uint8Array(g),p=K.length+C.length+ae.length,l=new Uint8Array(p);return l.set(K,0),l.set(C,K.length),l.set(ae,K.length+C.length),{enc:b(new Uint8Array(f)),wrappedKey:b(l)}}async unwrap(e,t,n){let i=await Le(e,"X25519","exchange"),o=new Uint8Array(v(t)),a=o.slice(0,x),y=o.slice(x,x+S),c=o.slice(x+S),g=await Ae(this.exchangeKey.privateKey,i,a,n||"key-wrap"),f=await crypto.subtle.decrypt({name:B,iv:y},g,oe(c));return await crypto.subtle.importKey("raw",f,{name:B},!0,["encrypt","decrypt"])}async encrypt(e,t,n,i,o){let a=crypto.getRandomValues(new Uint8Array(x)),y=t||this.exchangeKey.publicKey,c;typeof y=="string"?c=await Le(y,"X25519","exchange"):c=y;let g=await r._createExchangeKeys(!0),f=i||await Ae(g.privateKey,c,a,n||r.INFO,o);f instanceof CryptoKey||(f=await Ae(g.privateKey,c,a,n||r.INFO));let K=crypto.getRandomValues(new Uint8Array(S)),C=typeof e=="string"?new TextEncoder().encode(e):e,ae=await crypto.subtle.encrypt({name:B,iv:K},f,oe(C)),p=new Uint8Array(await Ke(g)),l=new Uint8Array(a),P=new Uint8Array(K),U=new Uint8Array(ae),T=p.length+l.length+P.length+U.length,w=new Uint8Array(T);return w.set(p,0),w.set(l,p.length),w.set(P,p.length+l.length),w.set(U,p.length+l.length+P.length),w}async encryptAsString(e,t,n,i,o){let a=await this.encrypt(e,t,n||r.INFO,i,o);return u(new Uint8Array(a),"base64pad")}async decrypt(e,t,n){let i=te(e,v),o=i.slice(0,W),a=await se.subtle.importKey("raw",o,{name:Ce},!0,[]),y=i.slice(W,W+x),c=i.slice(W+x,W+x+S),g=i.slice(W+x+S),{privateKey:f}=this.exchangeKey,K=await Ae(f,a,y,n||r.INFO);return await crypto.subtle.decrypt({name:t||B,iv:c},K,g)}async decryptAsString(e,t,n){let i=await this.decrypt(e,t,n);return u(new Uint8Array(i))}async getAesKey(e,t){e||(e=this.publicExchangeKey);let n;typeof e=="string"?n=await Le(e,"X25519","exchange"):n=e;let i=crypto.getRandomValues(new Uint8Array(x));return await Ae(this.exchangeKey.privateKey,n,i,t||r.INFO)}async sign(e,t){let n=new TextEncoder,i;typeof e=="string"?i=n.encode(e):e instanceof ArrayBuffer?i=new Uint8Array(e):i=e;let o=await se.subtle.sign({name:ee},this.writeKey.privateKey,oe(i));return new Uint8Array(o)}async signAsString(e,t){let n=await this.sign(e,t);return u(n,"base64pad")}};Object.assign(q.prototype.sign,{asString:s(async function(r,e){let t=await this.sign(r,e);return u(t,"base64pad")},"asString")});async function Ke(r,e="raw"){return await crypto.subtle.exportKey(e,r.publicKey)}s(Ke,"exportPublicKey");async function Le(r,e,t){ht(t);let n=t==="exchange"?Ce:ee,i=t==="exchange"?[]:["verify"],o=v(r),a=e==="X25519"?{name:n}:{name:n,namedCurve:e};return se.subtle.importKey("raw",o,a,!0,i)}s(Le,"importPublicKey");var $n={EccKeys:q,importPublicKey:Le};Object.assign(q.prototype.encrypt,{asString:s(async function(r,e,t,n,i){return this.encryptAsString(r,e,t,n,i)},"asString")});Object.assign(q.prototype.decrypt,{asString:s(async function(r,e){return this.decryptAsString(r,e)},"asString")});Object.assign(q.prototype.sign,{asString:s(async function(r,e){return this.signAsString(r,e)},"asString")});async function Jn(r,e,t){try{let n=Ye(t);if(n.type!=="ed25519")throw new Error(`Expected Ed25519 key for ECC verification, got ${n.type}`);let i=n.publicKey.slice(-32),o=await se.subtle.importKey("raw",i,{name:ee},!0,["verify"]),a=new TextEncoder,y;typeof r=="string"?y=a.encode(r):r instanceof ArrayBuffer?y=new Uint8Array(r):y=r;let c;return typeof e=="string"?c=new Uint8Array(v(e)):c=e,await se.subtle.verify({name:ee},o,oe(c),oe(y))}catch{return!1}}s(Jn,"verify");async function Ae(r,e,t,n,i){let o=new TextEncoder,a=await crypto.subtle.deriveBits({name:"X25519",public:e},r,256),y=await crypto.subtle.importKey("raw",a,"HKDF",!1,["deriveKey"]);return crypto.subtle.deriveKey({name:"HKDF",hash:"SHA-256",salt:t instanceof Uint8Array?oe(t):t,info:o.encode(n)},y,{name:"AES-GCM",length:i||R},!1,["encrypt","decrypt"])}s(Ae,"deriveKey");})();
|
|
3
|
+
-----END PUBLIC KEY-----`,"")}s(On,"stripKeyHeader");function ge(t){return Pr(t,"algorithm")&&Pr(t,"extractable")&&Pr(t,"type")}s(ge,"isCryptoKey");function Pr(t,e){return typeof t=="object"&&t!=null&&e in t}s(Pr,"hasProp");function zn(t,e){return _.subtle.importKey("spki",t instanceof Uint8Array?m(t):t,{name:X,hash:Oe},!1,e)}s(zn,"importRsaKey");async function _e(t,e="rsa"){let r;if(t instanceof CryptoKey){e=t.algorithm.name.includes("RSA")?"rsa":"ed25519";let o=e==="ed25519"?"raw":"spki",a=await _.subtle.exportKey(o,t);r=new Uint8Array(a)}else r=t;let n=Xe.keyTypes[e]?.magicBytes;if(!n)throw new Error(`Key type '${e}' not supported, available types: ${Object.keys(Xe.keyTypes).join(", ")}`);let i=Qe([n,r]);return H+u(i,"base58btc")}s(_e,"publicKeyToDid");function $s(t){let e=t.substring(H.length),r=h(e,"base58btc"),n=Object.entries(Xe.keyTypes).find(([o,a])=>le(r.buffer,a.magicBytes.buffer));if(!n)throw new Error("Invalid key type");let i=n[0];if(i!=="ed25519"&&i!=="rsa")throw new Error(`Unsupported key type: ${i}`);return i}s($s,"keyTypeFromDid");async function J(t){return t instanceof CryptoKey?await _.subtle.exportKey("spki",t):await _.subtle.exportKey("spki",t.publicKey)}s(J,"getPublicKeyAsArrayBuffer");async function Br(t){let e=await J(t);return new Uint8Array(e)}s(Br,"getPublicKeyAsUint8Array");function Ye(t){if(!t.startsWith(H))throw new Error("Please use a base58-encoded DID formatted `did:key:z...`");let e=t.substring(H.length),r=h(e,"base58btc"),n=Object.entries(Xe.keyTypes).find(([o,a])=>le(r.buffer,a.magicBytes.buffer));if(!n)throw new Error("Unsupported key algorithm.");let i=r.slice(n[1].magicBytes.length);return n[0]==="ed25519"&&i.length===44?{publicKey:i.slice(-32),type:n[0]}:{publicKey:i,type:n[0]}}s(Ye,"didToPublicKey");var ne=globalThis.crypto,A={create(t={alg:B,length:R}){let e=t.length||R,r=t.alg||B;return ne.subtle.generateKey({name:r,length:e},!0,["encrypt","decrypt"])},export:Object.assign(async t=>{let e=await ne.subtle.exportKey("raw",t);return new Uint8Array(e)},{asString:s(async(t,e)=>{let r=await A.export(t);return e?u(r,e):b(r)},"asString")}),import(t){return me(typeof t=="string"?v(t):t)},async exportAsString(t){let e=await A.export(t);return b(e)},encrypt:Hn,async decrypt(t,e,r){let n=ge(e)?e:await me(e),i=r?await ne.subtle.decrypt({name:M,iv:m(r)},n,typeof t=="string"?m(h(t)):t instanceof Uint8Array?m(t):t):await ft(t,n);return new Uint8Array(i)}};function me(t,e){return ne.subtle.importKey("raw",t instanceof Uint8Array?m(t):t,{name:M,length:e||256},!0,["encrypt","decrypt"])}s(me,"importAesKey");async function Hn(t,e,r,n){let i=ge(e)?e:await me(e),o=n?await ne.subtle.encrypt({name:M,iv:m(n)},i,t instanceof ArrayBuffer?t:m(typeof t=="string"?h(t):t)):await Fn(t,i);return r&&r==="arraybuffer"?o:new Uint8Array(o)}s(Hn,"encrypt");async function Fn(t,e,r){let n=$(t,r?.charsize??L),i=typeof e=="string"?await Te(e,r):e,o=r?.iv||We(S),a=await ne.subtle.encrypt({name:M,iv:o},i,n);return F(o,a)}s(Fn,"encryptBytes");async function ft(t,e,r){let n=Ve(t),i=typeof e=="string"?await Te(e,r):e,o=n.slice(0,S),a=n.slice(S);return await ne.subtle.decrypt({name:B,iv:o},i,a)}s(ft,"decryptBytes");function $e(t){return new Promise((e,r)=>{t.oncomplete=t.onsuccess=()=>e(t.result),t.onabort=t.onerror=()=>r(t.error)})}s($e,"promisifyRequest");function Gn(t,e){let r,n=s(()=>{if(r)return r;let i=indexedDB.open(t);return i.onupgradeneeded=()=>i.result.createObjectStore(e),r=$e(i),r.then(o=>{o.onclose=()=>r=void 0},()=>{}),r},"getDB");return(i,o)=>n().then(a=>o(a.transaction(e,i).objectStore(e)))}s(Gn,"createStore");var vr;function Tr(){return vr||(vr=Gn("keyval-store","keyval")),vr}s(Tr,"defaultGetStore");function Ie(t,e=Tr()){return e("readonly",r=>$e(r.get(t)))}s(Ie,"get");function Dr(t,e,r=Tr()){return r("readwrite",n=>(n.put(e,t),$e(n.transaction)))}s(Dr,"set");function _r(t,e=Tr()){return e("readwrite",r=>(t.forEach(n=>r.delete(n)),$e(r.transaction)))}s(_r,"delMany");var ao=new Error("Key does not exist. Make sure you properly instantiated the keystore."),yo=new Error("Retrieved a symmetric key when an asymmetric keypair was expected. Please use a different key name."),co=new Error("Retrieved an asymmetric keypair when an symmetric key was expected. Please use a different key name."),po=new Error("ECC is not enabled for this browser. Please use RSA instead."),uo=new Error("Cryptosystem not supported. Please use ECC or RSA"),Vn=new Error("Invalid key use. Please use 'exchange' or 'write'"),fo=new Error("Max must be less than 256 and greater than 0"),de=class extends Error{static{s(this,"KeysDeletedError")}constructor(e){super(e||"These keys were deleted"),this.name="KeysDeletedError"}};function gt(t){jn(t,["exchange","write","signing"],Vn)}s(gt,"checkValidKeyUse");function jn(t,e,r){if(!e.some(i=>i===t))throw r}s(jn,"checkValid");var he=class t{static{s(this,"AbstractKeys")}DID;hasPersisted;isSessionOnly;destroyed;exchangeKeyName;writeKeyName;static EXCHANGE_KEY_NAME;static WRITE_KEY_NAME;static _instance;_exchangeKey;_writeKey;constructor(e){let{keys:r}=e,n=this.constructor;this.DID=e.did,this._exchangeKey=r.exchange,this._writeKey=r.write,this.hasPersisted=e.hasPersisted,this.isSessionOnly=!!e.isSessionOnly,this.destroyed=!1,this.exchangeKeyName=e.exchangeKeyName||n.EXCHANGE_KEY_NAME,this.writeKeyName=e.writeKeyName||n.WRITE_KEY_NAME}get exchangeKey(){if(this.destroyed||!this._exchangeKey)throw new de;return this._exchangeKey}get writeKey(){if(this.destroyed||!this._writeKey)throw new de;return this._writeKey}publicExchangeKeyAsString(e){return this.publicExchangeKey.asString(e)}publicWriteKeyAsString(e){return this.publicWriteKey.asString(e)}get publicWriteKey(){let e=this.writeKey.publicKey;return Object.assign(e,{asString:s(async r=>{let n=await J(this.writeKey),i=new Uint8Array(n);return r?u(i,r):b(i)},"asString")})}get publicExchangeKey(){let e=this.exchangeKey.publicKey;return Object.assign(e,{asString:s(async r=>{let n=await J(this.exchangeKey),i=new Uint8Array(n);return r?u(i,r):b(i)},"asString")})}get privateWriteKey(){return this.writeKey.privateKey}get privateExchangeKey(){return this.exchangeKey.privateKey}get deviceName(){return t.deviceName(this.DID)}static deviceName(e){return Wn(e)}async persist(){if(this.destroyed)throw new de;this.isSessionOnly||(await Promise.all([Dr(this.exchangeKeyName,this.exchangeKey),Dr(this.writeKeyName,this.writeKey)]),this.hasPersisted=!0)}async delete(){let e=this.exchangeKeyName,r=this.writeKeyName;await _r([e,r]),this.constructor._invalidate(e,r),this._destroy()}_destroy(){this.destroyed=!0,this.hasPersisted=!1,this._exchangeKey=null,this._writeKey=null}static _invalidate(e,r){let n=this._instance;n&&n.exchangeKeyName===e&&n.writeKeyName===r&&(n._destroy(),this._instance=null)}static async delete(e={}){let r=e.encryptionKeyName||this.EXCHANGE_KEY_NAME,n=e.writeKeyName||this.WRITE_KEY_NAME;await _r([r,n]),this._invalidate(r,n)}async getDeviceName(){return t.deviceName(this.DID)}static _createExchangeKeys(e){throw new Error("The child should implement this")}static _createWriteKeys(e){throw new Error("The child should implement this")}static async create(e,r,n){let i=n?.exchangeKeys||await this._createExchangeKeys(r),o=n?.writeKeys||await this._createWriteKeys(r),a=await J(o),y=await _e(new Uint8Array(a),this.TYPE==="ecc"?"ed25519":"rsa"),c=new this({keys:{exchange:i,write:o},did:y,hasPersisted:!1,isSessionOnly:!!e});return this._instance=c,c}static async exist(e={}){let r=await Ie(e.writeKeyName||this.WRITE_KEY_NAME),n=await Ie(e.encryptionKeyName||this.EXCHANGE_KEY_NAME);return!(!r||!n)}static async load(e={session:!1}){let r=e.encryptionKeyName||this.EXCHANGE_KEY_NAME,n=e.writeKeyName||this.WRITE_KEY_NAME,i=this._instance;if(i&&!i.destroyed&&i.exchangeKeyName===r&&i.writeKeyName===n)return i;let o=!0,a=await Ie(r),y=await Ie(n);a||(o=!1,a=await this._createExchangeKeys(e.extractable)),y||(o=!1,y=await this._createWriteKeys(e.extractable));let c=await J(y),l=await _e(new Uint8Array(c),this.TYPE==="ecc"?"ed25519":"rsa"),f=new this({keys:{exchange:a,write:y},did:l,hasPersisted:o,isSessionOnly:!!e.session,exchangeKeyName:r,writeKeyName:n});return this._instance=f,f}};async function mt(t,e){let{content:r,publicKey:n}=t,i=e||await A.create(),o=await A.encrypt(typeof r=="string"?h(r):r,typeof i=="string"?await A.import(i):i),a=await Ir({key:i,publicKey:n});return F(a,o)}s(mt,"encryptTo");mt.asString=async function(t,e){let r=await mt(t,e);return u(new Uint8Array(r),"base64pad")};async function Ir({key:t,publicKey:e},r){let n;t instanceof CryptoKey?n=await A.export(t):n=t;let i=await G.encrypt(n,e);return r&&r==="arraybuffer"?i:new Uint8Array(i)}s(Ir,"encryptKeyTo");Ir.asString=async function({key:t,publicKey:e},r){let n=await Ir({key:t,publicKey:e});return r?u(n,r):b(n)};async function Wn(t){let e=await yt(new TextEncoder().encode(t.normalize("NFD")));return u(e,"base32").slice(0,32)}s(Wn,"getDeviceName");var Nr=globalThis.crypto;function dt(t){return new Uint8Array(t).buffer}s(dt,"toArrayBuffer");var ie=class extends he{static{s(this,"RsaKeys")}static TYPE="rsa";static INFO="keys";static EXCHANGE_KEY_NAME=xr;static WRITE_KEY_NAME=Er;static async create(e,r,n){return await super.create(e,r,n)}static async _createExchangeKeys(e=!1){return await Cr(He,re,"exchange",e)}static async _createWriteKeys(e=!1){return await Cr(He,re,"write",e)}async toJson(e){let r=this.publicExchangeKey,n=this.DID,i=await Nr.subtle.exportKey("spki",r);return{publicExchangeKey:e?u(new Uint8Array(i),e):b(i),DID:n}}async sign(e,r=L){let n=this.writeKey,i=await G.sign(e,n.privateKey,r);return new Uint8Array(i)}async signAsString(e,r){let n=await this.sign(e,r);return b(n)}async encrypt(e,r,n,i,o){let a=r||this.exchangeKey.publicKey,y=n,c=i,l=y||await A.create({length:c||o}),f=await A.encrypt(typeof e=="string"?h(e):e,typeof l=="string"?await A.import(l):l),K=await Ae({key:l,publicKey:a}),C=F(K,f);return new Uint8Array(C)}async encryptAsString(e,r,n,i,o){let a=await this.encrypt(e,r,n,i,o);return u(a,"base64pad")}async decrypt(e,r,n){let i=(typeof r=="number"?r:void 0)||R,o=te(e,v),a=o.slice(0,i),y=o.slice(i),c=await this.decryptKey(a);return await A.decrypt(y,c)}async decryptAsString(e,r,n){let i=await this.decrypt(e,r,n);return u(i)}async decryptKey(e){return await G.decrypt(e,this.exchangeKey.privateKey)}async decryptKeyAsString(e,r){let n=await G.decrypt(e,this.exchangeKey.privateKey);return u(n,r)}async getAesKey(e,r){if(!e)throw new Error("RSA requires an encrypted AES key to decrypt");if(typeof e=="string"){let n=await this.decryptKey(e);return await A.import(n)}else return e}};Object.assign(ie.prototype.sign,{asString:s(async function(t,e){return this.signAsString(t,e)},"asString")});Object.assign(ie.prototype.encrypt,{asString:s(async function(t,e,r,n){return this.encryptAsString(t,e,r,n)},"asString")});Object.assign(ie.prototype.decrypt,{asString:s(async function(t,e){return this.decryptAsString(t,e)},"asString")});async function Xn(t,e,r){let n=je(r),i=await De(n.publicKey.buffer,"SHA-256","signing");try{return G.verify(t,e,i)}catch{return!1}}s(Xn,"verify");Object.assign(ie.prototype.decryptKey,{asString:s(async function(t,e){return this.decryptKeyAsString(t,e)},"asString")});async function ht(t,e){let{content:r,publicKey:n}=t,i=e||await A.create(),o=await A.encrypt(typeof r=="string"?h(r):r,typeof i=="string"?await A.import(i):i),a=await Ae({key:i,publicKey:n});return F(a,o)}s(ht,"encryptTo");ht.asString=async function(t,e){let{content:r,publicKey:n}=t,i=e||await A.create(),o=await A.encrypt(typeof r=="string"?h(r):r,typeof i=="string"?await A.import(i):i,"arraybuffer"),a=await Ae({key:i,publicKey:n}),y=F(a,o);return u(new Uint8Array(y),"base64pad")};async function Ae({key:t,publicKey:e},r){let n;t instanceof CryptoKey?n=await A.export(t):n=t;let i=await G.encrypt(n,e);return r&&r==="arraybuffer"?i:new Uint8Array(i)}s(Ae,"encryptKeyTo");Ae.asString=async function({key:t,publicKey:e},r){let n=await Ae({key:t,publicKey:e});return r?u(n,r):b(n)};async function Yn(t,e,r,n){let i=ge(e)?e:await me(e),o=n?await Nr.subtle.encrypt({name:M,iv:dt(n)},i,dt(t)):await $n(t,i);return r&&r==="arraybuffer"?o:new Uint8Array(o)}s(Yn,"encrypt");async function $n(t,e,r){let n=$(t,r?.charsize??L),i=typeof e=="string"?await Te(e,r):e,o=r?.iv||We(S),a=await Nr.subtle.encrypt({name:M,iv:o},i,n);return F(o,a)}s($n,"encryptBytes");var Kt={};E(Kt,{EccKeys:()=>q,default:()=>Jn,exportPublicKey:()=>be,importPublicKey:()=>Ne,verify:()=>qn});var se=globalThis.crypto;function oe(t){return new Uint8Array(t).buffer}s(oe,"toArrayBuffer");var q=class t extends he{static{s(this,"EccKeys")}static TYPE="ecc";static EXCHANGE_KEY_NAME=Ur;static WRITE_KEY_NAME=Sr;static INFO="keys";static async create(e,r,n){return await super.create(e,r,n)}get publicExchangeKey(){let e=this.exchangeKey.publicKey;return Object.assign(e,{asString:s(async r=>{let n=await be(this.exchangeKey),i=new Uint8Array(n);return r?u(i,r):b(n)},"asString")})}get publicWriteKey(){let e=this.writeKey.publicKey;return Object.assign(e,{asString:s(async r=>{let n=await be(this.writeKey),i=new Uint8Array(n);return r?u(i,r):b(n)},"asString")})}static async _createExchangeKeys(e=!1){return await se.subtle.generateKey({name:Pe},e,["deriveKey","deriveBits"])}static async _createWriteKeys(e=!1){return await se.subtle.generateKey({name:ee},e,["sign","verify"])}static async load(e={session:!1}){return super.load(e)}async toJson(e){let r=this.DID,n=await be(this.exchangeKey);return{publicExchangeKey:e?u(new Uint8Array(n),e):b(n),DID:r}}async wrap(e,r,n){let i=await t._createExchangeKeys(!0),o=crypto.getRandomValues(new Uint8Array(x)),a=await Ke(i.privateKey,r,o,n||"key-wrap"),y;if(e instanceof CryptoKey){let P=await crypto.subtle.exportKey("raw",e);y=new Uint8Array(P)}else typeof e=="string"?y=h(e,"base64pad"):y=e;let c=crypto.getRandomValues(new Uint8Array(S)),l=await crypto.subtle.encrypt({name:B,iv:c},a,oe(y)),f=await be(i),K=new Uint8Array(o),C=new Uint8Array(c),ae=new Uint8Array(l),p=K.length+C.length+ae.length,g=new Uint8Array(p);return g.set(K,0),g.set(C,K.length),g.set(ae,K.length+C.length),{enc:b(new Uint8Array(f)),wrappedKey:b(g)}}async unwrap(e,r,n){let i=await Ne(e,"X25519","exchange"),o=new Uint8Array(v(r)),a=o.slice(0,x),y=o.slice(x,x+S),c=o.slice(x+S),l=await Ke(this.exchangeKey.privateKey,i,a,n||"key-wrap"),f=await crypto.subtle.decrypt({name:B,iv:y},l,oe(c));return await crypto.subtle.importKey("raw",f,{name:B},!0,["encrypt","decrypt"])}async encrypt(e,r,n,i,o){let a=crypto.getRandomValues(new Uint8Array(x)),y=r||this.exchangeKey.publicKey,c;typeof y=="string"?c=await Ne(y,"X25519","exchange"):c=y;let l=await t._createExchangeKeys(!0),f=i||await Ke(l.privateKey,c,a,n||t.INFO,o);f instanceof CryptoKey||(f=await Ke(l.privateKey,c,a,n||t.INFO));let K=crypto.getRandomValues(new Uint8Array(S)),C=typeof e=="string"?new TextEncoder().encode(e):e,ae=await crypto.subtle.encrypt({name:B,iv:K},f,oe(C)),p=new Uint8Array(await be(l)),g=new Uint8Array(a),P=new Uint8Array(K),U=new Uint8Array(ae),T=p.length+g.length+P.length+U.length,w=new Uint8Array(T);return w.set(p,0),w.set(g,p.length),w.set(P,p.length+g.length),w.set(U,p.length+g.length+P.length),w}async encryptAsString(e,r,n,i,o){let a=await this.encrypt(e,r,n||t.INFO,i,o);return u(new Uint8Array(a),"base64pad")}async decrypt(e,r,n){let i=te(e,v),o=i.slice(0,W),a=await se.subtle.importKey("raw",o,{name:Pe},!0,[]),y=i.slice(W,W+x),c=i.slice(W+x,W+x+S),l=i.slice(W+x+S),{privateKey:f}=this.exchangeKey,K=await Ke(f,a,y,n||t.INFO);return await crypto.subtle.decrypt({name:r||B,iv:c},K,l)}async decryptAsString(e,r,n){let i=await this.decrypt(e,r,n);return u(new Uint8Array(i))}async getAesKey(e,r){e||(e=this.publicExchangeKey);let n;typeof e=="string"?n=await Ne(e,"X25519","exchange"):n=e;let i=crypto.getRandomValues(new Uint8Array(x));return await Ke(this.exchangeKey.privateKey,n,i,r||t.INFO)}async sign(e,r){let n=new TextEncoder,i;typeof e=="string"?i=n.encode(e):e instanceof ArrayBuffer?i=new Uint8Array(e):i=e;let o=await se.subtle.sign({name:ee},this.writeKey.privateKey,oe(i));return new Uint8Array(o)}async signAsString(e,r){let n=await this.sign(e,r);return u(n,"base64pad")}};Object.assign(q.prototype.sign,{asString:s(async function(t,e){let r=await this.sign(t,e);return u(r,"base64pad")},"asString")});async function be(t,e="raw"){return await crypto.subtle.exportKey(e,t.publicKey)}s(be,"exportPublicKey");async function Ne(t,e,r){gt(r);let n=r==="exchange"?Pe:ee,i=r==="exchange"?[]:["verify"],o=v(t),a=e==="X25519"?{name:n}:{name:n,namedCurve:e};return se.subtle.importKey("raw",o,a,!0,i)}s(Ne,"importPublicKey");var Jn={EccKeys:q,importPublicKey:Ne};Object.assign(q.prototype.encrypt,{asString:s(async function(t,e,r,n,i){return this.encryptAsString(t,e,r,n,i)},"asString")});Object.assign(q.prototype.decrypt,{asString:s(async function(t,e){return this.decryptAsString(t,e)},"asString")});Object.assign(q.prototype.sign,{asString:s(async function(t,e){return this.signAsString(t,e)},"asString")});async function qn(t,e,r){try{let n=je(r);if(n.type!=="ed25519")throw new Error(`Expected Ed25519 key for ECC verification, got ${n.type}`);let i=n.publicKey.slice(-32),o=await se.subtle.importKey("raw",i,{name:ee},!0,["verify"]),a=new TextEncoder,y;typeof t=="string"?y=a.encode(t):t instanceof ArrayBuffer?y=new Uint8Array(t):y=t;let c;return typeof e=="string"?c=new Uint8Array(v(e)):c=e,await se.subtle.verify({name:ee},o,oe(c),oe(y))}catch{return!1}}s(qn,"verify");async function Ke(t,e,r,n,i){let o=new TextEncoder,a=await crypto.subtle.deriveBits({name:"X25519",public:e},t,256),y=await crypto.subtle.importKey("raw",a,"HKDF",!1,["deriveKey"]);return crypto.subtle.deriveKey({name:"HKDF",hash:"SHA-256",salt:r instanceof Uint8Array?oe(r):r,info:o.encode(n)},y,{name:"AES-GCM",length:i||R},!1,["encrypt","decrypt"])}s(Ke,"deriveKey");})();
|
package/dist/ecc/index.cjs
CHANGED
|
@@ -52,11 +52,6 @@ const _EccKeys = class _EccKeys extends import_base.AbstractKeys {
|
|
|
52
52
|
static async create(session, extractable, keys) {
|
|
53
53
|
return await super.create(session, extractable, keys);
|
|
54
54
|
}
|
|
55
|
-
constructor(opts) {
|
|
56
|
-
super(opts);
|
|
57
|
-
_EccKeys.EXCHANGE_KEY_NAME = opts.exchangeKeyName || import_constants.DEFAULT_ECC_EXCHANGE;
|
|
58
|
-
_EccKeys.WRITE_KEY_NAME = opts.writeKeyName || import_constants.DEFAULT_ECC_WRITE;
|
|
59
|
-
}
|
|
60
55
|
get publicExchangeKey() {
|
|
61
56
|
const publicKey = this.exchangeKey.publicKey;
|
|
62
57
|
return Object.assign(publicKey, {
|
|
@@ -103,14 +98,7 @@ const _EccKeys = class _EccKeys extends import_base.AbstractKeys {
|
|
|
103
98
|
static async load(opts = {
|
|
104
99
|
session: false
|
|
105
100
|
}) {
|
|
106
|
-
|
|
107
|
-
const exchangeKeyName = opts.encryptionKeyName || import_constants.DEFAULT_ECC_EXCHANGE;
|
|
108
|
-
const writeKeyName = opts.writeKeyName || import_constants.DEFAULT_ECC_WRITE;
|
|
109
|
-
return super.load({
|
|
110
|
-
...opts,
|
|
111
|
-
encryptionKeyName: exchangeKeyName,
|
|
112
|
-
writeKeyName
|
|
113
|
-
});
|
|
101
|
+
return super.load(opts);
|
|
114
102
|
}
|
|
115
103
|
/**
|
|
116
104
|
* Serialize this keys instance. Will return an object of
|