lite_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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":"LTC","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":"LTC","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":"LTC","starth":1,"endh":1243398,"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":"LTC","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":"LTC","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_SendRawTransaction(t *testing.T) {
  147. client := http.DefaultClient
  148. geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendrawtransaction`)
  149. timestamp := fmt.Sprintf("%v", time.Now().Unix())
  150. t.Logf("post url:%v", geturl)
  151. //var b []byte
  152. postdata := fmt.Sprintf(`{"cointype":"LTC","data":"0100000002e7ca9a8950b6bb4b4f5bd2a57d85b6f328e818f5c401586a2dfbe6cdac22011b010000008a4730440220141d4bc8c50b93c13e083f00657688c59863d334051412b3bdeb489f0054a10f02204748ce85de1c16cc6a54ecd1a84ec442b50bb3e2e2826f293a34b1c1ad49632a0141048d4bceb2879580a9d0bcd897c4fe5cfdf490f93bea099785e6680e0c16372b91e124d22b12b40627d2cc098452d7cabbd0f0fb5079fc5c5062ccb4558aec830effffffff0c00ee71246819f7d9f5450151ef6bbdf681d00917cae9e6d3d63ff3fe0f97d9000000008b483045022100b8e2f91ead5eaf0b6c7eade504deb15eb22e531c547d34ffc86891ca1eac31a402202c31e5291541c9ec7a5752e404a7f1ccb6dee875b4c63c042a3388dbf74fb3c60141048d4bceb2879580a9d0bcd897c4fe5cfdf490f93bea099785e6680e0c16372b91e124d22b12b40627d2cc098452d7cabbd0f0fb5079fc5c5062ccb4558aec830effffffff02a08f3e00000000001976a914631134f74ce5f456d2256d29d64657cf3fc8a6c388acc05c1500000000001976a914a1de9978896cdf29aa844942d652d5705e3e2c5a88ac00000000","address":"","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/sendrawtransaction"), 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 Signature(body []byte, timestamp string) string {
  173. sha1Contain := sha1.New()
  174. byteSha1 := append(body, append([]byte(timestamp), []byte("coinsapi^&#@(*33")...)...)
  175. sha1Contain.Write(byteSha1)
  176. localSig := hex.EncodeToString(sha1Contain.Sum(nil))
  177. return localSig
  178. }
  179. //local test is ok,not support remote test by docker deploy so
  180. /*
  181. //get current address of the account
  182. func Test_WalletGetAccountAddress(t *testing.T) {
  183. var liteapi lite.LiteApi
  184. addr, err := liteapi.WalletGetAccountAddress("")
  185. if err == nil {
  186. t.Logf("read WalletGetAccountAddress bytes:%s", addr)
  187. }
  188. if err != nil {
  189. t.Errorf("err:%s", err.Error())
  190. return
  191. }
  192. return
  193. }
  194. //list all address of the account
  195. func Test_WalletAddrList(t *testing.T) {
  196. var liteapi lite.LiteApi
  197. addr, err := liteapi.WalletAddrList("")
  198. if err == nil {
  199. t.Logf("read WalletAddrList bytes:%s", addr)
  200. }
  201. if err != nil {
  202. t.Errorf("err:%s", err.Error())
  203. return
  204. }
  205. return
  206. }
  207. //get wallet info
  208. func Test_WallectInfo(t *testing.T) {
  209. var liteapi lite.LiteApi
  210. addr, err := liteapi.WalletInfo()
  211. if err == nil {
  212. t.Logf("read WallectInfo bytes:%s", addr)
  213. }
  214. if err != nil {
  215. t.Errorf("err:%s", err.Error())
  216. return
  217. }
  218. return
  219. }
  220. // dump wallet address to the file
  221. func Test_WalleExportWalletAddress(t *testing.T) {
  222. var liteapi lite.LiteApi
  223. err := liteapi.WalleDumpWalletAddress("fuzameiwallet")
  224. if err == nil {
  225. t.Logf("read WalleDumpWalletAddress success!")
  226. }
  227. if err != nil {
  228. t.Errorf("err:%s", err.Error())
  229. return
  230. }
  231. return
  232. }
  233. // Get Received By Account
  234. func Test_WalleGetReceivedByAccount(t *testing.T) {
  235. var liteapi lite.LiteApi
  236. filedir, err := liteapi.WalleGetReceivedByAccount("", 1)
  237. if err == nil {
  238. t.Logf("read WalleGetReceivedByAccount bytes:%.8f", filedir)
  239. }
  240. if err != nil {
  241. t.Errorf("err:%s", err.Error())
  242. return
  243. }
  244. return
  245. }
  246. // Get Received By Address
  247. func Test_WalleGetReceivedByAddress(t *testing.T) {
  248. var liteapi lite.LiteApi
  249. filedir, err := liteapi.WalleGetReceivedByAddress("LdRVfDh3p8PR4iaPk2d2PjuvLa67XBWvZJ", 1)
  250. if err == nil {
  251. t.Logf("read WalleGetReceivedByAddress bytes:%.8f", filedir)
  252. }
  253. if err != nil {
  254. t.Errorf("err:%s", err.Error())
  255. return
  256. }
  257. return
  258. }
  259. //set tx fee
  260. func Test_WalletSetTxFee(t *testing.T) {
  261. var liteapi lite.LiteApi
  262. filedir, err := liteapi.WalletSetTxFee(float64(0))
  263. if err == nil {
  264. t.Logf("read WalletSetTxFee bytes:%v", filedir)
  265. }
  266. if err != nil {
  267. t.Errorf("err:%s", err.Error())
  268. return
  269. }
  270. return
  271. }
  272. */