github.com/Finschia/finschia-sdk@v0.48.1/server/rosetta/client_offline.go (about)

     1  package rosetta
     2  
     3  import (
     4  	"context"
     5  	"encoding/hex"
     6  
     7  	"github.com/coinbase/rosetta-sdk-go/types"
     8  
     9  	crgerrs "github.com/Finschia/finschia-sdk/server/rosetta/lib/errors"
    10  
    11  	sdk "github.com/Finschia/finschia-sdk/types"
    12  )
    13  
    14  // ---------- cosmos-rosetta-gateway.types.NetworkInformationProvider implementation ------------ //
    15  
    16  func (c *Client) OperationStatuses() []*types.OperationStatus {
    17  	return []*types.OperationStatus{
    18  		{
    19  			Status:     StatusTxSuccess,
    20  			Successful: true,
    21  		},
    22  		{
    23  			Status:     StatusTxReverted,
    24  			Successful: false,
    25  		},
    26  	}
    27  }
    28  
    29  func (c *Client) Version() string {
    30  	return c.version
    31  }
    32  
    33  func (c *Client) SupportedOperations() []string {
    34  	return c.supportedOperations
    35  }
    36  
    37  // ---------- cosmos-rosetta-gateway.types.OfflineClient implementation ------------ //
    38  
    39  func (c *Client) SignedTx(_ context.Context, txBytes []byte, signatures []*types.Signature) (signedTxBytes []byte, err error) {
    40  	return c.converter.ToSDK().SignedTx(txBytes, signatures)
    41  }
    42  
    43  func (c *Client) ConstructionPayload(_ context.Context, request *types.ConstructionPayloadsRequest) (resp *types.ConstructionPayloadsResponse, err error) {
    44  	// check if there is at least one operation
    45  	if len(request.Operations) < 1 {
    46  		return nil, crgerrs.WrapError(crgerrs.ErrInvalidOperation, "expected at least one operation")
    47  	}
    48  
    49  	tx, err := c.converter.ToSDK().UnsignedTx(request.Operations)
    50  	if err != nil {
    51  		return nil, crgerrs.WrapError(crgerrs.ErrInvalidOperation, err.Error())
    52  	}
    53  
    54  	metadata := new(ConstructionMetadata)
    55  	if err = metadata.FromMetadata(request.Metadata); err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	txBytes, payloads, err := c.converter.ToRosetta().SigningComponents(tx, metadata, request.PublicKeys)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	return &types.ConstructionPayloadsResponse{
    65  		UnsignedTransaction: hex.EncodeToString(txBytes),
    66  		Payloads:            payloads,
    67  	}, nil
    68  }
    69  
    70  func (c *Client) PreprocessOperationsToOptions(_ context.Context, req *types.ConstructionPreprocessRequest) (response *types.ConstructionPreprocessResponse, err error) {
    71  	if len(req.Operations) == 0 {
    72  		return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, "no operations")
    73  	}
    74  
    75  	// now we need to parse the operations to cosmos sdk messages
    76  	tx, err := c.converter.ToSDK().UnsignedTx(req.Operations)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	// get the signers
    82  	signers := tx.GetSigners()
    83  	signersStr := make([]string, len(signers))
    84  	accountIdentifiers := make([]*types.AccountIdentifier, len(signers))
    85  
    86  	for i, sig := range signers {
    87  		addr := sig.String()
    88  		signersStr[i] = addr
    89  		accountIdentifiers[i] = &types.AccountIdentifier{
    90  			Address: addr,
    91  		}
    92  	}
    93  	// get the metadata request information
    94  	meta := new(ConstructionPreprocessMetadata)
    95  	err = meta.FromMetadata(req.Metadata)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  
   100  	if meta.GasPrice == "" {
   101  		return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, "no gas prices")
   102  	}
   103  
   104  	if meta.GasLimit == 0 {
   105  		return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, "no gas limit")
   106  	}
   107  
   108  	// prepare the options to return
   109  	options := &PreprocessOperationsOptionsResponse{
   110  		ExpectedSigners: signersStr,
   111  		Memo:            meta.Memo,
   112  		GasLimit:        meta.GasLimit,
   113  		GasPrice:        meta.GasPrice,
   114  	}
   115  
   116  	metaOptions, err := options.ToMetadata()
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  	return &types.ConstructionPreprocessResponse{
   121  		Options:            metaOptions,
   122  		RequiredPublicKeys: accountIdentifiers,
   123  	}, nil
   124  }
   125  
   126  func (c *Client) AccountIdentifierFromPublicKey(pubKey *types.PublicKey) (*types.AccountIdentifier, error) {
   127  	pk, err := c.converter.ToSDK().PubKey(pubKey)
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  
   132  	return &types.AccountIdentifier{
   133  		Address: sdk.AccAddress(pk.Address()).String(),
   134  	}, nil
   135  }