2018-01-12 11:49:53 -08:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2018-02-15 06:35:46 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp/testtx"
|
2018-01-12 14:30:02 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-01-12 11:49:53 -08:00
|
|
|
)
|
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
func NewAnteHandler(accountMapper sdk.AccountMapper) sdk.AnteHandler {
|
2018-01-12 11:49:53 -08:00
|
|
|
return func(
|
2018-01-12 14:30:02 -08:00
|
|
|
ctx sdk.Context, tx sdk.Tx,
|
2018-01-26 05:11:01 -08:00
|
|
|
) (_ sdk.Context, _ sdk.Result, abort bool) {
|
2018-01-12 11:49:53 -08:00
|
|
|
|
|
|
|
// Deduct the fee from the fee payer.
|
|
|
|
// This is done first because it only
|
|
|
|
// requires fetching 1 account.
|
|
|
|
payerAddr := tx.GetFeePayer()
|
2018-01-26 04:19:33 -08:00
|
|
|
if payerAddr != nil {
|
|
|
|
payerAcc := accountMapper.GetAccount(ctx, payerAddr)
|
|
|
|
if payerAcc == nil {
|
|
|
|
return ctx,
|
2018-01-26 06:22:56 -08:00
|
|
|
sdk.ErrUnrecognizedAddress(payerAddr).Result(),
|
2018-01-26 04:19:33 -08:00
|
|
|
true
|
|
|
|
}
|
|
|
|
// TODO: Charge fee from payerAcc.
|
|
|
|
// TODO: accountMapper.SetAccount(ctx, payerAddr)
|
|
|
|
} else {
|
|
|
|
// TODO: Ensure that some other spam prevention is used.
|
2018-02-14 17:09:00 -08:00
|
|
|
// NOTE: testapp.TestApp.RunDeliverMsg/RunCheckMsg will
|
|
|
|
// create a Tx with no payer.
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
|
|
|
|
2018-01-26 06:22:56 -08:00
|
|
|
var sigs = tx.GetSignatures()
|
2018-01-12 11:49:53 -08:00
|
|
|
|
2018-01-26 06:22:56 -08:00
|
|
|
// Assert that there are signatures.
|
2018-02-15 06:35:46 -08:00
|
|
|
if !testtx.IsTestAppTx(tx) {
|
2018-01-26 06:22:56 -08:00
|
|
|
if len(sigs) == 0 {
|
2018-01-26 04:19:33 -08:00
|
|
|
return ctx,
|
|
|
|
sdk.ErrUnauthorized("no signers").Result(),
|
|
|
|
true
|
|
|
|
}
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
2018-01-26 04:19:33 -08:00
|
|
|
|
2018-01-26 06:22:56 -08:00
|
|
|
// Ensure that sigs are correct.
|
|
|
|
var msg = tx.GetMsg()
|
|
|
|
var signerAddrs = msg.GetSigners()
|
|
|
|
var signerAccs = make([]sdk.Account, len(signerAddrs))
|
|
|
|
|
2018-01-26 04:19:33 -08:00
|
|
|
// Assert that number of signatures is correct.
|
2018-02-15 06:35:46 -08:00
|
|
|
if !testtx.IsTestAppTx(tx) {
|
2018-01-26 06:22:56 -08:00
|
|
|
if len(sigs) != len(signerAddrs) {
|
|
|
|
return ctx,
|
|
|
|
sdk.ErrUnauthorized("wrong number of signers").Result(),
|
|
|
|
true
|
|
|
|
}
|
2018-01-12 11:49:53 -08:00
|
|
|
|
2018-01-26 06:22:56 -08:00
|
|
|
// Check each nonce and sig.
|
|
|
|
// TODO Refactor out.
|
|
|
|
for i, sig := range sigs {
|
2018-01-12 11:49:53 -08:00
|
|
|
|
2018-01-26 06:22:56 -08:00
|
|
|
var signerAcc = accountMapper.GetAccount(ctx, signerAddrs[i])
|
|
|
|
signerAccs[i] = signerAcc
|
2018-01-12 11:49:53 -08:00
|
|
|
|
2018-01-26 06:22:56 -08:00
|
|
|
// If no pubkey, set pubkey.
|
|
|
|
if signerAcc.GetPubKey() == nil {
|
|
|
|
err := signerAcc.SetPubKey(sig.PubKey)
|
|
|
|
if err != nil {
|
|
|
|
return ctx,
|
|
|
|
sdk.ErrInternal("setting PubKey on signer").Result(),
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check and increment sequence number.
|
|
|
|
seq := signerAcc.GetSequence()
|
|
|
|
if seq != sig.Sequence {
|
2018-01-26 04:19:33 -08:00
|
|
|
return ctx,
|
2018-01-26 06:22:56 -08:00
|
|
|
sdk.ErrInvalidSequence("").Result(),
|
2018-01-26 04:19:33 -08:00
|
|
|
true
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
2018-01-26 06:22:56 -08:00
|
|
|
signerAcc.SetSequence(seq + 1)
|
2018-01-12 11:49:53 -08:00
|
|
|
|
2018-01-26 06:22:56 -08:00
|
|
|
// Check sig.
|
|
|
|
if !sig.PubKey.VerifyBytes(msg.GetSignBytes(), sig.Signature) {
|
|
|
|
return ctx,
|
|
|
|
sdk.ErrUnauthorized("").Result(),
|
|
|
|
true
|
|
|
|
}
|
2018-01-12 11:49:53 -08:00
|
|
|
|
2018-01-26 06:22:56 -08:00
|
|
|
// Save the account.
|
|
|
|
accountMapper.SetAccount(ctx, signerAcc)
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = WithSigners(ctx, signerAccs)
|
2018-01-12 14:30:02 -08:00
|
|
|
return ctx, sdk.Result{}, false // continue...
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
|
|
|
}
|