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

CT types - Apply balance close account #1919

Merged
merged 3 commits into from
Nov 4, 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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ require (
github.com/fzipp/gocyclo v0.5.1 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-critic/go-critic v0.6.3 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
Expand Down Expand Up @@ -175,6 +176,7 @@ require (
github.com/gostaticanalysis/nilerr v0.1.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 h1:BAIP2Gihuqh
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46/go.mod h1:QNpY22eby74jVhqH4WhDLDwxc/vqsern6pW+u2kbkpc=
github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
github.com/ghemawat/stream v0.0.0-20171120220530-696b145b53b9/go.mod h1:106OIgooyS7OzLDOpUGgm9fA3bQENb/cFSyyBmMoJDs=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down Expand Up @@ -769,6 +770,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=
github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is=
Expand Down
24 changes: 24 additions & 0 deletions proto/confidentialtransfers/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ option go_package = "github.com/sei-protocol/sei-chain/x/confidentialtransfers/t
service Msg {
// Transfer defines a method for sending coins from one account to another account.
rpc Transfer(MsgTransfer) returns (MsgTransferResponse);

// ApplyPendingBalance defines a method for applying pending balance to an account.
rpc ApplyPendingBalance(MsgApplyPendingBalance) returns (MsgApplyPendingBalanceResponse);

// CloseAccount defines a method for closing an account.
rpc CloseAccount(MsgCloseAccount) returns (MsgCloseAccountResponse);
}

// MsgTransfer represents a message to send coins from one account to another.
Expand Down Expand Up @@ -44,3 +50,21 @@ message Auditor {
CiphertextCiphertextEqualityProof transfer_amount_lo_equality_proof = 6 [(gogoproto.moretags) = "yaml:\"transfer_amount_lo_equality_proof\""];
CiphertextCiphertextEqualityProof transfer_amount_hi_equality_proof = 7 [(gogoproto.moretags) = "yaml:\"transfer_amount_hi_equality_proof\""];
}

// Message to be used in apply pending balance instruction/transaction
message MsgApplyPendingBalance {
string address = 1;
string denom = 2;
string new_decryptable_available_balance = 3;
}

message MsgApplyPendingBalanceResponse {}

// Message to be used in close account instruction/transaction
message MsgCloseAccount {
string address = 1;
string denom = 2;
CloseAccountProof proof = 3;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call this proofs instead for consistency

}

message MsgCloseAccountResponse {}
12 changes: 12 additions & 0 deletions proto/confidentialtransfers/zk.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,15 @@ message CiphertextCiphertextEqualityProof {
bytes zx = 6;
bytes zr = 7;
}

message ZeroBalanceProof {
bytes y_p = 1;
bytes y_d = 2;
bytes z = 3;
}

message CloseAccountProof {
ZeroBalanceProof zero_available_balance_proof = 1;
ZeroBalanceProof zero_pending_balance_lo_proof = 2;
ZeroBalanceProof zero_pending_balance_hi_proof = 3;
}
74 changes: 73 additions & 1 deletion x/confidentialtransfers/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

// confidential transfers message types
const (
TypeMsgTransfer = "transfer"
TypeMsgTransfer = "transfer"
TypeMsgApplyPendingBalance = "apply_pending_balance"
TypeMsgCloseAccount = "close_account"
)

var _ sdk.Msg = &MsgTransfer{}
Expand Down Expand Up @@ -134,3 +136,73 @@
Auditors: auditors,
}, nil
}

var _ sdk.Msg = &MsgApplyPendingBalance{}

// Route Implements Msg.
func (m *MsgApplyPendingBalance) Route() string { return RouterKey }

Check warning on line 143 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L143

Added line #L143 was not covered by tests

// Type Implements Msg.
func (m *MsgApplyPendingBalance) Type() string { return TypeMsgApplyPendingBalance }

Check warning on line 146 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L146

Added line #L146 was not covered by tests

func (m *MsgApplyPendingBalance) ValidateBasic() error {
if len(m.Address) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Address is required")
}

Check warning on line 151 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L148-L151

Added lines #L148 - L151 were not covered by tests

if len(m.Denom) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Denom is required")
}

Check warning on line 155 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L153-L155

Added lines #L153 - L155 were not covered by tests

if len(m.NewDecryptableAvailableBalance) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "NewDecryptableAvailableBalance is required")
}
return nil

Check warning on line 160 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L157-L160

Added lines #L157 - L160 were not covered by tests
}

func (m *MsgApplyPendingBalance) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(m))

Check warning on line 164 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L163-L164

Added lines #L163 - L164 were not covered by tests
}

func (m *MsgApplyPendingBalance) GetSigners() []sdk.AccAddress {
sender, _ := sdk.AccAddressFromBech32(m.Address)
return []sdk.AccAddress{sender}

Check warning on line 169 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L167-L169

Added lines #L167 - L169 were not covered by tests
}

var _ sdk.Msg = &MsgCloseAccount{}

// Route Implements Msg.
func (m *MsgCloseAccount) Route() string { return RouterKey }

Check warning on line 175 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L175

Added line #L175 was not covered by tests

// Type Implements Msg.
func (m *MsgCloseAccount) Type() string { return TypeMsgCloseAccount }

Check warning on line 178 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L178

Added line #L178 was not covered by tests

func (m *MsgCloseAccount) ValidateBasic() error {
if len(m.Address) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Address is required")
}

Check warning on line 183 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L182-L183

Added lines #L182 - L183 were not covered by tests

if len(m.Denom) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Denom is required")
}

Check warning on line 187 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L186-L187

Added lines #L186 - L187 were not covered by tests

if m.Proof == nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Proofs is required")
}

Check warning on line 191 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L190-L191

Added lines #L190 - L191 were not covered by tests

err := m.Proof.Validate()
if err != nil {
return err
}

Check warning on line 196 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L195-L196

Added lines #L195 - L196 were not covered by tests

return nil
}

func (m *MsgCloseAccount) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(m))

Check warning on line 202 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L201-L202

Added lines #L201 - L202 were not covered by tests
}

func (m *MsgCloseAccount) GetSigners() []sdk.AccAddress {
sender, _ := sdk.AccAddressFromBech32(m.Address)
return []sdk.AccAddress{sender}

Check warning on line 207 in x/confidentialtransfers/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/msgs.go#L205-L207

Added lines #L205 - L207 were not covered by tests
}
71 changes: 71 additions & 0 deletions x/confidentialtransfers/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,74 @@ func TestMsgTransfer_ValidateBasic(t *testing.T) {
})
}
}

func TestMsgCloseAccount_FromProto(t *testing.T) {
address := sdk.AccAddress("address1")
testDenom := "factory/sei1ft98au55a24vnu9tvd92cz09pzcfqkm5vlx99w/TEST"
privateKey, _ := elgamal.GenerateKey()
eg := elgamal.NewTwistedElgamal()
keypair, _ := eg.KeyGen(*privateKey, testDenom)
availableBalanceCiphertext, _, _ := eg.Encrypt(keypair.PublicKey, 0)
pendingBalanceLoCiphertext, _, _ := eg.Encrypt(keypair.PublicKey, 0)
pendingBalanceHiCiphertext, _, _ := eg.Encrypt(keypair.PublicKey, 0)

availableBalanceProof, err := zkproofs.NewZeroBalanceProof(keypair, availableBalanceCiphertext)
require.NoError(t, err)
pendingBalanceProofLo, err := zkproofs.NewZeroBalanceProof(keypair, pendingBalanceLoCiphertext)
require.NoError(t, err)
pendingBalanceProofHi, err := zkproofs.NewZeroBalanceProof(keypair, pendingBalanceHiCiphertext)
require.NoError(t, err)

closeAccountProofs := &CloseAccountProofs{
ZeroAvailableBalanceProof: availableBalanceProof,
ZeroPendingBalanceLoProof: pendingBalanceProofLo,
ZeroPendingBalanceHiProof: pendingBalanceProofHi,
}

closeAccountProof := CloseAccountProof{}
proof := closeAccountProof.ToProto(closeAccountProofs)

m := &MsgCloseAccount{
Address: address.String(),
Denom: testDenom,
Proof: proof,
}

marshalled, err := m.Marshal()
require.NoError(t, err)

// Reset the message
result := &MsgCloseAccount{}
err = result.Unmarshal(marshalled)
require.NoError(t, err)

assert.Equal(t, m.Address, result.Address)
assert.Equal(t, m.Denom, result.Denom)
resultProof, err := result.Proof.FromProto()
require.NoError(t, err)

assert.NoError(t, result.ValidateBasic())

assert.True(t, closeAccountProofs.ZeroAvailableBalanceProof.Yd.Equal(resultProof.ZeroAvailableBalanceProof.Yd))
assert.True(t, closeAccountProofs.ZeroAvailableBalanceProof.Yp.Equal(resultProof.ZeroAvailableBalanceProof.Yp))
assert.Equal(t, closeAccountProofs.ZeroAvailableBalanceProof.Z, resultProof.ZeroAvailableBalanceProof.Z)

assert.True(t, closeAccountProofs.ZeroPendingBalanceLoProof.Yd.Equal(resultProof.ZeroPendingBalanceLoProof.Yd))
assert.True(t, closeAccountProofs.ZeroPendingBalanceLoProof.Yp.Equal(resultProof.ZeroPendingBalanceLoProof.Yp))
assert.Equal(t, closeAccountProofs.ZeroPendingBalanceLoProof.Z, resultProof.ZeroPendingBalanceLoProof.Z)

assert.True(t, closeAccountProofs.ZeroPendingBalanceHiProof.Yd.Equal(resultProof.ZeroPendingBalanceHiProof.Yd))
assert.True(t, closeAccountProofs.ZeroPendingBalanceHiProof.Yp.Equal(resultProof.ZeroPendingBalanceHiProof.Yp))
assert.Equal(t, closeAccountProofs.ZeroPendingBalanceHiProof.Z, resultProof.ZeroPendingBalanceHiProof.Z)

// Make sure the proofs are valid
assert.True(t, zkproofs.VerifyZeroBalance(
resultProof.ZeroAvailableBalanceProof, &keypair.PublicKey, availableBalanceCiphertext))

assert.True(t, zkproofs.VerifyZeroBalance(
resultProof.ZeroPendingBalanceLoProof, &keypair.PublicKey, pendingBalanceLoCiphertext))

assert.True(t, zkproofs.VerifyZeroBalance(
resultProof.ZeroPendingBalanceHiProof, &keypair.PublicKey, pendingBalanceHiCiphertext))

}
Loading
Loading