Skip to content
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
2 changes: 1 addition & 1 deletion demo/rn-bare-aggregate-verifier-example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export default function App() {
setConsole("Getting account");
const ethersProvider = new ethers.BrowserProvider(provider!);
const signer = await ethersProvider.getSigner();
const address = signer.getAddress();
const address = await signer.getAddress();
uiConsole(address);
return address;
};
Expand Down
124 changes: 14 additions & 110 deletions demo/rn-bare-example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,8 @@ import "@ethersproject/shims";

// IMP START - Quick Start
import * as WebBrowser from "@toruslabs/react-native-web-browser";
import {
AccountAbstractionProvider,
BiconomySmartAccount,
ISmartAccount,
KernelSmartAccount,
SafeSmartAccount,
TrustSmartAccount,
} from "@web3auth/account-abstraction-provider";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
import Web3Auth, { AUTH_CONNECTION, CHAIN_NAMESPACES, WEB3AUTH_NETWORK } from "@web3auth/react-native-sdk";
import { ethers } from "ethers";
import Web3Auth, { AUTH_CONNECTION, WEB3AUTH_NETWORK } from "@web3auth/react-native-sdk";
import { ethers, Wallet } from "ethers";
import React, { useEffect, useState } from "react";
import { Button, Dimensions, ScrollView, StyleSheet, Switch, Text, TextInput, View } from "react-native";
import EncryptedStorage from "react-native-encrypted-storage";
Expand All @@ -28,27 +19,6 @@ const redirectUrl = `${scheme}://auth`;
const clientId = "BCfIbiMcEwBkmyNxwn-DcYIfUU4QrpQgyOZZTNi5f_ygWMS1g_dNcuxylwDkIbVNhDtn7dAs-aMUhX0dtAYhvWk"; // get from https://dashboard.web3auth.io
// IMP END - Dashboard Registration

// IMP START - SDK Initialization
const chainConfig = {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0xaa36a7",
rpcTarget: `https://api.web3auth.io/infura-service/v1/0xaa36a7/${clientId}`,
// 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 privateKeyProvider = new EthereumPrivateKeyProvider({
config: {
chainConfig,
},
});

const PIMLICO_API_KEY = "pim_WDBELWbZeo9guUAr7HNFaF";

export const getDefaultBundlerUrl = (chainId: string): string => {
Expand All @@ -75,7 +45,7 @@ const storage = new MMKVLoader().initialize();
export default function App() {
const [web3auth, setWeb3auth] = useState<Web3Auth | null>(null);
const [loggedIn, setLoggedIn] = useState(false);
const [provider, setProvider] = useState<any>(null);
const [signer, setSigner] = useState<Wallet | null>(null);
const [console, setConsole] = useState<string>("");
const [email, setEmail] = useState<string>("");
const [useAccountAbstraction, setUseAccountAbstraction] = useMMKVStorage<boolean>("useAccountAbstraction", storage, false);
Expand All @@ -86,58 +56,13 @@ export default function App() {

useEffect(() => {
const init = async () => {
// setup aa provider
let aaProvider: AccountAbstractionProvider | undefined;
if (useAccountAbstraction) {
const { bundlerUrl, paymasterUrl, smartAccountType } = AAConfig;

let smartAccountInit: ISmartAccount;
switch (smartAccountType) {
case "biconomy":
smartAccountInit = new BiconomySmartAccount();
break;
case "kernel":
smartAccountInit = new KernelSmartAccount();
break;
case "trust":
smartAccountInit = new TrustSmartAccount();
break;
// case "light":
// smartAccountInit = new LightSmartAccount();
// break;
// case "simple":
// smartAccountInit = new SimpleSmartAccount();
// break;
case "safe":
default:
smartAccountInit = new SafeSmartAccount();
break;
}

aaProvider = new AccountAbstractionProvider({
config: {
chainConfig,
bundlerConfig: {
url: bundlerUrl ?? getDefaultBundlerUrl(chainConfig.chainId),
},
paymasterConfig: paymasterUrl
? {
url: paymasterUrl,
}
: undefined,
smartAccountInit,
},
});
}

const web3auth = new Web3Auth(WebBrowser, EncryptedStorage, {
clientId,
// IMP START - Whitelist bundle ID
redirectUrl,
// IMP END - Whitelist bundle ID
network: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET, // or other networks
privateKeyProvider,
accountAbstractionProvider: aaProvider,
accountAbstractionConfig: useAccountAbstraction ? AAConfig : null,
});
setWeb3auth(web3auth);

Expand All @@ -146,7 +71,7 @@ export default function App() {

if (web3auth.connected) {
// IMP END - SDK Initialization
setProvider(web3auth.provider);
setSigner(web3auth.signer as Wallet);
setLoggedIn(true);
}
};
Expand Down Expand Up @@ -176,7 +101,7 @@ export default function App() {

if (web3auth.connected) {
// IMP END - Login
setProvider(web3auth.provider);
setSigner(web3auth.signer as Wallet);
uiConsole("Logged In");
setLoggedIn(true);
}
Expand All @@ -197,26 +122,19 @@ export default function App() {
// IMP END - Logout

if (!web3auth.connected) {
setProvider(null);
setSigner(null);
uiConsole("Logged out");
setLoggedIn(false);
}
};

// IMP START - Blockchain Calls
const getAccounts = async (): Promise<string> => {
if (!provider) {
uiConsole("provider not set");
if (!signer) {
uiConsole("signer not set");
return "";
}
setConsole("Getting account");
// For ethers v5
// const ethersProvider = new ethers.providers.Web3Provider(this.provider);
const ethersProvider = new ethers.BrowserProvider(provider!);

// For ethers v5
// const signer = ethersProvider.getSigner();
const signer = await ethersProvider.getSigner();

// Get user's Ethereum public address
const address = signer.getAddress();
Expand All @@ -225,18 +143,11 @@ export default function App() {
};

const getBalance = async () => {
if (!provider) {
uiConsole("provider not set");
if (!signer) {
uiConsole("signer not set");
return;
}
setConsole("Fetching balance");
// For ethers v5
// const ethersProvider = new ethers.providers.Web3Provider(this.provider);
const ethersProvider = new ethers.BrowserProvider(provider!);

// For ethers v5
// const signer = ethersProvider.getSigner();
const signer = await ethersProvider.getSigner();

// Get user's Ethereum public address
const address = signer.getAddress();
Expand All @@ -247,24 +158,17 @@ export default function App() {
// await ethersProvider.getBalance(address) // Balance is in wei
// );
const balance = ethers.formatEther(
await ethersProvider.getBalance(address) // Balance is in wei
(await signer.provider?.getBalance(address))?.toString() ?? "0" // Balance is in wei
);
uiConsole(balance);
};

const signMessage = async () => {
if (!provider) {
uiConsole("provider not set");
if (!signer) {
uiConsole("signer not set");
return;
}
setConsole("Signing message");
// For ethers v5
// const ethersProvider = new ethers.providers.Web3Provider(this.provider);
const ethersProvider = new ethers.BrowserProvider(provider!);

// For ethers v5
// const signer = ethersProvider.getSigner();
const signer = await ethersProvider.getSigner();
const originalMessage = "YOUR_MESSAGE";

// Sign the message
Expand Down
63 changes: 0 additions & 63 deletions demo/rn-bare-example/globals.js

This file was deleted.

6 changes: 5 additions & 1 deletion demo/rn-bare-example/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Web3Auth setup - must be imported first before any other imports
import "@web3auth/react-native-sdk/setup";

import { AppRegistry } from "react-native";
import "./globals";

import App from "./App";
import { name as appName } from "./app.json";

AppRegistry.registerComponent(appName, () => App);
59 changes: 9 additions & 50 deletions demo/rn-bare-example/metro.config.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,11 @@
const path = require("path");
const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");
const { getDefaultConfig } = require("@react-native/metro-config");
const { withWeb3Auth } = require("@web3auth/react-native-sdk/metro-config");

const defaultConfig = getDefaultConfig(__dirname);
const config = getDefaultConfig(__dirname);

const config = {
resolver: {
extraNodeModules: {
assert: require.resolve("empty-module"), // assert can be polyfilled here if needed
http: require.resolve("empty-module"), // stream-http can be polyfilled here if needed
https: require.resolve("empty-module"), // https-browserify can be polyfilled here if needed
os: require.resolve("empty-module"), // os-browserify can be polyfilled here if needed
url: require.resolve("empty-module"), // url can be polyfilled here if needed
zlib: require.resolve("empty-module"), // browserify-zlib can be polyfilled here if needed
path: require.resolve("empty-module"),
crypto: require.resolve("crypto-browserify"),
stream: require.resolve("readable-stream"),
},
sourceExts: [...defaultConfig.resolver.sourceExts, "svg"],
unstable_enablePackageExports: true,
},
};

const baseResolve = defaultConfig.resolver.resolveRequest;

function isRelative(spec) {
return spec.startsWith("./") || spec.startsWith("../");
}

function isFromPathResolveNeeded(context) {
const p = context.originModulePath || "";
// handles macOS/Linux/Windows path separators
const packages = ["permissionless", "ox"];
return packages.some((name) => p.includes(`${path.sep}node_modules${path.sep}${name}${path.sep}`));
}

// Metro (Expo’s bundler) resolves modules before transforming TS. When the specifier already includes “.js”, Metro treats “.js” as part of the filename and does not replace it with “.ts”
config.resolver.resolveRequest = (context, moduleName, platform) => {
// Example: inside node_modules/permissionless/accounts/index.ts
// moduleName is "./kernel/to7702KernelSmartAccount.js"
if (isRelative(moduleName) && moduleName.endsWith(".js") && isFromPathResolveNeeded(context)) {
const withoutExt = moduleName.slice(0, -3); // drop ".js"
try {
return (baseResolve || context.resolveRequest)(context, withoutExt, platform);
} catch (e) {
// fall through if that didn't resolve
}
}
return (baseResolve || context.resolveRequest)(context, moduleName, platform);
};

module.exports = mergeConfig(defaultConfig, config);
// Wrap the config with Web3Auth's Metro configuration
// This automatically sets up all necessary polyfills and shims
module.exports = withWeb3Auth(config, {
// Optional: Add additional packages that need .js extension fix
// additionalPackagesForJsExtFix: ['some-other-package'],
});
Loading