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

feat: add ZKAuthMsgDecorator anteHandler for zkauth #1246

Merged
merged 21 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
## [Unreleased](https://github.com/Finschia/finschia-sdk/compare/v0.48.1...HEAD)

### Features
* (x/zkauth) [\#1246](https://github.com/Finschia/finschia-sdk/pull/1246) add ZKAuthMsgDecorator anteHandler for zkauth

### Improvements
* (x/zkauth) [\#1239](https://github.com/Finschia/finschia-sdk/pull/1239) add CalculateAllInputsHash in ZKAuthInputs
Expand Down
2 changes: 1 addition & 1 deletion docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -9201,7 +9201,7 @@ Contains the values needed to calculate "all inputs hash".
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `zk_auth_inputs` | [ZKAuthInputs](#finschia.zkauth.v1beta1.ZKAuthInputs) | | |
| `max_block_height` | [uint64](#uint64) | | |
| `max_block_height` | [int64](#int64) | | |



Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ require (
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d
github.com/hdevalence/ed25519consensus v0.1.0
github.com/iden3/go-iden3-crypto v0.0.15
github.com/iden3/go-rapidsnark/types v0.0.3
github.com/iden3/go-rapidsnark/verifier v0.0.5
github.com/improbable-eng/grpc-web v0.15.0
github.com/jhump/protoreflect v1.15.3
github.com/magiconair/properties v1.8.7
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/iden3/go-iden3-crypto v0.0.15 h1:4MJYlrot1l31Fzlo2sF56u7EVFeHHJkxGXXZCtESgK4=
github.com/iden3/go-iden3-crypto v0.0.15/go.mod h1:dLpM4vEPJ3nDHzhWFXDjzkn1qHoBeOT/3UEhXsEsP3E=
github.com/iden3/go-rapidsnark/types v0.0.3 h1:f0s1Qdut1qHe1O67+m+xUVRBPwSXnq5j0xSrBi0jqM4=
github.com/iden3/go-rapidsnark/types v0.0.3/go.mod h1:ApgcaUxKIgSRA6fAeFxK7p+lgXXfG4oA2HN5DhFlfF4=
github.com/iden3/go-rapidsnark/verifier v0.0.5 h1:J7y0ovrEjDQoWtZmlrp4tgGng1A9faMeYsQH4igAEqA=
github.com/iden3/go-rapidsnark/verifier v0.0.5/go.mod h1:KgL3Yr9NehlFDI4EIWVLE3UDUi8ulyjbp7HcXSBfiGI=
github.com/improbable-eng/grpc-web v0.15.0 h1:BN+7z6uNXZ1tQGcNAuaU1YjsLTApzkjt2tzCixLaUPQ=
github.com/improbable-eng/grpc-web v0.15.0/go.mod h1:1sy9HKV4Jt9aEs9JSnkWlRJPuPtwNr0l57L4f878wP8=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down
2 changes: 1 addition & 1 deletion proto/finschia/zkauth/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ message MsgExecutionResponse {

message ZKAuthSignature {
ZKAuthInputs zk_auth_inputs = 1;
uint64 max_block_height = 2;
int64 max_block_height = 2;
}
55 changes: 55 additions & 0 deletions simapp/ante/ante.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ante

import (
sdk "github.com/Finschia/finschia-sdk/types"
sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
"github.com/Finschia/finschia-sdk/x/auth/ante"
zkauthante "github.com/Finschia/finschia-sdk/x/zkauth/ante"
zkauthtypes "github.com/Finschia/finschia-sdk/x/zkauth/types"
)

type HandlerOptions struct {
ante.HandlerOptions

ZKAuthKeeper zkauthtypes.ZKAuthKeeper
}

func NewAnteHandler(opts HandlerOptions) (sdk.AnteHandler, error) {
if opts.AccountKeeper == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "account keeper is required for ante builder")
}

Check warning on line 20 in simapp/ante/ante.go

View check run for this annotation

Codecov / codecov/patch

simapp/ante/ante.go#L19-L20

Added lines #L19 - L20 were not covered by tests

if opts.BankKeeper == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "bank keeper is required for ante builder")
}

Check warning on line 24 in simapp/ante/ante.go

View check run for this annotation

Codecov / codecov/patch

simapp/ante/ante.go#L23-L24

Added lines #L23 - L24 were not covered by tests

if opts.SignModeHandler == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
}

Check warning on line 28 in simapp/ante/ante.go

View check run for this annotation

Codecov / codecov/patch

simapp/ante/ante.go#L27-L28

Added lines #L27 - L28 were not covered by tests

//sigGasConsumer := opts.SigGasConsumer
//if sigGasConsumer == nil {
// sigGasConsumer = ante.DefaultSigVerificationGasConsumer
//}

anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
ante.NewRejectExtensionOptionsDecorator(),
ante.NewMempoolFeeDecorator(),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(opts.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(opts.AccountKeeper),
ante.NewDeductFeeDecorator(opts.AccountKeeper, opts.BankKeeper, opts.FeegrantKeeper),
// todo: SetPubKeyDecorator should modify for zkauth
//ante.NewSetPubKeyDecorator(opts.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
ante.NewValidateSigCountDecorator(opts.AccountKeeper),
// todo: SigGasConsumeDecorator should modify for zkauth
//ante.NewSigGasConsumeDecorator(opts.AccountKeeper, sigGasConsumer),
//ante.NewSigVerificationDecorator(opts.AccountKeeper, opts.SignModeHandler),
zkauthante.NewZKAuthMsgDecorator(opts.ZKAuthKeeper),
ante.NewIncrementSequenceDecorator(opts.AccountKeeper),
}

return sdk.ChainAnteDecorators(anteDecorators...), nil
}
41 changes: 41 additions & 0 deletions simapp/ante/ante_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ante_test

import (
"encoding/base64"
"testing"

"github.com/stretchr/testify/require"

appante "github.com/Finschia/finschia-sdk/simapp/ante"
"github.com/Finschia/finschia-sdk/x/auth/ante"
"github.com/Finschia/finschia-sdk/x/zkauth/testutil"
)

func TestAnteHandler(t *testing.T) {
k := testutil.ZkAuthKeeper(t)
_, err := k.AddTestAccounts([]string{"link19qvkxwmln5kf0z59ecw744ue2gzsndlwcuz9uq2kav4evuerjlysxduwzj"})
require.NoError(t, err)

const sampleTxBase64 = "CtcICtQICiUvZmluc2NoaWEuemthdXRoLnYxYmV0YTEuTXNnRXhlY3V0aW9uEqoICo4BChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEm4KP2xpbmsxZzd1ZDYzZXFsbGo3emo0cTdma2NhNWg3czIyM2o3OHR5dnIwZTJjeHV3NHF5eWFhZjN1c2E2NGRxYxIrbGluazEwMDh3ZW5ncjI4ejVxdWF0MmR6cnBydDloOGV1YXY0aGVyZnl1bRKWBwqPBwqvBXsicGlfYSI6WyIzNjE1OTE2NzQ3NTQxMzkyMzMzOTA1MjYyNjkyNjQ3NTE1MjE2MDc2MTQzMTkwNzQwNDUyOTEyNjMzMzExMDAyMDc4NjYzNjEwNTU5IiwiMjAxMzg5MzA5NDk5MjEyMTA2Njk0NDYyMDA4ODAwNzI2MDg2MjUwOTM0MzQ5OTc3NDc2NDkyMTU2MDA5NDU0MTg4ODA1ODM2NzE5MDciLCIxIl0sInBpX2IiOltbIjE2MjY0ODM5OTg1NTE5MTY1MzM1MTQzMjk5MTczMzI0NjU1NDgwNjI1MjE2NDUyNDkzNzQzOTE0NDI3MTA0MDUzMDU0OTA2NzYwMTk1IiwiMjAyNTY3NzY4MTY5MjEzMjUzOTkwMjI4NjYyMTkyNzAzNTc5Mzc4Mzc4ODk2NjA2MDQ0OTU5ODk3Njg3MjczMjUzNzY1ODIyMzM0NTgiXSxbIjY2MzU2NzY4NzMxOTU4OTkyMDUxMDM3MjYwNDE4ODIyNTkxNjc3MDgwNDQwNzc4MzQ2MzM5NzA5ODIxMzc4NDIxNjkyNjg0OTU1OTYiLCI0ODc4ODc2OTQ4MTE1NTQwMDQ4NTE5MzE2MjI2NzE0OTQ3NDMxMDQ5ODc1Njg1MTcxNTg1Mzk3OTE4NDMzNjEwNzI5NTExMjcyNzM1Il0sWyIxIiwiMCJdXSwicGlfYyI6WyIxMTAzOTE3OTgzNjgwNTQyNTU5NzQzNDYzOTYxMzM0MzExOTI3MzAyNjk4NTc3ODQyMDIxMzEwNDEzMzMzNTU5NTEzOTI2Mzg2MjAzNyIsIjEwMjM4MzE5NjY1MjY0MDc0ODQwNTY3MTc5Njg1MjYzOTA1NDY5NTE4NDgzMTk4ODkzMzUwNDMxMDMwNjE3NTMwODY3NjA1NDczNjEzIiwiMSJdfRIkYUhSMGNITTZMeTloWTJOdmRXNTBjeTVuYjI5bmJHVXVZMjl0GmZleUpoYkdjaU9pSlNVekkxTmlJc0ltdHBaQ0k2SWpabU9UYzNOMkUyT0RVNU1EYzNPVGhsWmpjNU5EQTJNbU13TUdJMk5XUTJObU15TkRCaU1XSWlMQ0owZVhBaU9pSktWMVFpZlEiTTE1MDM1MTYxNTYwMTU5OTcxNjMzODAwOTgzNjE5OTMxNDk4Njk2MTUyNjMzNDI2NzY4MDE2OTY2MDU3NzcwNjQzMjYyMDIyMDk2MDczEISIBxJkCk4KRgofL2Nvc21vcy5jcnlwdG8uc2VjcDI1NmsxLlB1YktleRIjCiEC620v5dX6qHW1pxNtJNnq/I/T5a7hOOaJx/I9MtIFFcwSBAoCCAESEgoMCgRjb255EgQyMDAwEMCaDBpA6pSglqOdkufji1fUl1NBOBCimsduiA4GD/bd3OXFlBoi35CE2qNfdclGgF8ZO7WchVsnx1PuYnJalNq/RJw8RA=="
sampleTxBytes, err := base64.StdEncoding.DecodeString(sampleTxBase64)
require.NoError(t, err)

tx, err := k.ClientCtx.TxConfig.TxDecoder()(sampleTxBytes)
require.NoError(t, err)

anteHandler, err := appante.NewAnteHandler(
appante.HandlerOptions{
HandlerOptions: ante.HandlerOptions{
AccountKeeper: k.Simapp.AccountKeeper,
BankKeeper: k.Simapp.BankKeeper,
FeegrantKeeper: k.Simapp.FeeGrantKeeper,
SignModeHandler: k.ClientCtx.TxConfig.SignModeHandler(),
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
ZKAuthKeeper: k.ZKAuthKeeper,
},
)

_, err = anteHandler(k.Ctx, tx, false)
require.NoError(t, err)
}
30 changes: 30 additions & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ import (
upgradeclient "github.com/Finschia/finschia-sdk/x/upgrade/client"
upgradekeeper "github.com/Finschia/finschia-sdk/x/upgrade/keeper"
upgradetypes "github.com/Finschia/finschia-sdk/x/upgrade/types"
"github.com/Finschia/finschia-sdk/x/zkauth"
zkauthkeeper "github.com/Finschia/finschia-sdk/x/zkauth/keeper"
zkauthtypes "github.com/Finschia/finschia-sdk/x/zkauth/types"

// unnamed import of statik for swagger UI support
_ "github.com/Finschia/finschia-sdk/client/docs/statik"
Expand Down Expand Up @@ -143,6 +146,7 @@ var (
vesting.AppModuleBasic{},
tokenmodule.AppModuleBasic{},
collectionmodule.AppModuleBasic{},
zkauth.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -202,6 +206,7 @@ type SimApp struct {
ClassKeeper classkeeper.Keeper
TokenKeeper tokenkeeper.Keeper
CollectionKeeper collectionkeeper.Keeper
ZKAuthKeeper zkauthkeeper.Keeper

// the module manager
mm *module.Manager
Expand Down Expand Up @@ -357,6 +362,14 @@ func NewSimApp(
// If evidence needs to be handled for the app, set routes in router here and seal
app.EvidenceKeeper = *evidenceKeeper

// create zkauth keeper
jwKsMap := zkauthtypes.NewJWKs()
Mdaiki0730 marked this conversation as resolved.
Show resolved Hide resolved
// todo: verification key should be loaded from file.
Mdaiki0730 marked this conversation as resolved.
Show resolved Hide resolved
var verificationKey = []byte("{\n \"protocol\": \"groth16\",\n \"curve\": \"bn128\",\n \"nPublic\": 1,\n \"vk_alpha_1\": [\n \"20491192805390485299153009773594534940189261866228447918068658471970481763042\",\n \"9383485363053290200918347156157836566562967994039712273449902621266178545958\",\n \"1\"\n ],\n \"vk_beta_2\": [\n [\n \"6375614351688725206403948262868962793625744043794305715222011528459656738731\",\n \"4252822878758300859123897981450591353533073413197771768651442665752259397132\"\n ],\n [\n \"10505242626370262277552901082094356697409835680220590971873171140371331206856\",\n \"21847035105528745403288232691147584728191162732299865338377159692350059136679\"\n ],\n [\n \"1\",\n \"0\"\n ]\n ],\n \"vk_gamma_2\": [\n [\n \"10857046999023057135944570762232829481370756359578518086990519993285655852781\",\n \"11559732032986387107991004021392285783925812861821192530917403151452391805634\"\n ],\n [\n \"8495653923123431417604973247489272438418190587263600148770280649306958101930\",\n \"4082367875863433681332203403145435568316851327593401208105741076214120093531\"\n ],\n [\n \"1\",\n \"0\"\n ]\n ],\n \"vk_delta_2\": [\n [\n \"21349319915249622662700217004338779716430783387183352766870647565870141979289\",\n \"8213816744021090866451311756048660670381089332123677295675725952502733471420\"\n ],\n [\n \"4787213629490370557685854255230879988945206163033639129474026644007741911075\",\n \"20003855859301921415178037270191878217707285640767940877063768682564788786247\"\n ],\n [\n \"1\",\n \"0\"\n ]\n ],\n \"vk_alphabeta_12\": [\n [\n [\n \"2029413683389138792403550203267699914886160938906632433982220835551125967885\",\n \"21072700047562757817161031222997517981543347628379360635925549008442030252106\"\n ],\n [\n \"5940354580057074848093997050200682056184807770593307860589430076672439820312\",\n \"12156638873931618554171829126792193045421052652279363021382169897324752428276\"\n ],\n [\n \"7898200236362823042373859371574133993780991612861777490112507062703164551277\",\n \"7074218545237549455313236346927434013100842096812539264420499035217050630853\"\n ]\n ],\n [\n [\n \"7077479683546002997211712695946002074877511277312570035766170199895071832130\",\n \"10093483419865920389913245021038182291233451549023025229112148274109565435465\"\n ],\n [\n \"4595479056700221319381530156280926371456704509942304414423590385166031118820\",\n \"19831328484489333784475432780421641293929726139240675179672856274388269393268\"\n ],\n [\n \"11934129596455521040620786944827826205713621633706285934057045369193958244500\",\n \"8037395052364110730298837004334506829870972346962140206007064471173334027475\"\n ]\n ]\n ],\n \"IC\": [\n [\n \"801233197807402683764630185033839955156034586542543249813920835808534245147\",\n \"13286420793149616228297035344471157585445615731792629462934831296345279687002\",\n \"1\"\n ],\n [\n \"17608180544527043978731301492557909061209088433544687588079992534282036547698\",\n \"11240405619785894451348234456278767489162139374206168239508590931049712428392\",\n \"1\"\n ]\n ]\n}")
zemyblue marked this conversation as resolved.
Show resolved Hide resolved
zkAuthVerifier := zkauthtypes.NewZKAuthVerifier(verificationKey)
zkauthKeeper := zkauthkeeper.NewKeeper(appCodec, keys[zkauthtypes.StoreKey], jwKsMap, zkAuthVerifier, app.MsgServiceRouter())
app.ZKAuthKeeper = *zkauthKeeper

/**** Module Options ****/

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
Expand Down Expand Up @@ -388,6 +401,7 @@ func NewSimApp(
tokenmodule.NewAppModule(appCodec, app.TokenKeeper),
collectionmodule.NewAppModule(appCodec, app.CollectionKeeper),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
zkauth.NewAppModule(appCodec, app.ZKAuthKeeper, app.AccountKeeper, app.BankKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -415,6 +429,7 @@ func NewSimApp(
vestingtypes.ModuleName,
token.ModuleName,
collection.ModuleName,
zkauthtypes.ModuleName,
)
app.mm.SetOrderEndBlockers(
crisistypes.ModuleName,
Expand All @@ -436,6 +451,7 @@ func NewSimApp(
foundation.ModuleName,
token.ModuleName,
collection.ModuleName,
zkauthtypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -463,6 +479,7 @@ func NewSimApp(
vestingtypes.ModuleName,
token.ModuleName,
collection.ModuleName,
zkauthtypes.ModuleName,
)

// Uncomment if you want to set a custom migration order here.
Expand Down Expand Up @@ -496,6 +513,19 @@ func NewSimApp(
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)

// todo: please use this code when integrate zkauth's anteHandler.
//anteHandler, err := appante.NewAnteHandler(
// appante.HandlerOptions{
// HandlerOptions: ante.HandlerOptions{
// AccountKeeper: app.AccountKeeper,
// BankKeeper: app.BankKeeper,
// FeegrantKeeper: app.FeeGrantKeeper,
// SignModeHandler: encodingConfig.TxConfig.SignModeHandler(),
// SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
// },
// ZKAuthKeeper: app.ZKAuthKeeper,
// },
//)
anteHandler, err := ante.NewAnteHandler(
ante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
Expand Down
51 changes: 51 additions & 0 deletions x/zkauth/ante/zkauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ante

import (
sdk "github.com/Finschia/finschia-sdk/types"
sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
authsigning "github.com/Finschia/finschia-sdk/x/auth/signing"
zkauthtypes "github.com/Finschia/finschia-sdk/x/zkauth/types"
)

type ZKAuthMsgDecorator struct {
zk zkauthtypes.ZKAuthKeeper
}

func NewZKAuthMsgDecorator(zk zkauthtypes.ZKAuthKeeper) ZKAuthMsgDecorator {
return ZKAuthMsgDecorator{zk: zk}
}

func (zka ZKAuthMsgDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
/*
todo:
If there are multiple msg, the order of the pubKey of the signer and signature must be the same.
- If msg is zkauth, use zkauth signature verification.
- If msg is a general tx, it is verified by general signature verification.
(In this implementation, it is assumed that there is only zkauth msg.)

If the number of msg and the number of pubKey are not the same, how should matching be done?
Basically, in the case of zkauth msg, ephPubKey must be idempotent for each msg.
*/

sigTx, ok := tx.(authsigning.SigVerifiableTx)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type")
}

Check warning on line 33 in x/zkauth/ante/zkauth.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/ante/zkauth.go#L32-L33

Added lines #L32 - L33 were not covered by tests

msgs := sigTx.GetMsgs()
pubKeys, err := sigTx.GetPubKeys()
if err != nil {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidPubKey, "invalid public key, %s", err)
}

Check warning on line 39 in x/zkauth/ante/zkauth.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/ante/zkauth.go#L38-L39

Added lines #L38 - L39 were not covered by tests

for i, msg := range msgs {
if zkMsg, ok := msg.(*zkauthtypes.MsgExecution); ok {
// verify ZKAuth signature
if err := zkauthtypes.VerifyZKAuthSignature(ctx, zka.zk, pubKeys[i].Bytes(), zkMsg); err != nil {
return ctx, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "invalid zkauth signature")
}

Check warning on line 46 in x/zkauth/ante/zkauth.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/ante/zkauth.go#L45-L46

Added lines #L45 - L46 were not covered by tests
}
}

return next(ctx, tx, simulate)
}
58 changes: 58 additions & 0 deletions x/zkauth/ante/zkauth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ante_test

import (
"math/big"
"testing"

"github.com/stretchr/testify/require"

codectypes "github.com/Finschia/finschia-sdk/codec/types"
"github.com/Finschia/finschia-sdk/crypto/keys/secp256k1"
types2 "github.com/Finschia/finschia-sdk/crypto/types"
sdk "github.com/Finschia/finschia-sdk/types"
banktype "github.com/Finschia/finschia-sdk/x/bank/types"
"github.com/Finschia/finschia-sdk/x/zkauth/ante"
"github.com/Finschia/finschia-sdk/x/zkauth/testutil"
"github.com/Finschia/finschia-sdk/x/zkauth/types"
)

func TestNewZKAuthMsgDecorator(t *testing.T) {
f := testutil.ZkAuthKeeper(t)
decorator := ante.NewZKAuthMsgDecorator(f.ZKAuthKeeper)
accounts, err := f.CreateTestAccounts(2)
require.NoError(t, err)

// bank msg
subMsg := &banktype.MsgSend{
FromAddress: accounts[0].String(),
ToAddress: accounts[1].String(),
Amount: sdk.Coins{sdk.NewInt64Coin("cony", 1000000)},
}
any, err := codectypes.NewAnyWithValue(subMsg)
const proofStr = "{\n \"pi_a\": [\n \"7575287679446209007446416020137456670042570578978230730578011103770415897062\",\n \"20469978368515629364541212704109752583692706286549284712208570249653184893207\",\n \"1\"\n ],\n \"pi_b\": [\n [\n \"4001119070037193619600086014535210556571209449080681376392853276923728808564\",\n \"18475391841797083641468254159150812922259839776046448499150732610021959794558\"\n ],\n [\n \"19781252109528278034156073207688818205850783935629584279449144780221040670063\",\n \"5873714313814830719712095806732872482213125567325442209795797618441438990229\"\n ],\n [\n \"1\",\n \"0\"\n ]\n ],\n \"pi_c\": [\n \"18920522434978516095250248740518039198650690968720755259416280639852277665022\",\n \"1945774583580804632084048753815901730674007769630810705050114062476636502591\",\n \"1\"\n ],\n \"protocol\": \"groth16\",\n \"curve\": \"bn128\"\n}"
msg := &types.MsgExecution{
Msgs: []*codectypes.Any{any},
ZkAuthSignature: types.ZKAuthSignature{
ZkAuthInputs: &types.ZKAuthInputs{
ProofPoints: []byte(proofStr),
IssF: "aHR0cHM6Ly9hY2NvdW50cy5nb29nbGUuY29t",
HeaderBase64: "eyJhbGciOiJSUzI1NiIsImtpZCI6IjU1YzE4OGE4MzU0NmZjMTg4ZTUxNTc2YmE3MjgzNmUwNjAwZThiNzMiLCJ0eXAiOiJKV1QifQ",
AddressSeed: "15035161560159971633800983619931498696152633426768016966057770643262022096073",
},
MaxBlockHeight: 32754,
},
}
err = f.TxBuilder.SetMsgs(msg)
require.NoError(t, err)

ephPubKey, ok := new(big.Int).SetString("18948426102457371978524559226152399917062673825697601263047735920285791872240", 10)
require.True(t, ok)
pub := secp256k1.PubKey{Key: ephPubKey.Bytes()}
tx, err := f.CreateTestTx([]types2.PubKey{&pub}, []uint64{uint64(0)})
require.NoError(t, err)

_, err = decorator.AnteHandle(f.Ctx, tx, false, func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, err error) {
return ctx, nil
})
require.NoError(t, err)
}
7 changes: 5 additions & 2 deletions x/zkauth/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ func NewCmdExecution() *cobra.Command {
if err != nil {
return err
}
clientCtx.Codec.UnmarshalInterfaceJSON(bytes, &zkAuthInputs)
maxBlockHeight, err := strconv.ParseUint(args[2], 10, 64)
err = clientCtx.Codec.UnmarshalInterfaceJSON(bytes, &zkAuthInputs)
if err != nil {
return err
}
maxBlockHeight, err := strconv.ParseInt(args[2], 10, 64)
if err != nil {
return err
}
Expand Down
Loading
Loading