dcrcash_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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://localhost:15741/coinproxy/coinbalance`)
  42. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  43. t.Logf("post url:%v", geturl)
  44. //var b []byte
  45. postdata := fmt.Sprintf(`{"cointype":"DCR","timestamp":"%v"}`, timestamp)
  46. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  47. if err != nil {
  48. t.Errorf("err:%s", err.Error())
  49. return
  50. }
  51. resp, err := client.Do(request)
  52. if err != nil {
  53. t.Errorf("err:%s", err.Error())
  54. return
  55. }
  56. defer resp.Body.Close()
  57. rbs, err := ioutil.ReadAll(resp.Body)
  58. if err != nil {
  59. t.Errorf("err:%s", err.Error())
  60. return
  61. }
  62. t.Logf("read bytes:%v", string(rbs))
  63. return
  64. }
  65. /*func Test_TransInfo(t *testing.T) {
  66. //first get block height then test transinfo
  67. client := http.DefaultClient
  68. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/query/transinfo`)
  69. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  70. t.Logf("post url:%v", geturl)
  71. //var b []byte
  72. postdata := fmt.Sprintf(`{"cointype":"DCR","starth":1,"endh":1243398,"timestamp":"%v"}`, timestamp)
  73. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  74. if err != nil {
  75. t.Errorf("err:%s", err.Error())
  76. return
  77. }
  78. resp, err := client.Do(request)
  79. if err != nil {
  80. t.Errorf("err:%s", err.Error())
  81. return
  82. }
  83. defer resp.Body.Close()
  84. rbs, err := ioutil.ReadAll(resp.Body)
  85. if err != nil {
  86. t.Errorf("err:%s", err.Error())
  87. return
  88. }
  89. t.Logf("read bytes:%v", string(rbs))
  90. return
  91. }*/
  92. func Test_ChainHeight(t *testing.T) {
  93. client := http.DefaultClient
  94. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/blockchain/height`)
  95. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  96. t.Logf("post url:%v", geturl)
  97. //var b []byte
  98. postdata := fmt.Sprintf(`{"cointype":"DCR","timestamp":"%v"}`, timestamp)
  99. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  100. if err != nil {
  101. t.Errorf("err:%s", err.Error())
  102. return
  103. }
  104. resp, err := client.Do(request)
  105. if err != nil {
  106. t.Errorf("err:%s", err.Error())
  107. return
  108. }
  109. defer resp.Body.Close()
  110. rbs, err := ioutil.ReadAll(resp.Body)
  111. if err != nil {
  112. t.Errorf("err:%s", err.Error())
  113. return
  114. }
  115. t.Logf("read bytes:%v", string(rbs))
  116. return
  117. }
  118. func Test_ListUnspent(t *testing.T) {
  119. client := http.DefaultClient
  120. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/listunspent`)
  121. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  122. t.Logf("post url:%v", geturl)
  123. postdata := fmt.Sprintf(`{"cointype":"DCR","address":[2],"timestamp":"%v"}`, timestamp)
  124. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  125. if err != nil {
  126. t.Errorf("err:%s", err.Error())
  127. return
  128. }
  129. resp, err := client.Do(request)
  130. if err != nil {
  131. t.Errorf("err:%s", err.Error())
  132. return
  133. }
  134. defer resp.Body.Close()
  135. rbs, err := ioutil.ReadAll(resp.Body)
  136. if err != nil {
  137. t.Errorf("err:%s", err.Error())
  138. return
  139. }
  140. t.Logf("read bytes:%v", string(rbs))
  141. return
  142. }
  143. func Test_SendRawTransaction(t *testing.T) {
  144. client := http.DefaultClient
  145. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendrawtransaction`)
  146. t.Logf("post url:%v", geturl)
  147. //var b []byte
  148. postdata := fmt.Sprintf(`{"cointype":"DCR","data":"a4db9908e6d01a347d2fe5040592851b0497da93fc5889e737daf5225f1d66c8","address":""}`)
  149. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  150. if err != nil {
  151. t.Errorf("err:%s", err.Error())
  152. return
  153. }
  154. resp, err := client.Do(request)
  155. if err != nil {
  156. t.Errorf("err:%s", err.Error())
  157. return
  158. }
  159. defer resp.Body.Close()
  160. rbs, err := ioutil.ReadAll(resp.Body)
  161. if err != nil {
  162. t.Errorf("err:%s", err.Error())
  163. return
  164. }
  165. t.Logf("read bytes:%v", string(rbs))
  166. return
  167. }
  168. func Test_SumCoinBalance(t *testing.T) {
  169. client := http.DefaultClient
  170. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sumcoinbalance`)
  171. t.Logf("post url:%v", geturl)
  172. //var b []byte
  173. postdata := fmt.Sprintf(`{"cointype":"DCR","sumamount":5.0}`)
  174. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  175. if err != nil {
  176. t.Errorf("err:%s", err.Error())
  177. return
  178. }
  179. resp, err := client.Do(request)
  180. if err != nil {
  181. t.Errorf("err:%s", err.Error())
  182. return
  183. }
  184. defer resp.Body.Close()
  185. rbs, err := ioutil.ReadAll(resp.Body)
  186. if err != nil {
  187. t.Errorf("err:%s", err.Error())
  188. return
  189. }
  190. t.Logf("read bytes:%v", string(rbs))
  191. return
  192. }
  193. /*func Test_TransferAccounts(t *testing.T) {
  194. client := http.DefaultClient
  195. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendcoins`)
  196. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  197. t.Logf("post url:%v", geturl)
  198. //var b []byte
  199. postdata := fmt.Sprintf(`{"cointype":"DCR","amount":"0.002747","address":"DsgbyoXAB2PdsiU3yDpCtBww7FcxhTAV2Vr","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/sendcoins"), timestamp))
  200. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  201. if err != nil {
  202. t.Errorf("err:%s", err.Error())
  203. return
  204. }
  205. resp, err := client.Do(request)
  206. if err != nil {
  207. t.Errorf("err:%s", err.Error())
  208. return
  209. }
  210. defer resp.Body.Close()
  211. rbs, err := ioutil.ReadAll(resp.Body)
  212. if err != nil {
  213. t.Errorf("err:%s", err.Error())
  214. return
  215. }
  216. t.Logf("read bytes:%v", string(rbs))
  217. return
  218. }*/
  219. func Signature(body []byte, timestamp string) string {
  220. sha1Contain := sha1.New()
  221. byteSha1 := append(body, append([]byte(timestamp), []byte("2017coins%^&#@(*33")...)...)
  222. sha1Contain.Write(byteSha1)
  223. localSig := hex.EncodeToString(sha1Contain.Sum(nil))
  224. return localSig
  225. }