refactor: rename and simplify `UpdatePrivateKeyAndControlNode`

This commit is contained in:
Patryk Osmaczko 2023-10-18 17:04:02 +02:00 committed by osmaczko
parent 6c4ce3dedf
commit 246b68a8c0
7 changed files with 14 additions and 45 deletions

View File

@ -1086,7 +1086,7 @@ func (o *Community) ValidateEditSharedAddresses(signer *ecdsa.PublicKey, request
// We treat control node as an owner with community key
func (o *Community) IsControlNode() bool {
return o.config.PrivateKey != nil
return o.config.PrivateKey != nil && o.config.PrivateKey.PublicKey.Equal(o.config.ControlNode)
}
func (o *Community) IsOwnerWithoutCommunityKey() bool {
@ -2073,6 +2073,7 @@ func (o *Community) CreateDeepCopy() *Community {
return &Community{
config: &Config{
PrivateKey: o.config.PrivateKey,
ControlNode: o.config.ControlNode,
CommunityDescription: proto.Clone(o.config.CommunityDescription).(*protobuf.CommunityDescription),
CommunityDescriptionProtocolMessage: o.config.CommunityDescriptionProtocolMessage,
ID: o.config.ID,

View File

@ -837,6 +837,7 @@ func (s *CommunitySuite) newConfig(identity *ecdsa.PrivateKey, description *prot
ID: &identity.PublicKey,
CommunityDescription: description,
PrivateKey: identity,
ControlNode: &identity.PublicKey,
}
}

View File

@ -25,7 +25,7 @@ var ErrInvalidCommunityDescriptionUnknownChatCategory = errors.New("invalid comm
var ErrInvalidCommunityTags = errors.New("invalid community tags")
var ErrNotAdmin = errors.New("no admin privileges for this community")
var ErrNotOwner = errors.New("no owner privileges for this community")
var ErrNotControlNode = errors.New("no owner private key found for this community")
var ErrNotControlNode = errors.New("not a control node")
var ErrInvalidGrant = errors.New("invalid grant")
var ErrNotAuthorized = errors.New("not authorized")
var ErrAlreadyMember = errors.New("already a member")

View File

@ -1208,11 +1208,11 @@ func (m *Manager) ImportCommunity(key *ecdsa.PrivateKey) (*Community, error) {
config := Config{
ID: &key.PublicKey,
PrivateKey: key,
ControlNode: &key.PublicKey,
Logger: m.logger,
Joined: true,
MemberIdentity: &m.identity.PublicKey,
CommunityDescription: description,
ControlNode: &m.identity.PublicKey,
}
community, err = New(config, m.timesource)
if err != nil {
@ -5022,7 +5022,7 @@ func (m *Manager) createCommunityTokenPermission(request *requests.CreateCommuni
}
func (m *Manager) UpdateControlNode(communityID types.HexBytes, pubKey *ecdsa.PublicKey) (*Community, error) {
func (m *Manager) PromoteSelfToControlNode(communityID types.HexBytes) (*Community, error) {
community, err := m.GetByID(communityID)
if err != nil {
return nil, err
@ -5031,47 +5031,12 @@ func (m *Manager) UpdateControlNode(communityID types.HexBytes, pubKey *ecdsa.Pu
return nil, ErrOrgNotFound
}
if community.ControlNode().Equal(pubKey) {
return community, nil
community.setPrivateKey(m.identity)
if !community.ControlNode().Equal(&m.identity.PublicKey) {
community.setControlNode(&m.identity.PublicKey)
}
community.setControlNode(pubKey)
err = m.persistence.SaveCommunity(community)
if err != nil {
return nil, err
}
return community, nil
}
func (m *Manager) UpdatePrivateKeyAndControlNode(communityID types.HexBytes, pk *ecdsa.PrivateKey) (*Community, error) {
_, err := m.UpdatePrivateKey(communityID, pk)
if err != nil {
return nil, err
}
community, err := m.UpdateControlNode(communityID, &pk.PublicKey)
if err != nil {
return nil, err
}
m.publish(&Subscription{Community: community})
return community, nil
}
func (m *Manager) UpdatePrivateKey(communityID types.HexBytes, pk *ecdsa.PrivateKey) (*Community, error) {
community, err := m.GetByID(communityID)
if err != nil {
return nil, err
}
if community == nil {
return nil, ErrOrgNotFound
}
community.setPrivateKey(pk)
err = m.persistence.SaveCommunity(community)
err = m.saveAndPublish(community)
if err != nil {
return nil, err
}

View File

@ -54,6 +54,7 @@ func (s *PersistenceSuite) TestSaveCommunity() {
community := Community{
config: &Config{
PrivateKey: id,
ControlNode: &id.PublicKey,
ID: &id.PublicKey,
Joined: true,
Spectated: true,
@ -258,6 +259,7 @@ func (s *PersistenceSuite) makeNewCommunity(identity *ecdsa.PrivateKey) *Communi
com, err := New(Config{
MemberIdentity: &identity.PublicKey,
PrivateKey: comPrivKey,
ControlNode: &comPrivKey.PublicKey,
ID: &comPrivKey.PublicKey,
}, &TimeSourceStub{})
s.NoError(err, "New shouldn't give any error")

View File

@ -168,7 +168,7 @@ func (s *MessengerCommunitiesSignersSuite) TestControlNodeUpdateSigner() {
s.collectiblesServiceMock.SetMockCollectibleContractData(chainID, tokenAddress,
&communitytokens.CollectibleContractData{TotalSupply: &bigint.BigInt{}})
community, err = s.alice.communitiesManager.UpdatePrivateKeyAndControlNode(community.ID(), s.alice.identity)
community, err = s.alice.communitiesManager.PromoteSelfToControlNode(community.ID())
s.Require().NoError(err)
s.Require().True(community.IsControlNode())

View File

@ -5120,7 +5120,7 @@ func (m *Messenger) SetCommunitySignerPubKey(ctx context.Context, communityID []
return "", err
}
_, err = m.communitiesManager.UpdatePrivateKeyAndControlNode(communityID, m.identity)
_, err = m.communitiesManager.PromoteSelfToControlNode(communityID)
if err != nil {
return "", err
}