Skip to content

Commit

Permalink
Merge pull request #55 from OpenBazaar/ethereum
Browse files Browse the repository at this point in the history
Update repo with changes needed for ethereum
  • Loading branch information
placer14 committed Jan 15, 2020
2 parents 39f04e8 + 988a15e commit d10f498
Show file tree
Hide file tree
Showing 21 changed files with 199 additions and 152 deletions.
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
name = "github.com/OpenBazaar/jsonpb"

[[constraint]]
branch = "master"
branch = "ethereum-master"
name = "github.com/OpenBazaar/wallet-interface"

[[constraint]]
Expand Down
44 changes: 26 additions & 18 deletions api/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package api
import (
"encoding/hex"
"errors"
"github.com/OpenBazaar/spvwallet"
"github.com/OpenBazaar/spvwallet/api/pb"
"math/big"
"net"
"strconv"
"sync"

"github.com/OpenBazaar/wallet-interface"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
Expand All @@ -15,8 +18,9 @@ import (
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"net"
"sync"

"github.com/OpenBazaar/spvwallet"
"github.com/OpenBazaar/spvwallet/api/pb"
)

const Addr = "127.0.0.1:8234"
Expand Down Expand Up @@ -77,7 +81,7 @@ func (s *server) ChainTip(ctx context.Context, in *pb.Empty) (*pb.Height, error)

func (s *server) Balance(ctx context.Context, in *pb.Empty) (*pb.Balances, error) {
confirmed, unconfirmed := s.w.Balance()
return &pb.Balances{uint64(confirmed), uint64(unconfirmed)}, nil
return &pb.Balances{confirmed.Value.Uint64(), unconfirmed.Value.Uint64()}, nil
}

func (s *server) MasterPrivateKey(ctx context.Context, in *pb.Empty) (*pb.Key, error) {
Expand Down Expand Up @@ -126,9 +130,10 @@ func (s *server) Transactions(ctx context.Context, in *pb.Empty) (*pb.Transactio
if err != nil {
return nil, err
}
val0, _ := strconv.ParseInt(tx.Value, 10, 64)
respTx := &pb.Tx{
Txid: tx.Txid,
Value: tx.Value,
Value: val0,
Height: tx.Height,
WatchOnly: tx.WatchOnly,
Timestamp: ts,
Expand All @@ -152,9 +157,10 @@ func (s *server) GetTransaction(ctx context.Context, in *pb.Txid) (*pb.Tx, error
if err != nil {
return nil, err
}
val0, _ := strconv.ParseInt(tx.Value, 10, 64)
respTx := &pb.Tx{
Txid: tx.Txid,
Value: tx.Value,
Value: val0,
Height: tx.Height,
WatchOnly: tx.WatchOnly,
Timestamp: ts,
Expand All @@ -175,7 +181,9 @@ func (s *server) GetFeePerByte(ctx context.Context, in *pb.FeeLevelSelection) (*
default:
return nil, errors.New("Unknown fee level")
}
return &pb.FeePerByte{s.w.GetFeePerByte(feeLevel)}, nil

var val = s.w.GetFeePerByte(feeLevel)
return &pb.FeePerByte{val.Uint64()}, nil
}

func (s *server) Spend(ctx context.Context, in *pb.SpendInfo) (*pb.Txid, error) {
Expand Down Expand Up @@ -209,7 +217,7 @@ func (s *server) Spend(ctx context.Context, in *pb.SpendInfo) (*pb.Txid, error)
if err != nil {
return nil, err
}
txid, err := s.w.Spend(int64(in.Amount), addr, feeLevel, "", false)
txid, err := s.w.Spend(*big.NewInt(int64(in.Amount)), addr, feeLevel, "", false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -297,7 +305,7 @@ func (s *server) SweepAddress(ctx context.Context, in *pb.SweepInfo) (*pb.Txid,
in := wallet.TransactionInput{
OutpointHash: h.CloneBytes(),
OutpointIndex: u.Index,
Value: int64(u.Value),
Value: *big.NewInt(int64(u.Value)),
}
ins = append(ins, in)
}
Expand Down Expand Up @@ -407,7 +415,7 @@ func (s *server) CreateMultisigSignature(ctx context.Context, in *pb.CreateMulti
}
o := wallet.TransactionOutput{
Address: addr,
Value: int64(output.Value),
Value: *big.NewInt(int64(output.Value)),
}
outs = append(outs, o)
}
Expand Down Expand Up @@ -455,7 +463,7 @@ func (s *server) CreateMultisigSignature(ctx context.Context, in *pb.CreateMulti
}
}
}
sigs, err := s.w.CreateMultisigSignature(ins, outs, key, in.RedeemScript, in.FeePerByte)
sigs, err := s.w.CreateMultisigSignature(ins, outs, key, in.RedeemScript, *big.NewInt(int64(in.FeePerByte)))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -491,7 +499,7 @@ func (s *server) Multisign(ctx context.Context, in *pb.MultisignInfo) (*pb.RawTx
}
o := wallet.TransactionOutput{
Address: addr,
Value: int64(output.Value),
Value: *big.NewInt(int64(output.Value)),
}
outs = append(outs, o)
}
Expand All @@ -511,7 +519,7 @@ func (s *server) Multisign(ctx context.Context, in *pb.MultisignInfo) (*pb.RawTx
}
sig2 = append(sig2, sig)
}
tx, err := s.w.Multisign(ins, outs, sig1, sig2, in.RedeemScript, in.FeePerByte, in.Broadcast)
tx, err := s.w.Multisign(ins, outs, sig1, sig2, in.RedeemScript, *big.NewInt(int64(in.FeePerByte)), in.Broadcast)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -539,12 +547,12 @@ func (s *server) EstimateFee(ctx context.Context, in *pb.EstimateFeeData) (*pb.F
}
o := wallet.TransactionOutput{
Address: addr,
Value: int64(output.Value),
Value: *big.NewInt(int64(output.Value)),
}
outs = append(outs, o)
}
fee := s.w.EstimateFee(ins, outs, in.FeePerByte)
return &pb.Fee{fee}, nil
fee := s.w.EstimateFee(ins, outs, *big.NewInt(int64(in.FeePerByte)))
return &pb.Fee{fee.Uint64()}, nil
}

func (s *server) WalletNotify(in *pb.Empty, stream pb.API_WalletNotifyServer) error {
Expand All @@ -555,7 +563,7 @@ func (s *server) WalletNotify(in *pb.Empty, stream pb.API_WalletNotifyServer) er
}
resp := &pb.Tx{
Txid: tx.Txid,
Value: tx.Value,
Value: tx.Value.Int64(),
Height: tx.Height,
Timestamp: ts,
WatchOnly: tx.WatchOnly,
Expand Down
27 changes: 14 additions & 13 deletions cmd/spvwallet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"net"
"net/url"
"os"
"os/signal"
"path"
"strings"
"sync"
"time"

"github.com/OpenBazaar/spvwallet"
"github.com/OpenBazaar/spvwallet/api"
Expand All @@ -24,15 +34,6 @@ import (
"github.com/skratchdot/open-golang/open"
"github.com/yawning/bulb"
"golang.org/x/net/proxy"
"io/ioutil"
"net"
"net/url"
"os"
"os/signal"
"path"
"strings"
"sync"
"time"
)

var parser = flags.NewParser(nil, flags.Default)
Expand Down Expand Up @@ -368,12 +369,12 @@ func (x *Start) Execute(args []string) error {
astilog.Errorf("Failed to get exchange rate")
return
}
btcVal := float64(confirmed) / 100000000
fiatVal := float64(btcVal) * rate
btcVal := float64(confirmed.Value.Int64()) / 100000000.0
fiatVal := (btcVal) * rate
height, _ := wallet.ChainTip()

st := Stats{
Confirmed: confirmed,
Confirmed: confirmed.Value.Int64(),
Fiat: fmt.Sprintf("%.2f", fiatVal),
Transactions: len(txs),
Height: height,
Expand Down Expand Up @@ -411,7 +412,7 @@ func (x *Start) Execute(args []string) error {
w.SendMessage(bootstrap.MessageOut{Name: "spendError", Payload: "Invalid address"})
return
}
_, err = wallet.Spend(int64(p.Amount), addr, feeLevel, "", false)
_, err = wallet.Spend(*big.NewInt(int64(p.Amount)), addr, feeLevel, "", false)
if err != nil {
w.SendMessage(bootstrap.MessageOut{Name: "spendError", Payload: err.Error()})
}
Expand Down
6 changes: 3 additions & 3 deletions db/stxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s *StxoDB) Put(stxo wallet.Stxo) error {
watchOnly = 1
}
outpoint := stxo.Utxo.Op.Hash.String() + ":" + strconv.Itoa(int(stxo.Utxo.Op.Index))
_, err = stmt.Exec(outpoint, int(stxo.Utxo.Value), int(stxo.Utxo.AtHeight), hex.EncodeToString(stxo.Utxo.ScriptPubkey), watchOnly, int(stxo.SpendHeight), stxo.SpendTxid.String())
_, err = stmt.Exec(outpoint, stxo.Utxo.Value, int(stxo.Utxo.AtHeight), hex.EncodeToString(stxo.Utxo.ScriptPubkey), watchOnly, int(stxo.SpendHeight), stxo.SpendTxid.String())
if err != nil {
tx.Rollback()
return err
Expand All @@ -52,7 +52,7 @@ func (s *StxoDB) GetAll() ([]wallet.Stxo, error) {
}
for rows.Next() {
var outpoint string
var value int
var value string
var height int
var scriptPubKey string
var watchOnlyInt int
Expand Down Expand Up @@ -88,7 +88,7 @@ func (s *StxoDB) GetAll() ([]wallet.Stxo, error) {
utxo := wallet.Utxo{
Op: *wire.NewOutPoint(shaHash, uint32(index)),
AtHeight: int32(height),
Value: int64(value),
Value: value,
ScriptPubkey: scriptBytes,
WatchOnly: watchOnly,
}
Expand Down
6 changes: 3 additions & 3 deletions db/stxo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {
utxo := wallet.Utxo{
Op: *outpoint,
AtHeight: 300000,
Value: 100000000,
Value: "100000000",
ScriptPubkey: []byte("scriptpubkey"),
WatchOnly: false,
}
Expand All @@ -48,7 +48,7 @@ func TestStxoPut(t *testing.T) {
defer stmt.Close()

var outpoint string
var value int
var value string
var height int
var scriptPubkey string
var spendHeight int
Expand All @@ -62,7 +62,7 @@ func TestStxoPut(t *testing.T) {
if outpoint != o {
t.Error("Stxo db returned wrong outpoint")
}
if value != int(stxo.Utxo.Value) {
if value != stxo.Utxo.Value {
t.Error("Stxo db returned wrong value")
}
if height != int(stxo.Utxo.AtHeight) {
Expand Down
10 changes: 5 additions & 5 deletions db/txns.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type TxnsDB struct {
lock *sync.RWMutex
}

func (t *TxnsDB) Put(txn []byte, txid string, value, height int, timestamp time.Time, watchOnly bool) error {
func (t *TxnsDB) Put(txn []byte, txid, value string, height int, timestamp time.Time, watchOnly bool) error {
t.lock.Lock()
defer t.lock.Unlock()
tx, err := t.db.Begin()
Expand Down Expand Up @@ -51,7 +51,7 @@ func (t *TxnsDB) Get(txid chainhash.Hash) (wallet.Txn, error) {
}
defer stmt.Close()
var ret []byte
var value int
var value string
var height int
var timestamp int
var watchOnlyInt int
Expand All @@ -68,7 +68,7 @@ func (t *TxnsDB) Get(txid chainhash.Hash) (wallet.Txn, error) {
}
txn = wallet.Txn{
Txid: msgTx.TxHash().String(),
Value: int64(value),
Value: value,
Height: int32(height),
Timestamp: time.Unix(int64(timestamp), 0),
WatchOnly: watchOnly,
Expand All @@ -89,7 +89,7 @@ func (t *TxnsDB) GetAll(includeWatchOnly bool) ([]wallet.Txn, error) {
defer rows.Close()
for rows.Next() {
var tx []byte
var value int
var value string
var height int
var timestamp int
var watchOnlyInt int
Expand All @@ -109,7 +109,7 @@ func (t *TxnsDB) GetAll(includeWatchOnly bool) ([]wallet.Txn, error) {

txn := wallet.Txn{
Txid: msgTx.TxHash().String(),
Value: int64(value),
Value: value,
Height: int32(height),
Timestamp: time.Unix(int64(timestamp), 0),
WatchOnly: watchOnly,
Expand Down
10 changes: 5 additions & 5 deletions db/txns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestTxnsPut(t *testing.T) {
r := bytes.NewReader(raw)
tx.Deserialize(r)

err := txdb.Put(raw, tx.TxHash().String(), 5, 1, time.Now(), false)
err := txdb.Put(raw, tx.TxHash().String(), "5", 1, time.Now(), false)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestTxnsGet(t *testing.T) {

now := time.Now()

err := txdb.Put(raw, tx.TxHash().String(), 0, 1, now, false)
err := txdb.Put(raw, tx.TxHash().String(), "0", 1, now, false)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestTxnsGetAll(t *testing.T) {
r := bytes.NewReader(raw)
tx.Deserialize(r)

err := txdb.Put(raw, tx.TxHash().String(), 1, 5, time.Now(), true)
err := txdb.Put(raw, tx.TxHash().String(), "1", 5, time.Now(), true)
if err != nil {
t.Error(err)
}
Expand All @@ -114,7 +114,7 @@ func TestDeleteTxns(t *testing.T) {
r := bytes.NewReader(raw)
tx.Deserialize(r)

err := txdb.Put(raw, tx.TxHash().String(), 0, 1, time.Now(), false)
err := txdb.Put(raw, tx.TxHash().String(), "0", 1, time.Now(), false)
if err != nil {
t.Error(err)
}
Expand All @@ -141,7 +141,7 @@ func TestTxnsDB_UpdateHeight(t *testing.T) {
r := bytes.NewReader(raw)
tx.Deserialize(r)

err := txdb.Put(raw, tx.TxHash().String(), 0, 1, time.Now(), false)
err := txdb.Put(raw, tx.TxHash().String(), "0", 1, time.Now(), false)
if err != nil {
t.Error(err)
}
Expand Down
Loading

0 comments on commit d10f498

Please sign in to comment.