You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: token/services/identity/role/registry.go
+97-39Lines changed: 97 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,51 @@ type WalletFactory interface {
22
22
NewWallet(ctx context.Context, id idriver.WalletID, role idriver.IdentityRoleType, isIdentitySupport, info idriver.IdentityInfo) (driver.Wallet, error)
23
23
}
24
24
25
+
// WalletIDStatus classifies the outcome of resolving an identity to the wallet id
26
+
// bound to it. It exists so callers branch on an explicit, named state instead of
27
+
// re-deriving intent from the ambiguous "(string, error)" shape — where ("", nil),
28
+
// ("", err) and ("id", nil) each mean something different and the difference is easy
29
+
// to get wrong (see issue #2063).
30
+
typeWalletIDStatusint
31
+
32
+
const (
33
+
// WalletIDUnknown is the zero value and never returned by GetWalletID; it guards
34
+
// against a WalletIDResolution that was constructed without going through GetWalletID.
35
+
WalletIDUnknownWalletIDStatus=iota
36
+
// WalletIDBound means storage holds a wallet id for the identity. WalletID is set.
37
+
WalletIDBound
38
+
// WalletIDUnbound means storage answered authoritatively that the identity has no
39
+
// wallet binding. This is a definitive, successful miss: it is safe to fall through
40
+
// to the next resolution step and, ultimately, to create a wallet.
41
+
WalletIDUnbound
42
+
// WalletIDFailed means the storage lookup itself failed (timeout, connection reset,
43
+
// ...), so whether a binding exists is UNKNOWN. Err carries the cause. Callers MUST
44
+
// NOT treat this as WalletIDUnbound: doing so lets a transient blip masquerade as an
45
+
// unregistered identity and triggers the creation of a duplicate wallet.
46
+
WalletIDFailed
47
+
)
48
+
49
+
// WalletIDResolution is the explicit result of resolving an identity to its bound
50
+
// wallet id. It is the single shared value every wallet-lookup fallback branches on,
51
+
// so the "not found" vs "could not check" distinction is decided once (in GetWalletID)
52
+
// rather than re-inferred at each call site.
53
+
typeWalletIDResolutionstruct {
54
+
// Status is the outcome of the lookup. Always inspect it via Bound/Unbound/Failed
55
+
// before reading the other fields.
56
+
StatusWalletIDStatus
57
+
// WalletID is the bound wallet id; meaningful only when Status is WalletIDBound.
58
+
WalletID idriver.WalletID
59
+
// Err is the underlying storage failure; set only when Status is WalletIDFailed.
60
+
Errerror
61
+
}
62
+
63
+
// Bound reports whether the identity has a wallet id bound to it.
// A storage error must not be treated as "not registered": doing so would
122
-
// let a transient blip fall through to wallet creation and duplicate state.
123
-
returnnil, nil, "", errors.WithMessagef(err, "failed to lookup wallet [%s]", id)
165
+
res:=r.GetWalletID(ctx, passedIdentity)
166
+
ifres.Failed() {
167
+
// A storage failure leaves the binding unknown; it must not be treated as
168
+
// "not registered", or a transient blip would fall through to wallet
169
+
// creation and duplicate state.
170
+
returnnil, nil, "", errors.WithMessagef(res.Err, "failed to lookup wallet [%s]", id)
124
171
}
125
-
iflen(passedWalletID) !=0 {
126
-
r.Logger.DebugfContext(ctx, "no wallet found, there is a wallet for identity [%s]: [%s]", passedIdentity, passedWalletID)
172
+
ifres.Bound() {
173
+
r.Logger.DebugfContext(ctx, "no wallet found, there is a wallet for identity [%s]: [%s]", passedIdentity, res.WalletID)
127
174
// we got a hit
128
175
r.WalletMu.RLock()
129
-
walletEntry, ok=r.Wallets[passedWalletID]
176
+
walletEntry, ok=r.Wallets[res.WalletID]
130
177
r.WalletMu.RUnlock()
131
178
ifok {
132
-
returnwalletEntry, nil, passedWalletID, nil
179
+
returnwalletEntry, nil, res.WalletID, nil
133
180
}
134
-
r.Logger.DebugfContext(ctx, "no wallet found, there is a wallet for identity [%s]: [%s] but it has not been recreated yet", passedIdentity, passedWalletID)
181
+
r.Logger.DebugfContext(ctx, "no wallet found, there is a wallet for identity [%s]: [%s] but it has not been recreated yet", passedIdentity, res.WalletID)
0 commit comments