-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathdev-common.sh
More file actions
145 lines (132 loc) · 5.9 KB
/
Copy pathdev-common.sh
File metadata and controls
145 lines (132 loc) · 5.9 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
#!/usr/bin/env bash
#
# dev-common.sh — helpers specific to the NEAR One dev clusters (source, don't
# run). Generic helpers live in ../common.sh.
#
# MPC_SIGN_WITH: use sign-with-legacy-keychain when the keychain can't find
# a key written to ~/.near-credentials.
#
SIGN_WITH="${MPC_SIGN_WITH:-sign-with-keychain}"
# Sets CONTRACT, NEAR_NET, MEMBER_ACCOUNTS, SIGN_DEPOSIT, PROPOSE_DEPOSIT and
# re-points endpoint vars from per-cluster exports (NOMAD_ADDR_DEV_TESTNET, ...)
# so the network choice drives every step; addresses stay out of this repo.
resolve_dev_cluster() {
local suffix var
case "$1" in
testnet)
CONTRACT="mpc-dev-contract.testnet" NEAR_NET="testnet" SIGN_DEPOSIT="1 NEAR"
MEMBER_ACCOUNTS="mpc-node-0-mpc-dev.testnet mpc-node-1-mpc-dev.testnet"
suffix="TESTNET" ;;
mainnet)
CONTRACT="dev-contract.near" NEAR_NET="mainnet" SIGN_DEPOSIT="0.1 NEAR"
MEMBER_ACCOUNTS="mpc-0-dev-mainnet.dev-signer.near mpc-1-dev-mainnet.dev-signer.near"
suffix="MAINNET" ;;
*) die "Unknown dev cluster '$1' (expected testnet|mainnet)." ;;
esac
# Over propose_update_required_deposit_yoctonear; excess is refunded.
# Read by upgrade-dev-contract.sh.
PROPOSE_DEPOSIT="16 NEAR"
var="NOMAD_ADDR_DEV_${suffix}"; [[ -z "${!var:-}" ]] || export NOMAD_ADDR="${!var}"
var="MPC_NODE_ADDRS_DEV_${suffix}"; [[ -z "${!var:-}" ]] || export MPC_NODE_ADDRS="${!var}"
# +set: an intentionally empty value still disables the prompt.
var="NOMAD_HTTP_AUTH_DEV_${suffix}"; [[ -z "${!var+set}" ]] || export NOMAD_HTTP_AUTH="${!var}"
}
# Typed in per run; the matching NOMAD_*_DEV_<NET> export skips the prompt.
# Takes the bare IP — scheme and API path are the script's business.
prompt_nomad_ip() {
local label=${1:-target} input scheme
while [[ -z "${NOMAD_ADDR:-}" ]]; do
read -rp "Nomad IP address for the ${label} dev cluster: " input
# Tolerate a pasted URL — keeping its scheme, never downgrading TLS.
scheme="http"; [[ "$input" != https://* ]] || scheme="https"
input="${input#http://}"; input="${input#https://}"; input="${input%%/*}"
if [[ ! "$input" =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}(:[0-9]+)?$ ]]; then
echo " Expected an IPv4 address, optionally with a port (e.g. 10.0.0.1 or 10.0.0.1:4646)."
continue
fi
NOMAD_ADDR="${scheme}://${input}"
done
export NOMAD_ADDR
}
prompt_http_auth() {
local user pass
read -rp "Nomad user for ${NOMAD_ADDR} (or user:password, blank for none): " user
if [[ -z "$user" ]]; then
NOMAD_HTTP_AUTH=""
elif [[ "$user" == *:* ]]; then
# Already joined — this form echoes the password to the terminal.
NOMAD_HTTP_AUTH="$user"
else
read -rsp "Nomad password: " pass
echo
NOMAD_HTTP_AUTH="${user}:${pass}"
fi
export NOMAD_HTTP_AUTH
# Basic auth is base64 on the wire — over http:// that is cleartext.
[[ -z "$NOMAD_HTTP_AUTH" || "${NOMAD_ADDR:-}" == https://* ]] \
|| warn "Note: these credentials will be sent over plain HTTP (${NOMAD_ADDR:-})."
}
prompt_node_addrs() {
local input
[[ -z "${MPC_NODE_ADDRS+set}" ]] || return 0
read -rp "Node metrics addresses, space-separated (blank to skip verification): " input
export MPC_NODE_ADDRS="$input"
}
# Whether a credential is configured — never the credential itself.
nomad_auth_state() {
if [[ -z "${NOMAD_HTTP_AUTH+set}" ]]; then echo "(will prompt)"
elif [[ -n "$NOMAD_HTTP_AUTH" ]]; then echo "(set)"
else echo "(none)"; fi
}
# Read-only contract query against the resolved cluster.
near_view() {
run_cmd near contract call-function as-read-only "$CONTRACT" "$1" \
json-args '{}' network-config "$NEAR_NET" now
}
# Check every MPC_NODE_ADDRS node reports release="<version>". Retries per
# node — a node can still be warming up right after its allocation starts.
verify_nodes() {
local version=$1
require_cmds curl
[[ -n "${MPC_NODE_ADDRS:-}" ]] || die "MPC_NODE_ADDRS is not set (e.g. \"host:8080 host:8080\")."
local addr info matched=0 fail=0 try fetch
for addr in ${MPC_NODE_ADDRS}; do
# Internal-only plain HTTP; no TLS endpoint exists.
# nosemgrep: trailofbits.generic.curl-unencrypted-url.curl-unencrypted-url
fetch=(curl -sf --max-time 5 "http://${addr}/metrics")
show_cmd "${fetch[@]}"
info=""
for try in 1 2 3; do
info=$("${fetch[@]}" | grep -o 'mpc_node_build_info{[^}]*}') || info=""
[[ "$info" != *"release=\"${version}\""* ]] || break
if (( try < 3 )); then sleep 5; fi
done
if [[ -z "$info" ]]; then echo " (unreachable)"; fail=1; continue; fi
echo " $info"
if [[ "$info" == *"release=\"${version}\""* ]]; then matched=1; else fail=1; fi
done
if [[ "$fail" -eq 0 && "$matched" -eq 1 ]]; then
ok "All nodes report release=\"${version}\"."
else
warn "Not all nodes are on ${version} yet."
fi
}
# Test signature request against the cluster contract (on-chain txn).
test_sign() {
resolve_dev_cluster "$1"
require_cmds near
local signer=${MEMBER_ACCOUNTS%% *}
local payload='[12,1,2,0,4,5,6,8,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,44]'
local cmd=(near contract call-function as-transaction "$CONTRACT" sign
json-args "{\"request\": {\"payload\": ${payload}, \"path\": \"test\", \"key_version\": 0}}"
prepaid-gas '300.0 Tgas' attached-deposit "$SIGN_DEPOSIT"
sign-as "$signer" network-config "$NEAR_NET" "$SIGN_WITH" send)
echo "Test sign on ${CONTRACT} as ${signer} (deposit ${SIGN_DEPOSIT})."
show_cmd "${cmd[@]}"
confirm "Send it?" || return 0
if "${cmd[@]}"; then
ok "Signature returned — the cluster is signing."
else
warn "Test sign failed — investigate before proceeding."
fi
}