@substrate-system/keys 0.2.11 → 0.2.13
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 +30 -3
- package/dist/_base.cjs +10 -10
- package/dist/_base.cjs.map +3 -3
- package/dist/_base.d.ts +2 -2
- package/dist/_base.d.ts.map +1 -1
- package/dist/_base.js +6 -4
- package/dist/_base.js.map +2 -2
- package/dist/_base.min.cjs +1 -1
- package/dist/_base.min.cjs.map +2 -2
- package/dist/_base.min.js +2 -2
- package/dist/_base.min.js.map +4 -4
- package/dist/aes/index.min.js +2 -2
- package/dist/aes/index.min.js.map +4 -4
- package/dist/browser/index.js +258 -185
- package/dist/browser/index.min.js +2 -2
- package/dist/crypto.cjs +121 -15
- package/dist/crypto.cjs.map +3 -3
- package/dist/crypto.d.ts +37 -5
- package/dist/crypto.d.ts.map +1 -1
- package/dist/crypto.js +126 -17
- package/dist/crypto.js.map +3 -3
- package/dist/crypto.min.cjs +2 -2
- package/dist/crypto.min.cjs.map +3 -3
- package/dist/crypto.min.js +2 -2
- package/dist/crypto.min.js.map +4 -4
- package/dist/ecc/index.min.js +2 -2
- package/dist/ecc/index.min.js.map +4 -4
- package/dist/index.cjs +1 -0
- package/dist/index.cjs.map +2 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +2 -2
- package/dist/index.min.cjs +1 -1
- package/dist/index.min.cjs.map +2 -2
- package/dist/index.min.js +2 -2
- package/dist/index.min.js.map +4 -4
- package/dist/meta.json +37 -39
- package/dist/rsa/index.cjs +4 -4
- package/dist/rsa/index.cjs.map +3 -3
- package/dist/rsa/index.d.ts +2 -2
- package/dist/rsa/index.d.ts.map +1 -1
- package/dist/rsa/index.js +4 -4
- package/dist/rsa/index.js.map +2 -2
- package/dist/rsa/index.min.cjs +1 -1
- package/dist/rsa/index.min.cjs.map +2 -2
- package/dist/rsa/index.min.js +2 -2
- package/dist/rsa/index.min.js.map +4 -4
- package/dist/util.cjs +13 -58
- package/dist/util.cjs.map +3 -3
- package/dist/util.d.ts +9 -16
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +13 -48
- package/dist/util.js.map +3 -3
- package/dist/util.min.cjs +1 -1
- package/dist/util.min.cjs.map +3 -3
- package/dist/util.min.js +1 -3
- package/dist/util.min.js.map +4 -4
- package/package.json +2 -2
package/dist/browser/index.js
CHANGED
|
@@ -2399,7 +2399,7 @@
|
|
|
2399
2399
|
getPublicKeyAsArrayBuffer: () => getPublicKeyAsArrayBuffer,
|
|
2400
2400
|
getPublicKeyAsUint8Array: () => getPublicKeyAsUint8Array,
|
|
2401
2401
|
publicKeyToDid: () => publicKeyToDid,
|
|
2402
|
-
verify: () =>
|
|
2402
|
+
verify: () => verify2
|
|
2403
2403
|
});
|
|
2404
2404
|
|
|
2405
2405
|
// node_modules/@substrate-system/one-webcrypto/browser.mjs
|
|
@@ -3673,12 +3673,6 @@
|
|
|
3673
3673
|
importAesKey: () => importAesKey
|
|
3674
3674
|
});
|
|
3675
3675
|
|
|
3676
|
-
// src/util.ts
|
|
3677
|
-
var import_tweetnacl2 = __toESM(require_nacl_fast(), 1);
|
|
3678
|
-
|
|
3679
|
-
// src/crypto.ts
|
|
3680
|
-
var import_tweetnacl = __toESM(require_nacl_fast(), 1);
|
|
3681
|
-
|
|
3682
3676
|
// src/constants.ts
|
|
3683
3677
|
var BASE58_DID_PREFIX = "did:key:z";
|
|
3684
3678
|
var ECC_WRITE_ALG = "Ed25519";
|
|
@@ -3714,155 +3708,9 @@
|
|
|
3714
3708
|
var DEFAULT_ECC_WRITE = "ecc-write";
|
|
3715
3709
|
var SALT_LENGTH = 16;
|
|
3716
3710
|
|
|
3717
|
-
// src/
|
|
3718
|
-
async function ed25519Verify({
|
|
3719
|
-
message,
|
|
3720
|
-
publicKey,
|
|
3721
|
-
signature
|
|
3722
|
-
}) {
|
|
3723
|
-
return import_tweetnacl.default.sign.detached.verify(message, signature, publicKey);
|
|
3724
|
-
}
|
|
3725
|
-
__name(ed25519Verify, "ed25519Verify");
|
|
3726
|
-
var did = {
|
|
3727
|
-
keyTypes: {
|
|
3728
|
-
"bls12-381": {
|
|
3729
|
-
magicBytes: new Uint8Array([234, 1]),
|
|
3730
|
-
verify: /* @__PURE__ */ __name(() => {
|
|
3731
|
-
throw new Error("Not implemented");
|
|
3732
|
-
}, "verify")
|
|
3733
|
-
},
|
|
3734
|
-
rsa: {
|
|
3735
|
-
magicBytes: new Uint8Array([0, 245, 2]),
|
|
3736
|
-
verify: rsaVerify
|
|
3737
|
-
},
|
|
3738
|
-
ed25519: {
|
|
3739
|
-
magicBytes: new Uint8Array([237, 1]),
|
|
3740
|
-
verify: ed25519Verify
|
|
3741
|
-
}
|
|
3742
|
-
}
|
|
3743
|
-
};
|
|
3744
|
-
async function rsaVerify({
|
|
3745
|
-
message,
|
|
3746
|
-
publicKey,
|
|
3747
|
-
signature
|
|
3748
|
-
}) {
|
|
3749
|
-
return rsaOperations.verify(
|
|
3750
|
-
message,
|
|
3751
|
-
signature,
|
|
3752
|
-
await webcrypto.subtle.importKey(
|
|
3753
|
-
"spki",
|
|
3754
|
-
toArrayBuffer(publicKey),
|
|
3755
|
-
{ name: RSA_SIGN_ALGORITHM, hash: RSA_HASHING_ALGORITHM },
|
|
3756
|
-
false,
|
|
3757
|
-
["verify"]
|
|
3758
|
-
),
|
|
3759
|
-
8
|
|
3760
|
-
);
|
|
3761
|
-
}
|
|
3762
|
-
__name(rsaVerify, "rsaVerify");
|
|
3763
|
-
var rsaOperations = {
|
|
3764
|
-
verify: /* @__PURE__ */ __name(async function rsaVerify2(msg, sig, publicKey, charSize = DEFAULT_CHAR_SIZE, hashAlg = DEFAULT_HASH_ALGORITHM) {
|
|
3765
|
-
return webcrypto.subtle.verify(
|
|
3766
|
-
{
|
|
3767
|
-
name: RSA_SIGN_ALGORITHM,
|
|
3768
|
-
saltLength: SALT_LENGTH
|
|
3769
|
-
},
|
|
3770
|
-
typeof publicKey === "string" ? await importPublicKey(publicKey, hashAlg, "signing" /* Sign */) : publicKey,
|
|
3771
|
-
normalizeBase64ToBuf(sig),
|
|
3772
|
-
normalizeUnicodeToBuf(msg, charSize)
|
|
3773
|
-
);
|
|
3774
|
-
}, "rsaVerify"),
|
|
3775
|
-
sign: /* @__PURE__ */ __name(async function sign(msg, privateKey, charSize = DEFAULT_CHAR_SIZE) {
|
|
3776
|
-
return webcrypto.subtle.sign(
|
|
3777
|
-
{ name: RSA_SIGN_ALGORITHM, saltLength: SALT_LENGTH },
|
|
3778
|
-
privateKey,
|
|
3779
|
-
normalizeUnicodeToBuf(msg, charSize)
|
|
3780
|
-
);
|
|
3781
|
-
}, "sign"),
|
|
3782
|
-
/**
|
|
3783
|
-
* Encrypt the given AES key *to* a given public key.
|
|
3784
|
-
*/
|
|
3785
|
-
encrypt: /* @__PURE__ */ __name(async function rsaEncrypt(msg, publicKey, charSize = DEFAULT_CHAR_SIZE, hashAlg = DEFAULT_HASH_ALGORITHM) {
|
|
3786
|
-
let pubKey;
|
|
3787
|
-
if (typeof publicKey === "string") {
|
|
3788
|
-
pubKey = await importPublicKey(publicKey, hashAlg, "exchange" /* Exchange */);
|
|
3789
|
-
} else {
|
|
3790
|
-
pubKey = publicKey instanceof Uint8Array ? await importPublicKey(toArrayBuffer(publicKey), hashAlg, "exchange" /* Exchange */) : publicKey;
|
|
3791
|
-
}
|
|
3792
|
-
return webcrypto.subtle.encrypt(
|
|
3793
|
-
{ name: RSA_ALGORITHM },
|
|
3794
|
-
pubKey,
|
|
3795
|
-
normalizeUnicodeToBuf(msg, charSize)
|
|
3796
|
-
);
|
|
3797
|
-
}, "rsaEncrypt"),
|
|
3798
|
-
/**
|
|
3799
|
-
* Use RSA to decrypt the given data.
|
|
3800
|
-
*/
|
|
3801
|
-
decrypt: /* @__PURE__ */ __name(async function rsaDecrypt(_data, privateKey) {
|
|
3802
|
-
const key = isCryptoKey(privateKey) ? privateKey : await importRsaKey(privateKey, ["decrypt"]);
|
|
3803
|
-
let data;
|
|
3804
|
-
if (typeof _data === "string") {
|
|
3805
|
-
data = fromString2(_data, "base64pad");
|
|
3806
|
-
} else {
|
|
3807
|
-
data = _data;
|
|
3808
|
-
}
|
|
3809
|
-
const arrayBuffer = await webcrypto.subtle.decrypt(
|
|
3810
|
-
{ name: RSA_ALGORITHM },
|
|
3811
|
-
key,
|
|
3812
|
-
data instanceof Uint8Array ? toArrayBuffer(data) : data
|
|
3813
|
-
);
|
|
3814
|
-
const arr = new Uint8Array(arrayBuffer);
|
|
3815
|
-
return arr;
|
|
3816
|
-
}, "rsaDecrypt")
|
|
3817
|
-
};
|
|
3818
|
-
async function importPublicKey(base64Key, hashAlg, use) {
|
|
3819
|
-
checkValidKeyUse(use);
|
|
3820
|
-
const alg = use === "exchange" /* Exchange */ ? RSA_ALGORITHM : RSA_SIGN_ALGORITHM;
|
|
3821
|
-
const uses = use === "exchange" /* Exchange */ ? ["encrypt"] : ["verify"];
|
|
3822
|
-
const buf = typeof base64Key === "string" ? base64ToArrBuf(stripKeyHeader(base64Key)) : base64Key;
|
|
3823
|
-
return webcrypto.subtle.importKey("spki", buf, {
|
|
3824
|
-
name: alg,
|
|
3825
|
-
hash: { name: hashAlg }
|
|
3826
|
-
}, true, uses);
|
|
3827
|
-
}
|
|
3828
|
-
__name(importPublicKey, "importPublicKey");
|
|
3711
|
+
// src/util.ts
|
|
3829
3712
|
var InvalidKeyUse = new Error("Invalid key use. Please use 'encryption' or 'signing");
|
|
3830
3713
|
var InvalidMaxValue = new Error("Max must be less than 256 and greater than 0");
|
|
3831
|
-
function checkValidKeyUse(use) {
|
|
3832
|
-
checkValid(use, ["signing" /* Sign */, "exchange" /* Exchange */], InvalidKeyUse);
|
|
3833
|
-
}
|
|
3834
|
-
__name(checkValidKeyUse, "checkValidKeyUse");
|
|
3835
|
-
function checkValid(toCheck, opts, error) {
|
|
3836
|
-
const match = opts.some((opt) => opt === toCheck);
|
|
3837
|
-
if (!match) {
|
|
3838
|
-
throw error;
|
|
3839
|
-
}
|
|
3840
|
-
}
|
|
3841
|
-
__name(checkValid, "checkValid");
|
|
3842
|
-
function stripKeyHeader(base64Key) {
|
|
3843
|
-
return base64Key.replace("-----BEGIN PUBLIC KEY-----\n", "").replace("\n-----END PUBLIC KEY-----", "");
|
|
3844
|
-
}
|
|
3845
|
-
__name(stripKeyHeader, "stripKeyHeader");
|
|
3846
|
-
function isCryptoKey(val) {
|
|
3847
|
-
return hasProp(val, "algorithm") && hasProp(val, "extractable") && hasProp(val, "type");
|
|
3848
|
-
}
|
|
3849
|
-
__name(isCryptoKey, "isCryptoKey");
|
|
3850
|
-
function hasProp(data, prop) {
|
|
3851
|
-
return typeof data === "object" && data != null && prop in data;
|
|
3852
|
-
}
|
|
3853
|
-
__name(hasProp, "hasProp");
|
|
3854
|
-
function importRsaKey(key, keyUsages) {
|
|
3855
|
-
return webcrypto.subtle.importKey(
|
|
3856
|
-
"spki",
|
|
3857
|
-
key instanceof Uint8Array ? toArrayBuffer(key) : key,
|
|
3858
|
-
{ name: RSA_ALGORITHM, hash: RSA_HASHING_ALGORITHM },
|
|
3859
|
-
false,
|
|
3860
|
-
keyUsages
|
|
3861
|
-
);
|
|
3862
|
-
}
|
|
3863
|
-
__name(importRsaKey, "importRsaKey");
|
|
3864
|
-
|
|
3865
|
-
// src/util.ts
|
|
3866
3714
|
function toArrayBuffer(data) {
|
|
3867
3715
|
return new Uint8Array(data).buffer;
|
|
3868
3716
|
}
|
|
@@ -3873,16 +3721,6 @@
|
|
|
3873
3721
|
);
|
|
3874
3722
|
}
|
|
3875
3723
|
__name(sha2562, "sha256");
|
|
3876
|
-
async function publicKeyToDid(_publicKey, keyType = "rsa") {
|
|
3877
|
-
const publicKey = _publicKey instanceof CryptoKey ? new Uint8Array(await getPublicKeyAsArrayBuffer(_publicKey)) : _publicKey;
|
|
3878
|
-
const prefix = did.keyTypes[keyType]?.magicBytes;
|
|
3879
|
-
if (!prefix) {
|
|
3880
|
-
throw new Error(`Key type '${keyType}' not supported, available types: ${Object.keys(did.keyTypes).join(", ")}`);
|
|
3881
|
-
}
|
|
3882
|
-
const prefixedBuf = concat([prefix, publicKey]);
|
|
3883
|
-
return BASE58_DID_PREFIX + toString2(prefixedBuf, "base58btc");
|
|
3884
|
-
}
|
|
3885
|
-
__name(publicKeyToDid, "publicKeyToDid");
|
|
3886
3724
|
function base64ToArrBuf(string2) {
|
|
3887
3725
|
const uint8 = fromString2(string2, "base64pad");
|
|
3888
3726
|
return toArrayBuffer(uint8);
|
|
@@ -3925,22 +3763,6 @@
|
|
|
3925
3763
|
}
|
|
3926
3764
|
}
|
|
3927
3765
|
__name(normalizeUnicodeToBuf, "normalizeUnicodeToBuf");
|
|
3928
|
-
async function getPublicKeyAsArrayBuffer(keypair) {
|
|
3929
|
-
const spki = keypair instanceof CryptoKey ? await webcrypto.subtle.exportKey(
|
|
3930
|
-
"spki",
|
|
3931
|
-
keypair
|
|
3932
|
-
) : await webcrypto.subtle.exportKey(
|
|
3933
|
-
"spki",
|
|
3934
|
-
keypair.publicKey
|
|
3935
|
-
);
|
|
3936
|
-
return spki;
|
|
3937
|
-
}
|
|
3938
|
-
__name(getPublicKeyAsArrayBuffer, "getPublicKeyAsArrayBuffer");
|
|
3939
|
-
async function getPublicKeyAsUint8Array(keypair) {
|
|
3940
|
-
const arr = await getPublicKeyAsArrayBuffer(keypair);
|
|
3941
|
-
return new Uint8Array(arr);
|
|
3942
|
-
}
|
|
3943
|
-
__name(getPublicKeyAsUint8Array, "getPublicKeyAsUint8Array");
|
|
3944
3766
|
function didToPublicKey(did2) {
|
|
3945
3767
|
if (!did2.startsWith(BASE58_DID_PREFIX)) {
|
|
3946
3768
|
throw new Error(
|
|
@@ -4064,6 +3886,257 @@
|
|
|
4064
3886
|
}
|
|
4065
3887
|
__name(publicExponent, "publicExponent");
|
|
4066
3888
|
|
|
3889
|
+
// src/crypto.ts
|
|
3890
|
+
var import_tweetnacl = __toESM(require_nacl_fast(), 1);
|
|
3891
|
+
var did = {
|
|
3892
|
+
keyTypes: {
|
|
3893
|
+
"bls12-381": {
|
|
3894
|
+
magicBytes: new Uint8Array([234, 1]),
|
|
3895
|
+
verify: /* @__PURE__ */ __name(() => {
|
|
3896
|
+
throw new Error("Not implemented");
|
|
3897
|
+
}, "verify")
|
|
3898
|
+
},
|
|
3899
|
+
rsa: {
|
|
3900
|
+
magicBytes: new Uint8Array([0, 245, 2]),
|
|
3901
|
+
verify: rsaVerify
|
|
3902
|
+
},
|
|
3903
|
+
ed25519: {
|
|
3904
|
+
magicBytes: new Uint8Array([237, 1]),
|
|
3905
|
+
verify: ed25519Verify
|
|
3906
|
+
}
|
|
3907
|
+
}
|
|
3908
|
+
};
|
|
3909
|
+
async function verify({
|
|
3910
|
+
message,
|
|
3911
|
+
publicKey,
|
|
3912
|
+
signature
|
|
3913
|
+
}) {
|
|
3914
|
+
const format2 = getAlgorithm(publicKey);
|
|
3915
|
+
const pub = didToPublicKey2(publicKey).publicKey;
|
|
3916
|
+
if (format2 === "ed25519") {
|
|
3917
|
+
return ed25519Verify({ message, publicKey: pub, signature });
|
|
3918
|
+
}
|
|
3919
|
+
if (format2 === "rsa") {
|
|
3920
|
+
return rsaVerify({ message, publicKey: pub, signature });
|
|
3921
|
+
}
|
|
3922
|
+
throw new Error("Unknown key format");
|
|
3923
|
+
}
|
|
3924
|
+
__name(verify, "verify");
|
|
3925
|
+
function getAlgorithm(did2) {
|
|
3926
|
+
try {
|
|
3927
|
+
const { type } = didToPublicKey2(did2);
|
|
3928
|
+
return type;
|
|
3929
|
+
} catch {
|
|
3930
|
+
return "unknown";
|
|
3931
|
+
}
|
|
3932
|
+
}
|
|
3933
|
+
__name(getAlgorithm, "getAlgorithm");
|
|
3934
|
+
async function ed25519Verify({
|
|
3935
|
+
message,
|
|
3936
|
+
publicKey,
|
|
3937
|
+
signature
|
|
3938
|
+
}) {
|
|
3939
|
+
let msg = message;
|
|
3940
|
+
if (typeof message === "string") {
|
|
3941
|
+
msg = fromString2(message, "utf8");
|
|
3942
|
+
}
|
|
3943
|
+
let sig = signature;
|
|
3944
|
+
if (typeof signature === "string") {
|
|
3945
|
+
sig = fromString2(signature, "base64pad");
|
|
3946
|
+
}
|
|
3947
|
+
let pub = publicKey;
|
|
3948
|
+
if (typeof publicKey === "string") {
|
|
3949
|
+
pub = didToPublicKey2(publicKey).publicKey;
|
|
3950
|
+
}
|
|
3951
|
+
console.log("Message length:", msg.length);
|
|
3952
|
+
console.log("Signature length:", sig.length, "expected: 64");
|
|
3953
|
+
console.log("Public key length:", pub.length, "expected: 32");
|
|
3954
|
+
if (pub.length !== 32) {
|
|
3955
|
+
throw new Error(`Invalid Ed25519 public key size: ${pub.length} bytes, expected 32`);
|
|
3956
|
+
}
|
|
3957
|
+
if (sig.length !== 64) {
|
|
3958
|
+
throw new Error(`Invalid Ed25519 signature size: ${sig.length} bytes, expected 64`);
|
|
3959
|
+
}
|
|
3960
|
+
return import_tweetnacl.default.sign.detached.verify(msg, sig, pub);
|
|
3961
|
+
}
|
|
3962
|
+
__name(ed25519Verify, "ed25519Verify");
|
|
3963
|
+
async function rsaVerify({
|
|
3964
|
+
message,
|
|
3965
|
+
publicKey,
|
|
3966
|
+
signature
|
|
3967
|
+
}) {
|
|
3968
|
+
let pub = publicKey;
|
|
3969
|
+
if (typeof publicKey === "string") {
|
|
3970
|
+
pub = didToPublicKey2(publicKey).publicKey;
|
|
3971
|
+
}
|
|
3972
|
+
return rsaOperations.verify(
|
|
3973
|
+
message,
|
|
3974
|
+
signature,
|
|
3975
|
+
await webcrypto.subtle.importKey(
|
|
3976
|
+
"spki",
|
|
3977
|
+
toArrayBuffer(pub),
|
|
3978
|
+
{ name: RSA_SIGN_ALGORITHM, hash: RSA_HASHING_ALGORITHM },
|
|
3979
|
+
false,
|
|
3980
|
+
["verify"]
|
|
3981
|
+
),
|
|
3982
|
+
8
|
|
3983
|
+
);
|
|
3984
|
+
}
|
|
3985
|
+
__name(rsaVerify, "rsaVerify");
|
|
3986
|
+
var rsaOperations = {
|
|
3987
|
+
verify: /* @__PURE__ */ __name(async function rsaVerify2(msg, sig, publicKey, charSize = DEFAULT_CHAR_SIZE, hashAlg = DEFAULT_HASH_ALGORITHM) {
|
|
3988
|
+
return webcrypto.subtle.verify(
|
|
3989
|
+
{
|
|
3990
|
+
name: RSA_SIGN_ALGORITHM,
|
|
3991
|
+
saltLength: SALT_LENGTH
|
|
3992
|
+
},
|
|
3993
|
+
typeof publicKey === "string" ? await importPublicKey(publicKey, hashAlg, "signing" /* Sign */) : publicKey,
|
|
3994
|
+
normalizeBase64ToBuf(sig),
|
|
3995
|
+
normalizeUnicodeToBuf(msg, charSize)
|
|
3996
|
+
);
|
|
3997
|
+
}, "rsaVerify"),
|
|
3998
|
+
sign: /* @__PURE__ */ __name(async function sign(msg, privateKey, charSize = DEFAULT_CHAR_SIZE) {
|
|
3999
|
+
return webcrypto.subtle.sign(
|
|
4000
|
+
{ name: RSA_SIGN_ALGORITHM, saltLength: SALT_LENGTH },
|
|
4001
|
+
privateKey,
|
|
4002
|
+
normalizeUnicodeToBuf(msg, charSize)
|
|
4003
|
+
);
|
|
4004
|
+
}, "sign"),
|
|
4005
|
+
/**
|
|
4006
|
+
* Encrypt the given AES key *to* a given public key.
|
|
4007
|
+
*/
|
|
4008
|
+
encrypt: /* @__PURE__ */ __name(async function rsaEncrypt(msg, publicKey, charSize = DEFAULT_CHAR_SIZE, hashAlg = DEFAULT_HASH_ALGORITHM) {
|
|
4009
|
+
let pubKey;
|
|
4010
|
+
if (typeof publicKey === "string") {
|
|
4011
|
+
pubKey = await importPublicKey(publicKey, hashAlg, "exchange" /* Exchange */);
|
|
4012
|
+
} else {
|
|
4013
|
+
pubKey = publicKey instanceof Uint8Array ? await importPublicKey(toArrayBuffer(publicKey), hashAlg, "exchange" /* Exchange */) : publicKey;
|
|
4014
|
+
}
|
|
4015
|
+
return webcrypto.subtle.encrypt(
|
|
4016
|
+
{ name: RSA_ALGORITHM },
|
|
4017
|
+
pubKey,
|
|
4018
|
+
normalizeUnicodeToBuf(msg, charSize)
|
|
4019
|
+
);
|
|
4020
|
+
}, "rsaEncrypt"),
|
|
4021
|
+
/**
|
|
4022
|
+
* Use RSA to decrypt the given data.
|
|
4023
|
+
*/
|
|
4024
|
+
decrypt: /* @__PURE__ */ __name(async function rsaDecrypt(_data, privateKey) {
|
|
4025
|
+
const key = isCryptoKey(privateKey) ? privateKey : await importRsaKey(privateKey, ["decrypt"]);
|
|
4026
|
+
let data;
|
|
4027
|
+
if (typeof _data === "string") {
|
|
4028
|
+
data = fromString2(_data, "base64pad");
|
|
4029
|
+
} else {
|
|
4030
|
+
data = _data;
|
|
4031
|
+
}
|
|
4032
|
+
const arrayBuffer = await webcrypto.subtle.decrypt(
|
|
4033
|
+
{ name: RSA_ALGORITHM },
|
|
4034
|
+
key,
|
|
4035
|
+
data instanceof Uint8Array ? toArrayBuffer(data) : data
|
|
4036
|
+
);
|
|
4037
|
+
const arr = new Uint8Array(arrayBuffer);
|
|
4038
|
+
return arr;
|
|
4039
|
+
}, "rsaDecrypt")
|
|
4040
|
+
};
|
|
4041
|
+
async function importPublicKey(base64Key, hashAlg, use) {
|
|
4042
|
+
checkValidKeyUse(use);
|
|
4043
|
+
const alg = use === "exchange" /* Exchange */ ? RSA_ALGORITHM : RSA_SIGN_ALGORITHM;
|
|
4044
|
+
const uses = use === "exchange" /* Exchange */ ? ["encrypt"] : ["verify"];
|
|
4045
|
+
const buf = typeof base64Key === "string" ? base64ToArrBuf(stripKeyHeader(base64Key)) : base64Key;
|
|
4046
|
+
return webcrypto.subtle.importKey("spki", buf, {
|
|
4047
|
+
name: alg,
|
|
4048
|
+
hash: { name: hashAlg }
|
|
4049
|
+
}, true, uses);
|
|
4050
|
+
}
|
|
4051
|
+
__name(importPublicKey, "importPublicKey");
|
|
4052
|
+
function checkValidKeyUse(use) {
|
|
4053
|
+
checkValid(use, ["signing" /* Sign */, "exchange" /* Exchange */], InvalidKeyUse);
|
|
4054
|
+
}
|
|
4055
|
+
__name(checkValidKeyUse, "checkValidKeyUse");
|
|
4056
|
+
function checkValid(toCheck, opts, error) {
|
|
4057
|
+
const match = opts.some((opt) => opt === toCheck);
|
|
4058
|
+
if (!match) {
|
|
4059
|
+
throw error;
|
|
4060
|
+
}
|
|
4061
|
+
}
|
|
4062
|
+
__name(checkValid, "checkValid");
|
|
4063
|
+
function stripKeyHeader(base64Key) {
|
|
4064
|
+
return base64Key.replace("-----BEGIN PUBLIC KEY-----\n", "").replace("\n-----END PUBLIC KEY-----", "");
|
|
4065
|
+
}
|
|
4066
|
+
__name(stripKeyHeader, "stripKeyHeader");
|
|
4067
|
+
function isCryptoKey(val) {
|
|
4068
|
+
return hasProp(val, "algorithm") && hasProp(val, "extractable") && hasProp(val, "type");
|
|
4069
|
+
}
|
|
4070
|
+
__name(isCryptoKey, "isCryptoKey");
|
|
4071
|
+
function hasProp(data, prop) {
|
|
4072
|
+
return typeof data === "object" && data != null && prop in data;
|
|
4073
|
+
}
|
|
4074
|
+
__name(hasProp, "hasProp");
|
|
4075
|
+
function importRsaKey(key, keyUsages) {
|
|
4076
|
+
return webcrypto.subtle.importKey(
|
|
4077
|
+
"spki",
|
|
4078
|
+
key instanceof Uint8Array ? toArrayBuffer(key) : key,
|
|
4079
|
+
{ name: RSA_ALGORITHM, hash: RSA_HASHING_ALGORITHM },
|
|
4080
|
+
false,
|
|
4081
|
+
keyUsages
|
|
4082
|
+
);
|
|
4083
|
+
}
|
|
4084
|
+
__name(importRsaKey, "importRsaKey");
|
|
4085
|
+
async function publicKeyToDid(_publicKey, keyType = "rsa") {
|
|
4086
|
+
const publicKey = _publicKey instanceof CryptoKey ? new Uint8Array(await getPublicKeyAsArrayBuffer(_publicKey)) : _publicKey;
|
|
4087
|
+
const prefix = did.keyTypes[keyType]?.magicBytes;
|
|
4088
|
+
if (!prefix) {
|
|
4089
|
+
throw new Error(`Key type '${keyType}' not supported, available types: ${Object.keys(did.keyTypes).join(", ")}`);
|
|
4090
|
+
}
|
|
4091
|
+
const prefixedBuf = concat([prefix, publicKey]);
|
|
4092
|
+
return BASE58_DID_PREFIX + toString2(prefixedBuf, "base58btc");
|
|
4093
|
+
}
|
|
4094
|
+
__name(publicKeyToDid, "publicKeyToDid");
|
|
4095
|
+
async function getPublicKeyAsArrayBuffer(keypair) {
|
|
4096
|
+
const spki = keypair instanceof CryptoKey ? await webcrypto.subtle.exportKey(
|
|
4097
|
+
"spki",
|
|
4098
|
+
keypair
|
|
4099
|
+
) : await webcrypto.subtle.exportKey(
|
|
4100
|
+
"spki",
|
|
4101
|
+
keypair.publicKey
|
|
4102
|
+
);
|
|
4103
|
+
return spki;
|
|
4104
|
+
}
|
|
4105
|
+
__name(getPublicKeyAsArrayBuffer, "getPublicKeyAsArrayBuffer");
|
|
4106
|
+
async function getPublicKeyAsUint8Array(keypair) {
|
|
4107
|
+
const arr = await getPublicKeyAsArrayBuffer(keypair);
|
|
4108
|
+
return new Uint8Array(arr);
|
|
4109
|
+
}
|
|
4110
|
+
__name(getPublicKeyAsUint8Array, "getPublicKeyAsUint8Array");
|
|
4111
|
+
function didToPublicKey2(inputDid) {
|
|
4112
|
+
if (!inputDid.startsWith(BASE58_DID_PREFIX)) {
|
|
4113
|
+
throw new Error("Please use a base58-encoded DID formatted `did:key:z...`");
|
|
4114
|
+
}
|
|
4115
|
+
const didWithoutPrefix = inputDid.substr(BASE58_DID_PREFIX.length);
|
|
4116
|
+
const magicalBuf = fromString2(didWithoutPrefix, "base58btc");
|
|
4117
|
+
const result = Object.entries(did.keyTypes).find(
|
|
4118
|
+
([_key, attr]) => hasPrefix(
|
|
4119
|
+
magicalBuf.buffer,
|
|
4120
|
+
attr.magicBytes.buffer
|
|
4121
|
+
)
|
|
4122
|
+
);
|
|
4123
|
+
if (!result) {
|
|
4124
|
+
throw new Error("Unsupported key algorithm.");
|
|
4125
|
+
}
|
|
4126
|
+
const rawKeyData = magicalBuf.slice(result[1].magicBytes.length);
|
|
4127
|
+
if (result[0] === "ed25519" && rawKeyData.length === 44) {
|
|
4128
|
+
return {
|
|
4129
|
+
publicKey: rawKeyData.slice(-32),
|
|
4130
|
+
type: result[0]
|
|
4131
|
+
};
|
|
4132
|
+
}
|
|
4133
|
+
return {
|
|
4134
|
+
publicKey: rawKeyData,
|
|
4135
|
+
type: result[0]
|
|
4136
|
+
};
|
|
4137
|
+
}
|
|
4138
|
+
__name(didToPublicKey2, "didToPublicKey");
|
|
4139
|
+
|
|
4067
4140
|
// src/aes/index.ts
|
|
4068
4141
|
function toArrayBuffer2(data) {
|
|
4069
4142
|
return new Uint8Array(data).buffer;
|
|
@@ -4582,7 +4655,7 @@
|
|
|
4582
4655
|
return this.decryptAsString(msg, keysize);
|
|
4583
4656
|
}, "asString")
|
|
4584
4657
|
});
|
|
4585
|
-
async function
|
|
4658
|
+
async function verify2(msg, sig, signingDid) {
|
|
4586
4659
|
const _key = didToPublicKey(signingDid);
|
|
4587
4660
|
const key = await importPublicKey(
|
|
4588
4661
|
toArrayBuffer3(_key.publicKey),
|
|
@@ -4596,7 +4669,7 @@
|
|
|
4596
4669
|
return false;
|
|
4597
4670
|
}
|
|
4598
4671
|
}
|
|
4599
|
-
__name(
|
|
4672
|
+
__name(verify2, "verify");
|
|
4600
4673
|
Object.assign(RsaKeys.prototype.decryptKey, {
|
|
4601
4674
|
asString: /* @__PURE__ */ __name(async function(msg, format2) {
|
|
4602
4675
|
return this.decryptKeyAsString(msg, format2);
|
|
@@ -4667,7 +4740,7 @@
|
|
|
4667
4740
|
default: () => ecc_default,
|
|
4668
4741
|
exportPublicKey: () => exportPublicKey,
|
|
4669
4742
|
importPublicKey: () => importPublicKey2,
|
|
4670
|
-
verify: () =>
|
|
4743
|
+
verify: () => verify3
|
|
4671
4744
|
});
|
|
4672
4745
|
|
|
4673
4746
|
// src/errors.ts
|
|
@@ -4997,7 +5070,7 @@
|
|
|
4997
5070
|
return this.signAsString(msg, _charsize);
|
|
4998
5071
|
}, "asString")
|
|
4999
5072
|
});
|
|
5000
|
-
async function
|
|
5073
|
+
async function verify3(msg, sig, signingDid) {
|
|
5001
5074
|
try {
|
|
5002
5075
|
const _key = didToPublicKey(signingDid);
|
|
5003
5076
|
if (_key.type !== "ed25519") {
|
|
@@ -5037,7 +5110,7 @@
|
|
|
5037
5110
|
return false;
|
|
5038
5111
|
}
|
|
5039
5112
|
}
|
|
5040
|
-
__name(
|
|
5113
|
+
__name(verify3, "verify");
|
|
5041
5114
|
async function deriveKey(privateKey, publicKey, salt, info, keysize) {
|
|
5042
5115
|
const encoder = new TextEncoder();
|
|
5043
5116
|
const sharedSecret = await crypto.subtle.deriveBits(
|