/
account.go
50 lines (43 loc) · 898 Bytes
/
account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"math/rand"
"time"
"golang.org/x/crypto/bcrypt"
)
type TransferRequest struct {
ToNumber int64
Amount int
}
type LoginRequest struct {
Username string
Password string
}
type LoginResponse struct {
Token string
UserID string
}
type AccountAPI struct {
Firstname string
Lastname string
}
type Account struct {
ID int
Firstname string
Lastname string
Number int64
Balance int64
EncryptedPassword string
createdAt time.Time
token string
}
func makeAccount(firstname, lastname string) *Account {
return &Account{
ID: rand.Intn(1000000),
Firstname: firstname,
Lastname: lastname,
Number: int64(rand.Intn(100000)),
}
}
func (a *Account) validPassword(pwd string) bool {
return bcrypt.CompareHashAndPassword([]byte(a.EncryptedPassword), []byte(pwd)) == nil
}