-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaction-create-tw.sh
More file actions
executable file
·471 lines (401 loc) · 16.3 KB
/
action-create-tw.sh
File metadata and controls
executable file
·471 lines (401 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
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
43
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
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
236
237
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/bin/bash
##################################################
# Default configuration values
WITHDRAW_TO_SCRIPT="true"
##################################################
# Exit immediately if a command exits with a non-zero status,
# treat unset variables as an error, and fail if any command in a pipeline fails
set -euo pipefail
# Colors
#BLACK='\033[0;30m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BRIGHTWHITE='\033[0;37;1m'
NC='\033[0m'
UNDERLINE='\033[4m'
BOLD='\033[1m'
GRAY='\033[0;90m'
# Check if cardano-cli is installed
if ! command -v cardano-cli >/dev/null 2>&1; then
echo -e "${RED}Error: cardano-cli is not installed or not in your PATH.${NC}" >&2
exit 1
fi
# Check if ipfs cli is installed
if ! command -v ipfs >/dev/null 2>&1; then
echo -e "${RED}Error: ipfs cli is not installed or not in your PATH.${NC}" >&2
exit 1
fi
# Usage message
usage() {
local col=50
echo -e "${UNDERLINE}${BOLD}Create a Treasury Withdrawal action from a given JSON-LD metadata file${NC}"
echo -e "\n"
echo -e "Syntax:${BOLD} $0 ${GREEN}<jsonld-file> ${NC}[${GREEN}--withdraw-to-key${NC}] [${GREEN}--deposit-return-addr ${NC}<stake address>] [${GREEN}--withdrawal-addr ${NC}<stake address>]"
printf "Params: ${GREEN}%-*s${GRAY}%s${NC}\n" $((col-8)) "<jsonld-file>" "- Path to the JSON-LD metadata file"
printf " ${GREEN}%-*s${GRAY}%s${NC}\n" $((col-8)) "[--deposit-return-addr <stake address>]" "- Check that metadata deposit return address matches provided one (Bech32)"
printf " ${GREEN}%-*s${GRAY}%s${NC}\n" $((col-8)) "[--withdraw-to-key]" "- Allow withdrawal address to be key-based (default is script-based)"
printf " ${GREEN}%-*s${GRAY}%s${NC}\n" $((col-8)) "[--withdrawal-addr <stake address>]" "- Check that metadata withdrawal address matches provided one (Bech32)"
printf " ${GREEN}%-*s${GRAY}%s${NC}\n" $((col-8)) "-h, --help" "- Show this help message and exit"
exit 1
}
# Initialize variables with defaults
input_file=""
# Optional variables
withdraw_to_script="$WITHDRAW_TO_SCRIPT"
deposit_return_address_input=""
withdrawal_address_input=""
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--withdraw-to-key)
withdraw_to_script="false"
shift
;;
--deposit-return-addr)
if [ -n "${2:-}" ]; then
deposit_return_address_input="$2"
shift 2
else
echo -e "${RED}Error: --deposit-return-addr requires a value${NC}" >&2
usage
fi
;;
--withdrawal-addr)
if [ -n "${2:-}" ]; then
withdrawal_address_input="$2"
shift 2
else
echo -e "${RED}Error: --withdrawal-addr requires a value${NC}" >&2
usage
fi
;;
-h|--help)
usage
;;
*)
if [ -z "$input_file" ]; then
input_file="$1"
else
echo -e "${RED}Error: Input file already specified. Unexpected argument: $1${NC}" >&2
usage
fi
shift
;;
esac
done
# If no input file provided, show usage
if [ -z "$input_file" ]; then
echo -e "${RED}Error: No input file specified${NC}" >&2
usage
fi
# Enforce .jsonld extension
if [[ "$input_file" != *.jsonld ]]; then
echo -e "${RED}Error: Input file '${YELLOW}$input_file${RED}' must be a JSON-LD metadata file with a ${YELLOW}.jsonld${RED} extension.${NC}" >&2
echo -e "${YELLOW}This script expects a CIP-108 metadata document whose body.onChain.gov_action.tag is 'treasury_withdrawals_action'.${NC}" >&2
exit 1
fi
# Ensure the input file actually exists
if [ ! -f "$input_file" ]; then
echo -e "${RED}Error: Input file '${YELLOW}$input_file${RED}' not found.${NC}" >&2
exit 1
fi
# If deposit return addr is not provided, show usage
if [ -z "$deposit_return_address_input" ]; then
echo -e "${RED}Error: --deposit-return-addr is required${NC}" >&2
usage
fi
# If withdrawal addr is not provided, show usage
if [ -z "$withdrawal_address_input" ]; then
echo -e "${RED}Error: --withdrawal-addr is required${NC}" >&2
usage
fi
echo -e " "
echo -e "${YELLOW}Creating a treasury withdrawal governance action from a given metadata file${NC}"
echo -e "${CYAN}This script assumes compliance Intersect's treasury withdrawal action schema${NC}"
echo -e "${CYAN}This script assumes that CARDANO_NODE_SOCKET_PATH, CARDANO_NODE_NETWORK_ID and IPFS_GATEWAY_URI are set${NC}"
# Exit if socket path is not set
if [ -z "$CARDANO_NODE_SOCKET_PATH" ]; then
echo "Error: Cardano node $CARDANO_NODE_SOCKET_PATH environment variable is not set." >&2
exit 1
fi
# Exit if network id is not set
if [ -z "$CARDANO_NODE_NETWORK_ID" ]; then
echo "Error: Cardano node $CARDANO_NODE_NETWORK_ID environment variable is not set." >&2
fi
# Get if mainnet or testnet
if [ "$CARDANO_NODE_NETWORK_ID" = "764824073" ] || [ "$CARDANO_NODE_NETWORK_ID" = "mainnet" ]; then
echo -e "${YELLOW}Local node is using mainnet${NC}"
protocol_magic="mainnet"
else
echo -e "${YELLOW}Local node is using a testnet${NC}"
protocol_magic="testnet"
fi
# Open the provided metadata file
# Do some basic validation checks on metadata
echo -e " "
echo -e "${CYAN}Doing some basic validation and checks on metadata${NC}"
# Function to check if jq query returned null or empty
check_field() {
local field_name="$1"
local field_value="$2"
if [ -z "$field_value" ] || [ "$field_value" = "null" ]; then
echo -e "${RED}Error: Required field '$field_name' not found in metadata${NC}" >&2
exit 1
fi
}
# Extract and validate required fields
title=$(jq -r '.body.title' "$input_file")
check_field "title" "$title"
ga_type=$(jq -r '.body.onChain.gov_action.tag' "$input_file")
check_field "tag" "$ga_type"
deposit_return=$(jq -r '.body.onChain.reward_account' "$input_file")
check_field "reward_account" "$deposit_return"
deposit_amount=$(jq -r '.body.onChain.deposit' "$input_file")
check_field "deposit" "$deposit_amount"
# Sanity-check the deposit magnitude. The current Cardano governance action
# deposit is 100,000 ada = 100_000_000_000 lovelace.
EXPECTED_DEPOSIT_LOVELACE="100000000000"
if [ "$deposit_amount" != "$EXPECTED_DEPOSIT_LOVELACE" ]; then
echo -e "${YELLOW}Warning: body.onChain.deposit = ${BRIGHTWHITE}$deposit_amount${YELLOW} lovelace, expected ${BRIGHTWHITE}$EXPECTED_DEPOSIT_LOVELACE${YELLOW} (100,000 ADA, the current governance action deposit). Verify this is intentional before submitting.${NC}" >&2
fi
# Authoritative deposit check against the live protocol parameter
echo "Checking that deposit matches the current protocol parameter"
onchain_deposit=$(cardano-cli conway query protocol-parameters | jq -r '.govActionDeposit')
if [ "$deposit_amount" = "$onchain_deposit" ]; then
echo -e "${GREEN}Metadata has expected deposit amount${NC}"
else
echo -e "${RED}Metadata does not have expected deposit amount${NC}" >&2
echo -e "${RED}Expected: $onchain_deposit found: $deposit_amount${NC}" >&2
exit 1
fi
withdrawal_list=$(jq -r '.body.onChain.gov_action.rewards' "$input_file")
check_field "rewards" "$withdrawal_list"
# todo: support multiple withdrawals
withdrawal_address=$(jq -r '.body.onChain.gov_action.rewards[0].key' "$input_file")
check_field "key" "$withdrawal_address"
withdrawal_amount=$(jq -r '.body.onChain.gov_action.rewards[0].value' "$input_file")
check_field "value" "$withdrawal_amount"
authors=$(jq -r '.authors' "$input_file")
check_field "authors" "$authors"
witness=$(jq -r '.authors[0].witness' "$input_file")
check_field "witness" "$witness"
if [ "$ga_type" = "treasury_withdrawals_action" ]; then
echo "Metadata has correct governanceActionType"
else
echo "Metadata does not have the correct governanceActionType"
echo "Expected: treasury_withdrawals_action found: $ga_type"
exit 1
fi
# if return address passed in check against metadata
if [ ! -z "$deposit_return_address_input" ]; then
echo "Deposit return address provided"
echo "Comparing provided address to metadata"
if [ "$deposit_return_address_input" = "$deposit_return" ]; then
echo -e "${GREEN}Metadata has expected deposit return address${NC}"
else
echo -e "${RED}Metadata does not have expected deposit return address${NC}"
exit 1
fi
fi
# check if withdrawal address is provided
if [ ! -z "$withdrawal_address_input" ]; then
echo "Withdrawal address provided"
echo "Comparing provided address to metadata"
if [ "$withdrawal_address_input" = "$withdrawal_address" ]; then
echo -e "${GREEN}Metadata has expected withdrawal address${NC}"
else
echo -e "${RED}Metadata does not have expected withdrawal address${NC}"
exit 1
fi
fi
# Verify bech32 integrity (checksum + address type) of stake address
validate_stake_address() {
local label="$1"
local address="$2"
local info
if ! info=$(cardano-cli address info --address "$address" 2>&1); then
echo -e "${RED}Error: $label is not a valid bech32 address: ${YELLOW}$address${NC}" >&2
echo -e "${GRAY}cardano-cli rejected it: $info${NC}" >&2
exit 1
fi
local addr_type
addr_type=$(echo "$info" | jq -r '.type // ""')
if [ "$addr_type" != "stake" ]; then
echo -e "${RED}Error: $label is bech32-valid but is not a stake address (type=${YELLOW}${addr_type:-unknown}${RED}): ${YELLOW}$address${NC}" >&2
exit 1
fi
}
validate_stake_address "metadata body.onChain.reward_account" "$deposit_return"
validate_stake_address "metadata body.onChain.gov_action.rewards[0].key" "$withdrawal_address"
# use bech32 prefix to determine if addresses are mainnet or testnet
is_stake_address_mainnet() {
local address="$1"
# Check if address starts with stake1 (mainnet)
if [[ "$address" =~ ^stake1 ]]; then
return 0
# Check if address starts with stake_test1 (testnet)
elif [[ "$address" =~ ^stake_test1 ]]; then
return 1
else
echo -e "${RED}Error: Invalid stake address format: $address${NC}" >&2
exit 1
fi
}
# if mainnet node then expect addresses to be mainnet
if [ "$protocol_magic" = "mainnet" ]; then
if is_stake_address_mainnet "$deposit_return"; then
echo -e "Deposit return address is a valid mainnet stake address"
else
echo -e "${RED}Deposit return address is not a valid mainnet stake address${NC}"
exit 1
fi
if is_stake_address_mainnet "$withdrawal_address"; then
echo -e "Withdrawal address is a valid mainnet stake address"
else
echo -e "${RED}Withdrawal address is not a valid mainnet stake address${NC}"
exit 1
fi
else
if ! is_stake_address_mainnet "$deposit_return"; then
echo -e "Deposit return address is a valid testnet stake address"
else
echo -e "${RED}Deposit return address is not a valid testnet stake address${NC}"
exit 1
fi
if ! is_stake_address_mainnet "$withdrawal_address"; then
echo -e "Withdrawal address is a valid testnet stake address"
else
echo -e "${RED}Withdrawal address is not a valid testnet stake address${NC}"
exit 1
fi
fi
# use header byte to determine if stake address is script-based or key-based
is_stake_address_script() {
local address="$1"
address_hex=$(cardano-cli address info --address "$address"| jq -r ".base16")
first_char="${address_hex:0:1}"
if [ "$first_char" = "f" ]; then
return 0 # true
elif [ "$first_char" = "e" ]; then
return 1 # false
else
echo -e "${RED}Error: Invalid stake address header byte${NC}" >&2
exit 1
fi
}
withdraw_addr_script_check=$(is_stake_address_script "$withdrawal_address" && echo "true" || echo "false")
if [ "$withdraw_addr_script_check" = "$withdraw_to_script" ]; then
if [ "$withdraw_to_script" = "true" ]; then
echo -e "Withdrawal address is script-based, as expected"
else
echo -e "Withdrawal address is key-based, as expected"
fi
else
expected_type="script-based"
[ "$withdraw_to_script" = "false" ] && expected_type="key-based"
echo -e "${RED}Withdrawal address type does not match expectation ($expected_type), exiting.${NC}"
exit 1
fi
is_stake_address_registered(){
local address="$1"
stake_address_deposit=$(cardano-cli conway query stake-address-info --address "$address" | jq -r '.[0].stakeRegistrationDeposit')
if [ "$stake_address_deposit" != "null" ]; then
return 0
else
return 1
fi
}
# check if stake addresses are registered
if is_stake_address_registered "$deposit_return"; then
echo -e "Deposit return stake address is registered"
else
echo -e "${RED}Deposit return stake address is not registered, exiting.${NC}"
exit 1
fi
if is_stake_address_registered "$withdrawal_address"; then
echo -e "Withdrawal stake address is registered"
else
echo -e "${RED}Withdrawal stake address is not registered, exiting.${NC}"
exit 1
fi
is_stake_address_delegated_to_abstain_or_null() {
local address="$1"
vote_delegation=$(cardano-cli conway query stake-address-info --address "$address" | jq -r '.[0].voteDelegation')
if [ "$vote_delegation" = "alwaysAbstain" ] || [ "$vote_delegation" = "null" ] ; then
return 0
else
return 1
fi
}
if is_stake_address_delegated_to_abstain_or_null "$withdrawal_address"; then
echo -e "Withdrawal stake address is delegated to always abstain or not delegated at all"
else
echo -e "${RED}Withdrawal stake address is delegated to something other than abstain, exiting.${NC}"
exit 1
fi
# todo add check if withdrawal address is delegated to an SPO
echo -e "${GREEN}Automatic validations passed${NC}"
echo -e " "
echo -e "${CYAN}Computing details${NC}"
# compute if provided addresses are script-based or key-based
# we should warn the user if they are key-based
# Compute the hash and IPFS URI
file_hash=$(b2sum -l 256 "$input_file" | awk '{print $1}')
echo -e "Metadata file hash: ${YELLOW}$file_hash${NC}"
ipfs_cid=$(ipfs add -Q --cid-version 1 "$input_file")
echo -e "IPFS URI: ${YELLOW}ipfs://$ipfs_cid${NC}"
# Make user manually confirm the choices
echo -e " "
echo -e "${CYAN}Creating treasury withdrawal action${NC}"
echo -e "Title: ${YELLOW}$title${NC}"
echo -e " "
echo -e "Deposit return address: ${YELLOW}$deposit_return${NC}"
if is_stake_address_script "$deposit_return"; then
echo -e "(this is a script-based address)"
else
echo -e "(this is a key-based address)"
fi
echo -e " "
read -p "Do you want to proceed with this deposit return address? (yes/no): " confirm_deposit
if [ "$confirm_deposit" != "yes" ]; then
echo -e "${RED}Deposit address not confirmed by user, exiting.${NC}"
exit 1
fi
echo -e " "
echo -e "Withdrawal address: ${YELLOW}$withdrawal_address${NC}"
if is_stake_address_script "$withdrawal_address"; then
echo -e "(this is a script-based address)"
else
echo -e "(this is a key-based address)"
fi
ada_amount=$(echo "scale=6; $withdrawal_amount / 1000000" | bc)
ada_amount_formatted=$(printf "%'0.6f" "$ada_amount")
echo -e "Withdrawal amount (ada): ${YELLOW}$ada_amount_formatted${NC}"
echo -e " "
read -p "Do you want to proceed with this withdrawal address and amount? (yes/no): " confirm_withdrawal
if [ "$confirm_withdrawal" != "yes" ]; then
echo -e "${RED}Withdrawal amount or withdrawal address not confirmed by user, exiting.${NC}"
exit 1
fi
# Create the action
echo -e " "
echo -e "${CYAN}Creating action file...${NC}"
cardano-cli conway governance action create-treasury-withdrawal \
--$protocol_magic \
--governance-action-deposit $(cardano-cli conway query gov-state | jq -r '.currentPParams.govActionDeposit') \
--deposit-return-stake-address "$deposit_return" \
--anchor-url "ipfs://$ipfs_cid" \
--anchor-data-hash "$file_hash" \
--check-anchor-data \
--funds-receiving-stake-address "$withdrawal_address" \
--transfer "$withdrawal_amount" \
--constitution-script-hash $(cardano-cli conway query constitution | jq -r '.script') \
--out-file "$input_file.action"
echo -e "${GREEN}Action file created at "$input_file.action" ${NC}"
echo -e " "
echo -e "${CYAN}Creating JSON representation of action file...${NC}"
cardano-cli conway governance action view --action-file "$input_file.action" > "$input_file.action.json"
echo -e "${GREEN}JSON file created at "$input_file.action.json" ${NC}"