|
| 1 | +import test from 'ava'; |
| 2 | +import { remixURL } from './remix'; |
| 3 | + |
| 4 | +// Decoder used in Remix |
| 5 | +const decodeBase64 = (b64Payload: string) => { |
| 6 | + const raw = atob(decodeURIComponent(b64Payload)); |
| 7 | + const bytes = Uint8Array.from(raw, c => c.charCodeAt(0)); |
| 8 | + return new TextDecoder().decode(bytes); |
| 9 | +}; |
| 10 | + |
| 11 | +test('remixURL encodes code param decodable by decodeBase64', t => { |
| 12 | + const contractSource = 'contract A{}'; |
| 13 | + |
| 14 | + const url = remixURL(contractSource); |
| 15 | + const codeParam = url.searchParams.get('code'); |
| 16 | + t.truthy(codeParam, 'Expected code search param to be set'); |
| 17 | + |
| 18 | + const decoded = decodeBase64(codeParam!); |
| 19 | + t.is(decoded, contractSource, 'Decoded code should equal original source'); |
| 20 | +}); |
| 21 | + |
| 22 | +test('remixURL sets deployProxy flag when upgradeable', t => { |
| 23 | + const contractSource = 'contract A{}'; |
| 24 | + |
| 25 | + const urlTrue = remixURL(contractSource, true); |
| 26 | + t.is(urlTrue.searchParams.get('deployProxy'), 'true'); |
| 27 | + |
| 28 | + const urlFalse = remixURL(contractSource, false); |
| 29 | + t.is(urlFalse.searchParams.get('deployProxy'), null); |
| 30 | +}); |
| 31 | + |
| 32 | +test('remixURL encodes code param with special characters decodable by decodeBase64', t => { |
| 33 | + // not a valid contract |
| 34 | + const contractSource = `// SPDX-License-Identifier: MIT |
| 35 | +// Compatible with OpenZeppelin Contracts ^5.4.0 |
| 36 | +pragma solidity ^0.8.27; |
| 37 | +
|
| 38 | +import {AccountERC7579} from "@openzeppelin/contracts/account/extensions/draft-AccountERC7579.sol"; |
| 39 | +import {ERC7739} from "@openzeppelin/contracts/utils/cryptography/signers/draft-ERC7739.sol"; |
| 40 | +
|
| 41 | +contract MyAccount is ERC7739, AccountERC7579 { |
| 42 | + constructor(address signer) |
| 43 | + EIP712(unicode"MyAccount🌾", "1") |
| 44 | + SignerECDSA(signer) |
| 45 | + {} |
| 46 | +
|
| 47 | + function isValidSignature(bytes32 hash, bytes calldata signature) |
| 48 | + public |
| 49 | + view |
| 50 | + override(AccountERC7579, ERC7739) |
| 51 | + returns (bytes4) |
| 52 | + { |
| 53 | + // ERC-7739 can return the ERC-1271 magic value, 0xffffffff (invalid) or 0x77390001 (detection). |
| 54 | + // If the returned value is 0xffffffff, fallback to ERC-7579 validation. |
| 55 | + bytes4 erc7739magic = ERC7739.isValidSignature(hash, signature); |
| 56 | + return erc7739magic == bytes4(0xffffffff) ? AccountERC7579.isValidSignature(hash, signature) : erc7739magic; |
| 57 | + } |
| 58 | +}`; |
| 59 | + |
| 60 | + const url = remixURL(contractSource); |
| 61 | + const codeParam = url.searchParams.get('code'); |
| 62 | + t.truthy(codeParam, 'Expected code search param to be set'); |
| 63 | + |
| 64 | + const decoded = decodeBase64(codeParam!); |
| 65 | + t.is(decoded, contractSource, 'Decoded code should equal original source'); |
| 66 | +}); |
0 commit comments