litecoin_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package gotest
  2. import (
  3. "crypto/sha1"
  4. "encoding/hex"
  5. "fmt"
  6. "io/ioutil"
  7. //"lite" /*local test need*/
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "time"
  12. )
  13. func Test_QueryByBlockId(t *testing.T) {
  14. client := http.DefaultClient
  15. geturl := fmt.Sprintf(`http://101.37.212.15:7782/v1/transbank/blockid`)
  16. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  17. t.Logf("post url:%v", geturl)
  18. postdata := fmt.Sprintf(`{"cointype":"LTC","blockid":"1266543","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/v1/transbank/blockid"), timestamp))
  19. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  20. if err != nil {
  21. t.Errorf("err:%s", err.Error())
  22. return
  23. }
  24. resp, err := client.Do(request)
  25. if err != nil {
  26. t.Errorf("err:%s", err.Error())
  27. return
  28. }
  29. defer resp.Body.Close()
  30. rbs, err := ioutil.ReadAll(resp.Body)
  31. if err != nil {
  32. t.Errorf("err:%s", err.Error())
  33. return
  34. }
  35. t.Logf("read bytes:%v", string(rbs))
  36. return
  37. }
  38. /*
  39. func Test_QueryByTransIdLoop(t *testing.T) {
  40. for i:=0;i<1000;i++{
  41. Test_QueryByTransId(t)
  42. t.Logf("Test_QueryByTransIdLoop:%v", i)
  43. }
  44. }
  45. */
  46. func Test_QueryByTransId(t *testing.T) {
  47. client := http.DefaultClient
  48. geturl := fmt.Sprintf(`http://101.37.212.15:7782/v1/transbank/transid`)
  49. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  50. t.Logf("post url:%v", geturl)
  51. //transcationid := "fba22732c61d177bb21e0b96ad6a10ee2a8fc19e33156907841453d38a749abd"
  52. postdata := fmt.Sprintf(`{"cointype":"LTC","transcationid":"b25f0bbce5ef839543e4f58b587b04d985df198924668ef0f29a787d1b714297","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/v1/transbank/transid"), timestamp))
  53. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  54. if err != nil {
  55. t.Errorf("err:%s", err.Error())
  56. return
  57. }
  58. resp, err := client.Do(request)
  59. if err != nil {
  60. t.Errorf("err:%s", err.Error())
  61. return
  62. }
  63. defer resp.Body.Close()
  64. rbs, err := ioutil.ReadAll(resp.Body)
  65. if err != nil {
  66. t.Errorf("err:%s", err.Error())
  67. return
  68. }
  69. t.Logf("read bytes :%v", (string(rbs)))
  70. return
  71. }
  72. func Test_QueryByAddress(t *testing.T) {
  73. client := http.DefaultClient
  74. geturl := fmt.Sprintf(`http://101.37.212.15:7782/v1/transbank/address`)
  75. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  76. t.Logf("post url:%v", geturl)
  77. //address := "LLw3b23h9XmZNirwSFV3ZD17HwVEGaNyMm"
  78. postdata := fmt.Sprintf(`{"cointype":"LTC","address":"LbVmdoLtioQaeMeoP9ezvZhsvj3Y7Qg1Ep","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/v1/transbank/address"), timestamp))
  79. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  80. if err != nil {
  81. t.Errorf("err:%s", err.Error())
  82. return
  83. }
  84. resp, err := client.Do(request)
  85. if err != nil {
  86. t.Errorf("err:%s", err.Error())
  87. return
  88. }
  89. defer resp.Body.Close()
  90. rbs, err := ioutil.ReadAll(resp.Body)
  91. if err != nil {
  92. t.Errorf("err:%s", err.Error())
  93. return
  94. }
  95. t.Logf("read bytes:%v", string(rbs))
  96. return
  97. }
  98. func Test_QueryBlockHeight(t *testing.T) {
  99. client := http.DefaultClient
  100. geturl := fmt.Sprintf(`http://101.37.212.15:7782/v1/transbank/blockheight`)
  101. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  102. t.Logf("post url:%v", geturl)
  103. postdata := fmt.Sprintf(`{"cointype":"LTC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/v1/transbank/blockheight"), timestamp))
  104. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  105. if err != nil {
  106. t.Errorf("err:%s", err.Error())
  107. return
  108. }
  109. resp, err := client.Do(request)
  110. if err != nil {
  111. t.Errorf("err:%s", err.Error())
  112. return
  113. }
  114. defer resp.Body.Close()
  115. rbs, err := ioutil.ReadAll(resp.Body)
  116. if err != nil {
  117. t.Errorf("err:%s", err.Error())
  118. return
  119. }
  120. t.Logf("read bytes:%v", string(rbs))
  121. return
  122. }
  123. func Signature(body []byte, timestamp string) string {
  124. sha1Contain := sha1.New()
  125. byteSha1 := append(body, append([]byte(timestamp), []byte("coinsapi^&#@(*33")...)...)
  126. sha1Contain.Write(byteSha1)
  127. localSig := hex.EncodeToString(sha1Contain.Sum(nil))
  128. return localSig
  129. }