Skip to content

Commit

Permalink
BuildPayForData
Browse files Browse the repository at this point in the history
  • Loading branch information
renaynay committed Apr 13, 2022
1 parent f6b97b9 commit ffed2e7
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
12 changes: 11 additions & 1 deletion service/state/core_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package state
import (
"context"
"fmt"

sdk_tx "github.com/cosmos/cosmos-sdk/types/tx"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"google.golang.org/grpc"
Expand Down Expand Up @@ -103,3 +102,14 @@ func (ca *CoreAccessor) SubmitTxWithBroadcastMode(
}
return txResp.TxResponse, nil
}

func (ca *CoreAccessor) KeyringSigner() *apptypes.KeyringSigner {
return ca.signer
}

func (ca *CoreAccessor) Conn() (*grpc.ClientConn, error) {
if ca.coreConn == nil {
return nil, fmt.Errorf("no running gRPC connection")
}
return ca.coreConn, nil
}
11 changes: 11 additions & 0 deletions service/state/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package state

import (
"context"

"google.golang.org/grpc"

apptypes "github.com/celestiaorg/celestia-app/x/payment/types"
)

// Accessor represents the behaviors necessary for a user to
Expand All @@ -12,6 +16,13 @@ type Accessor interface {
Start(context.Context) error
// Stop stops the state Accessor.
Stop(context.Context) error

// KeyringSigner returns the KeyringSigner used by the Accessor.
KeyringSigner() *apptypes.KeyringSigner
// Conn returns a gRPC connection to node that serves state-related
// requests.
Conn() (*grpc.ClientConn, error)

// Balance retrieves the Celestia coin balance
// for the node's account/signer.
Balance(ctx context.Context) (*Balance, error)
Expand Down
78 changes: 78 additions & 0 deletions service/state/pfd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package state

import (
"context"
"github.com/cosmos/cosmos-sdk/x/auth/signing"

apptypes "github.com/celestiaorg/celestia-app/x/payment/types"
"github.com/celestiaorg/nmt/namespace"
)

var (
gasLim = uint64(200000)
)

type PayForData = apptypes.MsgWirePayForMessage

// SubmitPayForData builds, signs and submits a PayForData message.
func (s *Service) SubmitPayForData(ctx context.Context, nID namespace.ID, message []byte) (*TxResponse, error) {
pfd, err := s.BuildPayForData(ctx, nID, message)
if err != nil {
return nil, err
}
signed, err := s.SignPayForData(pfd, apptypes.SetGasLimit(gasLim))
if err != nil {
return nil, err
}

signer := s.accessor.KeyringSigner()
rawTx, err := signer.EncodeTx(signed)
if err != nil {
return nil, err
}

return s.accessor.SubmitTx(ctx, rawTx)
}

// BuildPayForData builds a PayForData message.
func (s *Service) BuildPayForData(ctx context.Context, nID namespace.ID, message []byte) (*PayForData, error) {
// create the raw WirePayForMessage transaction
wpfmMsg, err := apptypes.NewWirePayForMessage(nID, message, 16, 32, 64, 128)
if err != nil {
return nil, err
}

// get signer and conn info
signer := s.accessor.KeyringSigner()
conn, err := s.accessor.Conn()
if err != nil {
return nil, err
}

// query for account information necessary to sign a valid tx
err = signer.QueryAccountNumber(ctx, conn)
if err != nil {
return nil, err
}

// generate the signatures for each `MsgPayForMessage` using the `KeyringSigner`,
// then set the gas limit for the tx
gasLimOption := apptypes.SetGasLimit(gasLim)
err = wpfmMsg.SignShareCommitments(signer, gasLimOption)
if err != nil {
return nil, err
}

return wpfmMsg, nil
}

// SignPayForData signs the given PayForData message.
func (s *Service) SignPayForData(pfd *PayForData, gasLimOption apptypes.TxBuilderOption) (signing.Tx, error) {
signer := s.accessor.KeyringSigner()
// Build and sign the final `WirePayForMessage` tx that now contains the signatures
// for potential `MsgPayForMessage`s
return signer.BuildSignedTx(
gasLimOption(signer.NewTxBuilder()),
pfd,
)
}

0 comments on commit ffed2e7

Please sign in to comment.