-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
123 additions
and
89 deletions.
There are no files selected for viewing
111 changes: 22 additions & 89 deletions
111
packages/extension/src/providers/ethereum/methods/eth_accounts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
99 changes: 99 additions & 0 deletions
99
packages/extension/src/providers/ethereum/methods/eth_requestAccounts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81e324d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Virus total analysis
chrome:
https://www.virustotal.com/gui/file/cde4d178c9bb2e4e36a6283200e22fed668b99eb2ce82c009713721c48e11f49
firefox:
https://www.virustotal.com/gui/file/84d3d2d06a48a1c1779020755689a8a11d14c3a94d86c12b9bb4462eac83f4a7