From: Chengcheng Zhang <943420582@qq.com> Date: Thu, 5 Sep 2019 12:47:12 +0000 (+0800) Subject: add buildLockHTLCContract X-Git-Url: http://git.osdn.net/view?p=bytom%2Fshuttle.git;a=commitdiff_plain;h=8926cabb09ba237f3bb983480f2e0a7c7d5747da add buildLockHTLCContract --- diff --git a/swap/htlc.go b/swap/htlc.go index f55c6f4..82725bf 100644 --- a/swap/htlc.go +++ b/swap/htlc.go @@ -1,5 +1,111 @@ package swap -// type compileLockContractResponse struct { -// Program string `json:"program"` +import ( + "fmt" + "strconv" +) + +type HTLCAccount struct { + AccountID string + TxFee uint64 +} + +type HTLCContractArgs struct { + SenderPublicKey string + RecipientPublicKey string + BlockHeight uint64 + Hash string +} + +type compileLockHTLCContractResponse struct { + Program string `json:"program"` +} + +var compileLockHTLCContractPayload = `{ + "contract":"contract HTLC(sender: PublicKey, recipient: PublicKey, blockHeight: Integer, hash: Hash) locks valueAmount of valueAsset { clause complete(preimage: String, sig: Signature) {verify sha256(preimage) == hash verify checkTxSig(recipient, sig) unlock valueAmount of valueAsset} clause cancel(sig: Signature) {verify above(blockHeight) verify checkTxSig(sender, sig) unlock valueAmount of valueAsset}}", + "args":[ + { + "string":"%s" + }, + { + "string":"%s" + }, + { + "integer":%s + }, + { + "string":"%s" + } + ] +}` + +func compileLockHTLCContract(contractArgs HTLCContractArgs) (string, error) { + payload := []byte(fmt.Sprintf( + compileLockHTLCContractPayload, + contractArgs.SenderPublicKey, + contractArgs.RecipientPublicKey, + strconv.FormatUint(contractArgs.BlockHeight, 10), + contractArgs.Hash, + )) + res := new(compileLockHTLCContractResponse) + if err := request(compileURL, payload, res); err != nil { + return "", err + } + return res.Program, nil +} + +var buildLockHTLCContractPayload = `{ + "actions": [ + { + "account_id": "%s", + "amount": %s, + "asset_id": "%s", + "use_unconfirmed":true, + "type": "spend_account" + }, + { + "amount": %s, + "asset_id": "%s", + "control_program": "%s", + "type": "control_program" + }, + { + "account_id": "%s", + "amount": %s, + "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "use_unconfirmed":true, + "type": "spend_account" + } + ], + "ttl": 0, + "base_transaction": null +}` + +func buildLockHTLCContract(account HTLCAccount, contractValue AssetAmount, contractControlProgram string) (interface{}, error) { + payload := []byte(fmt.Sprintf( + buildLockHTLCContractPayload, + account.AccountID, + strconv.FormatUint(contractValue.Amount, 10), + contractValue.Asset, + strconv.FormatUint(contractValue.Amount, 10), + contractValue.Asset, + contractControlProgram, + account.AccountID, + strconv.FormatUint(account.TxFee, 10), + )) + res := new(interface{}) + if err := request(buildTransactionURL, payload, res); err != nil { + return "", err + } + return res, nil +} + +// func DeployHTLCContract(contractArgs HTLCContractArgs) (string, error) { +// // compile locked HTLC cotnract +// HTLCContractControlProgram, err := compileLockHTLCContract(contractArgs) +// if err != nil { +// return "", err +// } + +// return // } diff --git a/swap/htlc_test.go b/swap/htlc_test.go new file mode 100644 index 0000000..0dfa478 --- /dev/null +++ b/swap/htlc_test.go @@ -0,0 +1,5 @@ +package swap + +// func TestCompileLockHTLCContract(t *testing.T) { +// HTLCContractArgs := +// } diff --git a/swap/trade_offer.go b/swap/trade_offer.go index e0838f6..d7d2eb1 100644 --- a/swap/trade_offer.go +++ b/swap/trade_offer.go @@ -106,7 +106,8 @@ func buildLockTransaction(accountInfo AccountInfo, contractValue AssetAmount, co strconv.FormatUint(contractValue.Amount, 10), contractValue.Asset, strconv.FormatUint(contractValue.Amount, 10), - contractValue.Asset, contractControlProgram, + contractValue.Asset, + contractControlProgram, accountInfo.AccountID, strconv.FormatUint(accountInfo.TxFee, 10), ))