Skip to content

Commit d6592ab

Browse files
authored
refactor: change IssuerWallet, AuditorWallet, CertifierWallet to return error (#1496)
Signed-off-by: Storm1289 <divakarsharm2934@gmail.com>
1 parent a93c158 commit d6592ab

10 files changed

Lines changed: 58 additions & 54 deletions

File tree

integration/token/fungible/views/auditor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ func (r *CurrentHoldingView) Call(context view.Context) (interface{}, error) {
224224
assert.NoError(err)
225225
assert.NotNil(tms, "tms not found [%s]", r.TMSID)
226226

227-
w := tms.WalletManager().AuditorWallet(context.Context(), "")
228-
assert.NotNil(w, "failed getting default auditor wallet")
227+
w, err := tms.WalletManager().AuditorWallet(context.Context(), "")
228+
assert.NoError(err, "failed getting default auditor wallet")
229229

230230
auditor, err := ttx.NewAuditor(context, w)
231231
assert.NoError(err, "failed to get auditor instance")

integration/token/fungible/views/checks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ func (m *CheckTTXDBView) Call(context view.Context) (interface{}, error) {
5454
assert.NoError(err, "failed getting management service")
5555
assert.NotNil(tms, "failed to get default tms")
5656
if m.Auditor {
57-
auditorWallet := tms.WalletManager().AuditorWallet(context.Context(), m.AuditorWalletID)
58-
assert.NotNil(auditorWallet, "cannot find auditor wallet [%s]", m.AuditorWalletID)
57+
auditorWallet, err := tms.WalletManager().AuditorWallet(context.Context(), m.AuditorWalletID)
58+
assert.NoError(err, "cannot find auditor wallet [%s]", m.AuditorWalletID)
5959
db, err := ttx.NewAuditor(context, auditorWallet)
6060
assert.NoError(err, "failed to get auditor instance")
6161

integration/token/fungible/views/info.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,13 @@ func (p *DoesWalletExistView) Call(context view.Context) (interface{}, error) {
229229

230230
return err == nil, nil
231231
case IssuerWallet:
232-
return tms.WalletManager().IssuerWallet(context.Context(), p.Wallet) != nil, nil
232+
_, err = tms.WalletManager().IssuerWallet(context.Context(), p.Wallet)
233+
234+
return err == nil, nil
233235
case AuditorWallet:
234-
return tms.WalletManager().AuditorWallet(context.Context(), p.Wallet) != nil, nil
236+
_, err = tms.WalletManager().AuditorWallet(context.Context(), p.Wallet)
237+
238+
return err == nil, nil
235239
default:
236240
_, err = tms.WalletManager().OwnerWallet(context.Context(), p.Wallet)
237241

integration/token/fungible/views/upgrade.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ func (p *TokensUpgradeResponderView) Call(context view.Context) (interface{}, er
150150
// No check is performed for other types.
151151
tms, err := token.GetManagementService(context, token.WithTMSID(upgradeRequest.TMSID))
152152
assert.NoError(err, "failed to lookup TMS [%s]", upgradeRequest.TMSID)
153-
wallet := tms.WalletManager().IssuerWallet(context.Context(), "")
154-
assert.NotNil(wallet, "issuer wallet not found")
153+
wallet, err := tms.WalletManager().IssuerWallet(context.Context(), "")
154+
assert.NoError(err, "issuer wallet not found")
155155

156156
// At this point, the issuer is ready to prepare the token transaction.
157157
// The issuer creates a new token transaction and specifies the auditor that must be contacted to approve the operation.

integration/token/fungible/views/withdraw.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ func (p *WithdrawalResponderView) Call(context view.Context) (interface{}, error
125125
// No check is performed for other types.
126126
tms, err := token.GetManagementService(context, token.WithTMSID(issueRequest.TMSID))
127127
assert.NoError(err, "failed getting management service")
128-
wallet := tms.WalletManager().IssuerWallet(context.Context(), "")
129-
assert.NotNil(wallet, "issuer wallet not found")
128+
wallet, err := tms.WalletManager().IssuerWallet(context.Context(), "")
129+
assert.NoError(err, "issuer wallet not found")
130130

131131
// At this point, the issuer is ready to prepare the token transaction.
132132
// The issuer creates a new token transaction and specifies the auditor that must be contacted to approve the operation.

token/services/certifier/interactive/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ func (c *CertificationService) Call(context view.Context) (interface{}, error) {
124124

125125
logger.Debugf("certify with wallet [%s]", walletID)
126126

127-
w := tms.WalletManager().CertifierWallet(context.Context(), walletID)
128-
if w == nil {
127+
w, err := tms.WalletManager().CertifierWallet(context.Context(), walletID)
128+
if err != nil {
129129
return nil, errors.Errorf("failed getting certifier wallet, wallet [%s] not found [%s:%s][%v]", walletID, cr.Channel, cr.Namespace, cr.IDs)
130130
}
131131

token/services/nfttx/wallet.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ func MyIssuerWallet(context view.Context, opts ...token.ServiceOption) *token.Is
112112
if err != nil {
113113
return nil
114114
}
115-
w := tms.WalletManager().IssuerWallet(context.Context(), "")
116-
if w == nil {
115+
w, err := tms.WalletManager().IssuerWallet(context.Context(), "")
116+
if err != nil {
117117
return nil
118118
}
119119

@@ -128,8 +128,8 @@ func GetIssuerWallet(context view.Context, id string, opts ...token.ServiceOptio
128128
if err != nil {
129129
return nil
130130
}
131-
w := tms.WalletManager().IssuerWallet(context.Context(), id)
132-
if w == nil {
131+
w, err := tms.WalletManager().IssuerWallet(context.Context(), id)
132+
if err != nil {
133133
return nil
134134
}
135135

@@ -144,8 +144,8 @@ func GetIssuerWalletForChannel(context view.Context, channel, id string, opts ..
144144
if err != nil {
145145
return nil
146146
}
147-
w := tms.WalletManager().IssuerWallet(context.Context(), id)
148-
if w == nil {
147+
w, err := tms.WalletManager().IssuerWallet(context.Context(), id)
148+
if err != nil {
149149
return nil
150150
}
151151

@@ -158,8 +158,8 @@ func MyAuditorWallet(context view.Context, opts ...token.ServiceOption) *token.A
158158
if err != nil {
159159
return nil
160160
}
161-
w := tms.WalletManager().AuditorWallet(context.Context(), "")
162-
if w == nil {
161+
w, err := tms.WalletManager().AuditorWallet(context.Context(), "")
162+
if err != nil {
163163
return nil
164164
}
165165

token/services/ttx/wallet.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ func MyIssuerWallet(context view.Context, opts ...token.ServiceOption) *token.Is
9494
if err != nil {
9595
return nil
9696
}
97-
w := tms.WalletManager().IssuerWallet(context.Context(), "")
98-
if w == nil {
97+
w, err := tms.WalletManager().IssuerWallet(context.Context(), "")
98+
if err != nil {
9999
return nil
100100
}
101101

@@ -110,8 +110,8 @@ func GetIssuerWallet(context view.Context, id string, opts ...token.ServiceOptio
110110
if err != nil {
111111
return nil
112112
}
113-
w := tms.WalletManager().IssuerWallet(context.Context(), id)
114-
if w == nil {
113+
w, err := tms.WalletManager().IssuerWallet(context.Context(), id)
114+
if err != nil {
115115
return nil
116116
}
117117

@@ -126,8 +126,8 @@ func GetIssuerWalletForChannel(context view.Context, channel, id string, opts ..
126126
if err != nil {
127127
return nil
128128
}
129-
w := tms.WalletManager().IssuerWallet(context.Context(), id)
130-
if w == nil {
129+
w, err := tms.WalletManager().IssuerWallet(context.Context(), id)
130+
if err != nil {
131131
return nil
132132
}
133133

@@ -140,8 +140,8 @@ func MyAuditorWallet(context view.Context, opts ...token.ServiceOption) *token.A
140140
if err != nil {
141141
return nil
142142
}
143-
w := tms.WalletManager().AuditorWallet(context.Context(), "")
144-
if w == nil {
143+
w, err := tms.WalletManager().AuditorWallet(context.Context(), "")
144+
if err != nil {
145145
return nil
146146
}
147147

@@ -156,8 +156,8 @@ func GetAuditorWallet(context view.Context, opts ...token.ServiceOption) *token.
156156
if err != nil {
157157
return nil
158158
}
159-
w := tms.WalletManager().AuditorWallet(context.Context(), "")
160-
if w == nil {
159+
w, err := tms.WalletManager().AuditorWallet(context.Context(), "")
160+
if err != nil {
161161
return nil
162162
}
163163

token/wallet.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,44 +116,38 @@ func (wm *WalletManager) OwnerWallet(ctx context.Context, id WalletLookupID) (*O
116116

117117
// IssuerWallet returns the issuer wallet bound to the passed identifier, if any is available.
118118
// The identifier can be a label, as defined in the configuration file, an identity or a wallet ID.
119-
// If no wallet is found, it returns nil.
120-
func (wm *WalletManager) IssuerWallet(ctx context.Context, id WalletLookupID) *IssuerWallet {
119+
// If no wallet is found, it returns an error.
120+
func (wm *WalletManager) IssuerWallet(ctx context.Context, id WalletLookupID) (*IssuerWallet, error) {
121121
w, err := wm.walletService.IssuerWallet(ctx, id)
122122
if err != nil {
123-
wm.managementService.logger.DebugfContext(ctx, "failed to get issuer wallet for id [%s]: [%s]", id, err)
124-
125-
return nil
123+
return nil, err
126124
}
127125

128-
return &IssuerWallet{Wallet: &Wallet{w: w, managementService: wm.managementService}, w: w}
126+
return &IssuerWallet{Wallet: &Wallet{w: w, managementService: wm.managementService}, w: w}, nil
129127
}
130128

131129
// AuditorWallet returns the auditor wallet bound to the passed identifier, if any is available.
132130
// The identifier can be a label, as defined in the configuration file, an identity or a wallet ID.
133-
// If no wallet is found, it returns nil.
134-
func (wm *WalletManager) AuditorWallet(ctx context.Context, id WalletLookupID) *AuditorWallet {
131+
// If no wallet is found, it returns an error.
132+
func (wm *WalletManager) AuditorWallet(ctx context.Context, id WalletLookupID) (*AuditorWallet, error) {
135133
w, err := wm.walletService.AuditorWallet(ctx, id)
136134
if err != nil {
137-
wm.managementService.logger.DebugfContext(ctx, "failed to get auditor wallet for id [%s]: [%s]", id, err)
138-
139-
return nil
135+
return nil, err
140136
}
141137

142-
return &AuditorWallet{Wallet: &Wallet{w: w, managementService: wm.managementService}, w: w}
138+
return &AuditorWallet{Wallet: &Wallet{w: w, managementService: wm.managementService}, w: w}, nil
143139
}
144140

145141
// CertifierWallet returns the certifier wallet bound to the passed identifier, if any is available.
146142
// The identifier can be a label, as defined in the configuration file, an identity or a wallet ID.
147-
// If no wallet is found, it returns nil.
148-
func (wm *WalletManager) CertifierWallet(ctx context.Context, id WalletLookupID) *CertifierWallet {
143+
// If no wallet is found, it returns an error.
144+
func (wm *WalletManager) CertifierWallet(ctx context.Context, id WalletLookupID) (*CertifierWallet, error) {
149145
w, err := wm.walletService.CertifierWallet(ctx, id)
150146
if err != nil {
151-
wm.managementService.logger.DebugfContext(ctx, "failed to get certifier wallet for id [%s]: [%s]", id, err)
152-
153-
return nil
147+
return nil, err
154148
}
155149

156-
return &CertifierWallet{Wallet: &Wallet{w: w, managementService: wm.managementService}, w: w}
150+
return &CertifierWallet{Wallet: &Wallet{w: w, managementService: wm.managementService}, w: w}, nil
157151
}
158152

159153
// GetEnrollmentID returns the enrollment ID of passed identity

token/wallet_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ func TestWalletManager_IssuerWallet(t *testing.T) {
183183
mockWS.IssuerWalletReturns(mockIW, nil)
184184

185185
ctx := context.Background()
186-
wallet := wm.IssuerWallet(ctx, "issuer1")
186+
wallet, err := wm.IssuerWallet(ctx, "issuer1")
187187

188+
require.NoError(t, err)
188189
assert.NotNil(t, wallet)
189190
assert.Equal(t, mockIW, wallet.w)
190191
}
@@ -198,8 +199,9 @@ func TestWalletManager_AuditorWallet(t *testing.T) {
198199
mockWS.AuditorWalletReturns(mockAW, nil)
199200

200201
ctx := context.Background()
201-
wallet := wm.AuditorWallet(ctx, "auditor1")
202+
wallet, err := wm.AuditorWallet(ctx, "auditor1")
202203

204+
require.NoError(t, err)
203205
assert.NotNil(t, wallet)
204206
assert.Equal(t, mockAW, wallet.w)
205207
}
@@ -213,8 +215,9 @@ func TestWalletManager_CertifierWallet(t *testing.T) {
213215
mockWS.CertifierWalletReturns(mockCW, nil)
214216

215217
ctx := context.Background()
216-
wallet := wm.CertifierWallet(ctx, "certifier1")
218+
wallet, err := wm.CertifierWallet(ctx, "certifier1")
217219

220+
require.NoError(t, err)
218221
assert.NotNil(t, wallet)
219222
assert.Equal(t, mockCW, wallet.w)
220223
}
@@ -647,8 +650,9 @@ func TestWalletManager_IssuerWallet_Nil(t *testing.T) {
647650
mockWS.IssuerWalletReturns(nil, errors.New("wallet not found"))
648651

649652
ctx := context.Background()
650-
wallet := wm.IssuerWallet(ctx, "unknown")
653+
wallet, err := wm.IssuerWallet(ctx, "unknown")
651654

655+
require.Error(t, err)
652656
assert.Nil(t, wallet)
653657
}
654658

@@ -663,8 +667,9 @@ func TestWalletManager_AuditorWallet_Nil(t *testing.T) {
663667
mockWS.AuditorWalletReturns(nil, errors.New("wallet not found"))
664668

665669
ctx := context.Background()
666-
wallet := wm.AuditorWallet(ctx, "unknown")
670+
wallet, err := wm.AuditorWallet(ctx, "unknown")
667671

672+
require.Error(t, err)
668673
assert.Nil(t, wallet)
669674
}
670675

@@ -679,8 +684,9 @@ func TestWalletManager_CertifierWallet_Nil(t *testing.T) {
679684
mockWS.CertifierWalletReturns(nil, errors.New("wallet not found"))
680685

681686
ctx := context.Background()
682-
wallet := wm.CertifierWallet(ctx, "unknown")
687+
wallet, err := wm.CertifierWallet(ctx, "unknown")
683688

689+
require.Error(t, err)
684690
assert.Nil(t, wallet)
685691
}
686692

0 commit comments

Comments
 (0)