bucash_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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":"BCC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/createaddr"), timestamp))
  20. postdata := fmt.Sprintf(`{"cointype":"BCC"}`)
  21. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  22. if err != nil {
  23. t.Errorf("err:%s", err.Error())
  24. return
  25. }
  26. resp, err := client.Do(request)
  27. if err != nil {
  28. t.Errorf("err:%s", err.Error())
  29. return
  30. }
  31. defer resp.Body.Close()
  32. rbs, err := ioutil.ReadAll(resp.Body)
  33. if err != nil {
  34. t.Errorf("err:%s", err.Error())
  35. return
  36. }
  37. t.Logf("read bytes:%v", string(rbs))
  38. return
  39. }
  40. func Test_Balance(t *testing.T) {
  41. client := http.DefaultClient
  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":"BCC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/coinbalance"), 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://localhost:15741/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":"BCC","starth":1,"endh":1243398,"timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/query/transinfo"), 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. /*
  94. func Test_TransferAccounts(t *testing.T) {
  95. client := http.DefaultClient
  96. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendcoins`)
  97. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  98. t.Logf("post url:%v", geturl)
  99. //var b []byte
  100. postdata := fmt.Sprintf(`{"cointype":"BCC","amount":"0.5","address":"Lg7zNKVUfEJeeCJ5oDMgVS9s2afCQKxrCr","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte ("/coinproxy/sendcoins"), 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. */
  121. func Test_ChainHeight(t *testing.T) {
  122. client := http.DefaultClient
  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":"BCC","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_ListUnspent(t *testing.T) {
  148. client := http.DefaultClient
  149. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/listunspent`)
  150. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  151. t.Logf("post url:%v", geturl)
  152. postdata := fmt.Sprintf(`{"cointype":"BCC","address":["1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg","14Tud2jEf4rV3TJssKg7VYuoLh6VgRxXBY"],"timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/listunspent"), timestamp))
  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. func Test_SendRawTransaction(t *testing.T) {
  173. client := http.DefaultClient
  174. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendrawtransaction`)
  175. t.Logf("post url:%v", geturl)
  176. //var b []byte
  177. postdata := fmt.Sprintf(`{"cointype":"BCC","data":"0100000002e7ca9a8950b6bb4b4f5bd2a57d85b6f328e818f5c401586a2dfbe6cdac22011b010000008a4730440220141d4bc8c50b93c13e083f00657688c59863d334051412b3bdeb489f0054a10f02204748ce85de1c16cc6a54ecd1a84ec442b50bb3e2e2826f293a34b1c1ad49632a0141048d4bceb2879580a9d0bcd897c4fe5cfdf490f93bea099785e6680e0c16372b91e124d22b12b40627d2cc098452d7cabbd0f0fb5079fc5c5062ccb4558aec830effffffff0c00ee71246819f7d9f5450151ef6bbdf681d00917cae9e6d3d63ff3fe0f97d9000000008b483045022100b8e2f91ead5eaf0b6c7eade504deb15eb22e531c547d34ffc86891ca1eac31a402202c31e5291541c9ec7a5752e404a7f1ccb6dee875b4c63c042a3388dbf74fb3c60141048d4bceb2879580a9d0bcd897c4fe5cfdf490f93bea099785e6680e0c16372b91e124d22b12b40627d2cc098452d7cabbd0f0fb5079fc5c5062ccb4558aec830effffffff02a08f3e00000000001976a914631134f74ce5f456d2256d29d64657cf3fc8a6c388acc05c1500000000001976a914a1de9978896cdf29aa844942d652d5705e3e2c5a88ac00000000","address":""}`)
  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. }