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

expo example updates #97

Merged
merged 1 commit into from
Jul 5, 2024
Merged
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
1 change: 0 additions & 1 deletion demo/rn-bare-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"jest": "^29.7.0",
"prettier": "^3.3.2",
"process": "^0.11.10",
"react-native-get-random-values": "^1.11.0",
"react-native-quick-crypto": "^0.7.0",
"react-test-renderer": "18.2.0",
"readable-stream": "^4.5.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="myapp"/>
<data android:scheme="com.anonymous.rnexpoexample"/>
<data android:scheme="com.anonymous.rnexpoexample"/>
</intent-filter>
</activity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<item name="android:editTextStyle">@style/ResetEditText</item>
<item name="android:editTextBackground">@drawable/rn_edit_text_material</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:statusBarColor">#ffffff</item>
</style>
<style name="ResetEditText" parent="@android:style/Widget.EditText">
<item name="android:padding">0dp</item>
Expand Down
53 changes: 32 additions & 21 deletions demo/rn-expo-example/app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,54 @@ import * as WebBrowser from "expo-web-browser";

import { Button, Dimensions, ScrollView, StyleSheet, Text, TextInput, View } from "react-native";
import Constants, { AppOwnership } from "expo-constants";
import React, { useEffect, useState } from "react";
import Web3Auth, { ChainNamespace, LOGIN_PROVIDER } from "@web3auth/react-native-sdk";
import { useEffect, useState } from "react";
import Web3Auth, { ChainNamespace, LOGIN_PROVIDER, OpenloginUserInfo } from "@web3auth/react-native-sdk";

import RPC from "../ethersRPC"; // for using ethers.js

const chainConfig = {
chainNamespace: ChainNamespace.EIP155,
chainId: "0xaa36a7",
rpcTarget: "https://rpc.ankr.com/eth_sepolia",
// Avoid using public rpcTarget in production.
// Use services like Infura, Quicknode etc
displayName: "Ethereum Sepolia Testnet",
blockExplorerUrl: "https://sepolia.etherscan.io",
ticker: "ETH",
tickerName: "Ethereum",
decimals: 18,
logo: "https://cryptologos.cc/logos/ethereum-eth-logo.png",
};

const resolvedRedirectUrl =
Constants.appOwnership == AppOwnership.Expo || Constants.appOwnership == AppOwnership.Guest
Constants.appOwnership == AppOwnership.Expo
? Linking.createURL("web3auth", {})
: Linking.createURL("web3auth", { scheme: "com.anonymous.rnexpoexample" });

const clientId = "BILxiaDYbvlcwNdeJsyXEUDieKdYPIHfSdvEabzidwYZ3zaGsEN6noiM5u8f-5wuIksJcOn-Ga1LWNqen1eUZbw";
const clientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ";

export default function App() {
const [key, setKey] = useState("");
const [userInfo, setUserInfo] = useState("");
const [userInfo, setUserInfo] = useState({});
const [console, setConsole] = useState("");
const [web3auth, setWeb3Auth] = useState(null);
const [email, setEmail] = React.useState("[email protected]");
const [web3auth, setWeb3Auth] = useState<Web3Auth | null>(null);
const [email, setEmail] = useState("[email protected]");

useEffect(() => {
const init = async () => {
try {
const auth = new Web3Auth(WebBrowser, SecureStore, {
clientId,
network: "cyan", // or other networks
network: "sapphire_mainnet", // or other networks
redirectUrl: resolvedRedirectUrl,
enableLogging: true,
buildEnv: "testing",
});
setWeb3Auth(auth);
await auth.init();
if (auth?.privKey) {
uiConsole("Re logged in");
setUserInfo(auth.userInfo());
setUserInfo(auth.userInfo() as OpenloginUserInfo);
setKey(auth.privKey);
}
} catch (e) {
Expand All @@ -47,16 +64,16 @@ export default function App() {
const login = async () => {
try {
setConsole("Logging in");
await web3auth.login({
await web3auth?.login({
loginProvider: LOGIN_PROVIDER.EMAIL_PASSWORDLESS,
redirectUrl: resolvedRedirectUrl,
extraLoginOptions: {
login_hint: email,
},
});

if (web3auth.privKey) {
setUserInfo(web3auth.userInfo());
if (web3auth?.privKey) {
setUserInfo(web3auth?.userInfo() as OpenloginUserInfo);
setKey(web3auth.privKey);
uiConsole("Logged In");
}
Expand Down Expand Up @@ -84,13 +101,7 @@ export default function App() {
}

setConsole("Launch Wallet Services");
await web3auth.launchWalletServices({
chainNamespace: ChainNamespace.EIP155,
decimals: 18,
chainId: "0x1",
rpcTarget: "https://mainnet.infura.io/v3/daeee53504be4cd3a997d4f2718d33e0",
ticker: "ETH",
});
await web3auth.launchWalletServices(chainConfig);
};

const logout = async () => {
Expand All @@ -103,7 +114,7 @@ export default function App() {
await web3auth.logout();

if (!web3auth.privKey) {
setUserInfo(undefined);
setUserInfo({});
setKey("");
uiConsole("Logged out");
}
Expand Down Expand Up @@ -233,7 +244,7 @@ export default function App() {
uiConsole(res);
};

const uiConsole = (...args) => {
const uiConsole = (...args: unknown[]) => {
setConsole(`${JSON.stringify(args || {}, null, 2)}\n\n\n\n${console}`);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ const getChainId = async () => {
}
};

const getAccounts = async (key) => {
const getAccounts = async (key: string) => {
try {
const wallet = new ethers.Wallet(key);
const address = await wallet.address;
const address = wallet.address;
return address;
} catch (error) {
return error;
}
};

const getBalance = async (key) => {
const getBalance = async (key: string) => {
try {
const ethersProvider = ethers.getDefaultProvider(providerUrl);
const wallet = new ethers.Wallet(key, ethersProvider);
Expand All @@ -36,7 +36,7 @@ const getBalance = async (key) => {
}
};

const sendTransaction = async (key) => {
const sendTransaction = async (key: string) => {
try {
const ethersProvider = ethers.getDefaultProvider(providerUrl);
const wallet = new ethers.Wallet(key, ethersProvider);
Expand All @@ -60,7 +60,7 @@ const sendTransaction = async (key) => {
}
};

const signMessage = async (key) => {
const signMessage = async (key: string) => {
try {
const ethersProvider = ethers.getDefaultProvider(providerUrl);
const wallet = new ethers.Wallet(key, ethersProvider);
Expand Down
14 changes: 0 additions & 14 deletions demo/rn-expo-example/globals.js

This file was deleted.

17 changes: 17 additions & 0 deletions demo/rn-expo-example/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { install } from "react-native-quick-crypto";

install();

// Needed so that 'stream-http' chooses the right default protocol.
// @ts-ignore
global.location = {
protocol: "file:",
};
// @ts-ignore
global.process.version = "v16.0.0";
if (!global.process.version) {
global.process = require("process");
console.log({ process: global.process });
}
// @ts-ignore
process.browser = true;
3 changes: 1 addition & 2 deletions demo/rn-expo-example/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "./globals";
import "@ethersproject/shims";
import "@expo/metro-runtime";
import "react-native-get-random-values";
import "./globals";

import { App } from "expo-router/build/qualified-entry";
import { renderRootComponent } from "expo-router/build/renderRootComponent";
Expand Down
4 changes: 1 addition & 3 deletions demo/rn-expo-example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ target 'rnexpoexample' do
:hermes_enabled => podfile_properties['expo.jsEngine'] == nil || podfile_properties['expo.jsEngine'] == 'hermes',
# An absolute path to your application root.
:app_path => "#{Pod::Config.instance.installation_root}/..",
# Temporarily disable privacy file aggregation by default, until React
# Native 0.74.2 is released with fixes.
:privacy_file_aggregation_enabled => podfile_properties['apple.privacyManifestAggregationEnabled'] == 'true',
:privacy_file_aggregation_enabled => podfile_properties['apple.privacyManifestAggregationEnabled'] != 'false',
)

post_install do |installer|
Expand Down
Loading