You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bitrpc.py 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. from jsonrpc import ServiceProxy
  2. import sys
  3. import string
  4. import getpass
  5. # ===== BEGIN USER SETTINGS =====
  6. # if you do not set these you will be prompted for a password for every command
  7. rpcuser = ""
  8. rpcpass = ""
  9. # ====== END USER SETTINGS ======
  10. if rpcpass == "":
  11. access = ServiceProxy("http://127.0.0.1:8332")
  12. else:
  13. access = ServiceProxy("http://"+rpcuser+":"+rpcpass+"@127.0.0.1:8332")
  14. cmd = sys.argv[1].lower()
  15. if cmd == "backupwallet":
  16. try:
  17. path = raw_input("Enter destination path/filename: ")
  18. print access.backupwallet(path)
  19. except:
  20. print "\n---An error occurred---\n"
  21. elif cmd == "encryptwallet":
  22. try:
  23. pwd = getpass.getpass(prompt="Enter passphrase: ")
  24. pwd2 = getpass.getpass(prompt="Repeat passphrase: ")
  25. if pwd == pwd2:
  26. access.encryptwallet(pwd)
  27. print "\n---Wallet encrypted. Server stopping, restart to run with encrypted wallet---\n"
  28. else:
  29. print "\n---Passphrases do not match---\n"
  30. except:
  31. print "\n---An error occurred---\n"
  32. elif cmd == "getaccount":
  33. try:
  34. addr = raw_input("Enter a Bitcoin address: ")
  35. print access.getaccount(addr)
  36. except:
  37. print "\n---An error occurred---\n"
  38. elif cmd == "getaccountaddress":
  39. try:
  40. acct = raw_input("Enter an account name: ")
  41. print access.getaccountaddress(acct)
  42. except:
  43. print "\n---An error occurred---\n"
  44. elif cmd == "getaddressesbyaccount":
  45. try:
  46. acct = raw_input("Enter an account name: ")
  47. print access.getaddressesbyaccount(acct)
  48. except:
  49. print "\n---An error occurred---\n"
  50. elif cmd == "getbalance":
  51. try:
  52. acct = raw_input("Enter an account (optional): ")
  53. mc = raw_input("Minimum confirmations (optional): ")
  54. try:
  55. print access.getbalance(acct, mc)
  56. except:
  57. print access.getbalance()
  58. except:
  59. print "\n---An error occurred---\n"
  60. elif cmd == "getblockbycount":
  61. try:
  62. height = raw_input("Height: ")
  63. print access.getblockbycount(height)
  64. except:
  65. print "\n---An error occurred---\n"
  66. elif cmd == "getblockcount":
  67. try:
  68. print access.getblockcount()
  69. except:
  70. print "\n---An error occurred---\n"
  71. elif cmd == "getblocknumber":
  72. try:
  73. print access.getblocknumber()
  74. except:
  75. print "\n---An error occurred---\n"
  76. elif cmd == "getconnectioncount":
  77. try:
  78. print access.getconnectioncount()
  79. except:
  80. print "\n---An error occurred---\n"
  81. elif cmd == "getdifficulty":
  82. try:
  83. print access.getdifficulty()
  84. except:
  85. print "\n---An error occurred---\n"
  86. elif cmd == "getgenerate":
  87. try:
  88. print access.getgenerate()
  89. except:
  90. print "\n---An error occurred---\n"
  91. elif cmd == "gethashespersec":
  92. try:
  93. print access.gethashespersec()
  94. except:
  95. print "\n---An error occurred---\n"
  96. elif cmd == "getinfo":
  97. try:
  98. print access.getinfo()
  99. except:
  100. print "\n---An error occurred---\n"
  101. elif cmd == "getnewaddress":
  102. try:
  103. acct = raw_input("Enter an account name: ")
  104. try:
  105. print access.getnewaddress(acct)
  106. except:
  107. print access.getnewaddress()
  108. except:
  109. print "\n---An error occurred---\n"
  110. elif cmd == "getreceivedbyaccount":
  111. try:
  112. acct = raw_input("Enter an account (optional): ")
  113. mc = raw_input("Minimum confirmations (optional): ")
  114. try:
  115. print access.getreceivedbyaccount(acct, mc)
  116. except:
  117. print access.getreceivedbyaccount()
  118. except:
  119. print "\n---An error occurred---\n"
  120. elif cmd == "getreceivedbyaddress":
  121. try:
  122. addr = raw_input("Enter a Bitcoin address (optional): ")
  123. mc = raw_input("Minimum confirmations (optional): ")
  124. try:
  125. print access.getreceivedbyaddress(addr, mc)
  126. except:
  127. print access.getreceivedbyaddress()
  128. except:
  129. print "\n---An error occurred---\n"
  130. elif cmd == "gettransaction":
  131. try:
  132. txid = raw_input("Enter a transaction ID: ")
  133. print access.gettransaction(txid)
  134. except:
  135. print "\n---An error occurred---\n"
  136. elif cmd == "getwork":
  137. try:
  138. data = raw_input("Data (optional): ")
  139. try:
  140. print access.gettransaction(data)
  141. except:
  142. print access.gettransaction()
  143. except:
  144. print "\n---An error occurred---\n"
  145. elif cmd == "help":
  146. try:
  147. cmd = raw_input("Command (optional): ")
  148. try:
  149. print access.help(cmd)
  150. except:
  151. print access.help()
  152. except:
  153. print "\n---An error occurred---\n"
  154. elif cmd == "listaccounts":
  155. try:
  156. mc = raw_input("Minimum confirmations (optional): ")
  157. try:
  158. print access.listaccounts(mc)
  159. except:
  160. print access.listaccounts()
  161. except:
  162. print "\n---An error occurred---\n"
  163. elif cmd == "listreceivedbyaccount":
  164. try:
  165. mc = raw_input("Minimum confirmations (optional): ")
  166. incemp = raw_input("Include empty? (true/false, optional): ")
  167. try:
  168. print access.listreceivedbyaccount(mc, incemp)
  169. except:
  170. print access.listreceivedbyaccount()
  171. except:
  172. print "\n---An error occurred---\n"
  173. elif cmd == "listreceivedbyaddress":
  174. try:
  175. mc = raw_input("Minimum confirmations (optional): ")
  176. incemp = raw_input("Include empty? (true/false, optional): ")
  177. try:
  178. print access.listreceivedbyaddress(mc, incemp)
  179. except:
  180. print access.listreceivedbyaddress()
  181. except:
  182. print "\n---An error occurred---\n"
  183. elif cmd == "listtransactions":
  184. try:
  185. acct = raw_input("Account (optional): ")
  186. count = raw_input("Number of transactions (optional): ")
  187. frm = raw_input("Skip (optional):")
  188. try:
  189. print access.listtransactions(acct, count, frm)
  190. except:
  191. print access.listtransactions()
  192. except:
  193. print "\n---An error occurred---\n"
  194. elif cmd == "move":
  195. try:
  196. frm = raw_input("From: ")
  197. to = raw_input("To: ")
  198. amt = raw_input("Amount:")
  199. mc = raw_input("Minimum confirmations (optional): ")
  200. comment = raw_input("Comment (optional): ")
  201. try:
  202. print access.move(frm, to, amt, mc, comment)
  203. except:
  204. print access.move(frm, to, amt)
  205. except:
  206. print "\n---An error occurred---\n"
  207. elif cmd == "sendfrom":
  208. try:
  209. frm = raw_input("From: ")
  210. to = raw_input("To: ")
  211. amt = raw_input("Amount:")
  212. mc = raw_input("Minimum confirmations (optional): ")
  213. comment = raw_input("Comment (optional): ")
  214. commentto = raw_input("Comment-to (optional): ")
  215. try:
  216. print access.sendfrom(frm, to, amt, mc, comment, commentto)
  217. except:
  218. print access.sendfrom(frm, to, amt)
  219. except:
  220. print "\n---An error occurred---\n"
  221. elif cmd == "sendmany":
  222. try:
  223. frm = raw_input("From: ")
  224. to = raw_input("To (in format address1:amount1,address2:amount2,...): ")
  225. mc = raw_input("Minimum confirmations (optional): ")
  226. comment = raw_input("Comment (optional): ")
  227. try:
  228. print access.sendmany(frm,to,mc,comment)
  229. except:
  230. print access.sendmany(frm,to)
  231. except:
  232. print "\n---An error occurred---\n"
  233. elif cmd == "sendtoaddress":
  234. try:
  235. to = raw_input("To (in format address1:amount1,address2:amount2,...): ")
  236. amt = raw_input("Amount:")
  237. comment = raw_input("Comment (optional): ")
  238. commentto = raw_input("Comment-to (optional): ")
  239. try:
  240. print access.sendtoaddress(to,amt,comment,commentto)
  241. except:
  242. print access.sendtoaddress(to,amt)
  243. except:
  244. print "\n---An error occurred---\n"
  245. elif cmd == "setaccount":
  246. try:
  247. addr = raw_input("Address: ")
  248. acct = raw_input("Account:")
  249. print access.setaccount(addr,acct)
  250. except:
  251. print "\n---An error occurred---\n"
  252. elif cmd == "setgenerate":
  253. try:
  254. gen= raw_input("Generate? (true/false): ")
  255. cpus = raw_input("Max processors/cores (-1 for unlimited, optional):")
  256. try:
  257. print access.setgenerate(gen, cpus)
  258. except:
  259. print access.setgenerate(gen)
  260. except:
  261. print "\n---An error occurred---\n"
  262. elif cmd == "settxfee":
  263. try:
  264. amt = raw_input("Amount:")
  265. print access.settxfee(amt)
  266. except:
  267. print "\n---An error occurred---\n"
  268. elif cmd == "stop":
  269. try:
  270. print access.stop()
  271. except:
  272. print "\n---An error occurred---\n"
  273. elif cmd == "validateaddress":
  274. try:
  275. addr = raw_input("Address: ")
  276. print access.validateaddress(addr)
  277. except:
  278. print "\n---An error occurred---\n"
  279. elif cmd == "walletpassphrase":
  280. try:
  281. pwd = getpass.getpass(prompt="Enter wallet passphrase: ")
  282. access.walletpassphrase(pwd, 60)
  283. print "\n---Wallet unlocked---\n"
  284. except:
  285. print "\n---An error occurred---\n"
  286. elif cmd == "walletpassphrasechange":
  287. try:
  288. pwd = getpass.getpass(prompt="Enter old wallet passphrase: ")
  289. pwd2 = getpass.getpass(prompt="Enter new wallet passphrase: ")
  290. access.walletpassphrasechange(pwd, pwd2)
  291. print
  292. print "\n---Passphrase changed---\n"
  293. except:
  294. print
  295. print "\n---An error occurred---\n"
  296. print
  297. else:
  298. print "Command not found or not supported"