예제 #1
0
func selectLanguage(maxRetry int) (int, error) {
	for i := 0; i < maxRetry; i++ {
		fmt.Printf("选择语言(select language):\r\n中文输入 %d 回车,For English press %d + Enter: ",
			publicdefine.L_Chinese+1, publicdefine.L_English+1)
		var input string
		_, err := fmt.Scanf("%s\n", &input)
		if err != nil {
			fmt.Println(err)
			fmt.Scanf("%s\n", &input)
			return -1, err
		}

		switch input {
		case strconv.Itoa(publicdefine.L_Chinese + 1):
			return publicdefine.L_Chinese, nil
		case strconv.Itoa(publicdefine.L_English + 1):
			return publicdefine.L_English, nil
		default:
			ConsoleColor.Println(ConsoleColor.C_RED, "语言选择错误(Language selection error)\r\n")
			// fmt.Println("语言选择错误(Language selection error)\r\n")
		}
	}
	ConsoleColor.Println(ConsoleColor.C_YELLOW, "ByeBye!\r\n")
	return -1, errors.New("Language selection error")
}
func (this *AccountInfoPayment) pay(src *publicdefine.StellarAccInfoDef,
	srcSeed, destAddr string, amount float64) *publicdefine.StellarPaymentInfo {
	payment := &publicdefine.StellarPaymentInfo{
		SrcInfo:    src,
		Amount:     amount,
		Destinaton: destAddr,
	}

	signed := payment.GetSigned(srcSeed)

	if len(signed) > 0 {
		data := "tx=" + url.QueryEscape(signed)
		// data, err := json.Marshal(map[string]interface{}{
		// 	"tx": signed,
		// })

		postUrl := publicdefine.STELLAR_DEFAULT_NETWORK + publicdefine.STELLAR_NETWORK_TRANSACTIONS
		ret, err := this.httppost_form(postUrl, data)
		// ret, err := this.httppost_json(postUrl, data)
		if err == nil {
			payment.PutResult(ret)
			return payment
		}
		ConsoleColor.Println(ConsoleColor.C_RED, err)
		// fmt.Println(err)
	}
	return nil
}
예제 #3
0
func (this *CreateAccount) execute(isSync bool) {
	var input string

	kp, err := keypair.Random()

	if err == nil {
		keyp := kp.(*keypair.Full)
		fmt.Printf("\r\n"+this.infoStrings[this.languageIndex][CA_INFO_SECRET_SEED]+" %s\r\n", keyp.Seed())
		fmt.Printf(this.infoStrings[this.languageIndex][CA_INFO_PUBLIC_ADDR]+" %s\r\n", keyp.Address())
		fmt.Printf("\r\n" + this.infoStrings[this.languageIndex][CA_INFO_MEMO_TEXT])
		fmt.Scanf("%s\n", &input)

		if input == "s" {
			err = this.savefile("account_info.txt", keyp.Seed(), keyp.Address())
			if err == nil {
				ConsoleColor.Printf(ConsoleColor.C_YELLOW,
					"\r\n"+this.infoStrings[this.languageIndex][CA_INFO_MEMO_SAVEFILE]+"\r\n\r\n", "account_info.txt")
				// fmt.Printf("\r\n"+this.infoStrings[this.languageIndex][CA_INFO_MEMO_SAVEFILE]+"\r\n\r\n", "account_info.txt")
			} else {
				ConsoleColor.Println(ConsoleColor.C_RED,
					"\r\n", this.infoStrings[this.languageIndex][CA_INFO_MEMO_SAVEFILE_ERR], "\r\n", err, "\r\n\r\n")
				// fmt.Println("\r\n", this.infoStrings[this.languageIndex][CA_INFO_MEMO_SAVEFILE_ERR], "\r\n", err, "\r\n\r\n")
			}
		}
	} else {
		fmt.Println(err.Error())
		fmt.Scanf("%s\n", &input)
	}

	if !isSync {
		this.ASyncChan <- 0
	}
}
func (this *AccountInfoOperationQuary) quary(addr string) {
	result := &publicdefine.StellarAccOperationQuary{
		QuaryCursor: "",
		IsEnd:       false,
	}
	for {
		ConsoleColor.Print(ConsoleColor.C_BLUE,
			this.infoStrings[this.languageIndex][AIOQ_INFO_QUARY_WAITING])

		reqUrl := fmt.Sprintf("%s%s/%s/%s?order=desc&limit=%d&cursor=%s", publicdefine.STELLAR_DEFAULT_NETWORK,
			publicdefine.STELLAR_NETWORK_ACCOUNTS, addr,
			publicdefine.STELLAR_NETWORK_OPERATIONS, 10, result.QuaryCursor)
		resMap, err := this.httpget(reqUrl)

		if err == nil {
			result.PutMapBody(resMap)
			this.PrintResult(result)
		} else {
			ConsoleColor.Println(ConsoleColor.C_RED, err)
			break
		}

		if result.IsEnd {
			break
		}
		if this.input_next() != "n" {
			break
		}
	}
}
예제 #5
0
func (this *MergeAccount) merging(srcInfo *publicdefine.StellarAccInfoDef, srcSeed, destAddr string) *publicdefine.StellarAccountMerge {
	mergeInfo := &publicdefine.StellarAccountMerge{
		SrcInfo:        srcInfo,
		DestPublicAddr: destAddr,
	}

	signed := mergeInfo.GetSigned(srcSeed)

	if len(signed) > 0 {
		data := "tx=" + url.QueryEscape(signed)
		// data, err := json.Marshal(map[string]interface{}{
		// 	"tx": signed,
		// })

		postUrl := publicdefine.STELLAR_DEFAULT_NETWORK + publicdefine.STELLAR_NETWORK_TRANSACTIONS
		ret, err := this.httppost_form(postUrl, data)
		if err == nil {
			mergeInfo.PutResult(ret)
			return mergeInfo
		}
		ConsoleColor.Println(ConsoleColor.C_RED, err)
		// fmt.Println(err)
	}
	return nil
}
예제 #6
0
func getProxyClient(proxyIP, proxyPort string, auth ...*proxy.Auth) *http.Client {

	proxyurl := proxyIP + ":" + proxyPort
	var author *proxy.Auth = nil
	if auth != nil {
		author = auth[0]
	}
	dialer, err := proxy.SOCKS5("tcp", proxyurl, author,
		&net.Dialer{
			Timeout:   30 * time.Second,
			KeepAlive: 30 * time.Second,
		},
	)

	if err != nil {
		ConsoleColor.Println(ConsoleColor.C_RED, " ** Socket5ProxyClient() Error\r\n\t", "proxy.SOCKS5: ", err)
		return nil
	}

	transport := &http.Transport{
		Proxy:               nil,
		Dial:                dialer.Dial,
		TLSHandshakeTimeout: 10 * time.Second,
	}

	return &http.Client{Transport: transport}
}
func (this *AccountInfoPayment) create_account(src *publicdefine.StellarAccInfoDef,
	srcSeed, destAddr string, amount float64) bool {

	if len(this.inputConfirm(destAddr)) == 0 {
		ConsoleColor.Println(ConsoleColor.C_RED,
			this.infoStrings[this.languageIndex][AIP_INFO_PAYMENT_ABORT])
		return false
	}

	ConsoleColor.Printf(ConsoleColor.C_BLUE,
		this.infoStrings[this.languageIndex][AIP_INFO_CREATE_DEST_ADDR], destAddr)

	if amount < 20 {
		ConsoleColor.Println(ConsoleColor.C_RED,
			this.infoStrings[this.languageIndex][AIP_INFO_PAYMENT_ABORT_LESS20])
		return false
	}

	cAcc := publicdefine.StellarAccountCreateInfo{
		SrcInfo:    src,
		Amount:     amount,
		Destinaton: destAddr,
	}

	signed := cAcc.GetSigned(srcSeed)

	if len(signed) > 0 {
		data := "tx=" + url.QueryEscape(signed)

		postUrl := publicdefine.STELLAR_DEFAULT_NETWORK + publicdefine.STELLAR_NETWORK_TRANSACTIONS
		ret, err := this.httppost_form(postUrl, data)
		// ret, err := this.httppost_json(postUrl, data)
		if err == nil {
			cAcc.PutResult(ret)
			if len(cAcc.ResultHash) > 0 {
				ConsoleColor.Println(ConsoleColor.C_BLUE,
					this.infoStrings[this.languageIndex][AIP_INFO_CREATE_DEST_ADDR_SUCCESS])
				return true
			}
		}
		ConsoleColor.Println(ConsoleColor.C_RED, err)
		// fmt.Println(err)
	}
	ConsoleColor.Println(ConsoleColor.C_RED,
		this.infoStrings[this.languageIndex][AIP_INFO_CHECK_TRANSACTION_ERROR])
	return false
}
예제 #8
0
func HttpGet(geturl string) (map[string]interface{}, error) {
	resp, err := http.Get(geturl)
	if err != nil {
		ConsoleColor.Println(ConsoleColor.C_RED, "ERROR [ HTTPGet() ]\r\n\t", "Get: ", err)
		return nil, err
	}
	return getResponseDecode(resp)
}
func (a *AccountInfoOperationQuary) PrintResult(r *publicdefine.StellarAccOperationQuary) {
	fmt.Print("\r")
	for i := 0; i < len(r.Records); i++ {
		ConsoleColor.Printf(ConsoleColor.C_GREEN,
			" %02d --------------------------------------------------------------------------------\r\n", i+1)

		ConsoleColor.Println(ConsoleColor.C_BLUE,
			r.Records[i].ToString(), "\r\n\r\n")
	}
}
예제 #10
0
func (this *MergeAccount) execute(isSync bool) {
	fmt.Println("")
	var isError bool = true
	var srcAddr, srcSeed, destAddr string
	for i := 0; i < 5; i++ {
		switch i {
		case 0:
			srcAddr = this.input_SrcAddr()
			isError = len(srcAddr) == 0
		case 1:
			srcSeed = this.input_SrcSeed()
			isError = len(srcSeed) == 0
		case 2:
			destAddr = this.input_DestAddr()
			isError = len(destAddr) == 0
		case 3:
			ConsoleColor.Printf(ConsoleColor.C_BLUE,
				this.infoStrings[this.languageIndex][MA_INFO_CONFIRM_INFOS], srcAddr, destAddr)
			// fmt.Printf(this.infoStrings[this.languageIndex][MA_INFO_CONFIRM_INFOS], srcAddr, destAddr)
			confirm := this.input_Confirm()
			if confirm != "yes" {
				i = 5
				ConsoleColor.Println(ConsoleColor.C_YELLOW,
					this.infoStrings[this.languageIndex][MA_INFO_OPERATION_BREAK])
				// fmt.Println(this.infoStrings[this.languageIndex][MA_INFO_OPERATION_BREAK])
			}
		case 4:
			this.beginMerge(srcAddr, srcSeed, destAddr)
		}

		if isError {
			ConsoleColor.Println(ConsoleColor.C_RED,
				this.infoStrings[this.languageIndex][MA_INFO_ADDR_FORMAT_ERR])
			// fmt.Println(this.infoStrings[this.languageIndex][MA_INFO_ADDR_FORMAT_ERR])
			break
		}
	}

	if !isSync {
		this.ASyncChan <- 0
	}
}
예제 #11
0
func HttpPost_form(address, data string) (map[string]interface{}, error) {

	resp, err := http.Post(address,
		"application/x-www-form-urlencoded", strings.NewReader(data))
	if err != nil {
		ConsoleColor.Println(ConsoleColor.C_RED, " ** HTTPPost() Error\r\n\t", "Post: ", err)
		return nil, err
	}

	return getResponseDecode(resp)
}
예제 #12
0
func HttpPost_json(address string, data []byte) (map[string]interface{}, error) {

	resp, err := http.Post(address,
		"application/json", strings.NewReader(string(data)))
	if err != nil {
		ConsoleColor.Println(ConsoleColor.C_RED, "ERROR [ HTTPPOST.json() ]\r\n\t", "Post: ", err)
		return nil, err
	}

	return getResponseDecode(resp)
}
예제 #13
0
func SPrintWarning(title, format string, v ...interface{}) (ret string) {
	ret = fmt.Sprintf("[ %s ]\r\n\t", title)
	if v == nil {
		ret += fmt.Sprintf(format)
	} else {
		ret += fmt.Sprintf(format, v)
	}
	if LogLevelConfig.Warning {
		ConsoleColor.Println(ConsoleColor.C_BLUE, time.Now().Format("2006-01-02 15:04:05")+" [W] "+ret)
	}
	return
}
예제 #14
0
func HttpProxyGet(pgUrl, proxyIP, proxyPort string, auth ...*proxy.Auth) (map[string]interface{}, error) {

	client := getProxyClient(proxyIP, proxyPort, auth...)

	resp, err := client.Get(pgUrl)
	if err != nil {
		ConsoleColor.Println(ConsoleColor.C_RED, " ** HTTPProxyGet() Error\r\n\t", "Get: ", err)
		return nil, err
	}

	return getResponseDecode(resp)
}
예제 #15
0
func (this *MergeAccount) beginMerge(srcAddr, srcSeed, destAddr string) {
	ConsoleColor.Println(ConsoleColor.C_BLUE,
		this.infoStrings[this.languageIndex][MA_INFO_CHECK_SOURCE_ACCOUNT])
	// fmt.Println(this.infoStrings[this.languageIndex][MA_INFO_CHECK_SOURCE_ACCOUNT])
	// 先检查账户是不是存在
	srcInfo := this.checkSourceAddr(srcAddr)
	if srcInfo == nil {
		return
	}
	// 检查Seed和Public Addr是不是匹配
	if this.checkSeed(srcSeed, srcAddr) == false {
		return
	}

	ConsoleColor.Println(ConsoleColor.C_BLUE,
		this.infoStrings[this.languageIndex][MA_INFO_MERGING])
	// fmt.Println(this.infoStrings[this.languageIndex][MA_INFO_MERGING])

	mergeInfo := this.merging(srcInfo, srcSeed, destAddr)
	if mergeInfo == nil {
		ConsoleColor.Println(ConsoleColor.C_RED,
			this.infoStrings[this.languageIndex][MA_INFO_MERGING_ERR])
		// fmt.Println(this.infoStrings[this.languageIndex][MA_INFO_MERGING_ERR])
		return
	}

	// 检查transaction hash是否生效
	if len(mergeInfo.ResultHash) == 0 {
		ConsoleColor.Println(ConsoleColor.C_RED,
			this.infoStrings[this.languageIndex][MA_INFO_MERGING_FIAL])
		// fmt.Println(this.infoStrings[this.languageIndex][MA_INFO_MERGING_FIAL])
		return
	}

	ConsoleColor.Println(ConsoleColor.C_GREEN,
		this.infoStrings[this.languageIndex][MA_INFO_MERGE_COMLETE])
	// fmt.Println(this.infoStrings[this.languageIndex][MA_INFO_MERGE_COMLETE])

}
예제 #16
0
func HttpProxyPost_form(address, data, proxyIP, proxyPort string, auth ...*proxy.Auth) (map[string]interface{}, error) {

	client := getProxyClient(proxyIP, proxyPort, auth...)

	resp, err := client.Post(address,
		"application/x-www-form-urlencoded", strings.NewReader(data))
	if err != nil {
		ConsoleColor.Println(ConsoleColor.C_RED, " ** HTTPProxyPost() Error\r\n\t", "Post: ", err)
		return nil, err
	}

	return getResponseDecode(resp)
}
예제 #17
0
func (this *MergeAccount) checkSeed(seed, srcAddr string) bool {
	pk, err := keypair.Parse(seed)
	if err == nil {
		if pk.Address() == srcAddr {
			return true
		}
		ConsoleColor.Printf(ConsoleColor.C_RED,
			this.infoStrings[this.languageIndex][MA_INFO_SEED_AND_ADDR_IS_NOT_PAIR]+"\r\n")
		// fmt.Printf(this.infoStrings[this.languageIndex][MA_INFO_SEED_AND_ADDR_IS_NOT_PAIR] + "\r\n")
	} else {
		ConsoleColor.Println(ConsoleColor.C_RED, err)
		// fmt.Println(err)
	}
	return false
}
예제 #18
0
func (this *AccountInfoBase) execute(isSync bool) {
	fmt.Println(this.infoStrings[this.languageIndex][AIB_INFO_INPUT_ADDR])

	var input string

	_, err := fmt.Scanf("%s\n", &input)
	if err == nil {
		addrs := strings.Split(input, ",")
		addrs = this.checkAddrs(addrs)
		fmt.Println("\r\n")
		length := len(addrs)
		for i := 0; i < length; i++ {
			ret, err := this.getAccountInfo(addrs[i])
			if err == nil {
				ConsoleColor.Println(ConsoleColor.C_GREEN, " ..........................................................................  ")

				ConsoleColor.Println(ConsoleColor.C_BLUE, ret.ToString(), "\r\n\r\n")
			}
		}
	}
	if !isSync {
		this.ASyncChan <- 0
	}
}
예제 #19
0
func getResponseDecode(resp *http.Response) (map[string]interface{}, error) {

	if resp == nil {
		return nil, errors.New("http.Response is nil!")
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		ConsoleColor.Println(ConsoleColor.C_RED, " ** HTTP Response ERROR\r\n\t", "ReadAll: ", err)
		return nil, err
	}

	// fmt.Println("[ HTTPGet().body ]\r\n\t", string(body))

	result := make(map[string]interface{})
	err = json.Unmarshal(body, &result)
	if err != nil {
		ConsoleColor.Println(ConsoleColor.C_RED, " ** HTTP Response ERROR\r\n\t", "Unmarshal: ", err)
		return nil, err
	}

	return result, nil
}
예제 #20
0
func (this *AccountInfoPayment) checkPublicAddrExist(addr string) *publicdefine.StellarAccInfoDef {
	reqUrl := publicdefine.STELLAR_DEFAULT_NETWORK + publicdefine.STELLAR_NETWORK_ACCOUNTS + "/" + addr
	resMap, err := this.httpget(reqUrl)

	if err == nil {
		ret := &publicdefine.StellarAccInfoDef{}
		ret.PutMapBody(addr, resMap)
		if ret.IsExist() {
			return ret
		}
	} else {
		ConsoleColor.Println(ConsoleColor.C_RED, err)
		// fmt.Println(err)
	}
	return nil
}
예제 #21
0
func main() {
	// publicdefine.CurrProxyInfo = &publicdefine.UseProxyInfo{}
	// publicdefine.CurrProxyInfo.Enabled = false
	setCurrentArgs(os.Args)

	currLanguage, errMsg := selectLanguage(3)
	if errMsg == nil {
		menu.MainMenuInstace.SetLanguageType(currLanguage)
		networkTitle := []string{
			publicdefine.L_Chinese: " 当前连接网络为 : ",
			publicdefine.L_English: " Current connecting network is : ",
		}
		ConsoleColor.Println(ConsoleColor.C_YELLOW,
			"\r\n", networkTitle[currLanguage], publicdefine.GetDefaultNWString(), "\r\n")
		menu.MainMenuInstace.ExecuteFunc(true)
	}
}
예제 #22
0
func (this *AccountInfoPayment) beginSend(srcAddr, srcSeed, destAddr string, amount float64) {
	ConsoleColor.Println(ConsoleColor.C_BLUE,
		this.infoStrings[this.languageIndex][AIP_INFO_CHECK_SOURCE_ACCOUNT])
	// fmt.Println(this.infoStrings[this.languageIndex][AIP_INFO_CHECK_SOURCE_ACCOUNT])
	// 先检查账户是不是存在并且余额是不是够
	ret := this.checkSourceAddr(srcAddr, amount)
	if ret == nil {
		return
	}
	// 检查Seed和Public Addr是不是匹配
	if this.checkSeed(srcSeed, srcAddr) == false {
		return
	}

	// 检查目标账户是否存在,不存在需要先建立账户
	if this.checkPublicAddrExist(destAddr) == nil {
		// 如果目标账户不存在,需要按照建立账户的方式进行
		if this.create_account(ret, srcSeed, destAddr, amount) == false {
			return
		}
	} else {
		// 如果已经存在,就按照正常流程付款
		ConsoleColor.Println(ConsoleColor.C_BLUE,
			this.infoStrings[this.languageIndex][AIP_INFO_SENDING])
		// fmt.Println(this.infoStrings[this.languageIndex][AIP_INFO_SENDING])

		// 签名支付
		payment := this.pay(ret, srcSeed, destAddr, amount)
		if payment == nil {
			ConsoleColor.Println(ConsoleColor.C_RED,
				this.infoStrings[this.languageIndex][AIP_INFO_SEND_ERROR])
			// fmt.Println(this.infoStrings[this.languageIndex][AIP_INFO_SEND_ERROR])
			return
		}

		ConsoleColor.Println(ConsoleColor.C_BLUE,
			this.infoStrings[this.languageIndex][AIP_INFO_SEND_COMPLETE])
		// fmt.Println(this.infoStrings[this.languageIndex][AIP_INFO_SEND_COMPLETE])

		// 检查transaction hash是否生效
		if len(payment.ResultHash) == 0 {
			ConsoleColor.Println(ConsoleColor.C_RED,
				this.infoStrings[this.languageIndex][AIP_INFO_CHECK_TRANSACTION_ERROR])
			// fmt.Println(this.infoStrings[this.languageIndex][AIP_INFO_CHECK_TRANSACTION_ERROR])
			return
		}
	}

	ConsoleColor.Println(ConsoleColor.C_GREEN,
		this.infoStrings[this.languageIndex][AIP_INFO_CHECK_TRANSACTION_SUCCESS])
	// fmt.Println(this.infoStrings[this.languageIndex][AIP_INFO_CHECK_TRANSACTION_SUCCESS])
}
예제 #23
0
func (this *MergeAccount) checkSourceAddr(addr string) *publicdefine.StellarAccInfoDef {
	reqUrl := publicdefine.STELLAR_DEFAULT_NETWORK + publicdefine.STELLAR_NETWORK_ACCOUNTS + "/" + addr
	resMap, err := publicdefine.HttpGet(reqUrl)

	if err == nil {
		ret := &publicdefine.StellarAccInfoDef{}
		ret.PutMapBody(addr, resMap)
		if ret.IsExist() {
			return ret
		} else {
			ConsoleColor.Printf(ConsoleColor.C_RED,
				this.infoStrings[this.languageIndex][MA_INFO_SOURCE_ACCOUNT_NOT_EXIST]+"\r\n", addr)
			// fmt.Printf(this.infoStrings[this.languageIndex][MA_INFO_SOURCE_ACCOUNT_NOT_EXIST]+"\r\n", addr)
		}
	} else {
		ConsoleColor.Println(ConsoleColor.C_RED, err)
		// fmt.Println(err)
	}
	return nil
}
예제 #24
0
func (this *AccountInfoPayment) execute(isSync bool) {
	fmt.Println("")
	var isError bool = true
	var srcAddr, srcSeed, destAddr string
	var amount float64

	for i := 0; i < 5; i++ {
		switch i {
		case 0:
			srcAddr = this.input_SrcAddr()
			isError = len(srcAddr) == 0
		case 1:
			srcSeed = this.input_SrcSeed()
			isError = len(srcSeed) == 0
		case 2:
			destAddr = this.input_DestAddr()
			isError = len(destAddr) == 0
		case 3:
			amount = this.input_Amount()
			isError = amount == 0
		case 4:
			this.beginSend(srcAddr, srcSeed, destAddr, amount)
		}

		if isError {
			ConsoleColor.Println(ConsoleColor.C_RED,
				this.infoStrings[this.languageIndex][AIP_INFO_ADDR_FORMAT_ERR])
			// fmt.Println(this.infoStrings[this.languageIndex][AIP_INFO_ADDR_FORMAT_ERR])
			break
		}
	}

	if !isSync {
		this.ASyncChan <- 0
	}
}