-
Notifications
You must be signed in to change notification settings - Fork 7
/
wallet_repository.go
85 lines (78 loc) · 3.04 KB
/
wallet_repository.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package domain
import (
"context"
)
const (
WalletCreated WalletEventType = iota
WalletUnlocked
WalletLocked
WalletPasswordChanged
WalletAccountCreated
WalletAccountAddressesDerived
WalletAccountDeleted
)
var (
walletTypeString = map[WalletEventType]string{
WalletCreated: "WalletCreated",
WalletUnlocked: "WalletUnlocked",
WalletLocked: "WalletLocked",
WalletPasswordChanged: "WalletPasswordChanged",
WalletAccountCreated: "WalletAccountCreated",
WalletAccountAddressesDerived: "WalletAccountAddressesDerived",
WalletAccountDeleted: "WalletAccountDeleted",
}
)
type WalletEventType int
func (t WalletEventType) String() string {
return walletTypeString[t]
}
// WalletEvent holds info about an event occured within the repository.
type WalletEvent struct {
EventType WalletEventType
AccountName string
AccountBirthdayBlock uint32
AccountAddresses []AddressInfo
}
// WalletRepository is the abstraction for any kind of database intended to
// persist a Wallet.
type WalletRepository interface {
// CreateWallet stores a new Wallet if not yet existing.
// Generates a WalletCreated event if successfull.
CreateWallet(ctx context.Context, wallet *Wallet) error
// GetWallet returns the stored wallet, if existing.
GetWallet(ctx context.Context) (*Wallet, error)
// UnlockWallet attempts to update the status of the Wallet to "unlocked".
// Generates a WalletUnlocked event if successfull.
UnlockWallet(ctx context.Context, password string) error
// LockkWallet updates the status of the Wallet to "locked".
// Generates a WalletLocked event if successfull.
LockWallet(ctx context.Context, password string) error
// UpdateWallet allows to make multiple changes to the Wallet in a
// transactional way.
UpdateWallet(
ctx context.Context, updateFn func(v *Wallet) (*Wallet, error),
) error
// CreateAccount creates a new wallet account with the given name and returns
// its basic info.
// Generates a WalletAccountCreated event if successfull.
CreateAccount(
ctx context.Context, accountName string, birthdayBlock uint32, unconf bool,
) (*AccountInfo, error)
// DeriveNextExternalAddressesForAccount returns one or more new receiving
// addresses for the given account.
// Generates a WalletAccountAddressesDerived event if successfull.
DeriveNextExternalAddressesForAccount(
ctx context.Context, accountName string, numOfAddresses uint64,
) ([]AddressInfo, error)
// DeriveNextInternalAddressesForAccount returns one or more new change
// addresses for the given account.
// Generates a WalletAccountAddressesDerived event if successfull.
DeriveNextInternalAddressesForAccount(
ctx context.Context, accountName string, numOfAddresses uint64,
) ([]AddressInfo, error)
// DeleteAccount deletes the wallet account with the given name.
// Generates a WalletAccountDeleted event if successfull.
DeleteAccount(ctx context.Context, accountName string) error
// GetEventChannel returns the channel of WalletEvents.
GetEventChannel() chan WalletEvent
}