-
Notifications
You must be signed in to change notification settings - Fork 463
/
Types.purs
146 lines (119 loc) · 4.8 KB
/
Types.purs
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
module Contacts.Types
( State
, WalletLibrary
, WalletNickname
, WalletDetails
, WalletInfo(..)
, Wallet(..)
, CardSection(..)
, WalletNicknameError(..)
, WalletIdError(..)
, Action(..)
) where
import Prologue
import Analytics (class IsEvent, defaultEvent, toEvent)
import Clipboard (Action) as Clipboard
import Data.BigInteger (BigInteger)
import Data.Generic.Rep (class Generic)
import Data.Map (Map)
import Data.Newtype (class Newtype)
import Foreign.Class (class Encode, class Decode)
import Foreign.Generic (defaultOptions, genericDecode, genericEncode)
import InputField.Types (Action, State) as InputField
import InputField.Types (class InputFieldError)
import Marlowe.PAB (PlutusAppId)
import Marlowe.Semantics (Assets, MarloweData, MarloweParams, PubKey, PubKeyHash)
import Types (WebData)
type State
= { walletLibrary :: WalletLibrary
, cardSection :: CardSection
, walletNicknameInput :: InputField.State WalletNicknameError
, walletIdInput :: InputField.State WalletIdError
, remoteWalletInfo :: WebData WalletInfo
}
type WalletLibrary
= Map WalletNickname WalletDetails
type WalletNickname
= String
type WalletDetails
= { walletNickname :: WalletNickname
, companionAppId :: PlutusAppId
, marloweAppId :: PlutusAppId
, walletInfo :: WalletInfo
, assets :: Assets
-- this property shouldn't be necessary, but at the moment we are getting too many update notifications
-- through the PAB - so until that bug is fixed, we use this to check whether an update notification
-- really has changed anything
, previousCompanionAppState :: Maybe (Map MarloweParams MarloweData)
}
-- this is the data that the wallet API returns when creating a wallet and when subsequently requesting
-- its "own-public-key"
newtype WalletInfo
= WalletInfo
{ wallet :: Wallet
, pubKey :: Maybe PubKey
, pubKeyHash :: PubKeyHash
}
derive instance newtypeWalletInfo :: Newtype WalletInfo _
derive instance eqWalletInfo :: Eq WalletInfo
derive instance genericWalletInfo :: Generic WalletInfo _
instance encodeWalletInfo :: Encode WalletInfo where
encode value = genericEncode defaultOptions value
instance decodeWalletInfo :: Decode WalletInfo where
decode value = genericDecode defaultOptions value
newtype Wallet
= Wallet String
derive instance newtypeWallet :: Newtype Wallet _
derive instance eqWallet :: Eq Wallet
derive instance genericWallet :: Generic Wallet _
instance encodeWallet :: Encode Wallet where
encode value = genericEncode defaultOptions value
instance decodeWallet :: Decode Wallet where
decode value = genericDecode defaultOptions value
data CardSection
= Home
| ViewWallet WalletDetails
| NewWallet (Maybe String)
derive instance eqCardSection :: Eq CardSection
data WalletNicknameError
= EmptyWalletNickname
| DuplicateWalletNickname
| BadWalletNickname
derive instance eqWalletNicknameError :: Eq WalletNicknameError
instance inputFieldErrorWalletNicknameError :: InputFieldError WalletNicknameError where
inputErrorToString EmptyWalletNickname = "Nickname cannot be blank"
inputErrorToString DuplicateWalletNickname = "Nickname is already in use in your contacts"
inputErrorToString BadWalletNickname = "Nicknames can only contain letters and numbers"
data WalletIdError
= EmptyWalletId
| DuplicateWalletId
| InvalidWalletId
| UnconfirmedWalletId
| NonexistentWalletId
derive instance eqWalletIdError :: Eq WalletIdError
instance inputeFieldErrorWalletIdError :: InputFieldError WalletIdError where
inputErrorToString EmptyWalletId = "Wallet ID cannot be blank"
inputErrorToString DuplicateWalletId = "Wallet ID is already in your contacts"
inputErrorToString InvalidWalletId = "Wallet ID is not valid"
inputErrorToString UnconfirmedWalletId = "Looking up wallet..."
inputErrorToString NonexistentWalletId = "Wallet not found"
data Action
= CloseContactsCard
| SetCardSection CardSection
| SaveWallet (Maybe String)
| CancelNewContactForRole
| WalletNicknameInputAction (InputField.Action WalletNicknameError)
| WalletIdInputAction (InputField.Action WalletIdError)
| SetRemoteWalletInfo (WebData WalletInfo)
| ConnectWallet WalletNickname PlutusAppId
| ClipboardAction Clipboard.Action
instance actionIsEvent :: IsEvent Action where
toEvent CloseContactsCard = Just $ defaultEvent "CloseContactsCard"
toEvent (SetCardSection _) = Just $ defaultEvent "SetCardSection"
toEvent (SaveWallet _) = Just $ defaultEvent "SaveWallet"
toEvent CancelNewContactForRole = Nothing
toEvent (WalletNicknameInputAction inputFieldAction) = toEvent inputFieldAction
toEvent (WalletIdInputAction inputFieldAction) = toEvent inputFieldAction
toEvent (SetRemoteWalletInfo _) = Nothing
toEvent (ConnectWallet _ _) = Just $ defaultEvent "ConnectWallet"
toEvent (ClipboardAction _) = Just $ defaultEvent "ClipboardAction"