5
5
sdk "github.com/cosmos/cosmos-sdk/types"
6
6
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
7
7
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"
8
+ "github.com/sei-protocol/sei-chain/x/confidentialtransfers/utils"
8
9
"github.com/sei-protocol/sei-cryptography/pkg/encryption/elgamal"
9
10
"github.com/sei-protocol/sei-cryptography/pkg/zkproofs"
10
11
"math"
@@ -25,21 +26,16 @@ var _ types.MsgServer = msgServer{}
25
26
func (m msgServer ) InitializeAccount (goCtx context.Context , req * types.MsgInitializeAccount ) (* types.MsgInitializeAccountResponse , error ) {
26
27
ctx := sdk .UnwrapSDKContext (goCtx )
27
28
28
- // Convert the instruction to proto. This also validates the request.
29
+ // Convert the instruction from proto. This also validates the request.
29
30
instruction , err := req .FromProto ()
30
31
if err != nil {
31
32
return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "invalid msg" )
32
33
}
33
34
34
35
// Check if the account already exists
35
- address , err := sdk .AccAddressFromBech32 (instruction .FromAddress )
36
- if err != nil {
37
- return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "invalid address" )
38
- }
39
-
40
- _ , exists := m .Keeper .GetAccount (ctx , address , instruction .Denom )
36
+ _ , exists := m .Keeper .GetAccount (ctx , req .FromAddress , instruction .Denom )
41
37
if exists {
42
- return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "account already exists" )
38
+ return nil , sdkerrors .Wrap (sdkerrors .ErrNotFound , "account already exists" )
43
39
}
44
40
45
41
// Validate the public key
@@ -77,7 +73,10 @@ func (m msgServer) InitializeAccount(goCtx context.Context, req *types.MsgInitia
77
73
}
78
74
79
75
// Store the account
80
- m .Keeper .SetAccount (ctx , address , req .Denom , account )
76
+ err = m .Keeper .SetAccount (ctx , req .FromAddress , req .Denom , account )
77
+ if err != nil {
78
+ return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "error setting account" )
79
+ }
81
80
82
81
ctx .EventManager ().EmitEvents (sdk.Events {
83
82
sdk .NewEvent (
@@ -105,9 +104,9 @@ func (m msgServer) Deposit(goCtx context.Context, req *types.MsgDeposit) (*types
105
104
return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "invalid address" )
106
105
}
107
106
108
- account , exists := m .Keeper .GetAccount (ctx , address , req .Denom )
107
+ account , exists := m .Keeper .GetAccount (ctx , req . FromAddress , req .Denom )
109
108
if ! exists {
110
- return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "account does not exist" )
109
+ return nil , sdkerrors .Wrap (sdkerrors .ErrNotFound , "account does not exist" )
111
110
}
112
111
113
112
// The maximum transfer amount is 2^48
@@ -131,10 +130,10 @@ func (m msgServer) Deposit(goCtx context.Context, req *types.MsgDeposit) (*types
131
130
132
131
// Split the deposit amount into lo and hi bits.
133
132
// Extract the bottom 16 bits (rightmost 16 bits)
134
- bottom16 := uint16 (req .Amount & 0xFFFF )
135
-
136
- // Extract the next 32 bits (from bit 16 to bit 47) (Everything else is ignored since the max is 48 bits )
137
- next32 := uint32 (( req . Amount >> 16 ) & 0xFFFFFFFF )
133
+ bottom16 , next32 , err := utils . SplitTransferBalance (req .Amount )
134
+ if err != nil {
135
+ return nil , sdkerrors . Wrap ( sdkerrors . ErrInvalidRequest , "error splitting transfer balance" )
136
+ }
138
137
139
138
// Compute the new balances
140
139
teg := elgamal .NewTwistedElgamal ()
@@ -154,7 +153,10 @@ func (m msgServer) Deposit(goCtx context.Context, req *types.MsgDeposit) (*types
154
153
account .PendingBalanceCreditCounter += 1
155
154
156
155
// Save the changes to the account state
157
- m .Keeper .SetAccount (ctx , address , req .Denom , account )
156
+ err = m .Keeper .SetAccount (ctx , req .FromAddress , req .Denom , account )
157
+ if err != nil {
158
+ return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "error setting account" )
159
+ }
158
160
159
161
// Emit any required events
160
162
ctx .EventManager ().EmitEvents (sdk.Events {
@@ -179,9 +181,9 @@ func (m msgServer) Withdraw(goCtx context.Context, req *types.MsgWithdraw) (*typ
179
181
}
180
182
181
183
// Get the user's account
182
- account , exists := m .Keeper .GetAccount (ctx , address , req .Denom )
184
+ account , exists := m .Keeper .GetAccount (ctx , req . FromAddress , req .Denom )
183
185
if ! exists {
184
- return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "account does not exist" )
186
+ return nil , sdkerrors .Wrap (sdkerrors .ErrNotFound , "account does not exist" )
185
187
}
186
188
187
189
// Convert the struct to a usable form. This also validates the request.
@@ -191,7 +193,8 @@ func (m msgServer) Withdraw(goCtx context.Context, req *types.MsgWithdraw) (*typ
191
193
}
192
194
193
195
// Verify that the account has sufficient funds (Remaining balance after making the transfer is greater than or equal to zero.)
194
- // This range proof verification is performed on the RemainingBalanceCommitment sent by the user. An additional check is required to ensure that this matches the remaining balance calculated by the server.
196
+ // This range proof verification is performed on the RemainingBalanceCommitment sent by the user.
197
+ // An additional check is required to ensure that this matches the remaining balance calculated by the server.
195
198
verified , _ := zkproofs .VerifyRangeProof (instruction .Proofs .RemainingBalanceRangeProof , instruction .RemainingBalanceCommitment , 64 )
196
199
if ! verified {
197
200
return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "range proof verification failed" )
@@ -204,7 +207,10 @@ func (m msgServer) Withdraw(goCtx context.Context, req *types.MsgWithdraw) (*typ
204
207
return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "error subtracting amount" )
205
208
}
206
209
207
- verified = zkproofs .VerifyCiphertextCommitmentEquality (instruction .Proofs .RemainingBalanceEqualityProof , & account .PublicKey , remainingBalanceCalculated , & instruction .RemainingBalanceCommitment .C )
210
+ verified = zkproofs .VerifyCiphertextCommitmentEquality (
211
+ instruction .Proofs .RemainingBalanceEqualityProof ,
212
+ & account .PublicKey , remainingBalanceCalculated ,
213
+ & instruction .RemainingBalanceCommitment .C )
208
214
if ! verified {
209
215
return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "ciphertext commitment equality verification failed" )
210
216
}
@@ -214,7 +220,10 @@ func (m msgServer) Withdraw(goCtx context.Context, req *types.MsgWithdraw) (*typ
214
220
account .AvailableBalance = remainingBalanceCalculated
215
221
216
222
// Save the account state
217
- m .Keeper .SetAccount (ctx , address , req .Denom , account )
223
+ err = m .Keeper .SetAccount (ctx , req .FromAddress , req .Denom , account )
224
+ if err != nil {
225
+ return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "error setting account" )
226
+ }
218
227
219
228
// Return the tokens to the sender
220
229
coins := sdk .NewCoins (sdk .NewCoin (instruction .Denom , sdk .NewIntFromUint64 (instruction .Amount )))
@@ -239,14 +248,9 @@ func (m msgServer) ApplyPendingBalance(goCtx context.Context, req *types.MsgAppl
239
248
ctx := sdk .UnwrapSDKContext (goCtx )
240
249
241
250
// Check if the account exists
242
- address , err := sdk .AccAddressFromBech32 (req .Address )
243
- if err != nil {
244
- return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "invalid address" )
245
- }
246
-
247
- account , exists := m .Keeper .GetAccount (ctx , address , req .Denom )
251
+ account , exists := m .Keeper .GetAccount (ctx , req .Address , req .Denom )
248
252
if ! exists {
249
- return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "account does not exist" )
253
+ return nil , sdkerrors .Wrap (sdkerrors .ErrNotFound , "account does not exist" )
250
254
}
251
255
252
256
if account .PendingBalanceCreditCounter == 0 {
@@ -277,7 +281,7 @@ func (m msgServer) ApplyPendingBalance(goCtx context.Context, req *types.MsgAppl
277
281
account .PendingBalanceCreditCounter = 0
278
282
279
283
// Save the changes to the account state
280
- m .Keeper .SetAccount (ctx , address , req .Denom , account )
284
+ m .Keeper .SetAccount (ctx , req . Address , req .Denom , account )
281
285
282
286
// Emit any required events
283
287
ctx .EventManager ().EmitEvents (sdk.Events {
@@ -295,14 +299,9 @@ func (m msgServer) CloseAccount(goCtx context.Context, req *types.MsgCloseAccoun
295
299
ctx := sdk .UnwrapSDKContext (goCtx )
296
300
297
301
// Check if the account exists
298
- address , err := sdk .AccAddressFromBech32 (req .Address )
299
- if err != nil {
300
- return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "invalid address" )
301
- }
302
-
303
- account , exists := m .Keeper .GetAccount (ctx , address , req .Denom )
302
+ account , exists := m .Keeper .GetAccount (ctx , req .Address , req .Denom )
304
303
if ! exists {
305
- return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "account does not exist" )
304
+ return nil , sdkerrors .Wrap (sdkerrors .ErrNotFound , "account does not exist" )
306
305
}
307
306
308
307
instruction , err := req .FromProto ()
@@ -329,7 +328,10 @@ func (m msgServer) CloseAccount(goCtx context.Context, req *types.MsgCloseAccoun
329
328
}
330
329
331
330
// Delete the account
332
- m .Keeper .DeleteAccount (ctx , address , req .Denom )
331
+ err = m .Keeper .DeleteAccount (ctx , req .Address , req .Denom )
332
+ if err != nil {
333
+ return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "error deleting account" )
334
+ }
333
335
334
336
// Emit any required events
335
337
ctx .EventManager ().EmitEvents (sdk.Events {
@@ -352,24 +354,14 @@ func (m msgServer) Transfer(goCtx context.Context, req *types.MsgTransfer) (*typ
352
354
}
353
355
354
356
// Check that sender and recipient accounts exist.
355
- senderAddress , err := sdk .AccAddressFromBech32 (req .FromAddress )
356
- if err != nil {
357
- return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "invalid sender address" )
358
- }
359
-
360
- recipientAddress , err := sdk .AccAddressFromBech32 (req .ToAddress )
361
- if err != nil {
362
- return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "invalid recipient address" )
363
- }
364
-
365
- senderAccount , exists := m .Keeper .GetAccount (ctx , senderAddress , req .Denom )
357
+ senderAccount , exists := m .Keeper .GetAccount (ctx , req .FromAddress , req .Denom )
366
358
if ! exists {
367
- return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "sender account does not exist" )
359
+ return nil , sdkerrors .Wrap (sdkerrors .ErrNotFound , "sender account does not exist" )
368
360
}
369
361
370
- recipientAccount , exists := m .Keeper .GetAccount (ctx , recipientAddress , req .Denom )
362
+ recipientAccount , exists := m .Keeper .GetAccount (ctx , req . ToAddress , req .Denom )
371
363
if ! exists {
372
- return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "recipient account does not exist" )
364
+ return nil , sdkerrors .Wrap (sdkerrors .ErrNotFound , "recipient account does not exist" )
373
365
}
374
366
375
367
// Check that account does not have the maximum limit of pending transactions.
@@ -392,14 +384,10 @@ func (m msgServer) Transfer(goCtx context.Context, req *types.MsgTransfer) (*typ
392
384
393
385
// Validate proofs for each auditor
394
386
for _ , auditorParams := range instruction .Auditors {
395
- auditorAddress , err := sdk .AccAddressFromBech32 (auditorParams .Address )
396
- if err != nil {
397
- return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "invalid auditor address" )
398
- }
399
387
400
- auditorAccount , exists := m .Keeper .GetAccount (ctx , auditorAddress , instruction .Denom )
388
+ auditorAccount , exists := m .Keeper .GetAccount (ctx , auditorParams . Address , instruction .Denom )
401
389
if ! exists {
402
- return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "auditor account does not exist" )
390
+ return nil , sdkerrors .Wrap (sdkerrors .ErrNotFound , "auditor account does not exist" )
403
391
}
404
392
405
393
err = types .VerifyAuditorProof (
@@ -434,8 +422,15 @@ func (m msgServer) Transfer(goCtx context.Context, req *types.MsgTransfer) (*typ
434
422
senderAccount .AvailableBalance = newSenderBalanceCiphertext
435
423
436
424
// Save the account states
437
- m .Keeper .SetAccount (ctx , senderAddress , req .Denom , senderAccount )
438
- m .Keeper .SetAccount (ctx , recipientAddress , req .Denom , recipientAccount )
425
+ err = m .Keeper .SetAccount (ctx , req .FromAddress , req .Denom , senderAccount )
426
+ if err != nil {
427
+ return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "error setting sender account" )
428
+ }
429
+
430
+ err = m .Keeper .SetAccount (ctx , req .ToAddress , req .Denom , recipientAccount )
431
+ if err != nil {
432
+ return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "error setting recipient account" )
433
+ }
439
434
440
435
// Emit any required events
441
436
ctx .EventManager ().EmitEvents (sdk.Events {
0 commit comments