github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/contract/identity.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package contract
    19  
    20  import (
    21  	"math/big"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/mysteriumnetwork/go-rest/apierror"
    25  	"github.com/mysteriumnetwork/node/identity"
    26  	pingpong_event "github.com/mysteriumnetwork/node/session/pingpong/event"
    27  )
    28  
    29  // IdentityRefDTO represents unique identity reference.
    30  // swagger:model IdentityRefDTO
    31  type IdentityRefDTO struct {
    32  	// identity in Ethereum address format
    33  	// required: true
    34  	// example: 0x0000000000000000000000000000000000000001
    35  	Address string `json:"id"`
    36  }
    37  
    38  // BalanceDTO holds balance information.
    39  // swagger:model BalanceDTO
    40  type BalanceDTO struct {
    41  	Balance       *big.Int `json:"balance"`
    42  	BalanceTokens Tokens   `json:"balance_tokens"`
    43  }
    44  
    45  // IdentityDTO holds identity information.
    46  // swagger:model IdentityDTO
    47  type IdentityDTO struct {
    48  	// identity in Ethereum address format
    49  	// required: true
    50  	// example: 0x0000000000000000000000000000000000000001
    51  	Address            string `json:"id"`
    52  	RegistrationStatus string `json:"registration_status"`
    53  	ChannelAddress     string `json:"channel_address"`
    54  
    55  	// deprecated
    56  	Balance       *big.Int `json:"balance"`
    57  	Earnings      *big.Int `json:"earnings"`
    58  	EarningsTotal *big.Int `json:"earnings_total"`
    59  	// ===========
    60  
    61  	BalanceTokens       Tokens `json:"balance_tokens"`
    62  	EarningsTokens      Tokens `json:"earnings_tokens"`
    63  	EarningsTotalTokens Tokens `json:"earnings_total_tokens"`
    64  
    65  	Stake             *big.Int               `json:"stake"`
    66  	HermesID          string                 `json:"hermes_id"`
    67  	EarningsPerHermes map[string]EarningsDTO `json:"earnings_per_hermes"`
    68  }
    69  
    70  // EarningsDTO holds earnings data.
    71  // swagger:model EarningsDTO
    72  type EarningsDTO struct {
    73  	Earnings      Tokens `json:"earnings"`
    74  	EarningsTotal Tokens `json:"earnings_total"`
    75  }
    76  
    77  // NewEarningsPerHermesDTO transforms the pingong value in a public one.
    78  func NewEarningsPerHermesDTO(earnings map[common.Address]pingpong_event.Earnings) map[string]EarningsDTO {
    79  	settlementsPerHermes := make(map[string]EarningsDTO)
    80  	for h, earn := range earnings {
    81  		settlementsPerHermes[h.Hex()] = EarningsDTO{
    82  			Earnings:      NewTokens(earn.UnsettledBalance),
    83  			EarningsTotal: NewTokens(earn.LifetimeBalance),
    84  		}
    85  	}
    86  
    87  	return settlementsPerHermes
    88  }
    89  
    90  // NewIdentityDTO maps to API identity.
    91  func NewIdentityDTO(id identity.Identity) IdentityRefDTO {
    92  	return IdentityRefDTO{Address: id.Address}
    93  }
    94  
    95  // ListIdentitiesResponse holds list of identities.
    96  // swagger:model ListIdentitiesResponse
    97  type ListIdentitiesResponse struct {
    98  	Identities []IdentityRefDTO `json:"identities"`
    99  }
   100  
   101  // NewIdentityListResponse maps to API identity list.
   102  func NewIdentityListResponse(ids []identity.Identity) ListIdentitiesResponse {
   103  	result := ListIdentitiesResponse{
   104  		Identities: make([]IdentityRefDTO, len(ids)),
   105  	}
   106  	for i, id := range ids {
   107  		result.Identities[i] = NewIdentityDTO(id)
   108  	}
   109  	return result
   110  }
   111  
   112  // IdentityCreateRequest request used for new identity creation.
   113  // swagger:model IdentityCreateRequestDTO
   114  type IdentityCreateRequest struct {
   115  	Passphrase *string `json:"passphrase"`
   116  }
   117  
   118  // Validate validates fields in request
   119  func (r IdentityCreateRequest) Validate() *apierror.APIError {
   120  	v := apierror.NewValidator()
   121  	if r.Passphrase == nil {
   122  		v.Required("passphrase")
   123  	}
   124  	return v.Err()
   125  }
   126  
   127  // IdentityUnlockRequest request used for identity unlocking.
   128  // swagger:model IdentityUnlockRequestDTO
   129  type IdentityUnlockRequest struct {
   130  	Passphrase *string `json:"passphrase"`
   131  }
   132  
   133  // Validate validates fields in request
   134  func (r IdentityUnlockRequest) Validate() *apierror.APIError {
   135  	v := apierror.NewValidator()
   136  	if r.Passphrase == nil {
   137  		v.Required("passphrase")
   138  	}
   139  	return v.Err()
   140  }
   141  
   142  // IdentityCurrentRequest request used for current identity remembering.
   143  // swagger:model IdentityCurrentRequestDTO
   144  type IdentityCurrentRequest struct {
   145  	Address    *string `json:"id"`
   146  	Passphrase *string `json:"passphrase"`
   147  }
   148  
   149  // Validate validates fields in request
   150  func (r IdentityCurrentRequest) Validate() *apierror.APIError {
   151  	v := apierror.NewValidator()
   152  	if r.Passphrase == nil {
   153  		v.Required("passphrase")
   154  	}
   155  	return v.Err()
   156  }
   157  
   158  // IdentityRegisterRequest represents the identity registration user input parameters
   159  // swagger:model IdentityRegisterRequestDTO
   160  type IdentityRegisterRequest struct {
   161  	// Token: referral token, if the user has one
   162  	ReferralToken *string `json:"referral_token,omitempty"`
   163  	// Beneficiary: beneficiary to set during registration. Optional.
   164  	Beneficiary string `json:"beneficiary,omitempty"`
   165  	// Fee: an agreed amount to pay for registration
   166  	Fee *big.Int `json:"fee"`
   167  }
   168  
   169  // IdentityRegistrationResponse represents registration status and needed data for registering of given identity
   170  // swagger:model IdentityRegistrationResponseDTO
   171  type IdentityRegistrationResponse struct {
   172  	Status string `json:"status"`
   173  	// Returns true if identity is registered in payments smart contract
   174  	Registered bool `json:"registered"`
   175  }
   176  
   177  // IdentityBeneficiaryResponse represents the provider beneficiary address.
   178  // swagger:model IdentityBeneficiaryResponseDTO
   179  type IdentityBeneficiaryResponse struct {
   180  	Beneficiary      string `json:"beneficiary"`
   181  	IsChannelAddress bool   `json:"is_channel_address"`
   182  }
   183  
   184  // IdentityImportRequest is received in identity import endpoint.
   185  // swagger:model IdentityImportRequest
   186  type IdentityImportRequest struct {
   187  	Data              []byte `json:"data"`
   188  	CurrentPassphrase string `json:"current_passphrase,omitempty"`
   189  
   190  	// Optional. Default values are OK.
   191  	SetDefault    bool   `json:"set_default"`
   192  	NewPassphrase string `json:"new_passphrase"`
   193  }
   194  
   195  // Validate validates the import request.
   196  func (i *IdentityImportRequest) Validate() *apierror.APIError {
   197  	v := apierror.NewValidator()
   198  	if len(i.CurrentPassphrase) == 0 {
   199  		v.Required("current_passphrase")
   200  	}
   201  	if len(i.Data) == 0 {
   202  		v.Required("data")
   203  	}
   204  	return v.Err()
   205  }
   206  
   207  // IdentityExportRequest is received in identity export endpoint.
   208  // swagger:model IdentityExportRequestDTO
   209  type IdentityExportRequest struct {
   210  	Identity      string `json:"identity,omitempty"`
   211  	NewPassphrase string `json:"newpassphrase,omitempty"`
   212  }
   213  
   214  // Validate validates the Export request.
   215  func (i *IdentityExportRequest) Validate() *apierror.APIError {
   216  	v := apierror.NewValidator()
   217  	if len(i.NewPassphrase) == 0 {
   218  		v.Required("newpassphrase")
   219  	}
   220  	return v.Err()
   221  }
   222  
   223  // borrowed from github.com/ethereum/go-ethereum@v1.10.17/accounts/keystore/key.go
   224  
   225  // EncryptedKeyJSON represents response to IdentityExportRequest.
   226  // swagger:model IdentityExportResponseDTO
   227  type EncryptedKeyJSON struct {
   228  	Address string     `json:"address"`
   229  	Crypto  cryptoJSON `json:"crypto"`
   230  	Id      string     `json:"id"`
   231  	Version int        `json:"version"`
   232  }
   233  
   234  type cryptoJSON struct {
   235  	Cipher       string                 `json:"cipher"`
   236  	CipherText   string                 `json:"ciphertext"`
   237  	CipherParams cipherparamsJSON       `json:"cipherparams"`
   238  	KDF          string                 `json:"kdf"`
   239  	KDFParams    map[string]interface{} `json:"kdfparams"`
   240  	MAC          string                 `json:"mac"`
   241  }
   242  
   243  type cipherparamsJSON struct {
   244  	IV string `json:"iv"`
   245  }
   246  
   247  // BeneficiaryAddressRequest address of the beneficiary
   248  // swagger:model BeneficiaryAddressRequest
   249  type BeneficiaryAddressRequest struct {
   250  	Address string `json:"address"`
   251  }