@aztec/foundation 0.87.4 → 0.87.5

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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/crypto/random/index.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,WAAW,GAAI,KAAK,MAAM,4BAOtC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,GAAI,KAAK,MAAM,WAIpC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,GAAI,KAAK,MAAM,WAIvC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,aAAa,eAGzB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/crypto/random/index.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,WAAW,GAAI,KAAK,MAAM,4BA8BtC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,GAAI,KAAK,MAAM,WAIpC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,GAAI,KAAK,MAAM,WAIvC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,aAAa,eAGzB,CAAC"}
@@ -1,11 +1,43 @@
1
- import { randomBytes as bbRandomBytes } from '@aztec/bb.js';
1
+ import nodeCrypto from 'crypto';
2
+ import isNode from 'detect-node';
2
3
  import { RandomnessSingleton } from './randomness_singleton.js';
4
+ // limit of Crypto.getRandomValues()
5
+ // https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
6
+ const MAX_BYTES = 65536;
7
+ const getWebCrypto = ()=>{
8
+ if (typeof window !== 'undefined' && window.crypto) {
9
+ return window.crypto;
10
+ }
11
+ if (typeof self !== 'undefined' && self.crypto) {
12
+ return self.crypto;
13
+ }
14
+ return undefined;
15
+ };
3
16
  export const randomBytes = (len)=>{
4
17
  const singleton = RandomnessSingleton.getInstance();
5
18
  if (singleton.isDeterministic()) {
6
19
  return singleton.getBytes(len);
7
20
  }
8
- return Buffer.from(bbRandomBytes(len));
21
+ if (isNode) {
22
+ return nodeCrypto.randomBytes(len);
23
+ }
24
+ const crypto = getWebCrypto();
25
+ if (!crypto) {
26
+ throw new Error('randomBytes UnsupportedEnvironment');
27
+ }
28
+ const buf = Buffer.allocUnsafe(len);
29
+ if (len > MAX_BYTES) {
30
+ // this is the max bytes crypto.getRandomValues
31
+ // can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
32
+ for(let generated = 0; generated < len; generated += MAX_BYTES){
33
+ // buffer.slice automatically checks if the end is past the end of
34
+ // the buffer so we don't have to here
35
+ crypto.getRandomValues(buf.slice(generated, generated + MAX_BYTES));
36
+ }
37
+ } else {
38
+ crypto.getRandomValues(buf);
39
+ }
40
+ return buf;
9
41
  };
10
42
  /**
11
43
  * Generate a random integer less than max.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/foundation",
3
- "version": "0.87.4",
3
+ "version": "0.87.5",
4
4
  "type": "module",
5
5
  "main": "./dest/index.js",
6
6
  "types": "./dest/index.d.ts",
@@ -100,11 +100,13 @@
100
100
  ]
101
101
  },
102
102
  "dependencies": {
103
- "@aztec/bb.js": "0.87.4",
103
+ "@aztec/bb.js": "0.87.5",
104
104
  "@koa/cors": "^5.0.0",
105
105
  "@noble/curves": "^1.2.0",
106
+ "bn.js": "^5.2.1",
106
107
  "c-kzg": "4.0.0-alpha.1",
107
108
  "colorette": "^2.0.20",
109
+ "debug": "^4.3.4",
108
110
  "detect-node": "^2.1.0",
109
111
  "hash.js": "^1.1.7",
110
112
  "koa": "^2.16.1",
@@ -124,6 +126,8 @@
124
126
  "devDependencies": {
125
127
  "@jest/globals": "^29.5.0",
126
128
  "@libp2p/interface": "1.3.1",
129
+ "@types/bn.js": "^5.1.3",
130
+ "@types/debug": "^4.1.7",
127
131
  "@types/detect-node": "^2.0.0",
128
132
  "@types/jest": "^29.5.0",
129
133
  "@types/koa": "^2.15.0",
@@ -1,14 +1,52 @@
1
- import { randomBytes as bbRandomBytes } from '@aztec/bb.js';
1
+ import nodeCrypto from 'crypto';
2
+ import isNode from 'detect-node';
2
3
 
3
4
  import { RandomnessSingleton } from './randomness_singleton.js';
4
5
 
6
+ // limit of Crypto.getRandomValues()
7
+ // https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
8
+ const MAX_BYTES = 65536;
9
+
10
+ const getWebCrypto = () => {
11
+ if (typeof window !== 'undefined' && window.crypto) {
12
+ return window.crypto;
13
+ }
14
+ if (typeof self !== 'undefined' && self.crypto) {
15
+ return self.crypto;
16
+ }
17
+ return undefined;
18
+ };
19
+
5
20
  export const randomBytes = (len: number) => {
6
21
  const singleton = RandomnessSingleton.getInstance();
7
22
 
8
23
  if (singleton.isDeterministic()) {
9
24
  return singleton.getBytes(len);
10
25
  }
11
- return Buffer.from(bbRandomBytes(len)) as Buffer<ArrayBuffer>;
26
+
27
+ if (isNode) {
28
+ return nodeCrypto.randomBytes(len) as Buffer;
29
+ }
30
+
31
+ const crypto = getWebCrypto();
32
+ if (!crypto) {
33
+ throw new Error('randomBytes UnsupportedEnvironment');
34
+ }
35
+
36
+ const buf = Buffer.allocUnsafe(len);
37
+ if (len > MAX_BYTES) {
38
+ // this is the max bytes crypto.getRandomValues
39
+ // can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
40
+ for (let generated = 0; generated < len; generated += MAX_BYTES) {
41
+ // buffer.slice automatically checks if the end is past the end of
42
+ // the buffer so we don't have to here
43
+ crypto.getRandomValues(buf.slice(generated, generated + MAX_BYTES));
44
+ }
45
+ } else {
46
+ crypto.getRandomValues(buf);
47
+ }
48
+
49
+ return buf;
12
50
  };
13
51
 
14
52
  /**