예제 #1
0
func TestAmountPaid(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	btcConf, err := util.LoadBitcoindConf("")
	if err != nil {
		t.Fatal(err)
	}
	peer := Peer{ID: msg.OcID("123id")}
	otherPeer := Peer{ID: msg.OcID("456otherid")}

	addr, err := peer.PaymentAddr(1, btcConf)
	if err != nil {
		t.Fatal(err)
	}
	otherAddr, err := otherPeer.PaymentAddr(1, btcConf)
	if err != nil {
		t.Fatal(err)
	}
	fmt.Printf("addr %v other addr %v\n", addr, otherAddr)

	// Send some BTC to ourselves.
	amt := int64(1e6)
	cmd, err := btcjson.NewSendToAddressCmd("", addr, amt)
	if err != nil {
		log.Fatal(err)
	}
	sendBtcResp, err := util.SendBtcRpc(cmd, btcConf)
	_, ok := sendBtcResp.Result.(string)
	if !ok {
		log.Fatal(sendBtcResp)
	}

	// Send some BTC to another address.
	cmd, err = btcjson.NewSendToAddressCmd("", otherAddr, 1e6)
	if err != nil {
		log.Fatal(err)
	}
	sendBtcResp, err = util.SendBtcRpc(cmd, btcConf)
	_, ok = sendBtcResp.Result.(string)
	if !ok {
		log.Fatal(sendBtcResp)
	}

	// Verify balance
	pv, err := peer.AmountPaid(0, btcConf)
	if err != nil {
		log.Fatal(err)
	}
	if pv.Amount != amt {
		t.Fatalf("%v != %v", paid, amt)
	}
}
예제 #2
0
파일: peer.go 프로젝트: therob3000/decloud
func (p *Peer) AmountPaid(minConf int, btcConf *util.BitcoindConf) (*msg.PaymentValue, error) {
	cmd, err := btcjson.NewListReceivedByAddressCmd("", minConf, false)
	if err != nil {
		return nil, fmt.Errorf("error while making cmd: %v", err.Error())
	}
	resp, err := util.SendBtcRpc(cmd, btcConf)
	ser, ok := resp.Result.([]interface{})
	if !ok {
		return nil, fmt.Errorf("error during bitcoind JSON-RPC: %v", resp)
	}
	addrs := p.readPaymentAddrs()
	addrsMap := make(map[string]bool)
	for _, addr := range addrs {
		addrsMap[addr] = true
	}
	amt := int64(0)
	for _, r := range ser {
		result := r.(map[string]interface{})
		if addrsMap[result["address"].(string)] {
			satoshis := util.B2S(result["amount"].(float64))
			fmt.Printf("addr: %v -> %v\n", result["address"], satoshis)
			amt += satoshis
		}
	}
	fmt.Printf("my addrs: %v\n", addrs)
	return &msg.PaymentValue{Amount: amt, Currency: msg.BTC}, nil
}
예제 #3
0
파일: peer.go 프로젝트: therob3000/decloud
func (p *Peer) fetchNewBtcAddr(btcConf *util.BitcoindConf) (string, error) {
	cmd, err := btcjson.NewGetNewAddressCmd("")
	if err != nil {
		return "", fmt.Errorf("error while making cmd: %v", err.Error())
	}
	resp, err := util.SendBtcRpc(cmd, btcConf)
	addr, ok := resp.Result.(string)
	if !ok {
		return "", fmt.Errorf("error during bitcoind JSON-RPC: %v", resp)
	}
	return addr, nil
}
예제 #4
0
파일: node.go 프로젝트: therob3000/decloud
func (c *Client) SendBtcPayment(payVal *msg.PaymentValue, payAddr *msg.PaymentAddr) (msg.BtcTxid, error) {
	if payVal.Currency != msg.BTC || payAddr.Currency != msg.BTC {
		panic("unexpected currency: " + payVal.Currency + " " + payAddr.Currency)
	}
	cmd, err := btcjson.NewSendToAddressCmd("", payAddr.Addr, payVal.Amount)
	if err != nil {
		return "", fmt.Errorf("error while making cmd: %v", err.Error())
	}
	resp, err := util.SendBtcRpc(cmd, c.BtcConf)
	if err != nil {
		return "", fmt.Errorf("error while making cmd: %v", err.Error())
	}
	txid, ok := resp.Result.(string)
	if !ok {
		return "", fmt.Errorf("error during bitcoind JSON-RPC: %v", resp)
	}
	return msg.BtcTxid(txid), nil
}
예제 #5
0
파일: cred.go 프로젝트: therob3000/decloud
func GetBtcCredInRange(min, max int64, conf *util.BitcoindConf) (*[]BtcCred, error) {
	cmd, err := btcjson.NewListUnspentCmd("")
	if err != nil {
		return nil, fmt.Errorf("error while making cmd: %v", err.Error())
	}
	resp, err := util.SendBtcRpc(cmd, conf)
	if err != nil {
		return nil, fmt.Errorf("error while making cmd: %v", err.Error())
	}
	if resp.Error != nil {
		return nil, fmt.Errorf("error during bitcoind JSON-RPC: %v", resp.Error)
	}
	addrs := make(map[string]*addressBalance)
	unspent := resp.Result.([]btcjson.ListUnSpentResult)
	for _, u := range unspent {
		if _, ok := addrs[u.Address]; !ok {
			addrs[u.Address] = &addressBalance{
				Address: u.Address,
				Amount:  0,
			}
		}
		ab := addrs[u.Address]
		ab.Amount += util.B2S(u.Amount)
	}
	addrsList := make([]addressBalance, len(addrs))
	i := 0
	for _, v := range addrs {
		addrsList[i] = *v
		i++
	}
	sort.Sort(byAmount(addrsList))
	var use *[]BtcCred
	for iter := 1; iter <= 5; iter++ {
		use, err = inputsInRange(&addrsList, min, max, iter, len(addrsList)-1)
		if use != nil {
			break
		}
	}
	if err != nil {
		return nil, err
	}
	return use, nil
}