|
| 1 | +import { v1, commons as v2commons } from '@0xsequence/v2core' |
| 2 | +import { WalletV1 } from '@0xsequence/v2wallet' |
| 3 | +import { Config as V3Config, Context as V3Context, Extensions as V3Extensions } from '@0xsequence/wallet-primitives' |
| 4 | +import { AbiFunction, Address, Hex } from 'ox' |
| 5 | +import { SignedMigration, UnsignedMigration, VersionedContext } from '../../migrator.js' |
| 6 | +import { Migration } from '../index.js' |
| 7 | +import { createDefaultV3Topology } from '../v3/config.js' |
| 8 | + |
| 9 | +// uint160(keccak256("org.sequence.sdk.migration.v1v3.space.nonce")) |
| 10 | +export const MIGRATION_V1_V3_NONCE_SPACE = '0x9e4d5bdafd978baf1290aff23057245a2a62bef5' |
| 11 | + |
| 12 | +export type ConvertOptions = { |
| 13 | + loginSigner: { |
| 14 | + address: Address.Address |
| 15 | + imageHash?: Hex.Hex |
| 16 | + } |
| 17 | + extensions?: V3Extensions.Extensions |
| 18 | +} |
| 19 | + |
| 20 | +export class Migration_v1v3 implements Migration<v1.config.WalletConfig, V3Config.Config, ConvertOptions> { |
| 21 | + fromVersion = 1 |
| 22 | + toVersion = 3 |
| 23 | + |
| 24 | + async convertConfig(v1Config: v1.config.WalletConfig, options: ConvertOptions): Promise<V3Config.Config> { |
| 25 | + const signerLeaves: V3Config.SignerLeaf[] = v1Config.signers.map((signer) => ({ |
| 26 | + type: 'signer', |
| 27 | + address: Address.from(signer.address), |
| 28 | + weight: BigInt(signer.weight), |
| 29 | + })) |
| 30 | + const v1NestedTopology = V3Config.flatLeavesToTopology(signerLeaves) |
| 31 | + const v3Config: V3Config.Config = { |
| 32 | + threshold: 1n, |
| 33 | + checkpoint: 0n, |
| 34 | + topology: [ |
| 35 | + { |
| 36 | + type: 'nested', |
| 37 | + weight: 1n, |
| 38 | + threshold: BigInt(v1Config.threshold), |
| 39 | + tree: v1NestedTopology, |
| 40 | + }, |
| 41 | + { |
| 42 | + type: 'nested', |
| 43 | + weight: 1n, |
| 44 | + threshold: 2n, |
| 45 | + tree: createDefaultV3Topology(options.loginSigner, options.extensions), |
| 46 | + }, |
| 47 | + ], |
| 48 | + } |
| 49 | + return v3Config |
| 50 | + } |
| 51 | + |
| 52 | + async prepareMigration( |
| 53 | + walletAddress: Address.Address, |
| 54 | + contexts: VersionedContext, |
| 55 | + toConfig: V3Config.Config, |
| 56 | + ): Promise<UnsignedMigration> { |
| 57 | + const v3Context = contexts[3] || V3Context.Rc3 |
| 58 | + if (!V3Context.isContext(v3Context)) { |
| 59 | + throw new Error('Invalid context') |
| 60 | + } |
| 61 | + |
| 62 | + const nonce = v2commons.transaction.encodeNonce(MIGRATION_V1_V3_NONCE_SPACE, 0) |
| 63 | + |
| 64 | + // Update implementation to v3 |
| 65 | + const updateImplementationAbi = AbiFunction.from('function updateImplementation(address implementation)') |
| 66 | + const updateImplementationTx = { |
| 67 | + to: walletAddress, |
| 68 | + data: AbiFunction.encodeData(updateImplementationAbi, [v3Context.stage2]), |
| 69 | + } |
| 70 | + // Update configuration to v3 |
| 71 | + const v3ImageHash = Hex.fromBytes(V3Config.hashConfiguration(toConfig)) |
| 72 | + const updateImageHashAbi = AbiFunction.from('function updateImageHash(bytes32 imageHash)') |
| 73 | + const updateImageHashTx = { |
| 74 | + to: walletAddress, |
| 75 | + data: AbiFunction.encodeData(updateImageHashAbi, [v3ImageHash]), |
| 76 | + } |
| 77 | + |
| 78 | + return { |
| 79 | + transactions: [updateImplementationTx, updateImageHashTx], |
| 80 | + fromVersion: this.fromVersion, |
| 81 | + toVersion: this.toVersion, |
| 82 | + nonce, |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Signs a migration with a wallet |
| 88 | + * @notice V1 Wallets must call this method for each chain they are migrating on |
| 89 | + * @param migration The unsigned migration to sign |
| 90 | + * @param wallet The wallet to sign the migration with |
| 91 | + * @returns The signed migration |
| 92 | + */ |
| 93 | + //FIXME Remove this function. Signing is not a responsibility of the migration class. |
| 94 | + async signMigration(migration: UnsignedMigration, wallet: WalletV1): Promise<SignedMigration> { |
| 95 | + const { address } = await this.decodeTransactions(migration.transactions) |
| 96 | + if (address !== wallet.address) { |
| 97 | + throw new Error('Wallet address does not match migration address') |
| 98 | + } |
| 99 | + const txBundle: v2commons.transaction.TransactionBundle = { |
| 100 | + entrypoint: wallet.address, |
| 101 | + transactions: migration.transactions.map((tx) => ({ |
| 102 | + to: tx.to, |
| 103 | + data: tx.data, |
| 104 | + gasLimit: 0n, |
| 105 | + revertOnError: true, |
| 106 | + })), |
| 107 | + nonce: migration.nonce, |
| 108 | + } |
| 109 | + const { signature } = await wallet.signTransactionBundle(txBundle) |
| 110 | + Hex.assert(signature) |
| 111 | + return { ...migration, signature } |
| 112 | + } |
| 113 | + |
| 114 | + async decodeTransactions(transactions: UnsignedMigration['transactions']): Promise<{ |
| 115 | + address: Address.Address |
| 116 | + toImageHash: Hex.Hex |
| 117 | + }> { |
| 118 | + if (transactions.length !== 2) { |
| 119 | + throw new Error('Invalid transactions') |
| 120 | + } |
| 121 | + const tx1 = transactions[0]! |
| 122 | + const tx2 = transactions[1]! |
| 123 | + if (tx1.to !== tx2.to) { |
| 124 | + throw new Error('Invalid to address') |
| 125 | + } |
| 126 | + const updateImplementationAbi = AbiFunction.from('function updateImplementation(address implementation)') |
| 127 | + AbiFunction.decodeData(updateImplementationAbi, tx1.data) // Check decoding works for update implementation |
| 128 | + const updateImageHashAbi = AbiFunction.from('function updateImageHash(bytes32 imageHash)') |
| 129 | + const updateImageHashArgs = AbiFunction.decodeData(updateImageHashAbi, tx2.data) |
| 130 | + return { |
| 131 | + address: tx1.to, |
| 132 | + toImageHash: updateImageHashArgs[0], |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments