Beispiel #1
0
// toSatoshi attempts to convert the passed string to a satoshi amount returned
// as an int64.  It returns the int64 packed into an interface so it can be used
// in the calls which expect interfaces.  An error will be returned if the string
// can't be converted first to a float64.
func toSatoshi(val string) (interface{}, error) {
	idx, err := strconv.ParseFloat(val, 64)
	if err != nil {
		return nil, err
	}
	amt, err := btcutil.NewAmount(idx)
	if err != nil {
		return nil, err
	}
	return int64(amt), nil
}
Beispiel #2
0
func TestBalance(t *testing.T) {
	log.Println("Testing to see if wallet has adequate balance")
	client, _ := ConfigureApp()

	bal, err := client.GetBalance("")
	if err != nil {
		log.Println(err)
		t.Fail()
	}

	target, _ := btcutil.NewAmount(1) // one bitcoin balance targeted
	if bal < target {
		log.Printf("Not enough funds %s short\n", target-bal)
		t.Fail()
	}
}
Beispiel #3
0
func rpcTxPick(exact bool, targetAmnt int64, params BuilderParams) (*TxInParams, error) {
	// selects an unspent outpoint that is funded over the minAmount
	list, err := params.Client.ListUnspent()
	if err != nil {
		log.Println("list unpsent threw")
		return nil, err
	}
	if len(list) < 1 {
		return nil, errors.New("No unspent outputs at all.")
	}

	for _, prevJson := range list {
		_amnt, _ := btcutil.NewAmount(prevJson.Amount)
		amnt := int64(_amnt)
		txid := prevJson.TxId
		prevHash, _ := btcwire.NewShaHashFromStr(txid)
		outPoint := btcwire.NewOutPoint(prevHash, prevJson.Vout)

		_, contained := params.PendingSet[outPointStr(outPoint)]
		// This unpsent is in the pending set and it either exactly equals the target or
		// has a value above that target
		if !contained && (exact && targetAmnt == amnt || !exact && targetAmnt <= amnt) {
			// Found one, lets use it
			script, _ := hex.DecodeString(prevJson.ScriptPubKey)
			// None of the above ~should~ ever throw errors
			txOut := btcwire.NewTxOut(amnt, script)

			prevAddress, _ := btcutil.DecodeAddress(prevJson.Address, params.NetParams)
			wifkey, err := params.Client.DumpPrivKey(prevAddress)
			if err != nil {
				return nil, err
			}
			inParams := TxInParams{
				TxOut:    txOut,
				OutPoint: outPoint,
				Wif:      wifkey,
			}
			params.PendingSet[outPointStr(outPoint)] = struct{}{}
			return &inParams, nil
		}
	}
	// Never found a good outpoint
	return nil, errors.New("No txout with the right funds")
}
Beispiel #4
0
// makeCreateRawTransaction generates the cmd structure for createrawtransaction
// commands.
func makeCreateRawTransaction(args []interface{}) (btcjson.Cmd, error) {
	var inputs []btcjson.TransactionInput
	err := json.Unmarshal([]byte(args[0].(string)), &inputs)
	if err != nil {
		return nil, err
	}

	var famounts map[string]float64
	err = json.Unmarshal([]byte(args[1].(string)), &famounts)
	if err != nil {
		return nil, err
	}

	amounts := make(map[string]int64, len(famounts))
	for k, v := range famounts {
		amt, err := btcutil.NewAmount(v)
		if err != nil {
			return nil, err
		}
		amounts[k] = int64(amt)
	}

	return btcjson.NewCreateRawTransactionCmd("btcctl", inputs, amounts)
}