-
Notifications
You must be signed in to change notification settings - Fork 71
/
nameservice.go
70 lines (57 loc) · 1.68 KB
/
nameservice.go
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
// Package nameservice reads the zblock/accounts folder and creates a name
// service lookup for the ardan accounts.
package nameservice
import (
"fmt"
"io/fs"
"path"
"path/filepath"
"strings"
"github.com/ardanlabs/blockchain/foundation/blockchain/database"
"github.com/ethereum/go-ethereum/crypto"
)
// NameService maintains a map of accounts for name lookup.
type NameService struct {
accounts map[database.AccountID]string
}
// New constructs an Ardan Name Service with accounts from the zblock/accounts folder.
func New(root string) (*NameService, error) {
ns := NameService{
accounts: make(map[database.AccountID]string),
}
fn := func(fileName string, info fs.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("walkdir failure: %w", err)
}
if path.Ext(fileName) != ".ecdsa" {
return nil
}
privateKey, err := crypto.LoadECDSA(fileName)
if err != nil {
return err
}
accountID := database.PublicKeyToAccountID(privateKey.PublicKey)
ns.accounts[accountID] = strings.TrimSuffix(filepath.Base(fileName), ".ecdsa")
return nil
}
if err := filepath.Walk(root, fn); err != nil {
return nil, fmt.Errorf("walking directory: %w", err)
}
return &ns, nil
}
// Lookup returns the name for the specified account.
func (ns *NameService) Lookup(accountID database.AccountID) string {
name, exists := ns.accounts[accountID]
if !exists {
return string(accountID)
}
return name
}
// Copy returns a copy of the map of names and accounts.
func (ns *NameService) Copy() map[database.AccountID]string {
accounts := make(map[database.AccountID]string, len(ns.accounts))
for account, name := range ns.accounts {
accounts[account] = name
}
return accounts
}