@substrate-system/keys 0.3.0 → 0.3.2
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 +54 -18
- package/dist/_base.cjs +4 -4
- package/dist/_base.cjs.map +2 -2
- package/dist/_base.d.ts +6 -3
- package/dist/_base.d.ts.map +1 -1
- package/dist/_base.js +4 -4
- package/dist/_base.js.map +2 -2
- package/dist/_base.min.cjs.map +2 -2
- package/dist/_base.min.js +2 -2
- package/dist/_base.min.js.map +3 -3
- package/dist/aes/index.min.js +2 -2
- package/dist/aes/index.min.js.map +3 -3
- package/dist/browser/index.js +102 -23
- package/dist/browser/index.min.js +2 -2
- package/dist/crypto.min.js +2 -2
- package/dist/crypto.min.js.map +3 -3
- package/dist/ecc/index.cjs +16 -1
- package/dist/ecc/index.cjs.map +2 -2
- package/dist/ecc/index.d.ts +6 -1
- package/dist/ecc/index.d.ts.map +1 -1
- package/dist/ecc/index.js +16 -1
- 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/index.min.js +2 -2
- package/dist/index.min.js.map +3 -3
- package/dist/meta.json +12 -12
- package/dist/rsa/index.cjs +24 -1
- package/dist/rsa/index.cjs.map +2 -2
- package/dist/rsa/index.d.ts +16 -1
- package/dist/rsa/index.d.ts.map +1 -1
- package/dist/rsa/index.js +24 -1
- 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 +3 -3
- package/dist/util.min.js +1 -1
- package/dist/util.min.js.map +3 -3
- package/package.json +2 -1
package/dist/browser/index.js
CHANGED
|
@@ -125,8 +125,9 @@
|
|
|
125
125
|
// node_modules/multiformats/dist/src/bytes.js
|
|
126
126
|
var empty = new Uint8Array(0);
|
|
127
127
|
function equals(aa, bb) {
|
|
128
|
-
if (aa === bb)
|
|
128
|
+
if (aa === bb) {
|
|
129
129
|
return true;
|
|
130
|
+
}
|
|
130
131
|
if (aa.byteLength !== bb.byteLength) {
|
|
131
132
|
return false;
|
|
132
133
|
}
|
|
@@ -139,10 +140,12 @@
|
|
|
139
140
|
}
|
|
140
141
|
__name(equals, "equals");
|
|
141
142
|
function coerce(o) {
|
|
142
|
-
if (o instanceof Uint8Array && o.constructor.name === "Uint8Array")
|
|
143
|
+
if (o instanceof Uint8Array && o.constructor.name === "Uint8Array") {
|
|
143
144
|
return o;
|
|
144
|
-
|
|
145
|
+
}
|
|
146
|
+
if (o instanceof ArrayBuffer) {
|
|
145
147
|
return new Uint8Array(o);
|
|
148
|
+
}
|
|
146
149
|
if (ArrayBuffer.isView(o)) {
|
|
147
150
|
return new Uint8Array(o.buffer, o.byteOffset, o.byteLength);
|
|
148
151
|
}
|
|
@@ -418,11 +421,7 @@
|
|
|
418
421
|
});
|
|
419
422
|
}
|
|
420
423
|
__name(baseX, "baseX");
|
|
421
|
-
function decode(string2,
|
|
422
|
-
const codes = {};
|
|
423
|
-
for (let i = 0; i < alphabet2.length; ++i) {
|
|
424
|
-
codes[alphabet2[i]] = i;
|
|
425
|
-
}
|
|
424
|
+
function decode(string2, alphabetIdx, bitsPerChar, name2) {
|
|
426
425
|
let end = string2.length;
|
|
427
426
|
while (string2[end - 1] === "=") {
|
|
428
427
|
--end;
|
|
@@ -432,7 +431,7 @@
|
|
|
432
431
|
let buffer = 0;
|
|
433
432
|
let written = 0;
|
|
434
433
|
for (let i = 0; i < end; ++i) {
|
|
435
|
-
const value =
|
|
434
|
+
const value = alphabetIdx[string2[i]];
|
|
436
435
|
if (value === void 0) {
|
|
437
436
|
throw new SyntaxError(`Non-${name2} character`);
|
|
438
437
|
}
|
|
@@ -474,7 +473,16 @@
|
|
|
474
473
|
return out;
|
|
475
474
|
}
|
|
476
475
|
__name(encode, "encode");
|
|
476
|
+
function createAlphabetIdx(alphabet2) {
|
|
477
|
+
const alphabetIdx = {};
|
|
478
|
+
for (let i = 0; i < alphabet2.length; ++i) {
|
|
479
|
+
alphabetIdx[alphabet2[i]] = i;
|
|
480
|
+
}
|
|
481
|
+
return alphabetIdx;
|
|
482
|
+
}
|
|
483
|
+
__name(createAlphabetIdx, "createAlphabetIdx");
|
|
477
484
|
function rfc4648({ name: name2, prefix, bitsPerChar, alphabet: alphabet2 }) {
|
|
485
|
+
const alphabetIdx = createAlphabetIdx(alphabet2);
|
|
478
486
|
return from({
|
|
479
487
|
prefix,
|
|
480
488
|
name: name2,
|
|
@@ -482,7 +490,7 @@
|
|
|
482
490
|
return encode(input, alphabet2, bitsPerChar);
|
|
483
491
|
},
|
|
484
492
|
decode(input) {
|
|
485
|
-
return decode(input,
|
|
493
|
+
return decode(input, alphabetIdx, bitsPerChar, name2);
|
|
486
494
|
}
|
|
487
495
|
});
|
|
488
496
|
}
|
|
@@ -875,7 +883,13 @@
|
|
|
875
883
|
var code = 0;
|
|
876
884
|
var name = "identity";
|
|
877
885
|
var encode4 = coerce;
|
|
878
|
-
function digest(input) {
|
|
886
|
+
function digest(input, options) {
|
|
887
|
+
if (options?.truncate != null && options.truncate !== input.byteLength) {
|
|
888
|
+
if (options.truncate < 0 || options.truncate > input.byteLength) {
|
|
889
|
+
throw new Error(`Invalid truncate option, must be less than or equal to ${input.byteLength}`);
|
|
890
|
+
}
|
|
891
|
+
input = input.subarray(0, options.truncate);
|
|
892
|
+
}
|
|
879
893
|
return create(code, encode4(input));
|
|
880
894
|
}
|
|
881
895
|
__name(digest, "digest");
|
|
@@ -889,8 +903,9 @@
|
|
|
889
903
|
});
|
|
890
904
|
|
|
891
905
|
// node_modules/multiformats/dist/src/hashes/hasher.js
|
|
892
|
-
|
|
893
|
-
|
|
906
|
+
var DEFAULT_MIN_DIGEST_LENGTH = 20;
|
|
907
|
+
function from2({ name: name2, code: code2, encode: encode5, minDigestLength, maxDigestLength }) {
|
|
908
|
+
return new Hasher(name2, code2, encode5, minDigestLength, maxDigestLength);
|
|
894
909
|
}
|
|
895
910
|
__name(from2, "from");
|
|
896
911
|
var Hasher = class {
|
|
@@ -900,20 +915,45 @@
|
|
|
900
915
|
name;
|
|
901
916
|
code;
|
|
902
917
|
encode;
|
|
903
|
-
|
|
918
|
+
minDigestLength;
|
|
919
|
+
maxDigestLength;
|
|
920
|
+
constructor(name2, code2, encode5, minDigestLength, maxDigestLength) {
|
|
904
921
|
this.name = name2;
|
|
905
922
|
this.code = code2;
|
|
906
923
|
this.encode = encode5;
|
|
924
|
+
this.minDigestLength = minDigestLength ?? DEFAULT_MIN_DIGEST_LENGTH;
|
|
925
|
+
this.maxDigestLength = maxDigestLength;
|
|
907
926
|
}
|
|
908
|
-
digest(input) {
|
|
927
|
+
digest(input, options) {
|
|
928
|
+
if (options?.truncate != null) {
|
|
929
|
+
if (options.truncate < this.minDigestLength) {
|
|
930
|
+
throw new Error(`Invalid truncate option, must be greater than or equal to ${this.minDigestLength}`);
|
|
931
|
+
}
|
|
932
|
+
if (this.maxDigestLength != null && options.truncate > this.maxDigestLength) {
|
|
933
|
+
throw new Error(`Invalid truncate option, must be less than or equal to ${this.maxDigestLength}`);
|
|
934
|
+
}
|
|
935
|
+
}
|
|
909
936
|
if (input instanceof Uint8Array) {
|
|
910
937
|
const result = this.encode(input);
|
|
911
|
-
|
|
938
|
+
if (result instanceof Uint8Array) {
|
|
939
|
+
return createDigest(result, this.code, options?.truncate);
|
|
940
|
+
}
|
|
941
|
+
return result.then((digest2) => createDigest(digest2, this.code, options?.truncate));
|
|
912
942
|
} else {
|
|
913
943
|
throw Error("Unknown type, must be binary type");
|
|
914
944
|
}
|
|
915
945
|
}
|
|
916
946
|
};
|
|
947
|
+
function createDigest(digest2, code2, truncate) {
|
|
948
|
+
if (truncate != null && truncate !== digest2.byteLength) {
|
|
949
|
+
if (truncate > digest2.byteLength) {
|
|
950
|
+
throw new Error(`Invalid truncate option, must be less than or equal to ${digest2.byteLength}`);
|
|
951
|
+
}
|
|
952
|
+
digest2 = digest2.subarray(0, truncate);
|
|
953
|
+
}
|
|
954
|
+
return create(code2, digest2);
|
|
955
|
+
}
|
|
956
|
+
__name(createDigest, "createDigest");
|
|
917
957
|
|
|
918
958
|
// node_modules/multiformats/dist/src/hashes/sha2-browser.js
|
|
919
959
|
function sha(name2) {
|
|
@@ -1986,6 +2026,7 @@
|
|
|
1986
2026
|
dbp.then((db) => {
|
|
1987
2027
|
db.onclose = () => dbp = void 0;
|
|
1988
2028
|
}, () => {
|
|
2029
|
+
dbp = void 0;
|
|
1989
2030
|
});
|
|
1990
2031
|
return dbp;
|
|
1991
2032
|
}, "getDB");
|
|
@@ -2067,7 +2108,6 @@
|
|
|
2067
2108
|
*/
|
|
2068
2109
|
exchangeKeyName;
|
|
2069
2110
|
writeKeyName;
|
|
2070
|
-
// type:'ecc'|'rsa'
|
|
2071
2111
|
static EXCHANGE_KEY_NAME;
|
|
2072
2112
|
// needs to be defined by child class
|
|
2073
2113
|
static WRITE_KEY_NAME;
|
|
@@ -2255,9 +2295,10 @@
|
|
|
2255
2295
|
* to skip that and call `.persist()` yourself. Session keys are never
|
|
2256
2296
|
* written to `indexedDB`, so `{ session: true }` wins over `persist`.
|
|
2257
2297
|
*
|
|
2258
|
-
* @param {{ encryptionKeyName, signingKeyName,
|
|
2259
|
-
*
|
|
2260
|
-
*
|
|
2298
|
+
* @param {{ dbKey, encryptionKeyName, signingKeyName,
|
|
2299
|
+
* session, persist }} opts
|
|
2300
|
+
* `dbKey` prefixes both default indexedDB names. Other strings are
|
|
2301
|
+
* custom names, plus session and persist options.
|
|
2261
2302
|
* @returns {Promise<AbstractKeys>}
|
|
2262
2303
|
*/
|
|
2263
2304
|
static async load(opts = {
|
|
@@ -2345,7 +2386,7 @@
|
|
|
2345
2386
|
return new Uint8Array(data).buffer;
|
|
2346
2387
|
}
|
|
2347
2388
|
__name(toArrayBuffer2, "toArrayBuffer");
|
|
2348
|
-
var RsaKeys = class extends AbstractKeys {
|
|
2389
|
+
var RsaKeys = class _RsaKeys extends AbstractKeys {
|
|
2349
2390
|
static {
|
|
2350
2391
|
__name(this, "RsaKeys");
|
|
2351
2392
|
}
|
|
@@ -2359,12 +2400,35 @@
|
|
|
2359
2400
|
* Default `false`.
|
|
2360
2401
|
* @param {boolean} [extractable] Can we extract the private keys? Default
|
|
2361
2402
|
* `false`.
|
|
2362
|
-
* @param {{ exchangeKeys, writeKeys }} [keys]
|
|
2403
|
+
* @param {{ dbKey, exchangeKeys, writeKeys }} [keys] Key options.
|
|
2404
|
+
* `dbKey` prefixes the IndexedDB names used for both keypairs.
|
|
2363
2405
|
* @returns {Promise<RsaKeys>} A new class instance.
|
|
2364
2406
|
*/
|
|
2365
2407
|
static async create(session, extractable, keys) {
|
|
2408
|
+
if (keys?.dbKey) {
|
|
2409
|
+
const exchangeKeyName = `${keys.dbKey}.${DEFAULT_RSA_EXCHANGE}`;
|
|
2410
|
+
const writeKeyName = `${keys.dbKey}.${DEFAULT_RSA_WRITE}`;
|
|
2411
|
+
_RsaKeys.EXCHANGE_KEY_NAME = exchangeKeyName;
|
|
2412
|
+
_RsaKeys.WRITE_KEY_NAME = writeKeyName;
|
|
2413
|
+
}
|
|
2366
2414
|
return await super.create(session, extractable, keys);
|
|
2367
2415
|
}
|
|
2416
|
+
/**
|
|
2417
|
+
* Restore keys from indexedDB using the RSA key names.
|
|
2418
|
+
*
|
|
2419
|
+
* @param {{ dbKey }} [opts] Prefix for both IndexedDB key names.
|
|
2420
|
+
*/
|
|
2421
|
+
static async load(opts = {
|
|
2422
|
+
session: false
|
|
2423
|
+
}) {
|
|
2424
|
+
if (opts.dbKey) {
|
|
2425
|
+
const exchangeKeyName = `${opts.dbKey}.${DEFAULT_RSA_EXCHANGE}`;
|
|
2426
|
+
const writeKeyName = `${opts.dbKey}.${DEFAULT_RSA_WRITE}`;
|
|
2427
|
+
_RsaKeys.EXCHANGE_KEY_NAME = exchangeKeyName;
|
|
2428
|
+
_RsaKeys.WRITE_KEY_NAME = writeKeyName;
|
|
2429
|
+
}
|
|
2430
|
+
return super.load(opts);
|
|
2431
|
+
}
|
|
2368
2432
|
static async _createExchangeKeys(extractable = false) {
|
|
2369
2433
|
const exchangeKeys = await makeRSAKeypair(
|
|
2370
2434
|
DEFAULT_RSA_SIZE,
|
|
@@ -2620,10 +2684,17 @@
|
|
|
2620
2684
|
* Default `false`.
|
|
2621
2685
|
* @param {boolean} [extractable] Can we extract the private keys? Default
|
|
2622
2686
|
* `false`.
|
|
2623
|
-
* @param {{ exchangeKeys, writeKeys }} [keys]
|
|
2687
|
+
* @param {{ dbKey, exchangeKeys, writeKeys }} [keys] Key options.
|
|
2688
|
+
* `dbKey` prefixes the IndexedDB names used for both keypairs.
|
|
2624
2689
|
* @returns {Promise<EccKeys>} A new class instance.
|
|
2625
2690
|
*/
|
|
2626
2691
|
static async create(session, extractable, keys) {
|
|
2692
|
+
if (keys?.dbKey) {
|
|
2693
|
+
const exchangeKeyName = `${keys.dbKey}.${DEFAULT_ECC_EXCHANGE}`;
|
|
2694
|
+
const writeKeyName = `${keys.dbKey}.${DEFAULT_ECC_WRITE}`;
|
|
2695
|
+
_EccKeys.EXCHANGE_KEY_NAME = exchangeKeyName;
|
|
2696
|
+
_EccKeys.WRITE_KEY_NAME = writeKeyName;
|
|
2697
|
+
}
|
|
2627
2698
|
return await super.create(session, extractable, keys);
|
|
2628
2699
|
}
|
|
2629
2700
|
get publicExchangeKey() {
|
|
@@ -2671,10 +2742,18 @@
|
|
|
2671
2742
|
*
|
|
2672
2743
|
* Saves the keys to `indexedDB` before returning, unless you pass
|
|
2673
2744
|
* `{ persist: false }`.
|
|
2745
|
+
*
|
|
2746
|
+
* @param {{ dbKey }} [opts] Prefix for both IndexedDB key names.
|
|
2674
2747
|
*/
|
|
2675
2748
|
static async load(opts = {
|
|
2676
2749
|
session: false
|
|
2677
2750
|
}) {
|
|
2751
|
+
if (opts.dbKey) {
|
|
2752
|
+
const exchangeKeyName = `${opts.dbKey}.${DEFAULT_ECC_EXCHANGE}`;
|
|
2753
|
+
const writeKeyName = `${opts.dbKey}.${DEFAULT_ECC_WRITE}`;
|
|
2754
|
+
_EccKeys.EXCHANGE_KEY_NAME = exchangeKeyName;
|
|
2755
|
+
_EccKeys.WRITE_KEY_NAME = writeKeyName;
|
|
2756
|
+
}
|
|
2678
2757
|
return super.load(opts);
|
|
2679
2758
|
}
|
|
2680
2759
|
/**
|
|
@@ -1,3 +1,3 @@
|
|
|
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 m(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(m,"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:m,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 m=i[t[f]];if(m===void 0)throw new SyntaxError(`Non-${n} character`);c=c<<r|m,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 h({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(h,"rfc4648");var Ct=j({prefix:"9",name:"base10",alphabet:"0123456789"});var sr={};E(sr,{base16:()=>Pt,base16upper:()=>Bt});var Pt=h({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),Bt=h({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var or={};E(or,{base2:()=>vt});var vt=h({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=h({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),kt=h({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Lt=h({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Mt=h({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Rt=h({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Ot=h({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),zt=h({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Ht=h({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Ft=h({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=h({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),Wt=h({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),Xt=h({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),Yt=h({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var fr={};E(fr,{base8:()=>$t});var $t=h({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[m,C]=Ue(e.subarray(r));return r+=C,m},"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 A(t,e="utf8"){let r=Re[e];if(r==null)throw new Error(`Unsupported encoding "${e}"`);return r.decoder.decode(`${r.prefix}${t}`)}s(A,"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:()=>K,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 d(t){return new Uint8Array(t).buffer}s(d,"toArrayBuffer");async function yt(t){return new Uint8Array(await ve.subtle.digest("sha-256",t.buffer))}s(yt,"sha256");function v(t){let e=A(t,"base64pad");return d(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=A(e,"base58btc"),{keyBuffer:n,type:i}=In(d(r));return{publicKey:new Uint8Array(n),type:i}}s(je,"didToPublicKey");function In(t){if(le(t,d(ze)))return{keyBuffer:t.slice(ze.byteLength),type:Be.RSA};if(le(t,d(Ge)))return{keyBuffer:t.slice(Ge.byteLength),type:Be.Edwards};if(le(t,d(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):d(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(d(t)),n=e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(d(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=A(t,"utf8"));let i=r;typeof r=="string"&&(i=A(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",d(o),{name:"Ed25519"},!1,["verify"]);return _.subtle.verify({name:"Ed25519"},a,d(i),d(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",d(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(d(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=A(e,"base64pad"):i=e;let o=await _.subtle.decrypt({name:X},n,i instanceof Uint8Array?d(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-----
|
|
1
|
+
"use strict";(()=>{var Lr=Object.defineProperty;var s=(r,e)=>Lr(r,"name",{value:e,configurable:!0});var E=(r,e)=>{for(var t in e)Lr(r,t,{get:e[t],enumerable:!0})};var Mr=(n=>(n[n.B1024=1024]="B1024",n[n.B2048=2048]="B2048",n[n.B4096=4096]="B4096",n))(Mr||{}),kr=(n=>(n[n.B128=128]="B128",n[n.B192=192]="B192",n[n.B256=256]="B256",n))(kr||{}),Rr=(t=>(t.ECC="ecc",t.RSA="rsa",t))(Rr||{}),er=(i=>(i.SHA_1="SHA-1",i.SHA_256="SHA-256",i.SHA_384="SHA-384",i.SHA_512="SHA-512",i))(er||{}),rr=(i=>(i.P_256="P-256",i.P_384="P-384",i.P_521="P-521",i.X25519="X25519",i))(rr||{}),Z=(n=>(n.Sign="signing",n.Exchange="exchange",n.Write="write",n))(Z||{}),tr=(t=>(t[t.B8=8]="B8",t[t.B16=16]="B16",t))(tr||{});var Kt={};E(Kt,{AES_GCM:()=>k,BASE58_DID_PREFIX:()=>H,BLS_DID_PREFIX:()=>We,DEFAULT_CHAR_SIZE:()=>M,DEFAULT_ECC_EXCHANGE:()=>De,DEFAULT_ECC_EXCHANGE_CURVE:()=>_n,DEFAULT_ECC_WRITE:()=>_e,DEFAULT_HASH_ALGORITHM:()=>ee,DEFAULT_RSA_EXCHANGE:()=>ve,DEFAULT_RSA_SIZE:()=>Ye,DEFAULT_RSA_WRITE:()=>Te,DEFAULT_SYMM_ALGORITHM:()=>B,DEFAULT_SYMM_LENGTH:()=>R,ECC_EXCHANGE_ALGORITHM:()=>Pe,ECC_WRITE_ALGORITHM:()=>Q,EDWARDS_DID_PREFIX:()=>Xe,IV_LENGTH:()=>S,KEY_TYPE:()=>Be,KEY_USE:()=>at,RSA_ALGORITHM:()=>X,RSA_DID_PREFIX:()=>Ve,RSA_HASHING_ALGORITHM:()=>Fe,RSA_SIGN_ALGORITHM:()=>$,RsaKeys:()=>ne,SALT_LENGTH:()=>x,X25519_PUBLIC_KEY_LENGTH:()=>W,encrypt:()=>Jn,encryptKeyTo:()=>Ae,encryptTo:()=>At,getPublicKeyAsArrayBuffer:()=>q,getPublicKeyAsUint8Array:()=>Br,publicKeyToDid:()=>Me,verify:()=>qn});function we(r=0){return new Uint8Array(r)}s(we,"allocUnsafe");function nr(r,e){e==null&&(e=r.reduce((i,o)=>i+o.length,0));let t=we(e),n=0;for(let i of r)t.set(i,n),n+=i.length;return t}s(nr,"concat");var yr={};E(yr,{base10:()=>Bt});var Ki=new Uint8Array(0);function Or(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(Or,"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 zr(r){return new TextEncoder().encode(r)}s(zr,"fromString");function Hr(r){return new TextDecoder().decode(r)}s(Hr,"toString");function xt(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),f=Math.log(y)/Math.log(256),A=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)*A+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 Oe=c.repeat(l);O<w;++O)Oe+=r.charAt(D[O]);return Oe}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)*f+1>>>0,w=new Uint8Array(T);p[l];){var D=t[p.charCodeAt(l)];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,l++}if(p[l]!==" "){for(var I=T-U;I!==T&&w[I]===0;)I++;for(var O=new Uint8Array(P+(T-I)),Oe=P;I!==T;)O[Oe++]=w[I++];return O}}}s(C,"decodeUnsafe");function oe(p){var l=C(p);if(l)return l;throw new Error(`Non-${e} character`)}return s(oe,"decode"),{encode:K,decodeUnsafe:C,decode:oe}}s(xt,"base");var Et=xt,Ut=Et,Fr=Ut;var ir=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")}},sr=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 Vr(this,e)}},or=class{static{s(this,"ComposedDecoder")}decoders;constructor(e){this.decoders=e}or(e){return Vr(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 Vr(r,e){return new or({...r.decoders??{[r.prefix]:r},...e.decoders??{[e.prefix]:e}})}s(Vr,"or");var ar=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 ir(e,t,n),this.decoder=new sr(e,t,i)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}};function ae({name:r,prefix:e,encode:t,decode:n}){return new ar(r,e,t,n)}s(ae,"from");function Y({name:r,prefix:e,alphabet:t}){let{encode:n,decode:i}=Fr(t,r);return ae({prefix:e,name:r,encode:n,decode:s(o=>z(i(o)),"decode")})}s(Y,"baseX");function St(r,e,t,n){let i=r.length;for(;r[i-1]==="=";)--i;let o=new Uint8Array(i*t/8|0),a=0,y=0,c=0;for(let f=0;f<i;++f){let A=e[r[f]];if(A===void 0)throw new SyntaxError(`Non-${n} character`);y=y<<t|A,a+=t,a>=8&&(a-=8,o[c++]=255&y>>a)}if(a>=t||(255&y<<8-a)!==0)throw new SyntaxError("Unexpected end of data");return o}s(St,"decode");function Ct(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(Ct,"encode");function Pt(r){let e={};for(let t=0;t<r.length;++t)e[r[t]]=t;return e}s(Pt,"createAlphabetIdx");function m({name:r,prefix:e,bitsPerChar:t,alphabet:n}){let i=Pt(n);return ae({prefix:e,name:r,encode(o){return Ct(o,n,t)},decode(o){return St(o,i,t,r)}})}s(m,"rfc4648");var Bt=Y({prefix:"9",name:"base10",alphabet:"0123456789"});var cr={};E(cr,{base16:()=>vt,base16upper:()=>Tt});var vt=m({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),Tt=m({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var pr={};E(pr,{base2:()=>Dt});var Dt=m({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var ur={};E(ur,{base256emoji:()=>Mt});var Yr=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}"),_t=Yr.reduce((r,e,t)=>(r[t]=e,r),[]),It=Yr.reduce((r,e,t)=>{let n=e.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${e}`);return r[n]=t,r},[]);function Nt(r){return r.reduce((e,t)=>(e+=_t[t],e),"")}s(Nt,"encode");function Lt(r){let e=[];for(let t of r){let n=t.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${t}`);let i=It[n];if(i==null)throw new Error(`Non-base256emoji character: ${t}`);e.push(i)}return new Uint8Array(e)}s(Lt,"decode");var Mt=ae({prefix:"\u{1F680}",name:"base256emoji",encode:Nt,decode:Lt});var fr={};E(fr,{base32:()=>ye,base32hex:()=>zt,base32hexpad:()=>Gt,base32hexpadupper:()=>Ft,base32hexupper:()=>Ht,base32pad:()=>Rt,base32padupper:()=>Ot,base32upper:()=>kt,base32z:()=>Vt});var ye=m({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),kt=m({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Rt=m({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Ot=m({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),zt=m({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Ht=m({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Gt=m({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Ft=m({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Vt=m({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var lr={};E(lr,{base36:()=>xe,base36upper:()=>Yt});var xe=Y({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),Yt=Y({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var gr={};E(gr,{base58btc:()=>L,base58flickr:()=>Wt});var L=Y({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),Wt=Y({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var mr={};E(mr,{base64:()=>Xt,base64pad:()=>$t,base64url:()=>jt,base64urlpad:()=>qt});var Xt=m({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),$t=m({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),jt=m({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),qt=m({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var hr={};E(hr,{base8:()=>Jt});var Jt=m({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var dr={};E(dr,{identity:()=>Zt});var Zt=ae({prefix:"\0",name:"identity",encode:s(r=>Hr(r),"encode"),decode:s(r=>zr(r),"decode")});var zi=new TextEncoder,Hi=new TextDecoder;var Kr={};E(Kr,{identity:()=>xn});var rn=$r,Wr=128,tn=127,nn=~tn,sn=Math.pow(2,31);function $r(r,e,t){e=e||[],t=t||0;for(var n=t;r>=sn;)e[t++]=r&255|Wr,r/=128;for(;r&nn;)e[t++]=r&255|Wr,r>>>=7;return e[t]=r|0,$r.bytes=t-n+1,e}s($r,"encode");var on=Ar,an=128,Xr=127;function Ar(r,n){var t=0,n=n||0,i=0,o=n,a,y=r.length;do{if(o>=y)throw Ar.bytes=0,new RangeError("Could not decode varint");a=r[o++],t+=i<28?(a&Xr)<<i:(a&Xr)*Math.pow(2,i),i+=7}while(a>=an);return Ar.bytes=o-n,t}s(Ar,"read");var yn=Math.pow(2,7),cn=Math.pow(2,14),pn=Math.pow(2,21),un=Math.pow(2,28),fn=Math.pow(2,35),ln=Math.pow(2,42),gn=Math.pow(2,49),mn=Math.pow(2,56),hn=Math.pow(2,63),dn=s(function(r){return r<yn?1:r<cn?2:r<pn?3:r<un?4:r<fn?5:r<ln?6:r<gn?7:r<mn?8:r<hn?9:10},"length"),An={encode:rn,decode:on,encodingLength:dn},Kn=An,Ee=Kn;function Ue(r,e=0){return[Ee.decode(r,e),Ee.decode.bytes]}s(Ue,"decode");function ce(r,e,t=0){return Ee.encode(r,e,t),e}s(ce,"encodeTo");function pe(r){return Ee.encodingLength(r)}s(pe,"encodingLength");function fe(r,e){let t=e.byteLength,n=pe(r),i=n+pe(t),o=new Uint8Array(i+t);return ce(r,o,0),ce(t,o,n),o.set(e,i),new ue(r,t,e,o)}s(fe,"create");function jr(r){let e=z(r),[t,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 ue(t,i,a,e)}s(jr,"decode");function qr(r,e){if(r===e)return!0;{let t=e;return r.code===t.code&&r.size===t.size&&t.bytes instanceof Uint8Array&&Or(r.bytes,t.bytes)}}s(qr,"equals");var ue=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 Jr=0,bn="identity",Zr=z;function wn(r,e){if(e?.truncate!=null&&e.truncate!==r.byteLength){if(e.truncate<0||e.truncate>r.byteLength)throw new Error(`Invalid truncate option, must be less than or equal to ${r.byteLength}`);r=r.subarray(0,e.truncate)}return fe(Jr,Zr(r))}s(wn,"digest");var xn={code:Jr,name:bn,encode:Zr,digest:wn};var xr={};E(xr,{sha256:()=>Un,sha512:()=>Sn});var En=20;function wr({name:r,code:e,encode:t,minDigestLength:n,maxDigestLength:i}){return new br(r,e,t,n,i)}s(wr,"from");var br=class{static{s(this,"Hasher")}name;code;encode;minDigestLength;maxDigestLength;constructor(e,t,n,i,o){this.name=e,this.code=t,this.encode=n,this.minDigestLength=i??En,this.maxDigestLength=o}digest(e,t){if(t?.truncate!=null){if(t.truncate<this.minDigestLength)throw new Error(`Invalid truncate option, must be greater than or equal to ${this.minDigestLength}`);if(this.maxDigestLength!=null&&t.truncate>this.maxDigestLength)throw new Error(`Invalid truncate option, must be less than or equal to ${this.maxDigestLength}`)}if(e instanceof Uint8Array){let n=this.encode(e);return n instanceof Uint8Array?Qr(n,this.code,t?.truncate):n.then(i=>Qr(i,this.code,t?.truncate))}else throw Error("Unknown type, must be binary type")}};function Qr(r,e,t){if(t!=null&&t!==r.byteLength){if(t>r.byteLength)throw new Error(`Invalid truncate option, must be less than or equal to ${r.byteLength}`);r=r.subarray(0,t)}return fe(e,r)}s(Qr,"createDigest");function rt(r){return async e=>new Uint8Array(await crypto.subtle.digest(r,e))}s(rt,"sha");var Un=wr({name:"sha2-256",code:18,encode:rt("SHA-256")}),Sn=wr({name:"sha2-512",code:19,encode:rt("SHA-512")});function tt(r,e){let{bytes:t,version:n}=r;return n===0?Pn(t,Er(r),e??L.encoder):Bn(t,Er(r),e??ye.encoder)}s(tt,"format");var nt=new WeakMap;function Er(r){let e=nt.get(r);if(e==null){let t=new Map;return nt.set(r,t),t}return e}s(Er,"baseCache");var He=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!==Ce)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(t.code!==vn)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=fe(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&&qr(e.multihash,n.multihash)}toString(e){return tt(this,e)}toJSON(){return{"/":tt(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??it(n,i,o.bytes))}else if(t[Tn]===!0){let{version:n,multihash:i,code:o}=t,a=jr(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!==Ce)throw new Error(`Version 0 CID must use dag-pb (code: ${Ce}) block encoding`);return new r(e,t,n,n.bytes)}case 1:{let i=it(e,t,n.bytes);return new r(e,t,n,i)}default:throw new Error("Invalid version")}}static createV0(e){return r.create(0,Ce,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 ue(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]=Ue(e.subarray(t));return t+=C,K},"next"),i=n(),o=Ce;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(),f=t+c,A=f-a;return{version:i,codec:o,multihashCode:y,digestSize:c,multihashSize:A,size:f}}static parse(e,t){let[n,i]=Cn(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 Er(o).set(n,e),o}};function Cn(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 ye.prefix:{let t=e??ye;return[ye.prefix,t.decode(r)]}case xe.prefix:{let t=e??xe;return[xe.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(Cn,"parseCIDtoBytes");function Pn(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(Pn,"toStringV0");function Bn(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(Bn,"toStringV1");var Ce=112,vn=18;function it(r,e,t){let n=pe(r),i=n+pe(e),o=new Uint8Array(i+t.byteLength);return ce(r,o,0),ce(e,o,n),o.set(t,i),o}s(it,"encodeCID");var Tn=Symbol.for("@ipld/js-cid/CID");var Ur={...dr,...pr,...hr,...yr,...cr,...fr,...lr,...gr,...mr,...ur},As={...xr,...Kr};function ot(r,e,t,n){return{name:r,prefix:e,encoder:{name:r,prefix:e,encode:t},decoder:{decode:n}}}s(ot,"createCodec");var st=ot("utf8","u",r=>"u"+new TextDecoder("utf8").decode(r),r=>new TextEncoder().encode(r.substring(1))),Sr=ot("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=we(r.length);for(let t=0;t<r.length;t++)e[t]=r.charCodeAt(t);return e}),Dn={utf8:st,"utf-8":st,hex:Ur.base16,latin1:Sr,ascii:Sr,binary:Sr,...Ur},Ge=Dn;function h(r,e="utf8"){let t=Ge[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=Ge[e];if(t==null)throw new Error(`Unsupported encoding "${e}"`);return t.encoder.encode(r).substring(1)}s(u,"toString");var gt={};E(gt,{AES:()=>d,decryptBytes:()=>lt,importAesKey:()=>me});var H="did:key:z",Q="Ed25519",Pe="X25519",_n="X25519",W=32,at={sign:["sign","verify"],encrypt:["deriveKey","deriveBits"]},X="RSA-OAEP",$="RSASSA-PKCS1-v1_5",Fe="SHA-256",Ve=new Uint8Array([0,245,2]),Ye=2048,ee="SHA-256",M=8,k="AES-GCM",B=k,R=256,S=12,We=new Uint8Array([234,1]),Xe=new Uint8Array([237,1]),Be={RSA:"rsa",Edwards:"ed25519",BLS:"bls12-381"},ve="rsa-exchange-key",Te="rsa-write-key",De="ecc-exchange",_e="ecc-write",x=16;var Ie=globalThis.crypto,yt=new Error("Invalid key use. Please use 'encryption' or 'signing"),In=new Error("Max must be less than 256 and greater than 0");function g(r){return new Uint8Array(r).buffer}s(g,"toArrayBuffer");async function ct(r){return new Uint8Array(await Ie.subtle.digest("sha-256",r.buffer))}s(ct,"sha256");function v(r){let e=h(r,"base64pad");return g(e)}s(v,"base64ToArrBuf");var re=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 $e(r){return re(r,v)}s($e,"normalizeBase64ToBuf");var Nn=s(r=>re(r,e=>pt(e,8)),"normalizeUtf8ToBuf");function pt(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(pt,"strToArrBuf");var Ln=s(r=>re(r,e=>pt(e,16)),"normalizeUtf16ToBuf");function j(r,e){return e===8?Nn(r):Ln(r)}s(j,"normalizeUnicodeToBuf");function je(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}=Mn(g(t));return{publicKey:new Uint8Array(n),type:i}}s(je,"didToPublicKey");function Mn(r){if(le(r,g(Ve)))return{keyBuffer:r.slice(Ve.byteLength),type:Be.RSA};if(le(r,g(Xe)))return{keyBuffer:r.slice(Xe.byteLength),type:Be.Edwards};if(le(r,g(We)))return{keyBuffer:r.slice(We.byteLength),type:Be.BLS};throw new Error("Unsupported key algorithm. Try using RSA.")}s(Mn,"parseMagicBytes");var kn={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 kn.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 Ne(r,e){let t=typeof r=="string"?v(r):g(r);return Ie.subtle.importKey("raw",t,{name:e?.alg||B,length:e?.length||R},!0,["encrypt","decrypt"])}s(Ne,"importKey");function qe(r,{max:e}={max:255}){if(e<1||e>255)throw In;let t=new Uint8Array(r);if(e===255)return Ie.getRandomValues(t),t.buffer;let n=0,i=e+1,o=Math.floor(256/i)*i,a=new Uint8Array(1);for(;n<t.length;)Ie.getRandomValues(a),a[0]<o&&(t[n]=a[0]%i,n++);return t.buffer}s(qe,"randomBuf");function G(r,e){let t=r instanceof ArrayBuffer?new Uint8Array(r):new Uint8Array(g(r)),n=e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(g(e)),i=new Uint8Array(t.length+n.length);return i.set(t),i.set(n,t.length),i.buffer}s(G,"joinBufs");async function Cr(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:$,a=t==="exchange"?["encrypt","decrypt"]:["sign","verify"];return Ie.subtle.generateKey({name:o,modulusLength:r,publicExponent:Rn(),hash:{name:e}},i,a)}s(Cr,"makeRSAKeypair");function Rn(){return new Uint8Array([1,0,1])}s(Rn,"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:ft},ed25519:{magicBytes:new Uint8Array([237,1]),verify:ut}}};async function Js({message:r,did:e,signature:t}){let n=On(e),i=Ze(e).publicKey;if(n==="ed25519")return ut({message:r,publicKey:i,signature:t});if(n==="rsa")return ft({message:r,publicKey:i,signature:t});throw new Error("Unknown key format")}s(Js,"verify");function On(r){try{let{type:e}=Ze(r);return e}catch{return"unknown"}}s(On,"getAlgorithm");async function ut({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=Ze(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",g(o),{name:"Ed25519"},!1,["verify"]);return _.subtle.verify({name:"Ed25519"},a,g(i),g(n))}s(ut,"ed25519Verify");async function ft({message:r,publicKey:e,signature:t}){let n=e;return typeof e=="string"&&(n=Ze(e).publicKey),F.verify(r,t,await _.subtle.importKey("spki",g(n),{name:$,hash:Fe},!1,["verify"]),8)}s(ft,"rsaVerify");var F={verify:s(async function(e,t,n,i=M,o=ee){return _.subtle.verify({name:$,saltLength:x},typeof n=="string"?await Le(n,o,"signing"):n,$e(t),j(e,i))},"rsaVerify"),sign:s(async function(e,t,n=M){return _.subtle.sign({name:$,saltLength:x},t,j(e,n))},"sign"),encrypt:s(async function(e,t,n=M,i=ee){let o;return typeof t=="string"?o=await Le(t,i,"exchange"):o=t instanceof Uint8Array?await Le(g(t),i,"exchange"):t,_.subtle.encrypt({name:X},o,j(e,n))},"rsaEncrypt"),decrypt:s(async function(e,t){let n=ge(t)?t:await Fn(t,["decrypt"]),i;typeof e=="string"?i=h(e,"base64pad"):i=e;let o=await _.subtle.decrypt({name:X},n,i instanceof Uint8Array?g(i):i);return new Uint8Array(o)},"rsaDecrypt")};async function Le(r,e,t){zn(t);let n=t==="exchange"?X:$,i=t==="exchange"?["encrypt"]:["verify"],o=typeof r=="string"?v(Gn(r)):r;return _.subtle.importKey("spki",o,{name:n,hash:{name:e}},!0,i)}s(Le,"rsaImportPublicKey");function zn(r){Hn(r,["signing","exchange"],yt)}s(zn,"checkValidKeyUse");function Hn(r,e,t){if(!e.some(i=>i===r))throw t}s(Hn,"checkValid");function Gn(r){return r.replace(`-----BEGIN PUBLIC KEY-----
|
|
2
2
|
`,"").replace(`
|
|
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?d(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=A(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=A(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,K={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 K.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 K.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:d(r)},n,typeof t=="string"?d(A(t)):t instanceof Uint8Array?d(t):t):await ft(t,n);return new Uint8Array(i)}};function me(t,e){return ne.subtle.importKey("raw",t instanceof Uint8Array?d(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:d(n)},i,t instanceof ArrayBuffer?t:d(typeof t=="string"?A(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.persist!==!1,n=e.encryptionKeyName||this.EXCHANGE_KEY_NAME,i=e.writeKeyName||this.WRITE_KEY_NAME,o=this._instance;if(o&&!o.destroyed&&o.exchangeKeyName===n&&o.writeKeyName===i)return r&&!o.hasPersisted&&await o.persist(),o;let a=!0,y=await Ie(n),c=await Ie(i);y||(a=!1,y=await this._createExchangeKeys(e.extractable)),c||(a=!1,c=await this._createWriteKeys(e.extractable));let l=await J(c),f=await _e(new Uint8Array(l),this.TYPE==="ecc"?"ed25519":"rsa"),m=new this({keys:{exchange:y,write:c},did:f,hasPersisted:a,isSessionOnly:!!e.session,exchangeKeyName:n,writeKeyName:i});return this._instance=m,r&&!a&&await m.persist(),m}};async function mt(t,e){let{content:r,publicKey:n}=t,i=e||await K.create(),o=await K.encrypt(typeof r=="string"?A(r):r,typeof i=="string"?await K.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 K.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 K.create({length:c||o}),f=await K.encrypt(typeof e=="string"?A(e):e,typeof l=="string"?await K.import(l):l),m=await Ae({key:l,publicKey:a}),C=F(m,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 K.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 K.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 K.create(),o=await K.encrypt(typeof r=="string"?A(r):r,typeof i=="string"?await K.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 K.create(),o=await K.encrypt(typeof r=="string"?A(r):r,typeof i=="string"?await K.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 K.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=A(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),m=new Uint8Array(o),C=new Uint8Array(c),ae=new Uint8Array(l),p=m.length+C.length+ae.length,g=new Uint8Array(p);return g.set(m,0),g.set(C,m.length),g.set(ae,m.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 m=crypto.getRandomValues(new Uint8Array(S)),C=typeof e=="string"?new TextEncoder().encode(e):e,ae=await crypto.subtle.encrypt({name:B,iv:m},f,oe(C)),p=new Uint8Array(await be(l)),g=new Uint8Array(a),P=new Uint8Array(m),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,m=await Ke(f,a,y,n||t.INFO);return await crypto.subtle.decrypt({name:r||B,iv:c},m,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");})();
|
|
3
|
+
-----END PUBLIC KEY-----`,"")}s(Gn,"stripKeyHeader");function ge(r){return Pr(r,"algorithm")&&Pr(r,"extractable")&&Pr(r,"type")}s(ge,"isCryptoKey");function Pr(r,e){return typeof r=="object"&&r!=null&&e in r}s(Pr,"hasProp");function Fn(r,e){return _.subtle.importKey("spki",r instanceof Uint8Array?g(r):r,{name:X,hash:Fe},!1,e)}s(Fn,"importRsaKey");async function Me(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=nr([n,t]);return H+u(i,"base58btc")}s(Me,"publicKeyToDid");function Zs(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(Zs,"keyTypeFromDid");async function q(r){return r instanceof CryptoKey?await _.subtle.exportKey("spki",r):await _.subtle.exportKey("spki",r.publicKey)}s(q,"getPublicKeyAsArrayBuffer");async function Br(r){let e=await q(r);return new Uint8Array(e)}s(Br,"getPublicKeyAsUint8Array");function Ze(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(Ze,"didToPublicKey");var te=globalThis.crypto,d={create(r={alg:B,length:R}){let e=r.length||R,t=r.alg||B;return te.subtle.generateKey({name:t,length:e},!0,["encrypt","decrypt"])},export:Object.assign(async r=>{let e=await te.subtle.exportKey("raw",r);return new Uint8Array(e)},{asString:s(async(r,e)=>{let t=await d.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 d.export(r);return b(e)},encrypt:Vn,async decrypt(r,e,t){let n=ge(e)?e:await me(e),i=t?await te.subtle.decrypt({name:k,iv:g(t)},n,typeof r=="string"?g(h(r)):r instanceof Uint8Array?g(r):r):await lt(r,n);return new Uint8Array(i)}};function me(r,e){return te.subtle.importKey("raw",r instanceof Uint8Array?g(r):r,{name:k,length:e||256},!0,["encrypt","decrypt"])}s(me,"importAesKey");async function Vn(r,e,t,n){let i=ge(e)?e:await me(e),o=n?await te.subtle.encrypt({name:k,iv:g(n)},i,r instanceof ArrayBuffer?r:g(typeof r=="string"?h(r):r)):await Yn(r,i);return t&&t==="arraybuffer"?o:new Uint8Array(o)}s(Vn,"encrypt");async function Yn(r,e,t){let n=j(r,t?.charsize??M),i=typeof e=="string"?await Ne(e,t):e,o=t?.iv||qe(S),a=await te.subtle.encrypt({name:k,iv:o},i,n);return G(o,a)}s(Yn,"encryptBytes");async function lt(r,e,t){let n=$e(r),i=typeof e=="string"?await Ne(e,t):e,o=n.slice(0,S),a=n.slice(S);return await te.subtle.decrypt({name:B,iv:o},i,a)}s(lt,"decryptBytes");function Qe(r){return new Promise((e,t)=>{r.oncomplete=r.onsuccess=()=>e(r.result),r.onabort=r.onerror=()=>t(r.error)})}s(Qe,"promisifyRequest");function Wn(r,e){let t,n=s(()=>{if(t)return t;let i=indexedDB.open(r);return i.onupgradeneeded=()=>i.result.createObjectStore(e),t=Qe(i),t.then(o=>{o.onclose=()=>t=void 0},()=>{t=void 0}),t},"getDB");return(i,o)=>n().then(a=>o(a.transaction(e,i).objectStore(e)))}s(Wn,"createStore");var vr;function Tr(){return vr||(vr=Wn("keyval-store","keyval")),vr}s(Tr,"defaultGetStore");function ke(r,e=Tr()){return e("readonly",t=>Qe(t.get(r)))}s(ke,"get");function Dr(r,e,t=Tr()){return t("readwrite",n=>(n.put(e,r),Qe(n.transaction)))}s(Dr,"set");function _r(r,e=Tr()){return e("readwrite",t=>(r.forEach(n=>t.delete(n)),Qe(t.transaction)))}s(_r,"delMany");var po=new Error("Key does not exist. Make sure you properly instantiated the keystore."),uo=new Error("Retrieved a symmetric key when an asymmetric keypair was expected. Please use a different key name."),fo=new Error("Retrieved an asymmetric keypair when an symmetric key was expected. Please use a different key name."),lo=new Error("ECC is not enabled for this browser. Please use RSA instead."),go=new Error("Cryptosystem not supported. Please use ECC or RSA"),Xn=new Error("Invalid key use. Please use 'exchange' or 'write'"),mo=new Error("Max must be less than 256 and greater than 0"),he=class extends Error{static{s(this,"KeysDeletedError")}constructor(e){super(e||"These keys were deleted"),this.name="KeysDeletedError"}};function mt(r){$n(r,["exchange","write","signing"],Xn)}s(mt,"checkValidKeyUse");function $n(r,e,t){if(!e.some(i=>i===r))throw t}s($n,"checkValid");var de=class r{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:t}=e,n=this.constructor;this.DID=e.did,this._exchangeKey=t.exchange,this._writeKey=t.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 he;return this._exchangeKey}get writeKey(){if(this.destroyed||!this._writeKey)throw new he;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 t=>{let n=await q(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 q(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 jn(e)}async persist(){if(this.destroyed)throw new he;this.isSessionOnly||(await Promise.all([Dr(this.exchangeKeyName,this.exchangeKey),Dr(this.writeKeyName,this.writeKey)]),this.hasPersisted=!0)}async delete(){let e=this.exchangeKeyName,t=this.writeKeyName;await _r([e,t]),this.constructor._invalidate(e,t),this._destroy()}_destroy(){this.destroyed=!0,this.hasPersisted=!1,this._exchangeKey=null,this._writeKey=null}static _invalidate(e,t){let n=this._instance;n&&n.exchangeKeyName===e&&n.writeKeyName===t&&(n._destroy(),this._instance=null)}static async delete(e={}){let t=e.encryptionKeyName||this.EXCHANGE_KEY_NAME,n=e.writeKeyName||this.WRITE_KEY_NAME;await _r([t,n]),this._invalidate(t,n)}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 q(o),y=await Me(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}){let t=e.persist!==!1,n=e.encryptionKeyName||this.EXCHANGE_KEY_NAME,i=e.writeKeyName||this.WRITE_KEY_NAME,o=this._instance;if(o&&!o.destroyed&&o.exchangeKeyName===n&&o.writeKeyName===i)return t&&!o.hasPersisted&&await o.persist(),o;let a=!0,y=await ke(n),c=await ke(i);y||(a=!1,y=await this._createExchangeKeys(e.extractable)),c||(a=!1,c=await this._createWriteKeys(e.extractable));let f=await q(c),A=await Me(new Uint8Array(f),this.TYPE==="ecc"?"ed25519":"rsa"),K=new this({keys:{exchange:y,write:c},did:A,hasPersisted:a,isSessionOnly:!!e.session,exchangeKeyName:n,writeKeyName:i});return this._instance=K,t&&!a&&await K.persist(),K}};async function ht(r,e){let{content:t,publicKey:n}=r,i=e||await d.create(),o=await d.encrypt(typeof t=="string"?h(t):t,typeof i=="string"?await d.import(i):i),a=await Ir({key:i,publicKey:n});return G(a,o)}s(ht,"encryptTo");ht.asString=async function(r,e){let t=await ht(r,e);return u(new Uint8Array(t),"base64pad")};async function Ir({key:r,publicKey:e},t){let n;r instanceof CryptoKey?n=await d.export(r):n=r;let i=await F.encrypt(n,e);return t&&t==="arraybuffer"?i:new Uint8Array(i)}s(Ir,"encryptKeyTo");Ir.asString=async function({key:r,publicKey:e},t){let n=await Ir({key:r,publicKey:e});return t?u(n,t):b(n)};async function jn(r){let e=await ct(new TextEncoder().encode(r.normalize("NFD")));return u(e,"base32").slice(0,32)}s(jn,"getDeviceName");var Nr=globalThis.crypto;function dt(r){return new Uint8Array(r).buffer}s(dt,"toArrayBuffer");var ne=class r extends de{static{s(this,"RsaKeys")}static TYPE="rsa";static INFO="keys";static EXCHANGE_KEY_NAME=ve;static WRITE_KEY_NAME=Te;static async create(e,t,n){if(n?.dbKey){let i=`${n.dbKey}.${ve}`,o=`${n.dbKey}.${Te}`;r.EXCHANGE_KEY_NAME=i,r.WRITE_KEY_NAME=o}return await super.create(e,t,n)}static async load(e={session:!1}){if(e.dbKey){let t=`${e.dbKey}.${ve}`,n=`${e.dbKey}.${Te}`;r.EXCHANGE_KEY_NAME=t,r.WRITE_KEY_NAME=n}return super.load(e)}static async _createExchangeKeys(e=!1){return await Cr(Ye,ee,"exchange",e)}static async _createWriteKeys(e=!1){return await Cr(Ye,ee,"write",e)}async toJson(e){let t=this.publicExchangeKey,n=this.DID,i=await Nr.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,f=y||await d.create({length:c||o}),A=await d.encrypt(typeof e=="string"?h(e):e,typeof f=="string"?await d.import(f):f),K=await Ae({key:f,publicKey:a}),C=G(K,A);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=re(e,v),a=o.slice(0,i),y=o.slice(i),c=await this.decryptKey(a);return await d.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 d.import(n)}else return e}};Object.assign(ne.prototype.sign,{asString:s(async function(r,e){return this.signAsString(r,e)},"asString")});Object.assign(ne.prototype.encrypt,{asString:s(async function(r,e,t,n){return this.encryptAsString(r,e,t,n)},"asString")});Object.assign(ne.prototype.decrypt,{asString:s(async function(r,e){return this.decryptAsString(r,e)},"asString")});async function qn(r,e,t){let n=je(t),i=await Le(n.publicKey.buffer,"SHA-256","signing");try{return F.verify(r,e,i)}catch{return!1}}s(qn,"verify");Object.assign(ne.prototype.decryptKey,{asString:s(async function(r,e){return this.decryptKeyAsString(r,e)},"asString")});async function At(r,e){let{content:t,publicKey:n}=r,i=e||await d.create(),o=await d.encrypt(typeof t=="string"?h(t):t,typeof i=="string"?await d.import(i):i),a=await Ae({key:i,publicKey:n});return G(a,o)}s(At,"encryptTo");At.asString=async function(r,e){let{content:t,publicKey:n}=r,i=e||await d.create(),o=await d.encrypt(typeof t=="string"?h(t):t,typeof i=="string"?await d.import(i):i,"arraybuffer"),a=await Ae({key:i,publicKey:n}),y=G(a,o);return u(new Uint8Array(y),"base64pad")};async function Ae({key:r,publicKey:e},t){let n;r instanceof CryptoKey?n=await d.export(r):n=r;let i=await F.encrypt(n,e);return t&&t==="arraybuffer"?i:new Uint8Array(i)}s(Ae,"encryptKeyTo");Ae.asString=async function({key:r,publicKey:e},t){let n=await Ae({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 Nr.subtle.encrypt({name:k,iv:dt(n)},i,dt(r)):await Zn(r,i);return t&&t==="arraybuffer"?o:new Uint8Array(o)}s(Jn,"encrypt");async function Zn(r,e,t){let n=j(r,t?.charsize??M),i=typeof e=="string"?await Ne(e,t):e,o=t?.iv||qe(S),a=await Nr.subtle.encrypt({name:k,iv:o},i,n);return G(o,a)}s(Zn,"encryptBytes");var bt={};E(bt,{EccKeys:()=>J,default:()=>Qn,exportPublicKey:()=>be,importPublicKey:()=>Re,verify:()=>ei});var ie=globalThis.crypto;function se(r){return new Uint8Array(r).buffer}s(se,"toArrayBuffer");var J=class r extends de{static{s(this,"EccKeys")}static TYPE="ecc";static EXCHANGE_KEY_NAME=De;static WRITE_KEY_NAME=_e;static INFO="keys";static async create(e,t,n){if(n?.dbKey){let i=`${n.dbKey}.${De}`,o=`${n.dbKey}.${_e}`;r.EXCHANGE_KEY_NAME=i,r.WRITE_KEY_NAME=o}return await super.create(e,t,n)}get publicExchangeKey(){let e=this.exchangeKey.publicKey;return Object.assign(e,{asString:s(async t=>{let n=await be(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 be(this.writeKey),i=new Uint8Array(n);return t?u(i,t):b(n)},"asString")})}static async _createExchangeKeys(e=!1){return await ie.subtle.generateKey({name:Pe},e,["deriveKey","deriveBits"])}static async _createWriteKeys(e=!1){return await ie.subtle.generateKey({name:Q},e,["sign","verify"])}static async load(e={session:!1}){if(e.dbKey){let t=`${e.dbKey}.${De}`,n=`${e.dbKey}.${_e}`;r.EXCHANGE_KEY_NAME=t,r.WRITE_KEY_NAME=n}return super.load(e)}async toJson(e){let t=this.DID,n=await be(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 Ke(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)),f=await crypto.subtle.encrypt({name:B,iv:c},a,se(y)),A=await be(i),K=new Uint8Array(o),C=new Uint8Array(c),oe=new Uint8Array(f),p=K.length+C.length+oe.length,l=new Uint8Array(p);return l.set(K,0),l.set(C,K.length),l.set(oe,K.length+C.length),{enc:b(new Uint8Array(A)),wrappedKey:b(l)}}async unwrap(e,t,n){let i=await Re(e,"X25519","exchange"),o=new Uint8Array(v(t)),a=o.slice(0,x),y=o.slice(x,x+S),c=o.slice(x+S),f=await Ke(this.exchangeKey.privateKey,i,a,n||"key-wrap"),A=await crypto.subtle.decrypt({name:B,iv:y},f,se(c));return await crypto.subtle.importKey("raw",A,{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 Re(y,"X25519","exchange"):c=y;let f=await r._createExchangeKeys(!0),A=i||await Ke(f.privateKey,c,a,n||r.INFO,o);A instanceof CryptoKey||(A=await Ke(f.privateKey,c,a,n||r.INFO));let K=crypto.getRandomValues(new Uint8Array(S)),C=typeof e=="string"?new TextEncoder().encode(e):e,oe=await crypto.subtle.encrypt({name:B,iv:K},A,se(C)),p=new Uint8Array(await be(f)),l=new Uint8Array(a),P=new Uint8Array(K),U=new Uint8Array(oe),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=re(e,v),o=i.slice(0,W),a=await ie.subtle.importKey("raw",o,{name:Pe},!0,[]),y=i.slice(W,W+x),c=i.slice(W+x,W+x+S),f=i.slice(W+x+S),{privateKey:A}=this.exchangeKey,K=await Ke(A,a,y,n||r.INFO);return await crypto.subtle.decrypt({name:t||B,iv:c},K,f)}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 Re(e,"X25519","exchange"):n=e;let i=crypto.getRandomValues(new Uint8Array(x));return await Ke(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 ie.subtle.sign({name:Q},this.writeKey.privateKey,se(i));return new Uint8Array(o)}async signAsString(e,t){let n=await this.sign(e,t);return u(n,"base64pad")}};Object.assign(J.prototype.sign,{asString:s(async function(r,e){let t=await this.sign(r,e);return u(t,"base64pad")},"asString")});async function be(r,e="raw"){return await crypto.subtle.exportKey(e,r.publicKey)}s(be,"exportPublicKey");async function Re(r,e,t){mt(t);let n=t==="exchange"?Pe:Q,i=t==="exchange"?[]:["verify"],o=v(r),a=e==="X25519"?{name:n}:{name:n,namedCurve:e};return ie.subtle.importKey("raw",o,a,!0,i)}s(Re,"importPublicKey");var Qn={EccKeys:J,importPublicKey:Re};Object.assign(J.prototype.encrypt,{asString:s(async function(r,e,t,n,i){return this.encryptAsString(r,e,t,n,i)},"asString")});Object.assign(J.prototype.decrypt,{asString:s(async function(r,e){return this.decryptAsString(r,e)},"asString")});Object.assign(J.prototype.sign,{asString:s(async function(r,e){return this.signAsString(r,e)},"asString")});async function ei(r,e,t){try{let n=je(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 ie.subtle.importKey("raw",i,{name:Q},!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 ie.subtle.verify({name:Q},o,se(c),se(y))}catch{return!1}}s(ei,"verify");async function Ke(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?se(t):t,info:o.encode(n)},y,{name:"AES-GCM",length:i||R},!1,["encrypt","decrypt"])}s(Ke,"deriveKey");})();
|