Skip to content

Commit

Permalink
Re-add some utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Agusx1211 committed Feb 12, 2024
1 parent 32fafa3 commit df35bd3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ The compressor has the following commands:
- `encode_any <data>` Encodes any data into a compressed representation.
- `encode_sequence_tx <decode/call> <sequence_tx> <sequence_wallet>` Compresses a Sequence wallet transaction.

```
czip-compressor is a tool for compressing Ethereum calldata. The compressed data can be decompressed using the decompressor contract.
Usage:
czip-compressor [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
encode_any Compress any calldata: <hex>
encode_call Compress a call to a contract: <data> <to>
encode_calls Compress a sequence of calls: <data> <to> <data> <to> ... <data> <to>
encode_sequence_tx Compress a sequence of transactions
extras Additional encoding methods, used for testing and debugging.
help Help about any command
Flags:
--allow-opcodes strings Will only encode using these operations, separated by commas.
--cache-dir string Path to the cache dir for indexes. (default "/tmp/czip-cache")
-c, --contract string Contract address of the decompressor contract.
--disallow-opcodes strings Will not encode using these operations, separated by commas.
-h, --help help for czip-compressor
-p, --provider string Ethereum RPC provider URL.
-s, --use-storage Use stateful read/write storage during compression.
Use "czip-compressor [command] --help" for more information about a command.
```

### Encode call

It encodes a single call to a contract, the subcommands are:
Expand Down
44 changes: 44 additions & 0 deletions compressor/cmd/czip-compressor/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package main
import (
"fmt"
"os"
"strings"

encoder "github.com/0xsequence/czip/compressor"
)

func ensureDir(path string) error {
Expand All @@ -23,3 +26,44 @@ func ensureDir(path string) error {
}
return nil
}

func FindOpcodesForFlag(flag string) []uint {
res := make([]uint, 0)

for val, op := range encoder.FlagNames() {
// Fuzzy match
if strings.Contains(strings.ToLower(val), strings.ToLower(flag)) {
res = append(res, op)
}
}

if len(res) == 0 {
fmt.Println("Error: Invalid opcode flag", flag)
os.Exit(1)
}

return res
}

func ParseAllowOpcodes(allow []string, disable []string) *encoder.AllowOpcodes {
var res encoder.AllowOpcodes
res.List = make(map[uint]bool)
var val []string

if len(allow) != 0 {
val = allow
res.Default = false
} else {
val = disable
res.Default = true
}

for _, part := range val {
opcodes := FindOpcodesForFlag(part)
for _, opcode := range opcodes {
res.List[opcode] = true
}
}

return &res
}
6 changes: 4 additions & 2 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/bin/sh

# Create build dir if it doesn't exist
mkdir -p build
set -e

# Delete the old bin if it exists
rm -f ./compressor/bin/*

# Compile ./encoder/main.go to ./build/main
cd ./compressor && make build-cli && cd ..
Expand Down

0 comments on commit df35bd3

Please sign in to comment.