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

Ethereum wallets selector. #1106

Merged
merged 47 commits into from
Jul 25, 2024
Merged
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
2c61d49
feat: Ethereum wallets mvp.
paouvrard Apr 24, 2024
8461c0b
feat: Relayer onboarding.
paouvrard May 3, 2024
0c468be
fix: Use near_getPublicKey api.
paouvrard May 8, 2024
8391e5b
feat: Get receipt and txStatus.
paouvrard May 14, 2024
16b5927
fix: Remove RLP_EXECUTE check.
paouvrard May 15, 2024
cee568b
fix: Handling of network switch from wallet.
paouvrard May 15, 2024
28b2468
style: Update logo.
paouvrard May 15, 2024
9a0a901
fix: Re-apply rlp_execute limitation to relayer key.
paouvrard May 15, 2024
fc18d7c
build: Upgrade web3Modal, add WC example.
paouvrard May 16, 2024
6c137e1
feat: Query estimateGas (also fixes MetaMask mobile).
paouvrard May 20, 2024
07705d4
fix: Improve modal text.
paouvrard May 21, 2024
27540ee
style: Modal lint.
paouvrard May 21, 2024
7a6c373
fix: Improve onboarding text.
paouvrard May 22, 2024
d26fbb0
feat: Parse error from NEAR transaction.
paouvrard May 23, 2024
d2a0f3e
build: Update assets ethereum-wallets-icon.png.
paouvrard May 27, 2024
73aff7a
feat: Network not supported error message.
paouvrard May 27, 2024
a52e7e2
feat: Optional wagmiCore param and dynamic import.
paouvrard May 27, 2024
396fab0
fix: Dynamic import on page reload.
paouvrard May 28, 2024
ec044bb
Add modal styling (#5)
santerisarle May 29, 2024
4deef6f
refactor: Remove rpcUrl param, get explorer url from wagmiConfig.
paouvrard May 29, 2024
ccb803d
chore: Improve docs.
paouvrard May 29, 2024
34608cd
build: Fix lint.
paouvrard May 29, 2024
ab88d31
More design (#6)
santerisarle May 31, 2024
f141026
style: Tweak text.
paouvrard May 31, 2024
72abcca
fix: Custom chainId.
paouvrard Jun 5, 2024
ab2bed4
style: Hide yocto deposit.
paouvrard Jun 5, 2024
5e8a9af
fix: Improve NEAR tx error parsing.
paouvrard Jun 13, 2024
e2e738b
feat: Add alwaysOnboardDuringSignIn option.
paouvrard Jun 20, 2024
104bec4
fix: Improve error handling during signIn.
paouvrard Jun 21, 2024
35841f8
feat: Log in with Ethereum flow.
paouvrard Jun 24, 2024
1fea570
fix: Check isConnecting state.
paouvrard Jun 24, 2024
447254b
docs: Log in with Ethereum flow.
paouvrard Jun 24, 2024
fb701d0
feat: Web3Modal is optional, default to injected wallet.
paouvrard Jun 25, 2024
9a984a1
fix: Wait for missing txStatus and receipt.
paouvrard Jun 26, 2024
bc9203a
fix: Handle alternative error message.
paouvrard Jun 26, 2024
34d0f82
fix: Tweak log in with Ethereum flow.
paouvrard Jun 26, 2024
a727916
fix: Disconnect wallet when switch to account without LAK.
paouvrard Jun 26, 2024
8c93268
fix: Use wagmi.simulateContract.
paouvrard Jun 28, 2024
35e85a1
feat: Confirm action and allow retry. (#11)
paouvrard Jul 1, 2024
a40b498
fix: Hide Web3Modal after signIn.
paouvrard Jul 1, 2024
d8e4619
chore: Rename.
paouvrard Jul 1, 2024
3348741
feat: 1wei free onboarding.
paouvrard Jul 10, 2024
d1967c1
Merge branch 'dev' into ethereum-wallets
paouvrard Jul 16, 2024
71533ff
build: Upgrade Ethereum dependencies.
paouvrard Jul 16, 2024
4574982
Merge branch 'dev' into ethereum-wallets
paouvrard Jul 25, 2024
2221a62
build: Fix Angular.
paouvrard Jul 25, 2024
6a4757d
feat: Chain switch modal. (#13)
paouvrard Jul 25, 2024
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
Prev Previous commit
Next Next commit
fix: Use near_getPublicKey api.
  • Loading branch information
paouvrard committed May 8, 2024
commit 0c468becb8e725e106e827a375babedf3a66937d
90 changes: 62 additions & 28 deletions packages/ethereum-wallets/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getAccount,
switchChain,
writeContract,
waitForTransactionReceipt,
disconnect,
type GetAccountReturnType,
type Config,
Expand Down Expand Up @@ -122,7 +123,13 @@ const EthereumWallets: WalletBehaviourFactory<
_state.subscriptions = [];
};

const executeTransaction = async (tx: Transaction) => {
const executeTransaction = async ({
tx,
relayerPublicKey,
}: {
tx: Transaction;
relayerPublicKey: string;
}): Promise<`0x${string}`> => {
const to = (
/^0x([A-Fa-f0-9]{40})$/.test(tx.receiverId)
? tx.receiverId
Expand Down Expand Up @@ -171,6 +178,14 @@ const EthereumWallets: WalletBehaviourFactory<
tx.actions[0].params.accessKey.permission.receiverId,
tx.actions[0].params.accessKey.permission.methodNames ?? [],
],
gasPrice:
tx.actions[0].params.publicKey === relayerPublicKey &&
tx.receiverId ===
tx.actions[0].params.accessKey.permission.receiverId
? // Fix 0 gasPrice to avoid wallet errors when account has 0 NEAR balance.
// The onboarding transaction is always free.
BigInt(0)
: undefined,
chainId: expectedChainId,
type: "legacy",
});
Expand Down Expand Up @@ -345,17 +360,20 @@ const EthereumWallets: WalletBehaviourFactory<
}> => {
let relayerPublicKey: string;
try {
const response = await fetch(
// TODO: update url when available.
`https://near-wallet-playground.testnet.aurora.dev/onboard?account=${accountId}`,
{
method: "GET",
}
);
// eslint-disable-next-line @typescript-eslint/naming-convention
const { public_key } = await response.json();
const response = await fetch(nearRpc, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
id: 3,
method: "near_getPublicKey",
}),
});
const { result } = await response.json();
relayerPublicKey =
"ed25519:" + bs58.encode(Buffer.from(public_key, "hex"));
"ed25519:" + bs58.encode(Buffer.from(result.public_key, "hex"));
} catch (error) {
logger.error(error);
throw new Error("Failed to fetch the relayer's public key.");
Expand All @@ -367,10 +385,18 @@ const EthereumWallets: WalletBehaviourFactory<
account_id: accountId,
public_key: relayerPublicKey,
});
logger.log("User account ready, relayer access key onboarded.", key);
logger.log(
"User account ready, relayer access key onboarded.",
relayerPublicKey,
key
);
return { relayerPublicKey, onboardingTransaction: null };
} catch (error) {
logger.warn("Need to add the relayer access key.", error);
logger.warn(
"Need to add the relayer access key.",
relayerPublicKey,
error
);
// Add the relayer's access key on-chain.
return {
relayerPublicKey,
Expand All @@ -387,7 +413,8 @@ const EthereumWallets: WalletBehaviourFactory<
permission: {
receiverId: accountId,
allowance: "0",
methodNames: [RLP_EXECUTE],
// methodNames: [RLP_EXECUTE],
methodNames: [],
},
},
},
Expand Down Expand Up @@ -431,7 +458,7 @@ const EthereumWallets: WalletBehaviourFactory<
}
} catch (error) {
logger.error(
"Failed to executed FunctionCall access key transaction, falling back to Ethereum wallet to sign and send transaction.",
"Failed to execute FunctionCall access key transaction, falling back to Ethereum wallet to sign and send transaction.",
error
);
}
Expand Down Expand Up @@ -465,8 +492,15 @@ const EthereumWallets: WalletBehaviourFactory<
try {
for (const [index, tx] of txs.entries()) {
renderTxs({ selectedIndex: index });
const result = await executeTransaction(tx);
logger.log(result);
const txHash = await executeTransaction({ tx, relayerPublicKey });
logger.log(`Sent transaction: ${txHash}`);
/*
const receipt = await waitForTransactionReceipt(wagmiConfig, {
hash: txHash,
chainId: expectedChainId,
});
logger.log(receipt);
*/
// TODO get the FinalExecutionOutcome of the rpc transaction
results.push({} as FinalExecutionOutcome);
}
Expand Down Expand Up @@ -494,9 +528,9 @@ const EthereumWallets: WalletBehaviourFactory<
account_id: accountLogIn.accountId,
public_key: accountLogIn.publicKey,
});
// NOTE: don't await, in case of a connection problem with the wallet, the user should still be disconnected from dApp.
// NOTE: If connection problem with the wallet, the user can cancel from the modal to skip the disconnect transaction.
// If not deleted, the access key will be reused during signIn.
signAndSendTransactions([
await signAndSendTransactions([
{
signerId: accountLogIn.accountId,
receiverId: accountLogIn.accountId,
Expand All @@ -505,19 +539,19 @@ const EthereumWallets: WalletBehaviourFactory<
type: "DeleteKey",
params: {
publicKey: accountLogIn.publicKey,
/*
publicKey:
"ed25519:3HDMUBDSSup8jPL7FMLiduSPwir6HhX4zedvZmzy25So",
*/
},
},
],
},
]).then(() => {
_state.keystore.removeKey(
options.network.networkId,
accountLogIn.accountId
);
});
// NOTE: await a promise so that event loop calls `signAndSendTransactions` before `disconnect(wagmiConfig)`.
await ((ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms)))(1);
]);
_state.keystore.removeKey(
options.network.networkId,
accountLogIn.accountId
);
}
cleanup();
} catch (error) {
Expand Down