OSDN Git Service

add server
authorChengcheng Zhang <943420582@qq.com>
Wed, 11 Sep 2019 10:31:35 +0000 (18:31 +0800)
committerChengcheng Zhang <943420582@qq.com>
Wed, 11 Sep 2019 10:31:35 +0000 (18:31 +0800)
cmd/commands.go
swap/common.go
swap/htlc.go
swap/request.go
swap/tradeoff.go

index e6732a9..93278cb 100644 (file)
@@ -18,6 +18,8 @@ func init() {
        deployCmd.PersistentFlags().StringVar(&cancelKey, "cancelKey", "", "tradeoff contract paramenter with seller pubkey for cancelling the contract")
        deployCmd.PersistentFlags().StringVar(&assetLocked, "assetLocked", "", "tradeoff contract locked value with assetID")
        deployCmd.PersistentFlags().Uint64Var(&amountLocked, "amountLocked", 0, "tradeoff contract locked value with amount")
+       deployCmd.PersistentFlags().StringVar(&ip, "ip", "127.0.0.1", "network address")
+       deployCmd.PersistentFlags().StringVar(&port, "port", "9888", "network port")
 
        // deploy HTLC contract arguments
        deployHTLCCmd.PersistentFlags().Uint64Var(&txFee, "txFee", 40000000, "contract transaction fee")
@@ -27,22 +29,34 @@ func init() {
        deployHTLCCmd.PersistentFlags().StringVar(&hash, "hash", "", "HTLC contract locked value with hash")
        deployHTLCCmd.PersistentFlags().StringVar(&assetLocked, "assetLocked", "", "HTLC contract locked value with assetID")
        deployHTLCCmd.PersistentFlags().Uint64Var(&amountLocked, "amountLocked", 0, "HTLC contract locked value with amount")
+       deployHTLCCmd.PersistentFlags().StringVar(&ip, "ip", "127.0.0.1", "network address")
+       deployHTLCCmd.PersistentFlags().StringVar(&port, "port", "9888", "network port")
 
        // call contract arguments
        callCmd.PersistentFlags().Uint64Var(&txFee, "txFee", 40000000, "contract transaction fee")
+       callCmd.PersistentFlags().StringVar(&ip, "ip", "127.0.0.1", "network address")
+       callCmd.PersistentFlags().StringVar(&port, "port", "9888", "network port")
 
        // call HTLC contract arguments
        callHTLCCmd.PersistentFlags().Uint64Var(&txFee, "txFee", 40000000, "contract transaction fee")
+       callHTLCCmd.PersistentFlags().StringVar(&ip, "ip", "127.0.0.1", "network address")
+       callHTLCCmd.PersistentFlags().StringVar(&port, "port", "9888", "network port")
 
        // cancel tradeoff contract arguments
        cancelCmd.PersistentFlags().Uint64Var(&txFee, "txFee", 40000000, "contract transaction fee")
+       cancelCmd.PersistentFlags().StringVar(&ip, "ip", "127.0.0.1", "network address")
+       cancelCmd.PersistentFlags().StringVar(&port, "port", "9888", "network port")
 
        // cancel HTLC contract arguments
        cancelHTLCCmd.PersistentFlags().Uint64Var(&txFee, "txFee", 40000000, "contract transaction fee")
+       cancelHTLCCmd.PersistentFlags().StringVar(&ip, "ip", "127.0.0.1", "network address")
+       cancelHTLCCmd.PersistentFlags().StringVar(&port, "port", "9888", "network port")
 }
 
 var (
        txFee = uint64(0)
+       ip    = "127.0.0.1"
+       port  = "9888"
 
        // contract paramenters
        assetRequested  = ""
@@ -68,7 +82,7 @@ var (
 )
 
 var deployCmd = &cobra.Command{
-       Use:   "deploy <accountID> <password> [contract flags(paramenters and locked value)] [txFee flag]",
+       Use:   "deploy <accountID> <password> [contract flags(paramenters and locked value)] [txFee flag] [URL flags(ip and port)]",
        Short: "deploy tradeoff contract",
        Args:  cobra.ExactArgs(2),
        Run: func(cmd *cobra.Command, args []string) {
@@ -104,7 +118,12 @@ var deployCmd = &cobra.Command{
                        os.Exit(0)
                }
 
-               contractUTXOID, err := swap.DeployContract(accountInfo, contractArgs, contractValue)
+               server := &swap.Server{
+                       IP:   ip,
+                       Port: port,
+               }
+
+               contractUTXOID, err := swap.DeployContract(server, accountInfo, contractArgs, contractValue)
                if err != nil {
                        fmt.Println(err)
                        os.Exit(0)
@@ -114,7 +133,7 @@ var deployCmd = &cobra.Command{
 }
 
 var callCmd = &cobra.Command{
-       Use:   "call <accountID> <password> <buyer-program> <contractUTXOID> [txFee flag]",
+       Use:   "call <accountID> <password> <buyer-program> <contractUTXOID> [txFee flag] [URL flags(ip and port)]",
        Short: "call tradeoff contract for asset swapping",
        Args:  cobra.ExactArgs(4),
        Run: func(cmd *cobra.Command, args []string) {
@@ -135,7 +154,12 @@ var callCmd = &cobra.Command{
                        os.Exit(0)
                }
 
-               txID, err := swap.CallContract(accountInfo, contractUTXOID)
+               server := &swap.Server{
+                       IP:   ip,
+                       Port: port,
+               }
+
+               txID, err := swap.CallContract(server, accountInfo, contractUTXOID)
                if err != nil {
                        fmt.Println(err)
                        os.Exit(0)
@@ -145,7 +169,7 @@ var callCmd = &cobra.Command{
 }
 
 var cancelCmd = &cobra.Command{
-       Use:   "cancel <accountID> <password> <redeem-program> <contractUTXOID> [txFee flag]",
+       Use:   "cancel <accountID> <password> <redeem-program> <contractUTXOID> [txFee flag] [URL flags(ip and port)]",
        Short: "cancel tradeoff contract for asset swapping",
        Args:  cobra.ExactArgs(4),
        Run: func(cmd *cobra.Command, args []string) {
@@ -166,7 +190,12 @@ var cancelCmd = &cobra.Command{
                        os.Exit(0)
                }
 
-               txID, err := swap.CancelTradeoffContract(accountInfo, contractUTXOID)
+               server := &swap.Server{
+                       IP:   ip,
+                       Port: port,
+               }
+
+               txID, err := swap.CancelTradeoffContract(server, accountInfo, contractUTXOID)
                if err != nil {
                        fmt.Println(err)
                        os.Exit(0)
@@ -176,7 +205,7 @@ var cancelCmd = &cobra.Command{
 }
 
 var deployHTLCCmd = &cobra.Command{
-       Use:   "deployHTLC <accountID> <password> [contract flags(paramenters and locked value)] [txFee flag]",
+       Use:   "deployHTLC <accountID> <password> [contract flags(paramenters and locked value)] [txFee flag] [URL flags(ip and port)]",
        Short: "deploy HTLC contract",
        Args:  cobra.ExactArgs(2),
        Run: func(cmd *cobra.Command, args []string) {
@@ -210,7 +239,12 @@ var deployHTLCCmd = &cobra.Command{
                        os.Exit(0)
                }
 
-               contractUTXOID, err := swap.DeployHTLCContract(account, contractValue, contractArgs)
+               server := &swap.Server{
+                       IP:   ip,
+                       Port: port,
+               }
+
+               contractUTXOID, err := swap.DeployHTLCContract(server, account, contractValue, contractArgs)
                if err != nil {
                        fmt.Println(err)
                        os.Exit(0)
@@ -220,7 +254,7 @@ var deployHTLCCmd = &cobra.Command{
 }
 
 var callHTLCCmd = &cobra.Command{
-       Use:   "callHTLC <accountID> <password> <buyer-program> <preimage> <contractUTXOID> [txFee flag]",
+       Use:   "callHTLC <accountID> <password> <buyer-program> <preimage> <contractUTXOID> [txFee flag] [URL flags(ip and port)]",
        Short: "callHTLC HTLC contract for asset swapping",
        Args:  cobra.ExactArgs(5),
        Run: func(cmd *cobra.Command, args []string) {
@@ -242,7 +276,11 @@ var callHTLCCmd = &cobra.Command{
                }
 
                preimage := args[3]
-               txID, err := swap.CallHTLCContract(account, contractUTXOID, preimage)
+               server := &swap.Server{
+                       IP:   ip,
+                       Port: port,
+               }
+               txID, err := swap.CallHTLCContract(server, account, contractUTXOID, preimage)
                if err != nil {
                        fmt.Println(err)
                        os.Exit(0)
@@ -252,7 +290,7 @@ var callHTLCCmd = &cobra.Command{
 }
 
 var cancelHTLCCmd = &cobra.Command{
-       Use:   "cancelHTLC <accountID> <password> <redeem-program> <contractUTXOID> [txFee flag]",
+       Use:   "cancelHTLC <accountID> <password> <redeem-program> <contractUTXOID> [txFee flag] [URL flags(ip and port)]",
        Short: "cancel HTLC contract for asset swapping",
        Args:  cobra.ExactArgs(4),
        Run: func(cmd *cobra.Command, args []string) {
@@ -273,7 +311,12 @@ var cancelHTLCCmd = &cobra.Command{
                        os.Exit(0)
                }
 
-               txID, err := swap.CancelHTLCContract(accountInfo, contractUTXOID)
+               server := &swap.Server{
+                       IP:   ip,
+                       Port: port,
+               }
+
+               txID, err := swap.CancelHTLCContract(server, accountInfo, contractUTXOID)
                if err != nil {
                        fmt.Println(err)
                        os.Exit(0)
index 7e4d125..989e31f 100644 (file)
@@ -1,21 +1,39 @@
 package swap
 
+// var (
+//     localURL = "http://127.0.0.1:9888/"
+
+//     buildTransactionURL     = localURL + "build-transaction"
+//     getTransactionURL       = localURL + "get-transaction"
+//     signTransactionURL      = localURL + "sign-transaction"
+//     decodeRawTransactionURL = localURL + "decode-raw-transaction"
+//     submitTransactionURL    = localURL + "submit-transaction"
+//     compileURL              = localURL + "compile"
+//     decodeProgramURL        = localURL + "decode-program"
+//     signMessageURl          = localURL + "sign-message"
+//     listAccountsURL         = localURL + "list-accounts"
+//     listAddressesURL        = localURL + "list-addresses"
+//     listBalancesURL         = localURL + "list-balances"
+//     listPubkeysURL          = localURL + "list-pubkeys"
+//     listUnspentOutputsURL   = localURL + "list-unspent-outputs"
+// )
+
 var (
-       localURL = "http://127.0.0.1:9888/"
+       // localURL = "http://127.0.0.1:9888/"
 
-       buildTransactionURL     = localURL + "build-transaction"
-       getTransactionURL       = localURL + "get-transaction"
-       signTransactionURL      = localURL + "sign-transaction"
-       decodeRawTransactionURL = localURL + "decode-raw-transaction"
-       submitTransactionURL    = localURL + "submit-transaction"
-       compileURL              = localURL + "compile"
-       decodeProgramURL        = localURL + "decode-program"
-       signMessageURl          = localURL + "sign-message"
-       listAccountsURL         = localURL + "list-accounts"
-       listAddressesURL        = localURL + "list-addresses"
-       listBalancesURL         = localURL + "list-balances"
-       listPubkeysURL          = localURL + "list-pubkeys"
-       listUnspentOutputsURL   = localURL + "list-unspent-outputs"
+       buildTransactionURL     = "build-transaction"
+       getTransactionURL       = "get-transaction"
+       signTransactionURL      = "sign-transaction"
+       decodeRawTransactionURL = "decode-raw-transaction"
+       submitTransactionURL    = "submit-transaction"
+       compileURL              = "compile"
+       decodeProgramURL        = "decode-program"
+       signMessageURl          = "sign-message"
+       listAccountsURL         = "list-accounts"
+       listAddressesURL        = "list-addresses"
+       listBalancesURL         = "list-balances"
+       listPubkeysURL          = "list-pubkeys"
+       listUnspentOutputsURL   = "list-unspent-outputs"
 )
 
 type AccountInfo struct {
index ece9f86..ba332b0 100644 (file)
@@ -46,7 +46,7 @@ var compileLockHTLCContractReq = `{
     ]
 }`
 
-func compileLockHTLCContract(contractArgs HTLCContractArgs) (string, error) {
+func compileLockHTLCContract(s *Server, contractArgs HTLCContractArgs) (string, error) {
        payload := []byte(fmt.Sprintf(compileLockHTLCContractReq,
                contractArgs.SenderPublicKey,
                contractArgs.RecipientPublicKey,
@@ -54,7 +54,7 @@ func compileLockHTLCContract(contractArgs HTLCContractArgs) (string, error) {
                contractArgs.Hash,
        ))
        res := new(compileLockHTLCContractResp)
-       if err := request(compileURL, payload, res); err != nil {
+       if err := s.request(compileURL, payload, res); err != nil {
                return "", err
        }
        return res.Program, nil
@@ -87,7 +87,7 @@ var buildLockHTLCContractTxReq = `{
     "base_transaction": null
 }`
 
-func buildLockHTLCContractTransaction(account AccountInfo, contractValue AssetAmount, contractControlProgram string) (interface{}, error) {
+func buildLockHTLCContractTransaction(s *Server, account AccountInfo, contractValue AssetAmount, contractControlProgram string) (interface{}, error) {
        payload := []byte(fmt.Sprintf(buildLockHTLCContractTxReq,
                account.AccountID,
                contractValue.Amount,
@@ -99,7 +99,7 @@ func buildLockHTLCContractTransaction(account AccountInfo, contractValue AssetAm
                account.TxFee,
        ))
        res := new(interface{})
-       if err := request(buildTransactionURL, payload, res); err != nil {
+       if err := s.request(buildTransactionURL, payload, res); err != nil {
                return "", err
        }
        return res, nil
@@ -138,7 +138,7 @@ var buildUnlockHTLCContractTxReq = `{
     "base_transaction": null
 }`
 
-func buildUnlockHTLCContractTransaction(account AccountInfo, contractUTXOID string, contractValue AssetAmount) (*buildUnlockHTLCContractTxResp, error) {
+func buildUnlockHTLCContractTransaction(s *Server, account AccountInfo, contractUTXOID string, contractValue AssetAmount) (*buildUnlockHTLCContractTxResp, error) {
        payload := []byte(fmt.Sprintf(buildUnlockHTLCContractTxReq,
                contractUTXOID,
                account.AccountID,
@@ -148,7 +148,7 @@ func buildUnlockHTLCContractTransaction(account AccountInfo, contractUTXOID stri
                account.Receiver,
        ))
        res := new(buildUnlockHTLCContractTxResp)
-       if err := request(buildTransactionURL, payload, res); err != nil {
+       if err := s.request(buildTransactionURL, payload, res); err != nil {
                return nil, err
        }
        return res, nil
@@ -168,14 +168,14 @@ type decodeRawTxReq struct {
        RawTx string `json:"raw_transaction"`
 }
 
-func decodeRawTransaction(rawTransaction string, contractValue AssetAmount) (string, string, error) {
+func decodeRawTransaction(s *Server, rawTransaction string, contractValue AssetAmount) (string, string, error) {
        payload, err := json.Marshal(decodeRawTxReq{RawTx: rawTransaction})
        if err != nil {
                return "", "", err
        }
 
        res := new(decodeRawTxResp)
-       if err := request(decodeRawTransactionURL, payload, res); err != nil {
+       if err := s.request(decodeRawTransactionURL, payload, res); err != nil {
                return "", "", err
        }
 
@@ -187,14 +187,14 @@ func decodeRawTransaction(rawTransaction string, contractValue AssetAmount) (str
        return "", "", errFailedGetSignData
 }
 
-func getRecipientPublicKey(contractControlProgram string) (string, error) {
+func getRecipientPublicKey(s *Server, contractControlProgram string) (string, error) {
        payload, err := json.Marshal(decodeProgramReq{Program: contractControlProgram})
        if err != nil {
                return "", err
        }
 
        res := new(decodeProgramResp)
-       if err := request(decodeProgramURL, payload, res); err != nil {
+       if err := s.request(decodeProgramURL, payload, res); err != nil {
                return "", err
        }
 
@@ -213,22 +213,22 @@ type listAddressesReq struct {
        AccountID string `json:"account_id"`
 }
 
-func listAddresses(accountID string) ([]AddressInfo, error) {
+func listAddresses(s *Server, accountID string) ([]AddressInfo, error) {
        payload, err := json.Marshal(listAddressesReq{AccountID: accountID})
        if err != nil {
                return nil, err
        }
 
        res := new([]AddressInfo)
-       if err := request(listAddressesURL, payload, res); err != nil {
+       if err := s.request(listAddressesURL, payload, res); err != nil {
                return nil, err
        }
 
        return *res, nil
 }
 
-func getAddress(accountID, contractControlProgram string) (string, error) {
-       publicKey, err := getRecipientPublicKey(contractControlProgram)
+func getAddress(s *Server, accountID, contractControlProgram string) (string, error) {
+       publicKey, err := getRecipientPublicKey(s, contractControlProgram)
        if err != nil {
                return "", err
        }
@@ -244,7 +244,7 @@ func getAddress(accountID, contractControlProgram string) (string, error) {
                return "", err
        }
 
-       addressInfos, err := listAddresses(accountID)
+       addressInfos, err := listAddresses(s, accountID)
        if err != nil {
                return "", err
        }
@@ -268,7 +268,7 @@ type signMessageResp struct {
        DerivedXPub string `json:"derived_xpub"`
 }
 
-func signMessage(address, message, password string) (string, error) {
+func signMessage(s *Server, address, message, password string) (string, error) {
        payload, err := json.Marshal(signMessageReq{
                Address:  address,
                Message:  message,
@@ -279,7 +279,7 @@ func signMessage(address, message, password string) (string, error) {
        }
 
        res := new(signMessageResp)
-       if err := request(signMessageURl, payload, res); err != nil {
+       if err := s.request(signMessageURl, payload, res); err != nil {
                return "", nil
        }
        return res.Signature, nil
@@ -314,7 +314,7 @@ var signUnlockHTLCContractTxReq = `{
     }
 }`
 
-func signUnlockHTLCContractTransaction(account AccountInfo, preimage, recipientSig, rawTransaction, signingInst string) (string, error) {
+func signUnlockHTLCContractTransaction(s *Server, account AccountInfo, preimage, recipientSig, rawTransaction, signingInst string) (string, error) {
        payload := []byte(fmt.Sprintf(signUnlockHTLCContractTxReq,
                account.Password,
                rawTransaction,
@@ -324,7 +324,7 @@ func signUnlockHTLCContractTransaction(account AccountInfo, preimage, recipientS
                account.TxFee,
        ))
        res := new(signTxResp)
-       if err := request(signTransactionURL, payload, res); err != nil {
+       if err := s.request(signTransactionURL, payload, res); err != nil {
                return "", err
        }
 
@@ -335,14 +335,14 @@ func signUnlockHTLCContractTransaction(account AccountInfo, preimage, recipientS
        return res.Tx.RawTransaction, nil
 }
 
-func decodeHTLCProgram(program string) (*HTLCContractArgs, error) {
+func decodeHTLCProgram(s *Server, program string) (*HTLCContractArgs, error) {
        payload, err := json.Marshal(decodeProgramReq{Program: program})
        if err != nil {
                return nil, err
        }
 
        res := new(decodeProgramResp)
-       if err := request(decodeProgramURL, payload, res); err != nil {
+       if err := s.request(decodeProgramURL, payload, res); err != nil {
                return nil, err
        }
 
@@ -365,33 +365,33 @@ func decodeHTLCProgram(program string) (*HTLCContractArgs, error) {
 }
 
 // DeployHTLCContract deploy HTLC contract.
-func DeployHTLCContract(account AccountInfo, contractValue AssetAmount, contractArgs HTLCContractArgs) (string, error) {
+func DeployHTLCContract(s *Server, account AccountInfo, contractValue AssetAmount, contractArgs HTLCContractArgs) (string, error) {
        // compile locked HTLC cotnract
-       HTLCContractControlProgram, err := compileLockHTLCContract(contractArgs)
+       HTLCContractControlProgram, err := compileLockHTLCContract(s, contractArgs)
        if err != nil {
                return "", err
        }
 
        // build locked HTLC contract
-       txLocked, err := buildLockHTLCContractTransaction(account, contractValue, HTLCContractControlProgram)
+       txLocked, err := buildLockHTLCContractTransaction(s, account, contractValue, HTLCContractControlProgram)
        if err != nil {
                return "", err
        }
 
        // sign locked HTLC contract transaction
-       signedTransaction, err := signTransaction(account.Password, txLocked)
+       signedTransaction, err := signTransaction(s, account.Password, txLocked)
        if err != nil {
                return "", err
        }
 
        // submit signed HTLC contract transaction
-       txID, err := submitTransaction(signedTransaction)
+       txID, err := submitTransaction(s, signedTransaction)
        if err != nil {
                return "", err
        }
 
        // get HTLC contract output ID
-       contractUTXOID, err := getContractUTXOID(txID, HTLCContractControlProgram)
+       contractUTXOID, err := getContractUTXOID(s, txID, HTLCContractControlProgram)
        if err != nil {
                return "", err
        }
@@ -399,14 +399,14 @@ func DeployHTLCContract(account AccountInfo, contractValue AssetAmount, contract
 }
 
 // CallHTLCContract call HTLC contract.
-func CallHTLCContract(account AccountInfo, contractUTXOID, preimage string) (string, error) {
-       _, contractValue, err := ListUnspentOutputs(contractUTXOID)
+func CallHTLCContract(s *Server, account AccountInfo, contractUTXOID, preimage string) (string, error) {
+       _, contractValue, err := ListUnspentOutputs(s, contractUTXOID)
        if err != nil {
                return "", err
        }
 
        // build unlocked contract transaction
-       buildTxResp, err := buildUnlockHTLCContractTransaction(account, contractUTXOID, *contractValue)
+       buildTxResp, err := buildUnlockHTLCContractTransaction(s, account, contractUTXOID, *contractValue)
        if err != nil {
                return "", err
        }
@@ -416,31 +416,31 @@ func CallHTLCContract(account AccountInfo, contractUTXOID, preimage string) (str
                fmt.Println(err)
        }
 
-       contractControlProgram, signData, err := decodeRawTransaction(buildTxResp.RawTransaction, *contractValue)
+       contractControlProgram, signData, err := decodeRawTransaction(s, buildTxResp.RawTransaction, *contractValue)
        if err != nil {
                fmt.Println(err)
        }
 
        // get address by account ID and contract control program
-       address, err := getAddress(account.AccountID, contractControlProgram)
+       address, err := getAddress(s, account.AccountID, contractControlProgram)
        if err != nil {
                return "", err
        }
 
        // sign raw transaction
-       recipientSig, err := signMessage(address, signData, account.Password)
+       recipientSig, err := signMessage(s, address, signData, account.Password)
        if err != nil {
                return "", err
        }
 
        // sign raw transaction
-       signedTransaction, err := signUnlockHTLCContractTransaction(account, preimage, recipientSig, buildTxResp.RawTransaction, string(signingInst))
+       signedTransaction, err := signUnlockHTLCContractTransaction(s, account, preimage, recipientSig, buildTxResp.RawTransaction, string(signingInst))
        if err != nil {
                return "", err
        }
 
        // submit signed HTLC contract transaction
-       txID, err := submitTransaction(signedTransaction)
+       txID, err := submitTransaction(s, signedTransaction)
        if err != nil {
                return "", err
        }
@@ -449,39 +449,39 @@ func CallHTLCContract(account AccountInfo, contractUTXOID, preimage string) (str
 }
 
 // CancelHTLCContract cancel HTLC contract.
-func CancelHTLCContract(accountInfo AccountInfo, contractUTXOID string) (string, error) {
+func CancelHTLCContract(s *Server, accountInfo AccountInfo, contractUTXOID string) (string, error) {
        // get contract control program by contract UTXOID
-       contractControlProgram, contractValue, err := ListUnspentOutputs(contractUTXOID)
+       contractControlProgram, contractValue, err := ListUnspentOutputs(s, contractUTXOID)
        if err != nil {
                return "", err
        }
 
        // get public key by contract control program
-       contractArgs, err := decodeHTLCProgram(contractControlProgram)
+       contractArgs, err := decodeHTLCProgram(s, contractControlProgram)
        if err != nil {
                return "", err
        }
 
        // get public key path and root xpub by contract args
-       xpubInfo, err := getXPubKeyInfo(accountInfo.AccountID, contractArgs.SenderPublicKey)
+       xpubInfo, err := getXPubKeyInfo(s, accountInfo.AccountID, contractArgs.SenderPublicKey)
        if err != nil {
                return "", err
        }
 
        // build cancel contract transaction
-       builtTx, err := buildCancelContractTransaction(accountInfo, contractUTXOID, xpubInfo, contractValue)
+       builtTx, err := buildCancelContractTransaction(s, accountInfo, contractUTXOID, xpubInfo, contractValue)
        if err != nil {
                return "", err
        }
 
        // sign cancel contract transaction
-       signedTx, err := signTransaction(accountInfo.Password, builtTx)
+       signedTx, err := signTransaction(s, accountInfo.Password, builtTx)
        if err != nil {
                return "", err
        }
 
        // submit signed unlocked contract transaction
-       txID, err := submitTransaction(signedTx)
+       txID, err := submitTransaction(s, signedTx)
        if err != nil {
                return "", err
        }
index 67809df..28c1294 100644 (file)
@@ -8,14 +8,20 @@ import (
        "net/http"
 )
 
+type Server struct {
+       IP   string
+       Port string
+}
+
 type response struct {
        Status    string          `json:"status"`
        Data      json.RawMessage `json:"data"`
        ErrDetail string          `json:"error_detail"`
 }
 
-func request(url string, payload []byte, respData interface{}) error {
-       resp := &response{}
+func (s *Server) request(apiURL string, payload []byte, respData interface{}) error {
+       url := "http://" + s.IP + ":" + s.Port + "/" + apiURL
+       resp := new(response)
        if err := post(url, payload, resp); err != nil {
                return err
        }
index 5b7ab9e..0a71565 100644 (file)
@@ -41,7 +41,7 @@ var compileLockContractReq = `{
 }`
 
 // compileLockContract return contract control program
-func compileLockContract(contractArgs ContractArgs) (string, error) {
+func compileLockContract(s *Server, contractArgs ContractArgs) (string, error) {
        payload := []byte(fmt.Sprintf(compileLockContractReq,
                contractArgs.Asset,
                contractArgs.Amount,
@@ -49,7 +49,7 @@ func compileLockContract(contractArgs ContractArgs) (string, error) {
                contractArgs.CancelKey,
        ))
        res := new(compileLockContractResp)
-       if err := request(compileURL, payload, res); err != nil {
+       if err := s.request(compileURL, payload, res); err != nil {
                return "", err
        }
        return res.Program, nil
@@ -83,7 +83,7 @@ var buildLockTxReq = `{
 }`
 
 // buildLockTransaction build locked contract transaction.
-func buildLockTransaction(accountInfo AccountInfo, contractValue AssetAmount, contractControlProgram string) (interface{}, error) {
+func buildLockTransaction(s *Server, accountInfo AccountInfo, contractValue AssetAmount, contractControlProgram string) (interface{}, error) {
        payload := []byte(fmt.Sprintf(buildLockTxReq,
                accountInfo.AccountID,
                contractValue.Amount,
@@ -95,7 +95,7 @@ func buildLockTransaction(accountInfo AccountInfo, contractValue AssetAmount, co
                accountInfo.TxFee,
        ))
        res := new(interface{})
-       if err := request(buildTransactionURL, payload, res); err != nil {
+       if err := s.request(buildTransactionURL, payload, res); err != nil {
                return "", err
        }
        return res, nil
@@ -116,14 +116,14 @@ type signTxResp struct {
 }
 
 // signTransaction sign built contract transaction.
-func signTransaction(password string, transaction interface{}) (string, error) {
+func signTransaction(s *Server, password string, transaction interface{}) (string, error) {
        payload, err := json.Marshal(signTxReq{Password: password, Transaction: transaction})
        if err != nil {
                return "", err
        }
 
        res := new(signTxResp)
-       if err := request(signTransactionURL, payload, res); err != nil {
+       if err := s.request(signTransactionURL, payload, res); err != nil {
                return "", err
        }
 
@@ -143,14 +143,15 @@ type submitTxResp struct {
 }
 
 // submitTransaction submit raw singed contract transaction.
-func submitTransaction(rawTransaction string) (string, error) {
+func submitTransaction(s *Server, rawTransaction string) (string, error) {
+       fmt.Println("raw transaction:", rawTransaction)
        payload, err := json.Marshal(submitTxReq{RawTransaction: rawTransaction})
        if err != nil {
                return "", err
        }
 
        res := new(submitTxResp)
-       if err := request(submitTransactionURL, payload, res); err != nil {
+       if err := s.request(submitTransactionURL, payload, res); err != nil {
                return "", err
        }
 
@@ -171,14 +172,14 @@ type getContractUTXOIDResp struct {
 }
 
 // getContractUTXOID get contract UTXO ID by transaction ID and contract control program.
-func getContractUTXOID(transactionID, controlProgram string) (string, error) {
+func getContractUTXOID(s *Server, transactionID, controlProgram string) (string, error) {
        payload, err := json.Marshal(getContractUTXOIDReq{TransactionID: transactionID})
        if err != nil {
                return "", err
        }
 
        res := new(getContractUTXOIDResp)
-       if err := request(getTransactionURL, payload, res); err != nil {
+       if err := s.request(getTransactionURL, payload, res); err != nil {
                return "", err
        }
 
@@ -238,13 +239,13 @@ var buildUnlockContractTxReq = `{
 }`
 
 // buildUnlockContractTransaction build unlocked contract transaction.
-func buildUnlockContractTransaction(accountInfo AccountInfo, contractUTXOID string) (interface{}, error) {
-       program, contractValue, err := ListUnspentOutputs(contractUTXOID)
+func buildUnlockContractTransaction(s *Server, accountInfo AccountInfo, contractUTXOID string) (interface{}, error) {
+       program, contractValue, err := ListUnspentOutputs(s, contractUTXOID)
        if err != nil {
                return "", err
        }
 
-       contractArgs, err := decodeProgram(program)
+       contractArgs, err := decodeProgram(s, program)
        if err != nil {
                return "", err
        }
@@ -264,7 +265,7 @@ func buildUnlockContractTransaction(accountInfo AccountInfo, contractUTXOID stri
                accountInfo.Receiver,
        ))
        res := new(interface{})
-       if err := request(buildTransactionURL, payload, res); err != nil {
+       if err := s.request(buildTransactionURL, payload, res); err != nil {
                return "", err
        }
        return res, nil
@@ -282,7 +283,7 @@ type listUnspentOutputsReq struct {
        SmartContract bool   `json:"smart_contract"`
 }
 
-func ListUnspentOutputs(contractUTXOID string) (string, *AssetAmount, error) {
+func ListUnspentOutputs(s *Server, contractUTXOID string) (string, *AssetAmount, error) {
        payload, err := json.Marshal(listUnspentOutputsReq{
                UTXOID:        contractUTXOID,
                Unconfirmed:   true,
@@ -293,7 +294,7 @@ func ListUnspentOutputs(contractUTXOID string) (string, *AssetAmount, error) {
        }
 
        var res []listUnspentOutputsResp
-       if err := request(listUnspentOutputsURL, payload, &res); err != nil {
+       if err := s.request(listUnspentOutputsURL, payload, &res); err != nil {
                return "", nil, err
        }
 
@@ -315,14 +316,14 @@ type decodeProgramReq struct {
        Program string `json:"program"`
 }
 
-func decodeProgram(program string) (*ContractArgs, error) {
+func decodeProgram(s *Server, program string) (*ContractArgs, error) {
        payload, err := json.Marshal(decodeProgramReq{Program: program})
        if err != nil {
                return nil, err
        }
 
        res := new(decodeProgramResp)
-       if err := request(decodeProgramURL, payload, res); err != nil {
+       if err := s.request(decodeProgramURL, payload, res); err != nil {
                return nil, err
        }
 
@@ -376,14 +377,14 @@ type XPubKeyInfo struct {
        DerivationPath []string `json:"derivation_path"`
 }
 
-func getXPubKeyInfo(accountID, publicKey string) (*XPubKeyInfo, error) {
+func getXPubKeyInfo(s *Server, accountID, publicKey string) (*XPubKeyInfo, error) {
        payload, err := json.Marshal(listPublicKeysReq{AccountID: accountID})
        if err != nil {
                return nil, err
        }
 
        res := new(listPublicKeysResp)
-       if err := request(listPubkeysURL, payload, res); err != nil {
+       if err := s.request(listPubkeysURL, payload, res); err != nil {
                return nil, err
        }
 
@@ -435,7 +436,7 @@ var buildCancelContractTxReq = `{
     "base_transaction": null
 }`
 
-func buildCancelContractTransaction(accountInfo AccountInfo, contractUTXOID string, xpubKeyInfo *XPubKeyInfo, contractValue *AssetAmount) (interface{}, error) {
+func buildCancelContractTransaction(s *Server, accountInfo AccountInfo, contractUTXOID string, xpubKeyInfo *XPubKeyInfo, contractValue *AssetAmount) (interface{}, error) {
        xpubKeyInfoStr, err := json.Marshal(xpubKeyInfo)
        if err != nil {
                return "", err
@@ -450,40 +451,40 @@ func buildCancelContractTransaction(accountInfo AccountInfo, contractUTXOID stri
                accountInfo.Receiver,
        ))
        res := new(interface{})
-       if err := request(buildTransactionURL, payload, res); err != nil {
+       if err := s.request(buildTransactionURL, payload, res); err != nil {
                return "", err
        }
        return res, nil
 }
 
 // DeployContract deploy contract.
-func DeployContract(accountInfo AccountInfo, contractArgs ContractArgs, contractValue AssetAmount) (string, error) {
+func DeployContract(s *Server, accountInfo AccountInfo, contractArgs ContractArgs, contractValue AssetAmount) (string, error) {
        // compile locked contract
-       contractControlProgram, err := compileLockContract(contractArgs)
+       contractControlProgram, err := compileLockContract(s, contractArgs)
        if err != nil {
                return "", err
        }
 
        // build locked contract
-       txLocked, err := buildLockTransaction(accountInfo, contractValue, contractControlProgram)
+       txLocked, err := buildLockTransaction(s, accountInfo, contractValue, contractControlProgram)
        if err != nil {
                return "", err
        }
 
        // sign locked contract transaction
-       signedTransaction, err := signTransaction(accountInfo.Password, txLocked)
+       signedTransaction, err := signTransaction(s, accountInfo.Password, txLocked)
        if err != nil {
                return "", err
        }
 
        // submit signed transaction
-       txID, err := submitTransaction(signedTransaction)
+       txID, err := submitTransaction(s, signedTransaction)
        if err != nil {
                return "", err
        }
 
        // get contract output ID
-       contractUTXOID, err := getContractUTXOID(txID, contractControlProgram)
+       contractUTXOID, err := getContractUTXOID(s, txID, contractControlProgram)
        if err != nil {
                return "", err
        }
@@ -491,21 +492,21 @@ func DeployContract(accountInfo AccountInfo, contractArgs ContractArgs, contract
 }
 
 // CallContract call contract.
-func CallContract(accountInfo AccountInfo, contractUTXOID string) (string, error) {
+func CallContract(s *Server, accountInfo AccountInfo, contractUTXOID string) (string, error) {
        // build unlocked contract transaction
-       txUnlocked, err := buildUnlockContractTransaction(accountInfo, contractUTXOID)
+       txUnlocked, err := buildUnlockContractTransaction(s, accountInfo, contractUTXOID)
        if err != nil {
                return "", err
        }
 
        // sign unlocked contract transaction
-       signedTransaction, err := signTransaction(accountInfo.Password, txUnlocked)
+       signedTransaction, err := signTransaction(s, accountInfo.Password, txUnlocked)
        if err != nil {
                return "", err
        }
 
        // submit signed unlocked contract transaction
-       txID, err := submitTransaction(signedTransaction)
+       txID, err := submitTransaction(s, signedTransaction)
        if err != nil {
                return "", err
        }
@@ -513,39 +514,40 @@ func CallContract(accountInfo AccountInfo, contractUTXOID string) (string, error
        return txID, nil
 }
 
-func CancelTradeoffContract(accountInfo AccountInfo, contractUTXOID string) (string, error) {
+// CancelTradeoffContract cancel tradeoff contract.
+func CancelTradeoffContract(s *Server, accountInfo AccountInfo, contractUTXOID string) (string, error) {
        // get contract control program by contract UTXOID
-       contractControlProgram, contractValue, err := ListUnspentOutputs(contractUTXOID)
+       contractControlProgram, contractValue, err := ListUnspentOutputs(s, contractUTXOID)
        if err != nil {
                return "", err
        }
 
        // get public key by contract control program
-       contractArgs, err := decodeProgram(contractControlProgram)
+       contractArgs, err := decodeProgram(s, contractControlProgram)
        if err != nil {
                return "", err
        }
 
        // get public key path and root xpub by contract args
-       xpubInfo, err := getXPubKeyInfo(accountInfo.AccountID, contractArgs.CancelKey)
+       xpubInfo, err := getXPubKeyInfo(s, accountInfo.AccountID, contractArgs.CancelKey)
        if err != nil {
                return "", err
        }
 
        // build cancel contract transaction
-       builtTx, err := buildCancelContractTransaction(accountInfo, contractUTXOID, xpubInfo, contractValue)
+       builtTx, err := buildCancelContractTransaction(s, accountInfo, contractUTXOID, xpubInfo, contractValue)
        if err != nil {
                return "", err
        }
 
        // sign cancel contract transaction
-       signedTx, err := signTransaction(accountInfo.Password, builtTx)
+       signedTx, err := signTransaction(s, accountInfo.Password, builtTx)
        if err != nil {
                return "", err
        }
 
        // submit signed unlocked contract transaction
-       txID, err := submitTransaction(signedTx)
+       txID, err := submitTransaction(s, signedTx)
        if err != nil {
                return "", err
        }