Ejemplo n.º 1
0
func TestBtcSignRequest(t *testing.T) {
	printBitcoindExpected()
	btcConf, err := util.LoadBitcoindConf("")
	if err != nil {
		t.Errorf(err.Error())
	}
	c, err := newClient(btcConf)
	if err != nil {
		log.Fatal(err)
	}
	err = c.BtcSignRequest(util.B2S(.08), util.B2S(.08), nil)
	if err != nil {
		log.Fatal(err)
	}
}
Ejemplo n.º 2
0
func TestPeerFromReq(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	ocCred := cred.NewOcCred()
	btcConf, err := util.LoadBitcoindConf("")
	if err != nil {
		t.Fatal(err)
	}
	// TODO(ortutay): this test is flakey, as we may not have any BTC at all
	btcCreds, err := cred.GetBtcCredInRange(0, util.B2S(1000), btcConf)
	if err != nil {
		t.Fatal(err)
	}
	req := calc.NewCalcReq([]string{"1 2 +"})
	err = ocCred.SignOcReq(req)
	if err != nil {
		t.Fatal(err)
	}
	for _, bc := range *btcCreds {
		err = bc.SignOcReq(req, btcConf)
		if err != nil {
			t.Fatal(err)
		}
	}
	p, err := NewPeerFromReq(req, btcConf)
	if err != nil {
		t.Fatal(err)
	}
	if p.ID != req.ID {
		t.FailNow()
	}
}
Ejemplo n.º 3
0
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
}
Ejemplo n.º 4
0
func TestPeerFromReqCoinReuse(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	ocCred1 := cred.NewOcCred()
	ocCred2 := cred.NewOcCred()
	btcConf, err := util.LoadBitcoindConf("")
	if err != nil {
		t.Fatal(err)
	}
	// TODO(ortutay): this test is flakey, as we may not have any BTC at all
	btcCreds, err := cred.GetBtcCredInRange(0, util.B2S(1000), btcConf)
	if err != nil {
		t.Fatal(err)
	}
	req1 := calc.NewCalcReq([]string{"1 2 +"})
	err = ocCred1.SignOcReq(req1)
	if err != nil {
		t.Fatal(err)
	}
	req2 := calc.NewCalcReq([]string{"1 2 +"})
	err = ocCred2.SignOcReq(req2)
	if err != nil {
		t.Fatal(err)
	}
	for _, bc := range *btcCreds {
		err = bc.SignOcReq(req1, btcConf)
		err = bc.SignOcReq(req2, btcConf)
		if err != nil {
			t.Fatal(err)
		}
	}
	p1, err := NewPeerFromReq(req1, btcConf)
	if err != nil {
		t.Fatal(err)
	}
	if p1.ID != req1.ID {
		t.FailNow()
	}
	p2, err := NewPeerFromReq(req2, btcConf)
	if err == nil || err != COIN_REUSE {
		t.Fatal("Expected COIN_REUSE error")
	}
	if p2 != nil {
		t.FailNow()
	}
}
Ejemplo n.º 5
0
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
}
Ejemplo n.º 6
0
func costForBytesSeconds(bytes int, seconds int) *msg.PaymentValue {
	costBtc := float64(bytes) * float64(seconds) * .000001
	return &msg.PaymentValue{Amount: util.B2S(costBtc), Currency: msg.BTC}
}