|
| 1 | +// pkg/accounts/account.go |
| 2 | + |
| 3 | +package accounts |
| 4 | + |
| 5 | +import ( |
| 6 | + "crypto/ecdsa" |
| 7 | + "github.com/ethereum/go-ethereum/crypto" |
| 8 | + libp2pCrypto "github.com/libp2p/go-libp2p/core/crypto" |
| 9 | + "github.com/libp2p/go-libp2p/core/peer" |
| 10 | + "github.com/unpackdev/fdb/logger" |
| 11 | + "github.com/unpackdev/fdb/rbac" |
| 12 | + "github.com/unpackdev/fdb/share" |
| 13 | + "github.com/unpackdev/fdb/types" |
| 14 | + |
| 15 | + "github.com/pkg/errors" |
| 16 | + "github.com/sasha-s/go-deadlock" |
| 17 | +) |
| 18 | + |
| 19 | +// Ensure Account implements the share.Account interface |
| 20 | +var _ share.Account = (*Account)(nil) |
| 21 | + |
| 22 | +// Account represents a Decentralized Identifier with associated cryptographic keys and permissions. |
| 23 | +type Account struct { |
| 24 | + logger logger.Logger |
| 25 | + rbacMgr *rbac.Manager |
| 26 | + id string // String representation of the peer ID |
| 27 | + signerType types.SignerType // SignerType associated with the account |
| 28 | + address types.Address // Derived address from MasterPublicKey |
| 29 | + peerID peer.ID // Associated peer.ID object for easier manipulation |
| 30 | + name string // Name of the account for descriptive purposes |
| 31 | + comment string // Optional comment or description for the account |
| 32 | + masterPrivateKey libp2pCrypto.PrivKey // Private key associated with libp2p PeerID, used as the master private key for all signers |
| 33 | + masterPublicKey libp2pCrypto.PubKey // Public key associated with libp2p PeerID, used as the master public key for all signers |
| 34 | + roles []types.Role |
| 35 | + extraPermissions map[types.Role][]types.Permission |
| 36 | + mu deadlock.RWMutex // Mutex for thread-safe operations |
| 37 | +} |
| 38 | + |
| 39 | +// NewConsensusAccount initializes Account used by the validators. |
| 40 | +// TODO: This needs to be extended with roles and many more things but for now it's this |
| 41 | +func NewConsensusAccount(logger logger.Logger, peerId peer.ID, address types.Address, pubKey libp2pCrypto.PubKey, roles []types.Role) (*Account, error) { |
| 42 | + account := &Account{ |
| 43 | + logger: logger, |
| 44 | + peerID: peerId, |
| 45 | + id: peerId.String(), |
| 46 | + address: address, |
| 47 | + masterPublicKey: pubKey, |
| 48 | + roles: roles, |
| 49 | + } |
| 50 | + |
| 51 | + return account, nil |
| 52 | +} |
| 53 | + |
| 54 | +// NewAccount initializes a new Account with all the cryptographic keys and metadata. |
| 55 | +func NewAccount( |
| 56 | + logger logger.Logger, |
| 57 | + peerID peer.ID, |
| 58 | + peerSk libp2pCrypto.PrivKey, |
| 59 | + peerPk libp2pCrypto.PubKey, |
| 60 | + signerType types.SignerType, |
| 61 | + name, comment string, |
| 62 | + roles []types.Role, |
| 63 | + extraPermissions map[types.Role][]types.Permission, |
| 64 | + rbacMgr *rbac.Manager, |
| 65 | +) (*Account, error) { |
| 66 | + |
| 67 | + // Null and validity checks |
| 68 | + if peerID == "" { |
| 69 | + return nil, errors.New("peerID cannot be empty") |
| 70 | + } |
| 71 | + if peerSk == nil { |
| 72 | + return nil, errors.New("peerSk (MasterPrivateKey) cannot be nil") |
| 73 | + } |
| 74 | + if peerPk == nil { |
| 75 | + return nil, errors.New("peerPk (MasterPublicKey) cannot be nil") |
| 76 | + } |
| 77 | + |
| 78 | + // Marshal the public key bytes |
| 79 | + pubKeyBytes, err := libp2pCrypto.MarshalPublicKey(peerPk) |
| 80 | + if err != nil { |
| 81 | + return nil, errors.Wrap(err, "failed to marshal MasterPublicKey") |
| 82 | + } |
| 83 | + |
| 84 | + // Compute account ID using SignerType |
| 85 | + accountID := ComputeAccountID(signerType, pubKeyBytes) |
| 86 | + |
| 87 | + // Compute address from the master public key bytes |
| 88 | + address, err := computeAddressFromPublicKey(pubKeyBytes) |
| 89 | + if err != nil { |
| 90 | + return nil, errors.Wrap(err, "failed to derive address from MasterPublicKey") |
| 91 | + } |
| 92 | + |
| 93 | + account := &Account{ |
| 94 | + logger: logger, |
| 95 | + id: accountID, |
| 96 | + signerType: signerType, |
| 97 | + peerID: peerID, |
| 98 | + address: address, |
| 99 | + name: name, |
| 100 | + comment: comment, |
| 101 | + masterPrivateKey: peerSk, |
| 102 | + masterPublicKey: peerPk, |
| 103 | + roles: roles, |
| 104 | + extraPermissions: extraPermissions, |
| 105 | + rbacMgr: rbacMgr, |
| 106 | + } |
| 107 | + |
| 108 | + return account, nil |
| 109 | +} |
| 110 | + |
| 111 | +// ID returns the unique identifier of the account. |
| 112 | +func (a *Account) ID() string { |
| 113 | + return a.id |
| 114 | +} |
| 115 | + |
| 116 | +// Address returns the derived address from the MasterPublicKey. |
| 117 | +func (a *Account) Address() types.Address { |
| 118 | + a.mu.RLock() |
| 119 | + defer a.mu.RUnlock() |
| 120 | + return a.address |
| 121 | +} |
| 122 | + |
| 123 | +// PeerID returns the associated libp2p PeerID. |
| 124 | +func (a *Account) PeerID() peer.ID { |
| 125 | + a.mu.RLock() |
| 126 | + defer a.mu.RUnlock() |
| 127 | + return a.peerID |
| 128 | +} |
| 129 | + |
| 130 | +// Name returns the name of the account. |
| 131 | +func (a *Account) Name() string { |
| 132 | + a.mu.RLock() |
| 133 | + defer a.mu.RUnlock() |
| 134 | + return a.name |
| 135 | +} |
| 136 | + |
| 137 | +// Comment returns the optional comment or description of the account. |
| 138 | +func (a *Account) Comment() string { |
| 139 | + a.mu.RLock() |
| 140 | + defer a.mu.RUnlock() |
| 141 | + return a.comment |
| 142 | +} |
| 143 | + |
| 144 | +// MasterPrivateKey returns the master private key associated with the account. |
| 145 | +func (a *Account) MasterPrivateKey() libp2pCrypto.PrivKey { |
| 146 | + a.mu.RLock() |
| 147 | + defer a.mu.RUnlock() |
| 148 | + return a.masterPrivateKey |
| 149 | +} |
| 150 | + |
| 151 | +func (a *Account) MasterKeyToECDSA() (*ecdsa.PrivateKey, error) { |
| 152 | + a.mu.RLock() |
| 153 | + defer a.mu.RUnlock() |
| 154 | + |
| 155 | + privKeyBytes, err := a.MasterPrivateKey().Raw() |
| 156 | + if err != nil { |
| 157 | + return nil, err |
| 158 | + } |
| 159 | + |
| 160 | + return crypto.ToECDSA(privKeyBytes) |
| 161 | +} |
| 162 | + |
| 163 | +// MasterPublicKey returns the master public key associated with the account. |
| 164 | +func (a *Account) MasterPublicKey() libp2pCrypto.PubKey { |
| 165 | + a.mu.RLock() |
| 166 | + defer a.mu.RUnlock() |
| 167 | + return a.masterPublicKey |
| 168 | +} |
| 169 | + |
| 170 | +// ExtraPermissions returns the additional permissions associated with each role. |
| 171 | +func (a *Account) ExtraPermissions() map[types.Role][]types.Permission { |
| 172 | + a.mu.RLock() |
| 173 | + defer a.mu.RUnlock() |
| 174 | + return a.extraPermissions |
| 175 | +} |
| 176 | + |
| 177 | +func (a *Account) MarshalPublicKey() ([]byte, error) { |
| 178 | + return libp2pCrypto.MarshalPublicKey(a.masterPublicKey) |
| 179 | +} |
0 commit comments