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

Add Confidential Transfers Module #1980

Open
wants to merge 83 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
b667e02
ciphertext example type
dssei Oct 16, 2024
e6dab7b
fix linting errors
dssei Oct 16, 2024
4bd729e
commit generated code
dssei Oct 16, 2024
aad5138
codec + some validation + routing
dssei Oct 16, 2024
6797664
adding zk related objects
dssei Oct 17, 2024
592beb2
tests
dssei Oct 17, 2024
b33ad17
remove challenge from ciphertext validity proof
dssei Oct 17, 2024
a565c04
revert params.pb.go
dssei Oct 17, 2024
3333a06
Fix test to use correct result object for proofs. Add range proofs
dssei Oct 29, 2024
46aac3c
Add decryption test
dssei Oct 29, 2024
085deb9
RemainingBalanceEqualityProof
dssei Oct 29, 2024
675c39e
TransferAmountLoEqualityProof & TransferAmountHiEqualityProof tests
dssei Oct 29, 2024
3e15326
fix formatting
dssei Oct 29, 2024
8a73f57
add missing validations
dssei Oct 29, 2024
7f7550f
auditors
dssei Oct 30, 2024
a792340
add marshalling/unmarshalling of the messages
dssei Oct 30, 2024
81265bc
go mod tidy
dssei Oct 30, 2024
bcdb017
fix linting issues
dssei Oct 30, 2024
fd16a60
add more tests
dssei Oct 30, 2024
0775f98
tests for zk.go
dssei Oct 30, 2024
1b07386
more tests
dssei Oct 30, 2024
ee14d68
Scaffolding (#1916)
mj850 Nov 1, 2024
6129d89
CT types - Apply balance close account (#1919)
dssei Nov 4, 2024
71ad41a
Add Deposit and InitializeAccount types (#1921)
mj850 Nov 5, 2024
92bbcf5
Genesis draft (#1924)
dssei Nov 7, 2024
77c7fb4
Ct queries (#1927)
dssei Nov 12, 2024
094bf82
Add msg_server methods (#1923)
mj850 Nov 13, 2024
26a9971
basic cli and first draft command for ct module (#1929)
dssei Nov 14, 2024
6c66a70
Add confidential transfer queries to seid (#1931)
mj850 Nov 15, 2024
a7d5cc5
Update confidential transfers swagger/openAPI spec (#1937)
dssei Nov 18, 2024
c51f80d
Add other confidential tx methods to seid (#1933)
mj850 Nov 19, 2024
a45b042
Ct ACL mappings (#1947)
dssei Nov 19, 2024
435c029
Add query decrypted tx method to confidential transfers client (#1950)
mj850 Nov 21, 2024
9a565b8
Add Integration tests for CT Module (#1958)
mj850 Nov 26, 2024
fcfa23b
add ct to load test framework (#1959)
dssei Dec 3, 2024
193a429
Use BigInt in CT Module (#1966)
mj850 Dec 3, 2024
8f1b057
Manually consume gas for expensive txes and add params (#1972)
mj850 Dec 6, 2024
cb36ad7
refactor ct cli to use SDK coin format (#1977)
dssei Dec 6, 2024
03f7e49
Use updated CachedRangeVerifier (#1975)
mj850 Dec 6, 2024
2147df3
params revert
mj850 Dec 7, 2024
a04736a
fix deps
mj850 Dec 7, 2024
7f04330
Merge branch 'main' of github.com:sei-protocol/sei-chain into feature…
dssei Dec 9, 2024
49203d9
rollback evm params
dssei Dec 9, 2024
e5953b7
change pendingBalanceLo in decryptable account object
dssei Dec 6, 2024
06ab705
Bump coinbase crypto lib version (#1985)
dssei Dec 10, 2024
b18cbc9
Increase test coverage (#1986)
dssei Dec 11, 2024
2a61565
Merge branch 'main' into feature/ct_module
dssei Dec 11, 2024
2a81859
increase test time-out
dssei Dec 11, 2024
dccadcc
tests for VerifyAuditorProof
dssei Dec 11, 2024
3ea3b67
manual merge with main
dssei Dec 11, 2024
5ff5ab3
MsgInitializeAccount tests
dssei Dec 12, 2024
5e292e3
MsgTransfer#FromProto invalid inputs tests
dssei Dec 12, 2024
dd546bc
Merge branch 'main' of github.com:sei-protocol/sei-chain into feature…
dssei Dec 12, 2024
1f75a8e
- tests for transfer msg decrypt
dssei Dec 12, 2024
616733e
- tests for InitializeAccount invalid fields
dssei Dec 12, 2024
5073bd0
- tests for MsgInitializeAccount_Decrypt
dssei Dec 12, 2024
c1d2146
- tests for MsgApplyPendingBalance
dssei Dec 12, 2024
88dd53d
- tests for MsgWithdraw
dssei Dec 13, 2024
2b6bea0
- route and type message tests
dssei Dec 13, 2024
0999e20
Use Signed Message for Key Devrivation (#1981)
mj850 Dec 13, 2024
e19c16c
Consume gas before verifying range proof (#1987)
mj850 Dec 13, 2024
ff2acb6
Merge branch 'main' into feature/ct_module
dssei Dec 13, 2024
e188015
- fix tests
dssei Dec 13, 2024
e4233ec
- few more tests
dssei Dec 13, 2024
7fa9de1
Ct transfer precompile (#1994)
dssei Dec 19, 2024
108b09f
Transfer with auditors precompile (#1997)
dssei Dec 20, 2024
ec80923
Ct init account precompile (#2017)
dssei Jan 6, 2025
417b4a4
Support Sei addresses in transfer precompiles (#2021)
dssei Jan 6, 2025
7a462fe
Ct deposit precompile (#2023)
dssei Jan 8, 2025
c398a7a
Use caller instead of from address (#2030)
dssei Jan 13, 2025
636ae87
Add ApplyPendingBalance, Withdraw and CloseAccount to ConfidentialTra…
mj850 Jan 15, 2025
f39d303
Ct Account query precompile (#2038)
dssei Jan 15, 2025
6bb5ffa
Fix account not found error code (#2041)
dssei Jan 17, 2025
b1b9047
Ct EVM transactions decryption (#2046)
dssei Jan 27, 2025
6e44b42
update artifacts ver
mj850 Feb 19, 2025
818ef99
Add ACL Mappings for InitializeAccount (#2076)
mj850 Feb 28, 2025
13f4775
L-09 Proto Serialization Failures (#2079)
mj850 Feb 28, 2025
aaca330
L02/05 Update proto messages (#2077)
mj850 Feb 28, 2025
322346d
Add range proofs for transfer amounts (#2070)
mj850 Feb 28, 2025
f54784f
Update RPC Response in CT CLI Query with Error (#2078)
mj850 Mar 5, 2025
9826597
Consume more gas in each CT operation. Limit number of auditors (#2080)
dssei Mar 6, 2025
eab8e9e
adding allowlist to CT (#2082)
dssei Mar 6, 2025
94c9f52
Validation fixes (#2084)
dssei Mar 6, 2025
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
Prev Previous commit
Next Next commit
Ct queries (#1927)
* add genesis init/export tests

* refactor store

* confidential transfers queries

* working draft test

* more tests

* refactor tests

* all accounts query

* formatting

* add pagination to request/response

* update implementation to use paginated response instead

* formatting

* remove redundant param

* all accounts with denoms

* clean up commented code

* return GetAccount back to keeper

* formatting
  • Loading branch information
dssei authored and mj850 committed Dec 6, 2024
commit 77c7fb493a75876c259ecb85a4a36c444c4877e1
14 changes: 14 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -116,6 +116,7 @@ import (
"github.com/sei-protocol/sei-chain/utils"
"github.com/sei-protocol/sei-chain/utils/metrics"
"github.com/sei-protocol/sei-chain/wasmbinding"
ctmodule "github.com/sei-protocol/sei-chain/x/confidentialtransfers"
ctkeeper "github.com/sei-protocol/sei-chain/x/confidentialtransfers/keeper"
cttypes "github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"
epochmodule "github.com/sei-protocol/sei-chain/x/epoch"
@@ -209,6 +210,7 @@ var (
wasm.AppModuleBasic{},
epochmodule.AppModuleBasic{},
tokenfactorymodule.AppModuleBasic{},
ctmodule.AppModuleBasic{},
// this line is used by starport scaffolding # stargate/app/moduleBasic
)

@@ -226,6 +228,7 @@ var (
wasm.ModuleName: {authtypes.Burner},
evmtypes.ModuleName: {authtypes.Minter, authtypes.Burner},
tokenfactorytypes.ModuleName: {authtypes.Minter, authtypes.Burner},
cttypes.ModuleName: {},
// this line is used by starport scaffolding # stargate/app/maccPerms
}

@@ -549,6 +552,12 @@ func New(
app.DistrKeeper,
)

app.ConfidentialTransfersKeeper = ctkeeper.NewKeeper(
appCodec,
app.keys[(cttypes.StoreKey)],
app.GetSubspace(cttypes.ModuleName),
app.AccountKeeper)

// The last arguments can contain custom message handlers, and custom query handlers,
// if we want to allow any custom callbacks
supportedFeatures := "iterator,staking,stargate,sei"
@@ -752,6 +761,7 @@ func New(
epochModule,
tokenfactorymodule.NewAppModule(app.TokenFactoryKeeper, app.AccountKeeper, app.BankKeeper),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
ctmodule.NewAppModule(app.ConfidentialTransfersKeeper),
// this line is used by starport scaffolding # stargate/app/appModule
)

@@ -783,6 +793,7 @@ func New(
evmtypes.ModuleName,
wasm.ModuleName,
tokenfactorytypes.ModuleName,
cttypes.ModuleName,
acltypes.ModuleName,
)

@@ -814,6 +825,7 @@ func New(
evmtypes.ModuleName,
wasm.ModuleName,
tokenfactorytypes.ModuleName,
cttypes.ModuleName,
acltypes.ModuleName,
)

@@ -843,6 +855,7 @@ func New(
feegrant.ModuleName,
oracletypes.ModuleName,
tokenfactorytypes.ModuleName,
cttypes.ModuleName,
epochmoduletypes.ModuleName,
wasm.ModuleName,
evmtypes.ModuleName,
@@ -874,6 +887,7 @@ func New(
transferModule,
epochModule,
tokenfactorymodule.NewAppModule(app.TokenFactoryKeeper, app.AccountKeeper, app.BankKeeper),
ctmodule.NewAppModule(app.ConfidentialTransfersKeeper),
// this line is used by starport scaffolding # stargate/app/appModule
)
app.sm.RegisterStoreDecoders()
5 changes: 5 additions & 0 deletions proto/confidentialtransfers/confidential.proto
Original file line number Diff line number Diff line change
@@ -15,4 +15,9 @@ message CtAccount {
uint32 pending_balance_credit_counter = 4;
Ciphertext available_balance = 5; // elgamal encoded balance
string decryptable_available_balance = 6; // aes encoded balance
}

message CtAccountWithDenom {
string denom = 1;
CtAccount account = 2 [(gogoproto.nullable) = false];
}
42 changes: 32 additions & 10 deletions proto/confidentialtransfers/query.proto
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
syntax = "proto3";
package seiprotocol.seichain.confidentialtransfers;

import "cosmos/base/query/v1beta1/pagination.proto";
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "confidentialtransfers/confidential.proto";

option go_package = "github.com/sei-protocol/sei-chain/x/confidentialtransfers/types";

// TODO: Define any query messages here
// Query defines the gRPC querier service.
service Query {
// TODO: This is a mock method. Remove when we add real queries
rpc TestQuery (TestQueryRequest) returns (TestQueryResponse) {
rpc GetCtAccount (GetCtAccountRequest) returns (GetCtAccountResponse) {
option (google.api.http) = {
get: "/seichain/confidentialtransfers/test_query"
get: "/seichain/confidentialtransfers/account/{address}/{denom}"
};
}

rpc GetAllCtAccounts (GetAllCtAccountsRequest) returns (GetAllCtAccountsResponse) {
option (google.api.http) = {
get: "/seichain/confidentialtransfers/account/{address}"
};
}
}

message GetCtAccountRequest {
string address = 1;
string denom = 2;
}

// TODO: This is a mock method. Remove when we add real queries
message TestQueryRequest {
string test_field = 1;
message GetCtAccountResponse {
CtAccount account = 1;
}

// TODO: This is a mock method. Remove when we add real queries
message TestQueryResponse {
string test_field = 1;
message GetAllCtAccountsRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

string address = 1;

// pagination defines an optional pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

message GetAllCtAccountsResponse {
repeated CtAccountWithDenom accounts = 1 [(gogoproto.nullable) = false];

// pagination defines the pagination in the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}
9 changes: 4 additions & 5 deletions x/confidentialtransfers/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -3,17 +3,17 @@ package keeper
import (
"fmt"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"
)

func (k BaseKeeper) InitGenesis(ctx sdk.Context, gs *types.GenesisState) {
k.SetParams(ctx, gs.Params)
store := k.getAccountStore(ctx)
for i := range gs.Accounts {
genesisCtAccount := gs.Accounts[i]
store := ctx.KVStore(k.storeKey)

bz := k.cdc.MustMarshal(&genesisCtAccount.Account) // Marshal the Account object into bytes
store.Set(genesisCtAccount.Key, bz)
}
@@ -31,11 +31,10 @@ func (k BaseKeeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
}

func (k BaseKeeper) GetPaginatedAccounts(ctx sdk.Context, pagination *query.PageRequest) ([]types.GenesisCtAccount, *query.PageResponse, error) {
store := ctx.KVStore(k.storeKey)
supplyStore := prefix.NewStore(store, types.AccountsKey)
store := k.getAccountStore(ctx)

genesisAccounts := make([]types.GenesisCtAccount, 0)
pageRes, err := query.Paginate(supplyStore, pagination, func(key, value []byte) error {
pageRes, err := query.Paginate(store, pagination, func(key, value []byte) error {
var ctAccount types.CtAccount
err := ctAccount.Unmarshal(value)
if err != nil {
65 changes: 65 additions & 0 deletions x/confidentialtransfers/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package keeper_test

import (
"crypto/ecdsa"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"
"github.com/sei-protocol/sei-cryptography/pkg/encryption"
"github.com/sei-protocol/sei-cryptography/pkg/encryption/elgamal"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

@@ -16,3 +21,63 @@ func (suite *KeeperTestSuite) TestDefaultGenesisState() {
suite.Require().NotNil(exportedGenesis)
suite.Require().Equal(genesisState, exportedGenesis)
}

func (suite *KeeperTestSuite) TestGenesisExportImportState() {
pk1, _ := encryption.GenerateKey()
pk2, _ := encryption.GenerateKey()
addr1 := sdk.AccAddress("addr1")
addr2 := sdk.AccAddress("addr2")
testDenom1 := fmt.Sprintf("factory/%s/TEST1", addr1.String())
testDenom2 := fmt.Sprintf("factory/%s/TEST2", addr2.String())

ctAcc1 := generateCtAccount(pk1, testDenom1, 1000)
ctAcc2 := generateCtAccount(pk2, testDenom2, 2000)

accounts := []types.GenesisCtAccount{
{
Key: []byte(addr1.String() + testDenom1),
Account: ctAcc1,
},
{
Key: []byte(addr2.String() + testDenom2),
Account: ctAcc2,
},
}
genesisState := types.NewGenesisState(types.DefaultParams(), accounts)
app := suite.App
suite.Ctx = app.BaseApp.NewContext(false, tmproto.Header{})

suite.App.ConfidentialTransfersKeeper.InitGenesis(suite.Ctx, genesisState)

exportedGenesis := suite.App.ConfidentialTransfersKeeper.ExportGenesis(suite.Ctx)
suite.Require().NotNil(exportedGenesis)
suite.Require().Equal(genesisState, exportedGenesis)
}

func generateCtAccount(pk *ecdsa.PrivateKey, testDenom string, balance uint64) types.CtAccount {
eg := elgamal.NewTwistedElgamal()
keyPair, _ := eg.KeyGen(*pk, testDenom)

aesPK1, _ := encryption.GetAESKey(*pk, testDenom)

amountLo := uint64(100)
amountHi := uint64(0)

decryptableBalance, _ := encryption.EncryptAESGCM(balance, aesPK1)

ciphertextLo, _, _ := eg.Encrypt(keyPair.PublicKey, amountLo)
ciphertextHi, _, _ := eg.Encrypt(keyPair.PublicKey, amountHi)

zeroCiphertextAvailable, _, _ := eg.Encrypt(keyPair.PublicKey, amountLo)

account := &types.Account{
PublicKey: keyPair.PublicKey,
PendingBalanceLo: ciphertextLo,
PendingBalanceHi: ciphertextHi,
PendingBalanceCreditCounter: 1,
AvailableBalance: zeroCiphertextAvailable,
DecryptableAvailableBalance: decryptableBalance,
}

return *types.NewCtAccount(account)
}
80 changes: 80 additions & 0 deletions x/confidentialtransfers/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package keeper

import (
"context"

"github.com/cosmos/cosmos-sdk/types/query"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var _ types.QueryServer = BaseKeeper{}

func (k BaseKeeper) GetCtAccount(ctx context.Context, req *types.GetCtAccountRequest) (*types.GetCtAccountResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if req.Address == "" {
return nil, status.Error(codes.InvalidArgument, "address cannot be empty")
}

address, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %s", err.Error())
}

if req.Denom == "" {
return nil, status.Error(codes.InvalidArgument, "invalid denom")
}

sdkCtx := sdk.UnwrapSDKContext(ctx)

ctAccount, found := k.getCtAccount(sdkCtx, address, req.Denom)
if !found {
return nil, status.Errorf(codes.NotFound, "account not found for account %s and denom %s",
req.Address, req.Denom)
}

return &types.GetCtAccountResponse{Account: &ctAccount}, nil
}

func (k BaseKeeper) GetAllCtAccounts(ctx context.Context, req *types.GetAllCtAccountsRequest) (*types.GetAllCtAccountsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if req.Address == "" {
return nil, status.Error(codes.InvalidArgument, "address cannot be empty")
}

address, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %s", err.Error())
}

sdkCtx := sdk.UnwrapSDKContext(ctx)

store := k.getAccountStoreForAddress(sdkCtx, address)
accounts := make([]types.CtAccountWithDenom, 0)
pageRes, err := query.Paginate(store, req.Pagination, func(denom, value []byte) error {

var ctAccount types.CtAccount
err = k.cdc.Unmarshal(value, &ctAccount)
if err != nil {
return err
}
accounts = append(accounts, types.CtAccountWithDenom{Denom: string(denom), Account: ctAccount})
return nil
})

if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)
}

return &types.GetAllCtAccountsResponse{Accounts: accounts, Pagination: pageRes}, nil

}
Loading