|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { mkdtempSync, readFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { secp256k1 } from "@noble/curves/secp256k1"; |
| 6 | +import { keccak_256 } from "@noble/hashes/sha3"; |
| 7 | +import { Account, CallData, RpcProvider, num, type Abi } from "starknet"; |
| 8 | +import { declareClass, deployContract, repoRoot } from "./utils.js"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Deploys a `StarknetEth712Account` (from `starkware_accounts`, emitted by the privacy test build) on |
| 12 | + * devnet: a Starknet account that validates EVM/secp256k1 EIP-712 signatures. The account has no |
| 13 | + * constructor — deploy it via the UDC, then `initialize(eth_address, ownership_signature)` proves |
| 14 | + * EVM-key ownership and registers its SRC5 interfaces (incl. custom-signature-validation). |
| 15 | + */ |
| 16 | + |
| 17 | +const TEST_BUILD_DIR = join(repoRoot(), "target/dev"); |
| 18 | +const ACCOUNT_CONTRACT = |
| 19 | + "privacy_unittest_StarknetEth712Account.test.contract_class.json"; |
| 20 | + |
| 21 | +// keccak256("\x19Ethereum Signed Message:\n41Sign to verify that you own this account.") |
| 22 | +// (starkware_accounts::eth_712_utils::OWNERSHIP_TRANSFER_MSG_HASH) — signing it with the EVM key |
| 23 | +// proves account ownership at initialize(). |
| 24 | +const OWNERSHIP_TRANSFER_MSG_HASH = |
| 25 | + 0x3ce976d55131cd0bdd49f20afbded052d8e907dc6034d95cdf117a8fd7752e3cn; |
| 26 | + |
| 27 | +function to32(value: bigint): Uint8Array { |
| 28 | + const out = new Uint8Array(32); |
| 29 | + let rest = value; |
| 30 | + for (let index = 31; index >= 0; index--) { |
| 31 | + out[index] = Number(rest & 0xffn); |
| 32 | + rest >>= 8n; |
| 33 | + } |
| 34 | + return out; |
| 35 | +} |
| 36 | + |
| 37 | +function bytesToBigInt(bytes: Uint8Array): bigint { |
| 38 | + let value = 0n; |
| 39 | + for (const byte of bytes) value = (value << 8n) | BigInt(byte); |
| 40 | + return value; |
| 41 | +} |
| 42 | + |
| 43 | +/** The EVM address (low 160 bits of keccak(uncompressed pubkey)) for a secp256k1 private key. */ |
| 44 | +export function evmAddress(evmPrivateKey: bigint): bigint { |
| 45 | + const publicKey = secp256k1.getPublicKey(to32(evmPrivateKey), false).slice(1); // drop 0x04 prefix |
| 46 | + return bytesToBigInt(keccak_256(publicKey).slice(12)); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * The EVM ownership signature over the fixed `OWNERSHIP_TRANSFER_MSG_HASH`, as the account's |
| 51 | + * `Signature { r, s, y_parity }`. `y_parity` matches the 6-felt convention the account uses |
| 52 | + * elsewhere (`v = 27 + recovery`, `y_parity = v % 2 == 0`), i.e. an odd recovery id. |
| 53 | + */ |
| 54 | +function ownershipSignature(evmPrivateKey: bigint): { |
| 55 | + r: bigint; |
| 56 | + s: bigint; |
| 57 | + y_parity: boolean; |
| 58 | +} { |
| 59 | + const signature = secp256k1.sign( |
| 60 | + to32(OWNERSHIP_TRANSFER_MSG_HASH), |
| 61 | + to32(evmPrivateKey), |
| 62 | + ); |
| 63 | + return { |
| 64 | + r: signature.r, |
| 65 | + s: signature.s, |
| 66 | + y_parity: signature.recovery % 2 === 1, |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +async function declareEth712Account( |
| 71 | + admin: Account, |
| 72 | + node: RpcProvider, |
| 73 | +): Promise<string> { |
| 74 | + const sierraPath = join(TEST_BUILD_DIR, ACCOUNT_CONTRACT); |
| 75 | + const casmPath = join( |
| 76 | + mkdtempSync(join(tmpdir(), "eth712-casm-")), |
| 77 | + "account.casm.json", |
| 78 | + ); |
| 79 | + execFileSync("universal-sierra-compiler", [ |
| 80 | + "compile-contract", |
| 81 | + "--sierra-path", |
| 82 | + sierraPath, |
| 83 | + "--output-path", |
| 84 | + casmPath, |
| 85 | + ]); |
| 86 | + return declareClass(admin, node, sierraPath, casmPath); |
| 87 | +} |
| 88 | + |
| 89 | +export interface Eth712Account { |
| 90 | + /** The deployed Starknet account address. */ |
| 91 | + address: string; |
| 92 | + /** The EVM address the account validates signatures against. */ |
| 93 | + ethAddress: bigint; |
| 94 | + /** The account ABI, for compiling `is_custom_signature_valid` / `execute_from_outside_v2` calls. */ |
| 95 | + abi: Abi; |
| 96 | +} |
| 97 | + |
| 98 | +/** Declare, UDC-deploy, and initialize a `StarknetEth712Account` owned by `evmPrivateKey`. */ |
| 99 | +export async function deployEth712Account( |
| 100 | + admin: Account, |
| 101 | + node: RpcProvider, |
| 102 | + evmPrivateKey: bigint, |
| 103 | +): Promise<Eth712Account> { |
| 104 | + const classHash = await declareEth712Account(admin, node); |
| 105 | + const abi: Abi = JSON.parse( |
| 106 | + readFileSync(join(TEST_BUILD_DIR, ACCOUNT_CONTRACT), "utf8"), |
| 107 | + ).abi; |
| 108 | + const address = await deployContract(admin, node, classHash, [], "0xe712"); |
| 109 | + |
| 110 | + const ethAddress = evmAddress(evmPrivateKey); |
| 111 | + const initialize = await admin.execute({ |
| 112 | + contractAddress: address, |
| 113 | + entrypoint: "initialize", |
| 114 | + calldata: new CallData(abi).compile("initialize", { |
| 115 | + eth_address: num.toHex(ethAddress), |
| 116 | + signature: ownershipSignature(evmPrivateKey), |
| 117 | + }), |
| 118 | + }); |
| 119 | + await node.waitForTransaction(initialize.transaction_hash); |
| 120 | + |
| 121 | + return { address, ethAddress, abi }; |
| 122 | +} |
0 commit comments