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

R4R: Fix HTLC CLI #2110

Merged
merged 45 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
f02580f
Create go.yml
oncloudit Nov 14, 2019
af637ec
Update go.yml
oncloudit Nov 14, 2019
7d1d71b
Update go.yml
oncloudit Nov 14, 2019
4ea80de
Update go.yml
oncloudit Nov 14, 2019
8e4731d
Update go.yml
oncloudit Nov 14, 2019
5a3e68d
Update go.yml
oncloudit Nov 14, 2019
21a45ab
Update go.yml
oncloudit Nov 14, 2019
2fc20e7
Update go.yml
oncloudit Nov 14, 2019
c02fea8
Update go.yml
oncloudit Nov 14, 2019
36a5c82
Update go.yml
oncloudit Nov 14, 2019
d099429
Update go.yml
oncloudit Nov 14, 2019
96c6cd6
Update go.yml
oncloudit Nov 14, 2019
76b7fd5
Update go.yml
oncloudit Nov 14, 2019
5a33919
Update go.yml
oncloudit Nov 14, 2019
e7931ad
Update go.yml
oncloudit Nov 14, 2019
f7a7a48
Create testUnit.yml
oncloudit Nov 15, 2019
68ed00a
Update and rename go.yml to buildAndPush.yml
oncloudit Nov 15, 2019
362d47b
Update buildAndPush.yml
oncloudit Nov 15, 2019
544c0dc
Update testUnit.yml
oncloudit Nov 15, 2019
c070a9c
Update buildAndPush.yml
oncloudit Nov 15, 2019
479266d
Update buildAndPush.yml
oncloudit Nov 18, 2019
30d4dcf
Update testUnit.yml
oncloudit Nov 18, 2019
0e5461f
Update testUnit.yml
oncloudit Nov 18, 2019
38a63c3
Update testUnit.yml
oncloudit Nov 18, 2019
672353c
instead circleci with github actions
oncloudit Nov 27, 2019
8fd3970
optimize
oncloudit Nov 27, 2019
1f2338f
remove trigger on PR
oncloudit Nov 27, 2019
09051a6
rename
oncloudit Nov 27, 2019
8210759
rename
oncloudit Nov 27, 2019
994a17a
rename
oncloudit Nov 27, 2019
fba28fc
Update .github/workflows/testUnit.yml
oncloudit Nov 28, 2019
a355039
Update .github/workflows/buildAndPush.yml
oncloudit Nov 28, 2019
b216b71
Update .github/workflows/buildAndPush.yml
oncloudit Nov 28, 2019
c3c9067
Rename buildAndPush.yml to BuildAndPublish.yml
oncloudit Nov 28, 2019
0bbc965
Update BuildAndPublish.yml
oncloudit Nov 28, 2019
64e2cc3
Rename testUnit.yml to TestUnit.yml
oncloudit Nov 28, 2019
0f7ba68
update latest version
zhangyelong Nov 22, 2019
669b48a
Update coinswap desc
zhangyelong Nov 25, 2019
e2eefd1
typo fix
zhangyelong Nov 26, 2019
6337b14
fix htlc cli
secret2830 Dec 2, 2019
b8e2144
modify
secret2830 Dec 2, 2019
c7582e4
fix by hint
secret2830 Dec 2, 2019
47c277e
fix
secret2830 Dec 2, 2019
d0968b6
fix docs
secret2830 Dec 3, 2019
f9b33d6
Merge branch 'hotfix/v0.16.0' into secret/fix-htlc-cli
secret2830 Dec 3, 2019
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
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