alias-fqdn #9
@ -18,17 +18,20 @@ import (
|
|||||||
"git.defalsify.org/vise.git/persist"
|
"git.defalsify.org/vise.git/persist"
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
"git.defalsify.org/vise.git/state"
|
"git.defalsify.org/vise.git/state"
|
||||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
"git.grassecon.net/grassrootseconomics/common/hex"
|
||||||
|
"git.grassecon.net/grassrootseconomics/common/identity"
|
||||||
|
commonlang "git.grassecon.net/grassrootseconomics/common/lang"
|
||||||
|
"git.grassecon.net/grassrootseconomics/common/person"
|
||||||
|
"git.grassecon.net/grassrootseconomics/common/phone"
|
||||||
|
"git.grassecon.net/grassrootseconomics/common/pin"
|
||||||
|
"git.grassecon.net/grassrootseconomics/sarafu-api/dev"
|
||||||
|
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||||
|
"git.grassecon.net/grassrootseconomics/sarafu-api/remote"
|
||||||
|
"git.grassecon.net/grassrootseconomics/sarafu-vise/config"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/profile"
|
"git.grassecon.net/grassrootseconomics/sarafu-vise/profile"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
||||||
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-api/remote"
|
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||||
"git.grassecon.net/grassrootseconomics/common/hex"
|
|
||||||
commonlang "git.grassecon.net/grassrootseconomics/common/lang"
|
|
||||||
"git.grassecon.net/grassrootseconomics/common/pin"
|
|
||||||
"git.grassecon.net/grassrootseconomics/common/person"
|
|
||||||
"git.grassecon.net/grassrootseconomics/common/phone"
|
|
||||||
"git.grassecon.net/grassrootseconomics/common/identity"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -99,7 +102,7 @@ func NewMenuHandlers(appFlags *asm.FlagParser, userdataStore db.Db, adminstore *
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WithPersister sets persister instance to the handlers.
|
// WithPersister sets persister instance to the handlers.
|
||||||
//func (h *MenuHandlers) WithPersister(pe *persist.Persister) *MenuHandlers {
|
// func (h *MenuHandlers) WithPersister(pe *persist.Persister) *MenuHandlers {
|
||||||
func (h *MenuHandlers) SetPersister(pe *persist.Persister) {
|
func (h *MenuHandlers) SetPersister(pe *persist.Persister) {
|
||||||
if h.pe != nil {
|
if h.pe != nil {
|
||||||
panic("persister already set")
|
panic("persister already set")
|
||||||
@ -108,7 +111,6 @@ func (h *MenuHandlers) SetPersister(pe *persist.Persister) {
|
|||||||
//return h
|
//return h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Init initializes the handler for a new session.
|
// Init initializes the handler for a new session.
|
||||||
func (h *MenuHandlers) Init(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *MenuHandlers) Init(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var r resource.Result
|
var r resource.Result
|
||||||
@ -465,6 +467,10 @@ func (h *MenuHandlers) SaveFirstname(ctx context.Context, sym string, input []by
|
|||||||
logg.ErrorCtxf(ctx, "failed to write firstName entry with", "key", storedb.DATA_FIRST_NAME, "value", temporaryFirstName, "error", err)
|
logg.ErrorCtxf(ctx, "failed to write firstName entry with", "key", storedb.DATA_FIRST_NAME, "value", temporaryFirstName, "error", err)
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
err := h.constructAccountAlias(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
res.FlagSet = append(res.FlagSet, flag_firstname_set)
|
res.FlagSet = append(res.FlagSet, flag_firstname_set)
|
||||||
} else {
|
} else {
|
||||||
if firstNameSet {
|
if firstNameSet {
|
||||||
@ -1080,16 +1086,16 @@ func (h *MenuHandlers) ValidateBlockedNumber(ctx context.Context, sym string, in
|
|||||||
// TODO: split up functino
|
// TODO: split up functino
|
||||||
func (h *MenuHandlers) ValidateRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *MenuHandlers) ValidateRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
|
var AliasAddressResult string
|
||||||
|
var AliasAddress *models.AliasAddress
|
||||||
store := h.userdataStore
|
store := h.userdataStore
|
||||||
|
|
||||||
sessionId, ok := ctx.Value("SessionId").(string)
|
sessionId, ok := ctx.Value("SessionId").(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return res, fmt.Errorf("missing session")
|
return res, fmt.Errorf("missing session")
|
||||||
}
|
}
|
||||||
|
|
||||||
flag_invalid_recipient, _ := h.flagManager.GetFlag("flag_invalid_recipient")
|
flag_invalid_recipient, _ := h.flagManager.GetFlag("flag_invalid_recipient")
|
||||||
flag_invalid_recipient_with_invite, _ := h.flagManager.GetFlag("flag_invalid_recipient_with_invite")
|
flag_invalid_recipient_with_invite, _ := h.flagManager.GetFlag("flag_invalid_recipient_with_invite")
|
||||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
|
||||||
|
|
||||||
recipient := string(input)
|
recipient := string(input)
|
||||||
|
|
||||||
@ -1149,21 +1155,32 @@ func (h *MenuHandlers) ValidateRecipient(ctx context.Context, sym string, input
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "alias":
|
case "alias":
|
||||||
// Call the API to validate and retrieve the address for the alias
|
if strings.Contains(recipient, ".") {
|
||||||
r, aliasErr := h.accountService.CheckAliasAddress(ctx, recipient)
|
AliasAddress, err = h.accountService.CheckAliasAddress(ctx, recipient)
|
||||||
if aliasErr != nil {
|
if err == nil {
|
||||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
AliasAddressResult = AliasAddress.Address
|
||||||
res.Content = recipient
|
}
|
||||||
|
} else {
|
||||||
logg.ErrorCtxf(ctx, "failed on CheckAliasAddress", "error", aliasErr)
|
//Perform a search for each search domain,break on first match
|
||||||
return res, err
|
for _, domain := range config.SearchDomains() {
|
||||||
|
fqdn := fmt.Sprintf("%s.%s", recipient, domain)
|
||||||
|
AliasAddress, err = h.accountService.CheckAliasAddress(ctx, fqdn)
|
||||||
|
if err == nil {
|
||||||
|
AliasAddressResult = AliasAddress.Address
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if AliasAddressResult == "" {
|
||||||
// Alias validation succeeded, save the Ethereum address
|
res.Content = recipient
|
||||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_RECIPIENT, []byte(r.Address))
|
res.FlagSet = append(res.FlagSet, flag_invalid_recipient)
|
||||||
if err != nil {
|
return res, nil
|
||||||
logg.ErrorCtxf(ctx, "failed to write recipient entry with", "key", storedb.DATA_RECIPIENT, "value", r.Address, "error", err)
|
} else {
|
||||||
return res, err
|
err = store.WriteEntry(ctx, sessionId, storedb.DATA_RECIPIENT, []byte(AliasAddressResult))
|
||||||
|
if err != nil {
|
||||||
|
logg.ErrorCtxf(ctx, "failed to write recipient entry with", "key", storedb.DATA_RECIPIENT, "value", AliasAddressResult, "error", err)
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1889,7 +1906,6 @@ func (h *MenuHandlers) GetVoucherDetails(ctx context.Context, sym string, input
|
|||||||
if !ok {
|
if !ok {
|
||||||
return res, fmt.Errorf("missing session")
|
return res, fmt.Errorf("missing session")
|
||||||
}
|
}
|
||||||
|
|
||||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||||
|
|
||||||
// get the active address
|
// get the active address
|
||||||
@ -2134,6 +2150,10 @@ func (h *MenuHandlers) UpdateAllProfileItems(ctx context.Context, sym string, in
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
err = h.constructAccountAlias(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2218,3 +2238,42 @@ func (h *MenuHandlers) persistLanguageCode(ctx context.Context, code string) err
|
|||||||
}
|
}
|
||||||
return h.persistInitialLanguageCode(ctx, sessionId, code)
|
return h.persistInitialLanguageCode(ctx, sessionId, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *MenuHandlers) constructAccountAlias(ctx context.Context) error {
|
||||||
|
var alias string
|
||||||
|
store := h.userdataStore
|
||||||
|
sessionId, ok := ctx.Value("SessionId").(string)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("missing session")
|
||||||
|
}
|
||||||
|
firstName, err := store.ReadEntry(ctx, sessionId, storedb.DATA_FIRST_NAME)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
familyName, err := store.ReadEntry(ctx, sessionId, storedb.DATA_FAMILY_NAME)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
pubKey, err := store.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
aliasInput := fmt.Sprintf("%s%s", firstName, familyName)
|
||||||
|
aliasResult, err := h.accountService.RequestAlias(ctx, string(pubKey), aliasInput)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed to retrieve alias: %s", err.Error())
|
||||||
|
}
|
||||||
|
if _, ok := h.accountService.(*dev.DevAccountService); ok {
|
||||||
|
//If using local dev api,append sarafu.local to the assigned available alias
|
||||||
|
alias = aliasResult.Alias + ".sarafu.local"
|
||||||
|
} else {
|
||||||
|
alias = aliasResult.Alias + ".sarafu.eth"
|
||||||
|
}
|
||||||
|
//Store the alias
|
||||||
|
err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(alias))
|
||||||
|
if err != nil {
|
||||||
|
logg.ErrorCtxf(ctx, "failed to write account alias", "key", storedb.DATA_ACCOUNT_ALIAS, "value", alias, "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user