term/lookup/db.go

56 lines
1.5 KiB
Go
Raw Permalink Normal View History

2024-10-23 22:01:10 +02:00
package lookup
import (
"context"
2024-11-03 01:34:28 +01:00
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/urdt/ussd/common"
2024-10-23 22:01:10 +02:00
)
2024-11-03 01:34:28 +01:00
var (
logg = logging.NewVanilla().WithDomain("term-lookup")
)
2024-11-03 20:04:44 +01:00
// Identity contains all flavors of identifiers used across stream, api and
// client for a single agent.
type Identity struct {
NormalAddress string
ChecksumAddress string
SessionId string
}
2024-11-03 20:04:44 +01:00
// IdentityFromAddress fully populates and Identity object from a given
// checksum address.
//
// It is the caller's responsibility to ensure that a valid checksum address
// is passed.
2024-11-02 17:08:05 +01:00
func IdentityFromAddress(ctx context.Context, store *common.UserDataStore, address string) (Identity, error) {
var err error
var ident Identity
ident.ChecksumAddress = address
ident.NormalAddress, err = common.NormalizeHex(ident.ChecksumAddress)
if err != nil {
return ident, err
}
ident.SessionId, err = getSessionIdByAddress(ctx, store, ident.NormalAddress)
if err != nil {
return ident, err
}
return ident, nil
}
2024-11-03 20:04:44 +01:00
// load matching session from address from db store.
2024-11-02 17:08:05 +01:00
func getSessionIdByAddress(ctx context.Context, store *common.UserDataStore, address string) (string, error) {
2024-11-03 01:34:28 +01:00
// TODO: replace with userdatastore when double sessionid issue fixed
//r, err := store.ReadEntry(ctx, address, common.DATA_PUBLIC_KEY_REVERSE)
store.Db.SetPrefix(db.DATATYPE_USERDATA)
store.Db.SetSession(address)
r, err := store.Db.Get(ctx, common.PackKey(common.DATA_PUBLIC_KEY_REVERSE, []byte{}))
if err != nil {
return "", err
}
return string(r), nil
2024-10-23 22:01:10 +02:00
}