-
Notifications
You must be signed in to change notification settings - Fork 968
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} |