Пример #1
0
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
}
Пример #2
0
func GenerateAddressStringFromMnemonic(name string, privateKey string) (string, error) {
	addr, err := GenerateAddressFromMnemonic(name, privateKey)
	if err != nil {
		return "", err
	}
	return factoid.ConvertFctAddressToUserStr(addr), nil
}
Пример #3
0
func GenerateAddressString(name string) (string, error) {
	addr, err := GenerateAddress(name)
	if err != nil {
		return "", err
	}
	return factoid.ConvertFctAddressToUserStr(addr), nil
}
func main() {

	privatekey := make([]byte, 32)
	_, err := rand.Read(privatekey)
	if err == nil {
		privAddr := factoid.NewAddress(privatekey)

		privHuman := factoid.ConvertFctPrivateToUserStr(privAddr)
		fmt.Printf("New Factoid Private Key: %v\n", privHuman)

		pub, priv, err := wallet.GenerateKeyFromPrivateKey(privatekey)
		if err != nil {
			panic(err)
		}

		we := new(wallet.WalletEntry)
		we.AddKey(pub, priv)
		we.SetName([]byte("test"))
		we.SetRCD(factoid.NewRCD_1(pub))
		we.SetType("fct")

		address, _ := we.GetAddress()

		adr := factoid.ConvertFctAddressToUserStr(address)

		fmt.Printf("New Factoid Address:     %v\n", adr)
	} else {
		fmt.Printf("error\n")
	}

}
Пример #5
0
func Test_Address(test *testing.T) {

	b, _ := hex.DecodeString("8cba3aeb7d16269da4f37d0c98979a7d9b23943774be232e817504ec8ab7e2a7")
	adr := fct.NewAddress(b)
	str := fct.ConvertFctAddressToUserStr(adr)
	fmt.Println(str)
}
Пример #6
0
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()
}
Пример #7
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
}
Пример #8
0
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
}
Пример #9
0
func main() {

	r := bufio.NewScanner(os.Stdin)

	fmt.Print("Enter 12 words from Koinify here: ")
	r.Scan()
	line := r.Text()
	args := strings.Fields(string(line))

	if len(args) == 12 {

		mnemonic := ""
		for _, v := range args {
			v = strings.ToLower(v)
			mnemonic = mnemonic + v + " "
		}
		mnemonic = strings.TrimSpace(mnemonic)
		private, err := wallet.MnemonicStringToPrivateKey(mnemonic)
		if err != nil {
			fmt.Print("\n\nThere was a problem with the 12 words you entered. Please check spelling against this list:\n")
			fmt.Print("https://github.com/FactomProject/go-bip39/blob/master/wordlist.go\n\n\n\n")
			panic(err)
		}
		pub, priv, err := wallet.GenerateKeyFromPrivateKey(private)
		if err != nil {
			panic(err)
		}

		we := new(wallet.WalletEntry)
		we.AddKey(pub, priv)
		we.SetName([]byte("test"))
		we.SetRCD(factoid.NewRCD_1(pub))
		we.SetType("fct")

		address, _ := we.GetAddress()

		adr := factoid.ConvertFctAddressToUserStr(address)

		fmt.Printf("\nFactoid Address: %v\n", adr)
		fmt.Printf("\nCheck your balance at http://explorer.factom.org/\n")
	} else {
		fmt.Printf("\n\nError: 12 and only 12 words are expected.\n")
	}

}
Пример #10
0
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
}
Пример #11
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
}
Пример #12
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
}
Пример #13
0
func (w *SCWallet) SignInputs(trans fct.ITransaction) (bool, error) {

	data, err := trans.MarshalBinarySig() // Get the part of the transaction we sign
	if err != nil {
		return false, err
	}

	var errMsg []byte

	rcds := trans.GetRCDs()
	for i, rcd := range rcds {
		rcd1, ok := rcd.(*fct.RCD_1)
		if ok {
			pub := rcd1.GetPublicKey()
			we, ok := w.db.GetRaw([]byte(fct.W_ADDRESS_PUB_KEY), pub).(*WalletEntry)
			if ok {
				var pri [fct.SIGNATURE_LENGTH]byte
				copy(pri[:], we.private[0])
				bsig := ed25519.Sign(&pri, data)
				sig := new(fct.Signature)
				sig.SetSignature(bsig[:])
				sigblk := new(fct.SignatureBlock)
				sigblk.AddSignature(sig)
				trans.SetSignatureBlock(i, sigblk)
			} else {
				errMsg = append(errMsg,
					[]byte("Do not have the private key for: "+
						fct.ConvertFctAddressToUserStr(fct.NewAddress(pub))+"\n")...)
			}
		}
	}

	if errMsg != nil {
		return false, fmt.Errorf("%s", string(errMsg))
	}
	return true, nil
}
Пример #14
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()
}
Пример #15
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))

	}
}