func VerifyAddressType(address string) (string, bool) { var resp string = "Not a Valid Factoid Address" var pass bool = false if strings.HasPrefix(address, "FA") { if factoid.ValidateFUserStr(address) { resp = "Factoid - Public" pass = true } } else if strings.HasPrefix(address, "EC") { if factoid.ValidateECUserStr(address) { resp = "Entry Credit - Public" pass = true } } else if strings.HasPrefix(address, "Fs") { if factoid.ValidateFPrivateUserStr(address) { resp = "Factoid - Private" pass = true } } else if strings.HasPrefix(address, "Es") { if factoid.ValidateECPrivateUserStr(address) { resp = "Entry Credit - Private" pass = true } } // Add Netki resolution here //else if (checkNetki) { // if (factoid.ValidateECPrivateUserStr(address)) { // resp = "{\"AddressType\":\"Factoid - Public\", \"TypeCode\":4 ,\"Success\":true}" // } //} return resp, pass }
func IsValidAddress(address string) bool { if !fct.ValidateFUserStr(address) && !fct.ValidateECUserStr(address) { return false } return true }
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 }
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 }
func SilentAddOutput(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.ValidateFUserStr(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().AddOutput(trans, addr, uint64(bamount)) if err != nil { return err } return nil }
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 }
// &key=<key>&name=<name or address>&amount=<amount> // If no amount is specified, a zero is returned. func getParams_(ctx *web.Context, params string, ec bool) ( trans fct.ITransaction, key string, name string, address fct.IAddress, amount int64, ok bool) { key = ctx.Params["key"] name = ctx.Params["name"] StrAmount := ctx.Params["amount"] if len(StrAmount) == 0 { StrAmount = "0" } if len(key) == 0 || len(name) == 0 { str := fmt.Sprintln("Missing Parameters: key='", key, "' name='", name, "' amount='", StrAmount, "'") reportResults(ctx, str, false) ok = false return } msg, valid := ValidateKey(key) if !valid { reportResults(ctx, msg, false) ok = false return } amount, err := strconv.ParseInt(StrAmount, 10, 64) if err != nil { str := fmt.Sprintln("Error parsing amount.\n", err) reportResults(ctx, str, false) ok = false return } // Get the transaction trans, err = getTransaction(ctx, key) if err != nil { reportResults(ctx, "Failure to locate the transaction", false) ok = false return } // Get the input/output/ec address. Which could be a name. First look and see if it is // a name. If it isn't, then look and see if it is an address. Someone could // do a weird Address as a name and fool the code, but that seems unlikely. // Could check for that some how, but there are many ways around such checks. if len(name) <= fct.ADDRESS_LENGTH { we := Wallet.GetRaw([]byte(fct.W_NAME), []byte(name)) if we != nil { address, err = we.(wallet.IWalletEntry).GetAddress() if we.(wallet.IWalletEntry).GetType() == "ec" { if !ec { reportResults(ctx, "Was Expecting a Factoid Address", false) ok = false return } } else { if ec { reportResults(ctx, "Was Expecting an Entry Credit Address", false) ok = false return } } if err != nil || address == nil { reportResults(ctx, "Should not get an error geting a address from a Wallet Entry", false) ok = false return } ok = true return } } if (!ec && !fct.ValidateFUserStr(name)) || (ec && !fct.ValidateECUserStr(name)) { reportResults(ctx, fmt.Sprintf("The address specified isn't defined or is invalid: %s", name), false) ctx.WriteHeader(httpBad) ok = false return } baddr := fct.ConvertUserStrToAddress(name) address = fct.NewAddress(baddr) ok = true return }