Skip to content

Commit

Permalink
feat: add scripts for random uploads and wallet generation
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef authored and maqi committed Jun 14, 2024
1 parent 0765c4a commit d68f6f0
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ At build time the following env vars can be set to override default keys (** and
- `NETWORK_ROYALTIES_PK` - The foundation public key to use for receiving network royalties.
- `PAYMENT_FORWARD_PK` - The public key to use for payment forwarding for the beta network collection.

When you start a network there are a few scripts to aid with basic processes:

- `resources/scripts/claim-genesis.sh` which will claim the genesis tokens for a wallet on a launched network (if you have set up the foundation wallet locally by adding a `client/account_secret` and regenerating the wallet or directly adding the `client/wallet/main_secret_key` itself).
- `resources/scripts/make-wallets.sh` which if you have a wallet with a balance will create a number of wallets with another balance. eg `resources/scripts/make-wallets.sh 5 1` will make 5 wallets with 1 token.
- `resources/scripts/upload-random-data` will use the existing `client` to upload random data to the network.

- [Client](https://github.com/maidsafe/safe_network/blob/main/sn_client/README.md) The client APIs
allowing use of the SafeNetwork to users and developers.
- [Registers](https://github.com/maidsafe/safe_network/blob/main/sn_registers/README.md) The CRDT
Expand Down
94 changes: 94 additions & 0 deletions resources/scripts/make-wallets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/bin/bash

# Function to print a message in a box
print_in_box() {
local message="$1"
local border_length=$((${#message} + 4))
local border=$(printf '%*s' "$border_length" | tr ' ' '#')

echo "$border"
echo "# $message #"
echo "$border"
}

# Check for correct number of arguments
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <number_of_wallets> <amount_per_wallet>"
exit 1
fi

# Get the input arguments
NUM_WALLETS=$1
AMOUNT_PER_WALLET=$2

# Define directories relative to the current working directory
APP_DIR=$(pwd)
CLIENT_DIR="${APP_DIR}/client"
NEW_CLIENT_DIR_TEMPLATE="${APP_DIR}/client_"

# Initialize an array to store the wallet details
declare -a WALLETS

# Loop to create and fund wallets
for ((i=1; i<=NUM_WALLETS; i++)); do
# Step 1: Create a new wallet address and capture the new address
safe wallet address
start_address=$(safe wallet address | awk 'NR==3')

# Step 2: Extract the first 5 characters of the wallet address
start_prefix=$(echo "$start_address" | cut -c1-5)

# Step 3: Define the new client directory names
SOURCE_CLIENT_DIR="${NEW_CLIENT_DIR_TEMPLATE}${start_prefix}"

# Step 4: Rename the original client directory to client_<5PK>
mv "$CLIENT_DIR" "$SOURCE_CLIENT_DIR"
print_in_box "Moving source client to $SOURCE_CLIENT_DIR"

# Step 5: Create a new wallet address and capture the new address
safe wallet address
new_address=$(safe wallet address | awk 'NR==3')
new_prefix=$(echo "$new_address" | cut -c1-5)
NEW_CLIENT_DIR="${NEW_CLIENT_DIR_TEMPLATE}${new_prefix}"

print_in_box "Moving new client to $NEW_CLIENT_DIR"

# Step 6: Move the new client
mv "$CLIENT_DIR" "$NEW_CLIENT_DIR"

# Step 7: Restore the original client directory
mv "$SOURCE_CLIENT_DIR" "$CLIENT_DIR"

# Step 8: Send tokens to the new address
echo "New address is: $new_address"
transfer_output=$(safe wallet send "$AMOUNT_PER_WALLET" "$new_address")
echo "Transfer output is: $transfer_output"

# Extract the transfer note
transfer_note=$(echo "$transfer_output" | awk '/Please share this to the recipient:/{getline; getline; print}')

echo "Transfer note is: $transfer_note"

# Store the wallet details in the array
WALLETS+=("Wallet: client_${new_prefix}, Address: $new_address")

# Step 9: Rename current client directory to client_<5PK>
mv "$CLIENT_DIR" "$SOURCE_CLIENT_DIR"

# Step 10: Restore the client_<5PK> to client
mv "$NEW_CLIENT_DIR" "$CLIENT_DIR"

# Step 11: Receive the funds using the transfer note
safe wallet receive "$transfer_note"

# Step 12: Rename the client directories back to their original names
mv "$CLIENT_DIR" "$NEW_CLIENT_DIR"
mv "$SOURCE_CLIENT_DIR" "$CLIENT_DIR"
done

# Print the summary of wallets created
print_in_box "Summary of Wallets Created"
for wallet in "${WALLETS[@]}"; do
echo "$wallet"
done
print_in_box "End of Summary"
48 changes: 48 additions & 0 deletions resources/scripts/upload-random-data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

# Function to check if the `safe` command exists, and install it if not
check_and_install_safe() {
if ! command -v safe &> /dev/null; then
echo "'safe' command not found. Installing..."
curl -sSL https://raw.githubusercontent.com/maidsafe/safeup/main/install.sh | sudo bash
safeup client
else
echo "'safe' command is already installed."
fi
}

# Function to generate a 10MB file of random data
generate_random_data_file() {
tmpfile=$(mktemp)
dd if=/dev/urandom of="$tmpfile" bs=10M count=1 iflag=fullblock &> /dev/null

echo "Generated random data file at $tmpfile"

# Upload the random data file using SAFE CLI
safe files upload "$tmpfile"
# cat $tmpfile
if [ $? -eq 0 ]; then
echo "Successfully uploaded $tmpfile using SAFE CLI"
else
echo "Failed to upload $tmpfile using SAFE CLI"
fi

# Remove the temporary file
rm "$tmpfile"

# Log and sleep for 60 seconds
echo "Sleeping for 60 seconds..."
sleep 60
}

# Check and install 'safe' if necessary
check_and_install_safe

# Example usage
total_files=100 # Total number of files to generate and upload

# Loop to generate and upload random data files
for i in $(seq 1 $total_files); do
echo "Generating and uploading file $i of $total_files..."
generate_random_data_file
done

0 comments on commit d68f6f0

Please sign in to comment.