diff --git a/server/account.go b/server/account.go index 01f97d6..7b74f85 100644 --- a/server/account.go +++ b/server/account.go @@ -122,6 +122,7 @@ modes: // TODO StatusCreated also for first wallet and/or for get auth token? w.WriteHeader(http.StatusCreated) fmt.Fprintf(w, string(response)) + log.Printf("User %s has registered", registerRequest.Email) } // TODO - There's probably a struct-based solution here like with POST/PUT. @@ -228,4 +229,8 @@ func (s *Server) verify(w http.ResponseWriter, req *http.Request) { } fmt.Fprintf(w, "Your account has been verified.") + + // if we really want to log the user's email at some point + // we can put in the effort then to fetch it + log.Printf("User has been verified with token %s", token) } diff --git a/server/wallet.go b/server/wallet.go index 8b79c43..91a05c9 100644 --- a/server/wallet.go +++ b/server/wallet.go @@ -3,6 +3,7 @@ package server import ( "encoding/json" "fmt" + "log" "net/http" "github.com/prometheus/client_golang/prometheus" @@ -30,7 +31,7 @@ func (r *WalletRequest) validate() error { if r.Hmac == "" { return fmt.Errorf("Missing 'hmac'") } - if r.Sequence < 1 { + if r.Sequence < store.InitialWalletSequence { return fmt.Errorf("Missing or zero-value 'sequence'") } return nil @@ -156,4 +157,7 @@ func (s *Server) postWallet(w http.ResponseWriter, req *http.Request) { } fmt.Fprintf(w, string(response)) + if walletRequest.Sequence == store.InitialWalletSequence { + log.Printf("Initial wallet created for user id %d", authToken.UserId) + } } diff --git a/store/store.go b/store/store.go index 8f9af4d..80c9eb0 100644 --- a/store/store.go +++ b/store/store.go @@ -37,6 +37,10 @@ var ( const ( AuthTokenLifespan = time.Hour * 24 * 14 VerifyTokenLifespan = time.Hour * 24 * 2 + + // Eventually it could become variable when we introduce server switching. A user + // might be on a later sequence when they switch from another server. + InitialWalletSequence = 1 ) // For test stubs @@ -281,12 +285,12 @@ func (s *Store) insertFirstWallet( encryptedWallet wallet.EncryptedWallet, hmac wallet.WalletHmac, ) (err error) { - // This will only be used to attempt to insert the first wallet (sequence=1). + // This will only be used to attempt to insert the first wallet (sequence=InitialWalletSequence). // The database will enforce that this will not be set if this user already // has a wallet. _, err = s.db.Exec( "INSERT INTO wallets (user_id, encrypted_wallet, sequence, hmac, updated) VALUES(?,?,?,?, datetime('now'))", - userId, encryptedWallet, 1, hmac, + userId, encryptedWallet, InitialWalletSequence, hmac, ) var sqliteErr sqlite3.Error @@ -309,7 +313,7 @@ func (s *Store) updateWalletToSequence( sequence wallet.Sequence, hmac wallet.WalletHmac, ) (err error) { - // This will be used for wallets with sequence > 1. + // This will be used for wallets with sequence > InitialWalletSequence. // Use the database to enforce that we only update if we are incrementing the sequence. // This way, if two clients attempt to update at the same time, it will return // an error for the second one. @@ -333,22 +337,22 @@ func (s *Store) updateWalletToSequence( return } -// Assumption: Sequence has been validated (>=1) +// Assumption: Sequence has been validated (>=InitialWalletSequence) // Assumption: Auth token has been checked (thus account is verified) func (s *Store) SetWallet(userId auth.UserId, encryptedWallet wallet.EncryptedWallet, sequence wallet.Sequence, hmac wallet.WalletHmac) (err error) { - if sequence == 1 { - // If sequence == 1, the client assumed that this is our first + if sequence == InitialWalletSequence { + // If sequence == InitialWalletSequence, the client assumed that this is our first // wallet. Try to insert. If we get a conflict, the client // assumed incorrectly and we proceed below to return the latest // wallet from the db. err = s.insertFirstWallet(userId, encryptedWallet, hmac) if err == ErrDuplicateWallet { - // A wallet already exists. That means the input sequence should not be 1. + // A wallet already exists. That means the input sequence should not be InitialWalletSequence. // To the caller, this means the sequence was wrong. err = ErrWrongSequence } } else { - // If sequence > 1, the client assumed that it is replacing wallet + // If sequence > InitialWalletSequence, the client assumed that it is replacing wallet // with sequence - 1. Explicitly try to update the wallet with // sequence - 1. If we updated no rows, the client assumed incorrectly // and we proceed below to return the latest wallet from the db. diff --git a/store/wallet_test.go b/store/wallet_test.go index e319b20..7ad3535 100644 --- a/store/wallet_test.go +++ b/store/wallet_test.go @@ -174,7 +174,7 @@ func TestStoreUpdateWallet(t *testing.T) { // NOTE - the "behind the scenes" comments give a view of what we're expecting // to happen, and why we're testing what we are. Sometimes it should insert, // sometimes it should update. It depends on whether it's the first wallet -// submitted, and that's easily determined by sequence=1. However, if we switch +// submitted, and that's easily determined by sequence=store.InitialWalletSequence. However, if we switch // to a database with "upserts" and take advantage of it, what happens behind // the scenes will change a little, so the comments should be updated. Though, // we'd probably best test the same cases.