Skip to content

Commit

Permalink
R4R: Fix HTLC CLI (#2110)
Browse files Browse the repository at this point in the history
* Create go.yml

* Update go.yml

* Update go.yml

* Update go.yml

* Update go.yml

* Update go.yml

* Update go.yml

* Update go.yml

* Update go.yml

* Update go.yml

* Update go.yml

* Update go.yml

* Update go.yml

* Update go.yml

* Update go.yml

* Create testUnit.yml

* Update and rename go.yml to buildAndPush.yml

* Update buildAndPush.yml

* Update testUnit.yml

* Update buildAndPush.yml

* Update buildAndPush.yml

* Update testUnit.yml

* Update testUnit.yml

* Update testUnit.yml

* update latest version

* Update coinswap desc

* typo fix

* instead circleci with github actions

* optimize

* remove trigger on PR

* rename

Co-Authored-By: Yelong Zhang <[email protected]>

* rename

Co-Authored-By: Yelong Zhang <[email protected]>

* rename

Co-Authored-By: Yelong Zhang <[email protected]>

* Update .github/workflows/testUnit.yml

Co-Authored-By: Yelong Zhang <[email protected]>

* Update .github/workflows/buildAndPush.yml

Co-Authored-By: Yelong Zhang <[email protected]>

* Update .github/workflows/buildAndPush.yml

Co-Authored-By: Yelong Zhang <[email protected]>

* Rename buildAndPush.yml to BuildAndPublish.yml

* Update BuildAndPublish.yml

* Rename testUnit.yml to TestUnit.yml

* fix htlc cli

* modify

* fix by hint

* fix

* fix docs
  • Loading branch information
secret2830 authored and chengwenxi committed Dec 4, 2019
1 parent c082f85 commit 82402a6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
3 changes: 2 additions & 1 deletion client/htlc/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func init() {
FsCreateHTLC.String(cli.FlagTo, "", "Bech32 encoding address to receive coins")
FsCreateHTLC.String(FlagReceiverOnOtherChain, "", "The claim receiving address on the other chain")
FsCreateHTLC.String(FlagAmount, "", "Similar to the amount in the original transfer")
FsCreateHTLC.BytesHex(FlagSecret, nil, "The secret for generating the hash lock, omission will be randomly generated")
FsCreateHTLC.BytesHex(FlagSecret, nil, "The secret for generating the hash lock, randomly generated if omitted")
FsCreateHTLC.BytesHex(FlagHashLock, nil, "The sha256 hash generated from secret (and timestamp if provided), generated from the secret flag if omitted")
FsCreateHTLC.Uint64(FlagTimestamp, 0, "The timestamp in seconds for generating the hash lock if provided")
FsCreateHTLC.String(FlagTimeLock, "", "The number of blocks to wait before the asset may be returned to")

Expand Down
57 changes: 39 additions & 18 deletions client/htlc/cli/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ func GetCmdCreateHTLC(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create an HTLC",
Example: "iriscli htlc create --chain-id=<chain-id> --from=<key-name> --fee=0.3iris --to=<to> --receiver-on-other-chain=<receiver-on-other-chain> --amount=<amount> --secret=<secret> " +
"--time-lock=<time-lock> --timestamp=<timestamp>",
Example: "iriscli htlc create --chain-id=<chain-id> --from=<key-name> --fee=0.3iris --to=<to> --receiver-on-other-chain=<receiver-on-other-chain> " +
"--amount=<amount> --secret=<secret> --hash-lock=<hash-lock> --timestamp=<timestamp> --time-lock=<time-lock>",
PreRunE: preCheckCmd,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().
WithCodec(cdc).
Expand Down Expand Up @@ -51,29 +52,39 @@ func GetCmdCreateHTLC(cdc *codec.Codec) *cobra.Command {
return err
}

secret := make([]byte, 32)
timestamp := viper.GetInt64(FlagTimestamp)
timeLock := viper.GetInt64(FlagTimeLock)

secretStr := strings.TrimSpace(viper.GetString(FlagSecret))
if len(secretStr) > 0 {
if len(secretStr) != 2*htlc.SecretLength {
return fmt.Errorf("the secret must be %d bytes long", htlc.SecretLength)
}
secret := make([]byte, 32)
var hashLock []byte

secret, err = hex.DecodeString(secretStr)
flags := cmd.Flags()
if flags.Changed(FlagHashLock) {
hashLockStr := strings.TrimSpace(viper.GetString(FlagHashLock))
hashLock, err = hex.DecodeString(hashLockStr)
if err != nil {
return err
}
} else {
_, err := rand.Read(secret)
if err != nil {
return err
secretStr := strings.TrimSpace(viper.GetString(FlagSecret))
if len(secretStr) > 0 {
if len(secretStr) != 2*htlc.SecretLength {
return fmt.Errorf("the secret must be %d bytes long", htlc.SecretLength)
}

secret, err = hex.DecodeString(secretStr)
if err != nil {
return err
}
} else {
_, err := rand.Read(secret)
if err != nil {
return err
}
}
}

timestamp := viper.GetInt64(FlagTimestamp)
hashLock := htlc.GetHashLock(secret, uint64(timestamp))

timeLock := viper.GetInt64(FlagTimeLock)
hashLock = htlc.GetHashLock(secret, uint64(timestamp))
}

msg := htlc.NewMsgCreateHTLC(
sender, toAddr, receiverOnOtherChain, amount,
Expand All @@ -84,7 +95,7 @@ func GetCmdCreateHTLC(cdc *codec.Codec) *cobra.Command {
}

err = utils.SendOrPrintTx(txCtx, cliCtx, []sdk.Msg{msg})
if err == nil {
if err == nil && !flags.Changed(FlagHashLock) {
fmt.Println("**Important** save this secret, hashLock in a safe place.")
fmt.Println("It is the only way to claim or refund the locked coins from an HTLC")
fmt.Println()
Expand Down Expand Up @@ -192,3 +203,13 @@ func GetCmdRefundHTLC(cdc *codec.Codec) *cobra.Command {

return cmd
}

func preCheckCmd(cmd *cobra.Command, _ []string) error {
// make sure either the secret or hash lock is provided
flags := cmd.Flags()
if flags.Changed(FlagSecret) && flags.Changed(FlagHashLock) {
return fmt.Errorf("only one flag is allowed among the secret and hash lock")
}

return nil
}
4 changes: 2 additions & 2 deletions docs/cli-client/htlc.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ iriscli htlc create --chain-id=<chain-id> --from=<key-name> --fee=0.3iris --to=<
| --to | string | Yes | | Bech32 encoding address to receive coins |
| --receiver-on-other-chain | string | | | The claim receiving address on the other chain |
| --amount | string | Yes | | Similar to the amount in the original transfer |
| --secret | bytesHex | | | The secret for generating the hash lock, omission will be randomly generated |
| --hash-lock | bytesHex | Yes | | The sha256 hash generated from secret (and timestamp if provided) |
| --secret | bytesHex | | | The secret for generating the hash lock, randomly generated if omitted |
| --hash-lock | bytesHex | | | The sha256 hash generated from secret (and timestamp if provided), generated from the secret flag if omitted |
| --time-lock | string | Yes | | The number of blocks to wait before the asset may be returned to |
| --timestamp | uint | | | The timestamp in seconds for generating hash lock if provided |

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/cli-client/htlc.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ iriscli htlc create --chain-id=<chain-id> --from=<key-name> --fee=0.3iris --to=<
| --receiver-on-other-chain | bytesHex | | | 另一条链上的HTLC认领接收地址 |
| --amount | string | | | 要发送的金额 |
| --secret | bytesHex | | | 用于生成Hash Lock的secret, 缺省将随机生成 |
| --hash-lock | bytesHex | | | 由secret和时间戳如果提供生成的sha256哈希 |
| --hash-lock | bytesHex | | | 由secret和时间戳如果提供生成的sha256哈希, 缺省将根据secret标志生成 |
| --time-lock | string | | | 资金锁定的区块数 |
| --timestamp | uint | | | 参与生成hash lock的10位时间戳可选|

Expand Down

0 comments on commit 82402a6

Please sign in to comment.