@waku/rln 0.1.5-ea6daae.0 → 0.1.5-f39d215.0

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 (129) hide show
  1. package/README.md +5 -0
  2. package/bundle/_virtual/index2.js +1 -1
  3. package/bundle/index.js +3 -1
  4. package/bundle/node_modules/@ethersproject/abi/lib.esm/_version.js +1 -1
  5. package/bundle/node_modules/@ethersproject/abstract-provider/lib.esm/_version.js +1 -1
  6. package/bundle/node_modules/@ethersproject/abstract-signer/lib.esm/_version.js +1 -1
  7. package/bundle/node_modules/@ethersproject/address/lib.esm/_version.js +1 -1
  8. package/bundle/node_modules/@ethersproject/bignumber/lib.esm/_version.js +1 -1
  9. package/bundle/node_modules/@ethersproject/contracts/lib.esm/_version.js +1 -1
  10. package/bundle/node_modules/@ethersproject/contracts/lib.esm/index.js +1 -1
  11. package/bundle/node_modules/@ethersproject/hash/lib.esm/_version.js +1 -1
  12. package/bundle/node_modules/@ethersproject/keccak256/lib.esm/index.js +1 -1
  13. package/bundle/node_modules/{js-sha3 → @ethersproject/keccak256/node_modules/js-sha3}/src/sha3.js +2 -2
  14. package/bundle/node_modules/@ethersproject/networks/lib.esm/_version.js +1 -1
  15. package/bundle/node_modules/@ethersproject/networks/lib.esm/index.js +1 -19
  16. package/bundle/node_modules/@ethersproject/properties/lib.esm/_version.js +1 -1
  17. package/bundle/node_modules/@ethersproject/providers/lib.esm/_version.js +1 -1
  18. package/bundle/node_modules/@ethersproject/signing-key/lib.esm/_version.js +1 -1
  19. package/bundle/node_modules/@ethersproject/signing-key/lib.esm/elliptic.js +10 -58
  20. package/bundle/node_modules/@ethersproject/strings/lib.esm/_version.js +1 -1
  21. package/bundle/node_modules/@ethersproject/transactions/lib.esm/_version.js +1 -1
  22. package/bundle/node_modules/@ethersproject/web/lib.esm/_version.js +1 -1
  23. package/bundle/packages/rln/dist/contract/abi.js +502 -248
  24. package/bundle/packages/rln/dist/contract/constants.js +4 -4
  25. package/bundle/packages/rln/dist/contract/rln_contract.js +124 -40
  26. package/bundle/packages/rln/dist/contract/rln_light_contract.js +477 -0
  27. package/bundle/packages/rln/dist/identity.js +9 -0
  28. package/bundle/packages/rln/dist/keystore/cipher.js +3 -3
  29. package/bundle/packages/rln/dist/keystore/keystore.js +16 -25
  30. package/bundle/packages/rln/dist/rln.js +8 -8
  31. package/bundle/packages/rln/dist/rln_light.js +149 -0
  32. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/checksum.js +3 -3
  33. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/cipher.js +4 -4
  34. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/class.js +7 -7
  35. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/functional.js +7 -7
  36. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/index.js +6 -6
  37. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/kdf.js +5 -5
  38. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/password.js +1 -1
  39. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/schema-validation-generated.js +1 -1
  40. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/schema-validation.js +2 -2
  41. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/types.js +1 -1
  42. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/aes.js +3 -3
  43. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/pbkdf2.js +7 -7
  44. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +3 -3
  45. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/scrypt.js +3 -3
  46. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/sha256.js +3 -3
  47. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +7 -7
  48. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/_assert.js +1 -1
  49. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/_sha2.js +3 -3
  50. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/_u64.js +1 -1
  51. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/cryptoBrowser.js +1 -1
  52. package/bundle/packages/rln/node_modules/@noble/hashes/esm/_assert.js +43 -0
  53. package/bundle/packages/rln/node_modules/@noble/hashes/esm/_sha2.js +116 -0
  54. package/bundle/packages/rln/node_modules/@noble/hashes/esm/hmac.js +79 -0
  55. package/bundle/packages/rln/node_modules/@noble/hashes/esm/sha256.js +126 -0
  56. package/bundle/packages/rln/node_modules/@noble/hashes/esm/utils.js +43 -0
  57. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/hmac.js +3 -3
  58. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/pbkdf2.js +4 -4
  59. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/scrypt.js +5 -5
  60. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/sha256.js +3 -3
  61. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/sha512.js +4 -4
  62. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/utils.js +2 -2
  63. package/dist/.tsbuildinfo +1 -1
  64. package/dist/codec.test-utils.d.ts +36 -0
  65. package/dist/codec.test-utils.js +56 -0
  66. package/dist/codec.test-utils.js.map +1 -0
  67. package/dist/contract/abi.d.ts +21 -17
  68. package/dist/contract/abi.js +502 -248
  69. package/dist/contract/abi.js.map +1 -1
  70. package/dist/contract/constants.d.ts +23 -19
  71. package/dist/contract/constants.js +3 -3
  72. package/dist/contract/constants.js.map +1 -1
  73. package/dist/contract/rln_contract.d.ts +10 -3
  74. package/dist/contract/rln_contract.js +124 -40
  75. package/dist/contract/rln_contract.js.map +1 -1
  76. package/dist/contract/rln_light_contract.d.ts +124 -0
  77. package/dist/contract/rln_light_contract.js +460 -0
  78. package/dist/contract/rln_light_contract.js.map +1 -0
  79. package/dist/contract/test-setup.d.ts +26 -0
  80. package/dist/contract/test-setup.js +56 -0
  81. package/dist/contract/test-setup.js.map +1 -0
  82. package/dist/contract/test-utils.d.ts +39 -0
  83. package/dist/contract/test-utils.js +118 -0
  84. package/dist/contract/test-utils.js.map +1 -0
  85. package/dist/identity.d.ts +1 -0
  86. package/dist/identity.js +9 -0
  87. package/dist/identity.js.map +1 -1
  88. package/dist/index.d.ts +5 -2
  89. package/dist/index.js +4 -2
  90. package/dist/index.js.map +1 -1
  91. package/dist/keystore/keystore.d.ts +0 -1
  92. package/dist/keystore/keystore.js +15 -24
  93. package/dist/keystore/keystore.js.map +1 -1
  94. package/dist/keystore/types.d.ts +2 -1
  95. package/dist/rln.d.ts +1 -1
  96. package/dist/rln.js +8 -8
  97. package/dist/rln.js.map +1 -1
  98. package/dist/rln_light.d.ts +64 -0
  99. package/dist/rln_light.js +144 -0
  100. package/dist/rln_light.js.map +1 -0
  101. package/package.json +1 -1
  102. package/src/codec.test-utils.ts +80 -0
  103. package/src/contract/abi.ts +502 -248
  104. package/src/contract/constants.ts +3 -3
  105. package/src/contract/rln_contract.ts +162 -50
  106. package/src/contract/rln_light_contract.ts +725 -0
  107. package/src/contract/test-setup.ts +86 -0
  108. package/src/contract/test-utils.ts +179 -0
  109. package/src/identity.ts +10 -0
  110. package/src/index.ts +17 -2
  111. package/src/keystore/keystore.ts +27 -42
  112. package/src/keystore/types.ts +2 -1
  113. package/src/rln.ts +9 -9
  114. package/src/rln_light.ts +235 -0
  115. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/index.js +0 -0
  116. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/md5.js +0 -0
  117. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/nil.js +0 -0
  118. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/parse.js +0 -0
  119. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/regex.js +0 -0
  120. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/rng.js +0 -0
  121. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/sha1.js +0 -0
  122. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/stringify.js +0 -0
  123. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v1.js +0 -0
  124. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v3.js +0 -0
  125. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v35.js +0 -0
  126. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v4.js +0 -0
  127. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v5.js +0 -0
  128. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/validate.js +0 -0
  129. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/version.js +0 -0
@@ -0,0 +1,149 @@
1
+ import { hmac } from '../node_modules/@noble/hashes/esm/hmac.js';
2
+ import { sha256 } from '../node_modules/@noble/hashes/esm/sha256.js';
3
+ import '../../interfaces/dist/protocols.js';
4
+ import '../../interfaces/dist/connection_manager.js';
5
+ import '../../interfaces/dist/health_indicator.js';
6
+ import '../../../node_modules/multiformats/dist/src/bases/base10.js';
7
+ import '../../../node_modules/multiformats/dist/src/bases/base16.js';
8
+ import '../../../node_modules/multiformats/dist/src/bases/base2.js';
9
+ import '../../../node_modules/multiformats/dist/src/bases/base256emoji.js';
10
+ import '../../../node_modules/multiformats/dist/src/bases/base32.js';
11
+ import '../../../node_modules/multiformats/dist/src/bases/base36.js';
12
+ import '../../../node_modules/multiformats/dist/src/bases/base58.js';
13
+ import '../../../node_modules/multiformats/dist/src/bases/base64.js';
14
+ import '../../../node_modules/multiformats/dist/src/bases/base8.js';
15
+ import '../../../node_modules/multiformats/dist/src/bases/identity.js';
16
+ import '../../../node_modules/multiformats/dist/src/codecs/json.js';
17
+ import { Logger } from '../../utils/dist/logger/index.js';
18
+ import { LINEA_CONTRACT } from './contract/constants.js';
19
+ import { RLNLightContract } from './contract/rln_light_contract.js';
20
+ import { IdentityCredential } from './identity.js';
21
+ import { Keystore } from './keystore/keystore.js';
22
+ import { extractMetaMaskSigner } from './utils/metamask.js';
23
+ import { buildBigIntFromUint8Array } from './utils/bytes.js';
24
+ import './utils/epoch.js';
25
+
26
+ new Logger("waku:rln");
27
+ class RLNLightInstance {
28
+ started = false;
29
+ starting = false;
30
+ _contract;
31
+ _signer;
32
+ keystore = Keystore.create();
33
+ _credentials;
34
+ constructor() { }
35
+ get contract() {
36
+ return this._contract;
37
+ }
38
+ get signer() {
39
+ return this._signer;
40
+ }
41
+ async start(options = {}) {
42
+ if (this.started || this.starting) {
43
+ return;
44
+ }
45
+ this.starting = true;
46
+ try {
47
+ const { credentials, keystore } = await RLNLightInstance.decryptCredentialsIfNeeded(options.credentials);
48
+ const { signer, address, rateLimit } = await this.determineStartOptions(options, credentials);
49
+ if (keystore) {
50
+ this.keystore = keystore;
51
+ }
52
+ this._credentials = credentials;
53
+ this._signer = signer;
54
+ this._contract = await RLNLightContract.init({
55
+ address: address,
56
+ signer: signer,
57
+ rateLimit: rateLimit
58
+ });
59
+ this.started = true;
60
+ }
61
+ finally {
62
+ this.starting = false;
63
+ }
64
+ }
65
+ get credentials() {
66
+ return this._credentials;
67
+ }
68
+ async determineStartOptions(options, credentials) {
69
+ let chainId = credentials?.membership.chainId;
70
+ const address = credentials?.membership.address ||
71
+ options.address ||
72
+ LINEA_CONTRACT.address;
73
+ if (address === LINEA_CONTRACT.address) {
74
+ chainId = LINEA_CONTRACT.chainId;
75
+ }
76
+ const signer = options.signer || (await extractMetaMaskSigner());
77
+ const currentChainId = (await signer.getChainId()).toString();
78
+ if (chainId && chainId !== currentChainId) {
79
+ throw Error(`Failed to start RLN contract, chain ID of contract is different from current one: contract-${chainId}, current network-${currentChainId}`);
80
+ }
81
+ return {
82
+ signer,
83
+ address
84
+ };
85
+ }
86
+ static async decryptCredentialsIfNeeded(credentials) {
87
+ if (!credentials) {
88
+ return {};
89
+ }
90
+ if ("identity" in credentials) {
91
+ return { credentials };
92
+ }
93
+ const keystore = Keystore.fromString(credentials.keystore);
94
+ if (!keystore) {
95
+ return {};
96
+ }
97
+ const decryptedCredentials = await keystore.readCredential(credentials.id, credentials.password);
98
+ return {
99
+ keystore,
100
+ credentials: decryptedCredentials
101
+ };
102
+ }
103
+ /**
104
+ * Generates an identity credential from a seed string
105
+ * This is a pure implementation that doesn't rely on Zerokit
106
+ * @param seed A string seed to generate the identity from
107
+ * @returns IdentityCredential
108
+ */
109
+ generateSeededIdentityCredential(seed) {
110
+ // Convert the seed to bytes
111
+ const encoder = new TextEncoder();
112
+ const seedBytes = encoder.encode(seed);
113
+ // Generate deterministic values using HMAC-SHA256
114
+ // We use different context strings for each component to ensure they're different
115
+ const idTrapdoor = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
116
+ const idNullifier = hmac(sha256, seedBytes, encoder.encode("IDNullifier"));
117
+ // Generate IDSecretHash as a hash of IDTrapdoor and IDNullifier
118
+ const combinedBytes = new Uint8Array([...idTrapdoor, ...idNullifier]);
119
+ const idSecretHash = sha256(combinedBytes);
120
+ // Generate IDCommitment as a hash of IDSecretHash
121
+ const idCommitment = sha256(idSecretHash);
122
+ // Convert IDCommitment to BigInt
123
+ const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
124
+ return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
125
+ }
126
+ async registerMembership(options) {
127
+ if (!this.contract) {
128
+ throw Error("RLN Contract is not initialized.");
129
+ }
130
+ let identity = "identity" in options && options.identity;
131
+ if ("signature" in options) {
132
+ identity = this.generateSeededIdentityCredential(options.signature);
133
+ }
134
+ if (!identity) {
135
+ throw Error("Missing signature or identity to register membership.");
136
+ }
137
+ return this.contract.registerWithIdentity(identity);
138
+ }
139
+ /**
140
+ * Changes credentials in use by relying on provided Keystore earlier in rln.start
141
+ * @param id: string, hash of credentials to select from Keystore
142
+ * @param password: string or bytes to use to decrypt credentials from Keystore
143
+ */
144
+ async useCredentials(id, password) {
145
+ this._credentials = await this.keystore?.readCredential(id, password);
146
+ }
147
+ }
148
+
149
+ export { RLNLightInstance };
@@ -1,8 +1,8 @@
1
- import { commonjsGlobal } from '../../../../_virtual/_commonjsHelpers.js';
2
- import { __exports as checksum$1 } from '../../../../_virtual/checksum.js';
1
+ import { commonjsGlobal } from '../../../../../../_virtual/_commonjsHelpers.js';
2
+ import { __exports as checksum$1 } from '../../../../../../_virtual/checksum.js';
3
3
  import '../node_modules/ethereum-cryptography/sha256.js';
4
4
  import { u as utilsExports } from '../node_modules/ethereum-cryptography/utils.js';
5
- import { __exports as sha256 } from '../../../../_virtual/sha256.js';
5
+ import { __exports as sha256 } from '../../../../../../_virtual/sha256.js';
6
6
 
7
7
  var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
8
8
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
@@ -1,10 +1,10 @@
1
- import { commonjsGlobal } from '../../../../_virtual/_commonjsHelpers.js';
2
- import { __exports as cipher } from '../../../../_virtual/cipher.js';
1
+ import { commonjsGlobal } from '../../../../../../_virtual/_commonjsHelpers.js';
2
+ import { __exports as cipher } from '../../../../../../_virtual/cipher.js';
3
3
  import '../node_modules/ethereum-cryptography/random.js';
4
4
  import '../node_modules/ethereum-cryptography/aes.js';
5
5
  import { u as utilsExports } from '../node_modules/ethereum-cryptography/utils.js';
6
- import { __exports as random } from '../../../../_virtual/random.js';
7
- import { __exports as aes } from '../../../../_virtual/aes.js';
6
+ import { __exports as random } from '../../../../../../_virtual/random.js';
7
+ import { __exports as aes } from '../../../../../../_virtual/aes.js';
8
8
 
9
9
  var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
10
10
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
@@ -1,15 +1,15 @@
1
- import { commonjsGlobal } from '../../../../_virtual/_commonjsHelpers.js';
2
- import { __exports as _class } from '../../../../_virtual/class.js';
1
+ import { commonjsGlobal } from '../../../../../../_virtual/_commonjsHelpers.js';
2
+ import { __exports as _class } from '../../../../../../_virtual/class.js';
3
3
  import './kdf.js';
4
4
  import './checksum.js';
5
5
  import './cipher.js';
6
6
  import './functional.js';
7
7
  import './schema-validation.js';
8
- import { __exports as kdf } from '../../../../_virtual/kdf.js';
9
- import { __exports as checksum } from '../../../../_virtual/checksum.js';
10
- import { __exports as cipher } from '../../../../_virtual/cipher.js';
11
- import { __exports as functional } from '../../../../_virtual/functional.js';
12
- import { __exports as schemaValidation } from '../../../../_virtual/schema-validation.js';
8
+ import { __exports as kdf } from '../../../../../../_virtual/kdf.js';
9
+ import { __exports as checksum } from '../../../../../../_virtual/checksum.js';
10
+ import { __exports as cipher } from '../../../../../../_virtual/cipher.js';
11
+ import { __exports as functional } from '../../../../../../_virtual/functional.js';
12
+ import { __exports as schemaValidation } from '../../../../../../_virtual/schema-validation.js';
13
13
 
14
14
  var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
15
15
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
@@ -1,15 +1,15 @@
1
- import { commonjsGlobal } from '../../../../_virtual/_commonjsHelpers.js';
2
- import { __exports as functional } from '../../../../_virtual/functional.js';
3
- import require$$0 from '../../../../_virtual/index2.js';
1
+ import { commonjsGlobal } from '../../../../../../_virtual/_commonjsHelpers.js';
2
+ import { __exports as functional } from '../../../../../../_virtual/functional.js';
3
+ import require$$0 from '../../../../../../_virtual/index2.js';
4
4
  import './kdf.js';
5
5
  import './checksum.js';
6
6
  import './cipher.js';
7
7
  import './password.js';
8
8
  import { u as utilsExports } from '../node_modules/ethereum-cryptography/utils.js';
9
- import { __exports as kdf } from '../../../../_virtual/kdf.js';
10
- import { __exports as checksum } from '../../../../_virtual/checksum.js';
11
- import { __exports as cipher } from '../../../../_virtual/cipher.js';
12
- import { __exports as password } from '../../../../_virtual/password.js';
9
+ import { __exports as kdf } from '../../../../../../_virtual/kdf.js';
10
+ import { __exports as checksum } from '../../../../../../_virtual/checksum.js';
11
+ import { __exports as cipher } from '../../../../../../_virtual/cipher.js';
12
+ import { __exports as password } from '../../../../../../_virtual/password.js';
13
13
 
14
14
  (function (exports) {
15
15
  var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
@@ -1,13 +1,13 @@
1
- import { commonjsGlobal } from '../../../../_virtual/_commonjsHelpers.js';
2
- import { __exports as lib } from '../../../../_virtual/index.js';
1
+ import { commonjsGlobal } from '../../../../../../_virtual/_commonjsHelpers.js';
2
+ import { __exports as lib } from '../../../../../../_virtual/index.js';
3
3
  import './types.js';
4
4
  import './functional.js';
5
5
  import './class.js';
6
6
  import './schema-validation.js';
7
- import { __exports as types } from '../../../../_virtual/types.js';
8
- import { __exports as functional } from '../../../../_virtual/functional.js';
9
- import { __exports as _class } from '../../../../_virtual/class.js';
10
- import { __exports as schemaValidation } from '../../../../_virtual/schema-validation.js';
7
+ import { __exports as types } from '../../../../../../_virtual/types.js';
8
+ import { __exports as functional } from '../../../../../../_virtual/functional.js';
9
+ import { __exports as _class } from '../../../../../../_virtual/class.js';
10
+ import { __exports as schemaValidation } from '../../../../../../_virtual/schema-validation.js';
11
11
 
12
12
  (function (exports) {
13
13
  var __createBinding = (commonjsGlobal && commonjsGlobal.__createBinding) || (Object.create ? (function(o, m, k, k2) {
@@ -1,12 +1,12 @@
1
- import { commonjsGlobal } from '../../../../_virtual/_commonjsHelpers.js';
2
- import { __exports as kdf$1 } from '../../../../_virtual/kdf.js';
1
+ import { commonjsGlobal } from '../../../../../../_virtual/_commonjsHelpers.js';
2
+ import { __exports as kdf$1 } from '../../../../../../_virtual/kdf.js';
3
3
  import '../node_modules/ethereum-cryptography/random.js';
4
4
  import '../node_modules/ethereum-cryptography/pbkdf2.js';
5
5
  import '../node_modules/ethereum-cryptography/scrypt.js';
6
6
  import { u as utilsExports } from '../node_modules/ethereum-cryptography/utils.js';
7
- import { __exports as random } from '../../../../_virtual/random.js';
8
- import { __exports as pbkdf2 } from '../../../../_virtual/pbkdf2.js';
9
- import { __exports as scrypt } from '../../../../_virtual/scrypt.js';
7
+ import { __exports as random } from '../../../../../../_virtual/random.js';
8
+ import { __exports as pbkdf2 } from '../../../../../../_virtual/pbkdf2.js';
9
+ import { __exports as scrypt } from '../../../../../../_virtual/scrypt.js';
10
10
 
11
11
  var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
12
12
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
@@ -1,4 +1,4 @@
1
- import { __exports as password } from '../../../../_virtual/password.js';
1
+ import { __exports as password } from '../../../../../../_virtual/password.js';
2
2
  import { u as utilsExports } from '../node_modules/ethereum-cryptography/utils.js';
3
3
 
4
4
  Object.defineProperty(password, "__esModule", { value: true });
@@ -1,4 +1,4 @@
1
- import { __exports as schemaValidationGenerated } from '../../../../_virtual/schema-validation-generated.js';
1
+ import { __exports as schemaValidationGenerated } from '../../../../../../_virtual/schema-validation-generated.js';
2
2
 
3
3
  Object.defineProperty(schemaValidationGenerated, "__esModule", { value: true });
4
4
  schemaValidationGenerated.Keystore = void 0;
@@ -1,6 +1,6 @@
1
- import { __exports as schemaValidation } from '../../../../_virtual/schema-validation.js';
1
+ import { __exports as schemaValidation } from '../../../../../../_virtual/schema-validation.js';
2
2
  import './schema-validation-generated.js';
3
- import { __exports as schemaValidationGenerated } from '../../../../_virtual/schema-validation-generated.js';
3
+ import { __exports as schemaValidationGenerated } from '../../../../../../_virtual/schema-validation-generated.js';
4
4
 
5
5
  Object.defineProperty(schemaValidation, "__esModule", { value: true });
6
6
  schemaValidation.isValidKeystore = schemaValidation.validateKeystore = schemaValidation.schemaValidationErrors = void 0;
@@ -1,4 +1,4 @@
1
- import { __exports as types } from '../../../../_virtual/types.js';
1
+ import { __exports as types } from '../../../../../../_virtual/types.js';
2
2
 
3
3
  Object.defineProperty(types, "__esModule", { value: true });
4
4
 
@@ -1,7 +1,7 @@
1
- import { __exports as aes } from '../../../../../_virtual/aes.js';
2
- import '../@noble/hashes/cryptoBrowser.js';
1
+ import { __exports as aes } from '../../../../../../../_virtual/aes.js';
2
+ import '../../../../@noble/hashes/cryptoBrowser.js';
3
3
  import { u as utilsExports } from './utils.js';
4
- import { __exports as cryptoBrowser } from '../../../../../_virtual/cryptoBrowser.js';
4
+ import { __exports as cryptoBrowser } from '../../../../../../../_virtual/cryptoBrowser.js';
5
5
 
6
6
  Object.defineProperty(aes, "__esModule", { value: true });
7
7
  aes.decrypt = aes.encrypt = void 0;
@@ -1,11 +1,11 @@
1
- import { __exports as pbkdf2$1 } from '../../../../../_virtual/pbkdf2.js';
2
- import '../@noble/hashes/pbkdf2.js';
3
- import '../@noble/hashes/sha256.js';
4
- import '../@noble/hashes/sha512.js';
1
+ import { __exports as pbkdf2$1 } from '../../../../../../../_virtual/pbkdf2.js';
2
+ import '../../../../@noble/hashes/pbkdf2.js';
3
+ import '../../../../@noble/hashes/sha256.js';
4
+ import '../../../../@noble/hashes/sha512.js';
5
5
  import { u as utilsExports } from './utils.js';
6
- import { __exports as pbkdf2$2 } from '../../../../../_virtual/pbkdf22.js';
7
- import { __exports as sha256 } from '../../../../../_virtual/sha2562.js';
8
- import { __exports as sha512 } from '../../../../../_virtual/sha512.js';
6
+ import { __exports as pbkdf2$2 } from '../../../../../../../_virtual/pbkdf22.js';
7
+ import { __exports as sha256 } from '../../../../../../../_virtual/sha2562.js';
8
+ import { __exports as sha512 } from '../../../../../../../_virtual/sha512.js';
9
9
 
10
10
  Object.defineProperty(pbkdf2$1, "__esModule", { value: true });
11
11
  pbkdf2$1.pbkdf2Sync = pbkdf2$1.pbkdf2 = void 0;
@@ -1,6 +1,6 @@
1
- import { __exports as random } from '../../../../../_virtual/random.js';
2
- import '../@noble/hashes/utils.js';
3
- import { __exports as utils } from '../../../../../_virtual/utils2.js';
1
+ import { __exports as random } from '../../../../../../../_virtual/random.js';
2
+ import '../../../../@noble/hashes/utils.js';
3
+ import { __exports as utils } from '../../../../../../../_virtual/utils2.js';
4
4
 
5
5
  Object.defineProperty(random, "__esModule", { value: true });
6
6
  random.getRandomBytes = random.getRandomBytesSync = void 0;
@@ -1,7 +1,7 @@
1
- import { __exports as scrypt$1 } from '../../../../../_virtual/scrypt.js';
2
- import '../@noble/hashes/scrypt.js';
1
+ import { __exports as scrypt$1 } from '../../../../../../../_virtual/scrypt.js';
2
+ import '../../../../@noble/hashes/scrypt.js';
3
3
  import { u as utilsExports } from './utils.js';
4
- import { __exports as scrypt$2 } from '../../../../../_virtual/scrypt2.js';
4
+ import { __exports as scrypt$2 } from '../../../../../../../_virtual/scrypt2.js';
5
5
 
6
6
  Object.defineProperty(scrypt$1, "__esModule", { value: true });
7
7
  scrypt$1.scryptSync = scrypt$1.scrypt = void 0;
@@ -1,7 +1,7 @@
1
- import { __exports as sha256 } from '../../../../../_virtual/sha256.js';
2
- import '../@noble/hashes/sha256.js';
1
+ import { __exports as sha256 } from '../../../../../../../_virtual/sha256.js';
2
+ import '../../../../@noble/hashes/sha256.js';
3
3
  import { u as utilsExports } from './utils.js';
4
- import { __exports as sha256$1 } from '../../../../../_virtual/sha2562.js';
4
+ import { __exports as sha256$1 } from '../../../../../../../_virtual/sha2562.js';
5
5
 
6
6
  Object.defineProperty(sha256, "__esModule", { value: true });
7
7
  sha256.sha256 = void 0;
@@ -1,10 +1,10 @@
1
- import { commonjsGlobal } from '../../../../../_virtual/_commonjsHelpers.js';
2
- import { commonjsRequire } from '../../../../../_virtual/_commonjs-dynamic-modules.js';
3
- import { __module as utils } from '../../../../../_virtual/utils.js';
4
- import '../@noble/hashes/_assert.js';
5
- import '../@noble/hashes/utils.js';
6
- import { __exports as _assert } from '../../../../../_virtual/_assert.js';
7
- import { __exports as utils$1 } from '../../../../../_virtual/utils2.js';
1
+ import { commonjsGlobal } from '../../../../../../../_virtual/_commonjsHelpers.js';
2
+ import { commonjsRequire } from '../../../../../../../_virtual/_commonjs-dynamic-modules.js';
3
+ import { __module as utils } from '../../../../../../../_virtual/utils.js';
4
+ import '../../../../@noble/hashes/_assert.js';
5
+ import '../../../../@noble/hashes/utils.js';
6
+ import { __exports as _assert } from '../../../../../../../_virtual/_assert.js';
7
+ import { __exports as utils$1 } from '../../../../../../../_virtual/utils2.js';
8
8
 
9
9
  utils.exports;
10
10
 
@@ -1,4 +1,4 @@
1
- import { __exports as _assert } from '../../../../../../_virtual/_assert.js';
1
+ import { __exports as _assert } from '../../../../../_virtual/_assert.js';
2
2
 
3
3
  Object.defineProperty(_assert, "__esModule", { value: true });
4
4
  _assert.output = _assert.exists = _assert.hash = _assert.bytes = _assert.bool = _assert.number = void 0;
@@ -1,8 +1,8 @@
1
- import { __exports as _sha2 } from '../../../../../../_virtual/_sha2.js';
1
+ import { __exports as _sha2 } from '../../../../../_virtual/_sha2.js';
2
2
  import './_assert.js';
3
3
  import './utils.js';
4
- import { __exports as utils } from '../../../../../../_virtual/utils2.js';
5
- import { __exports as _assert } from '../../../../../../_virtual/_assert.js';
4
+ import { __exports as utils } from '../../../../../_virtual/utils2.js';
5
+ import { __exports as _assert } from '../../../../../_virtual/_assert.js';
6
6
 
7
7
  Object.defineProperty(_sha2, "__esModule", { value: true });
8
8
  _sha2.SHA2 = void 0;
@@ -1,4 +1,4 @@
1
- import { __exports as _u64 } from '../../../../../../_virtual/_u64.js';
1
+ import { __exports as _u64 } from '../../../../../_virtual/_u64.js';
2
2
 
3
3
  (function (exports) {
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,4 +1,4 @@
1
- import { __exports as cryptoBrowser } from '../../../../../../_virtual/cryptoBrowser.js';
1
+ import { __exports as cryptoBrowser } from '../../../../../_virtual/cryptoBrowser.js';
2
2
 
3
3
  Object.defineProperty(cryptoBrowser, "__esModule", { value: true });
4
4
  cryptoBrowser.crypto = void 0;
@@ -0,0 +1,43 @@
1
+ function number(n) {
2
+ if (!Number.isSafeInteger(n) || n < 0)
3
+ throw new Error(`Wrong positive integer: ${n}`);
4
+ }
5
+ function bool(b) {
6
+ if (typeof b !== 'boolean')
7
+ throw new Error(`Expected boolean, not ${b}`);
8
+ }
9
+ function bytes(b, ...lengths) {
10
+ if (!(b instanceof Uint8Array))
11
+ throw new TypeError('Expected Uint8Array');
12
+ if (lengths.length > 0 && !lengths.includes(b.length))
13
+ throw new TypeError(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
14
+ }
15
+ function hash(hash) {
16
+ if (typeof hash !== 'function' || typeof hash.create !== 'function')
17
+ throw new Error('Hash should be wrapped by utils.wrapConstructor');
18
+ number(hash.outputLen);
19
+ number(hash.blockLen);
20
+ }
21
+ function exists(instance, checkFinished = true) {
22
+ if (instance.destroyed)
23
+ throw new Error('Hash instance has been destroyed');
24
+ if (checkFinished && instance.finished)
25
+ throw new Error('Hash#digest() has already been called');
26
+ }
27
+ function output(out, instance) {
28
+ bytes(out);
29
+ const min = instance.outputLen;
30
+ if (out.length < min) {
31
+ throw new Error(`digestInto() expects output buffer of length at least ${min}`);
32
+ }
33
+ }
34
+ const assert = {
35
+ number,
36
+ bool,
37
+ bytes,
38
+ hash,
39
+ exists,
40
+ output,
41
+ };
42
+
43
+ export { bool, bytes, assert as default, exists, hash, number, output };
@@ -0,0 +1,116 @@
1
+ import assert from './_assert.js';
2
+ import { Hash, createView, toBytes } from './utils.js';
3
+
4
+ // Polyfill for Safari 14
5
+ function setBigUint64(view, byteOffset, value, isLE) {
6
+ if (typeof view.setBigUint64 === 'function')
7
+ return view.setBigUint64(byteOffset, value, isLE);
8
+ const _32n = BigInt(32);
9
+ const _u32_max = BigInt(0xffffffff);
10
+ const wh = Number((value >> _32n) & _u32_max);
11
+ const wl = Number(value & _u32_max);
12
+ const h = isLE ? 4 : 0;
13
+ const l = isLE ? 0 : 4;
14
+ view.setUint32(byteOffset + h, wh, isLE);
15
+ view.setUint32(byteOffset + l, wl, isLE);
16
+ }
17
+ // Base SHA2 class (RFC 6234)
18
+ class SHA2 extends Hash {
19
+ constructor(blockLen, outputLen, padOffset, isLE) {
20
+ super();
21
+ this.blockLen = blockLen;
22
+ this.outputLen = outputLen;
23
+ this.padOffset = padOffset;
24
+ this.isLE = isLE;
25
+ this.finished = false;
26
+ this.length = 0;
27
+ this.pos = 0;
28
+ this.destroyed = false;
29
+ this.buffer = new Uint8Array(blockLen);
30
+ this.view = createView(this.buffer);
31
+ }
32
+ update(data) {
33
+ assert.exists(this);
34
+ const { view, buffer, blockLen } = this;
35
+ data = toBytes(data);
36
+ const len = data.length;
37
+ for (let pos = 0; pos < len;) {
38
+ const take = Math.min(blockLen - this.pos, len - pos);
39
+ // Fast path: we have at least one block in input, cast it to view and process
40
+ if (take === blockLen) {
41
+ const dataView = createView(data);
42
+ for (; blockLen <= len - pos; pos += blockLen)
43
+ this.process(dataView, pos);
44
+ continue;
45
+ }
46
+ buffer.set(data.subarray(pos, pos + take), this.pos);
47
+ this.pos += take;
48
+ pos += take;
49
+ if (this.pos === blockLen) {
50
+ this.process(view, 0);
51
+ this.pos = 0;
52
+ }
53
+ }
54
+ this.length += data.length;
55
+ this.roundClean();
56
+ return this;
57
+ }
58
+ digestInto(out) {
59
+ assert.exists(this);
60
+ assert.output(out, this);
61
+ this.finished = true;
62
+ // Padding
63
+ // We can avoid allocation of buffer for padding completely if it
64
+ // was previously not allocated here. But it won't change performance.
65
+ const { buffer, view, blockLen, isLE } = this;
66
+ let { pos } = this;
67
+ // append the bit '1' to the message
68
+ buffer[pos++] = 0b10000000;
69
+ this.buffer.subarray(pos).fill(0);
70
+ // we have less than padOffset left in buffer, so we cannot put length in current block, need process it and pad again
71
+ if (this.padOffset > blockLen - pos) {
72
+ this.process(view, 0);
73
+ pos = 0;
74
+ }
75
+ // Pad until full block byte with zeros
76
+ for (let i = pos; i < blockLen; i++)
77
+ buffer[i] = 0;
78
+ // Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
79
+ // You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
80
+ // So we just write lowest 64 bits of that value.
81
+ setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
82
+ this.process(view, 0);
83
+ const oview = createView(out);
84
+ const len = this.outputLen;
85
+ // NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
86
+ if (len % 4)
87
+ throw new Error('_sha2: outputLen should be aligned to 32bit');
88
+ const outLen = len / 4;
89
+ const state = this.get();
90
+ if (outLen > state.length)
91
+ throw new Error('_sha2: outputLen bigger than state');
92
+ for (let i = 0; i < outLen; i++)
93
+ oview.setUint32(4 * i, state[i], isLE);
94
+ }
95
+ digest() {
96
+ const { buffer, outputLen } = this;
97
+ this.digestInto(buffer);
98
+ const res = buffer.slice(0, outputLen);
99
+ this.destroy();
100
+ return res;
101
+ }
102
+ _cloneInto(to) {
103
+ to || (to = new this.constructor());
104
+ to.set(...this.get());
105
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
106
+ to.length = length;
107
+ to.pos = pos;
108
+ to.finished = finished;
109
+ to.destroyed = destroyed;
110
+ if (length % blockLen)
111
+ to.buffer.set(buffer);
112
+ return to;
113
+ }
114
+ }
115
+
116
+ export { SHA2 };