Skip to content

Commit

Permalink
fix: get proxy bytecode from compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Haypierre committed Sep 21, 2024
1 parent 6cc8f2d commit d03baf0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 61 deletions.
2 changes: 1 addition & 1 deletion contracts/core/base/BaseOpenfortAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ abstract contract BaseOpenfortAccount is
/**
* @inheritdoc IAccount
*/
function executeTransaction(bytes32, bytes32 _suggestedSignedHash, Transaction calldata _transaction)
function executeTransaction(bytes32, bytes32, Transaction calldata _transaction)
external
payable
override
Expand Down
12 changes: 0 additions & 12 deletions script/Counter.s.sol

This file was deleted.

11 changes: 6 additions & 5 deletions tasks/createAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { createWalletClient,
getAddress,
slice,
pad,
concat
concat,
Hex,
fromBytes
} from "viem"
import { eip712WalletActions } from "viem/zksync"
import { eip712WalletActions, hashBytecode } from "viem/zksync"
import { privateKeyToAccount } from "viem/accounts"
import { task } from "hardhat/config"
import { getViemChainFromConfig, writeContract } from "./utils"
Expand Down Expand Up @@ -61,14 +63,13 @@ task("get-account", "Compute zkSync create2 address of an account")
.addParam("nonce", "Number to generate predictive address with CREATE2")
.setAction(async (args, hre) => {
const account = privateKeyToAccount(hre.network.config.accounts[0])
// const proxyArtifactName = "UpgradeableOpenfortProxy";
// const proxyArtifact = await hre.deployer.loadArtifact(proxyArtifactName);
const proxyArtifact = await hre.deployer.loadArtifact("UpgradeableOpenfortProxy");
const abiTypes = parseAbiParameters("address, bytes32");
const nonce = numberToHex(args.nonce, {size: 32})
// https://docs.zksync.io/build/developer-reference/ethereum-differences/evm-instructions#address-derivation
const accountProxy = create2Address(
args.factory,
"0x010000a54c9de7cae403cb015684edcf24a518b73a06ef7ac6ea5da27134f3e0", //hashBytecode(proxyArtifact.bytecode) from viem/zksync/utils
fromBytes(hashBytecode(proxyArtifact.bytecode as Hex), "hex"),
keccak256(encodeAbiParameters(abiTypes, [account.address, nonce])),
encodeAbiParameters(parseAbiParameters("address, bytes"), [args.implementation, "0x"])
)
Expand Down
68 changes: 26 additions & 42 deletions tasks/deployFactory.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,32 @@
import { utils, Wallet } from "zksync-ethers";
import { utils, Wallet } from "zksync-ethers"
import { task } from "hardhat/config"
import { randomBytes } from "ethers";
import { fromBytes } from "viem/utils";

import { randomBytes } from "ethers"
import { fromBytes } from "viem/utils"
import { hashBytecode } from "zksync-ethers/build/utils"
import { Hex } from "viem"

task("deploy-factory", "Deploy an Openfort Factory")
.addFlag("verify", "Verify the contract code on explorer")
.addOptionalParam("account", "The account implementation address")
.addOptionalParam("salt", "Salt for create2 deployment")
.setAction(async (args, hre) => {
const contractArtifactName = "UpgradeableOpenfortFactory";
const proxyArtifactName = "UpgradeableOpenfortProxy";
const factoryArtifact = await hre.deployer.loadArtifact(contractArtifactName);
const proxyArtifact = await hre.deployer.loadArtifact(proxyArtifactName);
const contractArtifactName = "UpgradeableOpenfortFactory"
const proxyArtifactName = "UpgradeableOpenfortProxy"
const factoryArtifact = await hre.deployer.loadArtifact(contractArtifactName)
const proxyArtifact = await hre.deployer.loadArtifact(proxyArtifactName)

// Private key of the account used to deploy
const wallet = new Wallet(hre.network.config.accounts[0]);
const wallet = new Wallet(hre.network.config.accounts[0])

const RECOVERY_PERIOD = 2 * 24 * 60 * 60; // 2 days in seconds
const SECURITY_PERIOD = 1.5 * 24 * 60 * 60; // 1.5 days in seconds
const SECURITY_WINDOW = 0.5 * 24 * 60 * 60; // 0.5 days in seconds
const LOCK_PERIOD = 5 * 24 * 60 * 60; // 5 days in seconds
const RECOVERY_PERIOD = 2 * 24 * 60 * 60 // 2 days in seconds
const SECURITY_PERIOD = 1.5 * 24 * 60 * 60 // 1.5 days in seconds
const SECURITY_WINDOW = 0.5 * 24 * 60 * 60 // 0.5 days in seconds
const LOCK_PERIOD = 5 * 24 * 60 * 60 // 5 days in seconds

if (!args.account) {
args.account = await hre.run("deploy-account", { verify: args.verify })
}

// https://docs.zksync.io/zk-stack/components/zksync-evm/bootloader#bytecode-hashes
// Bytecode of UpgradeableOpenfortProxy SHOULD be constant no matter the BaseOpenfortAccount.sol update.

// Something looks wrong with the build system because dynamically reading the proxy bytecode `proxyArtifact.bytecode` gives a wrong value.
// proxyBytecodeHash SHOULD ALWAYS BE === 0x010000a54c9de7cae403cb015684edcf24a518b73a06ef7ac6ea5da27134f3e0
const proxyBytecode = "0x0002000000000002000600000000000200000000030100190000006003300270000000890330019700010000003103550000008009000039000000400090043f00000001002001900000001a0000c13d000000000431034f000000040030008c000000680000413d000000000201043b000000a002200197000000a10020009c0000007e0000c13d0000000001000416000000000001004b000000660000c13d0000009001000041000000000101041a0000008b01100197000000800010043f000000a2010000410000021e0001042e0000000002000416000000000002004b000000660000c13d0000001f023000390000008a022001970000008002200039000000400020043f0000001f0230018f000000050430027200000005044002100000002c0000613d00000080050000390000008006400039000000000701034f000000007807043c0000000005850436000000000065004b000000280000c13d000000000002004b0000003a0000613d000000000141034f00000003022002100000008004400039000000000504043300000000052501cf000000000525022f000000000101043b0000010002200089000000000121022f00000000012101cf000000000151019f0000000000140435000000400030008c000000660000413d000000800600043d0000008b0060009c000000660000213d000000a00200043d0000008c0020009c000000660000213d0000001f01200039000000000031004b00000000040000190000008d040080410000008d01100197000000000001004b00000000050000190000008d050040410000008d0010009c000000000504c019000000000005004b000000660000c13d000000800120003900000000010104330000008c0010009c000001a10000213d0000001f04100039000000a3044001970000003f04400039000000a304400197000000400700043d0000000004470019000000000074004b000000000500001900000001050040390000008c0040009c000001a10000213d0000000100500190000001a10000c13d0000008003300039000000400040043f0000000008170436000000a0022000390000000004210019000000000034004b000001040000a13d00000000010000190000021f00010430000000000003004b0000007e0000c13d0000009001000041000000000201041a00000000010004140000008b02200197000000040020008c000000db0000c13d00000000030000310000001f0230018f00000005013002720000007b0000613d0000000505100210000000000604034f0000000007000019000000006806043c0000000007870436000000000057004b000000770000c13d000000000002004b000000a80000c13d000000fe0000013d0000001f0630018f0000009002000041000000000202041a000000050530027200000005055002100000008a0000613d000000000701034f0000000008000019000000007907043c0000000008980436000000000058004b000000860000c13d0000008b02200197000000000006004b000000980000613d0000000306600210000000000705043300000000076701cf000000000767022f000000000151034f000000000101043b0000010006600089000000000161022f00000000016101cf000000000171019f00000000001504350000000001000414000000040020008c000000b50000c13d00000000030000310000001f0230018f0000000501300272000000a60000613d0000000505100210000000000604034f0000000007000019000000006806043c0000000007870436000000000057004b000000a20000c13d000000000002004b000000fe0000613d00000003022002100000000501100210000000000501043300000000052501cf000000000525022f000000000414034f000000000404043b0000010002200089000000000424022f00000000022401cf000000000252019f0000000000210435000000fe0000013d0000006003300210000000890010009c0000008901008041000000c001100210000000000131019f021d02180000040f0001000000010355000000000301001900000060033002700000001f0430018f000000890030019d00000089033001970000000505300272000000ca0000613d0000000506500210000000000701034f0000000008000019000000007907043c0000000008980436000000000068004b000000c60000c13d000000000004004b000000d80000613d00000003044002100000000505500210000000000605043300000000064601cf000000000646022f000000000151034f000000000101043b0000010004400089000000000141022f00000000014101cf000000000161019f00000000001504350000000100200190000000fe0000c13d000001020000013d000000890010009c0000008901008041000000c001100210021d02180000040f0001000000010355000000000301001900000060033002700000001f0430018f000000890030019d00000089033001970000000505300272000000ee0000613d0000000506500210000000000701034f0000000008000019000000007907043c0000000008980436000000000068004b000000ea0000c13d000000000004004b000000fc0000613d00000003044002100000000505500210000000000605043300000000064601cf000000000646022f000000000151034f000000000101043b0000010004400089000000000141022f00000000014101cf000000000161019f00000000001504350000000100200190000001020000613d000000890030009c000000890300804100000060013002100000021e0001042e00000060013002100000021f00010430000500000007001d000300000009001d000000000001004b000001100000613d000000000300001900000000048300190000000005230019000000000505043300000000005404350000002003300039000000000013004b000001090000413d000400000008001d000000000118001900000000000104350000008e010000410000000000100439000600000006001d00000004006004430000000001000414000000890010009c0000008901008041000000c0011002100000008f011001c70000800202000039021d02130000040f0000000100200190000001ca0000613d000000000101043b000000000001004b000001370000c13d000000400100043d00000064021000390000009b03000041000000000032043500000044021000390000009c03000041000000000032043500000024021000390000002d03000039000000000032043500000098020000410000000000210435000000040210003900000020030000390000000000320435000000890010009c000000890100804100000040011002100000009d011001c70000021f0001043000000006010000290000008b051001970000009001000041000000000201041a0000009102200197000000000252019f000000000021041b0000000001000414000000890010009c0000008901008041000000c00110021000000092011001c70000800d0200003900000002030000390000009304000041021d020e0000040f0000000604000029000000010020019000000005050000290000000406000029000000660000613d0000000001050433000000000001004b000001540000c13d0000002001000039000001000010044300000120000004430000009a010000410000021e0001042e000000400300043d000000940030009c000001a10000213d0000006001300039000000400010043f0000004001300039000000950200004100000000002104350000002701000039000200000003001d00000000021304360000009601000041000100000002001d000000000012043500000000020504330000000001000414000000040040008c000001690000c13d000000010200003900000000030000310000017b0000013d000000890060009c00000089060080410000004003600210000000890020009c00000089020080410000006002200210000000000232019f000000890010009c0000008901008041000000c001100210000000000112019f0000000002040019021d02180000040f000000010220018f00010000000103550000006001100270000000890010019d0000008903100197000000000003004b0000019f0000c13d00000060010000390000000001010433000000000002004b000001a70000c13d000000000001004b000001f70000c13d000000400400043d000600000004001d0000009801000041000000000014043500000004014000390000002002000039000000000021043500000002010000290000000003010433000500000003001d0000002401400039000000000031043500000044024000390000000101000029021d02000000040f00000005010000290000001f01100039000000a3011001970000004401100039000000890010009c00000089010080410000000602000029000000890020009c000000890200804100000060011002100000004002200210000000000121019f0000021f000104300000008c0030009c000001cb0000a13d0000009e0100004100000000001004350000004101000039000000040010043f0000009f010000410000021f00010430000000000001004b00000006020000290000014f0000c13d0000008e01000041000000000010043900000004002004430000000001000414000000890010009c0000008901008041000000c0011002100000008f011001c70000800202000039021d02130000040f0000000100200190000001ca0000613d000000000101043b000000000001004b0000014f0000c13d000000400100043d00000044021000390000009703000041000000000032043500000024021000390000001d03000039000000000032043500000098020000410000000000210435000000040210003900000020030000390000000000320435000000890010009c0000008901008041000000400110021000000099011001c70000021f00010430000000000001042f0000001f01300039000000a3011001970000003f01100039000000a304100197000000400100043d0000000004410019000000000014004b000000000500001900000001050040390000008c0040009c000001a10000213d0000000100500190000001a10000c13d000000400040043f0000001f0430018f0000000005310436000300000005001d00000001050003670000000503300272000001e70000613d000000050630021000000003080000290000000006680019000000000705034f000000007907043c0000000008980436000000000068004b000001e30000c13d000000000004004b0000017e0000613d0000000503300210000000000535034f00000003033000290000000304400210000000000603043300000000064601cf000000000646022f000000000505043b0000010004400089000000000545022f00000000044501cf000000000464019f00000000004304350000017e0000013d0000000302000029000000890020009c00000089020080410000004002200210000000890010009c00000089010080410000006001100210000000000121019f0000021f00010430000000000003004b0000020a0000613d000000000400001900000000052400190000000006140019000000000606043300000000006504350000002004400039000000000034004b000002030000413d00000000012300190000000000010435000000000001042d000000000001042f00000211002104210000000102000039000000000001042d0000000002000019000000000001042d00000216002104230000000102000039000000000001042d0000000002000019000000000001042d0000021b002104250000000102000039000000000001042d0000000002000019000000000001042d0000021d000004320000021e0001042e0000021f00010430000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffff00000000000000000000000000000000000000000000000000000001ffffffe0000000000000000000000000ffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000ffffffffffffffff80000000000000000000000000000000000000000000000000000000000000001806aa1896bbf26568e884a7374b41e002500962caba6a15023a8d90e8508b830200000200000000000000000000000000000024000000000000000000000000360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbcffffffffffffffffffffffff00000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000bc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b000000000000000000000000000000000000000000000000ffffffffffffff9f206661696c656400000000000000000000000000000000000000000000000000416464726573733a206c6f772d6c6576656c2064656c65676174652063616c6c416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000008c379a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006400000000000000000000000000000002000000000000000000000000000000400000010000000000000000006f74206120636f6e747261637400000000000000000000000000000000000000455243313936373a206e657720696d706c656d656e746174696f6e206973206e00000000000000000000000000000000000000840000000000000000000000004e487b71000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024000000000000000000000000ffffffff000000000000000000000000000000000000000000000000000000005c60da1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000800000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe06bc2a927edf0709c699e167c1b10038f40febe785b01de63b8385e86b638e2a7";
const proxyBytecodeHash = "0x010000a54c9de7cae403cb015684edcf24a518b73a06ef7ac6ea5da27134f3e0";

// DEBUG
console.log("Correct proxy bytecode hash: ", proxyBytecodeHash)
// proxyArtifact.bytecode != proxyBytecode
console.log("Wrong computed proxy bytecode hash: ", Buffer.from(utils.hashBytecode(proxyArtifact.bytecode)).toString('hex'))

const proxyBytecodeHash = hashBytecode(proxyArtifact.bytecode as Hex)
const constructorArguments = [
wallet.address,
proxyBytecodeHash,
Expand All @@ -50,13 +38,13 @@ task("deploy-factory", "Deploy an Openfort Factory")
wallet.address,
]

const salt = args.salt ?? fromBytes(randomBytes(32), 'hex');
const salt = args.salt ?? fromBytes(randomBytes(32), "hex")

const contract = await hre.deployer.deploy(factoryArtifact,
constructorArguments,
"create2",
{
// fill paymaster params for sophon, leave empty .
customData: !hre.network.config.url.includes("sophon") ? {salt} : {
customData: !hre.network.config.url.includes("sophon") ? { salt } : {
salt,
paymasterParams: utils.getPaymasterParams(
process.env.SOPHON_TESTNET_PAYMASTER_ADDRESS!,
Expand All @@ -67,30 +55,26 @@ task("deploy-factory", "Deploy an Openfort Factory")
gasPerPubdata: utils.DEFAULT_GAS_PER_PUBDATA_LIMIT,
},
},
[proxyBytecode] // additional factory deps
);
[proxyArtifact.bytecode] // Pass the bytecode for additional factory deps
)

const FACTORY_ADDRESS = await contract.getAddress();
console.log(`Factory deployed to: ${FACTORY_ADDRESS}`);
const FACTORY_ADDRESS = await contract.getAddress()
console.log(`Factory deployed to: ${FACTORY_ADDRESS}`)
if (args.verify) {
const fullContractSource = `${factoryArtifact.sourceName}:${factoryArtifact.contractName}`;
// verification of implementation contract always fail
// because it has already been verified
// on failure: print error and do nothing
// don't do that in production code, only in test setup
const fullContractSource = `${factoryArtifact.sourceName}:${factoryArtifact.contractName}`
try {
await hre.run("verify:verify", {
address: FACTORY_ADDRESS,
constructorArguments: [],
contract: fullContractSource,
noCompile: true,
});
})
} catch (e) {
console.log(e)
}
}
return {
factory: FACTORY_ADDRESS,
implementation: args.account
implementation: args.account,
}
});
})
2 changes: 1 addition & 1 deletion test/upgradeableOpenfortAccountTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe("ERC20 interactions from Openfort Account", function () {

// OPENFORT FLOW:
// for self-custody accounts: Openfort returns a serialized signable hash from a transaction intent
// User must sign it then call the `signature` endpoint to broadcast through `sendRawTranscatoin`
// User must sign it then call the `signature` endpoint to broadcast through `sendRawTransaction`

const EIP712hash = hashTypedData(chain.custom.getEip712Domain(signableTransaction))
const signature = await accountWithOwner.sign({ hash: EIP712hash })
Expand Down

0 comments on commit d03baf0

Please sign in to comment.