Ejemplo n.º 1
0
func CommitChain(name string, data []byte) error {
	type walletcommit struct {
		Message string
	}

	type commit struct {
		CommitChainMsg string
	}

	in := new(walletcommit)
	json.Unmarshal(data, in)
	msg, err := hex.DecodeString(in.Message)
	if err != nil {
		return fmt.Errorf("Could not decode message:", err)
	}

	var we fct.IBlock

	if Utility.IsValidAddress(name) && strings.HasPrefix(name, "EC") {
		addr := fct.ConvertUserStrToAddress(name)
		we = factoidState.GetDB().GetRaw([]byte(fct.W_ADDRESS_PUB_KEY), addr)
	} else if Utility.IsValidHexAddress(name) {
		addr, err := hex.DecodeString(name)
		if err == nil {
			we = factoidState.GetDB().GetRaw([]byte(fct.W_ADDRESS_PUB_KEY), addr)
		}
	} else {
		we = factoidState.GetDB().GetRaw([]byte(fct.W_NAME), []byte(name))
	}

	if we == nil {
		return fmt.Errorf("Unknown address")
	}

	signed := make([]byte, 0)
	switch we.(type) {
	case wallet.IWalletEntry:
		signed = factoidState.GetWallet().SignCommit(we.(wallet.IWalletEntry), msg)
	default:
		return fmt.Errorf("Cannot use non Entry Credit Address for Chain Commit")
	}

	com := new(commit)
	com.CommitChainMsg = hex.EncodeToString(signed)
	j, err := json.Marshal(com)
	if err != nil {
		return fmt.Errorf("Could not create json post:", err)
	}

	resp, err := http.Post(
		fmt.Sprintf("http://%s:%d/v1/commit-chain", ipaddressFD, portNumberFD),
		"application/json",
		bytes.NewBuffer(j))
	if err != nil {
		return fmt.Errorf("Could not post to server:", err)
	}
	resp.Body.Close()

	return nil
}
Ejemplo n.º 2
0
func LookupAddress(adrType string, adr string) (string, error) {
	if Utility.IsValidAddress(adr) && strings.HasPrefix(adr, adrType) {
		baddr := fct.ConvertUserStrToAddress(adr)
		adr = hex.EncodeToString(baddr)
	} else if Utility.IsValidHexAddress(adr) {
		// the address is good enough.
	} else if Utility.IsValidNickname(adr) {
		we := factoidState.GetDB().GetRaw([]byte(fct.W_NAME), []byte(adr))

		if we != nil {
			we2 := we.(wallet.IWalletEntry)

			if we2.GetType() == "ec" {
				if strings.ToLower(adrType) == "fct" {
					return "", fmt.Errorf("%s is an entry credit address, not a factoid address.", adr)
				}
			} else if we2.GetType() == "fct" {
				if strings.ToLower(adrType) == "ec" {
					return "", fmt.Errorf("%s is a factoid address, not an entry credit address.", adr)
				}
			}

			addr, _ := we2.GetAddress()
			adr = hex.EncodeToString(addr.Bytes())
		} else {
			return "", fmt.Errorf("Name %s is undefined.", adr)
		}
	} else {
		return "", fmt.Errorf("Invalid Name.  Check that you have entered the name correctly.")
	}

	return adr, nil
}
Ejemplo n.º 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
}
Ejemplo n.º 4
0
// ImportKey <name> <private key>
func (ImportKey) Execute(state IState, args []string) error {

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

	if err := ValidName(name); err != nil {
		return err
	}

	a := state.GetFS().GetDB().GetRaw([]byte(fct.W_NAME), []byte(name))
	if a != nil {
		return fmt.Errorf("That address name is in use.  Specify a different name.")
	}

	fa := fct.ValidateFPrivateUserStr(adr)
	ec := fct.ValidateECPrivateUserStr(adr)
	b, err := hex.DecodeString(adr)
	if err == nil && len(b) != 32 {
		err = fmt.Errorf("wrong length")
	}
	if fa || ec || err == nil {
		var privateKey []byte
		if err != nil {
			privateKey = fct.ConvertUserStrToAddress(adr)
		} else {
			privateKey = b
		}
		var fixed [64]byte
		copy(fixed[:], privateKey)
		publicKey := ed25519.GetPublicKey(&fixed)
		adrtype := "ec"
		if fa {
			adrtype = "fct"
		}
		_, err := state.GetFS().GetWallet().AddKeyPair(adrtype, []byte(name), publicKey[:], privateKey, false)
		if err != nil {
			return err
		}
		return nil
	}

	return fmt.Errorf("Not a valid Private Key; Check that your key is typed correctly")
}
Ejemplo n.º 5
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
}
Ejemplo n.º 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
}
Ejemplo n.º 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
}
Ejemplo n.º 8
0
// &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
}