-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathante.go
86 lines (71 loc) · 2.76 KB
/
ante.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, NASD Inc. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.
package forwarding
import (
storetypes "cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/noble-assets/forwarding/v2/types"
)
// SigVerificationGasConsumer is a wrapper around the default provided by the
// Cosmos SDK that supports forwarding account public keys.
func SigVerificationGasConsumer(
meter storetypes.GasMeter, sig signing.SignatureV2, params authtypes.Params,
) error {
switch sig.PubKey.(type) {
case *types.ForwardingPubKey:
return nil
default:
return ante.DefaultSigVerificationGasConsumer(meter, sig, params)
}
}
//
var _ sdk.AnteDecorator = SigVerificationDecorator{}
type SigVerificationDecorator struct {
bank types.BankKeeper
underlying sdk.AnteDecorator
}
var _ sdk.AnteDecorator = SigVerificationDecorator{}
func NewSigVerificationDecorator(bk types.BankKeeper, underlying sdk.AnteDecorator) SigVerificationDecorator {
if underlying == nil {
panic("underlying ante decorator cannot be nil")
}
return SigVerificationDecorator{
bank: bk,
underlying: underlying,
}
}
func (d SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if msgs := tx.GetMsgs(); len(msgs) == 1 {
msg, ok := msgs[0].(*types.MsgRegisterAccount)
if !ok {
return d.underlying.AnteHandle(ctx, tx, simulate, next)
}
address := types.GenerateAddress(msg.Channel, msg.Recipient, msg.Fallback)
balance := d.bank.GetAllBalances(ctx, address)
if balance.IsZero() || msg.Signer != address.String() {
return d.underlying.AnteHandle(ctx, tx, simulate, next)
}
return next(ctx, tx, simulate)
}
return d.underlying.AnteHandle(ctx, tx, simulate, next)
}