-
Notifications
You must be signed in to change notification settings - Fork 119
crypto and localstorage error in react native #96
Comments
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 From what I can tell, the APIs required are:
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 ( 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 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: #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 🙏🏻.
The text was updated successfully, but these errors were encountered: