From 1c4a27bfaabf1bf942adc44bfbcd48ad5755209b Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Wed, 30 Sep 2020 19:08:27 +0300 Subject: [PATCH 01/10] replaced `git branch --show-current` with `git rev-parse --abbrev-ref HEAD` --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 51378ae8c..3c1f60aa0 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ PACKAGES=$(shell go list ./... | grep -v '/simulation') VERSION ?= $(shell echo $(shell git describe --tags) | sed 's/^v//') COMMIT := $(shell git log -1 --format='%H') -CURRENT_BRANCH := $(shell git branch --show-current) +CURRENT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD) LEDGER_ENABLED ?= true BINDIR ?= $(GOPATH)/bin BUILD_PROFILE ?= release @@ -339,7 +339,7 @@ build-all-test-contracts: build-test-contract bin-data: bin-data-sw bin-data-develop bin-data-production bin-data-sw: - cd ./cmd/secretd && go-bindata -o ias_bin_sw.go -prefix "../../ias_keys/sw_dummy/" -tags "!hw" ../../ias_keys/sw_dummy/... + cd ./cmd/secretd && pwd && go-bindata -o ias_bin_sw.go -prefix "../../ias_keys/sw_dummy/" -tags "!hw" ../../ias_keys/sw_dummy/... bin-data-develop: cd ./cmd/secretd && go-bindata -o ias_bin_dev.go -prefix "../../ias_keys/develop/" -tags "develop,hw" ../../ias_keys/develop/... From f8b38484e3fd726f4ca33a305ecbbc433fcedf6e Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Wed, 30 Sep 2020 19:09:03 +0300 Subject: [PATCH 02/10] added copying of sw keys to base.Dockerfile --- deployment/dockerfiles/base.Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/deployment/dockerfiles/base.Dockerfile b/deployment/dockerfiles/base.Dockerfile index a6b13b8bf..573068127 100644 --- a/deployment/dockerfiles/base.Dockerfile +++ b/deployment/dockerfiles/base.Dockerfile @@ -44,6 +44,8 @@ COPY api_key.txt /go/src/github.com/enigmampc/SecretNetwork/ias_keys/develop/ COPY spid.txt /go/src/github.com/enigmampc/SecretNetwork/ias_keys/develop/ COPY api_key.txt /go/src/github.com/enigmampc/SecretNetwork/ias_keys/production/ COPY spid.txt /go/src/github.com/enigmampc/SecretNetwork/ias_keys/production/ +COPY api_key.txt /go/src/github.com/enigmampc/SecretNetwork/ias_keys/sw_dummy/ +COPY spid.txt /go/src/github.com/enigmampc/SecretNetwork/ias_keys/sw_dummy/ RUN . /opt/sgxsdk/environment && env && MITIGATION_CVE_2020_0551=LOAD VERSION=${VERSION} FEATURES=${FEATURES} SGX_MODE=${SGX_MODE} make build-rust From 1e5b8dcdf89638d9e54261375f5180c5356fea9f Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Wed, 30 Sep 2020 19:19:04 +0300 Subject: [PATCH 03/10] removed debug `pwd` --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3c1f60aa0..37f2bcd48 100644 --- a/Makefile +++ b/Makefile @@ -339,7 +339,7 @@ build-all-test-contracts: build-test-contract bin-data: bin-data-sw bin-data-develop bin-data-production bin-data-sw: - cd ./cmd/secretd && pwd && go-bindata -o ias_bin_sw.go -prefix "../../ias_keys/sw_dummy/" -tags "!hw" ../../ias_keys/sw_dummy/... + cd ./cmd/secretd && go-bindata -o ias_bin_sw.go -prefix "../../ias_keys/sw_dummy/" -tags "!hw" ../../ias_keys/sw_dummy/... bin-data-develop: cd ./cmd/secretd && go-bindata -o ias_bin_dev.go -prefix "../../ias_keys/develop/" -tags "develop,hw" ../../ias_keys/develop/... From c2d2b58b256b08fcf86d6bdeb025b87efd02aa99 Mon Sep 17 00:00:00 2001 From: TomL94 Date: Sun, 4 Oct 2020 18:13:21 +0300 Subject: [PATCH 04/10] Safe unmarshal --- x/compute/client/cli/query.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/compute/client/cli/query.go b/x/compute/client/cli/query.go index 32d972a0a..da34239ed 100644 --- a/x/compute/client/cli/query.go +++ b/x/compute/client/cli/query.go @@ -348,7 +348,7 @@ func GetQueryDecryptTxCmd(cdc *amino.Codec) *cobra.Command { } answer.RawInput = string(plaintextInput) - answer.Input = []byte(plaintextInput[64:]) + _ = json.Unmarshal(plaintextInput[64:], answer.Input) // decrypt data if answer.Type == "execute" { From 9b23b81e966403cff9edfa2e079dff8701941f2e Mon Sep 17 00:00:00 2001 From: TomL94 Date: Tue, 13 Oct 2020 13:14:29 +0300 Subject: [PATCH 05/10] Space padding --- cmd/secretcli/secret20.go | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/cmd/secretcli/secret20.go b/cmd/secretcli/secret20.go index 6f428b88c..9753eda89 100644 --- a/cmd/secretcli/secret20.go +++ b/cmd/secretcli/secret20.go @@ -16,15 +16,17 @@ import ( "github.com/spf13/viper" "math/rand" "strconv" + "strings" "time" ) +const MESSAGE_BLOCK_SIZE = 256 const flagAmount = "amount" // GetQueryCmd returns the cli query commands for this module func S20GetQueryCmd(cdc *codec.Codec) *cobra.Command { s20QueryCmd := &cobra.Command{ - Use: "secret20", + Use: "snip20", Short: "*EXPERIMENTAL* Querying commands for the secret20 contracts", DisableFlagParsing: true, SuggestionsMinimumDistance: 2, @@ -42,7 +44,7 @@ func S20GetQueryCmd(cdc *codec.Codec) *cobra.Command { // GetTxCmd returns the transaction commands for this module func S20GetTxCmd(cdc *codec.Codec) *cobra.Command { s20TxCmd := &cobra.Command{ - Use: "secret20", + Use: "snip20", Short: "*EXPERIMENTAL* Secret20 transactions subcommands", DisableFlagParsing: true, SuggestionsMinimumDistance: 2, @@ -50,7 +52,7 @@ func S20GetTxCmd(cdc *codec.Codec) *cobra.Command { } s20TxCmd.AddCommand(flags.PostCommands( - s20SendCmd(cdc), + s20TransferCmd(cdc), s20CreatingViewingKey(cdc), s20DepositCmd(cdc), s20Withdraw(cdc), @@ -140,7 +142,6 @@ key yet, use the "create-viewing-key" command. Otherwise, you can still see your } func addressFromBechOrLabel(addressOrLabel string, cliCtx context.CLIContext) (string, error) { - var contractAddr string _, err := sdk.AccAddressFromBech32(addressOrLabel) @@ -158,7 +159,7 @@ func addressFromBechOrLabel(addressOrLabel string, cliCtx context.CLIContext) (s return contractAddr, nil } -func s20SendCmd(cdc *codec.Codec) *cobra.Command { +func s20TransferCmd(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "send [contract address or label] [to account] [amount]", @@ -191,7 +192,7 @@ func s20SendCmd(cdc *codec.Codec) *cobra.Command { return errors.New("invalid amount format") } - msg := sendCoinMsg(toAddr, amount) + msg := transferCoinMsg(toAddr, amount) return cli.ExecuteWithData(cmd, contractAddr, msg, "", false, "", "", cdc) }, @@ -342,30 +343,40 @@ func s20Withdraw(cdc *codec.Codec) *cobra.Command { return cmd } +func spacePad(blockSize int, message string) string { + surplus := len(message) % blockSize + if surplus == 0 { + return message + } + + missing := blockSize - surplus + return message + strings.Repeat(" ", missing) +} + func transferHistoryMsg(fromAddress sdk.AccAddress, viewingKey string) []byte { - return []byte(fmt.Sprintf("{\"transfers\": {\"address\": \"%s\", \"key\": \"%s\"}}", fromAddress.String(), viewingKey)) + return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"transfers\": {\"address\": \"%s\", \"key\": \"%s\"}}", fromAddress.String(), viewingKey))) } func balanceMsg(fromAddress sdk.AccAddress, viewingKey string) []byte { - return []byte(fmt.Sprintf("{\"balance\": {\"address\": \"%s\", \"key\": \"%s\"}}", fromAddress.String(), viewingKey)) + return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"balance\": {\"address\": \"%s\", \"key\": \"%s\"}}", fromAddress.String(), viewingKey))) } -func sendCoinMsg(toAddress sdk.AccAddress, amount string) []byte { - return []byte(fmt.Sprintf("{\"transfer\": {\"recipient\": \"%s\", \"amount\": \"%s\"}}", toAddress.String(), amount)) +func transferCoinMsg(toAddress sdk.AccAddress, amount string) []byte { + return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"transfer\": {\"recipient\": \"%s\", \"amount\": \"%s\"}}", toAddress.String(), amount))) } func depositMsg() []byte { - return []byte(fmt.Sprintf("{\"deposit\": {}}")) + return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"deposit\": {}}"))) } func withdrawMsg(amount string) []byte { - return []byte(fmt.Sprintf("{\"withdraw\": {\"amount\": \"%s\"}}", amount)) + return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"withdraw\": {\"amount\": \"%s\"}}", amount))) } func createViewingKeyMsg(data string) []byte { - return []byte(fmt.Sprintf("{\"create_viewing_key\": {\"entropy\": \"%s\"}}", data)) + return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"create_viewing_key\": {\"entropy\": \"%s\"}}", data))) } func setViewingKeyMsg(data string) []byte { - return []byte(fmt.Sprintf("{\"set_viewing_key\": {\"key\": \"%s\"}}", data)) + return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"set_viewing_key\": {\"key\": \"%s\"}}", data))) } From b0e5bcb0b20601b4c74200997ab66e722bd32f41 Mon Sep 17 00:00:00 2001 From: TomL94 Date: Tue, 13 Oct 2020 13:19:11 +0300 Subject: [PATCH 06/10] Transfer history semantics and added paging --- cmd/secretcli/secret20.go | 70 +++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/cmd/secretcli/secret20.go b/cmd/secretcli/secret20.go index 9753eda89..56b736ea9 100644 --- a/cmd/secretcli/secret20.go +++ b/cmd/secretcli/secret20.go @@ -55,7 +55,7 @@ func S20GetTxCmd(cdc *codec.Codec) *cobra.Command { s20TransferCmd(cdc), s20CreatingViewingKey(cdc), s20DepositCmd(cdc), - s20Withdraw(cdc), + s20Redeem(cdc), s20SetViewingKey(cdc), )...) @@ -64,10 +64,10 @@ func S20GetTxCmd(cdc *codec.Codec) *cobra.Command { func S20TransferHistoryCmd(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "history [contract address] [account] [viewing_key]", + Use: "history [contract address] [account] [viewing_key] [optional: page, default: 0] [optional: page_size, default: 10]", Short: "*EXPERIMENTAL* View your transaction history", - Long: `Print out all transactions you have been a part of - either as a sender or recipient`, - Args: cobra.ExactArgs(3), + Long: `Print out transactions you have been a part of - either as a sender or recipient`, + Args: cobra.MinimumNArgs(3), RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) @@ -87,7 +87,24 @@ func S20TransferHistoryCmd(cdc *codec.Codec) *cobra.Command { return errors.New("viewing key must not be empty") } - queryData := transferHistoryMsg(addr, key) + var page uint64 = 0 + var pageSize uint64 = 10 + + if len(args) == 4 { + page, err = strconv.ParseUint(args[3], 10, 32) + if err != nil { + return err + } + } + + if len(args) == 5 { + pageSize, err = strconv.ParseUint(args[3], 10, 32) + if err != nil { + return err + } + } + + queryData := queryTransferHistoryMsg(addr, key, uint32(page), uint32(pageSize)) err = cli.QueryWithData(contractAddr, cdc, queryData) if err != nil { @@ -127,7 +144,7 @@ key yet, use the "create-viewing-key" command. Otherwise, you can still see your return errors.New("viewing key must not be empty") } - queryData := balanceMsg(addr, key) + queryData := queryBalanceMsg(addr, key) err = cli.QueryWithData(contractAddr, cdc, queryData) if err != nil { @@ -192,7 +209,7 @@ func s20TransferCmd(cdc *codec.Codec) *cobra.Command { return errors.New("invalid amount format") } - msg := transferCoinMsg(toAddr, amount) + msg := handleTransferMsg(toAddr, amount) return cli.ExecuteWithData(cmd, contractAddr, msg, "", false, "", "", cdc) }, @@ -207,9 +224,7 @@ func s20CreatingViewingKey(cdc *codec.Codec) *cobra.Command { Use: "create-viewing-key [contract address or label]", Short: "*EXPERIMENTAL* Create a new viewing key. To view the resulting key, use 'secretcli q compute tx '", Long: `This allows a user to generate a key that enables off-chain queries. -This way you can perform balance and transaction history queries without waiting for a transaction on-chain. -This transaction will be expensive, so you must have about 3,000,000 gas in your account to perform this step. - This is intended to make queries take a long time to execute to be resistant to brute-force attacks.`, +This way you can perform balance and transaction history queries without waiting for a transaction on-chain.`, Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { @@ -235,7 +250,7 @@ This transaction will be expensive, so you must have about 3,000,000 gas in your randomData := hex.EncodeToString(byteArr)[:64] - msg := createViewingKeyMsg(randomData) + msg := handleCreateViewingKeyMsg(randomData) return cli.ExecuteWithData(cmd, contractAddr, msg, "", false, "", "", cdc) }, @@ -268,7 +283,7 @@ you're doing`, return errors.New("invalid contract address or label") } - msg := setViewingKeyMsg(args[1]) + msg := handleSetViewingKeyMsg(args[1]) return cli.ExecuteWithData(cmd, contractAddr, msg, "", false, "", "", cdc) }, @@ -299,7 +314,7 @@ func s20DepositCmd(cdc *codec.Codec) *cobra.Command { return errors.New("invalid contract address or label") } - msg := depositMsg() + msg := handleDepositMsg() amountStr := viper.GetString(flagAmount) @@ -310,10 +325,10 @@ func s20DepositCmd(cdc *codec.Codec) *cobra.Command { return cmd } -func s20Withdraw(cdc *codec.Codec) *cobra.Command { +func s20Redeem(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "withdraw [contract address or label] [amount]", + Use: "redeem [contract address or label] [amount]", Short: "Convert your secret token back to SCRT", Long: `Convert your secret token back to SCRT. This command will only work if the token supports native currency conversion`, Args: cobra.MinimumNArgs(2), @@ -334,7 +349,7 @@ func s20Withdraw(cdc *codec.Codec) *cobra.Command { amount := args[1] - msg := withdrawMsg(amount) + msg := handleRedeemMsg(amount) return cli.ExecuteWithData(cmd, contractAddr, msg, "", false, "", "", cdc) }, @@ -353,30 +368,35 @@ func spacePad(blockSize int, message string) string { return message + strings.Repeat(" ", missing) } -func transferHistoryMsg(fromAddress sdk.AccAddress, viewingKey string) []byte { - return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"transfers\": {\"address\": \"%s\", \"key\": \"%s\"}}", fromAddress.String(), viewingKey))) +func queryTransferHistoryMsg(fromAddress sdk.AccAddress, viewingKey string, page uint32, pageSize uint32) []byte { + return []byte(spacePad(MESSAGE_BLOCK_SIZE, + fmt.Sprintf("{\"transfer_history\": {\"address\": \"%s\", \"key\": \"%s\", \"page\": %d, \"page_size\": %d}}", + fromAddress.String(), + viewingKey, + page, + pageSize))) } -func balanceMsg(fromAddress sdk.AccAddress, viewingKey string) []byte { +func queryBalanceMsg(fromAddress sdk.AccAddress, viewingKey string) []byte { return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"balance\": {\"address\": \"%s\", \"key\": \"%s\"}}", fromAddress.String(), viewingKey))) } -func transferCoinMsg(toAddress sdk.AccAddress, amount string) []byte { +func handleTransferMsg(toAddress sdk.AccAddress, amount string) []byte { return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"transfer\": {\"recipient\": \"%s\", \"amount\": \"%s\"}}", toAddress.String(), amount))) } -func depositMsg() []byte { +func handleDepositMsg() []byte { return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"deposit\": {}}"))) } -func withdrawMsg(amount string) []byte { - return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"withdraw\": {\"amount\": \"%s\"}}", amount))) +func handleRedeemMsg(amount string) []byte { + return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"redeem\": {\"amount\": \"%s\"}}", amount))) } -func createViewingKeyMsg(data string) []byte { +func handleCreateViewingKeyMsg(data string) []byte { return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"create_viewing_key\": {\"entropy\": \"%s\"}}", data))) } -func setViewingKeyMsg(data string) []byte { +func handleSetViewingKeyMsg(data string) []byte { return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"set_viewing_key\": {\"key\": \"%s\"}}", data))) } From 21f7a237ac17c71b48620ffae98614a861cd33ab Mon Sep 17 00:00:00 2001 From: TomL94 Date: Tue, 13 Oct 2020 17:27:03 +0300 Subject: [PATCH 07/10] Send and Burn --- cmd/secretcli/secret20.go | 130 +++++++++++++++++++++++++++++++++----- 1 file changed, 115 insertions(+), 15 deletions(-) diff --git a/cmd/secretcli/secret20.go b/cmd/secretcli/secret20.go index 56b736ea9..eec597661 100644 --- a/cmd/secretcli/secret20.go +++ b/cmd/secretcli/secret20.go @@ -53,10 +53,12 @@ func S20GetTxCmd(cdc *codec.Codec) *cobra.Command { s20TxCmd.AddCommand(flags.PostCommands( s20TransferCmd(cdc), + s20SendCmd(cdc), s20CreatingViewingKey(cdc), s20DepositCmd(cdc), s20Redeem(cdc), s20SetViewingKey(cdc), + s20BurnCmd(cdc), )...) return s20TxCmd @@ -177,12 +179,11 @@ func addressFromBechOrLabel(addressOrLabel string, cliCtx context.CLIContext) (s } func s20TransferCmd(cdc *codec.Codec) *cobra.Command { - cmd := &cobra.Command{ - Use: "send [contract address or label] [to account] [amount]", - Short: "*EXPERIMENTAL* send tokens to another address", - Long: `send tokens to another address`, - Args: cobra.MinimumNArgs(3), + Use: "transfer [contract address or label] [to account] [amount]", + Short: "*EXPERIMENTAL* Transfer tokens to another address", + Long: `transfer tokens to another address`, + Args: cobra.ExactArgs(3), RunE: func(cmd *cobra.Command, args []string) error { inBuf := bufio.NewReader(cmd.InOrStdin()) @@ -219,13 +220,12 @@ func s20TransferCmd(cdc *codec.Codec) *cobra.Command { } func s20CreatingViewingKey(cdc *codec.Codec) *cobra.Command { - cmd := &cobra.Command{ Use: "create-viewing-key [contract address or label]", Short: "*EXPERIMENTAL* Create a new viewing key. To view the resulting key, use 'secretcli q compute tx '", Long: `This allows a user to generate a key that enables off-chain queries. This way you can perform balance and transaction history queries without waiting for a transaction on-chain.`, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { inBuf := bufio.NewReader(cmd.InOrStdin()) @@ -260,14 +260,13 @@ This way you can perform balance and transaction history queries without waiting } func s20SetViewingKey(cdc *codec.Codec) *cobra.Command { - cmd := &cobra.Command{ Use: "set-viewing-key [contract address or label] [viewing-key]", - Short: "*EXPERIMENTAL* sets the viewing key for your account", + Short: "*EXPERIMENTAL* Sets the viewing key for your account", Long: `This command is useful if you want to manage multiple secret tokens with the same viewing key. *WARNING*: This should only be used to duplicate keys created with the create-viewing-key command, or if you really really know what you're doing`, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { inBuf := bufio.NewReader(cmd.InOrStdin()) @@ -293,12 +292,11 @@ you're doing`, } func s20DepositCmd(cdc *codec.Codec) *cobra.Command { - cmd := &cobra.Command{ Use: "deposit [contract address or label]", - Short: "Convert your SCRT into a secret token", + Short: "*EXPERIMENTAL* convert your SCRT into a secret token", Long: `Convert your SCRT into a secret token. This command will only work if the token supports native currency conversion`, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { inBuf := bufio.NewReader(cmd.InOrStdin()) @@ -329,9 +327,9 @@ func s20Redeem(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "redeem [contract address or label] [amount]", - Short: "Convert your secret token back to SCRT", + Short: "*EXPERIMENTAL* convert your secret token back to SCRT", Long: `Convert your secret token back to SCRT. This command will only work if the token supports native currency conversion`, - Args: cobra.MinimumNArgs(2), + Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { inBuf := bufio.NewReader(cmd.InOrStdin()) @@ -358,6 +356,92 @@ func s20Redeem(cdc *codec.Codec) *cobra.Command { return cmd } +func s20SendCmd(cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "send [contract_address or label] [to_account] [amount] [optional: callback_message]", + Short: "*EXPERIMENTAL* send tokens to another address. Optionally add a callback message", + Long: `Send tokens to another address (contract or not). If 'to_account' is a contract, you can optionally add a callback message to this contract. +If no callback provided, this is identical to 'transfer'. +WARNING! The tokens will be transferred even if the callback message fails!`, + Args: cobra.MinimumNArgs(3), + RunE: func(cmd *cobra.Command, args []string) error { + + inBuf := bufio.NewReader(cmd.InOrStdin()) + cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) + + contractAddrStr, err := addressFromBechOrLabel(args[0], cliCtx) + if err != nil { + return err + } + + contractAddr, err := sdk.AccAddressFromBech32(contractAddrStr) + if err != nil { + return errors.New("invalid contract address or label") + } + + toAddr, err := sdk.AccAddressFromBech32(args[1]) + if err != nil { + return errors.New("invalid recipient address") + } + + amount := args[2] + _, err = strconv.ParseUint(amount, 10, 64) + if err != nil { + return errors.New("invalid amount format") + } + + var msg []byte + if len(args) > 3 { + callback := args[3] + msg = handleSendWithCallbackMsg(toAddr, amount, callback) + } else { + msg = handleSendMsg(toAddr, amount) + } + + return cli.ExecuteWithData(cmd, contractAddr, msg, "", false, "", "", cdc) + }, + } + + return cmd +} + +func s20BurnCmd(cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "burn [contract_address or label] [amount]", + Short: "*EXPERIMENTAL* burn tokens forever", + Long: `Burn tokens. The tokens will be removed from your account and will be lost forever. +WARNING! This action is irreversible and permanent! use at your own risk`, + Args: cobra.MinimumNArgs(3), + RunE: func(cmd *cobra.Command, args []string) error { + + inBuf := bufio.NewReader(cmd.InOrStdin()) + cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) + + contractAddrStr, err := addressFromBechOrLabel(args[0], cliCtx) + if err != nil { + return err + } + + contractAddr, err := sdk.AccAddressFromBech32(contractAddrStr) + if err != nil { + return errors.New("invalid contract address or label") + } + + amount := args[1] + _, err = strconv.ParseUint(amount, 10, 64) + if err != nil { + return errors.New("invalid amount format") + } + + msg := handleBurnMsg(contractAddr, amount) + + return cli.ExecuteWithData(cmd, contractAddr, msg, "", false, "", "", cdc) + }, + } + + return cmd +} + func spacePad(blockSize int, message string) string { surplus := len(message) % blockSize if surplus == 0 { @@ -400,3 +484,19 @@ func handleCreateViewingKeyMsg(data string) []byte { func handleSetViewingKeyMsg(data string) []byte { return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"set_viewing_key\": {\"key\": \"%s\"}}", data))) } + +func handleSendWithCallbackMsg(toAddress sdk.AccAddress, amount string, message string) []byte { + return []byte(spacePad(MESSAGE_BLOCK_SIZE, + fmt.Sprintf("{\"send\": {\"recipient\": \"%s\", \"amount\": \"%s\", \"msg\": \"%s\"}}", + toAddress.String(), + amount, + message))) +} + +func handleSendMsg(toAddress sdk.AccAddress, amount string) []byte { + return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"send\": {\"recipient\": \"%s\", \"amount\": \"%s\"}}", toAddress.String(), amount))) +} + +func handleBurnMsg(toAddress sdk.AccAddress, amount string) []byte { + return []byte(spacePad(MESSAGE_BLOCK_SIZE, fmt.Sprintf("{\"burn\": {\"amount\": \"%s\"}}", amount))) +} From a51db22aa4dfe4c60f41b5cb8d8f2446606e97bb Mon Sep 17 00:00:00 2001 From: TomL94 Date: Thu, 15 Oct 2020 15:02:43 +0300 Subject: [PATCH 08/10] Removed warning from send() --- cmd/secretcli/secret20.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/secretcli/secret20.go b/cmd/secretcli/secret20.go index eec597661..92c5da905 100644 --- a/cmd/secretcli/secret20.go +++ b/cmd/secretcli/secret20.go @@ -361,8 +361,7 @@ func s20SendCmd(cdc *codec.Codec) *cobra.Command { Use: "send [contract_address or label] [to_account] [amount] [optional: callback_message]", Short: "*EXPERIMENTAL* send tokens to another address. Optionally add a callback message", Long: `Send tokens to another address (contract or not). If 'to_account' is a contract, you can optionally add a callback message to this contract. -If no callback provided, this is identical to 'transfer'. -WARNING! The tokens will be transferred even if the callback message fails!`, +If no callback provided, this is identical to 'transfer'.`, Args: cobra.MinimumNArgs(3), RunE: func(cmd *cobra.Command, args []string) error { From a8e22abe385190f94a6531482353775dd728f7b1 Mon Sep 17 00:00:00 2001 From: TomL94 Date: Mon, 19 Oct 2020 16:41:23 +0300 Subject: [PATCH 09/10] Reuse cli context instead of creating a double --- cmd/secretcli/secret20.go | 4 ++-- x/compute/client/cli/query.go | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/cmd/secretcli/secret20.go b/cmd/secretcli/secret20.go index 92c5da905..6a40b4d6a 100644 --- a/cmd/secretcli/secret20.go +++ b/cmd/secretcli/secret20.go @@ -108,7 +108,7 @@ func S20TransferHistoryCmd(cdc *codec.Codec) *cobra.Command { queryData := queryTransferHistoryMsg(addr, key, uint32(page), uint32(pageSize)) - err = cli.QueryWithData(contractAddr, cdc, queryData) + err = cli.QueryWithData(contractAddr, cdc, queryData, cliCtx) if err != nil { return err } @@ -148,7 +148,7 @@ key yet, use the "create-viewing-key" command. Otherwise, you can still see your queryData := queryBalanceMsg(addr, key) - err = cli.QueryWithData(contractAddr, cdc, queryData) + err = cli.QueryWithData(contractAddr, cdc, queryData, cliCtx) if err != nil { return err } diff --git a/x/compute/client/cli/query.go b/x/compute/client/cli/query.go index da34239ed..503a0d6ba 100644 --- a/x/compute/client/cli/query.go +++ b/x/compute/client/cli/query.go @@ -475,7 +475,7 @@ func GetCmdQuery(cdc *codec.Codec) *cobra.Command { return errors.New("query data must be json") } - return QueryWithData(contractAddr, cdc, queryData) + return QueryWithData(contractAddr, cdc, queryData, cliCtx) }, } decoder.RegisterFlags(cmd.PersistentFlags(), "query argument") @@ -483,9 +483,7 @@ func GetCmdQuery(cdc *codec.Codec) *cobra.Command { return cmd } -func QueryWithData(contractAddress string, cdc *codec.Codec, queryData []byte) error { - cliCtx := context.NewCLIContext().WithCodec(cdc) - +func QueryWithData(contractAddress string, cdc *codec.Codec, queryData []byte, cliCtx context.CLIContext) error { addr, err := sdk.AccAddressFromBech32(contractAddress) if err != nil { return err From 971dd9e145ba100345df82b7e860193ff1a48087 Mon Sep 17 00:00:00 2001 From: TomL94 Date: Mon, 19 Oct 2020 17:05:39 +0300 Subject: [PATCH 10/10] Removed unnecessary parameter --- cmd/secretcli/secret20.go | 4 ++-- x/compute/client/cli/query.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/secretcli/secret20.go b/cmd/secretcli/secret20.go index 6a40b4d6a..8a88839b1 100644 --- a/cmd/secretcli/secret20.go +++ b/cmd/secretcli/secret20.go @@ -108,7 +108,7 @@ func S20TransferHistoryCmd(cdc *codec.Codec) *cobra.Command { queryData := queryTransferHistoryMsg(addr, key, uint32(page), uint32(pageSize)) - err = cli.QueryWithData(contractAddr, cdc, queryData, cliCtx) + err = cli.QueryWithData(contractAddr, cliCtx, queryData) if err != nil { return err } @@ -148,7 +148,7 @@ key yet, use the "create-viewing-key" command. Otherwise, you can still see your queryData := queryBalanceMsg(addr, key) - err = cli.QueryWithData(contractAddr, cdc, queryData, cliCtx) + err = cli.QueryWithData(contractAddr, cliCtx, queryData) if err != nil { return err } diff --git a/x/compute/client/cli/query.go b/x/compute/client/cli/query.go index 503a0d6ba..9bfa1ddf6 100644 --- a/x/compute/client/cli/query.go +++ b/x/compute/client/cli/query.go @@ -475,7 +475,7 @@ func GetCmdQuery(cdc *codec.Codec) *cobra.Command { return errors.New("query data must be json") } - return QueryWithData(contractAddr, cdc, queryData, cliCtx) + return QueryWithData(contractAddr, cliCtx, queryData) }, } decoder.RegisterFlags(cmd.PersistentFlags(), "query argument") @@ -483,7 +483,7 @@ func GetCmdQuery(cdc *codec.Codec) *cobra.Command { return cmd } -func QueryWithData(contractAddress string, cdc *codec.Codec, queryData []byte, cliCtx context.CLIContext) error { +func QueryWithData(contractAddress string, cliCtx context.CLIContext, queryData []byte) error { addr, err := sdk.AccAddressFromBech32(contractAddress) if err != nil { return err