cheatcodes_utilities.go 8.31 KB
Newer Older
1 2 3 4 5
package script

import (
	"fmt"
	"math/big"
6
	"regexp"
7 8 9
	"strconv"
	"strings"

10 11
	"github.com/BurntSushi/toml"

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
	hdwallet "github.com/ethereum-optimism/go-ethereum-hdwallet"

	"github.com/ethereum/go-ethereum/accounts"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/common/hexutil"
	"github.com/ethereum/go-ethereum/common/math"
	"github.com/ethereum/go-ethereum/core/vm"
	"github.com/ethereum/go-ethereum/crypto"
)

// Addr implements https://book.getfoundry.sh/cheatcodes/addr
func (c *CheatCodesPrecompile) Addr(privateKey *big.Int) (common.Address, error) {
	priv, err := crypto.ToECDSA(leftPad32(privateKey.Bytes()))
	if err != nil {
		return common.Address{}, err
	}
	return crypto.PubkeyToAddress(priv.PublicKey), nil
}

// Sign implements https://book.getfoundry.sh/cheatcodes/sign
func (c *CheatCodesPrecompile) Sign() error {
	return vm.ErrExecutionReverted
}

// Skip implements https://book.getfoundry.sh/cheatcodes/skip
func (c *CheatCodesPrecompile) Skip() error {
	return vm.ErrExecutionReverted
}

// Label implements https://book.getfoundry.sh/cheatcodes/label
func (c *CheatCodesPrecompile) Label(addr common.Address, label string) {
43
	c.h.Label(addr, label)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
}

// GetLabel implements https://book.getfoundry.sh/cheatcodes/get-label
func (c *CheatCodesPrecompile) GetLabel(addr common.Address) string {
	label, ok := c.h.labels[addr]
	if !ok {
		return "unlabeled:" + addr.String()
	}
	return label
}

// DeriveKey_6229498b implements https://book.getfoundry.sh/cheatcodes/derive-key
func (c *CheatCodesPrecompile) DeriveKey_6229498b(mnemonic string, index uint32) (*big.Int, error) {
	return c.DeriveKey_6bcb2c1b(mnemonic, "m/44'/60'/0'/0/", index)
}

// DeriveKey_6bcb2c1b implements https://book.getfoundry.sh/cheatcodes/derive-key
func (c *CheatCodesPrecompile) DeriveKey_6bcb2c1b(mnemonic string, path string, index uint32) (*big.Int, error) {
	w, err := hdwallet.NewFromMnemonic(mnemonic)
	if err != nil {
		return nil, fmt.Errorf("invalid mnemonic: %w", err)
	}
	account := accounts.Account{URL: accounts.URL{Path: path + strconv.FormatInt(int64(index), 10)}}
	priv, err := w.PrivateKey(account)
	if err != nil {
		return nil, fmt.Errorf("failed to derive key of path %s: %w", account.URL.Path, err)
	}
	return common.Hash(crypto.FromECDSA(priv)).Big(), nil
}

// ParseBytes implements https://book.getfoundry.sh/cheatcodes/parse-bytes
func (c *CheatCodesPrecompile) ParseBytes(stringifiedValue string) ([]byte, error) {
	return hexutil.Decode(stringifiedValue)
}

// ParseAddress implements https://book.getfoundry.sh/cheatcodes/parse-address
func (c *CheatCodesPrecompile) ParseAddress(stringifiedValue string) (common.Address, error) {
	var out common.Address
	err := out.UnmarshalText([]byte(stringifiedValue))
	return out, err
}

// ParseUint implements https://book.getfoundry.sh/cheatcodes/parse-uint
func (c *CheatCodesPrecompile) ParseUint(stringifiedValue string) (*big.Int, error) {
	out := new(big.Int)
	err := out.UnmarshalText([]byte(stringifiedValue))
	if err != nil {
		return big.NewInt(0), err
	}
	if out.BitLen() > 256 {
		return big.NewInt(0), fmt.Errorf("value %d is not a uint256, got %d bits", out, out.BitLen())
	}
	if out.Sign() < 0 {
		return big.NewInt(0), fmt.Errorf("value %d is not a uint256, it has a negative sign", out)
	}
	return out, nil
}

var (
	topBit    = math.BigPow(2, 255)
	maxInt256 = new(big.Int).Sub(topBit, big.NewInt(1))
	minInt256 = new(big.Int).Neg(topBit)
)

// ParseInt implements https://book.getfoundry.sh/cheatcodes/parse-int
func (c *CheatCodesPrecompile) ParseInt(stringifiedValue string) (*ABIInt256, error) {
	out := new(big.Int)
	err := out.UnmarshalText([]byte(stringifiedValue))
	if err != nil {
		return (*ABIInt256)(big.NewInt(0)), err
	}
	if out.Cmp(minInt256) < 0 || out.Cmp(maxInt256) > 0 {
		return (*ABIInt256)(big.NewInt(0)), fmt.Errorf("input %q out of int256 bounds", stringifiedValue)
	}
	return (*ABIInt256)(out), nil
}

// ParseBytes32 implements https://book.getfoundry.sh/cheatcodes/parse-bytes32
func (c *CheatCodesPrecompile) ParseBytes32(stringifiedValue string) ([32]byte, error) {
	var out common.Hash
	err := out.UnmarshalText([]byte(stringifiedValue))
	return out, err
}

// ParseBool implements https://book.getfoundry.sh/cheatcodes/parse-bool
func (c *CheatCodesPrecompile) ParseBool(stringifiedValue string) (bool, error) {
	switch strings.ToLower(stringifiedValue) {
	case "true", "1":
		return true, nil
	case "false", "0":
		return false, nil
	default:
		return false, fmt.Errorf("failed parsing %q as type `bool`", stringifiedValue)
	}
}

// RememberKey implements https://book.getfoundry.sh/cheatcodes/remember-key
func (c *CheatCodesPrecompile) RememberKey(privateKey *big.Int) (common.Address, error) {
	// We don't store the key, but we can return the address of it, to not break compat
	return c.Addr(privateKey)
}

// ToString_56ca623e implements https://book.getfoundry.sh/cheatcodes/to-string
func (c *CheatCodesPrecompile) ToString_56ca623e(v common.Address) string {
	return v.String()
}

// ToString_71dce7da implements https://book.getfoundry.sh/cheatcodes/to-string
func (c *CheatCodesPrecompile) ToString_71dce7da(v bool) string {
	if v {
		return "true"
	} else {
		return "false"
	}
}

// ToString_6900a3ae implements https://book.getfoundry.sh/cheatcodes/to-string
func (c *CheatCodesPrecompile) ToString_6900a3ae(v *big.Int) string {
	return v.String()
}

// ToString_a322c40e implements https://book.getfoundry.sh/cheatcodes/to-string
func (c *CheatCodesPrecompile) ToString_a322c40e(v *ABIInt256) string {
	return (*big.Int)(v).String()
}

// ToString_b11a19e8 implements https://book.getfoundry.sh/cheatcodes/to-string
func (c *CheatCodesPrecompile) ToString_b11a19e8(v [32]byte) string {
	return common.Hash(v).String()
}

// ToString_71aad10d implements https://book.getfoundry.sh/cheatcodes/to-string
func (c *CheatCodesPrecompile) ToString_71aad10d(v []byte) string {
	return hexutil.Bytes(v).String()
}

// Breakpoint_f0259e92 implements https://book.getfoundry.sh/cheatcodes/breakpoint
func (c *CheatCodesPrecompile) Breakpoint_f0259e92(name string) {
	c.h.log.Debug("breakpoint hit", "name", name)
}

// Breakpoint_f7d39a8d implements https://book.getfoundry.sh/cheatcodes/breakpoint
func (c *CheatCodesPrecompile) Breakpoint_f7d39a8d(name string, v bool) {
	if v {
		c.h.log.Debug("breakpoint set", "name", name)
	} else {
		c.h.log.Debug("breakpoint unset", "name", name)
	}
}

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
// ParseTomlAddress_65e7c844 implements https://book.getfoundry.sh/cheatcodes/parse-toml. This
// method is not well optimized or implemented. It's optimized for quickly delivering OPCM. We
// can come back and clean it up more later.
func (c *CheatCodesPrecompile) ParseTomlAddress_65e7c844(tomlStr string, key string) (common.Address, error) {
	var data map[string]any
	if err := toml.Unmarshal([]byte(tomlStr), &data); err != nil {
		return common.Address{}, fmt.Errorf("failed to parse TOML: %w", err)
	}

	keys, err := SplitJSONPathKeys(key)
	if err != nil {
		return common.Address{}, fmt.Errorf("failed to split keys: %w", err)
	}

	loc := data
	for i, k := range keys {
		value, ok := loc[k]
		if !ok {
			return common.Address{}, fmt.Errorf("key %q not found in TOML", k)
		}

		if i == len(keys)-1 {
			addrStr, ok := value.(string)
			if !ok {
				return common.Address{}, fmt.Errorf("key %q is not a string", key)
			}
			if !common.IsHexAddress(addrStr) {
				return common.Address{}, fmt.Errorf("key %q is not a valid address", key)
			}
			return common.HexToAddress(addrStr), nil
		}

		next, ok := value.(map[string]any)
		if !ok {
			return common.Address{}, fmt.Errorf("key %q is not a nested map", key)
		}
		loc = next
	}

	panic("should never get here")
}

236 237
// unsupported
//func (c *CheatCodesPrecompile) CreateWallet() {}
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

// SplitJSONPathKeys splits a JSON path into keys. It supports bracket notation. There is a much
// better way to implement this, but I'm keeping this simple for now.
func SplitJSONPathKeys(path string) ([]string, error) {
	var out []string
	bracketSplit := regexp.MustCompile(`[\[\]]`).Split(path, -1)
	for _, split := range bracketSplit {
		if len(split) == 0 {
			continue
		}

		split = strings.ReplaceAll(split, "\"", "")
		split = strings.ReplaceAll(split, " ", "")

		if !strings.HasPrefix(split, ".") {
			out = append(out, split)
			continue
		}

		keys := strings.Split(split, ".")
		for _, key := range keys {
			if len(key) == 0 {
				continue
			}
			out = append(out, key)
		}
	}

	return out, nil
}