123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- package dcrcash
- import (
- "crypto/sha1"
- "encoding/hex"
- "fmt"
- "io/ioutil"
- "net/http"
- "strings"
- "testing"
- "time"
- )
- /*func Test_newaddr(t *testing.T) {
- client := http.DefaultClient
- geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/createaddr`)
- //timestamp := fmt.Sprintf("%v", time.Now().Unix())
- fmt.Printf("post url:%v", geturl)
- //var b []byte
- //postdata := fmt.Sprintf(`{"cointype":"BCC","timestamp":"%v"}`, timestamp)
- postdata := fmt.Sprintf(`{"cointype":"DCR"}`)
- request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
- if err != nil {
- fmt.Printf("err:%s", err.Error())
- return
- }
- resp, err := client.Do(request)
- if err != nil {
- fmt.Printf("err:%s", err.Error())
- return
- }
- defer resp.Body.Close()
- rbs, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Printf("err:%s", err.Error())
- return
- }
- fmt.Printf("read bytes:%v", string(rbs))
- return
- }*/
- func Test_Balance(t *testing.T) {
- client := http.DefaultClient
- //geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/coinbalance`)
- geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/coinbalance`)
- timestamp := fmt.Sprintf("%v", time.Now().Unix())
- t.Logf("post url:%v", geturl)
- //var b []byte
- postdata := fmt.Sprintf(`{"cointype":"DCR","Sync":false,"timestamp":"%v"}`, timestamp)
- request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- resp, err := client.Do(request)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- defer resp.Body.Close()
- rbs, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- t.Logf("read bytes:%v", string(rbs))
- return
- }
- /*func Test_TransInfo(t *testing.T) {
- //first get block height then test transinfo
- client := http.DefaultClient
- geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/query/transinfo`)
- timestamp := fmt.Sprintf("%v", time.Now().Unix())
- t.Logf("post url:%v", geturl)
- //var b []byte
- postdata := fmt.Sprintf(`{"cointype":"DCR","starth":1,"endh":1243398,"timestamp":"%v"}`, timestamp)
- request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- resp, err := client.Do(request)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- defer resp.Body.Close()
- rbs, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- t.Logf("read bytes:%v", string(rbs))
- return
- }*/
- func Test_ChainHeight(t *testing.T) {
- client := http.DefaultClient
- //geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/blockchain/height`)
- geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/blockchain/height`)
- timestamp := fmt.Sprintf("%v", time.Now().Unix())
- t.Logf("post url:%v", geturl)
- //var b []byte
- postdata := fmt.Sprintf(`{"cointype":"DCR","timestamp":"%v"}`, timestamp)
- request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- resp, err := client.Do(request)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- defer resp.Body.Close()
- rbs, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- t.Logf("read bytes:%v", string(rbs))
- return
- }
- func Test_ListUnspent(t *testing.T) {
- client := http.DefaultClient
- //geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/listunspent`)
- geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/listunspent`)
- timestamp := fmt.Sprintf("%v", time.Now().Unix())
- t.Logf("post url:%v", geturl)
- postdata := fmt.Sprintf(`{"cointype":"DCR","Sync":false,"address":[],"timestamp":"%v"}`, timestamp)
- request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- resp, err := client.Do(request)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- defer resp.Body.Close()
- rbs, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- t.Logf("read bytes:%v", string(rbs))
- return
- }
- func Test_SendRawTransaction(t *testing.T) {
- client := http.DefaultClient
- geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendrawtransaction`)
- //geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/sendrawtransaction`)
- t.Logf("post url:%v", geturl)
- //var b []byte
- postdata := fmt.Sprintf(`{"cointype":"DCR","data":"a4db9908e6d01a347d2fe5040592851b0497da93fc5889e737daf5225f1d66c8","address":""}`)
- request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- resp, err := client.Do(request)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- defer resp.Body.Close()
- rbs, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- t.Logf("read bytes:%v", string(rbs))
- return
- }
- /*
- func Test_SumCoinBalance(t *testing.T) {
- client := http.DefaultClient
- //geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sumcoinbalance`)
- geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/sumcoinbalance`)
- t.Logf("post url:%v", geturl)
- //var b []byte
- postdata := fmt.Sprintf(`{"cointype":"DCR","sumamount":600}`)
- request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- resp, err := client.Do(request)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- defer resp.Body.Close()
- rbs, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- t.Logf("read bytes:%v", string(rbs))
- return
- }
- */
- func Test_AccountsBalance(t *testing.T) {
- client := http.DefaultClient
- geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/accountsbalance`)
- timetamp := fmt.Sprintf("%v", time.Now().Unix())
- t.Logf("post url:%v", geturl)
- posdata := fmt.Sprintf(`{"cointype":"DCR","timestamp":"%v"}`, timetamp)
- request, err := http.NewRequest("POST", geturl, strings.NewReader(posdata))
- if err != nil {
- t.Error("err:%s", err.Error())
- return
- }
- resp, err := client.Do(request)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- defer resp.Body.Close()
- rbs, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- t.Logf("read bytes:%v", string(rbs))
- }
- /*
- func Test_TransferAccounts(t *testing.T) {
- client := http.DefaultClient
- geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendcoins`)
- //geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/sendcoins`)
- timestamp := fmt.Sprintf("%v", time.Now().Unix())
- t.Logf("post url:%v", geturl)
- postdata := fmt.Sprintf(`{"cointype":"DCR","amount":"0.02","address":"DsctbrcDVEF4g4gqTpRF8ciZMqwe4va32uK","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/sendcoins"), timestamp))
- request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- resp, err := client.Do(request)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- defer resp.Body.Close()
- rbs, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- t.Errorf("err:%s", err.Error())
- return
- }
- t.Logf("read bytes:%v", string(rbs))
- return
- }
- */
- func Signature(body []byte, timestamp string) string {
- sha1Contain := sha1.New()
- byteSha1 := append(body, append([]byte(timestamp), []byte("2017coins%^&#@(*33")...)...)
- sha1Contain.Write(byteSha1)
- localSig := hex.EncodeToString(sha1Contain.Sum(nil))
- return localSig
- }
|