github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/std/doc.go (about)

     1  package std
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gnolang/gno/tm2/pkg/amino"
     7  	"github.com/gnolang/gno/tm2/pkg/crypto"
     8  )
     9  
    10  // SignDoc is the standard object for transactions.
    11  // AccountNumber is a replay-prevention field for the whole account
    12  // (eg. nonce) to prevent the replay of txs after an account has been deleted
    13  // (due to zero balance). Sequence is a replay-prevention field for each transaction
    14  // given a nonce
    15  type SignDoc struct {
    16  	ChainID       string `json:"chain_id" yaml:"chain_id"`
    17  	AccountNumber uint64 `json:"account_number" yaml:"account_number"`
    18  	Sequence      uint64 `json:"sequence" yaml:"sequence"`
    19  	Fee           Fee    `json:"fee" yaml:"fee"`
    20  	Msgs          []Msg  `json:"msgs" yaml:"msgs"`
    21  	Memo          string `json:"memo" yaml:"memo"`
    22  }
    23  
    24  // GetSignaturePayload returns the sign payload for the SignDoc.
    25  // The signature payload is generated by marshalling the SignDoc
    26  // into Amino JSON, and then sorting the Amino JSON.
    27  // Ultimately, the formula for signing is sign(sortJSON(aminoJSON(SignDoc)))
    28  func GetSignaturePayload(s SignDoc) ([]byte, error) {
    29  	// Prepare the amino JSON
    30  	data, err := amino.MarshalJSON(s)
    31  	if err != nil {
    32  		return nil, fmt.Errorf("unable to marshal sign doc, %w", err)
    33  	}
    34  
    35  	// Sort the JSON
    36  	sortedData, err := sortJSON(data)
    37  	if err != nil {
    38  		return nil, fmt.Errorf("unable to sort payload JSON, %w", err)
    39  	}
    40  
    41  	return sortedData, nil
    42  }
    43  
    44  // Signature represents a wrapped signature of a transaction
    45  type Signature struct {
    46  	PubKey    crypto.PubKey `json:"pub_key" yaml:"pub_key"` // optional
    47  	Signature []byte        `json:"signature" yaml:"signature"`
    48  }