Skip to content

Commit

Permalink
update with queries
Browse files Browse the repository at this point in the history
  • Loading branch information
mj850 committed Jan 13, 2025
1 parent 66c6f4d commit f153b83
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 3 deletions.
2 changes: 2 additions & 0 deletions precompiles/confidentialtransfers/CT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface ICT {
bytes toAmountLo,
bytes toAmountHi,
bytes remainingBalance,
string decryptableBalance,
bytes proofs
) external returns (bool success);

Expand All @@ -40,6 +41,7 @@ interface ICT {
bytes toAmountLo,
bytes toAmountHi,
bytes remainingBalance,
string decryptableBalance,
bytes proofs,
Auditor[] auditors
) external returns (bool success);
Expand Down
2 changes: 1 addition & 1 deletion precompiles/confidentialtransfers/abi.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"inputs":[{"internalType":"string","name":"fromAddress","type":"string"},{"internalType":"string","name":"denom","type":"string"},{"internalType":"bytes","name":"publicKey","type":"bytes"},{"internalType":"string","name":"decryptableBalance","type":"string"},{"internalType":"bytes","name":"pendingBalanceLo","type":"bytes"},{"internalType":"bytes","name":"pendingBalanceHi","type":"bytes"},{"internalType":"bytes","name":"availableBalance","type":"bytes"},{"internalType":"bytes","name":"proofs","type":"bytes"}],"name":"initializeAccount","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"fromAddress","type":"string"},{"internalType":"string","name":"toAddress","type":"string"},{"internalType":"string","name":"denom","type":"string"},{"internalType":"bytes","name":"fromAmountLo","type":"bytes"},{"internalType":"bytes","name":"fromAmountHi","type":"bytes"},{"internalType":"bytes","name":"toAmountLo","type":"bytes"},{"internalType":"bytes","name":"toAmountHi","type":"bytes"},{"internalType":"bytes","name":"remainingBalance","type":"bytes"},{"internalType":"bytes","name":"proofs","type":"bytes"}],"name":"transfer","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"fromAddress","type":"string"},{"internalType":"string","name":"toAddress","type":"string"},{"internalType":"string","name":"denom","type":"string"},{"internalType":"bytes","name":"fromAmountLo","type":"bytes"},{"internalType":"bytes","name":"fromAmountHi","type":"bytes"},{"internalType":"bytes","name":"toAmountLo","type":"bytes"},{"internalType":"bytes","name":"toAmountHi","type":"bytes"},{"internalType":"bytes","name":"remainingBalance","type":"bytes"},{"internalType":"bytes","name":"proofs","type":"bytes"},{"components":[{"internalType":"string","name":"auditorAddress","type":"string"},{"internalType":"bytes","name":"encryptedTransferAmountLo","type":"bytes"},{"internalType":"bytes","name":"encryptedTransferAmountHi","type":"bytes"},{"internalType":"bytes","name":"transferAmountLoValidityProof","type":"bytes"},{"internalType":"bytes","name":"transferAmountHiValidityProof","type":"bytes"},{"internalType":"bytes","name":"transferAmountLoEqualityProof","type":"bytes"},{"internalType":"bytes","name":"transferAmountHiEqualityProof","type":"bytes"}],"internalType":"struct ICT.Auditor[]","name":"auditors","type":"tuple[]"}],"name":"transferWithAuditors","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"fromAddress","type":"string"},{"internalType":"string","name":"denom","type":"string"},{"internalType":"uint64","name":"amount","type":"uint64"}],"name":"deposit","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"fromAddress","type":"string"},{"internalType":"string","name":"denom","type":"string"},{"internalType":"string","name":"decryptableBalance","type":"string"},{"internalType":"uint32","name":"pendingBalanceCreditCounter","type":"uint32"},{"internalType":"bytes","name":"availableBalance","type":"bytes"}],"name":"applyPendingBalance","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"fromAddress","type":"string"},{"internalType":"string","name":"denom","type":"string"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"string","name":"decryptableBalance","type":"string"},{"internalType":"bytes","name":"remainingBalanceCommitment","type":"bytes"},{"internalType":"bytes","name":"proofs","type":"bytes"}],"name":"withdraw","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]
[{"inputs":[{"internalType":"string","name":"fromAddress","type":"string"},{"internalType":"string","name":"denom","type":"string"},{"internalType":"bytes","name":"publicKey","type":"bytes"},{"internalType":"string","name":"decryptableBalance","type":"string"},{"internalType":"bytes","name":"pendingBalanceLo","type":"bytes"},{"internalType":"bytes","name":"pendingBalanceHi","type":"bytes"},{"internalType":"bytes","name":"availableBalance","type":"bytes"},{"internalType":"bytes","name":"proofs","type":"bytes"}],"name":"initializeAccount","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"fromAddress","type":"string"},{"internalType":"string","name":"toAddress","type":"string"},{"internalType":"string","name":"denom","type":"string"},{"internalType":"bytes","name":"fromAmountLo","type":"bytes"},{"internalType":"bytes","name":"fromAmountHi","type":"bytes"},{"internalType":"bytes","name":"toAmountLo","type":"bytes"},{"internalType":"bytes","name":"toAmountHi","type":"bytes"},{"internalType":"bytes","name":"remainingBalance","type":"bytes"},{"internalType":"string","name":"decryptableBalance","type":"string"},{"internalType":"bytes","name":"proofs","type":"bytes"}],"name":"transfer","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"fromAddress","type":"string"},{"internalType":"string","name":"toAddress","type":"string"},{"internalType":"string","name":"denom","type":"string"},{"internalType":"bytes","name":"fromAmountLo","type":"bytes"},{"internalType":"bytes","name":"fromAmountHi","type":"bytes"},{"internalType":"bytes","name":"toAmountLo","type":"bytes"},{"internalType":"bytes","name":"toAmountHi","type":"bytes"},{"internalType":"bytes","name":"remainingBalance","type":"bytes"},{"internalType":"string","name":"decryptableBalance","type":"string"},{"internalType":"bytes","name":"proofs","type":"bytes"},{"components":[{"internalType":"string","name":"auditorAddress","type":"string"},{"internalType":"bytes","name":"encryptedTransferAmountLo","type":"bytes"},{"internalType":"bytes","name":"encryptedTransferAmountHi","type":"bytes"},{"internalType":"bytes","name":"transferAmountLoValidityProof","type":"bytes"},{"internalType":"bytes","name":"transferAmountHiValidityProof","type":"bytes"},{"internalType":"bytes","name":"transferAmountLoEqualityProof","type":"bytes"},{"internalType":"bytes","name":"transferAmountHiEqualityProof","type":"bytes"}],"internalType":"struct ICT.Auditor[]","name":"auditors","type":"tuple[]"}],"name":"transferWithAuditors","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"fromAddress","type":"string"},{"internalType":"string","name":"denom","type":"string"},{"internalType":"uint64","name":"amount","type":"uint64"}],"name":"deposit","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"fromAddress","type":"string"},{"internalType":"string","name":"denom","type":"string"},{"internalType":"string","name":"decryptableBalance","type":"string"},{"internalType":"uint32","name":"pendingBalanceCreditCounter","type":"uint32"},{"internalType":"bytes","name":"availableBalance","type":"bytes"}],"name":"applyPendingBalance","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"fromAddress","type":"string"},{"internalType":"string","name":"denom","type":"string"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"string","name":"decryptableBalance","type":"string"},{"internalType":"bytes","name":"remainingBalanceCommitment","type":"bytes"},{"internalType":"bytes","name":"proofs","type":"bytes"}],"name":"withdraw","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]
205 changes: 203 additions & 2 deletions x/evm/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func GetQueryCmd(_ string) *cobra.Command {
cmd.AddCommand(CmdQueryPointee())
cmd.AddCommand(GetCmdQueryCtTransferPayload())
cmd.AddCommand(GetCmdQueryCtInitAccountPayload())
cmd.AddCommand(GetCmdQueryCtApplyPendingBalancePayload())
cmd.AddCommand(GetCmdQueryCtWithdrawPayload())

return cmd
}
Expand Down Expand Up @@ -734,8 +736,8 @@ func queryCtInitAccountPayload(cmd *cobra.Command, args []string) error {

bz, err := newAbi.Pack(
confidentialtransfers.InitializeAccountMethod,
seiAddress,
denom,
initAccountProto.FromAddress,
initAccountProto.Denom,
initAccountProto.PublicKey,
initAccountProto.DecryptableBalance,
pendingBalanceLo,
Expand All @@ -749,6 +751,205 @@ func queryCtInitAccountPayload(cmd *cobra.Command, args []string) error {
return queryClientCtx.PrintString(hex.EncodeToString(bz))
}

func GetCmdQueryCtApplyPendingBalancePayload() *cobra.Command {
cmd := &cobra.Command{
Use: "ct-apply-pending-balance-payload [abi-filepath] [from_address] [denom]",
Short: "get hex payload for the confidential transfers apply pending balance method",
Args: cobra.ExactArgs(3),
RunE: queryCtApplyPendingBalancePayload,
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func queryCtApplyPendingBalancePayload(cmd *cobra.Command, args []string) error {
queryClientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(queryClientCtx)

dat, err := os.ReadFile(args[0])
if err != nil {
return err
}

newAbi, err := abi.JSON(bytes.NewReader(dat))
if err != nil {
return err
}

fromAddress := args[1]
if fromAddress == "" {
return errors.New("from address cannot be empty")
}

seiAddress, err := getSeiAddress(queryClient, fromAddress)
if err != nil {
return err
}

denom := args[2]
if denom == "" {
return errors.New("denom cannot be empty")
}

_, name, _, err := client.GetFromFields(queryClientCtx, queryClientCtx.Keyring, seiAddress)
if err != nil {
return err
}

privKey, err := getPrivateKeyForName(cmd, name)
if err != nil {
return err
}

ctQueryClient := cttypes.NewQueryClient(queryClientCtx)
fromAccount, err := ctcliutils.GetAccount(ctQueryClient, seiAddress, denom)
if err != nil {
return err
}

applyPendingBalance, err := cttypes.NewApplyPendingBalance(
*privKey,
seiAddress,
denom,
fromAccount.DecryptableAvailableBalance,
fromAccount.PendingBalanceCreditCounter,
fromAccount.AvailableBalance,
fromAccount.PendingBalanceLo,
fromAccount.PendingBalanceHi)
if err != nil {
return err
}

applyPendingBalanceProto := cttypes.NewMsgApplyPendingBalanceProto(applyPendingBalance)

if err = applyPendingBalanceProto.ValidateBasic(); err != nil {
return err
}

availableBalance, err := applyPendingBalanceProto.CurrentAvailableBalance.Marshal()
if err != nil {
return err
}

bz, err := newAbi.Pack(
confidentialtransfers.ApplyPendingBalanceMethod,
applyPendingBalanceProto.Address,
applyPendingBalanceProto.Denom,
applyPendingBalanceProto.NewDecryptableAvailableBalance,
applyPendingBalanceProto.CurrentPendingBalanceCounter,
availableBalance)

if err != nil {
return err
}
return queryClientCtx.PrintString(hex.EncodeToString(bz))
}

func GetCmdQueryCtWithdrawPayload() *cobra.Command {
cmd := &cobra.Command{
Use: "ct-withdraw-payload [abi-filepath] [from_address] [amount]",
Short: "get hex payload for the confidential transfers withdraw method",
Args: cobra.ExactArgs(3),
RunE: queryCtWithdrawPayload,
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func queryCtWithdrawPayload(cmd *cobra.Command, args []string) error {
queryClientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(queryClientCtx)

dat, err := os.ReadFile(args[0])
if err != nil {
return err
}

newAbi, err := abi.JSON(bytes.NewReader(dat))
if err != nil {
return err
}

fromAddress := args[1]
if fromAddress == "" {
return errors.New("from address cannot be empty")
}

seiAddress, err := getSeiAddress(queryClient, fromAddress)
if err != nil {
return err
}

coin, err := sdk.ParseCoinNormalized(args[2])
if err != nil {
return err
}

_, name, _, err := client.GetFromFields(queryClientCtx, queryClientCtx.Keyring, seiAddress)
if err != nil {
return err
}

privKey, err := getPrivateKeyForName(cmd, name)
if err != nil {
return err
}

ctQueryClient := cttypes.NewQueryClient(queryClientCtx)
fromAccount, err := ctcliutils.GetAccount(ctQueryClient, seiAddress, coin.Denom)
if err != nil {
return err
}

withdraw, err := cttypes.NewWithdraw(
*privKey,
fromAccount.AvailableBalance,
coin.Denom,
seiAddress,
fromAccount.DecryptableAvailableBalance,
coin.Amount.BigInt())

if err != nil {
return err
}

withdrawProto := cttypes.NewMsgWithdrawProto(withdraw)

if err = withdrawProto.ValidateBasic(); err != nil {
return err
}

remainingBalanceCommitment, err := withdrawProto.RemainingBalanceCommitment.Marshal()

Check failure on line 932 in x/evm/client/cli/query.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

This definition of err is never used.
proofs, err := withdrawProto.Proofs.Marshal()
if err != nil {
return err
}

bz, err := newAbi.Pack(
confidentialtransfers.WithdrawMethod,
withdrawProto.FromAddress,
withdrawProto.Denom,
withdrawProto.Amount,
withdrawProto.DecryptableBalance,
remainingBalanceCommitment,
proofs)

if err != nil {
return err
}
return queryClientCtx.PrintString(hex.EncodeToString(bz))
}

func getSeiAddress(queryClient types.QueryClient, address string) (string, error) {
if common.IsHexAddress(address) {
evmAddr := common.HexToAddress(address)
Expand Down

0 comments on commit f153b83

Please sign in to comment.