Open destimon opened 3 years ago
customLocalStorage
.You can follow future releases in the official telegram channel - https://t.me/mtproto_core
I've spent a bunch of time attempting to get the correct crypto
APIs to be available in react-native, without success.
From what I can tell, the APIs required are:
crypto.getRandomBytes
crypto.rsa
crypto.SHA1
crypto.SHA256
crypto.getRandomBytes
crypto.subtle.deriveBits
, crypto.subtle.importKey
, and crypto.subtle.digest
I've found alternatives to these APIs in various packages, but ultimately run into either compatibility problems or an API that is async when MTProto expects it to be sync (getRandomBytes
for example).
Do you have a suggestion for an alternative library that would work in react-native? If not, is there anything I can do to help get react-native support? Thanks!
I had just about given up, but then tried the react-native-webview-crypto package.
Here's what worked for me:
App.js
import React, { useEffect } from "react";
import WebviewCrypto from "react-native-webview-crypto";
import { TextEncoder, TextDecoder } from "web-encoding";
import "react-native-get-random-values";
import { View } from "react-native";
import { polyfillGlobal } from "react-native/Libraries/Utilities/PolyfillFunctions";
import MTProto from "@mtproto/core/envs/browser";
polyfillGlobal("TextEncoder", () => TextEncoder);
polyfillGlobal("TextDecoder", () => TextDecoder);
// Dummy storage to get things working
class CustomStorage {
constructor() {
this.data = {};
}
set(key, value) {
this.data[key] = value;
return Promise.resolve();
}
get(key) {
return Promise.resolve(this.data[key]);
}
}
const App = () => {
useEffect(() => {
// REPLACE WITH YOUR VALUES
const api_id = "xxxxxxx";
const api_hash = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const mtproto = new MTProto({
api_id,
api_hash,
storageOptions: {
instance: new CustomStorage(),
},
});
// 2. Print the user country code
mtproto.call("help.getNearestDc").then(result => {
console.log("Nearest DC:", result.country);
});
}, []);
return (
<View>
<WebviewCrypto />
</View>
);
};
export default App;
You'll need react-native-webview-crypto
, web-encoding
, react-native-get-random-values
, as well as the polyfillGlobal
helper built into react-native
. Then load the browser version of MTProto located at @mtproto/core/envs/browser
.
I hope this saves someone time in the future. If I run into any other issues, I'll followup in this thread.
Added a PR here: https://github.com/alik0211/mtproto-core/pull/187
Currently trying to use mtproto-core in react-native project.
Have next issues:
During call method from the instance - getting undefined localStorage
Any call method causes that error. Obviously, localStorage property, as i checked, is undefined in mtproto instance after initializating.
I've tried to replace default storage with custom storage:
and now getting the next error:
Seems like it can't support Node.js, but in simple Node.js project(w/o any frameworks) everything works fine.
Will be grateful for any help šš».