|
| 1 | +"""Check token publication records, consumer linkage, and deployed module bytes.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import base64 |
| 5 | +import hashlib |
| 6 | +import json |
| 7 | +from pathlib import Path |
| 8 | +import shutil |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +import tempfile |
| 12 | +import tomllib |
| 13 | + |
| 14 | + |
| 15 | +ROOT = Path(__file__).resolve().parents[2] |
| 16 | +TOKEN = ROOT / "talus" |
| 17 | +CONSUMER = ROOT / "Tests" / "dependency" |
| 18 | +DEPLOYMENTS = { |
| 19 | + "mainnet": ( |
| 20 | + "35834a8a", |
| 21 | + "0xee962a61432231c2ede6946515beb02290cb516ad087bb06a731e922b2a5f57a", |
| 22 | + ), |
| 23 | + "testnet": ( |
| 24 | + "4c78adac", |
| 25 | + "0x5f1861ac8198fae5dc6ec79e435d653d08167187ad8c0522c34f34c372853c5a", |
| 26 | + ), |
| 27 | +} |
| 28 | +# SHA256 of the single module submitted in both successful publications: |
| 29 | +# Mainnet: EwmnFH7uYr9uDALhSNqdkQQzEqBn3ryS6vE2iYWm1agg |
| 30 | +# Testnet: 9VH4oB8Xi6uA69nqTTNbaxwooZgRCJC3s95CEDxkETqE |
| 31 | +# Publication bytes have the module's own address set to zero. |
| 32 | +MODULE_SHA256 = "fd276ffb8e5c75685315b7a931a2a453015aeb1b0faa245c0a2c491aaa6b5e0f" |
| 33 | + |
| 34 | + |
| 35 | +def run_sui(binary, *args): |
| 36 | + result = subprocess.run( |
| 37 | + [binary, *args], check=True, capture_output=True, text=True, |
| 38 | + ) |
| 39 | + if result.stderr: |
| 40 | + print(result.stderr, file=sys.stderr, end="") |
| 41 | + return result.stdout |
| 42 | + |
| 43 | + |
| 44 | +def check_publications(): |
| 45 | + with (TOKEN / "Published.toml").open("rb") as source: |
| 46 | + records = tomllib.load(source)["published"] |
| 47 | + if set(records) != set(DEPLOYMENTS): |
| 48 | + raise ValueError("Publication records must contain Mainnet and Testnet") |
| 49 | + for network, (chain, package) in DEPLOYMENTS.items(): |
| 50 | + expected = { |
| 51 | + "chain-id": chain, |
| 52 | + "published-at": package, |
| 53 | + "original-id": package, |
| 54 | + "version": 1, |
| 55 | + "toolchain-version": "1.59.1", |
| 56 | + } |
| 57 | + for key, value in expected.items(): |
| 58 | + if records[network].get(key) != value: |
| 59 | + raise ValueError(f"Unexpected {network} publication field: {key}") |
| 60 | + |
| 61 | + |
| 62 | +def module_bytes(build): |
| 63 | + if len(build["modules"]) != 1: |
| 64 | + raise ValueError("Expected exactly one module") |
| 65 | + return base64.b64decode(build["modules"][0], validate=True) |
| 66 | + |
| 67 | + |
| 68 | +def check_bytecode(binary): |
| 69 | + version = run_sui(binary, "--version").split()[1].split("-")[0] |
| 70 | + if version != "1.59.1": |
| 71 | + raise ValueError("Bytecode reproduction requires Sui 1.59.1") |
| 72 | + # The release compiler supplies its pinned framework. Compile in isolation |
| 73 | + # so the current package manager's lockfile stays intact. |
| 74 | + with tempfile.TemporaryDirectory(prefix="talus_verify_") as directory: |
| 75 | + package = Path(directory) |
| 76 | + shutil.copyfile(TOKEN / "Move.toml", package / "Move.toml") |
| 77 | + shutil.copytree(TOKEN / "sources", package / "sources") |
| 78 | + build = json.loads(run_sui( |
| 79 | + binary, "move", "build", "--path", str(package), |
| 80 | + "--dump-bytecode-as-base64", "--ignore-chain", |
| 81 | + )) |
| 82 | + digest = hashlib.sha256(module_bytes(build)).hexdigest() |
| 83 | + if digest != MODULE_SHA256: |
| 84 | + raise ValueError(f"Token module differs from the deployed bytecode: {digest}") |
| 85 | + print("Token bytecode matches the published Mainnet and Testnet module") |
| 86 | + |
| 87 | + |
| 88 | +def check_dependency(binary, network): |
| 89 | + build = json.loads(run_sui( |
| 90 | + binary, "move", "build", "--path", str(CONSUMER), "-e", network, |
| 91 | + "--dump-bytecode-as-base64", "--no-tree-shaking", |
| 92 | + )) |
| 93 | + package = DEPLOYMENTS[network][1] |
| 94 | + dependencies = {int(address, 16) for address in build["dependencies"]} |
| 95 | + if int(package, 16) not in dependencies: |
| 96 | + raise ValueError(f"Consumer does not link the {network} token package") |
| 97 | + for other_network, (_, other_package) in DEPLOYMENTS.items(): |
| 98 | + if other_network != network and int(other_package, 16) in dependencies: |
| 99 | + raise ValueError(f"Consumer unexpectedly links the {other_network} token") |
| 100 | + if bytes.fromhex(package[2:]) not in module_bytes(build): |
| 101 | + raise ValueError("Consumer bytecode does not reference the expected token type") |
| 102 | + print(f"Consumer uses the correct {network} token: {package}") |
| 103 | + |
| 104 | + |
| 105 | +def main(): |
| 106 | + parser = argparse.ArgumentParser(description=__doc__) |
| 107 | + parser.add_argument("--sui", default="sui", help="Path to the Sui binary") |
| 108 | + commands = parser.add_subparsers(dest="command", required=True) |
| 109 | + commands.add_parser("bytecode", help="Reproduce the token with Sui 1.59.1") |
| 110 | + dependency = commands.add_parser("dependency", help="Check consumer linkage") |
| 111 | + dependency.add_argument("network", choices=DEPLOYMENTS) |
| 112 | + args = parser.parse_args() |
| 113 | + check_publications() |
| 114 | + if args.command == "bytecode": |
| 115 | + check_bytecode(args.sui) |
| 116 | + else: |
| 117 | + check_dependency(args.sui, args.network) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + try: |
| 122 | + main() |
| 123 | + except subprocess.CalledProcessError as error: |
| 124 | + print(error.stdout or "", file=sys.stderr, end="") |
| 125 | + print(error.stderr or "", file=sys.stderr, end="") |
| 126 | + sys.exit(error.returncode) |
| 127 | + except (OSError, ValueError, KeyError) as error: |
| 128 | + sys.exit(str(error)) |
0 commit comments