Пример #1
0
func Test_Input_Amounts(test *testing.T) {

	str, err := fct.ConvertFixedPoint("10")
	if err != nil {
		test.Fail()
	}
	if str != "1000000000" {
		test.Fail()
	}

	str, err = fct.ConvertFixedPoint("10.08")
	if err != nil {
		test.Fail()
	}
	if str != "1008000000" {
		test.Fail()
	}

	str, err = fct.ConvertFixedPoint("10.08000001")
	if err != nil {
		test.Fail()
	}
	if str != "1008000001" {
		test.Fail()
	}

}
Пример #2
0
func FactoidAddInput(key, name, amount string) {
	msg, valid := ValidateKey(key)
	if !valid {
		fmt.Println(msg)
		os.Exit(1)
	}

	amt, err := factoid.ConvertFixedPoint(amount)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	ramt, err := strconv.ParseInt(amt, 10, 64)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	_, err = factoid.ValidateAmounts(uint64(ramt))
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	str := fmt.Sprintf("http://%s/v1/factoid-add-input/?key=%s&name=%s&amount=%s",
		serverfactoid, key, name, amt)
	SendCommand(false, str)

	return
}
Пример #3
0
func (AddInput) 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: " + key)
	}

	var addr fct.IAddress
	if !fct.ValidateFUserStr(adr) {
		if len(adr) != 64 {
			if len(adr) > 32 {
				return fmt.Errorf("Invalid Name. Check the address or name for proper entry.", len(adr))
			}

			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 {
			badr, err := hex.DecodeString(adr)
			if err != nil {
				return fmt.Errorf("Looks like an Invalid hex address.  Check that you entered it correctly")
			}
			addr = fct.NewAddress(badr)
		}
	} else {
		fmt.Printf("adr: %x\n", adr)
		addr = fct.NewAddress(fct.ConvertUserStrToAddress(adr))
	}
	amount, _ := fct.ConvertFixedPoint(amt)
	bamount, _ := strconv.ParseInt(amount, 10, 64)
	err := state.GetFS().GetWallet().AddInput(trans, addr, uint64(bamount))

	if err != nil {
		return err
	}

	fmt.Println("Added Input of ", amt, " to be paid from ", args[2],
		fct.ConvertFctAddressToUserStr(addr))
	return nil
}
Пример #4
0
func SilentAddInput(txKey string, inputAddress string, inputSize string) error {
	ib := myState.GetFS().GetDB().GetRaw([]byte(fct.DB_BUILD_TRANS), []byte(txKey))
	trans, ok := ib.(fct.ITransaction)
	if ib == nil || !ok {
		return fmt.Errorf("Unknown Transaction: " + txKey)
	}

	var addr fct.IAddress
	if !fct.ValidateFUserStr(inputAddress) {
		if len(inputAddress) != 64 {
			if len(inputAddress) > 32 {
				return fmt.Errorf("Invalid Name. Check the address or name for proper entry.", len(inputAddress))
			}

			we := myState.GetFS().GetDB().GetRaw([]byte(fct.W_NAME), []byte(inputAddress))

			if we != nil {
				we2 := we.(wallet.IWalletEntry)
				addr, _ = we2.GetAddress()
				inputAddress = hex.EncodeToString(addr.Bytes())
			} else {
				return fmt.Errorf("Name is undefined.")
			}
		} else {
			badr, err := hex.DecodeString(inputAddress)
			if err != nil {
				return fmt.Errorf("Looks like an Invalid hex address.  Check that you entered it correctly.")
			}
			addr = fct.NewAddress(badr)
		}
	} else {
		//fmt.Printf("adr: %x\n",adr)
		addr = fct.NewAddress(fct.ConvertUserStrToAddress(inputAddress))
	}
	amount, _ := fct.ConvertFixedPoint(inputSize)
	bamount, _ := strconv.ParseInt(amount, 10, 64)
	err := myState.GetFS().GetWallet().AddInput(trans, addr, uint64(bamount))

	if err != nil {
		return err
	}
	return nil
}
Пример #5
0
func fctaddoutput(args []string) {
	os.Args = args
	flag.Parse()
	args = flag.Args()
	if len(args) < 3 {
		fmt.Println("Expecting a 1) transaction key, 2) an Address or Address name, and 3) an amount.")
		os.Exit(1)
	}
	// localhost:8089/v1/factoid-add-input/?key=<key>&name=<name or address>&amount=<amount>

	msg, valid := ValidateKey(args[0])
	if !valid {
		fmt.Println(msg)
		os.Exit(1)
	}

	amt, err := fct.ConvertFixedPoint(args[2])
	if err != nil {
		fmt.Println("Invalid format for a number: ", args[2])
		man("addoutput")
		os.Exit(1)
	}

	ramt, err := strconv.ParseInt(amt, 10, 64)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	_, err = fct.ValidateAmounts(uint64(ramt))
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	str := fmt.Sprintf("http://%s/v1/factoid-add-output/?key=%s&name=%s&amount=%s",
		serverFct, args[0], args[1], amt)
	postCmd(str)

	return
}
Пример #6
0
func SilentAddECOutput(txKey string, outputAddress string, outputSize string) error {
	ib := myState.GetFS().GetDB().GetRaw([]byte(fct.DB_BUILD_TRANS), []byte(txKey))
	trans, ok := ib.(fct.ITransaction)
	if ib == nil || !ok {
		return fmt.Errorf("Unknown Transaction")
	}

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

			we := myState.GetFS().GetDB().GetRaw([]byte(fct.W_NAME), []byte(outputAddress))

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

	return nil
}
Пример #7
0
func (AddFee) Execute(state IState, args []string) (err error) {

	if len(args) != 3 && len(args) != 4 {
		return fmt.Errorf("Invalid Parameters")
	}
	key := args[1]
	adr := args[2]
	rate := int64(0)
	if len(args) == 4 {
		srate, err := fct.ConvertFixedPoint(args[3])
		if err != nil {
			return fmt.Errorf("Could not parse exchange rate: %v", err)
		}
		rate, err = strconv.ParseInt(srate, 10, 64)
	} else {
		if rate, err = GetRate(state); err != nil {
			return fmt.Errorf("Could not reach the server to get the exchange rate")
		}
	}

	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.ValidateFUserStr(adr) {
		addr = fct.NewAddress(fct.ConvertUserStrToAddress(adr))
	} else if Utility.IsValidHexAddress(adr) {
		badr, _ := hex.DecodeString(adr)
		addr = fct.NewAddress(badr)
	} else if Utility.IsValidNickname(adr) {
		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.")
		}
	}

	fee, err := trans.CalculateFee(uint64(rate))
	var tin, tout, tec uint64
	if tin, err = trans.TotalInputs(); err != nil {
		return err
	}
	if tout, err = trans.TotalOutputs(); err != nil {
		return err
	}
	if tec, err = trans.TotalECs(); err != nil {
		return err
	}

	if tin != tout+tec {
		msg := fmt.Sprintf("%s Total Inputs\n", fct.ConvertDecimal(tin))
		msg += fmt.Sprintf("%s Total Outputs and Entry Credits\n", fct.ConvertDecimal(tout+tec))
		msg += fmt.Sprintf("\nThe Inputs must match the outputs to use AddFee to add the fee to an input")
		return fmt.Errorf(msg)
	}

	for _, input := range trans.GetInputs() {
		if bytes.Equal(input.GetAddress().Bytes(), addr.Bytes()) {
			input.SetAmount(input.GetAmount() + fee)
			fmt.Printf("Added fee of %v\n", strings.TrimSpace(fct.ConvertDecimal(fee)))
			break
		}
	}

	return nil
}