Documentation
¶
Index ¶
- Constants
- Variables
- func AuthenticateRequest(db Database, username, token string, otherServers []string) (bool, *DatabaseTransaction, *Server)
- func BackupDatabase(db Database, writer io.Writer) error
- func ConstantTimeCompare(a, b string) bool
- func ForEach(db Database, f func(*Server) error, saveChanges bool) error
- func GenerateToken() string
- func GenerateTransactionID() (string, int64)
- func GetV2History(summary Summary, appendID bool) (h []string)
- func HomogeniseUsername(username string) string
- func LookupError(code string) (string, string, int)
- func PasteuriseUsername(username string) (res string, runeCount int)
- func PrintASCIIArt()
- func RestoreDatabase(db Database, reader io.Reader) error
- func SeedPRNG()
- func ValidateWebhookURL(rawURL string) (string, bool)
- type Currency
- func CurrencyFromFloat(num *big.Float) Currency
- func CurrencyFromFloat64(num float64) Currency
- func CurrencyFromInt(num *big.Int) Currency
- func CurrencyFromInt64(num int64) Currency
- func CurrencyFromString(num string) Currency
- func GetExchangeRate(db Database, source, target string, amount Currency) (Currency, error)
- func ParseCurrency(num string) (Currency, error)
- func (self Currency) Add(num Currency) Currency
- func (self Currency) Cmp(num Currency) int
- func (self Currency) DeltaString() string
- func (self Currency) Div(num Currency) *big.Float
- func (self Currency) Eq(num Currency) bool
- func (self Currency) Float() *big.Float
- func (self *Currency) GobDecode(data []byte) error
- func (self *Currency) GobEncode() ([]byte, error)
- func (self Currency) Gt(num Currency) bool
- func (self Currency) GtZero() bool
- func (self Currency) Int() *big.Int
- func (self Currency) IsNil() bool
- func (self Currency) IsZero() bool
- func (self Currency) Lt(num Currency) bool
- func (self Currency) LtZero() bool
- func (self Currency) MarshalJSON() ([]byte, error)
- func (self Currency) Neg() Currency
- func (self Currency) RawString() string
- func (self Currency) String() string
- func (self Currency) Sub(num Currency) Currency
- func (self *Currency) UnmarshalJSON(data []byte) error
- type Database
- type DatabaseTransaction
- func (self *DatabaseTransaction) Abort()
- func (self *DatabaseTransaction) CreateServer(name string) (*Server, bool)
- func (self *DatabaseTransaction) Finish()
- func (self *DatabaseTransaction) ForEach(f func(*Server) error, saveChanges bool) error
- func (self *DatabaseTransaction) GetCachedServer(name string) (server *Server, ok bool)
- func (self *DatabaseTransaction) GetOneServer(name string) (server *Server, ok bool)
- func (self *DatabaseTransaction) GetOrCreateServer(name string) (*Server, bool)
- func (self *DatabaseTransaction) GetRawDatabase() Database
- func (self *DatabaseTransaction) GetServers(names ...string) ([]*Server, bool, string)
- func (self *DatabaseTransaction) ListServers() []string
- type EncodedServer
- type Server
- func (self *Server) AddToHistory(transaction Transaction)
- func (self *Server) ChangeBal(num Currency) bool
- func (self *Server) CheckToken(token string) bool
- func (self *Server) Encode() EncodedServer
- func (self *Server) GetBalance() Currency
- func (self *Server) GetExchangeRate(amount Currency, toLurkcoin bool) (Currency, *big.Float)
- func (self *Server) GetHistory() []Transaction
- func (self *Server) GetPendingTransactions() []Transaction
- func (self *Server) GetSummary() Summary
- func (self *Server) GetTargetBalance() Currency
- func (self *Server) HasPendingTransactions() bool
- func (self *Server) IsModified() bool
- func (sourceServer *Server) Pay(source, target string, targetServer *Server, sentAmount Currency, ...) (*Transaction, error)
- func (self *Server) RegenerateToken() string
- func (self *Server) RejectPendingTransaction(id string, tr *DatabaseTransaction)
- func (self *Server) RemoveFirstPendingTransactions(amount int)
- func (self *Server) RemovePendingTransaction(id string)
- func (self *Server) SetModified()
- func (self *Server) SetTargetBalance(targetBalance Currency) bool
- func (self *Server) SetWebhookURL(webhookURL string) (ok bool)
- type ServerCollection
- type Summary
- type Transaction
Constants ¶
const COPYRIGHT = "Copyright © 2020 by luk3yx"
Copyrights should be separated by newlines
const DefaultTargetBalance int64 = 500000
Make a new server The default target balance is currently ¤500,000.
const REPORT_SECURITY = "https://gitlab.com/luk3yx/lurkcoin-core/-/issues/new"
const SOURCE_URL = "https://github.com/luk3yx/lurkcoin-core"
Note that public source code is required by the AGPL
const SYMBOL = "¤"
const VERSION = "3.0.9"
Variables ¶
var MaxTargetBalance = CurrencyFromInt64(500000000)
Functions ¶
func AuthenticateRequest ¶
func ConstantTimeCompare ¶
Returns true if a == b in a constant time. Note that this will however leak string lengths.
func GenerateToken ¶
func GenerateToken() string
Generate a secure random API token. This will probably be around 171 characters long.
func GenerateTransactionID ¶
func GetV2History ¶
A helper used by both lurkcoin/api and lurkcoin/databases.
func HomogeniseUsername ¶
func PasteuriseUsername ¶
Remove control characters and leading+trailing whitespace from a username. HomogeniseUsername(PasteuriseUsername(username)) should always equal HomogeniseUsername(username).
func PrintASCIIArt ¶
func PrintASCIIArt()
func RestoreDatabase ¶
Restore a database. This is not atomic and may result in a partially restored database. TODO: Delete servers that exist in the database but do not exist in the backup.
func SeedPRNG ¶
func SeedPRNG()
Gets a random uint64 with crypto/rand, casts it to an int64 and feeds it to rand.Seed().
func ValidateWebhookURL ¶
Validate a webhook URL, returns the actual URL that should be used and a boolean indicating success.
Types ¶
type Currency ¶
type Currency struct {
// contains filtered or unexported fields
}
Create a custom Currency type that stores read-only currency values.
func CurrencyFromFloat64 ¶
func CurrencyFromInt ¶
func CurrencyFromInt64 ¶
func CurrencyFromString ¶
func GetExchangeRate ¶
Get an exchange rate between two servers
func ParseCurrency ¶
func (Currency) DeltaString ¶
Returns the currency as a human-readable string. Positive numbers are prefixed with +.
func (*Currency) UnmarshalJSON ¶
type Database ¶
type Database interface { // GetServers(serverNames) (servers, ok, badServer) // This must atomically get all servers specified, and if one fails free // the previous ones and return nil, false, <failed server UID>. // NOTE THAT THIS WILL DEADLOCK IF DUPLICATE SERVERs ARE PROVIDED! Use // a DatabaseTransaction object to mitigate this issue. GetServers([]string) ([]*Server, bool, string) // FreeServers(servers, saveChanges) // This must atomically free all servers in servers, and if saveChanges is // true write any changes to the database. FreeServers([]*Server, bool) CreateServer(string) (*Server, bool) ListServers() []string DeleteServer(string) bool }
type DatabaseTransaction ¶
type DatabaseTransaction struct {
// contains filtered or unexported fields
}
An atomic database transaction.
func BeginDbTransaction ¶
func BeginDbTransaction(db Database) *DatabaseTransaction
Creates a new DatabaseTransaction object for a database.
func (*DatabaseTransaction) Abort ¶
func (self *DatabaseTransaction) Abort()
Aborts the transaction and discards any changes made. This is a no-op if Finish() or Abort() have already been called.
func (*DatabaseTransaction) CreateServer ¶
func (self *DatabaseTransaction) CreateServer(name string) (*Server, bool)
Creates a server. This may or may not be able to be reverted with Abort().
func (*DatabaseTransaction) Finish ¶
func (self *DatabaseTransaction) Finish()
Commits the changes made to the database.
func (*DatabaseTransaction) ForEach ¶
func (self *DatabaseTransaction) ForEach(f func(*Server) error, saveChanges bool) error
Iterate over the database. Server objects are freed after f() returns.
func (*DatabaseTransaction) GetCachedServer ¶
func (self *DatabaseTransaction) GetCachedServer(name string) (server *Server, ok bool)
Get a server already in the cache
func (*DatabaseTransaction) GetOneServer ¶
func (self *DatabaseTransaction) GetOneServer(name string) (server *Server, ok bool)
func (*DatabaseTransaction) GetOrCreateServer ¶
func (self *DatabaseTransaction) GetOrCreateServer(name string) (*Server, bool)
Gets a server or creates one if it doesn't exist.
func (*DatabaseTransaction) GetRawDatabase ¶
func (self *DatabaseTransaction) GetRawDatabase() Database
func (*DatabaseTransaction) GetServers ¶
func (self *DatabaseTransaction) GetServers(names ...string) ([]*Server, bool, string)
Get a server. The server will be freed once Finish() or Abort() is called.
func (*DatabaseTransaction) ListServers ¶
func (self *DatabaseTransaction) ListServers() []string
Calls the underlying database's ListServers().
type EncodedServer ¶
type EncodedServer struct { // A version number for breaking changes, because of the way gob works this // can be upgraded to a uint16/uint32 at a later time. Version uint8 `json:"version"` // The server name (not passed through HomogeniseUsername) Name string `json:"name"` // The balance in integer form where 1234 is ¤12.34. Balance *big.Int `json:"balance"` // The target balance in the same format as the above balance. TargetBalance *big.Int `json:"target_balance"` // Other values History []Transaction `json:"history"` PendingTransactions []Transaction `json:"pending_transactions"` Token string `json:"token"` WebhookURL string `json:"webhook_url"` }
"Encoded" servers that have all their values public
func (*EncodedServer) Decode ¶
func (self *EncodedServer) Decode() *Server
type Server ¶
type Server struct { UID string Name string WebhookURL string // contains filtered or unexported fields }
Most mutable fields in Server are private to prevent race conditions. Note that pendingTransactions has to be ordered to retain lurkcoinV2 compatibility. If compatibiltiy is ever dropped, it could possibly become a map[string]Transaction to improve the efficiency of delete operations.
func (*Server) AddToHistory ¶
func (self *Server) AddToHistory(transaction Transaction)
func (*Server) ChangeBal ¶
Changes the user's balance, returns false if the user does not have enough money. This is an atomic operation, changing the balance manually is not recommended.
func (*Server) CheckToken ¶
Check an API token. WARNING: This may leak the length of the stored token, however that is probably already deducible by inspecting GenerateToken().
func (*Server) Encode ¶
func (self *Server) Encode() EncodedServer
func (*Server) GetBalance ¶
func (*Server) GetExchangeRate ¶
Exchange rate calculations are horrible at the moment, however they work (at least I think they work).
func (*Server) GetHistory ¶
func (self *Server) GetHistory() []Transaction
Gets the server's history. The slice returned can be modified, however the transaction objects should not be.
func (*Server) GetPendingTransactions ¶
func (self *Server) GetPendingTransactions() []Transaction
Get a list of pending transactions, similar to GetHistory().
func (*Server) GetSummary ¶
func (*Server) GetTargetBalance ¶
func (*Server) HasPendingTransactions ¶
Returns true if the server has pending transactions.
func (*Server) IsModified ¶
func (*Server) Pay ¶
func (sourceServer *Server) Pay(source, target string, targetServer *Server, sentAmount Currency, localCurrency bool, revertable bool) (*Transaction, error)
Sends a payment.
func (*Server) RegenerateToken ¶
Regenerates the token and returns the new one.
func (*Server) RejectPendingTransaction ¶
func (self *Server) RejectPendingTransaction(id string, tr *DatabaseTransaction)
Reject (and possibly revert) a pending transaction.
func (*Server) RemoveFirstPendingTransactions ¶
Remove the first <amount> pending transactions. This is here to support lurkcoinV2 and probably shouldn't be used outside of that.
func (*Server) RemovePendingTransaction ¶
Remove a pending transaction given its ID.
func (*Server) SetModified ¶
func (self *Server) SetModified()
func (*Server) SetTargetBalance ¶
Sets the target balance.
func (*Server) SetWebhookURL ¶
Validates and sets a webhook URL.
type ServerCollection ¶
type Summary ¶
type Summary struct { UID string `json:"uid"` Name string `json:"name"` Bal Currency `json:"bal"` Balance string `json:"balance"` History []Transaction `json:"history"` InterestRate float64 `json:"interest_rate"` TargetBalance Currency `json:"target_balance"` }
Summaries
type Transaction ¶
type Transaction struct { ID string `json:"id"` Source string `json:"source"` SourceServer string `json:"source_server"` Target string `json:"target"` TargetServer string `json:"target_server"` Amount Currency `json:"amount"` SentAmount Currency `json:"sent_amount"` ReceivedAmount Currency `json:"received_amount"` Time int64 `json:"time"` // If true lurkcoin will attempt to revert the transaction if it is // rejected. The transaction can still be rejected if this is false. Revertable bool `json:"revertable"` }
func MakeTransaction ¶
func MakeTransaction(source, sourceServer, target, targetServer string, amount, sentAmount, receivedAmount Currency) Transaction
func (*Transaction) GetLegacyID ¶
func (self *Transaction) GetLegacyID() int32
func (Transaction) GetTime ¶
func (self Transaction) GetTime() time.Time
Get a time.Time object from the transaction's Time attribute.
func (Transaction) String ¶
func (self Transaction) String() string