This repository was archived by the owner on May 26, 2023. It is now read-only.
File tree 4 files changed +14
-22
lines changed
packages/zilliqa-js-crypto
4 files changed +14
-22
lines changed Original file line number Diff line number Diff line change 26
26
},
27
27
"dependencies" : {
28
28
"@types/uuid" : " 8.3.1" ,
29
- "@types/sodium-native" : " 2.3.5" ,
30
29
"@types/elliptic" : " ^6.4.13" ,
31
30
"@zilliqa-js/util" : " ^3.3.2" ,
32
31
"aes-js" : " ^3.1.1" ,
Original file line number Diff line number Diff line change 15
15
// You should have received a copy of the GNU General Public License
16
16
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17
17
18
- import sodium from 'sodium-native' ;
19
-
20
18
/**
21
19
* randomBytes
22
20
*
@@ -69,6 +67,15 @@ export const randomBytes = (bytes: number) => {
69
67
// References:
70
68
// - https://paragonie.com/blog/2016/05/how-generate-secure-random-numbers-in-various-programming-languages#nodejs-csprng
71
69
// - https://github.com/nodejs/node/issues/5798
70
+ //
71
+ // This logic should run only in node env. Otherwise, it will throw an error 'require is not defined'.
72
+ //
73
+ // Consider using createRequire when typescipt 4.5 is available.
74
+ // https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta
75
+ // https://nodejs.org/api/module.html#modulecreaterequirefilename
76
+ //
77
+ // eslint-disable-next-line
78
+ const sodium = require ( 'sodium-native' ) ;
72
79
sodium . randombytes_buf ( b ) ;
73
80
} else {
74
81
throw new Error ( 'No secure random number generator available' ) ;
Original file line number Diff line number Diff line change 21
21
22
22
import { randomBytes } from '../src/random' ;
23
23
24
- const mockFn = jest . fn ( ) ;
25
-
26
- jest . mock ( 'sodium-native' ) ;
27
-
28
- // eslint-disable-next-line
29
- const sodium = require ( 'sodium-native' ) ;
30
-
31
- sodium . randombytes_buf . mockImplementation ( ( ) => {
32
- mockFn ( ) ;
33
- return global . Buffer . allocUnsafe ( 16 ) . fill ( 0 ) ;
34
- } ) ;
35
-
36
24
beforeEach ( ( ) => {
37
25
jest . resetModules ( ) ;
38
26
} ) ;
@@ -47,6 +35,11 @@ describe('random', () => {
47
35
} ,
48
36
} ) ;
49
37
38
+ const mockFn = jest . fn ( ) ;
39
+ jest . doMock ( 'sodium-native' , ( ) => {
40
+ return { randombytes_buf : mockFn } ;
41
+ } ) ;
42
+
50
43
const want = '00000000000000000000000000000000' ;
51
44
const result = randomBytes ( 16 ) ;
52
45
expect ( result ) . toEqual ( want ) ;
Original file line number Diff line number Diff line change 1840
1840
resolved "https://registry.npmjs.org/@types/prettier/-/prettier-2.3.2.tgz#fc8c2825e4ed2142473b4a81064e6e081463d1b3"
1841
1841
integrity sha512-eI5Yrz3Qv4KPUa/nSIAi0h+qX0XyewOliug5F2QAtuRg6Kjg6jfmxe1GIwoIRhZspD1A0RP8ANrPwvEXXtRFog==
1842
1842
1843
-
1844
- version "2.3.5"
1845
- resolved "https://registry.npmjs.org/@types/sodium-native/-/sodium-native-2.3.5.tgz#5d2681e7b6b67bcbdc63cfb133e303ec9e942e43"
1846
- integrity sha512-a3DAIpW8+36XAY8aIR36JBQQsfOabxHuJwx11DL/PTvnbwEWPAXW66b8QbMi0r2vUnkOfREsketxdvjBmQxqDQ==
1847
- dependencies :
1848
- " @types/node" " *"
1849
-
1850
1843
" @types/source-list-map@* " :
1851
1844
version "0.1.2"
1852
1845
resolved "https://registry.npmjs.org/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9"
You can’t perform that action at this time.
0 commit comments