@substrate-system/keys 0.2.11 → 0.2.12

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.
Files changed (50) hide show
  1. package/dist/_base.cjs +10 -10
  2. package/dist/_base.cjs.map +3 -3
  3. package/dist/_base.d.ts +2 -2
  4. package/dist/_base.d.ts.map +1 -1
  5. package/dist/_base.js +6 -4
  6. package/dist/_base.js.map +2 -2
  7. package/dist/_base.min.cjs +1 -1
  8. package/dist/_base.min.cjs.map +2 -2
  9. package/dist/_base.min.js +2 -2
  10. package/dist/_base.min.js.map +4 -4
  11. package/dist/aes/index.min.js +2 -2
  12. package/dist/aes/index.min.js.map +4 -4
  13. package/dist/browser/index.js +174 -179
  14. package/dist/browser/index.min.js +2 -2
  15. package/dist/crypto.cjs +30 -5
  16. package/dist/crypto.cjs.map +3 -3
  17. package/dist/crypto.d.ts +11 -3
  18. package/dist/crypto.d.ts.map +1 -1
  19. package/dist/crypto.js +34 -7
  20. package/dist/crypto.js.map +2 -2
  21. package/dist/crypto.min.cjs +2 -2
  22. package/dist/crypto.min.cjs.map +3 -3
  23. package/dist/crypto.min.js +2 -2
  24. package/dist/crypto.min.js.map +4 -4
  25. package/dist/ecc/index.min.js +2 -2
  26. package/dist/ecc/index.min.js.map +4 -4
  27. package/dist/index.min.js +2 -2
  28. package/dist/index.min.js.map +4 -4
  29. package/dist/meta.json +23 -34
  30. package/dist/rsa/index.cjs +4 -4
  31. package/dist/rsa/index.cjs.map +3 -3
  32. package/dist/rsa/index.d.ts +2 -2
  33. package/dist/rsa/index.d.ts.map +1 -1
  34. package/dist/rsa/index.js +4 -4
  35. package/dist/rsa/index.js.map +2 -2
  36. package/dist/rsa/index.min.cjs +1 -1
  37. package/dist/rsa/index.min.cjs.map +2 -2
  38. package/dist/rsa/index.min.js +2 -2
  39. package/dist/rsa/index.min.js.map +4 -4
  40. package/dist/util.cjs +12 -58
  41. package/dist/util.cjs.map +3 -3
  42. package/dist/util.d.ts +2 -11
  43. package/dist/util.d.ts.map +1 -1
  44. package/dist/util.js +12 -48
  45. package/dist/util.js.map +3 -3
  46. package/dist/util.min.cjs +1 -1
  47. package/dist/util.min.cjs.map +3 -3
  48. package/dist/util.min.js +1 -3
  49. package/dist/util.min.js.map +4 -4
  50. package/package.json +1 -1
@@ -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/crypto.ts
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,179 @@
4064
3886
  }
4065
3887
  __name(publicExponent, "publicExponent");
4066
3888
 
3889
+ // src/crypto.ts
3890
+ var import_tweetnacl = __toESM(require_nacl_fast(), 1);
3891
+ async function ed25519Verify({
3892
+ message,
3893
+ publicKey,
3894
+ signature
3895
+ }) {
3896
+ return import_tweetnacl.default.sign.detached.verify(message, signature, publicKey);
3897
+ }
3898
+ __name(ed25519Verify, "ed25519Verify");
3899
+ var did = {
3900
+ keyTypes: {
3901
+ "bls12-381": {
3902
+ magicBytes: new Uint8Array([234, 1]),
3903
+ verify: /* @__PURE__ */ __name(() => {
3904
+ throw new Error("Not implemented");
3905
+ }, "verify")
3906
+ },
3907
+ rsa: {
3908
+ magicBytes: new Uint8Array([0, 245, 2]),
3909
+ verify: rsaVerify
3910
+ },
3911
+ ed25519: {
3912
+ magicBytes: new Uint8Array([237, 1]),
3913
+ verify: ed25519Verify
3914
+ }
3915
+ }
3916
+ };
3917
+ async function rsaVerify({
3918
+ message,
3919
+ publicKey,
3920
+ signature
3921
+ }) {
3922
+ return rsaOperations.verify(
3923
+ message,
3924
+ signature,
3925
+ await webcrypto.subtle.importKey(
3926
+ "spki",
3927
+ toArrayBuffer(publicKey),
3928
+ { name: RSA_SIGN_ALGORITHM, hash: RSA_HASHING_ALGORITHM },
3929
+ false,
3930
+ ["verify"]
3931
+ ),
3932
+ 8
3933
+ );
3934
+ }
3935
+ __name(rsaVerify, "rsaVerify");
3936
+ var rsaOperations = {
3937
+ verify: /* @__PURE__ */ __name(async function rsaVerify2(msg, sig, publicKey, charSize = DEFAULT_CHAR_SIZE, hashAlg = DEFAULT_HASH_ALGORITHM) {
3938
+ return webcrypto.subtle.verify(
3939
+ {
3940
+ name: RSA_SIGN_ALGORITHM,
3941
+ saltLength: SALT_LENGTH
3942
+ },
3943
+ typeof publicKey === "string" ? await importPublicKey(publicKey, hashAlg, "signing" /* Sign */) : publicKey,
3944
+ normalizeBase64ToBuf(sig),
3945
+ normalizeUnicodeToBuf(msg, charSize)
3946
+ );
3947
+ }, "rsaVerify"),
3948
+ sign: /* @__PURE__ */ __name(async function sign(msg, privateKey, charSize = DEFAULT_CHAR_SIZE) {
3949
+ return webcrypto.subtle.sign(
3950
+ { name: RSA_SIGN_ALGORITHM, saltLength: SALT_LENGTH },
3951
+ privateKey,
3952
+ normalizeUnicodeToBuf(msg, charSize)
3953
+ );
3954
+ }, "sign"),
3955
+ /**
3956
+ * Encrypt the given AES key *to* a given public key.
3957
+ */
3958
+ encrypt: /* @__PURE__ */ __name(async function rsaEncrypt(msg, publicKey, charSize = DEFAULT_CHAR_SIZE, hashAlg = DEFAULT_HASH_ALGORITHM) {
3959
+ let pubKey;
3960
+ if (typeof publicKey === "string") {
3961
+ pubKey = await importPublicKey(publicKey, hashAlg, "exchange" /* Exchange */);
3962
+ } else {
3963
+ pubKey = publicKey instanceof Uint8Array ? await importPublicKey(toArrayBuffer(publicKey), hashAlg, "exchange" /* Exchange */) : publicKey;
3964
+ }
3965
+ return webcrypto.subtle.encrypt(
3966
+ { name: RSA_ALGORITHM },
3967
+ pubKey,
3968
+ normalizeUnicodeToBuf(msg, charSize)
3969
+ );
3970
+ }, "rsaEncrypt"),
3971
+ /**
3972
+ * Use RSA to decrypt the given data.
3973
+ */
3974
+ decrypt: /* @__PURE__ */ __name(async function rsaDecrypt(_data, privateKey) {
3975
+ const key = isCryptoKey(privateKey) ? privateKey : await importRsaKey(privateKey, ["decrypt"]);
3976
+ let data;
3977
+ if (typeof _data === "string") {
3978
+ data = fromString2(_data, "base64pad");
3979
+ } else {
3980
+ data = _data;
3981
+ }
3982
+ const arrayBuffer = await webcrypto.subtle.decrypt(
3983
+ { name: RSA_ALGORITHM },
3984
+ key,
3985
+ data instanceof Uint8Array ? toArrayBuffer(data) : data
3986
+ );
3987
+ const arr = new Uint8Array(arrayBuffer);
3988
+ return arr;
3989
+ }, "rsaDecrypt")
3990
+ };
3991
+ async function importPublicKey(base64Key, hashAlg, use) {
3992
+ checkValidKeyUse(use);
3993
+ const alg = use === "exchange" /* Exchange */ ? RSA_ALGORITHM : RSA_SIGN_ALGORITHM;
3994
+ const uses = use === "exchange" /* Exchange */ ? ["encrypt"] : ["verify"];
3995
+ const buf = typeof base64Key === "string" ? base64ToArrBuf(stripKeyHeader(base64Key)) : base64Key;
3996
+ return webcrypto.subtle.importKey("spki", buf, {
3997
+ name: alg,
3998
+ hash: { name: hashAlg }
3999
+ }, true, uses);
4000
+ }
4001
+ __name(importPublicKey, "importPublicKey");
4002
+ function checkValidKeyUse(use) {
4003
+ checkValid(use, ["signing" /* Sign */, "exchange" /* Exchange */], InvalidKeyUse);
4004
+ }
4005
+ __name(checkValidKeyUse, "checkValidKeyUse");
4006
+ function checkValid(toCheck, opts, error) {
4007
+ const match = opts.some((opt) => opt === toCheck);
4008
+ if (!match) {
4009
+ throw error;
4010
+ }
4011
+ }
4012
+ __name(checkValid, "checkValid");
4013
+ function stripKeyHeader(base64Key) {
4014
+ return base64Key.replace("-----BEGIN PUBLIC KEY-----\n", "").replace("\n-----END PUBLIC KEY-----", "");
4015
+ }
4016
+ __name(stripKeyHeader, "stripKeyHeader");
4017
+ function isCryptoKey(val) {
4018
+ return hasProp(val, "algorithm") && hasProp(val, "extractable") && hasProp(val, "type");
4019
+ }
4020
+ __name(isCryptoKey, "isCryptoKey");
4021
+ function hasProp(data, prop) {
4022
+ return typeof data === "object" && data != null && prop in data;
4023
+ }
4024
+ __name(hasProp, "hasProp");
4025
+ function importRsaKey(key, keyUsages) {
4026
+ return webcrypto.subtle.importKey(
4027
+ "spki",
4028
+ key instanceof Uint8Array ? toArrayBuffer(key) : key,
4029
+ { name: RSA_ALGORITHM, hash: RSA_HASHING_ALGORITHM },
4030
+ false,
4031
+ keyUsages
4032
+ );
4033
+ }
4034
+ __name(importRsaKey, "importRsaKey");
4035
+ async function publicKeyToDid(_publicKey, keyType = "rsa") {
4036
+ const publicKey = _publicKey instanceof CryptoKey ? new Uint8Array(await getPublicKeyAsArrayBuffer(_publicKey)) : _publicKey;
4037
+ const prefix = did.keyTypes[keyType]?.magicBytes;
4038
+ if (!prefix) {
4039
+ throw new Error(`Key type '${keyType}' not supported, available types: ${Object.keys(did.keyTypes).join(", ")}`);
4040
+ }
4041
+ const prefixedBuf = concat([prefix, publicKey]);
4042
+ return BASE58_DID_PREFIX + toString2(prefixedBuf, "base58btc");
4043
+ }
4044
+ __name(publicKeyToDid, "publicKeyToDid");
4045
+ async function getPublicKeyAsArrayBuffer(keypair) {
4046
+ const spki = keypair instanceof CryptoKey ? await webcrypto.subtle.exportKey(
4047
+ "spki",
4048
+ keypair
4049
+ ) : await webcrypto.subtle.exportKey(
4050
+ "spki",
4051
+ keypair.publicKey
4052
+ );
4053
+ return spki;
4054
+ }
4055
+ __name(getPublicKeyAsArrayBuffer, "getPublicKeyAsArrayBuffer");
4056
+ async function getPublicKeyAsUint8Array(keypair) {
4057
+ const arr = await getPublicKeyAsArrayBuffer(keypair);
4058
+ return new Uint8Array(arr);
4059
+ }
4060
+ __name(getPublicKeyAsUint8Array, "getPublicKeyAsUint8Array");
4061
+
4067
4062
  // src/aes/index.ts
4068
4063
  function toArrayBuffer2(data) {
4069
4064
  return new Uint8Array(data).buffer;