@substrate-system/keys 0.2.10 → 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 (56) hide show
  1. package/dist/_base.cjs +12 -11
  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 +3 -3
  9. package/dist/_base.min.js +2 -2
  10. package/dist/_base.min.js.map +4 -4
  11. package/dist/aes/index.cjs +3 -2
  12. package/dist/aes/index.cjs.map +2 -2
  13. package/dist/aes/index.js +1 -1
  14. package/dist/aes/index.js.map +2 -2
  15. package/dist/aes/index.min.cjs +1 -1
  16. package/dist/aes/index.min.cjs.map +3 -3
  17. package/dist/aes/index.min.js +2 -2
  18. package/dist/aes/index.min.js.map +4 -4
  19. package/dist/browser/index.js +178 -177
  20. package/dist/browser/index.min.js +2 -2
  21. package/dist/crypto.cjs +221 -0
  22. package/dist/crypto.cjs.map +7 -0
  23. package/dist/crypto.d.ts +54 -0
  24. package/dist/crypto.d.ts.map +1 -0
  25. package/dist/crypto.js +208 -0
  26. package/dist/crypto.js.map +7 -0
  27. package/dist/crypto.min.cjs +4 -0
  28. package/dist/crypto.min.cjs.map +7 -0
  29. package/dist/crypto.min.js +4 -0
  30. package/dist/crypto.min.js.map +7 -0
  31. package/dist/ecc/index.min.js +2 -2
  32. package/dist/ecc/index.min.js.map +4 -4
  33. package/dist/index.min.js +2 -2
  34. package/dist/index.min.js.map +4 -4
  35. package/dist/meta.json +99 -34
  36. package/dist/rsa/index.cjs +12 -11
  37. package/dist/rsa/index.cjs.map +3 -3
  38. package/dist/rsa/index.d.ts +2 -2
  39. package/dist/rsa/index.d.ts.map +1 -1
  40. package/dist/rsa/index.js +8 -6
  41. package/dist/rsa/index.js.map +2 -2
  42. package/dist/rsa/index.min.cjs +1 -1
  43. package/dist/rsa/index.min.cjs.map +3 -3
  44. package/dist/rsa/index.min.js +2 -2
  45. package/dist/rsa/index.min.js.map +4 -4
  46. package/dist/util.cjs +13 -203
  47. package/dist/util.cjs.map +3 -3
  48. package/dist/util.d.ts +4 -54
  49. package/dist/util.d.ts.map +1 -1
  50. package/dist/util.js +14 -198
  51. package/dist/util.js.map +3 -3
  52. package/dist/util.min.cjs +1 -3
  53. package/dist/util.min.cjs.map +3 -3
  54. package/dist/util.min.js +1 -3
  55. package/dist/util.min.js.map +4 -4
  56. package/package.json +5 -1
@@ -3673,9 +3673,6 @@
3673
3673
  importAesKey: () => importAesKey
3674
3674
  });
3675
3675
 
3676
- // src/util.ts
3677
- var import_tweetnacl = __toESM(require_nacl_fast(), 1);
3678
-
3679
3676
  // src/constants.ts
3680
3677
  var BASE58_DID_PREFIX = "did:key:z";
3681
3678
  var ECC_WRITE_ALG = "Ed25519";
@@ -3712,154 +3709,18 @@
3712
3709
  var SALT_LENGTH = 16;
3713
3710
 
3714
3711
  // src/util.ts
3712
+ var InvalidKeyUse = new Error("Invalid key use. Please use 'encryption' or 'signing");
3713
+ var InvalidMaxValue = new Error("Max must be less than 256 and greater than 0");
3715
3714
  function toArrayBuffer(data) {
3716
3715
  return new Uint8Array(data).buffer;
3717
3716
  }
3718
3717
  __name(toArrayBuffer, "toArrayBuffer");
3719
- var did = {
3720
- keyTypes: {
3721
- "bls12-381": {
3722
- magicBytes: new Uint8Array([234, 1]),
3723
- verify: /* @__PURE__ */ __name(() => {
3724
- throw new Error("Not implemented");
3725
- }, "verify")
3726
- },
3727
- rsa: {
3728
- magicBytes: new Uint8Array([0, 245, 2]),
3729
- verify: rsaVerify2
3730
- },
3731
- ed25519: {
3732
- magicBytes: new Uint8Array([237, 1]),
3733
- verify: ed25519Verify
3734
- }
3735
- }
3736
- };
3737
- async function ed25519Verify({
3738
- message,
3739
- publicKey,
3740
- signature
3741
- }) {
3742
- return import_tweetnacl.default.sign.detached.verify(message, signature, publicKey);
3743
- }
3744
- __name(ed25519Verify, "ed25519Verify");
3745
3718
  async function sha2562(bytes) {
3746
3719
  return new Uint8Array(
3747
3720
  await webcrypto.subtle.digest("sha-256", bytes.buffer)
3748
3721
  );
3749
3722
  }
3750
3723
  __name(sha2562, "sha256");
3751
- async function publicKeyToDid(_publicKey, keyType = "rsa") {
3752
- const publicKey = _publicKey instanceof CryptoKey ? new Uint8Array(await getPublicKeyAsArrayBuffer(_publicKey)) : _publicKey;
3753
- const prefix = did.keyTypes[keyType]?.magicBytes;
3754
- if (!prefix) {
3755
- throw new Error(`Key type '${keyType}' not supported, available types: ${Object.keys(did.keyTypes).join(", ")}`);
3756
- }
3757
- const prefixedBuf = concat([prefix, publicKey]);
3758
- return BASE58_DID_PREFIX + toString2(prefixedBuf, "base58btc");
3759
- }
3760
- __name(publicKeyToDid, "publicKeyToDid");
3761
- var rsaOperations = {
3762
- verify: /* @__PURE__ */ __name(async function rsaVerify(msg, sig, publicKey, charSize = DEFAULT_CHAR_SIZE, hashAlg = DEFAULT_HASH_ALGORITHM) {
3763
- return webcrypto.subtle.verify(
3764
- {
3765
- name: RSA_SIGN_ALGORITHM,
3766
- saltLength: SALT_LENGTH
3767
- },
3768
- typeof publicKey === "string" ? await importPublicKey(publicKey, hashAlg, "signing" /* Sign */) : publicKey,
3769
- normalizeBase64ToBuf(sig),
3770
- normalizeUnicodeToBuf(msg, charSize)
3771
- );
3772
- }, "rsaVerify"),
3773
- sign: /* @__PURE__ */ __name(async function sign(msg, privateKey, charSize = DEFAULT_CHAR_SIZE) {
3774
- return webcrypto.subtle.sign(
3775
- { name: RSA_SIGN_ALGORITHM, saltLength: SALT_LENGTH },
3776
- privateKey,
3777
- normalizeUnicodeToBuf(msg, charSize)
3778
- );
3779
- }, "sign"),
3780
- /**
3781
- * Encrypt the given AES key *to* a given public key.
3782
- */
3783
- encrypt: /* @__PURE__ */ __name(async function rsaEncrypt(msg, publicKey, charSize = DEFAULT_CHAR_SIZE, hashAlg = DEFAULT_HASH_ALGORITHM) {
3784
- let pubKey;
3785
- if (typeof publicKey === "string") {
3786
- pubKey = await importPublicKey(publicKey, hashAlg, "exchange" /* Exchange */);
3787
- } else {
3788
- pubKey = publicKey instanceof Uint8Array ? await importPublicKey(toArrayBuffer(publicKey), hashAlg, "exchange" /* Exchange */) : publicKey;
3789
- }
3790
- return webcrypto.subtle.encrypt(
3791
- { name: RSA_ALGORITHM },
3792
- pubKey,
3793
- normalizeUnicodeToBuf(msg, charSize)
3794
- );
3795
- }, "rsaEncrypt"),
3796
- /**
3797
- * Use RSA to decrypt the given data.
3798
- */
3799
- decrypt: /* @__PURE__ */ __name(async function rsaDecrypt(_data, privateKey) {
3800
- const key = isCryptoKey(privateKey) ? privateKey : await importRsaKey(privateKey, ["decrypt"]);
3801
- let data;
3802
- if (typeof _data === "string") {
3803
- data = fromString2(_data, "base64pad");
3804
- } else {
3805
- data = _data;
3806
- }
3807
- const arrayBuffer = await webcrypto.subtle.decrypt(
3808
- { name: RSA_ALGORITHM },
3809
- key,
3810
- data instanceof Uint8Array ? toArrayBuffer(data) : data
3811
- );
3812
- const arr = new Uint8Array(arrayBuffer);
3813
- return arr;
3814
- }, "rsaDecrypt")
3815
- };
3816
- async function rsaVerify2({
3817
- message,
3818
- publicKey,
3819
- signature
3820
- }) {
3821
- return rsaOperations.verify(
3822
- message,
3823
- signature,
3824
- await webcrypto.subtle.importKey(
3825
- "spki",
3826
- toArrayBuffer(publicKey),
3827
- { name: RSA_SIGN_ALGORITHM, hash: RSA_HASHING_ALGORITHM },
3828
- false,
3829
- ["verify"]
3830
- ),
3831
- 8
3832
- );
3833
- }
3834
- __name(rsaVerify2, "rsaVerify");
3835
- async function importPublicKey(base64Key, hashAlg, use) {
3836
- checkValidKeyUse(use);
3837
- const alg = use === "exchange" /* Exchange */ ? RSA_ALGORITHM : RSA_SIGN_ALGORITHM;
3838
- const uses = use === "exchange" /* Exchange */ ? ["encrypt"] : ["verify"];
3839
- const buf = typeof base64Key === "string" ? base64ToArrBuf(stripKeyHeader(base64Key)) : base64Key;
3840
- return webcrypto.subtle.importKey("spki", buf, {
3841
- name: alg,
3842
- hash: { name: hashAlg }
3843
- }, true, uses);
3844
- }
3845
- __name(importPublicKey, "importPublicKey");
3846
- var InvalidKeyUse = new Error("Invalid key use. Please use 'encryption' or 'signing");
3847
- var InvalidMaxValue = new Error("Max must be less than 256 and greater than 0");
3848
- function checkValidKeyUse(use) {
3849
- checkValid(use, ["signing" /* Sign */, "exchange" /* Exchange */], InvalidKeyUse);
3850
- }
3851
- __name(checkValidKeyUse, "checkValidKeyUse");
3852
- function checkValid(toCheck, opts, error) {
3853
- const match = opts.some((opt) => opt === toCheck);
3854
- if (!match) {
3855
- throw error;
3856
- }
3857
- }
3858
- __name(checkValid, "checkValid");
3859
- function stripKeyHeader(base64Key) {
3860
- return base64Key.replace("-----BEGIN PUBLIC KEY-----\n", "").replace("\n-----END PUBLIC KEY-----", "");
3861
- }
3862
- __name(stripKeyHeader, "stripKeyHeader");
3863
3724
  function base64ToArrBuf(string2) {
3864
3725
  const uint8 = fromString2(string2, "base64pad");
3865
3726
  return toArrayBuffer(uint8);
@@ -3902,40 +3763,6 @@
3902
3763
  }
3903
3764
  }
3904
3765
  __name(normalizeUnicodeToBuf, "normalizeUnicodeToBuf");
3905
- function importRsaKey(key, keyUsages) {
3906
- return webcrypto.subtle.importKey(
3907
- "spki",
3908
- key instanceof Uint8Array ? toArrayBuffer(key) : key,
3909
- { name: RSA_ALGORITHM, hash: RSA_HASHING_ALGORITHM },
3910
- false,
3911
- keyUsages
3912
- );
3913
- }
3914
- __name(importRsaKey, "importRsaKey");
3915
- function isCryptoKey(val) {
3916
- return hasProp(val, "algorithm") && hasProp(val, "extractable") && hasProp(val, "type");
3917
- }
3918
- __name(isCryptoKey, "isCryptoKey");
3919
- function hasProp(data, prop) {
3920
- return typeof data === "object" && data != null && prop in data;
3921
- }
3922
- __name(hasProp, "hasProp");
3923
- async function getPublicKeyAsArrayBuffer(keypair) {
3924
- const spki = keypair instanceof CryptoKey ? await webcrypto.subtle.exportKey(
3925
- "spki",
3926
- keypair
3927
- ) : await webcrypto.subtle.exportKey(
3928
- "spki",
3929
- keypair.publicKey
3930
- );
3931
- return spki;
3932
- }
3933
- __name(getPublicKeyAsArrayBuffer, "getPublicKeyAsArrayBuffer");
3934
- async function getPublicKeyAsUint8Array(keypair) {
3935
- const arr = await getPublicKeyAsArrayBuffer(keypair);
3936
- return new Uint8Array(arr);
3937
- }
3938
- __name(getPublicKeyAsUint8Array, "getPublicKeyAsUint8Array");
3939
3766
  function didToPublicKey(did2) {
3940
3767
  if (!did2.startsWith(BASE58_DID_PREFIX)) {
3941
3768
  throw new Error(
@@ -4039,7 +3866,8 @@
4039
3866
  return joined.buffer;
4040
3867
  }
4041
3868
  __name(joinBufs, "joinBufs");
4042
- async function makeRSAKeypair(size, hashAlg, use) {
3869
+ async function makeRSAKeypair(size, hashAlg, use, extractable) {
3870
+ const isExtractable = extractable || false;
4043
3871
  if (!Object.values(KeyUse).includes(use)) {
4044
3872
  throw new Error("invalid key use");
4045
3873
  }
@@ -4050,7 +3878,7 @@
4050
3878
  modulusLength: size,
4051
3879
  publicExponent: publicExponent(),
4052
3880
  hash: { name: hashAlg }
4053
- }, false, uses);
3881
+ }, isExtractable, uses);
4054
3882
  }
4055
3883
  __name(makeRSAKeypair, "makeRSAKeypair");
4056
3884
  function publicExponent() {
@@ -4058,6 +3886,179 @@
4058
3886
  }
4059
3887
  __name(publicExponent, "publicExponent");
4060
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
+
4061
4062
  // src/aes/index.ts
4062
4063
  function toArrayBuffer2(data) {
4063
4064
  return new Uint8Array(data).buffer;