-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathsetup_identities.sh
More file actions
executable file
·131 lines (118 loc) · 4.35 KB
/
setup_identities.sh
File metadata and controls
executable file
·131 lines (118 loc) · 4.35 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
#!/bin/bash
set -euo pipefail
# Number of nodes to create (default is 3)
NUM_NODES=3
echo "🚀 Setting up Node Identities..."
# Preconditions
command -v mpcium-cli >/dev/null 2>&1 || { echo "❌ mpcium-cli not found in PATH"; exit 1; }
[ -f config.yaml ] || { echo "❌ config.yaml not found in repo root"; exit 1; }
# Check if peers.json exists, if not provide helpful instructions
if [ ! -f peers.json ]; then
echo "❌ peers.json not found in repo root"
echo ""
echo "📝 Please generate peers.json first by running:"
echo " mpcium-cli generate-peers -n $NUM_NODES"
echo ""
echo "This will create a peers.json file with $NUM_NODES peer nodes."
exit 1
fi
# Create node directories and copy config files
echo "📁 Creating node directories..."
for i in $(seq 0 $((NUM_NODES-1))); do
mkdir -p "node$i/identity"
if [ ! -f "node$i/config.yaml" ]; then
cp config.yaml "node$i/"
fi
if [ ! -f "node$i/peers.json" ]; then
cp peers.json "node$i/"
fi
done
# Generate identity for each node
echo "🔑 Generating identities for each node..."
for i in $(seq 0 $((NUM_NODES-1))); do
echo "📝 Generating identity for node$i..."
( cd "node$i" && mpcium-cli generate-identity --node "node$i" )
done
# Generate a single chain_code if not present and set it in configs
if [ ! -f .chain_code ]; then
echo "🔐 Generating chain_code (32-byte hex) ..."
CC=$(openssl rand -hex 32)
echo "$CC" > .chain_code
else
CC=$(cat .chain_code)
fi
if [ -z "$CC" ]; then
echo "❌ Failed to determine chain_code"
exit 1
fi
echo "📝 Setting chain_code in root config.yaml ..."
if grep -q '^\s*chain_code:' config.yaml; then
if [[ "${OSTYPE:-}" == darwin* ]]; then
sed -i '' -E "s|^([[:space:]]*chain_code:).*|\\1 \"$CC\"|" config.yaml
else
sed -i -E "s|^([[:space:]]*chain_code:).*|\1 \"$CC\"|" config.yaml
fi
else
printf '\nchain_code: "%s"\n' "$CC" >> config.yaml
fi
echo "📦 Distributing chain_code to node configs ..."
for i in $(seq 0 $((NUM_NODES-1))); do
if grep -q '^\s*chain_code:' "node$i/config.yaml"; then
if [[ "${OSTYPE:-}" == darwin* ]]; then
sed -i '' -E "s|^([[:space:]]*chain_code:).*|\\1 \"$CC\"|" config.yaml
else
sed -i -E "s|^([[:space:]]*chain_code:).*|\1 \"$CC\"|" config.yaml
fi
else
printf '\nchain_code: "%s"\n' "$CC" >> "node$i/config.yaml"
fi
done
# Distribute event_initiator_pubkey to all node configs
if [ -f "event_initiator.identity.json" ]; then
INITIATOR_PUBKEY=$(grep -o '"public_key": *"[^"]*"' event_initiator.identity.json | cut -d '"' -f4)
if [ -n "${INITIATOR_PUBKEY}" ]; then
echo "📦 Distributing event_initiator_pubkey to node configs ..."
for i in $(seq 0 $((NUM_NODES-1))); do
if grep -q '^\s*event_initiator_pubkey:' "node$i/config.yaml"; then
if [[ "${OSTYPE:-}" == darwin* ]]; then
sed -i '' -E "s|^([[:space:]]*event_initiator_pubkey:).*|\1 \"${INITIATOR_PUBKEY}\"|" "node$i/config.yaml"
else
sed -i -E "s|^([[:space:]]*event_initiator_pubkey:).*|\1 \"${INITIATOR_PUBKEY}\"|" "node$i/config.yaml"
fi
fi
done
fi
fi
# Distribute identity files to all nodes
echo "🔄 Distributing identity files across nodes..."
for i in $(seq 0 $((NUM_NODES-1))); do
src="node$i/identity/node${i}_identity.json"
[ -f "$src" ] || { echo "❌ Missing identity file for node$i at $src"; exit 1; }
for j in $(seq 0 $((NUM_NODES-1))); do
if [ $i != $j ]; then
mkdir -p "node$j/identity"
echo "📋 Copying node${i}_identity.json to node$j..."
cp -f "$src" "node$j/identity/"
fi
done
done
echo "✨ Node identities setup complete!"
echo
echo "📂 Created folder structure:"
echo "├── node0"
echo "│ ├── config.yaml"
echo "│ ├── identity/"
echo "│ └── peers.json"
echo "├── node1"
echo "│ ├── config.yaml"
echo "│ ├── identity/"
echo "│ └── peers.json"
echo "└── node2"
echo " ├── config.yaml"
echo " ├── identity/"
echo " └── peers.json"
echo
echo "✅ You can now start your nodes with:"
echo "cd node0 && mpcium start -n node0"
echo "cd node1 && mpcium start -n node1"
echo "cd node2 && mpcium start -n node2"