diff --git a/envs/react-native/get-local-storage.js b/envs/react-native/get-local-storage.js new file mode 100644 index 00000000..d065a0c6 --- /dev/null +++ b/envs/react-native/get-local-storage.js @@ -0,0 +1,10 @@ +const { AsyncStorage } = require('react-native'); + +function getLocalStorage() { + return { + set: AsyncStorage.setItem, + get: AsyncStorage.getItem + } +} + +module.exports = getLocalStorage; diff --git a/envs/react-native/get-random-bytes.js b/envs/react-native/get-random-bytes.js new file mode 100644 index 00000000..b7d4b77c --- /dev/null +++ b/envs/react-native/get-random-bytes.js @@ -0,0 +1,9 @@ +require('react-native-get-random-values'); + +function getRandomBytes(length) { + const bytes = new Uint8Array(length); + crypto.getRandomValues(bytes); + return bytes; +} + +module.exports = getRandomBytes; diff --git a/envs/react-native/index.js b/envs/react-native/index.js new file mode 100644 index 00000000..403c84b5 --- /dev/null +++ b/envs/react-native/index.js @@ -0,0 +1,27 @@ +const { polyfillGlobal } = require("react-native/Libraries/Utilities/PolyfillFunctions"); +const { TextEncoder, TextDecoder } = require("text-encoding"); +const makeMTProto = require('../../src'); +const SHA1 = require('./sha1'); +const SHA256 = require('./sha256'); +const PBKDF2 = require('./pbkdf2'); +const Transport = require('./transport'); +const getRandomBytes = require('./get-random-bytes'); +const getLocalStorage = require('./get-local-storage'); + +polyfillGlobal("TextEncoder", () => TextEncoder); +polyfillGlobal("TextDecoder", () => TextDecoder); + +function createTransport(dc, crypto) { + return new Transport(dc, crypto); +} + +const MTProto = makeMTProto({ + SHA1, + SHA256, + PBKDF2, + getRandomBytes, + getLocalStorage, + createTransport, +}); + +module.exports = MTProto; diff --git a/envs/react-native/pbkdf2.js b/envs/react-native/pbkdf2.js new file mode 100644 index 00000000..7e9dba9e --- /dev/null +++ b/envs/react-native/pbkdf2.js @@ -0,0 +1,7 @@ +const sha256 = require("fast-sha256"); + +async function PBKDF2(password, salt, iterations) { + return sha256.pbkdf2(password, salt, iterations, 512); +} + +module.exports = PBKDF2; diff --git a/envs/react-native/sha1.js b/envs/react-native/sha1.js new file mode 100644 index 00000000..9771d97f --- /dev/null +++ b/envs/react-native/sha1.js @@ -0,0 +1,10 @@ +const jsSHA = require("jssha"); + +async function SHA1(data) { + const format = data instanceof Uint8Array ? "UINT8ARRAY" : "ARRAYBUFFER"; + const digest = new jsSHA("SHA-1", format) + digest.update(data) + return digest.getHash("UINT8ARRAY") +} + +module.exports = SHA1; \ No newline at end of file diff --git a/envs/react-native/sha256.js b/envs/react-native/sha256.js new file mode 100644 index 00000000..84747c45 --- /dev/null +++ b/envs/react-native/sha256.js @@ -0,0 +1,10 @@ +const jsSHA = require("jssha"); + +async function SHA256(data) { + const format = data instanceof Uint8Array ? "UINT8ARRAY" : "ARRAYBUFFER"; + const digest = new jsSHA("SHA-256", format) + digest.update(data) + return digest.getHash("UINT8ARRAY") +} + +module.exports = SHA256; \ No newline at end of file diff --git a/envs/react-native/transport.js b/envs/react-native/transport.js new file mode 100644 index 00000000..d1865836 --- /dev/null +++ b/envs/react-native/transport.js @@ -0,0 +1 @@ +module.exports = require('../browser/transport'); \ No newline at end of file diff --git a/package.json b/package.json index de04209b..f519385e 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "/src" ], "main": "./envs/node/index.js", + "react-native": "./envs/react-native/index.js", "engines": { "node": ">=12" }, @@ -54,5 +55,12 @@ "devDependencies": { "jest": "26.6.3", "npm-run-all": "4.1.5" + }, + "peerDependencies": { + "react-native": "*", + "fast-sha256": "^1.3.0", + "jssha": "^3.2.0", + "react-native-get-random-values": "^1.7.0", + "text-encoding": "^0.7.0" } }