@@ -3,15 +3,18 @@ package app
33import (
44 "context"
55 "fmt"
6+ "log"
67 "os"
78 "path/filepath"
9+ "runtime/debug"
810 "sync"
11+ "time"
912
13+ "gioui.org/x/explorer"
1014 "github.com/vocdoni/gofirma/vocsign/internal/crypto/pkcs12store"
1115 "github.com/vocdoni/gofirma/vocsign/internal/crypto/systemstore"
1216 "github.com/vocdoni/gofirma/vocsign/internal/model"
1317 "github.com/vocdoni/gofirma/vocsign/internal/storage"
14- "gioui.org/x/explorer"
1518)
1619
1720type Screen int
@@ -25,10 +28,10 @@ const (
2528)
2629
2730type App struct {
28- mu sync.Mutex
31+ mu sync.RWMutex
2932 CurrentScreen Screen
3033 ShowWizard bool
31-
34+
3235 // Services
3336 Store pkcs12store.Store
3437 AuditLogger * storage.AuditLogger
@@ -37,58 +40,105 @@ type App struct {
3740 // State
3841 Identities []pkcs12store.Identity
3942 SystemIdentities []pkcs12store.Identity
40-
43+
4144 // Current Action State
42- CurrentReq * model.SignRequest
43- RawReq []byte
44- ReqError error
45- FetchStatus string
46- SignStatus string
45+ CurrentReq * model.SignRequest
46+ RawReq []byte
47+ ReqError error
48+ FetchStatus string
49+ SignStatus string
4750 SignResponse * model.SignResponse
48-
51+
4952 // UI Actions
5053 RequestURL string
5154 Invalidate func ()
5255}
5356
57+ func (a * App ) SystemIdentitiesSnapshot () []pkcs12store.Identity {
58+ a .mu .RLock ()
59+ defer a .mu .RUnlock ()
60+ out := make ([]pkcs12store.Identity , len (a .SystemIdentities ))
61+ copy (out , a .SystemIdentities )
62+ return out
63+ }
64+
65+ func (a * App ) IdentitiesSnapshot () []pkcs12store.Identity {
66+ a .mu .RLock ()
67+ defer a .mu .RUnlock ()
68+ out := make ([]pkcs12store.Identity , len (a .Identities ))
69+ copy (out , a .Identities )
70+ return out
71+ }
72+
73+ func (a * App ) SetIdentities (ids []pkcs12store.Identity ) {
74+ a .mu .Lock ()
75+ defer a .mu .Unlock ()
76+ out := make ([]pkcs12store.Identity , len (ids ))
77+ copy (out , ids )
78+ a .Identities = out
79+ }
80+
5481func (a * App ) ScanSystemStores (ctx context.Context ) {
82+ start := time .Now ()
83+ log .Printf ("DEBUG: ScanSystemStores started" )
5584 var all []pkcs12store.Identity
5685
5786 // 1. OS-Native Store
5887 osStore := & systemstore.OSStore {Label : "System" }
59- ids , err := osStore .List (ctx )
88+ log .Printf ("DEBUG: ScanSystemStores: scanning OS store %q" , osStore .Label )
89+ ids , err := safeList (osStore .List , ctx , "OS store" )
6090 if err == nil {
6191 all = append (all , ids ... )
92+ log .Printf ("DEBUG: ScanSystemStores: OS store returned %d identities" , len (ids ))
93+ } else {
94+ log .Printf ("DEBUG: ScanSystemStores: OS store error: %v" , err )
6295 }
6396
6497 // 2. NSS Stores
6598 nssStores := systemstore .DiscoverNSSStores ()
99+ log .Printf ("DEBUG: ScanSystemStores: discovered %d NSS stores" , len (nssStores ))
66100 for _ , s := range nssStores {
67- ids , err := s .List (ctx )
101+ log .Printf ("DEBUG: ScanSystemStores: scanning NSS store label=%q profile=%q" , s .Label , s .ProfileDir )
102+ ids , err := safeList (s .List , ctx , "NSS store " + s .Label )
68103 if err == nil {
69104 all = append (all , ids ... )
105+ log .Printf ("DEBUG: ScanSystemStores: NSS store %q returned %d identities" , s .Label , len (ids ))
106+ } else {
107+ log .Printf ("DEBUG: ScanSystemStores: NSS store %q error: %v" , s .Label , err )
70108 }
71109 }
72-
110+
73111 a .mu .Lock ()
74112 defer a .mu .Unlock ()
75-
113+
76114 // Deduplicate based on Fingerprint
77115 seen := make (map [string ]bool )
78116 for _ , id := range a .Identities {
79117 seen [fmt .Sprintf ("%x" , id .Fingerprint256 )] = true
80118 }
81-
119+
82120 var filtered []pkcs12store.Identity
83121 for _ , sid := range all {
84122 fp := fmt .Sprintf ("%x" , sid .Fingerprint256 )
85123 if ! seen [fp ] {
86124 filtered = append (filtered , sid )
87- seen [fp ] = true
125+ seen [fp ] = true
88126 }
89127 }
90-
128+
91129 a .SystemIdentities = filtered
130+ log .Printf ("DEBUG: ScanSystemStores finished in %s, total=%d, new=%d" , time .Since (start ), len (all ), len (filtered ))
131+ }
132+
133+ func safeList (fn func (context.Context ) ([]pkcs12store.Identity , error ), ctx context.Context , label string ) (ids []pkcs12store.Identity , err error ) {
134+ defer func () {
135+ if r := recover (); r != nil {
136+ log .Printf ("ERROR: panic while listing %s: %v\n %s" , label , r , string (debug .Stack ()))
137+ ids = nil
138+ err = fmt .Errorf ("panic while listing %s" , label )
139+ }
140+ }()
141+ return fn (ctx )
92142}
93143
94144func NewApp () (* App , error ) {
@@ -121,7 +171,7 @@ func NewApp() (*App, error) {
121171
122172 // Initial load
123173 ids , _ := store .List (context .Background ())
124- app .Identities = ids
174+ app .SetIdentities ( ids )
125175
126176 if len (ids ) == 0 {
127177 app .ShowWizard = true
0 commit comments