-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaction-create-info.sh
More file actions
executable file
·320 lines (269 loc) · 10.4 KB
/
action-create-info.sh
File metadata and controls
executable file
·320 lines (269 loc) · 10.4 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
#!/bin/bash
##################################################
# Default configuration values
##################################################
# 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." >&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." >&2
exit 1
fi
# Usage message
usage() {
local col=50
echo -e "${UNDERLINE}${BOLD}Create an Info action from a given JSON-LD metadata file${NC}"
echo -e "\n"
echo -e "Syntax:${BOLD} $0 ${GREEN}<jsonld-file> ${NC}[${GREEN}--deposit-return-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${NC}${GRAY}%s${NC}\n" $((col-8)) "[--deposit-return-addr <stake address>]" "- Optional check that metadata deposit return address"
printf " %-*s${GRAY}%s${NC}\n" $col "" "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
deposit_return_address_input=""
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--deposit-return-addr)
if [ -n "${2:-}" ]; then
deposit_return_address_input="$2"
shift 2
else
echo -e "\n${RED}Error: --deposit-return-addr requires a value${NC}\n" >&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 'info_action'.${NC}" >&2
exit 1
fi
# Check if input file exists
if [ ! -f "$input_file" ]; then
echo -e "${RED}Error: Input file not found: $input_file${NC}" >&2
exit 1
fi
echo -e " "
echo -e "${YELLOW}Creating an Info governance action from a given metadata file${NC}"
echo -e "${CYAN}This script assumes compliance Intersect's Info 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=$(jq -r '.body.onChain.deposit' "$input_file")
check_field "deposit" "$deposit"
# 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" != "$EXPECTED_DEPOSIT_LOVELACE" ]; then
echo -e "${YELLOW}Warning: body.onChain.deposit = ${BRIGHTWHITE}$deposit${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" = "$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${NC}" >&2
exit 1
fi
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" = "info_action" ]; then
echo "Metadata has correct governance action tag"
else
echo "Metadata does not have the correct governance action tag"
echo "Expected: info_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
# 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
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
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
}
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
echo -e "${GREEN}Automatic validations passed${NC}"
echo -e " "
echo -e "${CYAN}Computing details${NC}"
# 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 info 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
# Create the action
echo -e " "
echo -e "${CYAN}Creating action file...${NC}"
cardano-cli conway governance action create-info \
--$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 \
--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}"