Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding react-native environment #187

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions envs/react-native/get-local-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { AsyncStorage } = require('react-native');

function getLocalStorage() {
return {
set: AsyncStorage.setItem,
get: AsyncStorage.getItem
}
}

module.exports = getLocalStorage;
9 changes: 9 additions & 0 deletions envs/react-native/get-random-bytes.js
Original file line number Diff line number Diff line change
@@ -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;
27 changes: 27 additions & 0 deletions envs/react-native/index.js
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 7 additions & 0 deletions envs/react-native/pbkdf2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const sha256 = require("fast-sha256");

async function PBKDF2(password, salt, iterations) {
return sha256.pbkdf2(password, salt, iterations, 512);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May return incorrect string. Also sometimes getting SRP_ID_INVALID error

}

module.exports = PBKDF2;
10 changes: 10 additions & 0 deletions envs/react-native/sha1.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 10 additions & 0 deletions envs/react-native/sha256.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions envs/react-native/transport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../browser/transport');
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"/src"
],
"main": "./envs/node/index.js",
"react-native": "./envs/react-native/index.js",
"engines": {
"node": ">=12"
},
Expand All @@ -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"
}
Comment on lines +59 to 65
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The package file.json is used for all environments, so I think it's better to remove peerDependencies for react-native from here. Instead, I will add instructions for installing these dependencies to the documentation

}