docs(accounts): update docs (backend.go, hub.go)

This commit is contained in:
keke-ka 2024-10-12 19:04:25 +08:00
parent fad7e74a1b
commit 5e608a567c
2 changed files with 21 additions and 0 deletions

View file

@ -37,10 +37,13 @@ type ExternalBackend struct {
signers []accounts.Wallet signers []accounts.Wallet
} }
// Wallets returns a list of wallets associated with the external backend.
func (eb *ExternalBackend) Wallets() []accounts.Wallet { func (eb *ExternalBackend) Wallets() []accounts.Wallet {
return eb.signers return eb.signers
} }
// NewExternalBackend creates a new instance of ExternalBackend by initializing
// an external signer with the provided endpoint.
func NewExternalBackend(endpoint string) (*ExternalBackend, error) { func NewExternalBackend(endpoint string) (*ExternalBackend, error) {
signer, err := NewExternalSigner(endpoint) signer, err := NewExternalSigner(endpoint)
if err != nil { if err != nil {
@ -51,6 +54,8 @@ func NewExternalBackend(endpoint string) (*ExternalBackend, error) {
}, nil }, nil
} }
// Subscribe sets up a subscription for wallet events, which is essentially
// a no-op in this implementation.
func (eb *ExternalBackend) Subscribe(sink chan<- accounts.WalletEvent) event.Subscription { func (eb *ExternalBackend) Subscribe(sink chan<- accounts.WalletEvent) event.Subscription {
return event.NewSubscription(func(quit <-chan struct{}) error { return event.NewSubscription(func(quit <-chan struct{}) error {
<-quit <-quit
@ -69,6 +74,8 @@ type ExternalSigner struct {
cache []accounts.Account cache []accounts.Account
} }
// NewExternalSigner creates a new instance of ExternalSigner by establishing
// a connection to the specified endpoint.
func NewExternalSigner(endpoint string) (*ExternalSigner, error) { func NewExternalSigner(endpoint string) (*ExternalSigner, error) {
client, err := rpc.Dial(endpoint) client, err := rpc.Dial(endpoint)
if err != nil { if err != nil {
@ -87,6 +94,7 @@ func NewExternalSigner(endpoint string) (*ExternalSigner, error) {
return extsigner, nil return extsigner, nil
} }
// URL returns the accounts.URL representation of the ExternalSigner.
func (api *ExternalSigner) URL() accounts.URL { func (api *ExternalSigner) URL() accounts.URL {
return accounts.URL{ return accounts.URL{
Scheme: "extapi", Scheme: "extapi",
@ -106,6 +114,7 @@ func (api *ExternalSigner) Close() error {
return errors.New("operation not supported on external signers") return errors.New("operation not supported on external signers")
} }
// Accounts retrieves the list of accounts from the external signer service.
func (api *ExternalSigner) Accounts() []accounts.Account { func (api *ExternalSigner) Accounts() []accounts.Account {
var accnts []accounts.Account var accnts []accounts.Account
res, err := api.listAccounts() res, err := api.listAccounts()
@ -128,6 +137,7 @@ func (api *ExternalSigner) Accounts() []accounts.Account {
return accnts return accnts
} }
// Contains checks if the specified account exists in the external signer's cached accounts.
func (api *ExternalSigner) Contains(account accounts.Account) bool { func (api *ExternalSigner) Contains(account accounts.Account) bool {
api.cacheMu.RLock() api.cacheMu.RLock()
defer api.cacheMu.RUnlock() defer api.cacheMu.RUnlock()
@ -170,6 +180,7 @@ func (api *ExternalSigner) SignData(account accounts.Account, mimeType string, d
return res, nil return res, nil
} }
// SignText signs a plain text message using the specified account.
func (api *ExternalSigner) SignText(account accounts.Account, text []byte) ([]byte, error) { func (api *ExternalSigner) SignText(account accounts.Account, text []byte) ([]byte, error) {
var signature hexutil.Bytes var signature hexutil.Bytes
var signAddress = common.NewMixedcaseAddress(account.Address) var signAddress = common.NewMixedcaseAddress(account.Address)

View file

@ -86,6 +86,9 @@ type Hub struct {
stateLock sync.RWMutex // Protects the internals of the hub from racey access stateLock sync.RWMutex // Protects the internals of the hub from racey access
} }
// readPairings reads smartcard pairings from a JSON file and populates the
// hub's pairing map. The method attempts to open the "smartcards.json" file
// located in the hub's data directory.
func (hub *Hub) readPairings() error { func (hub *Hub) readPairings() error {
hub.pairings = make(map[string]smartcardPairing) hub.pairings = make(map[string]smartcardPairing)
pairingFile, err := os.Open(filepath.Join(hub.datadir, "smartcards.json")) pairingFile, err := os.Open(filepath.Join(hub.datadir, "smartcards.json"))
@ -112,6 +115,9 @@ func (hub *Hub) readPairings() error {
return nil return nil
} }
// writePairings writes the current smartcard pairings to a JSON file.
// The method opens (or creates) the "smartcards.json" file located in the
// hub's data directory in read/write mode.
func (hub *Hub) writePairings() error { func (hub *Hub) writePairings() error {
pairingFile, err := os.OpenFile(filepath.Join(hub.datadir, "smartcards.json"), os.O_RDWR|os.O_CREATE, 0755) pairingFile, err := os.OpenFile(filepath.Join(hub.datadir, "smartcards.json"), os.O_RDWR|os.O_CREATE, 0755)
if err != nil { if err != nil {
@ -136,6 +142,7 @@ func (hub *Hub) writePairings() error {
return nil return nil
} }
// pairing retrieves the smartcard pairing associated with the given wallet.
func (hub *Hub) pairing(wallet *Wallet) *smartcardPairing { func (hub *Hub) pairing(wallet *Wallet) *smartcardPairing {
if pairing, ok := hub.pairings[string(wallet.PublicKey)]; ok { if pairing, ok := hub.pairings[string(wallet.PublicKey)]; ok {
return &pairing return &pairing
@ -143,6 +150,9 @@ func (hub *Hub) pairing(wallet *Wallet) *smartcardPairing {
return nil return nil
} }
// setPairing sets or removes the smartcard pairing for the given wallet.
// If the pairing parameter is nil, it deletes the pairing entry from the
// hub's pairings map.
func (hub *Hub) setPairing(wallet *Wallet, pairing *smartcardPairing) error { func (hub *Hub) setPairing(wallet *Wallet, pairing *smartcardPairing) error {
if pairing == nil { if pairing == nil {
delete(hub.pairings, string(wallet.PublicKey)) delete(hub.pairings, string(wallet.PublicKey))