Skip to content

Commit

Permalink
return correct data for /purchaseinfo first call
Browse files Browse the repository at this point in the history
  • Loading branch information
itswisdomagain committed Sep 4, 2019
1 parent f89deb1 commit 19cbba1
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions controllers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func (controller *MainController) APIAddress(c web.C, r *http.Request) ([]string
createMultiSig.RedeemScript, poolPubKeyAddr, userPubKeyAddr,
userFeeAddr.EncodeAddress(), importedHeight)

log.Infof("successfully create multisigaddress for user %d", c.Env["APIUserID"])
log.Infof("successfully create multisigaddress for user %d", int(user.Id))

err = controller.StakepooldUpdateUsers(dbMap)
if err != nil {
Expand All @@ -367,8 +367,7 @@ func (controller *MainController) APIAddress(c web.C, r *http.Request) ([]string
func (controller *MainController) APIPurchaseTicket(c web.C, r *http.Request) (*poolapi.PurchaseInfo, codes.Code, string, error) {
userPubKeyAddr := r.FormValue("UserPubKeyAddr")

u, err := validateUserPubKeyAddr(userPubKeyAddr)
if err != nil {
if _, err := validateUserPubKeyAddr(userPubKeyAddr); err != nil {
return nil, codes.InvalidArgument, "address error", err
}

Expand All @@ -387,32 +386,29 @@ func (controller *MainController) APIPurchaseTicket(c web.C, r *http.Request) (*
return purchaseInfo, codes.OK, "APIPurchaseTicket: purchaseinfo retrieved for existing userPubKeyAddr", nil
}

//token := models.NewUserToken()
user = &models.User{
Username: userPubKeyAddr,
Email: userPubKeyAddr,
//EmailToken: token.String(),
EmailVerified: 0,
VoteBits: 1,
VoteBitsVersion: int64(controller.voteVersion),
}
//user.HashPassword(password)

remoteIP := getClientIP(r, controller.realIPHeader)
log.Infof("APIPurchaseTicket POST from %v, created new user account %v. Inserting.", remoteIP, user.Email)

err = models.InsertUser(dbMap, user)
err := models.InsertUser(dbMap, user)
if err != nil {
log.Errorf("Error while creating new user account for ticket purchase: %v", err)
return nil, codes.Unavailable, "system error", errors.New("unable to setup db entry for purchase")
}

// load saved user info from db to get the user id
user = models.GetUserByEmail(dbMap, userPubKeyAddr)
userId := user.Id
userId := int(user.Id)

// Get the ticket address for this user
pooladdress, err := controller.TicketAddressForUserID(int(userId))
pooladdress, err := controller.TicketAddressForUserID(int(user.Id))
if err != nil {
log.Errorf("unable to derive ticket address: %v", err)
return nil, codes.Unavailable, "system error", errors.New("unable to process wallet commands")
Expand All @@ -431,29 +427,22 @@ func (controller *MainController) APIPurchaseTicket(c web.C, r *http.Request) (*

poolPubKeyAddr := poolValidateAddress.PubKeyAddr

p, err := dcrutil.DecodeAddress(poolPubKeyAddr)
if err != nil {
controller.handlePotentialFatalError("DecodeAddress poolPubKeyAddr", err)
if _, err = dcrutil.DecodeAddress(poolPubKeyAddr); err != nil {
return nil, codes.Unavailable, "system error", errors.New("unable to process wallet commands")
}

if controller.RPCIsStopped() {
return nil, codes.Unavailable, "system error", errors.New("unable to process wallet commands")
}
createMultiSig, err := controller.rpcServers.CreateMultisig(1, []dcrutil.Address{p, u})
createMultiSig, err := controller.StakepooldServers.CreateMultisig([]string{poolPubKeyAddr, userPubKeyAddr})
if err != nil {
controller.handlePotentialFatalError("CreateMultisig", err)
return nil, codes.Unavailable, "system error", errors.New("unable to process wallet commands")
}

// Serialize the RedeemScript (hex string -> []byte)
// Serialize the redeem script (hex string -> []byte)
serializedScript, err := hex.DecodeString(createMultiSig.RedeemScript)
if err != nil {
controller.handlePotentialFatalError("CreateMultisig DecodeString", err)
return nil, codes.Unavailable, "system error", errors.New("unable to process wallet commands")
}

// Import the RedeemScript
// Import the redeem script
var importedHeight int64
importedHeight, err = controller.StakepooldServers.ImportScript(serializedScript)
if err != nil {
Expand All @@ -471,18 +460,18 @@ func (controller *MainController) APIPurchaseTicket(c web.C, r *http.Request) (*
createMultiSig.RedeemScript, poolPubKeyAddr, userPubKeyAddr,
userFeeAddr.EncodeAddress(), importedHeight)

log.Infof("successfully create multisigaddress for user %d", c.Env["APIUserID"])
log.Infof("successfully create multisigaddress for user %d", userId)

err = controller.StakepooldUpdateUsers(dbMap)
if err != nil {
log.Warnf("failure to update users: %v", err)
}

purchaseInfo := &poolapi.PurchaseInfo{
PoolAddress: user.UserFeeAddr,
PoolAddress: userFeeAddr.EncodeAddress(),
PoolFees: controller.poolFees,
Script: user.MultiSigScript,
TicketAddress: user.MultiSigAddress,
Script: createMultiSig.RedeemScript,
TicketAddress: createMultiSig.Address,
VoteBits: uint16(user.VoteBits),
}
return purchaseInfo, codes.OK, "APIPurchaseTicket: purchaseinfo generated for userPubKeyAddr", nil
Expand Down

0 comments on commit 19cbba1

Please sign in to comment.