bitcoin_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. //"lite"/*local test need*/
  12. )
  13. func Test_newaddr(t *testing.T) {
  14. client := http.DefaultClient
  15. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/createaddr`)
  16. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  17. t.Logf("post url:%v", geturl)
  18. //var b []byte
  19. postdata := fmt.Sprintf(`{"cointype":"BTC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/createaddr"), timestamp))
  20. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  21. if err != nil {
  22. t.Errorf("err:%s", err.Error())
  23. return
  24. }
  25. resp, err := client.Do(request)
  26. if err != nil {
  27. t.Errorf("err:%s", err.Error())
  28. return
  29. }
  30. defer resp.Body.Close()
  31. rbs, err := ioutil.ReadAll(resp.Body)
  32. if err != nil {
  33. t.Errorf("err:%s", err.Error())
  34. return
  35. }
  36. t.Logf("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":"BTC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/coinbalance"), 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":"BTC","starth":486116,"endh":486124,"timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/query/transinfo"), 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. /*
  93. func Test_TransferAccounts(t *testing.T) {
  94. client := http.DefaultClient
  95. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendcoins`)
  96. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  97. t.Logf("post url:%v", geturl)
  98. //var b []byte
  99. postdata := fmt.Sprintf(`{"cointype":"BTC","amount":"0.5","address":"Lg7zNKVUfEJeeCJ5oDMgVS9s2afCQKxrCr","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte ("/coinproxy/sendcoins"), timestamp))
  100. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  101. if err != nil {
  102. t.Errorf("err:%s", err.Error())
  103. return
  104. }
  105. resp, err := client.Do(request)
  106. if err != nil {
  107. t.Errorf("err:%s", err.Error())
  108. return
  109. }
  110. defer resp.Body.Close()
  111. rbs, err := ioutil.ReadAll(resp.Body)
  112. if err != nil {
  113. t.Errorf("err:%s", err.Error())
  114. return
  115. }
  116. t.Logf("read bytes:%v", string(rbs))
  117. return
  118. }
  119. */
  120. func Test_ChainHeight(t *testing.T) {
  121. client := http.DefaultClient
  122. geturl := fmt.Sprintf(`http://118.31.213.53:6784/coinproxy/blockchain/height`)
  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":"BTC","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 Test_AccountsBalance(t *testing.T) {
  148. client := http.DefaultClient
  149. geturl := fmt.Sprintf(`http://118.31.213.53:6784/coinproxy/accountsbalance`)
  150. //geturl := fmt.Sprintf(`http://192.168.1.107:15741/coinproxy/accountsbalance`)
  151. timetamp := fmt.Sprintf("%v", time.Now().Unix())
  152. t.Logf("post url:%v", geturl)
  153. posdata := fmt.Sprintf(`{"cointype":"BTC","timestamp":"%v","signature":"%v"}`, timetamp, Signature([]byte("/coinproxy/accountsbalance"), timetamp))
  154. request, err := http.NewRequest("POST", geturl, strings.NewReader(posdata))
  155. if err != nil {
  156. t.Error("err:%s", err.Error())
  157. return
  158. }
  159. resp, err := client.Do(request)
  160. if err != nil {
  161. t.Errorf("err:%s", err.Error())
  162. return
  163. }
  164. defer resp.Body.Close()
  165. rbs, err := ioutil.ReadAll(resp.Body)
  166. if err != nil {
  167. t.Errorf("err:%s", err.Error())
  168. return
  169. }
  170. t.Logf("read bytes:%v", string(rbs))
  171. }
  172. /*func Test_ListUnspent(t *testing.T) {
  173. client := http.DefaultClient
  174. geturl := fmt.Sprintf(`http://118.31.212.137:6784/coinproxy/listunspent`)
  175. //geturl := fmt.Sprintf(`http://192.168.1.107:15741/coinproxy/listunspent`)
  176. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  177. t.Logf("post url:%v", geturl)
  178. postdata := fmt.Sprintf(`{"cointype":"BTC","address":[],"timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/listunspent"), timestamp))
  179. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  180. if err != nil {
  181. t.Errorf("err:%s", err.Error())
  182. return
  183. }
  184. resp, err := client.Do(request)
  185. if err != nil {
  186. t.Errorf("err:%s", err.Error())
  187. return
  188. }
  189. defer resp.Body.Close()
  190. rbs, err := ioutil.ReadAll(resp.Body)
  191. if err != nil {
  192. t.Errorf("err:%s", err.Error())
  193. return
  194. }
  195. t.Logf("read bytes:%v", string(rbs))
  196. return
  197. }
  198. func Test_SendRawTransaction(t *testing.T) {
  199. client := http.DefaultClient
  200. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendrawtransaction`)
  201. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  202. t.Logf("post url:%v", geturl)
  203. //var b []byte
  204. postdata := fmt.Sprintf(`{"cointype":"BTC","data":"0100000002e7ca9a8950b6bb4b4f5bd2a57d85b6f328e818f5c401586a2dfbe6cdac22011b010000008a4730440220141d4bc8c50b93c13e083f00657688c59863d334051412b3bdeb489f0054a10f02204748ce85de1c16cc6a54ecd1a84ec442b50bb3e2e2826f293a34b1c1ad49632a0141048d4bceb2879580a9d0bcd897c4fe5cfdf490f93bea099785e6680e0c16372b91e124d22b12b40627d2cc098452d7cabbd0f0fb5079fc5c5062ccb4558aec830effffffff0c00ee71246819f7d9f5450151ef6bbdf681d00917cae9e6d3d63ff3fe0f97d9000000008b483045022100b8e2f91ead5eaf0b6c7eade504deb15eb22e531c547d34ffc86891ca1eac31a402202c31e5291541c9ec7a5752e404a7f1ccb6dee875b4c63c042a3388dbf74fb3c60141048d4bceb2879580a9d0bcd897c4fe5cfdf490f93bea099785e6680e0c16372b91e124d22b12b40627d2cc098452d7cabbd0f0fb5079fc5c5062ccb4558aec830effffffff02a08f3e00000000001976a914631134f74ce5f456d2256d29d64657cf3fc8a6c388acc05c1500000000001976a914a1de9978896cdf29aa844942d652d5705e3e2c5a88ac00000000","address":"","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/sendrawtransaction"), timestamp))
  205. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  206. if err != nil {
  207. t.Errorf("err:%s", err.Error())
  208. return
  209. }
  210. resp, err := client.Do(request)
  211. if err != nil {
  212. t.Errorf("err:%s", err.Error())
  213. return
  214. }
  215. defer resp.Body.Close()
  216. rbs, err := ioutil.ReadAll(resp.Body)
  217. if err != nil {
  218. t.Errorf("err:%s", err.Error())
  219. return
  220. }
  221. t.Logf("read bytes:%v", string(rbs))
  222. return
  223. }*/
  224. func Signature(body []byte, timestamp string) string {
  225. sha1Contain := sha1.New()
  226. byteSha1 := append(body, append([]byte(timestamp), []byte("coinsapi^&#@(*33")...)...)
  227. sha1Contain.Write(byteSha1)
  228. localSig := hex.EncodeToString(sha1Contain.Sum(nil))
  229. return localSig
  230. }