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")
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 = ""
)
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) {
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)
}
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) {
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)
}
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) {
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)
}
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) {
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)
}
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) {
}
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)
}
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) {
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)
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 {
]
}`
-func compileLockHTLCContract(contractArgs HTLCContractArgs) (string, error) {
+func compileLockHTLCContract(s *Server, contractArgs HTLCContractArgs) (string, error) {
payload := []byte(fmt.Sprintf(compileLockHTLCContractReq,
contractArgs.SenderPublicKey,
contractArgs.RecipientPublicKey,
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
"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,
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
"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,
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
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
}
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
}
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
}
return "", err
}
- addressInfos, err := listAddresses(accountID)
+ addressInfos, err := listAddresses(s, accountID)
if err != nil {
return "", err
}
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,
}
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
}
}`
-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,
account.TxFee,
))
res := new(signTxResp)
- if err := request(signTransactionURL, payload, res); err != nil {
+ if err := s.request(signTransactionURL, payload, res); err != nil {
return "", err
}
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
}
}
// 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
}
}
// 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
}
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
}
}
// 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
}
"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
}
}`
// 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,
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
}`
// 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,
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
}
// 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
}
}
// 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
}
}
// 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
}
}`
// 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
}
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
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,
}
var res []listUnspentOutputsResp
- if err := request(listUnspentOutputsURL, payload, &res); err != nil {
+ if err := s.request(listUnspentOutputsURL, payload, &res); err != nil {
return "", nil, err
}
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
}
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
}
"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
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
}
}
// 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
}
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
}