-
Notifications
You must be signed in to change notification settings - Fork 1
/
io
executable file
ยท212 lines (184 loc) ยท 5.22 KB
/
io
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
#/usr/bin/env bash
readonly BASE="peer chaincode invoke -o orderer.example.com:7050"
readonly TLS="--tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
readonly fct=( "totalSupply" "balanceOf" "allowance" "transfer" "approve"
"transferFrom" "getState" "getPublicKey" "listUsers",
"whoOwesMe", "whoOweI")
readonly usage=("io totalSupply"
"io balanceOf [address tokenOwner]"
"io allowance [address tokenOwner] [address spender]"
"io transfer [address to] [uint tokens]"
"io approve [address spender] [uint tokens]"
"io transferFrom [address from] [address to] [uint tokens]"
"io get [key]"
"io publicKey [flag silent]"
"io listUsers"
"io whoOwesMe"
"io whoOweI")
# **************************************************************************** #
# USAGE #
# **************************************************************************** #
function fctUsage {
printf ${fct["$1"]} ""
printf "$2"
echo ${usage["$1"]}
}
function basicUsage {
echo "----------> Usage ๐ <----------"
echo ""
local i=0
local function_number=${#fct[@]}
while [[ ${i} -lt ${function_number} ]]; do
fctUsage ${i} ":\t"
(( i++ ))
done
}
# **************************************************************************** #
# PRIVATE #
# **************************************************************************** #
function get {
if [ $1 ]; then
echo "---------------> Get [$1] ๐ <---------------"
echo ""
$BASE $TLS -C ptwist -n ptwist -c "{\"Args\":[\"get\", \"$1\"]}"
[[ ${?} -ne 0 ]] && exit 2 || exit 0
else
fctUsage 6 ": "
exit 1
fi
}
function totalSupply {
echo "---------------> Total Supply ๐ฐ <---------------"
echo ""
$BASE $TLS -C ptwist -n ptwist -c "{\"Args\":[\"totalSupply\"]}"
[[ ${?} -ne 0 ]] && exit 2 || exit 0
}
function balanceOf {
if [ $1 ]; then
echo "---------------> Balance of $1 ๐ต <---------------"
echo ""
$BASE $TLS -C ptwist -n ptwist -c "{\"Args\":[\"balanceOf\", \"$1\"]}"
[[ ${?} -ne 0 ]] && exit 2 || exit 0
else
fctUsage 1 ": "
exit 1
fi
}
function allowance {
if [ $1 ] && [ $2 ]; then
echo "---------------> Allowance from $1 to $2 ๐ค <---------------"
echo ""
$BASE $TLS -C ptwist -n ptwist -c "{\"Args\":[\"allowance\", \"$1\", \"$2\"]}"
[[ ${?} -ne 0 ]] && exit 2 || exit 0
else
fctUsage 2 ": "
exit 1
fi
}
function transfer {
if [ $1 ] && [ $2 ]; then
echo "---------------> Transfer to $1 of $2 ๐ฒ <---------------"
echo ""
$BASE $TLS -C ptwist -n ptwist -c "{\"Args\":[\"transfer\", \"$1\", \"$2\"]}"
[[ ${?} -ne 0 ]] && exit 2 || exit 0
else
fctUsage 3 ": "
exit 1
fi
}
function approve {
if [ $1 ] && [ $2 ]; then
echo "---------------> Approve from $1 of $2 ๐ฎ <---------------"
echo ""
$BASE $TLS -C ptwist -n ptwist -c "{\"Args\":[\"approve\", \"$1\", \"$2\"]}"
[[ ${?} -ne 0 ]] && exit 2 || exit 0
else
fctUsage 4 ": "
exit 1
fi
}
function transferFrom {
if [ $1 ] && [ $2 ] && [ $3 ]; then
echo "---------------> TransferFrom from $1 to $2 of $3 ๐ <---------------"
echo ""
$BASE $TLS -C ptwist -n ptwist -c "{\"Args\":[\"transferFrom\", \"$1\", \"$2\", \"$3\"]}"
[[ ${?} -ne 0 ]] && exit 2 || exit 0
else
fctUsage 5 ": "
exit 1
fi
}
function getPublicKey {
if [[ -n $1 ]]; then
if [[ $1 != "-s" ]] && [[ $1 != "--silent" ]]; then
fctUsage 7 ": "
fi
else
printf -- "---------------> Public key ๐ <---------------\n\n"
fi
# set "nullglob" shell option
# globbing which does not match will result as empty string
shopt -s nullglob
publicKey=$(openssl ec \
-in "${CORE_PEER_MSPCONFIGPATH}/keystore/"* \
-pubout 2>&- \
| tail -n 3 \
| head -n 2 \
| tr -d '\n')
if [[ ${#publicKey} -gt 0 ]]; then
echo "${publicKey}"
else
if [[ -z ${CORE_PEER_MSPCONFIGPATH} ]]; then
printf "error: CORE_PEER_MSPCONFIGPATH is not set.\n" >&2
else
printf "error: dammaged or non-existent msp config file in [%s]\n" \
"${CORE_PEER_MSPCONFIGPATH}"
fi
exit 2
fi
shopt -u nullglob
}
function listUsers {
printf -- "---------------> List users ๐ฉ๐ฆ๐ฉ๐ฆ <---------------\n\n"
$BASE $TLS -C ptwist -n ptwist -c "{\"Args\":[\"listUsers\"]}"
[[ ${?} -ne 0 ]] && exit 2 || exit 0
}
function whoOwesMe {
printf -- "---------------> Who owes me ๐ค <---------------\n\n"
$BASE $TLS -C ptwist -n ptwist -c "{\"Args\":[\"whoOwesMe\"]}"
[[ ${?} -ne 0 ]] && exit 2 || exit 0
}
function whoOweI {
printf -- "---------------> Who owe I ๐ <---------------\n\n"
$BASE $TLS -C ptwist -n ptwist -c "{\"Args\":[\"whoOweI\"]}"
[[ ${?} -ne 0 ]] && exit 2 || exit 0
}
# **************************************************************************** #
# PUBLIC #
# **************************************************************************** #
case $1 in
get)
get $2 ;;
totalSupply)
totalSupply ;;
balanceOf)
balanceOf $2 ;;
allowance)
allowance $2 $3 ;;
transfer)
transfer $2 $3 ;;
approve)
approve $2 $3 $4 ;;
transferFrom)
transferFrom $2 $3 $4 ;;
publicKey)
getPublicKey $2;;
listUsers)
listUsers ;;
whoOwesMe)
whoOwesMe ;;
whoOweI)
whoOweI ;;
*)
basicUsage ;;
esac 2>&1