func GenerateECAddressStringFromHumanReadablePrivateKey(name string, privateKey string) (string, error) {
	addr, err := GenerateECAddressFromHumanReadablePrivateKey(name, privateKey)
	if err != nil {
		return "", err
	}
	return factoid.ConvertECAddressToUserStr(addr), nil
}
func DumpTransactionsJSON(addresses [][]byte) ([]byte, error) {
	if err := refresh(); err != nil {
		return nil, err
	}

	var transactions []fct.ITransaction

	for i, fb := range FactoidBlocks {
		for _, t := range fb.GetTransactions() {
			t.SetBlockHeight(i)
			t.GetSigHash()
			for _, input := range t.GetInputs() {
				input.SetUserAddress(fct.ConvertFctAddressToUserStr(input.GetAddress()))
			}
			for _, output := range t.GetOutputs() {
				output.SetUserAddress(fct.ConvertFctAddressToUserStr(output.GetAddress()))
			}
			for _, ecoutput := range t.GetECOutputs() {
				ecoutput.SetUserAddress(fct.ConvertECAddressToUserStr(ecoutput.GetAddress()))
			}
			prtTrans := filtertransaction(t, addresses)
			if prtTrans {
				transactions = append(transactions, t)
			}
		}
	}

	ret, err := json.Marshal(transactions)

	return ret, err
}
func GenerateECAddressString(name string) (string, error) {
	addr, err := GenerateECAddress(name)
	if err != nil {
		return "", err
	}
	return factoid.ConvertECAddressToUserStr(addr), nil
}
func GetAddresses() []byte {
	values := Wallet.GetAddresses()

	ecKeys := make([]string, 0, len(values))
	fctKeys := make([]string, 0, len(values))
	ecBalances := make([]string, 0, len(values))
	fctBalances := make([]string, 0, len(values))
	fctAddresses := make([]string, 0, len(values))
	ecAddresses := make([]string, 0, len(values))

	var maxlen int
	for _, we := range values {
		if len(we.GetName()) > maxlen {
			maxlen = len(we.GetName())
		}
		var adr string
		if we.GetType() == "ec" {
			address, err := we.GetAddress()
			if err != nil {
				continue
			}
			adr = fct.ConvertECAddressToUserStr(address)
			ecAddresses = append(ecAddresses, adr)
			ecKeys = append(ecKeys, string(we.GetName()))
			bal, _ := ECBalance(adr)
			ecBalances = append(ecBalances, strconv.FormatInt(bal, 10))
		} else {
			address, err := we.GetAddress()
			if err != nil {
				continue
			}
			adr = fct.ConvertFctAddressToUserStr(address)
			fctAddresses = append(fctAddresses, adr)
			fctKeys = append(fctKeys, string(we.GetName()))
			bal, _ := FctBalance(adr)
			sbal := fct.ConvertDecimal(uint64(bal))
			fctBalances = append(fctBalances, sbal)
		}
	}
	var out bytes.Buffer
	if len(fctKeys) > 0 {
		out.WriteString("\n  Factoid Addresses\n\n")
	}
	fstr := fmt.Sprintf("%s%vs    %s38s %s14s\n", "%", maxlen+4, "%", "%")
	for i, key := range fctKeys {
		str := fmt.Sprintf(fstr, key, fctAddresses[i], fctBalances[i])
		out.WriteString(str)
	}
	if len(ecKeys) > 0 {
		out.WriteString("\n  Entry Credit Addresses\n\n")
	}
	for i, key := range ecKeys {
		str := fmt.Sprintf(fstr, key, ecAddresses[i], ecBalances[i])
		out.WriteString(str)
	}

	return out.Bytes()
}
func (AddECOutput) Execute(state IState, args []string) error {

	if len(args) != 4 {
		return fmt.Errorf("Invalid Parameters")
	}
	key := args[1]
	adr := args[2]
	amt := args[3]

	ib := state.GetFS().GetDB().GetRaw([]byte(fct.DB_BUILD_TRANS), []byte(key))
	trans, ok := ib.(fct.ITransaction)
	if ib == nil || !ok {
		return fmt.Errorf("Unknown Transaction")
	}

	var addr fct.IAddress
	if !fct.ValidateECUserStr(adr) {
		if len(adr) != 64 {
			if len(adr) > 32 {
				return fmt.Errorf("Invalid Address or Name.  Check that you entered it correctly.")
			}

			we := state.GetFS().GetDB().GetRaw([]byte(fct.W_NAME), []byte(adr))

			if we != nil {
				we2 := we.(wallet.IWalletEntry)
				addr, _ = we2.GetAddress()
				adr = hex.EncodeToString(addr.Bytes())
			} else {
				return fmt.Errorf("Name is undefined.")
			}
		} else {
			if badHexChar.FindStringIndex(adr) != nil {
				return fmt.Errorf("Looks like an invalid hex address. Check that you entered it correctly.")
			}
		}
	} else {
		addr = fct.NewAddress(fct.ConvertUserStrToAddress(adr))
	}
	amount, _ := fct.ConvertFixedPoint(amt)
	bamount, _ := strconv.ParseInt(amount, 10, 64)
	err := state.GetFS().GetWallet().AddECOutput(trans, addr, uint64(bamount))
	if err != nil {
		return err
	}

	fmt.Println("Added Output of ", amt, " to be paid to ", args[2],
		fct.ConvertECAddressToUserStr(addr))

	return nil
}
func GetAddresses() (*Addresses, error) {
	addresses := Wallet.GetAddresses()

	answer := new(Addresses)
	for _, we := range addresses {
		tmp := Address{}
		tmp.Type = we.GetType()
		if we.GetType() == "ec" {
			address, err := we.GetAddress()
			if err != nil {
				return nil, err
			}

			tmp.Address = factoid.ConvertECAddressToUserStr(address)
			tmp.Name = string(we.GetName())
			bal, err := Wallet.ECBalance(tmp.Address)
			if err != nil {
				return nil, err
			}
			tmp.Balance = float64(bal)
			answer.ECAddresses = append(answer.ECAddresses, tmp)
		} else {
			address, err := we.GetAddress()
			if err != nil {
				return nil, err
			}

			tmp.Address = factoid.ConvertFctAddressToUserStr(address)
			tmp.Name = string(we.GetName())
			bal, err := Wallet.FactoidBalance(tmp.Address)
			if err != nil {
				return nil, err
			}
			tmp.Balance = factoid.ConvertDecimalToFloat(uint64(bal))
			answer.FactoidAddresses = append(answer.FactoidAddresses, tmp)
		}
	}
	return answer, nil
}
Exemple #7
0
// ExportKey <name>
func (ExportKey) Execute(state IState, args []string) error {

	if len(args) != 2 {
		return fmt.Errorf("Invalid Parameters")
	}
	name := args[1]

	weblk := state.GetFS().GetDB().GetRaw([]byte(fct.W_NAME), []byte(name))
	if weblk == nil {
		return fmt.Errorf("Unknown address.  Check that you spelled the name correctly")
	}
	we := weblk.(wallet.IWalletEntry)
	public, err := we.GetAddress()
	if err != nil {
		return err
	}
	private := we.GetPrivKey(0)
	adrtype := we.GetType()

	binPublic := hex.EncodeToString(public.Bytes())
	binPrivate := hex.EncodeToString(private[:32])
	var usrPublic, usrPrivate string
	if adrtype == "fct" {
		usrPublic = fct.ConvertFctAddressToUserStr(fct.NewAddress(public.Bytes()))
		usrPrivate = fct.ConvertFctPrivateToUserStr(fct.NewAddress(private[:32]))
	} else {
		usrPublic = fct.ConvertECAddressToUserStr(fct.NewAddress(public.Bytes()))
		usrPrivate = fct.ConvertECPrivateToUserStr(fct.NewAddress(private[:32]))
	}

	fmt.Println("Private Key:")
	fmt.Println("  ", usrPrivate)
	fmt.Println("  ", binPrivate)
	fmt.Println("Public Key:")
	fmt.Println("  ", usrPublic)
	fmt.Println("  ", binPublic)
	return nil
}
Exemple #8
0
func GenAddress(state IState, adrType string, key string) error {
	validErr := ValidName(key)
	if validErr != nil {
		return validErr
	}
	switch strings.ToLower(adrType) {
	case "ec":
		adr, err := state.GetFS().GetWallet().GenerateECAddress([]byte(key))
		if err != nil {
			return err
		}
		fmt.Println(key, "=", fct.ConvertECAddressToUserStr(adr))
	case "fct":
		adr, err := state.GetFS().GetWallet().GenerateFctAddress([]byte(key), 1, 1)
		if err != nil {
			return err
		}
		fmt.Println(key, "=", fct.ConvertFctAddressToUserStr(adr))
	default:
		return fmt.Errorf("Invalid Parameters")
	}
	return nil
}
func DumpTransactionsJSON(addresses [][]byte, start int, end int) ([]byte, error) {
	if err := refresh(); err != nil {
		return nil, err
	}

	if end == 0 {
		end = 1000000000 // No end, set to big number.
	}

	var transactions []fct.ITransaction

	for i, fb := range FactoidBlocks {
		if fb.GetDBHeight() >= uint32(start) && fb.GetDBHeight() <= uint32(end) {
			for _, t := range fb.GetTransactions() {
				t.SetBlockHeight(i)
				t.GetSigHash()
				for _, input := range t.GetInputs() {
					input.SetUserAddress(fct.ConvertFctAddressToUserStr(input.GetAddress()))
				}
				for _, output := range t.GetOutputs() {
					output.SetUserAddress(fct.ConvertFctAddressToUserStr(output.GetAddress()))
				}
				for _, ecoutput := range t.GetECOutputs() {
					ecoutput.SetUserAddress(fct.ConvertECAddressToUserStr(ecoutput.GetAddress()))
				}
				prtTrans := filtertransaction(t, addresses)
				if prtTrans {
					transactions = append(transactions, t)
				}
			}
		}
	}

	ret, err := json.Marshal(transactions)

	return ret, err
}
Exemple #10
0
func GetBalances(state IState) []byte {
	keys, values := state.GetFS().GetDB().GetKeysValues([]byte(fct.W_NAME))

	ecKeys := make([]string, 0, len(keys))
	fctKeys := make([]string, 0, len(keys))
	ecBalances := make([]string, 0, len(keys))
	fctBalances := make([]string, 0, len(keys))
	fctAddresses := make([]string, 0, len(keys))
	ecAddresses := make([]string, 0, len(keys))

	var maxlen int
	var connect = true
	for i, k := range keys {
		if len(k) > maxlen {
			maxlen = len(k)
		}
		we, ok := values[i].(wallet.IWalletEntry)
		if !ok {
			panic("Get Addresses finds the database corrupt.  Shouldn't happen")
		}
		var adr string
		if we.GetType() == "ec" {
			address, err := we.GetAddress()
			if err != nil {
				continue
			}
			adr = fct.ConvertECAddressToUserStr(address)
			ecAddresses = append(ecAddresses, adr)
			ecKeys = append(ecKeys, string(k))
			bal, err := ECBalance(state, adr)
			if err != nil {
				connect = false
			}
			if connect {
				ecBalances = append(ecBalances, strconv.FormatInt(bal, 10))
			} else {
				ecBalances = append(ecBalances, "-")
			}
		} else {
			address, err := we.GetAddress()
			if err != nil {
				continue
			}
			adr = fct.ConvertFctAddressToUserStr(address)
			fctAddresses = append(fctAddresses, adr)
			fctKeys = append(fctKeys, string(k))
			bal, err := FctBalance(state, adr)
			if err != nil {
				connect = false
			}
			sbal := fct.ConvertDecimal(uint64(bal))
			if connect {
				fctBalances = append(fctBalances, sbal)
			} else {
				fctBalances = append(fctBalances, "-")
			}
		}
	}
	var out bytes.Buffer
	if len(fctKeys) > 0 {
		out.WriteString("\n  Factoid Addresses\n\n")
	}
	fstr := fmt.Sprintf("%s%vs    %s38s %s14s\n", "%", maxlen+4, "%", "%")
	for i, key := range fctKeys {
		str := fmt.Sprintf(fstr, key, fctAddresses[i], fctBalances[i])
		out.WriteString(str)
	}
	if len(ecKeys) > 0 {
		out.WriteString("\n  Entry Credit Addresses\n\n")
	}
	for i, key := range ecKeys {
		str := fmt.Sprintf(fstr, key, ecAddresses[i], ecBalances[i])
		out.WriteString(str)
	}
	if !connect {
		out.WriteString("Balances are unavailable;  Wallet is offline\n")
	}
	return out.Bytes()
}
Exemple #11
0
func loadTx(w http.ResponseWriter, r *http.Request) {
	ajax_post_data := r.FormValue("ajax_post_data")
	txName := r.FormValue("txName")
	if len(ajax_post_data) > 0 {
		loadFeedString := []string{"Import", string(txName), string(ajax_post_data)}
		loadErr := myState.Execute(loadFeedString)
		if loadErr != nil {
			w.Write([]byte(loadErr.Error()))
			return
		}
	}

	ib := myState.GetFS().GetDB().GetRaw([]byte(fct.DB_BUILD_TRANS), []byte(txName))
	jib, jerr := json.Marshal(ib)
	var dat map[string]interface{}

	if err := json.Unmarshal(jib, &dat); err != nil {
		panic(err)
	}
	//fmt.Printf("%+v", dat)
	if dat["Inputs"] != nil {
		inputObjects := dat["Inputs"].([]interface{})
		myInps := make([]inputList, len(inputObjects))
		if len(inputObjects) > 0 {
			currInput := inputObjects[0].(map[string]interface{})
			for i := range inputObjects {
				currInput = inputObjects[i].(map[string]interface{})
				decodeAddr, hexErr := hex.DecodeString(currInput["Address"].(string))
				if hexErr != nil {
					fmt.Println("Error: " + hexErr.Error())
					return
				}
				myInps[i].InputAddress = fct.ConvertFctAddressToUserStr(fct.NewAddress(decodeAddr))
				myInps[i].InputSize = currInput["Amount"].(float64)
			}
		}
		loo := 0
		loeco := 0
		var outputObjects []interface{}
		var outputECObjects []interface{}
		if dat["Outputs"] != nil {
			outputObjects = dat["Outputs"].([]interface{})
			loo = len(outputObjects)
		}
		if dat["OutECs"] != nil {
			outputECObjects = dat["OutECs"].([]interface{})
			loeco = len(outputECObjects)
		}
		myOuts := make([]outputList, (loo + loeco))
		if outputObjects != nil {
			if loo > 0 {
				currOutput := outputObjects[0].(map[string]interface{})
				for i := range outputObjects {
					currOutput = outputObjects[i].(map[string]interface{})
					decodeAddr, hexErr := hex.DecodeString(currOutput["Address"].(string))
					if hexErr != nil {
						fmt.Println("Error: " + hexErr.Error())
						return
					}
					myOuts[i].OutputAddress = fct.ConvertFctAddressToUserStr(fct.NewAddress(decodeAddr))
					myOuts[i].OutputSize = currOutput["Amount"].(float64)
					myOuts[i].OutputType = "fct"
				}
			}
		}

		if outputECObjects != nil {
			if loeco > 0 {
				currOutput := outputECObjects[0].(map[string]interface{})
				for i := range outputECObjects {
					currOutput = outputECObjects[i].(map[string]interface{})
					decodeAddr, hexErr := hex.DecodeString(currOutput["Address"].(string))
					if hexErr != nil {
						fmt.Println("Error: " + hexErr.Error())
						return
					}
					myOuts[(i + len(outputObjects))].OutputAddress = fct.ConvertECAddressToUserStr(fct.NewAddress(decodeAddr))
					myOuts[(i + len(outputObjects))].OutputSize = currOutput["Amount"].(float64)
					myOuts[(i + len(outputObjects))].OutputType = "ec"
				}
			}
		}

		returnTran := pseudoTransaction{
			Inputs:  myInps,
			Outputs: myOuts,
		}

		lastTry, jayErr := json.Marshal(returnTran)
		if jayErr != nil {
			w.Write([]byte(jerr.Error()))
			return
		}

		if jerr != nil {
			w.Write([]byte(jerr.Error()))
			return
		}
		w.Write([]byte(lastTry))

	}
}