Skip to content
This repository was archived by the owner on May 26, 2023. It is now read-only.

Commit fc8f66f

Browse files
author
Noel
authored
fix: sodium-native (#432)
1 parent 2f7ba77 commit fc8f66f

File tree

4 files changed

+14
-22
lines changed

4 files changed

+14
-22
lines changed

packages/zilliqa-js-crypto/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
},
2727
"dependencies": {
2828
"@types/uuid": "8.3.1",
29-
"@types/sodium-native": "2.3.5",
3029
"@types/elliptic": "^6.4.13",
3130
"@zilliqa-js/util": "^3.3.2",
3231
"aes-js": "^3.1.1",

packages/zilliqa-js-crypto/src/random.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
// You should have received a copy of the GNU General Public License
1616
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1717

18-
import sodium from 'sodium-native';
19-
2018
/**
2119
* randomBytes
2220
*
@@ -69,6 +67,15 @@ export const randomBytes = (bytes: number) => {
6967
// References:
7068
// - https://paragonie.com/blog/2016/05/how-generate-secure-random-numbers-in-various-programming-languages#nodejs-csprng
7169
// - 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');
7279
sodium.randombytes_buf(b);
7380
} else {
7481
throw new Error('No secure random number generator available');

packages/zilliqa-js-crypto/test/random.node.spec.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@
2121

2222
import { randomBytes } from '../src/random';
2323

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-
3624
beforeEach(() => {
3725
jest.resetModules();
3826
});
@@ -47,6 +35,11 @@ describe('random', () => {
4735
},
4836
});
4937

38+
const mockFn = jest.fn();
39+
jest.doMock('sodium-native', () => {
40+
return { randombytes_buf: mockFn };
41+
});
42+
5043
const want = '00000000000000000000000000000000';
5144
const result = randomBytes(16);
5245
expect(result).toEqual(want);

yarn.lock

-7
Original file line numberDiff line numberDiff line change
@@ -1840,13 +1840,6 @@
18401840
resolved "https://registry.npmjs.org/@types/prettier/-/prettier-2.3.2.tgz#fc8c2825e4ed2142473b4a81064e6e081463d1b3"
18411841
integrity sha512-eI5Yrz3Qv4KPUa/nSIAi0h+qX0XyewOliug5F2QAtuRg6Kjg6jfmxe1GIwoIRhZspD1A0RP8ANrPwvEXXtRFog==
18421842

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-
18501843
"@types/source-list-map@*":
18511844
version "0.1.2"
18521845
resolved "https://registry.npmjs.org/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9"

0 commit comments

Comments
 (0)