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 TestIsValidHexAddress(t *testing.T) {
	validHexes := []string{"dceb1ce5778444e7777172e1f586488d2382fb1037887cd79a70b0cba4fb3dce", "9881aeb264452a4f7fafa1cc7bc4b93a05c55537c0703453e585f6d83ce77dca"}
	invalidHexes := []string{"", "cat", "deadbeef", "FA3eNd17NgaXZA3rXQVvzSvWHrpXfHWPzLQjJy2PQVQSc4ZutjC1", "FA38F8fY6duMqDLyCNUYWemdFSWgXDSteeNvNCmJ1Eyb86Z3VNZo"}

	for _, v := range validHexes {
		valid := Utility.IsValidHexAddress(v)
		if valid == false {
			t.Errorf("IsValidHexAddress returned false for valid key `%v`\n", v)
		}
	}
	for _, v := range invalidHexes {
		valid := Utility.IsValidHexAddress(v)
		if valid {
			t.Errorf("IsValidHexAddress returned true for invalid key `%v`\n", v)
		}
	}
}
Ejemplo n.º 4
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
}