Skip to content

Commit d8880c1

Browse files
✨ Add Docker Support (#41)
### 🕓 Changelog This PR adds a complete [Docker](https://www.docker.com) setup, allowing the script to be run in a _containerised_ environment. It includes a hardened [`compose.yaml`](https://docs.docker.com/compose/intro/compose-application-model/#the-compose-file) file for easy, secure, and reproducible runs. #### Building the Docker Image Build the [Docker](https://www.docker.com) image using [Docker Compose](https://docs.docker.com/compose/): ```console docker-compose build ``` #### Basic Usage To run the [script](https://github.com/pcaversaccio/safe-tx-hashes-util/blob/main/safe_hashes.sh) using [Docker Compose](https://docs.docker.com/compose/), use the `compose.yaml` file provided in the repository. The container is named `safe-tx-hashes-util`. Example displaying help: ```console docker-compose run --rm safe-tx-hashes-util --help ``` Example calculating the Safe transaction hashes: ```console docker-compose run --rm safe-tx-hashes-util --network arbitrum --address 0x111CEEee040739fD91D29C34C33E6B3E112F2177 --nonce 234 ``` #### Using Message Files When calculating off-chain message hashes, you need to provide a local directory containing your message file. The included `compose.yaml` configuration mounts the `./data` directory by default. ```console # First, create a `data` directory and add your message file. ~$ mkdir -p data ~$ echo "Your message content here" > data/message.txt # Run the container with the mounted directory. ~$ docker-compose run --rm safe-tx-hashes-util \ --network sepolia \ --address 0x657ff0D4eC65D82b2bC1247b0a558bcd2f80A0f1 \ --message /data/message.txt ``` #### With Environment Variables You can pass environment variables directly via [Docker Compose](https://docs.docker.com/compose/): ```console # Disable all formatting. docker-compose run --rm -e NO_COLOR=true safe-tx-hashes-util \ --network arbitrum \ --address 0x111CEEee040739fD91D29C34C33E6B3E112F2177 \ --nonce 234 ``` --------- Signed-off-by: Pascal Marco Caversaccio <pascal.caversaccio@hotmail.ch> Co-authored-by: Pascal Marco Caversaccio <pascal.caversaccio@hotmail.ch>
1 parent 5dd0b6c commit d8880c1

File tree

5 files changed

+138
-1
lines changed

5 files changed

+138
-1
lines changed

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore all files by default.
2+
**
3+
4+
# Include only the main script.
5+
!safe_hashes.sh

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore the `data` directory by default.
2+
data/

Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# For reproducible builds, consider pinning to a specific digest instead of `latest`.
2+
# See https://github.com/foundry-rs/foundry/pkgs/container/foundry.
3+
FROM ghcr.io/foundry-rs/foundry:latest
4+
5+
# Switch to the root user to install the necessary packages.
6+
USER root
7+
8+
# Install `curl` and `jq` using the package manager.
9+
RUN apt-get update && \
10+
apt-get install -y --no-install-recommends \
11+
curl \
12+
jq \
13+
&& rm -rf /var/lib/apt/lists/*
14+
15+
# Copy the script into the image.
16+
COPY ./safe_hashes.sh /app/safe_hashes.sh
17+
RUN chmod +x /app/safe_hashes.sh
18+
19+
# Switch back to the default, non-root Foundry user for security.
20+
USER foundry

README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ This Bash [script](./safe_hashes.sh) calculates the Safe transaction hashes by r
2020
- [Usage](#usage)
2121
- [macOS Users: Upgrading Bash](#macos-users-upgrading-bash)
2222
- [Optional: Set the New Bash as Your Default Shell](#optional-set-the-new-bash-as-your-default-shell)
23+
- [Docker Usage](#docker-usage)
24+
- [Building the Docker Image](#building-the-docker-image)
25+
- [Basic Usage](#basic-usage)
26+
- [Using Message Files](#using-message-files)
27+
- [With Environment Variables](#with-environment-variables)
2328
- [Safe Transaction Hashes](#safe-transaction-hashes)
2429
- [Interactive Mode](#interactive-mode)
2530
- [Transaction Simulation](#transaction-simulation)
@@ -81,7 +86,7 @@ This Bash [script](./safe_hashes.sh) calculates the Safe transaction hashes by r
8186
> Ensure that [`cast`](https://github.com/foundry-rs/foundry/tree/master/crates/cast) and [`chisel`](https://github.com/foundry-rs/foundry/tree/master/crates/chisel) are installed locally. For installation instructions, refer to this [guide](https://getfoundry.sh/introduction/installation/). This [script](./safe_hashes.sh) is designed to work with the latest _stable_ versions of [`cast`](https://github.com/foundry-rs/foundry/tree/master/crates/cast) and [`chisel`](https://github.com/foundry-rs/foundry/tree/master/crates/chisel), starting from version [`1.3.5`](https://github.com/foundry-rs/foundry/releases/tag/v1.3.5).
8287
8388
> [!TIP]
84-
> For macOS users, please refer to the [macOS Users: Upgrading Bash](#macos-users-upgrading-bash) section.
89+
> For macOS users, please refer to the [macOS Users: Upgrading Bash](#macos-users-upgrading-bash) section. Alternatively, you can use the Docker container, which comes pre-installed with all required dependencies. For details, see the [Docker Usage](#docker-usage) section below.
8590
8691
```console
8792
./safe_hashes.sh [--help] [--version] [--list-networks] --network <network> --address <address>
@@ -199,6 +204,65 @@ chsh -s BASH_PATH
199204

200205
Make sure to replace `BASH_PATH` with the actual path you retrieved in step 1.
201206

207+
### Docker Usage
208+
209+
Using [Docker](https://www.docker.com), you can run the [script](./safe_hashes.sh) in a containerised environment with all dependencies pre-installed. This is useful if you do not wish to install the required tools locally, or if you are on a system where installation is difficult.
210+
211+
#### Building the Docker Image
212+
213+
Build the [Docker](https://www.docker.com) image using [Docker Compose](https://docs.docker.com/compose/):
214+
215+
```console
216+
docker-compose build
217+
```
218+
219+
#### Basic Usage
220+
221+
To run the [script](./safe_hashes.sh) using [Docker Compose](https://docs.docker.com/compose/), use the [`compose.yaml`](./compose.yaml) file provided in the repository. The container is named `safe-tx-hashes-util`.
222+
223+
Example displaying help:
224+
225+
```console
226+
docker-compose run --rm safe-tx-hashes-util --help
227+
```
228+
229+
Example calculating the Safe transaction hashes:
230+
231+
```console
232+
docker-compose run --rm safe-tx-hashes-util --network arbitrum --address 0x111CEEee040739fD91D29C34C33E6B3E112F2177 --nonce 234
233+
```
234+
235+
#### Using Message Files
236+
237+
When calculating off-chain message hashes, you need to provide a local directory containing your message file. The included [`compose.yaml`](./compose.yaml) configuration mounts the `./data` directory by default.
238+
239+
```console
240+
# First, create a `data` directory and add your message file.
241+
~$ mkdir -p data
242+
~$ echo "Your message content here" > data/message.txt
243+
244+
# Run the container with the mounted directory.
245+
~$ docker-compose run --rm safe-tx-hashes-util \
246+
--network sepolia \
247+
--address 0x657ff0D4eC65D82b2bC1247b0a558bcd2f80A0f1 \
248+
--message /data/message.txt
249+
```
250+
251+
#### With Environment Variables
252+
253+
You can pass environment variables directly via [Docker Compose](https://docs.docker.com/compose/):
254+
255+
```console
256+
# Disable all formatting.
257+
docker-compose run --rm -e NO_COLOR=true safe-tx-hashes-util \
258+
--network arbitrum \
259+
--address 0x111CEEee040739fD91D29C34C33E6B3E112F2177 \
260+
--nonce 234
261+
```
262+
263+
> [!IMPORTANT]
264+
> Running in a [Docker](https://www.docker.com) container offers isolation, but it is important to always follow the [Security Best Practices](#security-best-practices-for-using-this-script).
265+
202266
## Safe Transaction Hashes
203267

204268
To calculate the Safe transaction hashes for a specific transaction, you need to specify the `network`, `address`, and `nonce` parameters. An example:

compose.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
services:
2+
safe-tx-hashes-util:
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
6+
7+
container_name: safe-tx-hashes-util
8+
9+
security_opt:
10+
- no-new-privileges:true # Prevent privilege escalation.
11+
- apparmor=docker-default # Set the `AppArmor` profile for Ubuntu/Debian/openSUSE (restrict file/network access).
12+
- label=type:container_t # Set the `SELinux` profile for RHEL/CentOS/Fedora (provide mandatory access control).
13+
14+
cap_drop:
15+
- ALL # Drop all root Linux kernel capabilities.
16+
17+
read_only: true # Make the container's root filesystem read-only.
18+
19+
working_dir: /app
20+
21+
volumes:
22+
# Mount the script from the host for live editing without rebuilding the image.
23+
- type: bind
24+
source: ./safe_hashes.sh
25+
target: /app/safe_hashes.sh
26+
read_only: true
27+
# Mount the host `./data` directory as read-only for off-chain message files.
28+
- type: bind
29+
source: ./data
30+
target: /data
31+
read_only: true
32+
bind:
33+
create_host_path: true
34+
35+
tmpfs:
36+
- /tmp:noexec,nosuid,nodev,size=50m # Store temporary files in memory only (limited to 50MB, not executable).
37+
- /home/foundry/.foundry:noexec,nosuid,nodev,size=50m # Keep Foundry's cache and settings in memory (limited to 50MB, cannot run programs from here).
38+
- /home/foundry/.svm:exec,nosuid,nodev,size=50m # Keep Solidity Version Manager (`svm`) files in memory (limited to 50MB, programs can run from here).
39+
40+
environment:
41+
- DEBUG=false # See https://github.com/pcaversaccio/safe-tx-hashes-util/tree/main#usage.
42+
- NO_COLOR=false # See https://github.com/pcaversaccio/safe-tx-hashes-util/tree/main#usage.
43+
- FOUNDRY_DISABLE_NIGHTLY_WARNING=false
44+
45+
# Override the image's default command to run the script.
46+
entrypoint: ["/bin/bash", "/app/safe_hashes.sh"]

0 commit comments

Comments
 (0)