bitcoin_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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:7783/v1/transbank/blockid`)
  15. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  16. t.Logf("post url:%v", geturl)
  17. postdata := fmt.Sprintf(`{"cointype":"BTC","blockid":"482331","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:7783/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":"BTC","transcationid":"6a14fc8c625c92de83dd26643a3094e1191c7ab6fd4c3387093ae04b27abef49","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:7783/v1/transbank/address`)
  74. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  75. t.Logf("post url:%v", geturl)
  76. //address := "LLw3b23h9XmZNirwSFV3ZD17HwVEGaNyMm"
  77. postdata := fmt.Sprintf(`{"cointype":"BTC","address":"128nRaMd4PSNHhAHGCyJExWqfBJ9mXETZH","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/v1/transbank/address"), timestamp))
  78. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  79. if err != nil {
  80. t.Errorf("err:%s", err.Error())
  81. return
  82. }
  83. resp, err := client.Do(request)
  84. if err != nil {
  85. t.Errorf("err:%s", err.Error())
  86. return
  87. }
  88. defer resp.Body.Close()
  89. rbs, err := ioutil.ReadAll(resp.Body)
  90. if err != nil {
  91. t.Errorf("err:%s", err.Error())
  92. return
  93. }
  94. t.Logf("read bytes:%v", string(rbs))
  95. return
  96. }
  97. func Test_QueryBlockHeight(t *testing.T) {
  98. client := http.DefaultClient
  99. geturl := fmt.Sprintf(`http://101.37.212.15:7783/v1/transbank/blockheight`)
  100. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  101. t.Logf("post url:%v", geturl)
  102. postdata := fmt.Sprintf(`{"cointype":"BTC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/v1/transbank/blockheight"), timestamp))
  103. request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
  104. if err != nil {
  105. t.Errorf("err:%s", err.Error())
  106. return
  107. }
  108. resp, err := client.Do(request)
  109. if err != nil {
  110. t.Errorf("err:%s", err.Error())
  111. return
  112. }
  113. defer resp.Body.Close()
  114. rbs, err := ioutil.ReadAll(resp.Body)
  115. if err != nil {
  116. t.Errorf("err:%s", err.Error())
  117. return
  118. }
  119. t.Logf("read bytes:%v", string(rbs))
  120. return
  121. }
  122. func Signature(body []byte, timestamp string) string {
  123. sha1Contain := sha1.New()
  124. byteSha1 := append(body, append([]byte(timestamp), []byte("coinsapi^&#@(*33")...)...)
  125. sha1Contain.Write(byteSha1)
  126. localSig := hex.EncodeToString(sha1Contain.Sum(nil))
  127. return localSig
  128. }