github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/tools/goc/goc.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"fmt"
     6  	"bytes"
     7  	"strings"
     8  	"net/url"
     9  	"net/http"
    10  	"io/ioutil"
    11  	"archive/zip"
    12  	"encoding/xml"
    13  	"encoding/hex"
    14  	"github.com/piotrnar/gocoin/lib/btc"
    15  	"github.com/piotrnar/gocoin/lib/others/sys"
    16  )
    17  
    18  var (
    19  	HOST string
    20  	SID string
    21  )
    22  
    23  
    24  func http_get(url string) (res []byte) {
    25  	req, _ := http.NewRequest("GET", url, nil)
    26  	if SID!="" {
    27  		req.AddCookie(&http.Cookie{Name:"sid", Value:SID})
    28  	}
    29  	r, er := new(http.Client).Do(req)
    30  	if er != nil {
    31  		println(url, er.Error())
    32  		os.Exit(1)
    33  	}
    34  	if SID=="" {
    35  		for i := range r.Cookies() {
    36  			if r.Cookies()[i].Name=="sid" {
    37  				SID = r.Cookies()[i].Value
    38  				//fmt.Println("sid", SID)
    39  			}
    40  		}
    41  	}
    42  	if r.StatusCode == 200 {
    43  		defer r.Body.Close()
    44  		res, _ = ioutil.ReadAll(r.Body)
    45  	} else {
    46  		println(url, "http.Get returned code", r.StatusCode)
    47  		os.Exit(1)
    48  	}
    49  	return
    50  }
    51  
    52  
    53  func fetch_balance() {
    54  	os.RemoveAll("balance/")
    55  
    56  	d := http_get(HOST+"balance.zip")
    57  	r, er := zip.NewReader(bytes.NewReader(d), int64(len(d)))
    58  	if er != nil {
    59  		println(er.Error())
    60  		os.Exit(1)
    61  	}
    62  
    63  	os.Mkdir("balance/", 0777)
    64  	for _, f := range r.File {
    65  		rc, err := f.Open()
    66  		if err != nil {
    67  			println(err.Error())
    68  			os.Exit(1)
    69  		}
    70  		dat, _ := ioutil.ReadAll(rc)
    71  		rc.Close()
    72  		ioutil.WriteFile(f.Name, dat, 0666)
    73  	}
    74  }
    75  
    76  
    77  func list_wallets() {
    78  	d := http_get(HOST+"wallets.xml")
    79  	var wls struct {
    80  		Wallet [] struct {
    81  			Name string
    82  			Selected bool
    83  		}
    84  	}
    85  	er := xml.Unmarshal(d, &wls)
    86  	if er != nil {
    87  		println(er.Error())
    88  		os.Exit(1)
    89  	}
    90  	for i := range wls.Wallet {
    91  		fmt.Print(wls.Wallet[i].Name)
    92  		if wls.Wallet[i].Selected {
    93  			fmt.Print(" (selected)")
    94  		}
    95  		fmt.Println()
    96  	}
    97  }
    98  
    99  func switch_to_wallet(s string) {
   100  	http_get(HOST+"cfg") // get SID
   101  	u, _ := url.Parse(HOST+"cfg")
   102  	ps := url.Values{}
   103  	ps.Add("sid", SID)
   104  	ps.Add("qwalsel", s)
   105  	u.RawQuery = ps.Encode()
   106  	http_get(u.String())
   107  }
   108  
   109  
   110  func push_tx(rawtx string) {
   111  	dat := sys.GetRawData(rawtx)
   112  	if dat == nil {
   113  		println("Cannot fetch the raw transaction data (specify hexdump or filename)")
   114  		return
   115  	}
   116  
   117  	val := make(url.Values)
   118  	val["rawtx"] = []string{hex.EncodeToString(dat)}
   119  
   120  	r, er := http.PostForm(HOST+"txs", val)
   121  	if er != nil {
   122  		println(er.Error())
   123  		os.Exit(1)
   124  	}
   125  	if r.StatusCode == 200 {
   126  		defer r.Body.Close()
   127  		res, _ := ioutil.ReadAll(r.Body)
   128  		if len(res)>100 {
   129  			txid := btc.NewSha2Hash(dat)
   130  			fmt.Println("TxID", txid.String(), "loaded")
   131  
   132  			http_get(HOST+"cfg") // get SID
   133  			//fmt.Println("sid", SID)
   134  
   135  			u, _ := url.Parse(HOST+"txs2s.xml")
   136  			ps := url.Values{}
   137  			ps.Add("sid", SID)
   138  			ps.Add("send", txid.String())
   139  			u.RawQuery = ps.Encode()
   140  			http_get(u.String())
   141  		}
   142  	} else {
   143  		println("http.Post returned code", r.StatusCode)
   144  		os.Exit(1)
   145  	}
   146  }
   147  
   148  
   149  func show_help() {
   150  	fmt.Println("Specify the command and (optionally) its arguments:")
   151  	fmt.Println("  wal [wallet_name] - switch to a given wallet (or list them)")
   152  	fmt.Println("  bal - creates balance/ folder for current wallet")
   153  	fmt.Println("  ptx <rawtx> - pushes raw tx into the network")
   154  }
   155  
   156  
   157  func main() {
   158  	if len(os.Args)<2 {
   159  		show_help()
   160  		return
   161  	}
   162  
   163  	HOST = os.Getenv("GOCOIN_WEBUI")
   164  	if HOST == "" {
   165  		HOST = "http://127.0.0.1:8833/"
   166  	} else {
   167  		if !strings.HasPrefix(HOST, "http://") {
   168  			HOST = "http://" + HOST
   169  		}
   170  		if !strings.HasSuffix(HOST, "/") {
   171  			HOST = HOST + "/"
   172  		}
   173  	}
   174  	fmt.Println("Gocoin WebUI at", HOST, "(you can overwrite it via env variable GOCOIN_WEBUI)")
   175  
   176  	switch os.Args[1] {
   177  		case "wal":
   178  			if len(os.Args)>2 {
   179  				switch_to_wallet(os.Args[2])
   180  			} else {
   181  				list_wallets()
   182  			}
   183  
   184  		case "bal":
   185  			fetch_balance()
   186  
   187  		case "ptx":
   188  			push_tx(os.Args[2])
   189  
   190  		default:
   191  			show_help()
   192  	}
   193  }