-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: separately handle extension connectWith
- Loading branch information
1 parent
9e79977
commit 3476c77
Showing
3 changed files
with
132 additions
and
58 deletions.
There are no files selected for viewing
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
66 changes: 66 additions & 0 deletions
66
packages/sdk/src/provider/extensionConnectWithOverwrite.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,66 @@ | ||
import { rpcWithAccountParam, RPC_METHODS } from '../config'; | ||
import { MetaMaskSDK } from '../sdk'; | ||
import { logger } from '../utils/logger'; | ||
|
||
export const extensionConnectWithOverwrite = async ({ | ||
method, | ||
sdk, | ||
params, | ||
}: { | ||
method: string; | ||
sdk: MetaMaskSDK; | ||
params: any; | ||
}) => { | ||
if (!sdk.isExtensionActive()) { | ||
throw new Error(`SDK state invalid -- extension is not active`); | ||
} | ||
|
||
logger( | ||
`[MetaMaskProvider: extensionConnectWithOverwrite()] Overwriting request method`, | ||
method, | ||
params, | ||
); | ||
|
||
const accounts = (await sdk.getProvider()?.request({ | ||
method: RPC_METHODS.ETH_REQUESTACCOUNTS, | ||
params: [], | ||
})) as string[]; | ||
if (!accounts.length) { | ||
throw new Error(`SDK state invalid -- undefined accounts`); | ||
} | ||
|
||
if (method?.toLowerCase() === RPC_METHODS.PERSONAL_SIGN.toLowerCase()) { | ||
const connectedRpc = { | ||
method, | ||
params: [params[0], accounts[0]], | ||
}; | ||
return await sdk.getProvider()?.request(connectedRpc); | ||
} else if ( | ||
method?.toLowerCase() === RPC_METHODS.ETH_SENDTRANSACTION.toLowerCase() | ||
) { | ||
const connectedRpc = { | ||
method, | ||
params: [ | ||
{ | ||
...params[0], | ||
from: accounts[0], | ||
}, | ||
], | ||
}; | ||
return await sdk.getProvider()?.request(connectedRpc); | ||
} | ||
|
||
// TODO: implement overwrite for each remaining signedTyped methods | ||
if (rpcWithAccountParam.includes(method.toLowerCase())) { | ||
console.warn( | ||
`MetaMaskSDK connectWith method=${method} -- not handled by the extension -- call separately`, | ||
); | ||
return accounts; | ||
} | ||
|
||
// Re-create the query on the active provider | ||
return await sdk.getProvider()?.request({ | ||
method, | ||
params, | ||
}); | ||
}; |
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