github.com/koko1123/flow-go-1@v0.29.6/engine/access/rest/models/account.go (about)

     1  package models
     2  
     3  import (
     4  	"github.com/koko1123/flow-go-1/engine/access/rest/util"
     5  	"github.com/koko1123/flow-go-1/model/flow"
     6  )
     7  
     8  const expandableKeys = "keys"
     9  const expandableContracts = "contracts"
    10  
    11  func (a *Account) Build(flowAccount *flow.Account, link LinkGenerator, expand map[string]bool) error {
    12  	a.Address = flowAccount.Address.String()
    13  	a.Balance = util.FromUint64(flowAccount.Balance)
    14  	a.Expandable = &AccountExpandable{}
    15  
    16  	if expand[expandableKeys] {
    17  		var keys AccountPublicKeys
    18  		keys.Build(flowAccount.Keys)
    19  		a.Keys = keys
    20  	} else {
    21  		a.Expandable.Keys = expandableKeys
    22  	}
    23  
    24  	if expand[expandableContracts] {
    25  		contracts := make(map[string]string, len(flowAccount.Contracts))
    26  		for name, code := range flowAccount.Contracts {
    27  			contracts[name] = util.ToBase64(code)
    28  		}
    29  		a.Contracts = contracts
    30  	} else {
    31  		a.Expandable.Contracts = expandableContracts
    32  	}
    33  
    34  	var self Links
    35  	err := self.Build(link.AccountLink(a.Address))
    36  	if err != nil {
    37  		return err
    38  	}
    39  	a.Links = &self
    40  
    41  	return nil
    42  }
    43  
    44  func (a *AccountPublicKey) Build(k flow.AccountPublicKey) {
    45  	sigAlgo := SigningAlgorithm(k.SignAlgo.String())
    46  	hashAlgo := HashingAlgorithm(k.HashAlgo.String())
    47  
    48  	a.Index = util.FromUint64(uint64(k.Index))
    49  	a.PublicKey = k.PublicKey.String()
    50  	a.SigningAlgorithm = &sigAlgo
    51  	a.HashingAlgorithm = &hashAlgo
    52  	a.SequenceNumber = util.FromUint64(k.SeqNumber)
    53  	a.Weight = util.FromUint64(uint64(k.Weight))
    54  	a.Revoked = k.Revoked
    55  }
    56  
    57  type AccountPublicKeys []AccountPublicKey
    58  
    59  func (a *AccountPublicKeys) Build(accountKeys []flow.AccountPublicKey) {
    60  	keys := make([]AccountPublicKey, len(accountKeys))
    61  	for i, k := range accountKeys {
    62  		var key AccountPublicKey
    63  		key.Build(k)
    64  		keys[i] = key
    65  	}
    66  
    67  	*a = keys
    68  }