Skip to content

Commit

Permalink
Interface refactor
Browse files Browse the repository at this point in the history
Moved some parts of the database interface as well as the OpenBazaar Wallet
interface into a separate package. This commit changes the imports to point
to the new package.
  • Loading branch information
cpacia committed Aug 31, 2017
1 parent 773769f commit d8b7c75
Show file tree
Hide file tree
Showing 23 changed files with 269 additions and 536 deletions.
75 changes: 38 additions & 37 deletions api/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"github.com/OpenBazaar/spvwallet"
"github.com/OpenBazaar/spvwallet/api/pb"
"github.com/OpenBazaar/wallet-interface"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
Expand Down Expand Up @@ -45,11 +46,11 @@ func (s *server) Stop(ctx context.Context, in *pb.Empty) (*pb.Empty, error) {
}

func (s *server) CurrentAddress(ctx context.Context, in *pb.KeySelection) (*pb.Address, error) {
var purpose spvwallet.KeyPurpose
var purpose wallet.KeyPurpose
if in.Purpose == pb.KeyPurpose_INTERNAL {
purpose = spvwallet.INTERNAL
purpose = wallet.INTERNAL
} else if in.Purpose == pb.KeyPurpose_EXTERNAL {
purpose = spvwallet.EXTERNAL
purpose = wallet.EXTERNAL
} else {
return nil, errors.New("Unknown key purpose")
}
Expand All @@ -58,11 +59,11 @@ func (s *server) CurrentAddress(ctx context.Context, in *pb.KeySelection) (*pb.A
}

func (s *server) NewAddress(ctx context.Context, in *pb.KeySelection) (*pb.Address, error) {
var purpose spvwallet.KeyPurpose
var purpose wallet.KeyPurpose
if in.Purpose == pb.KeyPurpose_INTERNAL {
purpose = spvwallet.INTERNAL
purpose = wallet.INTERNAL
} else if in.Purpose == pb.KeyPurpose_EXTERNAL {
purpose = spvwallet.EXTERNAL
purpose = wallet.EXTERNAL
} else {
return nil, errors.New("Unknown key purpose")
}
Expand Down Expand Up @@ -164,14 +165,14 @@ func (s *server) GetTransaction(ctx context.Context, in *pb.Txid) (*pb.Tx, error
}

func (s *server) GetFeePerByte(ctx context.Context, in *pb.FeeLevelSelection) (*pb.FeePerByte, error) {
var feeLevel spvwallet.FeeLevel
var feeLevel wallet.FeeLevel
switch in.FeeLevel {
case pb.FeeLevel_ECONOMIC:
feeLevel = spvwallet.ECONOMIC
feeLevel = wallet.ECONOMIC
case pb.FeeLevel_NORMAL:
feeLevel = spvwallet.NORMAL
feeLevel = wallet.NORMAL
case pb.FeeLevel_PRIORITY:
feeLevel = spvwallet.PRIOIRTY
feeLevel = wallet.PRIOIRTY
default:
return nil, errors.New("Unknown fee level")
}
Expand All @@ -194,14 +195,14 @@ func (s *server) Spend(ctx context.Context, in *pb.SpendInfo) (*pb.Txid, error)
default:
return nil, errors.New("Unknown network parameters")
}
var feeLevel spvwallet.FeeLevel
var feeLevel wallet.FeeLevel
switch in.FeeLevel {
case pb.FeeLevel_ECONOMIC:
feeLevel = spvwallet.ECONOMIC
feeLevel = wallet.ECONOMIC
case pb.FeeLevel_NORMAL:
feeLevel = spvwallet.NORMAL
feeLevel = wallet.NORMAL
case pb.FeeLevel_PRIORITY:
feeLevel = spvwallet.PRIOIRTY
feeLevel = wallet.PRIOIRTY
default:
return nil, errors.New("Unknown fee level")
}
Expand Down Expand Up @@ -298,14 +299,14 @@ func (s *server) GetConfirmations(ctx context.Context, in *pb.Txid) (*pb.Confirm
}

func (s *server) SweepAddress(ctx context.Context, in *pb.SweepInfo) (*pb.Txid, error) {
var utxos []spvwallet.Utxo
var utxos []wallet.Utxo
for _, u := range in.Utxos {
h, err := chainhash.NewHashFromStr(u.Txid)
if err != nil {
return nil, err
}
op := wire.NewOutPoint(h, u.Index)
utxo := spvwallet.Utxo{
utxo := wallet.Utxo{
Op: *op,
Value: int64(u.Value),
}
Expand Down Expand Up @@ -369,14 +370,14 @@ func (s *server) SweepAddress(ctx context.Context, in *pb.SweepInfo) (*pb.Txid,
if len(in.RedeemScript) > 0 {
rs = &in.RedeemScript
}
var feeLevel spvwallet.FeeLevel
var feeLevel wallet.FeeLevel
switch in.FeeLevel {
case pb.FeeLevel_ECONOMIC:
feeLevel = spvwallet.ECONOMIC
feeLevel = wallet.ECONOMIC
case pb.FeeLevel_NORMAL:
feeLevel = spvwallet.NORMAL
feeLevel = wallet.NORMAL
case pb.FeeLevel_PRIORITY:
feeLevel = spvwallet.PRIOIRTY
feeLevel = wallet.PRIOIRTY
default:
return nil, errors.New("Unknown fee level")
}
Expand All @@ -393,21 +394,21 @@ func (s *server) ReSyncBlockchain(ctx context.Context, in *pb.Height) (*pb.Empty
}

func (s *server) CreateMultisigSignature(ctx context.Context, in *pb.CreateMultisigInfo) (*pb.SignatureList, error) {
var ins []spvwallet.TransactionInput
var ins []wallet.TransactionInput
for _, input := range in.Inputs {
h, err := hex.DecodeString(input.Txid)
if err != nil {
return nil, err
}
i := spvwallet.TransactionInput{
i := wallet.TransactionInput{
OutpointHash: h,
OutpointIndex: input.Index,
}
ins = append(ins, i)
}
var outs []spvwallet.TransactionOutput
var outs []wallet.TransactionOutput
for _, output := range in.Outputs {
o := spvwallet.TransactionOutput{
o := wallet.TransactionOutput{
ScriptPubKey: output.ScriptPubKey,
Value: int64(output.Value),
}
Expand Down Expand Up @@ -473,37 +474,37 @@ func (s *server) CreateMultisigSignature(ctx context.Context, in *pb.CreateMulti
}

func (s *server) Multisign(ctx context.Context, in *pb.MultisignInfo) (*pb.RawTx, error) {
var ins []spvwallet.TransactionInput
var ins []wallet.TransactionInput
for _, input := range in.Inputs {
h, err := hex.DecodeString(input.Txid)
if err != nil {
return nil, err
}
i := spvwallet.TransactionInput{
i := wallet.TransactionInput{
OutpointHash: h,
OutpointIndex: input.Index,
}
ins = append(ins, i)
}
var outs []spvwallet.TransactionOutput
var outs []wallet.TransactionOutput
for _, output := range in.Outputs {
o := spvwallet.TransactionOutput{
o := wallet.TransactionOutput{
ScriptPubKey: output.ScriptPubKey,
Value: int64(output.Value),
}
outs = append(outs, o)
}
var sig1 []spvwallet.Signature
var sig1 []wallet.Signature
for _, s := range in.Sig1 {
sig := spvwallet.Signature{
sig := wallet.Signature{
InputIndex: s.Index,
Signature: s.Signature,
}
sig1 = append(sig1, sig)
}
var sig2 []spvwallet.Signature
var sig2 []wallet.Signature
for _, s := range in.Sig2 {
sig := spvwallet.Signature{
sig := wallet.Signature{
InputIndex: s.Index,
Signature: s.Signature,
}
Expand All @@ -517,21 +518,21 @@ func (s *server) Multisign(ctx context.Context, in *pb.MultisignInfo) (*pb.RawTx
}

func (s *server) EstimateFee(ctx context.Context, in *pb.EstimateFeeData) (*pb.Fee, error) {
var ins []spvwallet.TransactionInput
var ins []wallet.TransactionInput
for _, input := range in.Inputs {
h, err := hex.DecodeString(input.Txid)
if err != nil {
return nil, err
}
i := spvwallet.TransactionInput{
i := wallet.TransactionInput{
OutpointHash: h,
OutpointIndex: input.Index,
}
ins = append(ins, i)
}
var outs []spvwallet.TransactionOutput
var outs []wallet.TransactionOutput
for _, output := range in.Outputs {
o := spvwallet.TransactionOutput{
o := wallet.TransactionOutput{
ScriptPubKey: output.ScriptPubKey,
Value: int64(output.Value),
}
Expand All @@ -542,7 +543,7 @@ func (s *server) EstimateFee(ctx context.Context, in *pb.EstimateFeeData) (*pb.F
}

func (s *server) WalletNotify(in *pb.Empty, stream pb.API_WalletNotifyServer) error {
cb := func(tx spvwallet.TransactionCallback) {
cb := func(tx wallet.TransactionCallback) {
ts, err := ptypes.TimestampProto(tx.Timestamp)
if err != nil {
return
Expand Down
15 changes: 8 additions & 7 deletions cmd/spvwallet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/OpenBazaar/spvwallet/db"
"github.com/OpenBazaar/spvwallet/gui"
"github.com/OpenBazaar/spvwallet/gui/bootstrap"
wi "github.com/OpenBazaar/wallet-interface"
"github.com/asticode/go-astilectron"
"github.com/asticode/go-astilog"
"github.com/atotto/clipboard"
Expand Down Expand Up @@ -302,7 +303,7 @@ func (x *Start) Execute(args []string) error {
}

txc := make(chan uint32)
listener := func(spvwallet.TransactionCallback) {
listener := func(wi.TransactionCallback) {
h, _ := wallet.ChainTip()
txc <- h
}
Expand Down Expand Up @@ -371,7 +372,7 @@ func (x *Start) Execute(args []string) error {
}
w.Send(bootstrap.MessageOut{Name: "statsUpdate", Payload: st})
case "getAddress":
addr := wallet.CurrentAddress(spvwallet.EXTERNAL)
addr := wallet.CurrentAddress(wi.EXTERNAL)
w.Send(bootstrap.MessageOut{Name: "address", Payload: addr.EncodeAddress()})
case "send":
type P struct {
Expand All @@ -385,16 +386,16 @@ func (x *Start) Execute(args []string) error {
astilog.Errorf("Unmarshaling %s failed", m.Payload)
return
}
var feeLevel spvwallet.FeeLevel
var feeLevel wi.FeeLevel
switch strings.ToLower(p.FeeLevel) {
case "priority":
feeLevel = spvwallet.PRIOIRTY
feeLevel = wi.PRIOIRTY
case "normal":
feeLevel = spvwallet.NORMAL
feeLevel = wi.NORMAL
case "economic":
feeLevel = spvwallet.ECONOMIC
feeLevel = wi.ECONOMIC
default:
feeLevel = spvwallet.NORMAL
feeLevel = wi.NORMAL
}
addr, err := btcutil.DecodeAddress(p.Address, wallet.Params())
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spvwallet

import (
"github.com/OpenBazaar/wallet-interface"
"github.com/btcsuite/btcd/chaincfg"
"github.com/mitchellh/go-homedir"
"github.com/op/go-logging"
Expand Down Expand Up @@ -31,7 +32,7 @@ type Config struct {
RepoPath string

// An implementation of the Datastore interface
DB Datastore
DB wallet.Datastore

// If you wish to connect to a single trusted peer set this. Otherwise leave nil.
TrustedPeer net.Addr
Expand Down

0 comments on commit d8b7c75

Please sign in to comment.