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

fix: Handle new error message, remove devMode. #1164

Merged
merged 10 commits into from
Aug 21, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const projectId = "30147604c5f01d0bc4482ab0665b5697";
// TODO: Replace with the NEAR chain after the protocol upgrade.
const near: Chain = {
id: 398,
name: "NEAR wallet playground",
name: "NEAR Protocol Testnet",
nativeCurrency: {
decimals: 18,
name: "NEAR",
Expand Down Expand Up @@ -156,7 +156,7 @@ export class WalletSelectorComponent implements OnInit {
setupNearMobileWallet(),
setupMintbaseWallet({ contractId: CONTRACT_ID }),
setupBitteWallet({ contractId: CONTRACT_ID }),
setupEthereumWallets({ wagmiConfig, web3Modal, devMode: true }),
setupEthereumWallets({ wagmiConfig, web3Modal }),
],
});

Expand Down
4 changes: 2 additions & 2 deletions examples/react/contexts/WalletSelectorContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const projectId = "30147604c5f01d0bc4482ab0665b5697";
// TODO: Replace with the NEAR chain after the protocol upgrade.
const near: Chain = {
id: 398,
name: "NEAR wallet playground",
name: "NEAR Protocol Testnet",
nativeCurrency: {
decimals: 18,
name: "NEAR",
Expand Down Expand Up @@ -188,7 +188,7 @@ export const WalletSelectorContextProvider: React.FC<{
setupNearMobileWallet(),
setupMintbaseWallet({ contractId: CONTRACT_ID }),
setupBitteWallet({ contractId: CONTRACT_ID }),
setupEthereumWallets({ wagmiConfig, web3Modal, devMode: true }),
setupEthereumWallets({ wagmiConfig, web3Modal }),
],
});
const _modal = setupModal(_selector, {
Expand Down
3 changes: 2 additions & 1 deletion packages/ethereum-wallets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ NEP-518 doesn't support multiple actions within the same transaction, so when mu

NEP-518 rpc relayer uses a FunctionCall access key to execute transactions on behalf of the user by calling `rlp_execute`. If this key is not yet added, the wallet will be onboarded before the first transaction is made.

`signMessage` and `verifyOwner` are not implemented because Ethereum wallets are not compatible with these standards, instead a dApp can use `eth_sign` or `eth_signTypedData_v4` to authenticate the wallet by interacting with it directly.
`signMessage` and `verifyOwner` are not implemented because Ethereum wallets are not compatible with these standards, instead a dApp can use `personal_sign` or `eth_signTypedData_v4` to authenticate the wallet by interacting with it directly.

## Installation and Usage

Expand Down Expand Up @@ -79,6 +79,7 @@ Project ID is required, please obtain it from [walletconnect.com](https://wallet
- `iconUrl` (`string?`): Image URL for the icon shown in the modal. This can also be a relative path or base64 encoded image. Defaults to `./assets/ethereum-wallets-icon.png`.
- `wagmiCore` (`typeof import("@wagmi/core")?`): Optional, @wagmi/core functions can be overidden by the dapp to interract with the wallet.
- `alwaysOnboardDuringSignIn` (`boolean?`): A dapp without SignIn access key will not onboard the relayer by default, this option does the relayer onboarding during login.
- `nearNodeUrl` (`string?`): NEAR node url to query the NEAR transaction status and onboarding access key.

Developent options (before the NEAR protocol upgrade to support 0x accounts natively):

Expand Down
2 changes: 1 addition & 1 deletion packages/ethereum-wallets/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@near-wallet-selector/ethereum-wallets",
"version": "1.0.0",
"version": "8.9.12",
"description": "Ethereum wallets package for NEAR Wallet Selector.",
"keywords": [
"near",
Expand Down
50 changes: 38 additions & 12 deletions packages/ethereum-wallets/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import type {
FunctionCallPermissionView,
} from "near-api-js/lib/providers/provider";
import { JsonRpcProvider } from "near-api-js/lib/providers";
import { stringifyJsonOrBytes } from "near-api-js/lib/transaction";
import {
// encodeTransaction,
paouvrard marked this conversation as resolved.
Show resolved Hide resolved
stringifyJsonOrBytes,
} from "near-api-js/lib/transaction";
import { parseRpcError } from "near-api-js/lib/utils/rpc_errors";
import {
type WalletModuleFactory,
Expand Down Expand Up @@ -64,6 +67,7 @@ export interface EthereumWalletsParams {
devMode?: boolean;
devModeAccount?: string;
deprecated?: boolean;
nearNodeUrl?: string;
}

interface EthereumWalletsState {
Expand Down Expand Up @@ -103,6 +107,7 @@ const EthereumWallets: WalletBehaviourFactory<
alwaysOnboardDuringSignIn = false,
devMode,
devModeAccount = "eth-wallet.testnet",
nearNodeUrl,
},
}) => {
if (!wagmiCore) {
Expand All @@ -123,6 +128,17 @@ const EthereumWallets: WalletBehaviourFactory<
if (!nearExplorer) {
throw new Error("Failed to parse NEAR explorer url from wagmiConfig.");
}
// NOTE: use a custom provider because the failover provider doesn't give error details.
const nearProvider = new JsonRpcProvider(
nearNodeUrl ??
// @ts-expect-error
provider.provider.connection ??
// @ts-expect-error
provider.provider.providers[
// @ts-expect-error
provider.provider.currentProviderIndex
].connection
);

const getAccounts = async (): Promise<Array<Account>> => {
const address = wagmiCore!.getAccount(wagmiConfig).address?.toLowerCase();
Expand Down Expand Up @@ -422,7 +438,7 @@ const EthereumWallets: WalletBehaviourFactory<
throw new Error("Failed to fetch the relayer's public key.");
}
try {
const key = await provider.query<AccessKeyViewRaw>({
const key = await nearProvider.query<AccessKeyViewRaw>({
request_type: "view_access_key",
finality: "final",
account_id: accountId,
Expand All @@ -439,6 +455,8 @@ const EthereumWallets: WalletBehaviourFactory<
logger.error(error);
if (
!error.message?.includes("does not exist while viewing") &&
!error.message?.includes("doesn't exist") &&
!error.message?.includes("does not exist") &&
!error.message?.includes("has never been observed on the node")
) {
throw new Error(
Expand Down Expand Up @@ -505,7 +523,7 @@ const EthereumWallets: WalletBehaviourFactory<
if (accountLogIn.publicKey && nearTxs.length) {
let accessKeyUsable;
try {
const accessKey = await provider.query<AccessKeyViewRaw>({
const accessKey = await nearProvider.query<AccessKeyViewRaw>({
request_type: "view_access_key",
finality: "final",
account_id: accountLogIn.accountId,
Expand All @@ -528,7 +546,19 @@ const EthereumWallets: WalletBehaviourFactory<
);
const results: Array<FinalExecutionOutcome> = [];
for (let i = 0; i < signedTransactions.length; i += 1) {
const nearTx = await provider.sendTransaction(signedTransactions[i]);
const nearTx = await nearProvider.sendTransaction(
signedTransactions[i]
);
/*
const bytes = encodeTransaction(signedTransactions[i]);
paouvrard marked this conversation as resolved.
Show resolved Hide resolved
const nearTx: FinalExecutionOutcome = await nearProvider.sendJsonRpc(
"send_tx",
{
signed_tx_base64: Buffer.from(bytes).toString("base64"),
wait_until: "EXECUTED_OPTIMISTIC",
}
);
*/
logger.log("NEAR transaction:", nearTx);
if (
typeof nearTx.status === "object" &&
Expand Down Expand Up @@ -651,10 +681,6 @@ const EthereumWallets: WalletBehaviourFactory<
}
}
logger.log("Receipt:", receipt);
const nearProvider = new JsonRpcProvider(
// @ts-expect-error
provider.provider.connection
);
let nearTx;
while (!nearTx) {
try {
Expand Down Expand Up @@ -711,7 +737,7 @@ const EthereumWallets: WalletBehaviourFactory<
if (accountLogIn.publicKey) {
try {
// Check that the key exists before making a transaction.
await provider.query<AccessKeyViewRaw>({
await nearProvider.query<AccessKeyViewRaw>({
request_type: "view_access_key",
finality: "final",
account_id: accountLogIn.accountId,
Expand Down Expand Up @@ -842,7 +868,7 @@ const EthereumWallets: WalletBehaviourFactory<
let reUseKeyPair = false;
if (keyPair) {
try {
await provider.query<AccessKeyViewRaw>({
await nearProvider.query<AccessKeyViewRaw>({
request_type: "view_access_key",
finality: "final",
account_id: accountId,
Expand Down Expand Up @@ -936,14 +962,14 @@ const EthereumWallets: WalletBehaviourFactory<
async verifyOwner({ message }) {
logger.log("EthereumWallets:verifyOwner", { message });
throw new Error(
"Not implemented: ed25519 N/A, use eth_sign or eth_signTypedData_v4 instead."
"Not implemented: ed25519 N/A, '\x19Ethereum Signed Message:\n' prefix is not compatible, use personal_sign or eth_signTypedData_v4 instead."
);
},

async signMessage({ message, nonce, recipient }) {
logger.log("EthereumWallets:signMessage", { message, nonce, recipient });
throw new Error(
"Not implemented: ed25519 N/A, use eth_sign or eth_signTypedData_v4 instead."
"Not implemented: ed25519 N/A, '\x19Ethereum Signed Message:\n' prefix is not compatible, use personal_sign or eth_signTypedData_v4 instead."
);
},

Expand Down
Loading