Skip to content

Commit

Permalink
client/webserver: add addbond, update register
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc committed Dec 2, 2022
1 parent 96fc414 commit 39030ca
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 13 deletions.
49 changes: 46 additions & 3 deletions client/webserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *WebServer) apiDiscoverAccount(w http.ResponseWriter, r *http.Request) {
return
}
defer zero(pass)
exchangeInfo, paid, err := s.core.DiscoverAccount(form.Addr, pass, cert)
exchangeInfo, paid, err := s.core.DiscoverAccount(form.Addr, pass, cert) // TODO: update when paid return removed
if err != nil {
s.writeAPIError(w, err)
return
Expand Down Expand Up @@ -248,6 +248,47 @@ func (s *WebServer) apiRegister(w http.ResponseWriter, r *http.Request) {
writeJSON(w, simpleAck(), s.indent)
}

// apiPostBond is the handler for the '/postbond' API request.
func (s *WebServer) apiPostBond(w http.ResponseWriter, r *http.Request) {
post := new(postBondForm)
defer post.Password.Clear()
if !readPost(w, r, post) {
return
}
assetID := uint32(42)
if post.AssetID != nil {
assetID = *post.AssetID
}
wallet := s.core.WalletState(assetID)
if wallet == nil {
s.writeAPIError(w, errors.New("no wallet"))
return
}
pass, err := s.resolvePass(post.Password, r)
if err != nil {
s.writeAPIError(w, fmt.Errorf("password error: %w", err))
return
}
defer zero(pass)

_, err = s.core.PostBond(&core.PostBondForm{
Addr: post.Addr,
Cert: []byte(post.Cert),
AppPass: pass,
Bond: post.Bond,
Asset: &assetID,
LockTime: post.LockTime,
})
if err != nil {
s.writeAPIError(w, fmt.Errorf("add bond error: %w", err))
return
}
// There was no error paying the fee, but we must wait on confirmations
// before informing the DEX of the fee payment. Those results will come
// through as a notification.
writeJSON(w, simpleAck(), s.indent)
}

// apiNewWallet is the handler for the '/newwallet' API request.
func (s *WebServer) apiNewWallet(w http.ResponseWriter, r *http.Request) {
form := new(newWalletForm)
Expand Down Expand Up @@ -490,7 +531,7 @@ func (s *WebServer) apiAccountExport(w http.ResponseWriter, r *http.Request) {
return
}
defer zero(pass)
account, err := s.core.AccountExport(pass, form.Host)
account, _, err := s.core.AccountExport(pass, form.Host)
if err != nil {
s.writeAPIError(w, fmt.Errorf("error exporting account: %w", err))
return
Expand All @@ -499,9 +540,11 @@ func (s *WebServer) apiAccountExport(w http.ResponseWriter, r *http.Request) {
res := &struct {
OK bool `json:"ok"`
Account *core.Account `json:"account"`
Bonds []*db.Bond `json:"bonds"`
}{
OK: true,
Account: account,
// Bonds TODO
}
writeJSON(w, res, s.indent)
}
Expand Down Expand Up @@ -545,7 +588,7 @@ func (s *WebServer) apiAccountImport(w http.ResponseWriter, r *http.Request) {
return
}
defer zero(pass)
err = s.core.AccountImport(pass, form.Account)
err = s.core.AccountImport(pass, form.Account, nil /* Bonds TODO */)
if err != nil {
s.writeAPIError(w, fmt.Errorf("error importing account: %w", err))
return
Expand Down
9 changes: 6 additions & 3 deletions client/webserver/live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,9 @@ func (c *TCore) Register(r *core.RegisterForm) (*core.RegisterResult, error) {
c.reg = r
return nil, nil
}
func (c *TCore) PostBond(r *core.PostBondForm) (*core.PostBondResult, error) {
return nil, nil
}
func (c *TCore) EstimateRegistrationTxFee(host string, certI interface{}, assetID uint32) (uint64, error) {
xc := tExchanges[host]
if xc == nil {
Expand Down Expand Up @@ -769,10 +772,10 @@ func (c *TCore) PreOrder(*core.TradeForm) (*core.OrderEstimate, error) {
}, nil
}

func (c *TCore) AccountExport(pw []byte, host string) (*core.Account, error) {
return nil, nil
func (c *TCore) AccountExport(pw []byte, host string) (*core.Account, []*db.Bond, error) {
return nil, nil, nil
}
func (c *TCore) AccountImport(pw []byte, account core.Account) error {
func (c *TCore) AccountImport(pw []byte, account *core.Account, bond []*db.Bond) error {
return nil
}
func (c *TCore) AccountDisable(pw []byte, host string) error { return nil }
Expand Down
14 changes: 12 additions & 2 deletions client/webserver/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ type registrationForm struct {
Cert string `json:"cert"`
Password encode.PassBytes `json:"pass"`
Fee uint64 `json:"fee"`
AssetID *uint32 `json:"asset,omitempty"` // prevent out-of-date frontend from paying fee in BTC
AssetID *uint32 `json:"asset,omitempty"` // prevent omission using BTC
}

// postBondForm is used to post a new bond for an existing DEX account.
type postBondForm struct {
Addr string `json:"addr"`
Cert string `json:"cert"` // may be empty for adding bond to existing account
Password encode.PassBytes `json:"pass"`
Bond uint64 `json:"bond"`
AssetID *uint32 `json:"asset,omitempty"` // prevent omission using BTC
LockTime uint64 `json:"lockTime"`
}

type registrationTxFeeForm struct {
Expand Down Expand Up @@ -112,7 +122,7 @@ type accountExportForm struct {

type accountImportForm struct {
Pass encode.PassBytes `json:"pw"`
Account core.Account `json:"account"`
Account *core.Account `json:"account"`
}

type accountDisableForm struct {
Expand Down
6 changes: 4 additions & 2 deletions client/webserver/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type clientCore interface {
Exchanges() map[string]*core.Exchange
Exchange(host string) (*core.Exchange, error)
Register(*core.RegisterForm) (*core.RegisterResult, error)
PostBond(form *core.PostBondForm) (*core.PostBondResult, error)
Login(pw []byte) error
InitializeClient(pw, seed []byte) error
AssetBalance(assetID uint32) (*core.WalletBalance, error)
Expand Down Expand Up @@ -124,8 +125,8 @@ type clientCore interface {
Order(oid dex.Bytes) (*core.Order, error)
MaxBuy(host string, base, quote uint32, rate uint64) (*core.MaxOrderEstimate, error)
MaxSell(host string, base, quote uint32) (*core.MaxOrderEstimate, error)
AccountExport(pw []byte, host string) (*core.Account, error)
AccountImport(pw []byte, account core.Account) error
AccountExport(pw []byte, host string) (*core.Account, []*db.Bond, error)
AccountImport(pw []byte, account *core.Account, bonds []*db.Bond) error
AccountDisable(pw []byte, host string) error
IsInitialized() bool
ExportSeed(pw []byte) ([]byte, error)
Expand Down Expand Up @@ -375,6 +376,7 @@ func New(cfg *Config) (*WebServer, error) {
apiAuth.Get("/user", s.apiUser)
apiAuth.Post("/defaultwalletcfg", s.apiDefaultWalletCfg)
apiAuth.Post("/register", s.apiRegister)
apiAuth.Post("/postbond", s.apiPostBond)
apiAuth.Post("/newwallet", s.apiNewWallet)
apiAuth.Post("/openwallet", s.apiOpenWallet)
apiAuth.Post("/depositaddress", s.apiNewDepositAddress)
Expand Down
10 changes: 7 additions & 3 deletions client/webserver/webserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type TCore struct {
syncFeed core.BookFeed
syncErr error
regErr error
postBondErr error
loginErr error
logoutErr error
initErr error
Expand Down Expand Up @@ -95,6 +96,9 @@ func (c *TCore) DiscoverAccount(dexAddr string, pw []byte, certI interface{}) (*
return nil, false, nil
}
func (c *TCore) Register(r *core.RegisterForm) (*core.RegisterResult, error) { return nil, c.regErr }
func (c *TCore) PostBond(r *core.PostBondForm) (*core.PostBondResult, error) {
return nil, c.postBondErr
}
func (c *TCore) EstimateRegistrationTxFee(host string, certI interface{}, assetID uint32) (uint64, error) {
return 0, nil
}
Expand Down Expand Up @@ -211,10 +215,10 @@ func (c *TCore) MaxSell(host string, base, quote uint32) (*core.MaxOrderEstimate
func (c *TCore) PreOrder(*core.TradeForm) (*core.OrderEstimate, error) {
return nil, nil
}
func (c *TCore) AccountExport(pw []byte, host string) (*core.Account, error) {
return nil, nil
func (c *TCore) AccountExport(pw []byte, host string) (*core.Account, []*db.Bond, error) {
return nil, nil, nil
}
func (c *TCore) AccountImport(pw []byte, account core.Account) error {
func (c *TCore) AccountImport(pw []byte, account *core.Account, bonds []*db.Bond) error {
return nil
}
func (c *TCore) AccountDisable(pw []byte, host string) error { return nil }
Expand Down

0 comments on commit 39030ca

Please sign in to comment.