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

upgrade to zeto v0.0.8; check transfer amount against range #418

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion domains/zeto/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.5

require (
github.com/go-resty/resty/v2 v2.14.0
github.com/hyperledger-labs/zeto/go-sdk v0.0.0-20241004174307-aa3c1fdf0966
github.com/hyperledger-labs/zeto/go-sdk v0.0.0-20241107154237-9855999483bf
github.com/hyperledger/firefly-common v1.4.11
github.com/hyperledger/firefly-signer v1.1.19-0.20241027192206-656dd986267e
github.com/iden3/go-iden3-crypto v0.0.17
Expand Down
4 changes: 4 additions & 0 deletions domains/zeto/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,12 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/hyperledger-labs/zeto/go-sdk v0.0.0-20241004174307-aa3c1fdf0966 h1:J5ZVvMRxRlM1GLtUdPTCNNRpB4QpJij/+ndud1QU8WY=
github.com/hyperledger-labs/zeto/go-sdk v0.0.0-20241004174307-aa3c1fdf0966/go.mod h1:WyUa1UIizlcBQnTEMK5tWWskFz2glSTm7S6HaJTarps=
github.com/hyperledger-labs/zeto/go-sdk v0.0.0-20241107154237-9855999483bf h1:FefdSudMmmGluZKFZY8wBLsxZ0gTNZoQhvqUo1lT298=
github.com/hyperledger-labs/zeto/go-sdk v0.0.0-20241107154237-9855999483bf/go.mod h1:/D/gnT0ajb4IJYyT5CpO0dXPsufD+3wramIVYKJ+iUQ=
github.com/hyperledger/firefly-common v1.4.11 h1:WKv2hQuNpS7yP51THxzpzrqU3jkln23C9vq5iminzBk=
github.com/hyperledger/firefly-common v1.4.11/go.mod h1:E7w/RxNtVnX52WXLQW9f2xVAgZnW70voZeE9sZrx/q0=
github.com/hyperledger/firefly-signer v1.1.19-0.20241027192206-656dd986267e h1:iqIs0NPtE9h1Vy4WBm2dsS4XbBIdpZPMNprlwdmIC0U=
github.com/hyperledger/firefly-signer v1.1.19-0.20241027192206-656dd986267e/go.mod h1:HDaDdht94JypRTunRGrcPL5Pvxfh4yigjatTrie5JUI=
github.com/iden3/go-iden3-crypto v0.0.17 h1:NdkceRLJo/pI4UpcjVah4lN/a3yzxRUGXqxbWcYh9mY=
github.com/iden3/go-iden3-crypto v0.0.17/go.mod h1:dLpM4vEPJ3nDHzhWFXDjzkn1qHoBeOT/3UEhXsEsP3E=
github.com/iden3/go-rapidsnark/prover v0.0.12 h1:IOqZrK+0J91zU7goB74qAzdwPg1ZRpZvsGjgpz4c+MQ=
Expand Down Expand Up @@ -249,6 +252,7 @@ github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIK
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down
1 change: 0 additions & 1 deletion domains/zeto/integration-test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func (s *zetoDomainTestSuite) TestZeto_Anon() {
}

func (s *zetoDomainTestSuite) TestZeto_AnonBatch() {
s.T().Skip()
s.testZetoFungible(s.T(), constants.TOKEN_ANON, true, false)
}

Expand Down
26 changes: 13 additions & 13 deletions domains/zeto/integration-test/testbed.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ db:
dsn: ':memory:'
autoMigrate: true
migrationsDir: '../../../core/go/db/migrations/sqlite'
debugQueries: false
debugQueries: true
# type: postgres
# postgres:
# dsn: 'postgres://postgres:my-secret@localhost:5432/postgres?sslmode=disable'
Expand Down Expand Up @@ -35,15 +35,15 @@ log:
file:
filename: build/testbed.zeto.log
wallets:
- name: wallet1
keySelector: .*
signer:
keyDerivation:
type: 'bip32'
keyStore:
type: 'static'
static:
keys:
seed:
encoding: none
inline: REPLACED BY GENERATED KEY IN TEST
- name: wallet1
keySelector: .*
signer:
keyDerivation:
type: 'bip32'
keyStore:
type: 'static'
static:
keys:
seed:
encoding: none
inline: REPLACED BY GENERATED KEY IN TEST
3 changes: 2 additions & 1 deletion domains/zeto/internal/msgs/en_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var (
MsgNoTransferParams = ffe("PD210024", "No transfer parameters provided")
MsgNoParamTo = ffe("PD210025", "Parameter 'to' is required (index=%d)")
MsgNoParamAmount = ffe("PD210026", "Parameter 'amount' is required (index=%d)")
MsgParamAmountGtZero = ffe("PD210027", "Parameter 'amount' must be greater than 0 (index=%d)")
MsgParamAmountInRange = ffe("PD210027", "Parameter 'amount' must be in the range (0, 2^100) (index=%d)")
MsgErrorParseTxId = ffe("PD210028", "Failed to parse transaction id. %s")
MsgErrorMarshalZetoCoinSchemaAbi = ffe("PD210029", "Failed to marshal Zeto Coin schema abi. %s")
MsgErrorMarshalMerkleTreeRootSchemaAbi = ffe("PD210030", "Failed to marshal Merkle Tree Root schema abi. %s")
Expand Down Expand Up @@ -144,4 +144,5 @@ var (
MsgNoDomainReceipt = ffe("PD210102", "Not implemented. See state receipt for coin transfers")
MsgUnknownSignPayload = ffe("PD210103", "Sign payload type '%s' not recognized")
MsgNullifierGenerationFailed = ffe("PD210104", "Failed to generate nullifier for coin")
MsgParamTotalAmountInRange = ffe("PD210105", "Total amount must be in the range (0, 2^100)")
)
9 changes: 7 additions & 2 deletions domains/zeto/internal/zeto/handler_mint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package zeto

import (
"context"
"math/big"
"testing"

"github.com/iden3/go-iden3-crypto/babyjub"
Expand All @@ -43,10 +44,14 @@ func TestMintValidateParams(t *testing.T) {
assert.EqualError(t, err, "PD210025: Parameter 'to' is required (index=0)")

_, err = h.ValidateParams(ctx, nil, "{\"mints\":[{\"to\":\"0x1234567890123456789012345678901234567890\",\"amount\":0}]}")
assert.EqualError(t, err, "PD210027: Parameter 'amount' must be greater than 0 (index=0)")
assert.EqualError(t, err, "PD210027: Parameter 'amount' must be in the range (0, 2^100) (index=0)")

_, err = h.ValidateParams(ctx, nil, "{\"mints\":[{\"to\":\"0x1234567890123456789012345678901234567890\",\"amount\":-10}]}")
assert.EqualError(t, err, "PD210027: Parameter 'amount' must be greater than 0 (index=0)")
assert.EqualError(t, err, "PD210027: Parameter 'amount' must be in the range (0, 2^100) (index=0)")

max := big.NewInt(0).Exp(big.NewInt(2), big.NewInt(100), nil).Text(10)
_, err = h.ValidateParams(ctx, nil, "{\"mints\":[{\"to\":\"0x1234567890123456789012345678901234567890\",\"amount\":"+max+"}]}")
assert.EqualError(t, err, "PD210105: Total amount must be in the range (0, 2^100)")

params, err := h.ValidateParams(ctx, nil, "{\"mints\":[{\"to\":\"0x1234567890123456789012345678901234567890\",\"amount\":10}]}")
assert.NoError(t, err)
Expand Down
13 changes: 11 additions & 2 deletions domains/zeto/internal/zeto/handler_transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"encoding/json"
"errors"
"math/big"
"testing"

"github.com/kaleido-io/paladin/domains/zeto/pkg/constants"
Expand Down Expand Up @@ -47,10 +48,18 @@ func TestTransferValidateParams(t *testing.T) {
assert.EqualError(t, err, "PD210025: Parameter 'to' is required (index=0)")

_, err = h.ValidateParams(ctx, nil, "{\"transfers\":[{\"to\":\"0x1234567890123456789012345678901234567890\",\"amount\":0}]}")
assert.EqualError(t, err, "PD210027: Parameter 'amount' must be greater than 0 (index=0)")
assert.EqualError(t, err, "PD210027: Parameter 'amount' must be in the range (0, 2^100) (index=0)")

_, err = h.ValidateParams(ctx, nil, "{\"transfers\":[{\"to\":\"0x1234567890123456789012345678901234567890\",\"amount\":-10}]}")
assert.EqualError(t, err, "PD210027: Parameter 'amount' must be greater than 0 (index=0)")
assert.EqualError(t, err, "PD210027: Parameter 'amount' must be in the range (0, 2^100) (index=0)")

amt1 := big.NewInt(0).Exp(big.NewInt(2), big.NewInt(100), nil)
amt1.Sub(amt1, big.NewInt(1000))
_, err = h.ValidateParams(ctx, nil, "{\"transfers\":[{\"to\":\"0x1234567890123456789012345678901234567890\",\"amount\":"+amt1.Text(10)+"}]}")
assert.NoError(t, err)
amt2 := big.NewInt(1000)
_, err = h.ValidateParams(ctx, nil, "{\"transfers\":[{\"to\":\"0x1234567890123456789012345678901234567890\",\"amount\":"+amt1.Text(10)+"},{\"to\":\"0x1234567890123456789012345678901234567890\",\"amount\":"+amt2.Text(10)+"}]}")
assert.EqualError(t, err, "PD210105: Total amount must be in the range (0, 2^100)")

params, err := h.ValidateParams(ctx, nil, "{\"transfers\":[{\"to\":\"0x1234567890123456789012345678901234567890\",\"amount\":10}]}")
assert.NoError(t, err)
Expand Down
14 changes: 13 additions & 1 deletion domains/zeto/internal/zeto/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package zeto

import (
"context"
"math/big"

"github.com/hyperledger/firefly-common/pkg/i18n"
"github.com/iden3/go-iden3-crypto/babyjub"
Expand All @@ -27,6 +28,11 @@ import (
"github.com/kaleido-io/paladin/toolkit/pkg/tktypes"
)

// due to the ZKP circuit needing to check if the amount is positive,
// the maximum transfer amount is (2^100 - 1)
// Reference: https://github.com/hyperledger-labs/zeto/blob/main/zkp/circuits/lib/check-positive.circom
var MAX_TRANSFER_AMOUNT = big.NewInt(0).Exp(big.NewInt(2), big.NewInt(100), nil)

func isNullifiersCircuit(circuitId string) bool {
return circuitId == constants.CIRCUIT_ANON_NULLIFIER || circuitId == constants.CIRCUIT_ANON_NULLIFIER_BATCH
}
Expand Down Expand Up @@ -61,6 +67,7 @@ func validateTransferParams(ctx context.Context, params []*types.TransferParamEn
if len(params) == 0 {
return i18n.NewError(ctx, msgs.MsgNoTransferParams)
}
total := big.NewInt(0)
for i, param := range params {
if param.To == "" {
return i18n.NewError(ctx, msgs.MsgNoParamTo, i)
Expand All @@ -69,9 +76,14 @@ func validateTransferParams(ctx context.Context, params []*types.TransferParamEn
return i18n.NewError(ctx, msgs.MsgNoParamAmount, i)
}
if param.Amount.Int().Sign() != 1 {
return i18n.NewError(ctx, msgs.MsgParamAmountGtZero, i)
return i18n.NewError(ctx, msgs.MsgParamAmountInRange, i)
}
total.Add(total, param.Amount.Int())
}
if total.Cmp(MAX_TRANSFER_AMOUNT) >= 0 {
return i18n.NewError(ctx, msgs.MsgParamTotalAmountInRange)
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion domains/zeto/internal/zeto/zeto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func TestInitTransaction(t *testing.T) {

req.Transaction.FunctionParamsJson = "{\"mints\":[{\"to\":\"Alice\",\"amount\":\"0\"}]}"
_, err = z.InitTransaction(context.Background(), req)
assert.EqualError(t, err, "PD210008: Failed to validate init transaction spec. PD210015: Failed to validate function params. PD210027: Parameter 'amount' must be greater than 0 (index=0)")
assert.EqualError(t, err, "PD210008: Failed to validate init transaction spec. PD210015: Failed to validate function params. PD210027: Parameter 'amount' must be in the range (0, 2^100) (index=0)")

req.Transaction.FunctionParamsJson = "{\"mints\":[{\"to\":\"Alice\",\"amount\":\"10\"}]}"
_, err = z.InitTransaction(context.Background(), req)
Expand Down