Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/payment/pfd.go: Add canonical PFD constructor/signer/submittor methods #352

Merged
merged 2 commits into from
Apr 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions x/payment/payfordata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package payment

import (
"context"
"google.golang.org/grpc"

sdk "github.com/cosmos/cosmos-sdk/types"
sdk_tx "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/x/auth/signing"

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

var (
// shareSizes includes all the possible share sizes of the given data
// that the signer must sign over.
shareSizes = []uint64{16, 32, 64, 128}
)

// SubmitPayForData constructs, signs and synchronously submits a PayForData
// transaction, returning a sdk.TxResponse upon submission.
func SubmitPayForData(
ctx context.Context,
signer *types.KeyringSigner,
conn *grpc.ClientConn,
nID namespace.ID,
data []byte,
gasLim uint64,
opts ...types.TxBuilderOption,
) (*sdk.TxResponse, error) {
pfd, err := BuildPayForData(ctx, signer, conn, nID, data, gasLim)
if err != nil {
return nil, err
}

signed, err := SignPayForData(signer, pfd, append(opts, types.SetGasLimit(gasLim))...)
if err != nil {
return nil, err
}

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

txResp, err := types.BroadcastTx(ctx, conn, sdk_tx.BroadcastMode_BROADCAST_MODE_BLOCK, rawTx)
if err != nil {
return nil, err
}
return txResp.TxResponse, nil
}

// BuildPayForData constructs a PayForData transaction.
func BuildPayForData(
ctx context.Context,
signer *types.KeyringSigner,
conn *grpc.ClientConn,
nID namespace.ID,
message []byte,
gasLim uint64,
) (*types.MsgWirePayForData, error) {
// create the raw WirePayForMessage transaction
wpfd, err := types.NewWirePayForData(nID, message, shareSizes...)
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 := types.SetGasLimit(gasLim)
err = wpfd.SignShareCommitments(signer, gasLimOption)
if err != nil {
return nil, err
}

return wpfd, nil
}

// SignPayForData signs a PayForData transaction.
func SignPayForData(
signer *types.KeyringSigner,
pfd *types.MsgWirePayForData,
opts ...types.TxBuilderOption,
) (signing.Tx, error) {
// Build and sign the final `WirePayForMessage` tx that now contains the signatures
// for potential `MsgPayForMessage`s
builder := signer.NewTxBuilder()
for _, opt := range opts {
opt(builder)
}
return signer.BuildSignedTx(
builder,
pfd,
)
}