OSDN Git Service

add buildLockHTLCContract
authorChengcheng Zhang <943420582@qq.com>
Thu, 5 Sep 2019 12:47:12 +0000 (20:47 +0800)
committerChengcheng Zhang <943420582@qq.com>
Thu, 5 Sep 2019 12:47:12 +0000 (20:47 +0800)
swap/htlc.go
swap/htlc_test.go [new file with mode: 0644]
swap/trade_offer.go

index f55c6f4..82725bf 100644 (file)
@@ -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 (file)
index 0000000..0dfa478
--- /dev/null
@@ -0,0 +1,5 @@
+package swap
+
+// func TestCompileLockHTLCContract(t *testing.T) {
+//     HTLCContractArgs :=
+// }
index e0838f6..d7d2eb1 100644 (file)
@@ -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),
        ))