bitcoin_test.go 8.0 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://192.168.1.107:15741/coinproxy/accountsbalance`)
  150. timetamp := fmt.Sprintf("%v", time.Now().Unix())
  151. t.Logf("post url:%v", geturl)
  152. posdata := fmt.Sprintf(`{"cointype":"BTC","timestamp":"%v"}`, timetamp)
  153. request, err := http.NewRequest("POST", geturl, strings.NewReader(posdata))
  154. if err != nil {
  155. t.Error("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. }
  171. /*func Test_ListUnspent(t *testing.T) {
  172. client := http.DefaultClient
  173. geturl := fmt.Sprintf(`http://118.31.212.137:6784/coinproxy/listunspent`)
  174. //geturl := fmt.Sprintf(`http://192.168.1.107:15741/coinproxy/listunspent`)
  175. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  176. t.Logf("post url:%v", geturl)
  177. postdata := fmt.Sprintf(`{"cointype":"BTC","address":[],"timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/listunspent"), timestamp))
  178. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  179. if err != nil {
  180. t.Errorf("err:%s", err.Error())
  181. return
  182. }
  183. resp, err := client.Do(request)
  184. if err != nil {
  185. t.Errorf("err:%s", err.Error())
  186. return
  187. }
  188. defer resp.Body.Close()
  189. rbs, err := ioutil.ReadAll(resp.Body)
  190. if err != nil {
  191. t.Errorf("err:%s", err.Error())
  192. return
  193. }
  194. t.Logf("read bytes:%v", string(rbs))
  195. return
  196. }
  197. func Test_SendRawTransaction(t *testing.T) {
  198. client := http.DefaultClient
  199. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendrawtransaction`)
  200. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  201. t.Logf("post url:%v", geturl)
  202. //var b []byte
  203. postdata := fmt.Sprintf(`{"cointype":"BTC","data":"0100000002e7ca9a8950b6bb4b4f5bd2a57d85b6f328e818f5c401586a2dfbe6cdac22011b010000008a4730440220141d4bc8c50b93c13e083f00657688c59863d334051412b3bdeb489f0054a10f02204748ce85de1c16cc6a54ecd1a84ec442b50bb3e2e2826f293a34b1c1ad49632a0141048d4bceb2879580a9d0bcd897c4fe5cfdf490f93bea099785e6680e0c16372b91e124d22b12b40627d2cc098452d7cabbd0f0fb5079fc5c5062ccb4558aec830effffffff0c00ee71246819f7d9f5450151ef6bbdf681d00917cae9e6d3d63ff3fe0f97d9000000008b483045022100b8e2f91ead5eaf0b6c7eade504deb15eb22e531c547d34ffc86891ca1eac31a402202c31e5291541c9ec7a5752e404a7f1ccb6dee875b4c63c042a3388dbf74fb3c60141048d4bceb2879580a9d0bcd897c4fe5cfdf490f93bea099785e6680e0c16372b91e124d22b12b40627d2cc098452d7cabbd0f0fb5079fc5c5062ccb4558aec830effffffff02a08f3e00000000001976a914631134f74ce5f456d2256d29d64657cf3fc8a6c388acc05c1500000000001976a914a1de9978896cdf29aa844942d652d5705e3e2c5a88ac00000000","address":"","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/sendrawtransaction"), timestamp))
  204. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  205. if err != nil {
  206. t.Errorf("err:%s", err.Error())
  207. return
  208. }
  209. resp, err := client.Do(request)
  210. if err != nil {
  211. t.Errorf("err:%s", err.Error())
  212. return
  213. }
  214. defer resp.Body.Close()
  215. rbs, err := ioutil.ReadAll(resp.Body)
  216. if err != nil {
  217. t.Errorf("err:%s", err.Error())
  218. return
  219. }
  220. t.Logf("read bytes:%v", string(rbs))
  221. return
  222. }*/
  223. func Signature(body []byte, timestamp string) string {
  224. sha1Contain := sha1.New()
  225. byteSha1 := append(body, append([]byte(timestamp), []byte("coinsapi^&#@(*33")...)...)
  226. sha1Contain.Write(byteSha1)
  227. localSig := hex.EncodeToString(sha1Contain.Sum(nil))
  228. return localSig
  229. }