-
Notifications
You must be signed in to change notification settings - Fork 5.4k
feat: Add account upgrade and check status RPC methods #36452
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
Draft
MoMannn
wants to merge
23
commits into
main
Choose a base branch
from
feat/account-upgrade-rpc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+336
−60
Draft
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
d1d3ebc
add account upgrade and check status RPC methods
MoMannn 0511ad1
Merge branch 'main' of https://github.com/MetaMask/metamask-extension…
MoMannn b173723
cast chainId to hex
MoMannn 3f7117f
optimize tests
MoMannn 7c7c089
Merge branch 'main' of https://github.com/MetaMask/metamask-extension…
MoMannn 0890df6
get config via isAtomicBatchSupported
MoMannn e8ccc03
Merge branch 'main' into feat/account-upgrade-rpc
MoMannn 21dc9b4
use txController
MoMannn 244a65d
Merge branch 'feat/account-upgrade-rpc' of https://github.com/MetaMas…
MoMannn 6f9f1a0
Fix supported chain check
MoMannn 6a001c7
cursor configuration check fix
MoMannn 3a2437c
Merge branch 'main' of https://github.com/MetaMask/metamask-extension…
MoMannn 9e936a5
wait for transaction hash
MoMannn ab1fdbf
Merge branch 'main' into feat/account-upgrade-rpc
MoMannn 9e4b674
test fix
MoMannn 2703ec4
Merge branch 'feat/account-upgrade-rpc' of https://github.com/MetaMas…
MoMannn 32ca828
Merge branch 'main' into feat/account-upgrade-rpc
MoMannn 928c2c7
update import path
MoMannn b52fe35
Merge branch 'feat/account-upgrade-rpc' of https://github.com/MetaMas…
MoMannn 4688eff
add null check to getCurrentChainId
MoMannn 60717f2
use getCurrentChainIdForDomain
MoMannn f7711ca
Move rpc methods to external core package
MoMannn 5ab0a9e
Merge branch 'main' into feat/account-upgrade-rpc
MoMannn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,135 @@ | ||
import { | ||
TransactionEnvelopeType, | ||
TransactionType, | ||
TransactionParams, | ||
} from '@metamask/transaction-controller'; | ||
import { Hex } from '@metamask/utils'; | ||
|
||
export const EIP_7702_REVOKE_ADDRESS = | ||
'0x0000000000000000000000000000000000000000'; | ||
|
||
export type EIP7702TransactionParams = { | ||
address: Hex; | ||
upgradeContractAddress?: Hex; | ||
networkClientId: string; | ||
}; | ||
|
||
export type EIP7702TransactionResult = { | ||
transactionHash: string; | ||
delegatedTo?: string; | ||
transactionId?: string; | ||
}; | ||
|
||
/** | ||
* Creates an EIP-7702 upgrade transaction. | ||
* | ||
* @param params - The transaction parameters | ||
* @param addTransactionAndWaitForPublish - Function to add transaction and wait for publish | ||
* @returns Promise with transaction result | ||
*/ | ||
export async function createEIP7702UpgradeTransaction( | ||
params: EIP7702TransactionParams, | ||
addTransactionAndWaitForPublish: ( | ||
transactionParams: TransactionParams, | ||
options: { | ||
networkClientId: string; | ||
requireApproval?: boolean; | ||
type?: TransactionType; | ||
}, | ||
) => Promise<Record<string, unknown>>, | ||
): Promise<EIP7702TransactionResult> { | ||
const { address, upgradeContractAddress, networkClientId } = params; | ||
|
||
if (!upgradeContractAddress) { | ||
throw new Error('Upgrade contract address is required'); | ||
} | ||
|
||
const transactionParams: TransactionParams = { | ||
authorizationList: [ | ||
{ | ||
address: upgradeContractAddress, | ||
}, | ||
], | ||
from: address, | ||
to: address, | ||
type: TransactionEnvelopeType.setCode, | ||
}; | ||
|
||
const transactionMeta = await addTransactionAndWaitForPublish( | ||
transactionParams, | ||
{ | ||
networkClientId, | ||
requireApproval: true, | ||
type: TransactionType.batch, | ||
}, | ||
); | ||
|
||
return { | ||
transactionHash: transactionMeta.hash as string, | ||
delegatedTo: upgradeContractAddress, | ||
transactionId: transactionMeta.id as string, | ||
MoMannn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
} | ||
|
||
/** | ||
* Creates an EIP-7702 downgrade transaction. | ||
* | ||
* @param params - The transaction parameters | ||
* @param addTransactionAndWaitForPublish - Function to add transaction and wait for publish | ||
* @returns Promise with transaction result | ||
*/ | ||
export async function createEIP7702DowngradeTransaction( | ||
params: EIP7702TransactionParams, | ||
addTransactionAndWaitForPublish: ( | ||
transactionParams: TransactionParams, | ||
options: { | ||
networkClientId: string; | ||
requireApproval?: boolean; | ||
type?: TransactionType; | ||
}, | ||
) => Promise<Record<string, unknown>>, | ||
): Promise<EIP7702TransactionResult> { | ||
const { address, networkClientId } = params; | ||
|
||
const transactionParams: TransactionParams = { | ||
authorizationList: [ | ||
{ | ||
address: EIP_7702_REVOKE_ADDRESS, | ||
}, | ||
], | ||
from: address, | ||
to: address, | ||
type: TransactionEnvelopeType.setCode, | ||
}; | ||
|
||
const transactionMeta = await addTransactionAndWaitForPublish( | ||
transactionParams, | ||
{ | ||
networkClientId, | ||
requireApproval: true, | ||
type: TransactionType.revokeDelegation, | ||
}, | ||
); | ||
|
||
return { | ||
transactionHash: transactionMeta.hash as string, | ||
transactionId: transactionMeta.id as string, | ||
}; | ||
} | ||
|
||
/** | ||
* Checks if an account is upgraded by checking if it has code. | ||
* | ||
* @param address - The account address to check | ||
* @param networkClientId - The network client ID | ||
* @param getCode - Function to get account code | ||
* @returns Promise with boolean indicating if account is upgraded | ||
*/ | ||
export async function isAccountUpgraded( | ||
address: Hex, | ||
networkClientId: string, | ||
getCode: (address: Hex, networkClientId: string) => Promise<string | null>, | ||
): Promise<boolean> { | ||
const code = await getCode(address, networkClientId); | ||
MoMannn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Boolean(code && code.length > 2); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It might be worth decomposing these hooks out to avoid bloating metamask-controller.js
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.
I don't know this is the pattern used for all other implementations I would go with the same way.