Skip to content

Commit

Permalink
Merge pull request #59 from OpenBazaar/fix-overflow-spend
Browse files Browse the repository at this point in the history
Fix overflow Spend and IsDust
  • Loading branch information
placer14 committed Feb 13, 2020
2 parents 10951cd + dcf6bc4 commit 41f07b1
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 37 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
language: go
go:
- 1.11
sudo: required
services:
- docker
env:
- "PATH=/home/travis/gopath/bin:$PATH"
- DEP_VERSION=0.5.4 PATH=/home/travis/gopath/bin:${PATH}
before_install:
- curl -L -s https://github.com/golang/dep/releases/download/v${DEP_VERSION}/dep-linux-amd64 -o $GOPATH/bin/dep
- chmod +x $GOPATH/bin/dep
- go get github.com/tcnksm/ghr
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
install:
- dep ensure
script:
- diff -u <(echo -n) <(gofmt -d -s $(find . -type f -name '*.go' -not -path "./cmd/spvwallet/vendor/*" -not -path "./gui/resources.go" -not -path "./vendor/*"))
- cd $TRAVIS_BUILD_DIR && chmod a+x test_compile.sh && ./test_compile.sh
Expand Down
10 changes: 10 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM golang:1.11
VOLUME /var/lib/openbazaar

WORKDIR /go/src/github.com/OpenBazaar/spvwallet
RUN curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh && \
go get -u github.com/derekparker/delve/cmd/dlv

COPY . .

ENTRYPOINT ["/bin/bash"]
50 changes: 25 additions & 25 deletions Gopkg.lock

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

10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3'
services:
dev:
build:
context: .
dockerfile: Dockerfile.dev
volumes:
- .:/go/src/github.com/OpenBazaar/spvwallet
security_opt:
- seccomp:unconfined #req: delve for golang
24 changes: 15 additions & 9 deletions sortsignsend.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"math/big"
"strconv"
"time"

"github.com/OpenBazaar/wallet-interface"
Expand Down Expand Up @@ -56,16 +57,20 @@ func (c *Coin) PkScript() []byte { return c.ScriptPubKey }
func (c *Coin) NumConfs() int64 { return c.TxNumConfs }
func (c *Coin) ValueAge() int64 { return int64(c.TxValue) * c.TxNumConfs }

func NewCoin(txid []byte, index uint32, value btc.Amount, numConfs int64, scriptPubKey []byte) coinset.Coin {
func newCoin(txid []byte, index uint32, value string, numConfs int64, scriptPubKey []byte) (coinset.Coin, error) {
iVal, err := strconv.Atoi(value)
if err != nil {
return nil, fmt.Errorf("coin value (%s) is invalid", value)
}
shaTxid, _ := chainhash.NewHash(txid)
c := &Coin{
TxHash: shaTxid,
TxIndex: index,
TxValue: value,
TxValue: btc.Amount(iVal),
TxNumConfs: numConfs,
ScriptPubKey: scriptPubKey,
}
return coinset.Coin(c)
return coinset.Coin(c), nil
}

func (w *SPVWallet) gatherCoins() map[coinset.Coin]*hd.ExtendedKey {
Expand All @@ -80,8 +85,10 @@ func (w *SPVWallet) gatherCoins() map[coinset.Coin]*hd.ExtendedKey {
if u.AtHeight > 0 {
confirmations = int32(height) - u.AtHeight
}
val0, _ := new(big.Int).SetString(u.Value, 10)
c := NewCoin(u.Op.Hash.CloneBytes(), u.Op.Index, btc.Amount(val0.Int64()), int64(confirmations), u.ScriptPubkey)
c, err := newCoin(u.Op.Hash.CloneBytes(), u.Op.Index, u.Value, int64(confirmations), u.ScriptPubkey)
if err != nil {
continue
}
addr, err := w.ScriptToAddress(u.ScriptPubkey)
if err != nil {
continue
Expand Down Expand Up @@ -605,7 +612,7 @@ func (w *SPVWallet) SweepAddress(ins []wallet.TransactionInput, address *btc.Add
func (w *SPVWallet) buildTx(amount int64, addr btc.Address, feeLevel wallet.FeeLevel, optionalOutput *wire.TxOut) (*wire.MsgTx, error) {
// Check for dust
script, _ := txscript.PayToAddrScript(addr)
if txrules.IsDustAmount(btc.Amount(amount), len(script), txrules.DefaultRelayFeePerKb) {
if isTxSizeDust(*big.NewInt(amount), len(script)) {
return nil, wallet.ErrorDustAmount
}

Expand Down Expand Up @@ -743,7 +750,7 @@ func (w *SPVWallet) buildSpendAllTx(addr btc.Address, feeLevel wallet.FeeLevel)
fee := int64(estimatedSize) * feePerByte.Int64()

// Check for dust output
if txrules.IsDustAmount(btc.Amount(totalIn-fee), len(script), txrules.DefaultRelayFeePerKb) {
if isTxSizeDust(*big.NewInt(totalIn - fee), len(script)) {
return nil, wallet.ErrorDustAmount
}

Expand Down Expand Up @@ -815,8 +822,7 @@ func NewUnsignedTransaction(outputs []*wire.TxOut, feePerKb btc.Amount, fetchInp
}
changeIndex := -1
changeAmount := inputAmount - targetAmount - maxRequiredFee
if changeAmount != 0 && !txrules.IsDustAmount(changeAmount,
P2PKHOutputSize, txrules.DefaultRelayFeePerKb) {
if changeAmount != 0 && !isTxSizeDust(*big.NewInt(int64(changeAmount)), P2PKHOutputSize) {
changeScript, err := fetchChange()
if err != nil {
return nil, err
Expand Down
9 changes: 8 additions & 1 deletion wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,14 @@ func (w *SPVWallet) CurrencyCode() string {
}

func (w *SPVWallet) IsDust(amount big.Int) bool {
return txrules.IsDustAmount(btc.Amount(amount.Int64()), 25, txrules.DefaultRelayFeePerKb)
return isTxSizeDust(amount, 25)
}

func isTxSizeDust(amount big.Int, size int) bool {
if !amount.IsInt64() || amount.Cmp(big.NewInt(0)) <= 0 {
return false
}
return txrules.IsDustAmount(btc.Amount(amount.Int64()), size, txrules.DefaultRelayFeePerKb)
}

func (w *SPVWallet) MasterPrivateKey() *hd.ExtendedKey {
Expand Down

0 comments on commit 41f07b1

Please sign in to comment.