zcash_test.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package gotest
  2. import (
  3. "crypto/sha1"
  4. "encoding/hex"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "time"
  11. // "encoding/json"
  12. //"zcash"/*local test need*/
  13. )
  14. func Test_newaddr(t *testing.T) {
  15. client := http.DefaultClient
  16. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/createaddr`)
  17. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  18. t.Logf("post url:%v", geturl)
  19. //var b []byte
  20. postdata := fmt.Sprintf(`{"cointype":"ZEC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte ("/coinproxy/createaddr"), timestamp))
  21. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  22. if err != nil {
  23. t.Errorf("err:%s", err.Error())
  24. return
  25. }
  26. resp, err := client.Do(request)
  27. if err != nil {
  28. t.Errorf("err:%s", err.Error())
  29. return
  30. }
  31. defer resp.Body.Close()
  32. rbs, err := ioutil.ReadAll(resp.Body)
  33. if err != nil {
  34. t.Errorf("err:%s", err.Error())
  35. return
  36. }
  37. t.Logf("read bytes:%v", string(rbs))
  38. return
  39. }
  40. func Test_Balance(t *testing.T) {
  41. client := http.DefaultClient
  42. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/coinbalance`)
  43. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  44. t.Logf("post url:%v", geturl)
  45. //var b []byte
  46. postdata := fmt.Sprintf(`{"cointype":"ZEC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte ("/coinproxy/coinbalance"), timestamp))
  47. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  48. if err != nil {
  49. t.Errorf("err:%s", err.Error())
  50. return
  51. }
  52. resp, err := client.Do(request)
  53. if err != nil {
  54. t.Errorf("err:%s", err.Error())
  55. return
  56. }
  57. defer resp.Body.Close()
  58. rbs, err := ioutil.ReadAll(resp.Body)
  59. if err != nil {
  60. t.Errorf("err:%s", err.Error())
  61. return
  62. }
  63. t.Logf("read bytes:%v", string(rbs))
  64. return
  65. }
  66. func Test_TransInfo(t *testing.T) {
  67. //first get block height then test transinfo
  68. client := http.DefaultClient
  69. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/query/transinfo`)
  70. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  71. t.Logf("post url:%v", geturl)
  72. //var b []byte
  73. postdata := fmt.Sprintf(`{"cointype":"ZEC","starth":10,"endh":100,"timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte ("/coinproxy/query/transinfo"), timestamp))
  74. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  75. if err != nil {
  76. t.Errorf("err:%s", err.Error())
  77. return
  78. }
  79. resp, err := client.Do(request)
  80. if err != nil {
  81. t.Errorf("err:%s", err.Error())
  82. return
  83. }
  84. defer resp.Body.Close()
  85. rbs, err := ioutil.ReadAll(resp.Body)
  86. if err != nil {
  87. t.Errorf("err:%s", err.Error())
  88. return
  89. }
  90. t.Logf("read bytes:%v", string(rbs))
  91. return
  92. }
  93. /*
  94. func Test_TransferAccounts(t *testing.T) {
  95. client := http.DefaultClient
  96. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendcoins`)
  97. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  98. t.Logf("post url:%v", geturl)
  99. //var b []byte
  100. postdata := fmt.Sprintf(`{"cointype":"ZEC","amount":"0.00005","address":"t1fY6WoALcAp8EfNTYbdyHYuaWNW4QcDz7H","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte ("/coinproxy/sendcoins"), timestamp))
  101. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  102. if err != nil {
  103. t.Errorf("err:%s", err.Error())
  104. return
  105. }
  106. resp, err := client.Do(request)
  107. if err != nil {
  108. t.Errorf("err:%s", err.Error())
  109. return
  110. }
  111. defer resp.Body.Close()
  112. rbs, err := ioutil.ReadAll(resp.Body)
  113. if err != nil {
  114. t.Errorf("err:%s", err.Error())
  115. return
  116. }
  117. t.Logf("read bytes:%v", string(rbs))
  118. return
  119. }
  120. */
  121. func Test_ChainHeight(t *testing.T) {
  122. client := http.DefaultClient
  123. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/blockchain/height`)
  124. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  125. t.Logf("post url:%v", geturl)
  126. //var b []byte
  127. postdata := fmt.Sprintf(`{"cointype":"ZEC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte ("/coinproxy/blockchain/height"), timestamp))
  128. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  129. if err != nil {
  130. t.Errorf("err:%s", err.Error())
  131. return
  132. }
  133. resp, err := client.Do(request)
  134. if err != nil {
  135. t.Errorf("err:%s", err.Error())
  136. return
  137. }
  138. defer resp.Body.Close()
  139. rbs, err := ioutil.ReadAll(resp.Body)
  140. if err != nil {
  141. t.Errorf("err:%s", err.Error())
  142. return
  143. }
  144. t.Logf("read bytes:%v", string(rbs))
  145. return
  146. }
  147. func Signature(body []byte, timestamp string) string {
  148. sha1Contain := sha1.New()
  149. byteSha1 := append(body, append([]byte(timestamp), []byte("coinsapi^&#@(*33")...)...)
  150. sha1Contain.Write(byteSha1)
  151. localSig := hex.EncodeToString(sha1Contain.Sum(nil))
  152. return localSig
  153. }
  154. //local test is ok,not support remote test by docker deploy so
  155. /*
  156. func Test_WalletGetAccountAddress(t *testing.T) {
  157. var zcashapi zcash.ZcashApi
  158. addr, err := zcashapi.WalletGetAccountAddress("")
  159. if err == nil {
  160. t.Logf("read WalletGetAccountAddress bytes:%s", addr)
  161. }
  162. if err != nil {
  163. t.Errorf("err:%s", err.Error())
  164. return
  165. }
  166. return
  167. }
  168. func Test_WalletAddrList(t *testing.T) {
  169. var zcashapi zcash.ZcashApi
  170. addr, err := zcashapi.WalletAddrList("")
  171. if err == nil {
  172. t.Logf("read WalletAddrList bytes:%s", addr)
  173. }
  174. if err != nil {
  175. t.Errorf("err:%s", err.Error())
  176. return
  177. }
  178. return
  179. }
  180. func Test_WallectInfo(t *testing.T) {
  181. var zcashapi zcash.ZcashApi
  182. addr, err := zcashapi.WallectInfo()
  183. if err == nil {
  184. t.Logf("read WallectInfo bytes:%s", addr)
  185. }
  186. if err != nil {
  187. t.Errorf("err:%s", err.Error())
  188. return
  189. }
  190. return
  191. }
  192. func Test_WalleExportWalletAddress(t *testing.T) {
  193. var zcashapi zcash.ZcashApi
  194. filedir, err := zcashapi.WalleExportWalletAddress("fuzameiwallet")
  195. if err == nil {
  196. t.Logf("read WalleExportWalletAddress bytes:%s", filedir)
  197. }
  198. if err != nil {
  199. t.Errorf("err:%s", err.Error())
  200. return
  201. }
  202. return
  203. }
  204. func Test_WallectGetNewZaddress(t *testing.T) {
  205. var zcashapi zcash.ZcashApi
  206. filedir, err := zcashapi.WallectGetNewZaddress()
  207. if err == nil {
  208. t.Logf("read WallectGetNewZaddress bytes:%s", filedir)
  209. zkey, err := zcashapi.WallectExportZkey(filedir)
  210. if err == nil {
  211. t.Logf("read WallectExportZkey bytes:%s", zkey)
  212. }
  213. if err != nil {
  214. t.Errorf("err:%s", err.Error())
  215. return
  216. }
  217. }
  218. if err != nil {
  219. t.Errorf("err:%s", err.Error())
  220. return
  221. }
  222. return
  223. }
  224. func Test_WallectListZaddresses(t *testing.T) {
  225. var zcashapi zcash.ZcashApi
  226. filedir, err := zcashapi.WallectListZaddresses()
  227. if err == nil {
  228. t.Logf("read WallectListZaddresses bytes:%s", filedir)
  229. }
  230. if err != nil {
  231. t.Errorf("err:%s", err.Error())
  232. return
  233. }
  234. return
  235. }
  236. func Test_WalleGetReceivedByAccount(t *testing.T) {
  237. var zcashapi zcash.ZcashApi
  238. filedir, err := zcashapi.WalleGetReceivedByAccount("", 1)
  239. if err == nil {
  240. t.Logf("read WalleGetReceivedByAccount bytes:%.8f", filedir)
  241. }
  242. if err != nil {
  243. t.Errorf("err:%s", err.Error())
  244. return
  245. }
  246. return
  247. }
  248. func Test_WalleGetReceivedByAddress(t *testing.T) {
  249. var zcashapi zcash.ZcashApi
  250. filedir, err := zcashapi.WalleGetReceivedByAddress("t1T8ei5znEsKhXmembrM8iQqB55GcUt6GRK", 1)
  251. if err == nil {
  252. t.Logf("read WalleGetReceivedByAddress bytes:%.8f", filedir)
  253. }
  254. if err != nil {
  255. t.Errorf("err:%s", err.Error())
  256. return
  257. }
  258. return
  259. }
  260. func Test_WalleGetbalanceByaddress(t *testing.T) {
  261. var zcashapi zcash.ZcashApi
  262. filedir, err := zcashapi.WalleGetbalanceByaddress("t1T8ei5znEsKhXmembrM8iQqB55GcUt6GRK", 1)
  263. if err == nil {
  264. t.Logf("read WalleGetbalanceByaddress bytes:%.8f", filedir)
  265. }
  266. if err != nil {
  267. t.Errorf("err:%s", err.Error())
  268. return
  269. }
  270. return
  271. }
  272. func Test_WalleGetTotalBalance(t *testing.T) {
  273. var zcashapi zcash.ZcashApi
  274. filedir, err := zcashapi.WalleGetTotalBalance(1)
  275. if err == nil {
  276. t.Logf("read WalleGetTotalBalance bytes:%s", filedir)
  277. }
  278. if err != nil {
  279. t.Errorf("err:%s", err.Error())
  280. return
  281. }
  282. return
  283. }
  284. func Test_WalletListReceivedByZaddress(t *testing.T) {
  285. var zcashapi zcash.ZcashApi
  286. filedir, err := zcashapi.WallezListReceivedByZaddress("zcdAm8ftZiDJ8G6iBfDPfeaWwh7JNcrkERFEQv1VXK7F5hwkH3K9vex9TLkxJzskqLcYKd2NPs4TeutpSk4VUPjB8wmbfUD", 1)
  287. if err == nil {
  288. t.Logf("read WallezListReceivedByZaddress bytes:%s", filedir)
  289. }
  290. if err != nil {
  291. t.Errorf("err:%s", err.Error())
  292. return
  293. }
  294. return
  295. }
  296. func Test_WalletSetTxFee(t *testing.T) {
  297. var zcashapi zcash.ZcashApi
  298. filedir, err := zcashapi.WalletSetTxFee(float64(0))
  299. if err == nil {
  300. t.Logf("read WalletSetTxFee bytes:%v", filedir)
  301. }
  302. if err != nil {
  303. t.Errorf("err:%s", err.Error())
  304. return
  305. }
  306. return
  307. }
  308. func Test_WalletZ_SendFrom(t *testing.T) {
  309. //sendmanyarry:= []zcash.ZcashAmountsToSend{{"t1Hy5oLGmdTLXFCqB4WMKbNacmbNuBL7q33",0.1,""}}
  310. //sendmanyarry := [{"address":"t1Hy5oLGmdTLXFCqB4WMKbNacmbNuBL7q33","amount":0.1,"memo":""},{"address":"t1RwAwki1n4z1i7M9oJhj9zTszTBYt6t1Lk","amount":0.2,"memo":""}]
  311. //sendmanyarry[0].address = "t1Hy5oLGmdTLXFCqB4WMKbNacmbNuBL7q33"
  312. //sendmanyarry[0].amount = 0.1
  313. //sendmanyarry[0].memo = ""
  314. //sendmanyarry[1].address = "t1RwAwki1n4z1i7M9oJhj9zTszTBYt6t1Lk"
  315. //sendmanyarry[1].amount = 0.3
  316. //sendmanyarry[1].memo = ""
  317. var zcashapi zcash.ZcashApi
  318. filedir, err := zcashapi.WalletZ_Send("t1T8ei5znEsKhXmembrM8iQqB55GcUt6GRK","zcfUAY3nq5bg2aqdj8FPf3KT5rFkTyhCLiHVvBnmVJ72ZpQwgufdqNHrBAaf8KpJzaNHf7NmtktUhBLRnqoP69sVvkjGUQg",0.000001,1,0.00000000)
  319. if err == nil {
  320. t.Logf("read WalletSetTxFee bytes:%s", filedir)
  321. }
  322. if err != nil {
  323. t.Errorf("err:%s", err.Error())
  324. return
  325. }
  326. return
  327. }
  328. */