Example #1
0
// CreateOrder create order through exchange server.
// mode: POST
// url: /api/v1/account/order?coin_pair=[:coin_pair]&type=[:type]&price=[:price]&amt=[:amt]
// params:
// 		coin_pair: order coin pair.
// 		type: order type, can be bid or ask.
// 		price: price.
// 		amt: amount.
func CreateOrder(se Servicer) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		rlt := &pp.EmptyRes{}
		for {
			req, err := makeOrderReq(r)
			if err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}

			a, err := account.GetActive()
			if err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}

			req.Pubkey = pp.PtrString(a.Pubkey)
			var res pp.OrderRes
			if err := sknet.EncryGet(se.GetServAddr(), "/create/order", req, &res); err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrResWithCode(pp.ErrCode_ServerError)
				break
			}

			sendJSON(w, res)
			return
		}
		sendJSON(w, rlt)
	}
}
Example #2
0
// GetCoins get supported coins from exchange server.
func GetCoins(se Servicer) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		rlt := &pp.EmptyRes{}
		for {
			a, err := account.GetActive()
			if err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}
			req := pp.GetCoinsReq{
				Pubkey: pp.PtrString(a.Pubkey),
			}

			var res pp.CoinsRes
			if err := sknet.EncryGet(se.GetServAddr(), "/get/coins", req, &res); err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrResWithCode(pp.ErrCode_ServerError)
				break
			}

			sendJSON(w, res)
			return
		}
		sendJSON(w, rlt)
	}
}
Example #3
0
// Withdraw transaction.
func Withdraw(se Servicer) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		rlt := &pp.EmptyRes{}
		for {
			a, err := account.GetActive()
			if err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}

			cp := r.FormValue("coin_type")
			if cp == "" {
				err := errors.New("coin_type empty")
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}

			amount := r.FormValue("amount")
			if amount == "" {
				rlt = pp.MakeErrRes(errors.New("amount empty"))
				break
			}

			toAddr := r.FormValue("toaddr")
			if toAddr == "" {
				rlt = pp.MakeErrRes(errors.New("toaddr empty"))
				break
			}

			amt, err := strconv.ParseUint(amount, 10, 64)
			if err != nil {
				rlt = pp.MakeErrRes(err)
				break
			}
			req := pp.WithdrawalReq{
				Pubkey:        &a.Pubkey,
				CoinType:      &cp,
				Coins:         &amt,
				OutputAddress: &toAddr,
			}

			var res pp.WithdrawalRes
			if err := sknet.EncryGet(se.GetServAddr(), "/withdrawl", req, &res); err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrResWithCode(pp.ErrCode_ServerError)
				break
			}

			sendJSON(w, res)
			return
		}
		sendJSON(w, rlt)
	}
}
Example #4
0
// GetAccount get account that matchs the condition in url param.
// mode: GET
// url: /api/v1/account?active=[:active]
// params:
// 		active: optional condition, must be 1, if not exist, then retun all accounts.
func GetAccount(se Servicer) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		// get active
		res := struct {
			Result   *pp.Result      `json:"result"`
			Accounts []accountResult `json:"accounts,omitempty"`
		}{}

		active := r.FormValue("active")
		switch active {
		case "1":
			a, err := account.GetActive()
			if err != nil {
				// no active account.
				res.Result = pp.MakeResult(pp.ErrCode_NotExits, err.Error())
				sendJSON(w, &res)
				return
			}

			res.Result = pp.MakeResultWithCode(pp.ErrCode_Success)
			res.Accounts = make([]accountResult, 1)
			res.Accounts[0].Pubkey = a.Pubkey
			res.Accounts[0].WalletID = make(map[string]string)
			for cp, id := range a.WltIDs {
				res.Accounts[0].WalletID[cp] = id
			}
			sendJSON(w, &res)
		case "":
			accounts := account.GetAll()
			res.Result = pp.MakeResultWithCode(pp.ErrCode_Success)
			res.Accounts = func(accounts []account.Account) []accountResult {
				as := make([]accountResult, len(accounts))
				for i, a := range accounts {
					as[i].Pubkey = a.Pubkey
					as[i].WalletID = make(map[string]string)
					for cp, id := range a.WltIDs {
						as[i].WalletID[cp] = id
					}
				}
				return as
			}(accounts)
			sendJSON(w, &res)
		default:
			sendJSON(w, pp.MakeErrResWithCode(pp.ErrCode_WrongRequest))
		}
	}
}
Example #5
0
// CreateWallet api for creating local wallet.
// mode: POST
// url: /api/v1/wallet?type=[:type]&seed=[:seed]
// params:
// 		type: bitcoin or skycoin
// 		seed: wallet seed.
func CreateWallet(se Servicer) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		rlt := &pp.EmptyRes{}
		for {
			// get coin type
			cp := r.FormValue("type")

			// get seed
			sd := r.FormValue("seed")
			if sd == "" {
				rlt = pp.MakeErrRes(errors.New("seed is required"))
				break
			}

			wlt, err := wallet.New(cp, sd)
			if err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrResWithCode(pp.ErrCode_ServerError)
				break
			}

			// bind the wallet to current account.
			a, err := account.GetActive()
			if err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}

			a.WltIDs[cp] = wlt.GetID()
			// update the account.
			account.Set(a)

			res := struct {
				Result *pp.Result `json:"result"`
				ID     string     `json:"id"`
			}{
				Result: pp.MakeResultWithCode(pp.ErrCode_Success),
				ID:     wlt.GetID(),
			}
			sendJSON(w, &res)
			return
		}
		sendJSON(w, rlt)
	}
}
Example #6
0
func getPrivKey(cp string) coin.GetPrivKey {
	return func(addr string) (string, error) {
		a, err := account.GetActive()
		if err != nil {
			return "", err
		}
		wltID := a.WltIDs[cp]
		if wltID == "" {
			return "", fmt.Errorf("does not have %s wallet", cp)
		}

		_, key, err := wallet.GetKeypair(wltID, addr)
		if err != nil {
			return "", err
		}
		return key, nil
	}
}
Example #7
0
func TestInitDir(t *testing.T) {
	// dir, teardown, err := setup(t)
	dir, _, err := setup(t)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Println(dir)
	// defer teardown()
	// check the exitence of dir.
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		t.Fatal("account init dir failed")
	}

	// store some account.
	p, s := cipher.GenerateKeyPair()
	acnt := account.Account{
		Pubkey: p.Hex(),
		Seckey: s.Hex(),
	}

	v := fmt.Sprintf(`{"active_account":"%s","accounts":[{"pubkey":"%s", "seckey":"%s"}]}`, p.Hex(), p.Hex(), s.Hex())

	if err := ioutil.WriteFile(filepath.Join(dir, "data.act"), []byte(v), 0777); err != nil {
		t.Fatal(err)
	}

	// init dir again.
	account.InitDir(dir)

	// check if the account is loaded.
	a, err := account.Get(p.Hex())
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, acnt.Pubkey, a.Pubkey)
	assert.Equal(t, acnt.Seckey, a.Seckey)

	activeAccount, err := account.GetActive()
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, activeAccount.Pubkey, p.Hex())
}
Example #8
0
// GetDepositAddress get deposit address from exchange server.
func GetDepositAddress(se Servicer) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		rlt := &pp.EmptyRes{}
		for {
			a, err := account.GetActive()
			if err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}

			cp := r.FormValue("coin_type")
			if cp == "" {
				err := errors.New("coin type empty")
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}

			req := pp.GetDepositAddrReq{
				Pubkey:   pp.PtrString(a.Pubkey),
				CoinType: pp.PtrString(cp),
			}

			var res pp.GetDepositAddrRes

			if err := sknet.EncryGet(se.GetServAddr(), "/create/deposit_address", req, &res); err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrResWithCode(pp.ErrCode_ServerError)
				break
			}

			sendJSON(w, res)
			return
		}
		sendJSON(w, rlt)
	}
}
Example #9
0
// AdminUpdateBalance update balance of specific account
// mode: PUT
// url: /api/v1/admin/account/balance?dst=[:dst]&coin_type=[:coin_type]&amt=[:amt]
// params:
//      dst: the dst account pubkey, whose balance will be updated.
//      coin_type: skycoin or bitcoin, the coin you want to credit.
//      amt: balance that will be updated.
func AdminUpdateBalance(se Servicer) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		var rlt *pp.EmptyRes
		for {
			// get dst_pubkey
			dstPk := r.FormValue("dst")
			if dstPk == "" {
				err := errors.New("dst pubkey is empty")
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}

			// validate the dst_pubkey
			if _, err := cipher.PubKeyFromHex(dstPk); err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(errors.New("invalid dst pubkey"))
				break
			}

			// get coin type.
			cp := r.FormValue("coin_type")
			if cp == "" {
				err := errors.New("coin_type is empty")
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}

			// get amt
			amt := r.FormValue("amt")
			if amt == "" {
				err := errors.New("amt is empty")
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}
			amount, err := strconv.ParseUint(amt, 10, 64)
			if err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}

			a, err := account.GetActive()
			if err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}
			req := pp.UpdateCreditReq{
				Pubkey:   pp.PtrString(a.Pubkey),
				CoinType: pp.PtrString(cp),
				Amount:   pp.PtrUint64(amount),
				Dst:      pp.PtrString(dstPk),
			}

			res := pp.UpdateCreditRes{}
			if err := sknet.EncryGet(se.GetServAddr(), "/admin/update/credit", req, &res); err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrResWithCode(pp.ErrCode_ServerError)
				break
			}

			sendJSON(w, res)
			return
		}
		sendJSON(w, rlt)
	}
}