bitcoin_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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://localhost:15741/coinproxy/blockchain/height`)
  123. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  124. t.Logf("post url:%v", geturl)
  125. //var b []byte
  126. postdata := fmt.Sprintf(`{"cointype":"BTC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/blockchain/height"), 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_ListUnspent(t *testing.T) {
  147. client := http.DefaultClient
  148. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/listunspent`)
  149. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  150. t.Logf("post url:%v", geturl)
  151. postdata := fmt.Sprintf(`{"cointype":"BTC","address":[],"timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/listunspent"), timestamp))
  152. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  153. if err != nil {
  154. t.Errorf("err:%s", err.Error())
  155. return
  156. }
  157. resp, err := client.Do(request)
  158. if err != nil {
  159. t.Errorf("err:%s", err.Error())
  160. return
  161. }
  162. defer resp.Body.Close()
  163. rbs, err := ioutil.ReadAll(resp.Body)
  164. if err != nil {
  165. t.Errorf("err:%s", err.Error())
  166. return
  167. }
  168. t.Logf("read bytes:%v", string(rbs))
  169. return
  170. }
  171. func Test_SendRawTransaction(t *testing.T) {
  172. client := http.DefaultClient
  173. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendrawtransaction`)
  174. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  175. t.Logf("post url:%v", geturl)
  176. //var b []byte
  177. postdata := fmt.Sprintf(`{"cointype":"BTC","data":"0100000002e7ca9a8950b6bb4b4f5bd2a57d85b6f328e818f5c401586a2dfbe6cdac22011b010000008a4730440220141d4bc8c50b93c13e083f00657688c59863d334051412b3bdeb489f0054a10f02204748ce85de1c16cc6a54ecd1a84ec442b50bb3e2e2826f293a34b1c1ad49632a0141048d4bceb2879580a9d0bcd897c4fe5cfdf490f93bea099785e6680e0c16372b91e124d22b12b40627d2cc098452d7cabbd0f0fb5079fc5c5062ccb4558aec830effffffff0c00ee71246819f7d9f5450151ef6bbdf681d00917cae9e6d3d63ff3fe0f97d9000000008b483045022100b8e2f91ead5eaf0b6c7eade504deb15eb22e531c547d34ffc86891ca1eac31a402202c31e5291541c9ec7a5752e404a7f1ccb6dee875b4c63c042a3388dbf74fb3c60141048d4bceb2879580a9d0bcd897c4fe5cfdf490f93bea099785e6680e0c16372b91e124d22b12b40627d2cc098452d7cabbd0f0fb5079fc5c5062ccb4558aec830effffffff02a08f3e00000000001976a914631134f74ce5f456d2256d29d64657cf3fc8a6c388acc05c1500000000001976a914a1de9978896cdf29aa844942d652d5705e3e2c5a88ac00000000","address":"","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/sendrawtransaction"), 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 Signature(body []byte, timestamp string) string {
  198. sha1Contain := sha1.New()
  199. byteSha1 := append(body, append([]byte(timestamp), []byte("coinsapi^&#@(*33")...)...)
  200. sha1Contain.Write(byteSha1)
  201. localSig := hex.EncodeToString(sha1Contain.Sum(nil))
  202. return localSig
  203. }