github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/common/rpc/convert/accounts.go (about)

     1  package convert
     2  
     3  import (
     4  	"github.com/onflow/crypto"
     5  	"github.com/onflow/crypto/hash"
     6  	"github.com/onflow/flow/protobuf/go/flow/entities"
     7  
     8  	"github.com/onflow/flow-go/model/flow"
     9  )
    10  
    11  // AccountToMessage converts a flow.Account to a protobuf message
    12  func AccountToMessage(a *flow.Account) (*entities.Account, error) {
    13  	keys := make([]*entities.AccountKey, len(a.Keys))
    14  	for i, k := range a.Keys {
    15  		messageKey, err := AccountKeyToMessage(k)
    16  		if err != nil {
    17  			return nil, err
    18  		}
    19  		keys[i] = messageKey
    20  	}
    21  
    22  	return &entities.Account{
    23  		Address:   a.Address.Bytes(),
    24  		Balance:   a.Balance,
    25  		Code:      nil,
    26  		Keys:      keys,
    27  		Contracts: a.Contracts,
    28  	}, nil
    29  }
    30  
    31  // MessageToAccount converts a protobuf message to a flow.Account
    32  func MessageToAccount(m *entities.Account) (*flow.Account, error) {
    33  	if m == nil {
    34  		return nil, ErrEmptyMessage
    35  	}
    36  
    37  	accountKeys := make([]flow.AccountPublicKey, len(m.GetKeys()))
    38  	for i, key := range m.GetKeys() {
    39  		accountKey, err := MessageToAccountKey(key)
    40  		if err != nil {
    41  			return nil, err
    42  		}
    43  
    44  		accountKeys[i] = *accountKey
    45  	}
    46  
    47  	return &flow.Account{
    48  		Address:   flow.BytesToAddress(m.GetAddress()),
    49  		Balance:   m.GetBalance(),
    50  		Keys:      accountKeys,
    51  		Contracts: m.Contracts,
    52  	}, nil
    53  }
    54  
    55  // AccountKeyToMessage converts a flow.AccountPublicKey to a protobuf message
    56  func AccountKeyToMessage(a flow.AccountPublicKey) (*entities.AccountKey, error) {
    57  	publicKey := a.PublicKey.Encode()
    58  	return &entities.AccountKey{
    59  		Index:          uint32(a.Index),
    60  		PublicKey:      publicKey,
    61  		SignAlgo:       uint32(a.SignAlgo),
    62  		HashAlgo:       uint32(a.HashAlgo),
    63  		Weight:         uint32(a.Weight),
    64  		SequenceNumber: uint32(a.SeqNumber),
    65  		Revoked:        a.Revoked,
    66  	}, nil
    67  }
    68  
    69  // MessageToAccountKey converts a protobuf message to a flow.AccountPublicKey
    70  func MessageToAccountKey(m *entities.AccountKey) (*flow.AccountPublicKey, error) {
    71  	if m == nil {
    72  		return nil, ErrEmptyMessage
    73  	}
    74  
    75  	sigAlgo := crypto.SigningAlgorithm(m.GetSignAlgo())
    76  	hashAlgo := hash.HashingAlgorithm(m.GetHashAlgo())
    77  
    78  	publicKey, err := crypto.DecodePublicKey(sigAlgo, m.GetPublicKey())
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	return &flow.AccountPublicKey{
    84  		Index:     int(m.GetIndex()),
    85  		PublicKey: publicKey,
    86  		SignAlgo:  sigAlgo,
    87  		HashAlgo:  hashAlgo,
    88  		Weight:    int(m.GetWeight()),
    89  		SeqNumber: uint64(m.GetSequenceNumber()),
    90  		Revoked:   m.GetRevoked(),
    91  	}, nil
    92  }