Skip to content

Commit

Permalink
devop: merge latest
Browse files Browse the repository at this point in the history
  • Loading branch information
kvhnuke committed May 1, 2024
2 parents 148eaae + 0bfd4ce commit 81e324d
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 89 deletions.
111 changes: 22 additions & 89 deletions packages/extension/src/providers/ethereum/methods/eth_accounts.ts
Original file line number Diff line number Diff line change
@@ -1,110 +1,43 @@
import { CallbackFunction, MiddlewareFunction } from "@enkryptcom/types";
import { MiddlewareFunction } from "@enkryptcom/types";
import type EthereumProvider from "..";
import { ProviderRPCRequest } from "@/types/provider";
import { WindowPromise } from "@/libs/window-promise";
import AccountState from "../libs/accounts-state";
import { getCustomError } from "@/libs/error";
import openOnboard from "@/libs/utils/open-onboard";
let isAccountAccessPending = false;
import { throttle } from "lodash";

const throttledOpenOnboard = throttle(() => openOnboard(), 10000);
const existingErrors: Record<string, { time: number; error: any }> = {};
const pendingPromises: {
payload: ProviderRPCRequest;
res: CallbackFunction;
}[] = [];
const method: MiddlewareFunction = async function (
this: EthereumProvider,
payload: ProviderRPCRequest,
res,
next
): Promise<void> {
if (
payload.method !== "eth_accounts" &&
payload.method !== "eth_requestAccounts" &&
payload.method !== "eth_coinbase"
)
if (payload.method !== "eth_accounts" && payload.method !== "eth_coinbase")
return next();
else {
if (isAccountAccessPending) {
pendingPromises.push({
payload,
res,
});
return;
}
isAccountAccessPending = true;
const isInitialized = await this.KeyRing.isInitialized();

const handleRemainingPromises = () => {
isAccountAccessPending = false;
if (pendingPromises.length) {
const promi = pendingPromises.pop();
if (promi) handleAccountAccess(promi.payload, promi.res);
if (payload.options && payload.options.domain) {
if (!isInitialized) {
res(null, payload.method === "eth_coinbase" ? "" : []);
return throttledOpenOnboard();
}
};
const handleAccountAccess = (
_payload: ProviderRPCRequest,
_res: CallbackFunction
) => {
if (_payload.options && _payload.options.domain) {
isAccountAccessPending = true;
if (!isInitialized) {
_res(getCustomError("Enkrypt not initialized"));
throttledOpenOnboard();
return handleRemainingPromises();
}
const accountsState = new AccountState();
if (
existingErrors[_payload.options.domain] &&
existingErrors[_payload.options.domain].time >
new Date().getTime() - 2000
) {
_res(existingErrors[_payload.options.domain].error as any);
return handleRemainingPromises();
}
accountsState
.getApprovedAddresses(_payload.options.domain)
.then((accounts) => {
if (accounts.length) {
_res(
null,
payload.method === "eth_coinbase" ? accounts[0] : accounts
);
handleRemainingPromises();
} else {
const windowPromise = new WindowPromise();
windowPromise
.getResponse(
this.getUIPath(this.UIRoutes.ethConnectDApp.path),
JSON.stringify({
..._payload,
params: [this.network.name],
})
)
.then(({ error, result }) => {
if (error) {
existingErrors[_payload.options!.domain] = {
time: new Date().getTime(),
error,
};
return _res(error as any);
}
const accounts = JSON.parse(result || "[]");
_res(
null,
payload.method === "eth_coinbase" ? accounts[0] : accounts
);
})
.finally(handleRemainingPromises);
}
});
} else {
_res(getCustomError("No domain set!"));
}
};
handleAccountAccess(payload, res);
const accountsState = new AccountState();
accountsState
.getApprovedAddresses(payload.options.domain)
.then((accounts) => {
if (accounts.length) {
res(
null,
payload.method === "eth_coinbase" ? accounts[0] : accounts
);
} else {
res(null, payload.method === "eth_coinbase" ? "" : []);
}
});
} else {
res(getCustomError("No domain set!"));
}
}
};
export default method;
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { CallbackFunction, MiddlewareFunction } from "@enkryptcom/types";
import type EthereumProvider from "..";
import { ProviderRPCRequest } from "@/types/provider";
import { WindowPromise } from "@/libs/window-promise";
import AccountState from "../libs/accounts-state";
import { getCustomError } from "@/libs/error";
import openOnboard from "@/libs/utils/open-onboard";
let isAccountAccessPending = false;
import { throttle } from "lodash";

const throttledOpenOnboard = throttle(() => openOnboard(), 10000);
const existingErrors: Record<string, { time: number; error: any }> = {};
const pendingPromises: {
payload: ProviderRPCRequest;
res: CallbackFunction;
}[] = [];
const method: MiddlewareFunction = async function (
this: EthereumProvider,
payload: ProviderRPCRequest,
res,
next
): Promise<void> {
if (payload.method !== "eth_requestAccounts") return next();
else {
if (isAccountAccessPending) {
pendingPromises.push({
payload,
res,
});
return;
}
isAccountAccessPending = true;
const isInitialized = await this.KeyRing.isInitialized();

const handleRemainingPromises = () => {
isAccountAccessPending = false;
if (pendingPromises.length) {
const promi = pendingPromises.pop();
if (promi) handleAccountAccess(promi.payload, promi.res);
}
};
const handleAccountAccess = (
_payload: ProviderRPCRequest,
_res: CallbackFunction
) => {
if (_payload.options && _payload.options.domain) {
isAccountAccessPending = true;
if (!isInitialized) {
_res(getCustomError("Enkrypt not initialized"));
throttledOpenOnboard();
return handleRemainingPromises();
}
const accountsState = new AccountState();
if (
existingErrors[_payload.options.domain] &&
existingErrors[_payload.options.domain].time >
new Date().getTime() - 2000
) {
_res(existingErrors[_payload.options.domain].error as any);
return handleRemainingPromises();
}
accountsState
.getApprovedAddresses(_payload.options.domain)
.then((accounts) => {
if (accounts.length) {
_res(null, accounts);
handleRemainingPromises();
} else {
const windowPromise = new WindowPromise();
windowPromise
.getResponse(
this.getUIPath(this.UIRoutes.ethConnectDApp.path),
JSON.stringify({
..._payload,
params: [this.network.name],
})
)
.then(({ error, result }) => {
if (error) {
existingErrors[_payload.options!.domain] = {
time: new Date().getTime(),
error,
};
return _res(error as any);
}
const accounts = JSON.parse(result || "[]");
_res(null, accounts);
})
.finally(handleRemainingPromises);
}
});
} else {
_res(getCustomError("No domain set!"));
}
};
handleAccountAccess(payload, res);
}
};
export default method;
2 changes: 2 additions & 0 deletions packages/extension/src/providers/ethereum/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import watchAsset from "./wallet_watchAsset";
import walletRequestPermissions from "./wallet_requestPermissions";
import ethSendRawTransaction from "./eth_sendRawTransaction";
import enkryptGetPublickKey from "./enkrypt_getPublicKey";
import ethRequestAccounts from "./eth_requestAccounts";
export default [
ethSendTransaction,
ethSign,
Expand All @@ -29,4 +30,5 @@ export default [
walletRequestPermissions,
ethSendRawTransaction,
enkryptGetPublickKey,
ethRequestAccounts,
];

1 comment on commit 81e324d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.