OSDN Git Service

add checkDeployParameter
authorChengcheng Zhang <943420582@qq.com>
Wed, 28 Aug 2019 03:00:15 +0000 (11:00 +0800)
committerChengcheng Zhang <943420582@qq.com>
Wed, 28 Aug 2019 03:00:15 +0000 (11:00 +0800)
cmd/main.go

index 38e1b6a..dd7e318 100644 (file)
@@ -1,6 +1,7 @@
 package main
 
 import (
+       "errors"
        "flag"
        "fmt"
 
@@ -14,7 +15,7 @@ var (
        amountRequested       = uint64(1000000000)                                                 // amountRequested represents asset amount which can unlock contract
        seller                = "00145dd7b82556226d563b6e7d573fe61d23bd461c1f"                     // control program which want to receive assetRequested
        cancelKey             = "3e5d7d52d334964eef173021ef6a04dc0807ac8c41700fe718f5a80c2109f79e" // cancelKey can cancel swap contract
-       assetLocked           = "bae7e17bb8f5d0cfbfd87a92f3204da082d388d4c9b10e8dcd36b3d0a18ceb3a" // assetIDLocked represents locked asset ID
+       assetIDLocked         = "bae7e17bb8f5d0cfbfd87a92f3204da082d388d4c9b10e8dcd36b3d0a18ceb3a" // assetIDLocked represents locked asset ID
        amountLocked          = uint64(20000000000)                                                // amountLocked represents locked asset amount
        txFee                 = uint64(50000000)                                                   // txFee represents transaction fee
 
@@ -23,6 +24,15 @@ var (
        accountPasswordUnlocked = "12345"                                        // accountPasswordUnlocked represents account password which create locked contract
 )
 
+var (
+       errInvalidAssetRequested = errors.New("assetRequested is invalid")
+       errInvalidSeller         = errors.New("seller is invalid")
+       errInvalidCancelKey      = errors.New("cancelKey is invalid")
+       errInvalidAccountID      = errors.New("accountID is invalid")
+       errInvalidAssetIDLocked  = errors.New("assetIDLocked is invalid")
+       errInvalidTxFee          = errors.New("txFee is invalid")
+)
+
 func main() {
        var accountID string
        flag.StringVar(&accountID, "accountID", "", "account ID, which is an identifier of account.")
@@ -42,8 +52,8 @@ func main() {
        var cancelKey string
        flag.StringVar(&cancelKey, "cancelKey", "", "public key, which can cancel contract.")
 
-       var assetLocked string
-       flag.StringVar(&assetLocked, "assetLocked", "", "asset ID, which is asset ID locked in contract by creator.")
+       var assetIDLocked string
+       flag.StringVar(&assetIDLocked, "assetIDLocked", "", "asset ID, which is asset ID locked in contract by creator.")
 
        var amountLocked uint64
        flag.Uint64Var(&amountLocked, "amountLocked", uint64(0), "asset amount, which is asset amount locked in contract by creator.")
@@ -53,19 +63,45 @@ func main() {
 
        flag.Parse()
 
-       // deploy contract
+       // fmt.Println("arg is :", flag.Arg(0))
+
+       if flag.Arg(0) == "deploy" {
+               if err := checkDeployParameter(assetRequested, seller, cancelKey, accountIDLocked, assetIDLocked, txFee); err != nil {
+                       panic(err)
+               }
+               contractUTXOID := swap.DeployContract(assetRequested, seller, cancelKey, accountIDLocked, assetIDLocked, accountPasswordLocked, amountRequested, amountLocked, txFee)
+               fmt.Println("--> contractUTXOID:", contractUTXOID)
+       }
+
+       // contractUTXOID := swap.DeployContract(assetRequested, seller, cancelKey, accountIDLocked, assetIDLocked, accountPasswordLocked, amountRequested, amountLocked, txFee)
+       // txID := swap.CallContract(accountIDUnlocked, contractUTXOID, seller, assetIDLocked, assetRequested, buyerContolProgram, accountPasswordUnlocked, amountRequested, amountLocked, txFee)
+       // fmt.Println("--> txID:", txID)
+}
+
+func checkDeployParameter(assetRequested, seller, cancelKey, accountIDLocked, assetIDLocked string, txFee uint64) error {
+       if len(assetRequested) != 64 {
+               return errInvalidAssetRequested
+       }
+
+       if len(seller) == 0 {
+               return errInvalidSeller
+       }
+
+       if len(cancelKey) == 0 {
+               return errInvalidCancelKey
+       }
 
-       //
+       if len(accountIDLocked) == 0 {
+               return errInvalidAccountID
+       }
 
-       // build unlocked contract transaction
-       txUnlocked := swap.BuildUnlockContractTransaction(accountIDUnlocked, contractUTXOID, seller, assetLocked, assetRequested, buyerContolProgram, amountRequested, amountLocked, txFee)
-       fmt.Println("--> txUnlocked:", string(txUnlocked))
+       if len(assetIDLocked) != 64 {
+               return errInvalidAssetIDLocked
+       }
 
-       // sign unlocked contract transaction
-       signedTransaction = swap.SignTransaction(accountPasswordUnlocked, string(txUnlocked))
-       fmt.Println("--> signedTransaction:", signedTransaction)
+       if txFee == uint64(0) {
+               return errInvalidTxFee
+       }
 
-       // submit signed unlocked contract transaction
-       txID = swap.SubmitTransaction(signedTransaction)
-       fmt.Println("--> txID:", txID)
+       return nil
 }