bucash_test.go 3.9 KB

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