-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
typescript conversion of teh file and type definition in handler
- Loading branch information
1 parent
47ae366
commit f1e8bd0
Showing
3 changed files
with
113 additions
and
48 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
app/scripts/lib/rpc-method-middleware/handlers/handlers-helpers.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,27 @@ | ||
export type HandlerWrapperType = { | ||
methodNames: [string]; | ||
hookNames: Record<string, boolean>; | ||
}; | ||
|
||
/** | ||
* @typedef {object} ProviderStateHandlerResult | ||
* @property {string} chainId - The current chain ID. | ||
* @property {boolean} isUnlocked - Whether the extension is unlocked or not. | ||
* @property {string} networkVersion - The current network ID. | ||
* @property {string[]} accounts - List of permitted accounts for the specified origin. | ||
*/ | ||
export type ProviderStateHandlerResult = { | ||
chainId: string; | ||
isUnlocked: boolean; | ||
networkVersion: string; | ||
accounts: string[]; | ||
}; | ||
|
||
export type getProviderStateType = ( | ||
origin: string, | ||
) => Promise<ProviderStateHandlerResult>; | ||
|
||
export type getAccountsType = () => Promise<string[]>; | ||
|
||
export type getWeb3ShimUsageStateType = (origin: string) => undefined | 1 | 2; | ||
export type setWeb3ShimUsageRecordedType = (origin: string) => void; |
48 changes: 0 additions & 48 deletions
48
app/scripts/lib/rpc-method-middleware/handlers/log-web3-shim-usage.js
This file was deleted.
Oops, something went wrong.
86 changes: 86 additions & 0 deletions
86
app/scripts/lib/rpc-method-middleware/handlers/log-web3-shim-usage.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,86 @@ | ||
import type { | ||
JsonRpcEngineNextCallback, | ||
JsonRpcEngineEndCallback, | ||
} from '@metamask/json-rpc-engine'; | ||
import type { | ||
JsonRpcRequest, | ||
JsonRpcResponse, | ||
JsonRpcParams, | ||
Json, | ||
} from '@metamask/utils'; | ||
import { MESSAGE_TYPE } from '../../../../../shared/constants/app'; | ||
import { | ||
HandlerWrapperType, | ||
getWeb3ShimUsageStateType, | ||
setWeb3ShimUsageRecordedType, | ||
} from './handlers-helpers'; | ||
|
||
type logWeb3ShimUsageOptionsType = { | ||
getWeb3ShimUsageState: getWeb3ShimUsageStateType; | ||
setWeb3ShimUsageRecorded: setWeb3ShimUsageRecordedType; | ||
}; | ||
|
||
type logWeb3ShimUsageType< | ||
Params extends JsonRpcParams = JsonRpcParams, | ||
Result extends Json = Json, | ||
> = { | ||
implementation: ( | ||
req: JsonRpcRequest<Params>, | ||
res: JsonRpcResponse<Result>, | ||
_next: JsonRpcEngineNextCallback, | ||
end: JsonRpcEngineEndCallback, | ||
{ | ||
getWeb3ShimUsageState, | ||
setWeb3ShimUsageRecorded, | ||
}: logWeb3ShimUsageOptionsType, | ||
) => void; | ||
} & HandlerWrapperType; | ||
|
||
/** | ||
* This RPC method is called by the inpage provider whenever it detects the | ||
* accessing of a non-existent property on our window.web3 shim. We use this | ||
* to alert the user that they are using a legacy dapp, and will have to take | ||
* further steps to be able to use it. | ||
*/ | ||
const logWeb3ShimUsage: logWeb3ShimUsageType = { | ||
methodNames: [MESSAGE_TYPE.LOG_WEB3_SHIM_USAGE], | ||
implementation: logWeb3ShimUsageHandler, | ||
hookNames: { | ||
getWeb3ShimUsageState: true, | ||
setWeb3ShimUsageRecorded: true, | ||
}, | ||
}; | ||
export default logWeb3ShimUsage; | ||
|
||
/** | ||
* @param req - The JSON-RPC request object. | ||
* @param res - The JSON-RPC response object. | ||
* @param _next - The json-rpc-engine 'next' callback. | ||
* @param end - The json-rpc-engine 'end' callback. | ||
* @param options | ||
* @param options.getWeb3ShimUsageState - A function that gets web3 shim | ||
* usage state for the given origin. | ||
* @param options.setWeb3ShimUsageRecorded - A function that records web3 shim | ||
* usage for a particular origin. | ||
*/ | ||
function logWeb3ShimUsageHandler< | ||
Params extends JsonRpcParams = JsonRpcParams, | ||
Result extends Json = Json, | ||
>( | ||
req: JsonRpcRequest<Params>, | ||
res: JsonRpcResponse<Result>, | ||
_next: JsonRpcEngineNextCallback, | ||
end: JsonRpcEngineEndCallback, | ||
{ | ||
getWeb3ShimUsageState, | ||
setWeb3ShimUsageRecorded, | ||
}: logWeb3ShimUsageOptionsType, | ||
) { | ||
const { origin } = req; | ||
if (getWeb3ShimUsageState(origin) === undefined) { | ||
setWeb3ShimUsageRecorded(origin); | ||
} | ||
|
||
res.result = true; | ||
return end(); | ||
} |