示例#1
0
// Create a wallet if no ID provided.  Otherwise update an existing wallet.
// Name the wallet with "name".
func walletHandlerPOST(gateway *daemon.Gateway, w http.ResponseWriter,
	r *http.Request) {
	id := wallet.WalletID(r.FormValue("id"))
	name := r.FormValue("name")
	if id == "" {
		// Create wallet
		iw := gateway.CreateWallet()
		if iw != nil {
			w := iw.(wallet.Wallet)
			w.SetName(name)
			if err := gateway.SaveWallet(w.GetID()); err != nil {
				m := "Failed to save wallet after renaming: %v"
				logger.Critical(m, err)
			}
		}
		SendOr500(w, iw)
	} else {
		// Update wallet
		iw := gateway.GetWallet(id)
		if iw != nil {
			w := iw.(wallet.Wallet)
			w.SetName(name)
			if err := gateway.SaveWallet(w.GetID()); err != nil {
				m := "Failed to save wallet after renaming: %v"
				logger.Critical(m, err)
			}
		}
		SendOr404(w, iw)
	}
}
示例#2
0
// Returns a wallet by ID if GET.  Creates or updates a wallet if POST.
func walletGet(gateway *daemon.Gateway) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			ret := Wg.GetWallet(wallet.WalletID(r.FormValue("id")))
			SendOr404(w, ret)
		}
	}
}
示例#3
0
// Creates and broadcasts a transaction sending money from one of our wallets
// to destination address.
func walletSpendHandler(gateway *daemon.Gateway) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		//log.Printf("Spend1")

		if r.FormValue("id") == "" {
			Error400(w, "Missing wallet_id")
			return
		}

		walletId := wallet.WalletID(r.FormValue("id"))
		if walletId == "" {
			Error400(w, "Invalid Wallet Id")
			return
		}
		sdst := r.FormValue("dst")
		if sdst == "" {
			Error400(w, "Missing destination address \"dst\"")
			return
		}
		dst, err := cipher.DecodeBase58Address(sdst)
		if err != nil {
			//Error400(w, "Invalid destination address: %v", err)
			Error400(w, "Invalid destination address: %v", err.Error())
			return
		}

		//set fee automatically for now
		/*
			sfee := r.FormValue("fee")
			fee, err := strconv.ParseUint(sfee, 10, 64)
			if err != nil {
				Error400(w, "Invalid \"fee\" value")
				return
			}
		*/
		//var fee uint64 = 0

		scoins := r.FormValue("coins")
		//shours := r.FormValue("hours")
		coins, err := strconv.ParseUint(scoins, 10, 64)
		if err != nil {
			Error400(w, "Invalid \"coins\" value")
			return
		}

		var hours uint64 = 0
		var fee uint64 = 0 //doesnt work/do anything right now

		//MOVE THIS INTO HERE
		ret := Spend(gateway.D, gateway.D.Visor, Wg, walletId, wallet.NewBalance(coins, hours), fee, dst)

		if ret.Error != "" {
			Error400(w, "Spend Failed: %s", ret.Error)
		}
		SendOr404(w, ret)
	}
}
示例#4
0
// Returns a wallet by ID if GET.  Creates or updates a wallet if POST.
func walletHandler(gateway *daemon.Gateway) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			SendOr404(w, gateway.GetWallet(wallet.WalletID(r.FormValue("id"))))
		} else if r.Method == "POST" {
			walletHandlerPOST(gateway, w, r)
		} else {
			Error405(w)
		}
	}
}
示例#5
0
// Returns JSON of pending transactions for user's wallet
func walletTransactionsHandler(gateway *daemon.Gateway) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			wallet := Wg.GetWallet(wallet.WalletID(r.FormValue("id")))
			addresses := wallet.GetAddresses()
			ret := gateway.Visor.GetWalletTransactions(gateway.V, addresses)

			SendOr404(w, ret)
		}
	}
}
示例#6
0
//all this does is update the wallet name
// Does Nothing
func walletUpdate(gateway *daemon.Gateway) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		// Update wallet
		id := wallet.WalletID(r.FormValue("id"))
		//name := r.FormValue("name")
		w1 := Wg.GetWallet(id)
		if w1 != nil {
			if err := Wg.SaveWallet(w1.GetID()); err != nil {
				m := "Failed to save wallet after renaming: %v"
				logger.Critical(m, err)
			}
		}
		iw := wallet.NewReadableWallet(*w1)
		SendOr404(w, iw)
	}
}
示例#7
0
// Returns the wallet's balance, both confirmed and predicted.  The predicted
// balance is the confirmed balance minus the pending spends.
func walletBalanceHandler(gateway *daemon.Gateway) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		id := r.FormValue("id")
		//addr := r.FormValue("addr")

		//r.ParseForm()
		//r.ParseMultipartForm()
		//log.Println(r.Form)

		//r.URL.String()
		r.ParseForm()

		b, err := Wg.GetWalletBalance(gateway.D.Visor.Visor, wallet.WalletID(id))

		if err != nil {
			_ = err
		}
		//log.Printf("%v, %v, %v \n", r.URL.String(), r.RequestURI, r.Form)
		SendOr404(w, b)
	}
}
示例#8
0
// Creates and broadcasts a transaction sending money from one of our wallets
// to destination address.
func walletSpendHandler(gateway *daemon.Gateway) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		walletId := wallet.WalletID(r.FormValue("id"))
		if walletId == "" {
			Error400(w, "Missing wallet_id")
			return
		}
		sdst := r.FormValue("dst")
		if sdst == "" {
			Error400(w, "Missing destination address \"dst\"")
			return
		}
		dst, err := coin.DecodeBase58Address(sdst)
		if err != nil {
			Error400(w, "Invalid destination address")
			return
		}
		sfee := r.FormValue("fee")
		fee, err := strconv.ParseUint(sfee, 10, 64)
		if err != nil {
			Error400(w, "Invalid \"fee\" value")
			return
		}
		scoins := r.FormValue("coins")
		shours := r.FormValue("hours")
		coins, err := strconv.ParseUint(scoins, 10, 64)
		if err != nil {
			Error400(w, "Invalid \"coins\" value")
			return
		}
		hours, err := strconv.ParseUint(shours, 10, 64)
		if err != nil {
			Error400(w, "Invalid \"hours\" value")
			return
		}
		SendOr404(w, gateway.Spend(walletId, visor.NewBalance(coins, hours),
			fee, dst))
	}
}
示例#9
0
// Returns the wallet's balance, both confirmed and predicted.  The predicted
// balance is the confirmed balance minus the pending spends.
func walletBalanceHandler(gateway *daemon.Gateway) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		id := r.FormValue("id")
		SendOr404(w, gateway.GetWalletBalance(wallet.WalletID(id)))
	}
}