2024-10-30 02:28:55 +01:00
|
|
|
package common
|
2024-09-05 16:12:38 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2024-11-07 07:18:11 +01:00
|
|
|
"errors"
|
2024-11-03 15:34:26 +01:00
|
|
|
|
|
|
|
"git.defalsify.org/vise.git/logging"
|
2024-09-05 16:12:38 +02:00
|
|
|
)
|
|
|
|
|
2024-12-08 22:21:48 +01:00
|
|
|
// DataType is a subprefix value used in association with vise/db.DATATYPE_USERDATA.
|
|
|
|
//
|
|
|
|
// All keys are used only within the context of a single account. Unless otherwise specified, the user context is the session id.
|
|
|
|
//
|
|
|
|
// * The first byte is vise/db.DATATYPE_USERDATA
|
|
|
|
// * The last 2 bytes are the DataTyp value, big-endian.
|
|
|
|
// * The intermediate bytes are the id of the user context.
|
|
|
|
//
|
|
|
|
// All values are strings
|
2024-09-05 16:12:38 +02:00
|
|
|
type DataTyp uint16
|
|
|
|
|
|
|
|
const (
|
2024-12-08 22:21:48 +01:00
|
|
|
// API Tracking id to follow status of account creation
|
2024-12-11 20:13:13 +01:00
|
|
|
DATA_TRACKING_ID = iota
|
2024-12-08 22:21:48 +01:00
|
|
|
// EVM address returned from API on account creation
|
2024-09-05 16:12:38 +02:00
|
|
|
DATA_PUBLIC_KEY
|
2024-12-08 22:21:48 +01:00
|
|
|
// Currently active PIN used to authenticate ussd state change requests
|
2024-09-05 18:50:02 +02:00
|
|
|
DATA_ACCOUNT_PIN
|
2024-12-08 22:21:48 +01:00
|
|
|
// The first name of the user
|
2024-09-06 07:35:01 +02:00
|
|
|
DATA_FIRST_NAME
|
2024-12-08 22:21:48 +01:00
|
|
|
// The last name of the user
|
2024-09-06 07:35:01 +02:00
|
|
|
DATA_FAMILY_NAME
|
2024-12-08 22:21:48 +01:00
|
|
|
// The year-of-birth of the user
|
2024-09-06 07:35:01 +02:00
|
|
|
DATA_YOB
|
2024-12-08 22:21:48 +01:00
|
|
|
// The location of the user
|
2024-09-06 07:35:01 +02:00
|
|
|
DATA_LOCATION
|
2024-12-08 22:21:48 +01:00
|
|
|
// The gender of the user
|
2024-09-06 07:35:01 +02:00
|
|
|
DATA_GENDER
|
2024-12-08 22:21:48 +01:00
|
|
|
// The offerings description of the user
|
2024-09-06 07:35:01 +02:00
|
|
|
DATA_OFFERINGS
|
2024-12-08 22:21:48 +01:00
|
|
|
// The ethereum address of the recipient of an ongoing send request
|
2024-09-06 07:35:01 +02:00
|
|
|
DATA_RECIPIENT
|
2024-12-08 22:21:48 +01:00
|
|
|
// The voucher value amount of an ongoing send request
|
2024-09-06 07:35:01 +02:00
|
|
|
DATA_AMOUNT
|
2024-12-08 22:21:48 +01:00
|
|
|
// A general swap field for temporary values
|
2024-10-31 22:42:23 +01:00
|
|
|
DATA_TEMPORARY_VALUE
|
2024-12-08 22:21:48 +01:00
|
|
|
// Currently active voucher symbol of user
|
2024-10-09 14:38:19 +02:00
|
|
|
DATA_ACTIVE_SYM
|
2024-12-08 22:21:48 +01:00
|
|
|
// Voucher balance of user's currently active voucher
|
2024-10-10 13:48:23 +02:00
|
|
|
DATA_ACTIVE_BAL
|
2024-12-08 22:21:48 +01:00
|
|
|
// String boolean indicating whether use of PIN is blocked
|
2024-10-28 13:47:56 +01:00
|
|
|
DATA_BLOCKED_NUMBER
|
2024-12-08 22:21:48 +01:00
|
|
|
// Reverse mapping of a user's evm address to a session id.
|
2024-10-30 01:59:59 +01:00
|
|
|
DATA_PUBLIC_KEY_REVERSE
|
2024-12-08 22:21:48 +01:00
|
|
|
// Decimal count of the currently active voucher
|
2024-10-25 16:24:09 +02:00
|
|
|
DATA_ACTIVE_DECIMAL
|
2024-12-08 22:21:48 +01:00
|
|
|
// EVM address of the currently active voucher
|
2024-10-25 16:24:09 +02:00
|
|
|
DATA_ACTIVE_ADDRESS
|
2024-12-08 22:21:48 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// List of valid voucher symbols in the user context.
|
2024-12-05 14:31:47 +01:00
|
|
|
DATA_VOUCHER_SYMBOLS DataTyp = 256 + iota
|
2024-12-08 22:21:48 +01:00
|
|
|
// List of voucher balances for vouchers valid in the user context.
|
2024-12-05 14:26:56 +01:00
|
|
|
DATA_VOUCHER_BALANCES
|
2024-12-08 22:21:48 +01:00
|
|
|
// List of voucher decimal counts for vouchers valid in the user context.
|
2024-12-05 14:26:56 +01:00
|
|
|
DATA_VOUCHER_DECIMALS
|
2024-12-08 22:21:48 +01:00
|
|
|
// List of voucher EVM addresses for vouchers valid in the user context.
|
2024-12-05 14:26:56 +01:00
|
|
|
DATA_VOUCHER_ADDRESSES
|
2024-12-08 22:21:48 +01:00
|
|
|
// List of senders for valid transactions in the user context.
|
2024-12-11 20:13:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DATA_TX_SENDERS = 512 + iota
|
2024-12-08 22:21:48 +01:00
|
|
|
// List of recipients for valid transactions in the user context.
|
2024-12-05 15:58:51 +01:00
|
|
|
DATA_TX_RECIPIENTS
|
2024-12-08 22:21:48 +01:00
|
|
|
// List of voucher values for valid transactions in the user context.
|
2024-12-05 15:58:51 +01:00
|
|
|
DATA_TX_VALUES
|
2024-12-08 22:21:48 +01:00
|
|
|
// List of voucher EVM addresses for valid transactions in the user context.
|
2024-12-05 15:58:51 +01:00
|
|
|
DATA_TX_ADDRESSES
|
2024-12-08 22:21:48 +01:00
|
|
|
// List of valid transaction hashes in the user context.
|
2024-12-05 15:58:51 +01:00
|
|
|
DATA_TX_HASHES
|
2024-12-08 22:21:48 +01:00
|
|
|
// List of transaction dates for valid transactions in the user context.
|
2024-12-05 15:58:51 +01:00
|
|
|
DATA_TX_DATES
|
2024-12-08 22:21:48 +01:00
|
|
|
// List of voucher symbols for valid transactions in the user context.
|
2024-12-05 15:58:51 +01:00
|
|
|
DATA_TX_SYMBOLS
|
2024-12-08 22:21:48 +01:00
|
|
|
// List of voucher decimal counts for valid transactions in the user context.
|
2024-12-05 15:58:51 +01:00
|
|
|
DATA_TX_DECIMALS
|
2024-09-05 16:12:38 +02:00
|
|
|
)
|
|
|
|
|
2024-11-03 15:34:26 +01:00
|
|
|
var (
|
|
|
|
logg = logging.NewVanilla().WithDomain("urdt-common")
|
|
|
|
)
|
|
|
|
|
2024-09-05 16:12:38 +02:00
|
|
|
func typToBytes(typ DataTyp) []byte {
|
2024-09-05 16:41:27 +02:00
|
|
|
var b [2]byte
|
|
|
|
binary.BigEndian.PutUint16(b[:], uint16(typ))
|
|
|
|
return b[:]
|
2024-09-05 16:12:38 +02:00
|
|
|
}
|
|
|
|
|
2024-09-07 15:25:29 +02:00
|
|
|
func PackKey(typ DataTyp, data []byte) []byte {
|
2024-09-05 16:12:38 +02:00
|
|
|
v := typToBytes(typ)
|
|
|
|
return append(v, data...)
|
|
|
|
}
|
2024-11-07 07:18:11 +01:00
|
|
|
|
|
|
|
func StringToDataTyp(str string) (DataTyp, error) {
|
|
|
|
switch str {
|
|
|
|
case "DATA_FIRST_NAME":
|
|
|
|
return DATA_FIRST_NAME, nil
|
|
|
|
case "DATA_FAMILY_NAME":
|
|
|
|
return DATA_FAMILY_NAME, nil
|
|
|
|
case "DATA_YOB":
|
|
|
|
return DATA_YOB, nil
|
|
|
|
case "DATA_LOCATION":
|
|
|
|
return DATA_LOCATION, nil
|
|
|
|
case "DATA_GENDER":
|
|
|
|
return DATA_GENDER, nil
|
|
|
|
case "DATA_OFFERINGS":
|
|
|
|
return DATA_OFFERINGS, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0, errors.New("invalid DataTyp string")
|
|
|
|
}
|
|
|
|
}
|
2024-12-04 18:42:47 +01:00
|
|
|
|
2024-12-05 15:50:40 +01:00
|
|
|
// ToBytes converts DataTyp or int to a byte slice
|
|
|
|
func ToBytes[T ~uint16 | int](value T) []byte {
|
2024-12-05 14:58:03 +01:00
|
|
|
bytes := make([]byte, 2)
|
|
|
|
binary.BigEndian.PutUint16(bytes, uint16(value))
|
|
|
|
return bytes
|
|
|
|
}
|