dcrcash_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package dcrcash
  2. import (
  3. "crypto/sha1"
  4. "encoding/hex"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. /*func Test_newaddr(t *testing.T) {
  13. client := http.DefaultClient
  14. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/createaddr`)
  15. //timestamp := fmt.Sprintf("%v", time.Now().Unix())
  16. fmt.Printf("post url:%v", geturl)
  17. //var b []byte
  18. //postdata := fmt.Sprintf(`{"cointype":"BCC","timestamp":"%v"}`, timestamp)
  19. postdata := fmt.Sprintf(`{"cointype":"DCR"}`)
  20. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  21. if err != nil {
  22. fmt.Printf("err:%s", err.Error())
  23. return
  24. }
  25. resp, err := client.Do(request)
  26. if err != nil {
  27. fmt.Printf("err:%s", err.Error())
  28. return
  29. }
  30. defer resp.Body.Close()
  31. rbs, err := ioutil.ReadAll(resp.Body)
  32. if err != nil {
  33. fmt.Printf("err:%s", err.Error())
  34. return
  35. }
  36. fmt.Printf("read bytes:%v", string(rbs))
  37. return
  38. }*/
  39. func Test_Balance(t *testing.T) {
  40. client := http.DefaultClient
  41. //geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/coinbalance`)
  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":"DCR","Sync":false,"timestamp":"%v"}`, 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://118.31.213.53:8803/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":"DCR","starth":1,"endh":1243398,"timestamp":"%v"}`, 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. func Test_ChainHeight(t *testing.T) {
  94. client := http.DefaultClient
  95. //geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/blockchain/height`)
  96. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/blockchain/height`)
  97. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  98. t.Logf("post url:%v", geturl)
  99. //var b []byte
  100. postdata := fmt.Sprintf(`{"cointype":"DCR","timestamp":"%v"}`, 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. func Test_ListUnspent(t *testing.T) {
  121. client := http.DefaultClient
  122. //geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/listunspent`)
  123. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/listunspent`)
  124. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  125. t.Logf("post url:%v", geturl)
  126. postdata := fmt.Sprintf(`{"cointype":"DCR","Sync":false,"address":[],"timestamp":"%v"}`, timestamp)
  127. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  128. if err != nil {
  129. t.Errorf("err:%s", err.Error())
  130. return
  131. }
  132. resp, err := client.Do(request)
  133. if err != nil {
  134. t.Errorf("err:%s", err.Error())
  135. return
  136. }
  137. defer resp.Body.Close()
  138. rbs, err := ioutil.ReadAll(resp.Body)
  139. if err != nil {
  140. t.Errorf("err:%s", err.Error())
  141. return
  142. }
  143. t.Logf("read bytes:%v", string(rbs))
  144. return
  145. }
  146. func Test_SendRawTransaction(t *testing.T) {
  147. client := http.DefaultClient
  148. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendrawtransaction`)
  149. //geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/sendrawtransaction`)
  150. t.Logf("post url:%v", geturl)
  151. //var b []byte
  152. postdata := fmt.Sprintf(`{"cointype":"DCR","data":"a4db9908e6d01a347d2fe5040592851b0497da93fc5889e737daf5225f1d66c8","address":""}`)
  153. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  154. if err != nil {
  155. t.Errorf("err:%s", err.Error())
  156. return
  157. }
  158. resp, err := client.Do(request)
  159. if err != nil {
  160. t.Errorf("err:%s", err.Error())
  161. return
  162. }
  163. defer resp.Body.Close()
  164. rbs, err := ioutil.ReadAll(resp.Body)
  165. if err != nil {
  166. t.Errorf("err:%s", err.Error())
  167. return
  168. }
  169. t.Logf("read bytes:%v", string(rbs))
  170. return
  171. }
  172. /*
  173. func Test_SumCoinBalance(t *testing.T) {
  174. client := http.DefaultClient
  175. //geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sumcoinbalance`)
  176. geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/sumcoinbalance`)
  177. t.Logf("post url:%v", geturl)
  178. //var b []byte
  179. postdata := fmt.Sprintf(`{"cointype":"DCR","sumamount":600}`)
  180. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  181. if err != nil {
  182. t.Errorf("err:%s", err.Error())
  183. return
  184. }
  185. resp, err := client.Do(request)
  186. if err != nil {
  187. t.Errorf("err:%s", err.Error())
  188. return
  189. }
  190. defer resp.Body.Close()
  191. rbs, err := ioutil.ReadAll(resp.Body)
  192. if err != nil {
  193. t.Errorf("err:%s", err.Error())
  194. return
  195. }
  196. t.Logf("read bytes:%v", string(rbs))
  197. return
  198. }
  199. */
  200. func Test_AccountsBalance(t *testing.T) {
  201. client := http.DefaultClient
  202. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/accountsbalance`)
  203. timetamp := fmt.Sprintf("%v", time.Now().Unix())
  204. t.Logf("post url:%v", geturl)
  205. posdata := fmt.Sprintf(`{"cointype":"DCR","timestamp":"%v"}`, timetamp)
  206. request, err := http.NewRequest("POST", geturl, strings.NewReader(posdata))
  207. if err != nil {
  208. t.Error("err:%s", err.Error())
  209. return
  210. }
  211. resp, err := client.Do(request)
  212. if err != nil {
  213. t.Errorf("err:%s", err.Error())
  214. return
  215. }
  216. defer resp.Body.Close()
  217. rbs, err := ioutil.ReadAll(resp.Body)
  218. if err != nil {
  219. t.Errorf("err:%s", err.Error())
  220. return
  221. }
  222. t.Logf("read bytes:%v", string(rbs))
  223. }
  224. /*
  225. func Test_TransferAccounts(t *testing.T) {
  226. client := http.DefaultClient
  227. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendcoins`)
  228. //geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/sendcoins`)
  229. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  230. t.Logf("post url:%v", geturl)
  231. postdata := fmt.Sprintf(`{"cointype":"DCR","amount":"0.02","address":"DsctbrcDVEF4g4gqTpRF8ciZMqwe4va32uK","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/sendcoins"), timestamp))
  232. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  233. if err != nil {
  234. t.Errorf("err:%s", err.Error())
  235. return
  236. }
  237. resp, err := client.Do(request)
  238. if err != nil {
  239. t.Errorf("err:%s", err.Error())
  240. return
  241. }
  242. defer resp.Body.Close()
  243. rbs, err := ioutil.ReadAll(resp.Body)
  244. if err != nil {
  245. t.Errorf("err:%s", err.Error())
  246. return
  247. }
  248. t.Logf("read bytes:%v", string(rbs))
  249. return
  250. }
  251. */
  252. func Signature(body []byte, timestamp string) string {
  253. sha1Contain := sha1.New()
  254. byteSha1 := append(body, append([]byte(timestamp), []byte("2017coins%^&#@(*33")...)...)
  255. sha1Contain.Write(byteSha1)
  256. localSig := hex.EncodeToString(sha1Contain.Sum(nil))
  257. return localSig
  258. }