main.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "strings"
  7. "github.com/ditcraft/client/config"
  8. "github.com/ditcraft/client/demo"
  9. "github.com/ditcraft/client/ethereum"
  10. "github.com/ditcraft/client/git"
  11. "github.com/ditcraft/client/helpers"
  12. )
  13. // The current dit coordinator address
  14. var liveDitCoodinator = "0x991f901E1Fc151D13ba8C0E27a7f8c6ea3C524Cb"
  15. var demoDitCoodinator = "0x841cA7196C2cf3d9ACBc719fE8Ace4778F0Ecb19"
  16. func main() {
  17. var err error
  18. args := os.Args[1:]
  19. // Loading the config
  20. err = config.Load()
  21. // If no arguments are provided print the usage information
  22. if len(args) == 0 {
  23. printUsage()
  24. }
  25. // Converting everything to lowercase, so that a command will not fail if something
  26. // is capitalized by accident
  27. command := strings.ToLower(args[0])
  28. // If there is an error during the config load and the user is not
  29. // in the process of setting dit up: print an error
  30. if err != nil && command != "setup" {
  31. helpers.PrintLine(err.Error(), 2)
  32. os.Exit(0)
  33. }
  34. if err == nil && command != "setup" {
  35. if (!config.DitConfig.DemoModeActive && config.DitConfig.DitCoordinator != liveDitCoodinator) || (config.DitConfig.DemoModeActive && config.DitConfig.DitCoordinator != demoDitCoodinator) {
  36. helpers.PrintLine("You are using an old version of the deployed ditCoordinator contract", 1)
  37. helpers.PrintLine("To fix this call '"+helpers.ColorizeCommand("setup")+"'", 1)
  38. }
  39. }
  40. switch command {
  41. case "setup":
  42. var ditCoordinatorAddress string
  43. // Create a new config
  44. if len(args) == 2 && strings.Contains(args[1], "live") {
  45. err = config.Create(false)
  46. ditCoordinatorAddress = liveDitCoodinator
  47. } else {
  48. err = config.Create(true)
  49. ditCoordinatorAddress = demoDitCoodinator
  50. }
  51. if err == nil {
  52. // Load the new config
  53. config.Load()
  54. // Set the DitCoordinator
  55. err = ethereum.SetDitCoordinator(ditCoordinatorAddress)
  56. if err == nil {
  57. fmt.Println()
  58. helpers.PrintLine("ditCoordinator automatically set to the current deployed one", 0)
  59. passedKYC, err := ethereum.CheckForKYC()
  60. if err == nil && !passedKYC {
  61. fmt.Println()
  62. helpers.PrintLine("You didn't pass the KYC yet. Please do the KYC now:", 0)
  63. helpers.PrintLine("Go to our Twitter @ditcraft and tweet the following at us:", 0)
  64. helpers.PrintLine("@ditcraft I want to use your client, please verify me "+config.DitConfig.EthereumKeys.Address+"!", 0)
  65. }
  66. }
  67. }
  68. break
  69. case "mode":
  70. checkIfExists(args, 1, "the mode that you want to switch to (live/demo)")
  71. var ditCoordinatorAddress string
  72. if args[1] == "live" {
  73. ditCoordinatorAddress = liveDitCoodinator
  74. config.DitConfig.DemoModeActive = false
  75. } else if args[1] == "demo" {
  76. ditCoordinatorAddress = demoDitCoodinator
  77. config.DitConfig.DemoModeActive = true
  78. } else {
  79. printUsage()
  80. os.Exit(0)
  81. }
  82. err = ethereum.SetDitCoordinator(ditCoordinatorAddress)
  83. if err == nil {
  84. helpers.PrintLine("dit client switched to the "+args[1]+" mode", 0)
  85. if args[1] == "live" {
  86. helpers.PrintLine("You are now using the client in live mode, you will be staking real xDai!", 1)
  87. }
  88. passedKYC, err := ethereum.CheckForKYC()
  89. if err == nil && !passedKYC {
  90. fmt.Println()
  91. helpers.PrintLine("You didn't pass the KYC yet. Please do the KYC now:", 0)
  92. helpers.PrintLine("Go to our Twitter @ditcraft and tweet the following at us:", 0)
  93. helpers.PrintLine("@ditcraft I want to try dit, the decentralized git. Please verify me "+config.DitConfig.EthereumKeys.Address+"!", 0)
  94. }
  95. }
  96. case "set_coordinator":
  97. checkIfExists(args, 1, "the address of the ditCoordinator contract")
  98. // Set the DitCoordinator to a provided address
  99. err = ethereum.SetDitCoordinator(args[1])
  100. if err == nil {
  101. helpers.PrintLine("ditCoordinator successfully set", 0)
  102. }
  103. break
  104. case "get_balance":
  105. // Retrieve the balances in ETH and KNW
  106. err = ethereum.GetBalances()
  107. break
  108. case "get_address":
  109. // Return the ETH address
  110. helpers.PrintLine("Ethereum Address: "+config.DitConfig.EthereumKeys.Address, 0)
  111. helpers.PrintLine("Etherscan-URL: https://rinkeby.etherscan.io/address/"+config.DitConfig.EthereumKeys.Address, 0)
  112. break
  113. case "clone":
  114. checkIfExists(args, 1, "a URL to a repository")
  115. var repositoryName string
  116. // Clone a repository and return the name of it
  117. repositoryName, err = git.Clone(args[1])
  118. if err == nil {
  119. // Initialize this new repository as a ditRepository
  120. err = ethereum.InitDitRepository(repositoryName)
  121. if err == nil {
  122. helpers.PrintLine("ditRepository successfully cloned and initialized", 0)
  123. }
  124. }
  125. break
  126. case "init":
  127. // Initialize the current repository as a ditRepository
  128. err = ethereum.InitDitRepository()
  129. if err == nil {
  130. helpers.PrintLine("ditRepository successfully initiated", 0)
  131. }
  132. break
  133. case "commit", "propose_commit", "demo_commit":
  134. checkIfExists(args, 1, "a commit message")
  135. err = git.Validate()
  136. if err == nil {
  137. var voteDetails string
  138. var proposalID int
  139. err = git.CheckForChanges()
  140. if err == nil {
  141. // Propose a new commit
  142. if config.DitConfig.DemoModeActive {
  143. voteDetails, proposalID, err = demo.ProposeCommit(args[1])
  144. } else {
  145. voteDetails, proposalID, err = ethereum.ProposeCommit(args[1])
  146. }
  147. if err == nil {
  148. // Push the commit into a proposal branch
  149. err = git.Commit(proposalID, args[1])
  150. if err == nil {
  151. // Print the details of the vote
  152. stringLines := strings.Split(voteDetails, "\n")
  153. for i := range stringLines {
  154. helpers.PrintLine(stringLines[i], 0)
  155. }
  156. }
  157. }
  158. }
  159. }
  160. break
  161. case "vote_info":
  162. // Prints the details of a vote
  163. if len(args) == 2 {
  164. voteID, _ := strconv.Atoi(args[1])
  165. err = ethereum.GetVoteInfo(voteID)
  166. } else {
  167. err = ethereum.GetVoteInfo()
  168. }
  169. break
  170. case "vote":
  171. checkIfExists(args, 1, "a proposal ID - dit vote <PROPOSAL_ID> <CHOICE> <SALT>")
  172. checkIfExists(args, 2, "your choice (1/0) - dit vote <PROPOSAL_ID> <CHOICE> <SALT>")
  173. checkIfExists(args, 3, "a salt (a non-zero number) - dit vote <PROPOSAL_ID> <CHOICE> <SALT>")
  174. // Votes on a proposal
  175. err = ethereum.Vote(args[1], args[2], args[3])
  176. break
  177. // case "demo_vote":
  178. // checkIfExists(args, 1, "a proposal ID - dit demo_vote <PROPOSAL_ID>")
  179. // // Demo-Votes on a proposal
  180. // err = demo.Vote(args[1])
  181. // break
  182. case "open", "open_vote", "reveal_vote", "reveal":
  183. checkIfExists(args, 1, "a proposal ID")
  184. // Reveals the vote on a proposal
  185. err = ethereum.Open(args[1])
  186. break
  187. // case "demo_open", "demo_open_vote", "demo_reveal_vote", "demo_reveal":
  188. // checkIfExists(args, 1, "a proposal ID")
  189. // // Reveals the votes of the demo voters on a proposal
  190. // for i := 0; i < 3; i++ {
  191. // err = demo.Open(args[1], i)
  192. // }
  193. // break
  194. case "finalize", "finalize_vote":
  195. checkIfExists(args, 1, "a proposal ID")
  196. err = git.Validate()
  197. if err == nil {
  198. var pollPassed bool
  199. var isProposer bool
  200. // Finalizes a proposal
  201. pollPassed, isProposer, err = ethereum.Finalize(args[1])
  202. if err == nil && isProposer {
  203. fmt.Println()
  204. if pollPassed {
  205. // Merges the proposal branch into master after a successful vote
  206. err = git.Merge(args[1])
  207. if err == nil {
  208. helpers.PrintLine("Successfully merged dit proposal "+args[1]+" into the master branch", 0)
  209. }
  210. } else {
  211. // Deletes the proposal branch after an unsuccessful vote
  212. err = git.DeleteBranch(args[1])
  213. if err == nil {
  214. helpers.PrintLine("Removed the dit proposal "+args[1]+" from the repository", 0)
  215. }
  216. }
  217. }
  218. }
  219. break
  220. default:
  221. printUsage()
  222. break
  223. }
  224. if err != nil {
  225. if len(err.Error()) > 0 {
  226. helpers.PrintLine(err.Error(), 2)
  227. }
  228. }
  229. }
  230. // Will check whether a provided argument exists and tell the user if its missing
  231. func checkIfExists(_arguments []string, _index int, _description string) {
  232. if len(_arguments) < _index+1 {
  233. helpers.PrintLine("Please provide "+_description, 2)
  234. os.Exit(0)
  235. }
  236. }
  237. func printUsage() {
  238. fmt.Println("--------- dit client v0.2 --------")
  239. if config.DitConfig.DemoModeActive {
  240. fmt.Println("--------- demo mode active -------")
  241. } else {
  242. fmt.Println("--------- live mode active -------")
  243. }
  244. fmt.Println("------------- General ------------")
  245. fmt.Println(" - dit setup\t\t\t\t\tCreates or imports the ethereum keys and creates a config")
  246. fmt.Println(" - dit mode <MODE>\t\t\t\tSwitch between the modes of the client (live or demo)")
  247. fmt.Println("")
  248. fmt.Println("------------- Ethereum ------------")
  249. fmt.Println(" - dit get_address\t\t\t\tReturns ethereum address of the account")
  250. fmt.Println(" - dit get_balance\t\t\t\tReturns the ETH and KNW balance of the account")
  251. fmt.Println("")
  252. fmt.Println("----------- Repositories ----------")
  253. fmt.Println(" - dit clone <REPOSITORY_URL>\t\t\tClones a repository (GitHub only) and automatically")
  254. fmt.Println("\t\t\t\t\t\tcalls 'dit init' afterwards")
  255. fmt.Println(" - dit init\t\t\t\t\tRetrieves the address of the ditContract for the repository")
  256. fmt.Println("\t\t\t\t\t\tyou are using (GitHub only)")
  257. fmt.Println(" - dit commit <COMMIT_MESSAGE>\t\t\tProposes a new commit with the specified")
  258. fmt.Println("\t\t\t\t\t\tcommit message (This will start a vote)")
  259. fmt.Println("")
  260. fmt.Println("------------- Voting --------------")
  261. fmt.Println(" - dit vote <PROPOSAL_ID> <CHOICE> <SALT>\tCasts a concealed vote on a proposed commit")
  262. fmt.Println(" - dit open <PROPOSAL_ID>\t\t\tOpens and reveals the vote commitment on a proposed commit")
  263. fmt.Println(" - dit finalize <PROPOSAL_ID>\t\t\tFinalizes the vote on proposed commit and claims the tokens")
  264. os.Exit(0)
  265. }