feat: add AC messages for setting signer operations

Issue #11964
This commit is contained in:
Michal Iskierko 2023-10-13 10:08:40 +02:00 committed by Michał Iskierko
parent 02e4cc6e1f
commit d909faf504
4 changed files with 89 additions and 8 deletions

View File

@ -29,6 +29,11 @@ const (
ActivityCenterNotificationTypeContactVerification
ActivityCenterNotificationTypeContactRemoved
ActivityCenterNotificationTypeNewKeypairAddedToPairedDevice
ActivityCenterNotificationTypeOwnerTokenReceived
ActivityCenterNotificationTypeOwnershipReceived
ActivityCenterNotificationTypeOwnershipLost
ActivityCenterNotificationTypeSetSignerFailed
ActivityCenterNotificationTypeSetSignerDeclined
)
type ActivityCenterMembershipStatus int

View File

@ -6089,3 +6089,26 @@ func (m *Messenger) PromoteSelfToControlNode(communityID types.HexBytes) (*Messe
return &response, nil
}
func (m *Messenger) CreateResponseWithACNotification(communityID string, acType ActivityCenterType, isRead bool) (*MessengerResponse, error) {
// Activity center notification
notification := &ActivityCenterNotification{
ID: types.FromHex(uuid.New().String()),
Type: acType,
Timestamp: m.getTimesource().GetCurrentTime(),
CommunityID: communityID,
Read: isRead,
Deleted: false,
UpdatedAt: m.GetCurrentTimeInMillis(),
}
response := &MessengerResponse{}
err := m.addActivityCenterNotification(response, notification, nil)
if err != nil {
m.logger.Error("failed to save notification", zap.Error(err))
return response, err
}
return response, nil
}

View File

@ -830,19 +830,14 @@ func (api *API) validateBurnAmount(ctx context.Context, burnAmount *bigint.BigIn
return nil
}
func (api *API) estimateMethod(ctx context.Context, chainID uint64, contractAddress string, fromAddress string, methodName string, args ...interface{}) (uint64, error) {
func (api *API) estimateMethodForTokenInstance(ctx context.Context, contractInstance TokenInstance, chainID uint64, contractAddress string, fromAddress string, methodName string, args ...interface{}) (uint64, error) {
ethClient, err := api.s.manager.rpcClient.EthClient(chainID)
if err != nil {
log.Error(err.Error())
return 0, err
}
contractInst, err := NewTokenInstance(api, chainID, contractAddress)
if err != nil {
return 0, err
}
data, err := contractInst.PackMethod(ctx, methodName, args...)
data, err := contractInstance.PackMethod(ctx, methodName, args...)
if err != nil {
return 0, err
@ -864,6 +859,14 @@ func (api *API) estimateMethod(ctx context.Context, chainID uint64, contractAddr
return estimate + uint64(float32(estimate)*0.1), nil
}
func (api *API) estimateMethod(ctx context.Context, chainID uint64, contractAddress string, fromAddress string, methodName string, args ...interface{}) (uint64, error) {
contractInst, err := NewTokenInstance(api, chainID, contractAddress)
if err != nil {
return 0, err
}
return api.estimateMethodForTokenInstance(ctx, contractInst, chainID, contractAddress, fromAddress, methodName, args...)
}
// Gets signer public key from smart contract with a given chainId and address
func (api *API) GetSignerPubKey(ctx context.Context, chainID uint64, contractAddress string) (string, error) {
return api.s.GetSignerPubKey(ctx, chainID, contractAddress)
@ -887,5 +890,25 @@ func (api *API) EstimateSetSignerPubKey(ctx context.Context, chainID uint64, con
if len(newSignerPubKey) <= 0 {
return 0, fmt.Errorf("signerPubKey is empty")
}
return api.estimateMethod(ctx, chainID, contractAddress, fromAddress, "setSignerPublicKey", newSignerPubKey)
contractInst, err := api.NewOwnerTokenInstance(chainID, contractAddress)
if err != nil {
return 0, err
}
ownerTokenInstance := &OwnerTokenInstance{instance: contractInst}
return api.estimateMethodForTokenInstance(ctx, ownerTokenInstance, chainID, contractAddress, fromAddress, "setSignerPublicKey", common.FromHex(newSignerPubKey))
}
func (api *API) OwnerTokenOwnerAddress(ctx context.Context, chainID uint64, contractAddress string) (string, error) {
callOpts := &bind.CallOpts{Context: ctx, Pending: false}
contractInst, err := api.NewOwnerTokenInstance(chainID, contractAddress)
if err != nil {
return "", err
}
ownerAddress, err := contractInst.OwnerOf(callOpts, big.NewInt(0))
if err != nil {
return "", err
}
return ownerAddress.Hex(), nil
}

View File

@ -1631,6 +1631,36 @@ func (api *PublicAPI) GetProfileShowcasePreferences() (*protocol.ProfileShowcase
return api.service.messenger.GetProfileShowcasePreferences()
}
// Returns response with AC notification when owner token is received
func (api *PublicAPI) RegisterOwnerTokenReceivedNotification(communityID string) (*protocol.MessengerResponse, error) {
return api.service.messenger.CreateResponseWithACNotification(communityID, protocol.ActivityCenterNotificationTypeOwnerTokenReceived, false)
}
// Returns response with AC notification when setting signer is successful
func (api *PublicAPI) RegisterReceivedOwnershipNotification(communityID string) (*protocol.MessengerResponse, error) {
return api.service.messenger.CreateResponseWithACNotification(communityID, protocol.ActivityCenterNotificationTypeOwnershipReceived, false)
}
// Returns response with AC notification when setting signer is failed
func (api *PublicAPI) RegisterSetSignerFailedNotification(communityID string) (*protocol.MessengerResponse, error) {
return api.service.messenger.CreateResponseWithACNotification(communityID, protocol.ActivityCenterNotificationTypeSetSignerFailed, false)
}
// Returns response with AC notification when setting signer is declined
func (api *PublicAPI) RegisterSetSignerDeclinedNotification(communityID string) (*protocol.MessengerResponse, error) {
return api.service.messenger.CreateResponseWithACNotification(communityID, protocol.ActivityCenterNotificationTypeSetSignerDeclined, true)
}
// Returns response with AC notification when ownership is lost
func (api *PublicAPI) RegisterLostOwnershipNotification(communityID string) (*protocol.MessengerResponse, error) {
return api.service.messenger.CreateResponseWithACNotification(communityID, protocol.ActivityCenterNotificationTypeOwnershipLost, false)
}
func (api *PublicAPI) PromoteSelfToControlMode(communityID string) error {
_, err := api.service.messenger.PromoteSelfToControlNode([]byte(communityID))
return err
}
// -----
// HELPER
// -----