Skip to content

Commit

Permalink
add btc verifications
Browse files Browse the repository at this point in the history
  • Loading branch information
trevormil committed Jan 31, 2024
1 parent cff1614 commit 2b34115
Show file tree
Hide file tree
Showing 4 changed files with 472 additions and 9 deletions.
42 changes: 35 additions & 7 deletions app/ante/eip712.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package ante

import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"fmt"
"os/exec"
"strings"

sdkerrors "cosmossdk.io/errors"
"github.com/btcsuite/btcutil/base58"
Expand Down Expand Up @@ -32,7 +35,7 @@ import (
solana "github.com/bitbadges/bitbadgeschain/chain-handlers/solana/utils"

"github.com/bitbadges/bitbadgeschain/x/badges/types"

wasmxkeeper "github.com/bitbadges/bitbadgeschain/x/wasmx/keeper"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
)

Expand All @@ -54,16 +57,22 @@ func init() {
// CONTRACT: Tx must implement SigVerifiableTx interface
type Eip712SigVerificationDecorator struct {
ak ante.AccountKeeper
wasmxKeeper wasmxkeeper.Keeper
signModeHandler authsigning.SignModeHandler
chain string
verifyBtcSigPath string
}

// NewEip712SigVerificationDecorator creates a new Eip712SigVerificationDecorator
func NewEip712SigVerificationDecorator(ak ante.AccountKeeper, signModeHandler authsigning.SignModeHandler, chain string) Eip712SigVerificationDecorator {
func NewEip712SigVerificationDecorator(ak ante.AccountKeeper,
wk wasmxkeeper.Keeper,
signModeHandler authsigning.SignModeHandler, chain string, verifyBtcSigPath string) Eip712SigVerificationDecorator {
return Eip712SigVerificationDecorator{
ak: ak,
wasmxKeeper: wk,
signModeHandler: signModeHandler,
chain: chain,
verifyBtcSigPath: verifyBtcSigPath,
}
}

Expand Down Expand Up @@ -146,7 +155,7 @@ func (svd Eip712SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx,
return next(ctx, tx, simulate)
}

if err := VerifySignature(pubKey, signerData, sig.Data, svd.signModeHandler, authSignTx, svd.chain); err != nil {
if err := VerifySignature(pubKey, signerData, sig.Data, svd.signModeHandler, authSignTx, svd.chain, svd.wasmxKeeper, ctx, svd.verifyBtcSigPath); err != nil {
errMsg := fmt.Errorf("signature verification failed; please verify account number (%d) and chain-id (%s): %w", accNum, chainID, err)
return ctx, sdkerrors.Wrap(types.ErrUnauthorized, errMsg.Error())
}
Expand All @@ -163,6 +172,9 @@ func VerifySignature(
_ authsigning.SignModeHandler,
tx authsigning.Tx,
chain string,
wk wasmxkeeper.Keeper,
ctx sdk.Context,
verifyBtcSigPath string,
) error {
switch data := sigData.(type) {
case *signing.SingleSignatureData:
Expand Down Expand Up @@ -319,10 +331,26 @@ func VerifySignature(
} else if chain == "Bitcoin" {
//verify bitcoin bip322 signature

//TODO:

return sdkerrors.Wrap(types.ErrorInvalidSigner, "unable to verify signer signature of Bitcoin signature: not implemented")

message := string(sortedBytes)
signature := hex.EncodeToString(feePayerSig)
address := feePayer.String()

scriptPath := verifyBtcSigPath + "/dist/verify.js"
output, err := exec.Command("node", scriptPath, message, signature, address).Output()
if err != nil {
return sdkerrors.Wrapf(err, "unable to verify signer signature of Bitcoin signature")
}

// Process the output as needed, for example, splitting it into lines
lines := strings.Split(string(output), "\n")
if len(lines) < 1 {
return sdkerrors.Wrap(types.ErrorInvalidSigner, "unable to verify signer signature of Bitcoin signature")
}

verified := string(lines[0]) == "true"
if !verified {
return sdkerrors.Wrap(types.ErrorInvalidSigner, "unable to verify signer signature of Bitcoin signature")
}
}

return nil
Expand Down
7 changes: 6 additions & 1 deletion app/ante/handler_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

ibcante "github.com/cosmos/ibc-go/v7/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"

wasmxkeeper "github.com/bitbadges/bitbadgeschain/x/wasmx/keeper"
)

// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC
Expand All @@ -23,6 +25,9 @@ type HandlerOptions struct {
SignModeHandler authsigning.SignModeHandler
SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params authtypes.Params) error
TxFeeChecker ante.TxFeeChecker
WasmXKeeper wasmxkeeper.Keeper

VerifyBtcSigPath string
}

func (options HandlerOptions) Validate() error {
Expand Down Expand Up @@ -81,7 +86,7 @@ func newCosmosAnteHandlerEip712(options HandlerOptions, chain string) sdk.AnteHa

// Note: signature verification uses EIP instead of the cosmos signature validator
//This also accounts for Solana signatures
NewEip712SigVerificationDecorator(options.AccountKeeper, options.SignModeHandler, chain),
NewEip712SigVerificationDecorator(options.AccountKeeper, options.WasmXKeeper, options.SignModeHandler, chain, options.VerifyBtcSigPath),

ante.NewIncrementSequenceDecorator(options.AccountKeeper),
ibcante.NewRedundantRelayDecorator(options.IBCKeeper),
Expand Down
4 changes: 3 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func init() {
panic(err)
}

DefaultNodeHome = filepath.Join(userHomeDir, "."+Name)
DefaultNodeHome = filepath.Join(userHomeDir, "." + Name)
}

// App extends an ABCI application, but with most of its parameters exported.
Expand Down Expand Up @@ -858,6 +858,8 @@ func New(
IBCKeeper: app.IBCKeeper,
SignModeHandler: encodingConfig.TxConfig.SignModeHandler(),
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
WasmXKeeper: app.WasmxKeeper,
VerifyBtcSigPath: homePath + "/bip322-js",
}

if err := options.Validate(); err != nil {
Expand Down
Loading

0 comments on commit 2b34115

Please sign in to comment.