/ tools / approve-dev-transaction.sh
approve-dev-transaction.sh
 1  #!/usr/bin/env bash
 2  
 3  port=10391
 4  if [ $# -gt 0 -a "$1" = "-t" ]; then
 5  	port=60391
 6  fi
 7  
 8  printf "Searching for auto-update transactions to approve...\n";
 9  
10  tx=$( curl --silent --url "http://localhost:${port}/transactions/search?txGroupId=1&txType=ADD_GROUP_ADMIN&txType=REMOVE_GROUP_ADMIN&confirmationStatus=CONFIRMED&limit=1&reverse=true" );
11  if fgrep --silent '"approvalStatus":"PENDING"' <<< "${tx}"; then
12  	true
13  else
14  	echo "Can't find any pending transactions"
15  	exit
16  fi
17  
18  sig=$( perl -n -e 'print $1 if m/"signature":"(\w+)"/' <<< "${tx}" )
19  if [ -z "${sig}" ]; then
20  	printf "Can't find transaction signature in JSON:\n%s\n" "${tx}"
21  	exit
22  fi
23  
24  printf "Found transaction %s\n" $sig;
25  
26  printf "\nPaste your dev account private key:\n";
27  IFS=
28  read -s privkey
29  printf "\n"
30  
31  # Convert to public key
32  pubkey=$( curl --silent --url "http://localhost:${port}/utils/publickey" --data @- <<< "${privkey}" );
33  if egrep -v --silent '^\w{44,46}$' <<< "${pubkey}"; then
34  	printf "Invalid response from API - was your private key correct?\n%s\n" "${pubkey}"
35  	exit
36  fi
37  printf "Your public key: %s\n" ${pubkey}
38  
39  # Convert to address
40  address=$( curl --silent --url "http://localhost:${port}/addresses/convert/${pubkey}" );
41  printf "Your address: %s\n" ${address}
42  
43  # Grab last reference
44  lastref=$( curl --silent --url "http://localhost:${port}/addresses/lastreference/{$address}" );
45  printf "Your last reference: %s\n" ${lastref}
46  
47  # Build GROUP_APPROVAL transaction
48  timestamp=$( date +%s )000
49  tx_json=$( cat <<TX_END
50  {
51    "timestamp": ${timestamp},
52    "reference": "${lastref}",
53    "fee": 0.01,
54    "txGroupId": 0,
55    "adminPublicKey": "${pubkey}",
56    "pendingSignature": "${sig}",
57    "approval": true
58  }
59  TX_END
60  )
61  
62  raw_tx=$( curl --silent --header "Content-Type: application/json" --url "http://localhost:${port}/groups/approval" --data @- <<< "${tx_json}" )
63  if egrep -v --silent '^\w{100,}' <<< "${raw_tx}"; then
64  	printf "Building GROUP_APPROVAL transaction failed:\n%s\n" "${raw_tx}"
65  	exit
66  fi
67  printf "\nRaw approval tx:\n%s\n" ${raw_tx}
68  
69  # sign
70  sign_json=$( cat <<SIGN_END
71  {
72    "privateKey": "${privkey}",
73    "transactionBytes": "${raw_tx}"
74  }
75  SIGN_END
76  )
77  signed_tx=$( curl --silent --header "Content-Type: application/json" --url "http://localhost:${port}/transactions/sign" --data @- <<< "${sign_json}" )
78  printf "\nSigned tx:\n%s\n" ${signed_tx}
79  if egrep -v --silent '^\w{100,}' <<< "${signed_tx}"; then
80  	printf "Signing GROUP_APPROVAL transaction failed:\n%s\n" "${signed_tx}"
81  	exit
82  fi
83  
84  # ready to publish?
85  plural="s"
86  printf "\n"
87  for ((seconds = 5; seconds > 0; seconds--)); do
88  	if [ "${seconds}" = "1" ]; then
89  		plural=""
90  	fi
91  	printf "\rBroadcasting in %d second%s...(CTRL-C) to abort " $seconds $plural
92  	sleep 1
93  done
94  
95  printf "\rBroadcasting signed GROUP_APPROVAL transaction...      \n"
96  result=$( curl --silent --url "http://localhost:${port}/transactions/process" --data @- <<< "${signed_tx}" )
97  printf "API response:\n%s\n" "${result}"