Skip to content

Commit

Permalink
Merge pull request #8 from libsv/develop
Browse files Browse the repository at this point in the history
Develop to Master (Patch & CI Changes)
  • Loading branch information
mrz1836 authored Dec 25, 2020
2 parents adc5870 + 4d6d344 commit e4a51e7
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 10 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ on:
branches:
- "*"
schedule:
- cron: '* 22 * * *'
- cron: '1 4 * * *'

jobs:
test:
strategy:
matrix:
go-version: [ 1.15.x ]
#go-version: [ 1.14.x, 1.15.x ]
go-version: [ 1.14.x, 1.15.x ]
os: [ ubuntu-latest ]
runs-on: ${{ matrix.os }}
steps:
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Common makefile commands & variables between projects
include .make/Makefile.common
include .make/common.mk

# Common Golang makefile commands & variables between projects
include .make/Makefile.go
include .make/go.mk

## Not defined? Use default repo name which is the application
ifeq ($(REPO_NAME),)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ require (
github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173
github.com/bitcoinsv/bsvutil v0.0.0-20181216182056-1d77cf353ea9
github.com/stretchr/testify v1.6.1
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392
golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392 h1:xYJJ3S178yv++9zXV/hnr29plCAGO9vAFG9dorqaFQc=
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9 h1:sYNJzB4J8toYPQTM6pAkcmBRgw9SnQKP9oXCHfgy604=
golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
5 changes: 3 additions & 2 deletions internalsigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (is *InternalSigner) SignAuto(unsignedTx *Tx) (signedTx *Tx, inputsSigned [

for i, in := range unsignedTx.Inputs {
pubKeyHash, _ := in.PreviousTxScript.GetPublicKeyHash() // doesn't matter if returns error (not p2pkh)

pubKeyHashStr := hex.EncodeToString(pubKeyHash)

pubKeyHashStrFromPriv := hex.EncodeToString(crypto.Hash160(is.PrivateKey.PubKey().SerializeCompressed()))
Expand All @@ -68,10 +69,10 @@ func (is *InternalSigner) SignAuto(unsignedTx *Tx) (signedTx *Tx, inputsSigned [
if pubKeyHashStr == pubKeyHashStrFromPriv {

if signedTx, err = is.Sign(uint32(i), unsignedTx); err != nil {
inputsSigned = append(inputsSigned, i)
} else {
return
}

inputsSigned = append(inputsSigned, i)
}
}

Expand Down
132 changes: 132 additions & 0 deletions tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,138 @@ func TestTx_Change(t *testing.T) {
feeRate := float64(feePaid) / float64(txSize)
assert.Equal(t, 0.4860557768924303, feeRate)
})

t.Run("spend entire utxo - basic - change address", func(t *testing.T) {
tx := bt.NewTx()
assert.NotNil(t, tx)

err := tx.From(
"07912972e42095fe58daaf09161c5a5da57be47c2054dc2aaa52b30fefa1940b",
0,
"76a914af2590a45ae401651fdbdf59a76ad43d1862534088ac",
4000000)
assert.NoError(t, err)

err = tx.ChangeToAddress("mwV3YgnowbJJB3LcyCuqiKpdivvNNFiK7M", bt.DefaultFees())
assert.NoError(t, err)

var wif *bsvutil.WIF
wif, err = bsvutil.DecodeWIF("L3MhnEn1pLWcggeYLk9jdkvA2wUK1iWwwrGkBbgQRqv6HPCdRxuw")
assert.NoError(t, err)
assert.NotNil(t, wif)

_, err = tx.SignAuto(&bt.InternalSigner{PrivateKey: wif.PrivKey, SigHashFlag: 0})
assert.NoError(t, err)

assert.Equal(t, "01000000010b94a1ef0fb352aa2adc54207ce47ba55d5a1c1609afda58fe9520e472299107000000006a473044022049ee0c0f26c00e6a6b3af5990fc8296c66eab3e3e42ab075069b89b1be6fefec02206079e49dd8c9e1117ef06fbe99714d822620b1f0f5d19f32a1128f5d29b7c3c4412102c8803fdd437d902f08e3c2344cb33065c99d7c99982018ff9f7219c3dd352ff0ffffffff01a0083d00000000001976a914af2590a45ae401651fdbdf59a76ad43d1862534088ac00000000", tx.ToString())

assert.Equal(t, uint64(3999904), tx.Outputs[0].Satoshis)
})

t.Run("spend entire utxo - multi payouts - expected fee", func(t *testing.T) {
tx := bt.NewTx()
assert.NotNil(t, tx)

err := tx.From(
"07912972e42095fe58daaf09161c5a5da57be47c2054dc2aaa52b30fefa1940b",
0,
"76a914af2590a45ae401651fdbdf59a76ad43d1862534088ac",
4000000)
assert.NoError(t, err)

err = tx.PayTo("mxAoAyZFXX6LZBWhoam3vjm6xt9NxPQ15f", 1000000)
assert.NoError(t, err)

err = tx.PayTo("mxAoAyZFXX6LZBWhoam3vjm6xt9NxPQ15f", 3000000)
assert.NoError(t, err)

err = tx.ChangeToAddress("mwV3YgnowbJJB3LcyCuqiKpdivvNNFiK7M", bt.DefaultFees())
assert.NoError(t, err)

var wif *bsvutil.WIF
wif, err = bsvutil.DecodeWIF("L3MhnEn1pLWcggeYLk9jdkvA2wUK1iWwwrGkBbgQRqv6HPCdRxuw")
assert.NoError(t, err)
assert.NotNil(t, wif)

_, err = tx.SignAuto(&bt.InternalSigner{PrivateKey: wif.PrivKey, SigHashFlag: 0})
assert.NoError(t, err)

assert.Equal(t, "01000000010b94a1ef0fb352aa2adc54207ce47ba55d5a1c1609afda58fe9520e472299107000000006a47304402206bbb4b23349bdf86e6fbc9067226e9a7b15c977fa530999b39cd0a6d9c83360d02202dd8ffdc610e58b3fc92b44400d99e38c78866765f31acb40d98007a52e7a826412102c8803fdd437d902f08e3c2344cb33065c99d7c99982018ff9f7219c3dd352ff0ffffffff0240420f00000000001976a914b6aa34534d2b11e66b438c7525f819aee01e397c88acc0c62d00000000001976a914b6aa34534d2b11e66b438c7525f819aee01e397c88ac00000000", tx.ToString())

assert.Equal(t, uint64(1000000), tx.Outputs[0].Satoshis)
assert.Equal(t, uint64(3000000), tx.Outputs[1].Satoshis)
})

t.Run("spend entire utxo - multi payouts - incorrect fee", func(t *testing.T) {
tx := bt.NewTx()
assert.NotNil(t, tx)

err := tx.From(
"07912972e42095fe58daaf09161c5a5da57be47c2054dc2aaa52b30fefa1940b",
0,
"76a914af2590a45ae401651fdbdf59a76ad43d1862534088ac",
4000000)
assert.NoError(t, err)

err = tx.PayTo("mxAoAyZFXX6LZBWhoam3vjm6xt9NxPQ15f", 999995)
assert.NoError(t, err)

err = tx.PayTo("mxAoAyZFXX6LZBWhoam3vjm6xt9NxPQ15f", 3000000)
assert.NoError(t, err)

err = tx.ChangeToAddress("mwV3YgnowbJJB3LcyCuqiKpdivvNNFiK7M", bt.DefaultFees())
assert.NoError(t, err)

var wif *bsvutil.WIF
wif, err = bsvutil.DecodeWIF("L3MhnEn1pLWcggeYLk9jdkvA2wUK1iWwwrGkBbgQRqv6HPCdRxuw")
assert.NoError(t, err)
assert.NotNil(t, wif)

_, err = tx.SignAuto(&bt.InternalSigner{PrivateKey: wif.PrivKey, SigHashFlag: 0})
assert.NoError(t, err)

assert.Equal(t, "01000000010b94a1ef0fb352aa2adc54207ce47ba55d5a1c1609afda58fe9520e472299107000000006b483045022100fd07316603e9abf393e695192e8ce1e7f808d2735cc57039109a2210ad32d9a7022000e301e2a988b23ab3872b041df8b6eb0315238e0918944cbaf8b6abdde75cac412102c8803fdd437d902f08e3c2344cb33065c99d7c99982018ff9f7219c3dd352ff0ffffffff023b420f00000000001976a914b6aa34534d2b11e66b438c7525f819aee01e397c88acc0c62d00000000001976a914b6aa34534d2b11e66b438c7525f819aee01e397c88ac00000000", tx.ToString())

// todo: expected the pay-to inputs to change based on the fee :P

assert.Equal(t, uint64(999995), tx.Outputs[0].Satoshis)
assert.Equal(t, uint64(3000000), tx.Outputs[1].Satoshis)
})

t.Run("multiple inputs, spend all", func(t *testing.T) {
tx := bt.NewTx()
assert.NotNil(t, tx)

err := tx.From(
"9e88ca8eec0845e9e864c024bc5e6711e670932c9c7d929f9fccdb2c440ae28e",
0,
"76a9147824dec00be2c45dad83c9b5e9f5d7ef05ba3cf988ac",
5689)
assert.NoError(t, err)

err = tx.From(
"4e25b077d4cbb955b5a215feb53f963cf04688ff1777b9bea097c7ddbdf7ea42",
0,
"76a9147824dec00be2c45dad83c9b5e9f5d7ef05ba3cf988ac",
5689)
assert.NoError(t, err)

err = tx.ChangeToAddress("1BxGFoRPSFgYxoAStEncL6HuELqPkV3JVj", bt.DefaultFees())
assert.NoError(t, err)

var wif *bsvutil.WIF
wif, err = bsvutil.DecodeWIF("5JXAjNX7cbiWvmkdnj1EnTKPChauttKAJibXLm8tqWtDhXrRbKz")
assert.NoError(t, err)
assert.NotNil(t, wif)

is, err := tx.SignAuto(&bt.InternalSigner{PrivateKey: wif.PrivKey, SigHashFlag: 0})
assert.NoError(t, err)

assert.ElementsMatch(t, []int{0, 1}, is)
assert.Equal(t, 2, len(is))

assert.Equal(t, "01000000028ee20a442cdbcc9f9f927d9c2c9370e611675ebc24c064e8e94508ec8eca889e000000006b483045022100fa52a44cd8010ba646a8df6bac6e5e8aa93f24439521c2ce1c8fe6550e73c1750220636e30d757702a6777d8310090962d4bac2b3fd634127856d51b184f5c702c8f4121034aaeabc056f33fd960d1e43fc8a0672723af02f275e54c31381af66a334634caffffffff42eaf7bdddc797a0beb97717ff8846f03c963fb5fe15a2b555b9cbd477b0254e000000006b483045022100c201fd55ef33525b3eb0557fac77408b8ec7f6ea5b00d08512df105172f992d60220753b21519a416dcbeaf1a501d9c36de2aea9c83c6d258320500371819d0758e14121034aaeabc056f33fd960d1e43fc8a0672723af02f275e54c31381af66a334634caffffffff01c62b0000000000001976a9147824dec00be2c45dad83c9b5e9f5d7ef05ba3cf988ac00000000", tx.ToString())
})
}

func TestTx_HasDataOutputs(t *testing.T) {
Expand Down

0 comments on commit e4a51e7

Please sign in to comment.