|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Test the /auth/login flow and create a profile using the returned JWT. |
| 5 | +# Requirements: curl, node, npm. Installs ethers locally into /tmp by default. |
| 6 | +# |
| 7 | +# Inputs (env): |
| 8 | +# PUBLIC_ADDRESS (required) - wallet address |
| 9 | +# PRIVATE_KEY (required) - wallet private key (0x-prefixed) |
| 10 | +# API_URL (optional) - defaults to http://localhost:3001 |
| 11 | +# PROFILE_NAME (optional) - defaults to "Shell Tester" |
| 12 | +# PROFILE_DESC (optional) - defaults to "Created via script" |
| 13 | +# PROFILE_AVATAR (optional) - defaults to null |
| 14 | + |
| 15 | +API_URL="${API_URL:-http://localhost:3001}" |
| 16 | +ADDRESS="${PUBLIC_ADDRESS:-}" |
| 17 | +PRIVATE_KEY="${PRIVATE_KEY:-}" |
| 18 | + |
| 19 | +# If not provided via env, prompt interactively (input hidden for private key). |
| 20 | +if [[ -z "${ADDRESS}" ]]; then |
| 21 | + read -r -p "Enter PUBLIC_ADDRESS (0x...): " ADDRESS |
| 22 | +fi |
| 23 | +if [[ -z "${PRIVATE_KEY}" ]]; then |
| 24 | + read -r -s -p "Enter PRIVATE_KEY (0x..., hidden): " PRIVATE_KEY |
| 25 | + echo |
| 26 | +fi |
| 27 | +if [[ -z "${ADDRESS}" || -z "${PRIVATE_KEY}" ]]; then |
| 28 | + echo "PUBLIC_ADDRESS and PRIVATE_KEY are required. Aborting." |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +PROFILE_NAME="${PROFILE_NAME:-Shell Tester}" |
| 33 | +PROFILE_DESC="${PROFILE_DESC:-Created via script}" |
| 34 | +PROFILE_AVATAR="${PROFILE_AVATAR:-null}" |
| 35 | + |
| 36 | +# Ensure we have ethers available without polluting the repo. |
| 37 | +TOOLS_DIR="${TOOLS_DIR:-/tmp/theguildgenesis-login}" |
| 38 | +export NODE_PATH="${TOOLS_DIR}/node_modules${NODE_PATH:+:${NODE_PATH}}" |
| 39 | +export PATH="${TOOLS_DIR}/node_modules/.bin:${PATH}" |
| 40 | +if ! node -e "require('ethers')" >/dev/null 2>&1; then |
| 41 | + echo "Installing ethers@6 to ${TOOLS_DIR}..." |
| 42 | + mkdir -p "${TOOLS_DIR}" |
| 43 | + npm install --prefix "${TOOLS_DIR}" ethers@6 >/dev/null |
| 44 | +fi |
| 45 | + |
| 46 | +echo "Fetching nonce for ${ADDRESS}..." |
| 47 | +nonce_resp="$(curl -sS "${API_URL}/auth/nonce/${ADDRESS}")" |
| 48 | +echo "Nonce response: ${nonce_resp}" |
| 49 | +# Parse nonce safely |
| 50 | +nonce="$(RESP="${nonce_resp}" python3 - <<'PY' |
| 51 | +import json, os |
| 52 | +data = json.loads(os.environ["RESP"]) |
| 53 | +print(data["nonce"]) |
| 54 | +PY |
| 55 | +)" |
| 56 | +if [[ -z "${nonce}" ]]; then |
| 57 | + echo "Failed to parse nonce from response" |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | + |
| 61 | +message=$'Sign this message to authenticate with The Guild.\n\nNonce: '"${nonce}" |
| 62 | + |
| 63 | +echo "Signing nonce..." |
| 64 | +signature="$( |
| 65 | + ADDRESS="${ADDRESS}" PRIVATE_KEY="${PRIVATE_KEY}" MESSAGE="${message}" \ |
| 66 | + node - <<'NODE' |
| 67 | +const { Wallet, hashMessage } = require('ethers'); |
| 68 | +
|
| 69 | +const address = process.env.ADDRESS; |
| 70 | +const pk = process.env.PRIVATE_KEY; |
| 71 | +const message = process.env.MESSAGE; |
| 72 | +
|
| 73 | +if (!address || !pk || !message) { |
| 74 | + console.error("Missing ADDRESS, PRIVATE_KEY or MESSAGE"); |
| 75 | + process.exit(1); |
| 76 | +} |
| 77 | +
|
| 78 | +const wallet = new Wallet(pk); |
| 79 | +if (wallet.address.toLowerCase() !== address.toLowerCase()) { |
| 80 | + console.error(`Private key does not match address. Wallet: ${wallet.address}, Provided: ${address}`); |
| 81 | + process.exit(1); |
| 82 | +} |
| 83 | +
|
| 84 | +(async () => { |
| 85 | + const sig = await wallet.signMessage(message); |
| 86 | + console.log(sig); |
| 87 | +})(); |
| 88 | +NODE |
| 89 | +)" |
| 90 | + |
| 91 | +echo "Signature: ${signature}" |
| 92 | + |
| 93 | +echo "Logging in..." |
| 94 | +login_tmp="$(mktemp)" |
| 95 | +http_status="$(curl -sS -o "${login_tmp}" -w "%{http_code}" -X POST \ |
| 96 | + -H "x-eth-address: ${ADDRESS}" \ |
| 97 | + -H "x-eth-signature: ${signature}" \ |
| 98 | + "${API_URL}/auth/login")" |
| 99 | +login_resp="$(cat "${login_tmp}")" |
| 100 | +rm -f "${login_tmp}" |
| 101 | +echo "Login HTTP ${http_status}: ${login_resp}" |
| 102 | +if [[ "${http_status}" != "200" ]]; then |
| 103 | + echo "Login failed with status ${http_status}" |
| 104 | + exit 1 |
| 105 | +fi |
| 106 | +jwt="$(RESP="${login_resp}" python3 - <<'PY' |
| 107 | +import json, os |
| 108 | +data = json.loads(os.environ["RESP"]) |
| 109 | +print(data["token"]) |
| 110 | +PY |
| 111 | +)" |
| 112 | +if [[ -z "${jwt}" ]]; then |
| 113 | + echo "Failed to parse JWT from login response" |
| 114 | + exit 1 |
| 115 | +fi |
| 116 | + |
| 117 | +echo "JWT: ${jwt}" |
| 118 | + |
| 119 | +echo "Creating profile..." |
| 120 | +profile_payload=$(cat <<EOF |
| 121 | +{ |
| 122 | + "name": "${PROFILE_NAME}", |
| 123 | + "description": "${PROFILE_DESC}", |
| 124 | + "avatar_url": ${PROFILE_AVATAR} |
| 125 | +} |
| 126 | +EOF |
| 127 | +) |
| 128 | + |
| 129 | +create_tmp="$(mktemp)" |
| 130 | +create_status="$(curl -sS -o "${create_tmp}" -w "%{http_code}" -X POST \ |
| 131 | + -H "Authorization: Bearer ${jwt}" \ |
| 132 | + -H "Content-Type: application/json" \ |
| 133 | + -d "${profile_payload}" \ |
| 134 | + "${API_URL}/profiles")" |
| 135 | +create_resp="$(cat "${create_tmp}")" |
| 136 | +rm -f "${create_tmp}" |
| 137 | + |
| 138 | +echo "Create profile HTTP ${create_status}: ${create_resp}" |
| 139 | +if [[ "${create_status}" != "201" && "${create_status}" != "200" ]]; then |
| 140 | + echo "Profile creation failed with status ${create_status}" |
| 141 | + exit 1 |
| 142 | +fi |
| 143 | + |
| 144 | +echo "Done." |
| 145 | + |
0 commit comments