diff --git a/input.go b/input.go index b4bd93ab..d938ff8a 100644 --- a/input.go +++ b/input.go @@ -117,11 +117,7 @@ func (i *Input) PreviousTxIDAddStr(txID string) error { if err != nil { return err } - if !IsValidTxID(bb) { - return ErrInvalidTxID - } - i.previousTxID = bb - return nil + return i.PreviousTxIDAdd(bb) } // PreviousTxID will return the PreviousTxID if set. diff --git a/localsigner.go b/localsigner.go index 5ffc8c03..65f314d0 100644 --- a/localsigner.go +++ b/localsigner.go @@ -7,8 +7,8 @@ import ( "github.com/libsv/go-bt/sighash" ) -// LocalSigner implements the Signer interface. It is used to sign Tx inputs locally -// using a bkec PrivateKey, any input found that can be signed by the key will be signed. +// LocalSigner implements the Signer interface. It is used to sign Tx Inputs locally +// using a bkec PrivateKey. type LocalSigner struct { PrivateKey *bec.PrivateKey } diff --git a/signaturehash.go b/signaturehash.go index 706a633e..52ef8d44 100644 --- a/signaturehash.go +++ b/signaturehash.go @@ -115,7 +115,7 @@ func (tx *Tx) CalcInputPreimage(inputNumber uint32, sigHashFlag sighash.Flag) ([ func (tx *Tx) getPreviousOutHash() []byte { buf := make([]byte, 0) - for _, in := range tx.Inputs() { + for _, in := range tx.Inputs { buf = append(buf, ReverseBytes(in.PreviousTxID())...) oi := make([]byte, 4) binary.LittleEndian.PutUint32(oi, in.PreviousTxOutIndex) @@ -128,7 +128,7 @@ func (tx *Tx) getPreviousOutHash() []byte { func (tx *Tx) getSequenceHash() []byte { buf := make([]byte, 0) - for _, in := range tx.Inputs() { + for _, in := range tx.Inputs { oi := make([]byte, 4) binary.LittleEndian.PutUint32(oi, in.SequenceNumber) buf = append(buf, oi...) @@ -141,11 +141,11 @@ func (tx *Tx) getOutputsHash(n int32) []byte { buf := make([]byte, 0) if n == -1 { - for _, out := range tx.Outputs() { + for _, out := range tx.Outputs { buf = append(buf, out.BytesForSigHash()...) } } else { - buf = append(buf, tx.Outputs()[n].BytesForSigHash()...) + buf = append(buf, tx.Outputs[n].BytesForSigHash()...) } return crypto.Sha256d(buf) diff --git a/signaturehash_test.go b/signaturehash_test.go index 8258a026..f3d1bbdf 100644 --- a/signaturehash_test.go +++ b/signaturehash_test.go @@ -124,8 +124,8 @@ func TestTx_CalcInputSignatureHash(t *testing.T) { assert.NotNil(t, tx) // Add the UTXO amount and script (PreviousTxScript already in unsiged tx) - tx.Inputs()[test.index].PreviousTxSatoshis = test.previousTxSatoshis - tx.Inputs()[test.index].PreviousTxScript, err = bscript.NewFromHexString(test.previousTxScript) + tx.Inputs[test.index].PreviousTxSatoshis = test.previousTxSatoshis + tx.Inputs[test.index].PreviousTxScript, err = bscript.NewFromHexString(test.previousTxScript) assert.NoError(t, err) var actualSigHash []byte diff --git a/signer.go b/signer.go index 6b433bd5..d8942070 100644 --- a/signer.go +++ b/signer.go @@ -25,8 +25,8 @@ type Signer interface { // canonical in accordance with RFC6979 and BIP0062. // // To automatically sign, the PublicKey() method must also be implemented in order to -// use the public key to check which inputs can be signed for before signing. +// use the public key to check which Inputs can be signed for before signing. type AutoSigner interface { Signer - PublicKey(ctx context.Context ) (publicKey []byte) + PublicKey(ctx context.Context) (publicKey []byte) } diff --git a/tx.go b/tx.go index 08f8bff9..50a39aae 100644 --- a/tx.go +++ b/tx.go @@ -20,12 +20,12 @@ Version no currently 1 In-counter positive integer VI = VarInt 1 - 9 bytes -list of inputs the first input of the first transaction is also called "coinbase" -many inputs +list of Inputs the first input of the first transaction is also called "coinbase" -many Inputs (its content was ignored in earlier versions) Out-counter positive integer VI = VarInt 1 - 9 bytes -list of outputs the outputs of the first transaction spend the mined -many outputs +list of Outputs the Outputs of the first transaction spend the mined -many Outputs bitcoins for the block lock_time if non-zero and sequence numbers are < 0xFFFFFFFF: block height or 4 bytes @@ -43,8 +43,8 @@ var ( // DO NOT CHANGE ORDER - Optimised memory via malign // type Tx struct { - inputs []*Input - outputs []*Output + Inputs []*Input + Outputs []*Output Version uint32 LockTime uint32 } @@ -65,14 +65,14 @@ func (tx *Tx) MarshalJSON() ([]byte, error) { if tx == nil { return nil, errors.New("tx is nil so cannot be marshalled") } - for i, o := range tx.outputs { + for i, o := range tx.Outputs { o.index = i } txj := txJSON{ Version: tx.Version, LockTime: tx.LockTime, - Inputs: tx.inputs, - Outputs: tx.outputs, + Inputs: tx.Inputs, + Outputs: tx.Outputs, TxID: tx.TxID(), Hash: tx.TxID(), Size: len(tx.Bytes()), @@ -96,8 +96,8 @@ func (tx *Tx) UnmarshalJSON(b []byte) error { *tx = *t return nil } - tx.inputs = txj.Inputs - tx.outputs = txj.Outputs + tx.Inputs = txj.Inputs + tx.Outputs = txj.Outputs tx.LockTime = txj.LockTime tx.Version = txj.Version return nil @@ -152,7 +152,7 @@ func NewTxFromStream(b []byte) (*Tx, int, error) { inputCount, size := DecodeVarInt(b[offset:]) offset += size - // create inputs + // create Inputs var i uint64 var err error var input *Input @@ -165,7 +165,7 @@ func NewTxFromStream(b []byte) (*Tx, int, error) { t.addInput(input) } - // create outputs + // create Outputs var outputCount uint64 var output *Output outputCount, size = DecodeVarInt(b[offset:]) @@ -189,7 +189,7 @@ func NewTxFromStream(b []byte) (*Tx, int, error) { // HasDataOutputs returns true if the transaction has // at least one data (OP_RETURN) output in it. func (tx *Tx) HasDataOutputs() bool { - for _, out := range tx.Outputs() { + for _, out := range tx.Outputs { if out.LockingScript.IsData() { return true } @@ -197,11 +197,6 @@ func (tx *Tx) HasDataOutputs() bool { return false } -// Inputs returns the inputs for the transaction. -func (tx *Tx) Inputs() []*Input { - return tx.inputs -} - // InputIdx will return the input at the specified index. // // This will consume an overflow error and simply return nil if the input @@ -210,12 +205,7 @@ func (tx *Tx) InputIdx(i int) *Input { if i > tx.InputCount()-1 { return nil } - return tx.inputs[i] -} - -// Outputs returns the outputs for the transaction. -func (tx *Tx) Outputs() []*Output { - return tx.outputs + return tx.Inputs[i] } // OutputIdx will return the output at the specified index. @@ -226,23 +216,23 @@ func (tx *Tx) OutputIdx(i int) *Output { if i > tx.OutputCount()-1 { return nil } - return tx.outputs[i] + return tx.Outputs[i] } // IsCoinbase determines if this transaction is a coinbase by // checking if the tx input is a standard coinbase input. func (tx *Tx) IsCoinbase() bool { - if len(tx.inputs) != 1 { + if len(tx.Inputs) != 1 { return false } cbi := make([]byte, 32) - if !bytes.Equal(tx.inputs[0].PreviousTxID(), cbi) { + if !bytes.Equal(tx.Inputs[0].PreviousTxID(), cbi) { return false } - if tx.inputs[0].PreviousTxOutIndex == DefaultSequenceNumber || tx.inputs[0].SequenceNumber == DefaultSequenceNumber { + if tx.Inputs[0].PreviousTxOutIndex == DefaultSequenceNumber || tx.Inputs[0].SequenceNumber == DefaultSequenceNumber { return true } @@ -268,15 +258,9 @@ func (tx *Tx) String() string { // IsValidTxID will check that the txid bytes are valid. // -// A txid should be in hexadecimal and be of 32 bytes length. +// A txid should be of 32 bytes length. func IsValidTxID(txid []byte) bool { - if len(txid) != 32 { - return false - } - if s := hex.EncodeToString(txid); s == "" { - return false - } - return true + return len(txid) == 32 } // Bytes encodes the transaction into a byte array. @@ -285,7 +269,7 @@ func (tx *Tx) Bytes() []byte { return tx.toBytesHelper(0, nil) } -// BytesWithClearedInputs encodes the transaction into a byte array but clears its inputs first. +// BytesWithClearedInputs encodes the transaction into a byte array but clears its Inputs first. // This is used when signing transactions. func (tx *Tx) BytesWithClearedInputs(index int, lockingScript []byte) []byte { return tx.toBytesHelper(index, lockingScript) @@ -296,9 +280,9 @@ func (tx *Tx) toBytesHelper(index int, lockingScript []byte) []byte { h = append(h, LittleEndianBytes(tx.Version, 4)...) - h = append(h, VarInt(uint64(len(tx.inputs)))...) + h = append(h, VarInt(uint64(len(tx.Inputs)))...) - for i, in := range tx.inputs { + for i, in := range tx.Inputs { s := in.Bytes(lockingScript != nil) if i == index && lockingScript != nil { h = append(h, VarInt(uint64(len(lockingScript)))...) @@ -308,8 +292,8 @@ func (tx *Tx) toBytesHelper(index int, lockingScript []byte) []byte { } } - h = append(h, VarInt(uint64(len(tx.outputs)))...) - for _, out := range tx.outputs { + h = append(h, VarInt(uint64(len(tx.Outputs)))...) + for _, out := range tx.Outputs { h = append(h, out.Bytes()...) } diff --git a/tx_test.go b/tx_test.go index e0c5429a..948946e9 100644 --- a/tx_test.go +++ b/tx_test.go @@ -34,7 +34,7 @@ func TestNewTx(t *testing.T) { func TestNewTxFromString(t *testing.T) { t.Parallel() - t.Run("valid tx no inputs", func(t *testing.T) { + t.Run("valid tx no Inputs", func(t *testing.T) { tx, err := bt.NewTxFromString("01000000000100000000000000001a006a07707265666978310c6578616d706c65206461746102133700000000") assert.NoError(t, err) assert.NotNil(t, tx) @@ -58,10 +58,10 @@ func TestNewTxFromString(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, tx) - // Check version, locktime, inputs + // Check version, locktime, Inputs assert.Equal(t, uint32(2), tx.Version) assert.Equal(t, uint32(0), tx.LockTime) - assert.Equal(t, 1, len(tx.Inputs())) + assert.Equal(t, 1, len(tx.Inputs)) // Create a new unlocking script //ptid, _ := hex.DecodeString("9c5b1428aaad5e9b0196c89be8628b366f33c7b22933da0489b921d487a7cb1c") @@ -78,7 +78,7 @@ func TestNewTxFromString(t *testing.T) { assert.Equal(t, tx.InputIdx(0), i) // Check output - assert.Equal(t, 1, len(tx.Outputs())) + assert.Equal(t, 1, len(tx.Outputs)) // New output var ls *bscript.Script @@ -88,7 +88,7 @@ func TestNewTxFromString(t *testing.T) { // Check the type o := bt.Output{Satoshis: 4999000000, LockingScript: ls} - assert.Equal(t, true, reflect.DeepEqual(*tx.Outputs()[0], o)) + assert.Equal(t, true, reflect.DeepEqual(*tx.Outputs[0], o)) }) } @@ -148,7 +148,7 @@ func TestVersion(t *testing.T) { func TestTx_IsCoinbase(t *testing.T) { t.Parallel() - t.Run("invalid number of inputs", func(t *testing.T) { + t.Run("invalid number of Inputs", func(t *testing.T) { tx := bt.NewTx() assert.NotNil(t, tx) assert.Equal(t, false, tx.IsCoinbase()) @@ -217,7 +217,7 @@ func TestTx_CreateTx(t *testing.T) { func TestTx_HasDataOutputs(t *testing.T) { t.Parallel() - t.Run("has data outputs", func(t *testing.T) { + t.Run("has data Outputs", func(t *testing.T) { tx := bt.NewTx() assert.NotNil(t, tx) @@ -249,7 +249,7 @@ func TestTx_HasDataOutputs(t *testing.T) { assert.Equal(t, true, tx.HasDataOutputs()) }) - t.Run("no data outputs", func(t *testing.T) { + t.Run("no data Outputs", func(t *testing.T) { tx := bt.NewTx() assert.NotNil(t, tx) @@ -397,7 +397,7 @@ func TestTx_MarshallJSON(t *testing.T) { } ] }`, - }, "transaction with multiple inputs": { + }, "transaction with multiple Inputs": { tx: func() *bt.Tx { tx := bt.NewTx() assert.NoError(t, tx.From( @@ -655,7 +655,7 @@ func TestTx_OutputIdx(t *testing.T) { idx int expOutput *bt.Output }{ - "tx with 3 outputs and output idx 0 requested should return output": { + "tx with 3 Outputs and output idx 0 requested should return output": { tx: func() *bt.Tx { tx := bt.NewTx() assert.NoError(t, tx.PayToAddress("myUmQeCYxQECGHXbupe539n41u6BTBz1Eh", 1000)) @@ -672,7 +672,7 @@ func TestTx_OutputIdx(t *testing.T) { return s }(), }, - }, "tx with 3 outputs and output idx 2 requested should return output": { + }, "tx with 3 Outputs and output idx 2 requested should return output": { tx: func() *bt.Tx { tx := bt.NewTx() assert.NoError(t, tx.PayToAddress("myUmQeCYxQECGHXbupe539n41u6BTBz1Eh", 1000)) @@ -689,7 +689,7 @@ func TestTx_OutputIdx(t *testing.T) { return s }(), }, - }, "tx with 3 outputs and output idx 5 requested should return nil": { + }, "tx with 3 Outputs and output idx 5 requested should return nil": { tx: func() *bt.Tx { tx := bt.NewTx() assert.NoError(t, tx.PayToAddress("myUmQeCYxQECGHXbupe539n41u6BTBz1Eh", 1000)) @@ -699,7 +699,7 @@ func TestTx_OutputIdx(t *testing.T) { }(), idx: 5, expOutput: nil, - }, "tx with 0 outputs and output idx 5 requested should return nil": { + }, "tx with 0 Outputs and output idx 5 requested should return nil": { tx: func() *bt.Tx { return bt.NewTx() }(), @@ -722,7 +722,7 @@ func TestTx_InputIdx(t *testing.T) { idx int expInput *bt.Input }{ - "tx with 3 inputs and input idx 0 requested should return correct input": { + "tx with 3 Inputs and input idx 0 requested should return correct input": { tx: func() *bt.Tx { tx := bt.NewTx() assert.NoError(t, tx.From( @@ -757,7 +757,7 @@ func TestTx_InputIdx(t *testing.T) { _ = in.PreviousTxIDAddStr("3c8edde27cb9a9132c22038dac4391496be9db16fd21351565cc1006966fdad5") return in }(), - }, "tx with 3 outputs and output idx 2 requested should return output": { + }, "tx with 3 Outputs and output idx 2 requested should return output": { tx: func() *bt.Tx { tx := bt.NewTx() assert.NoError(t, tx.From( @@ -792,7 +792,7 @@ func TestTx_InputIdx(t *testing.T) { _ = in.PreviousTxIDAddStr("3c8edde27cb9a9132c22038dac4391496be9db16fd21351565cc1006966fdac4") return in }(), - }, "tx with 3 outputs and output idx 5 requested should return nil": { + }, "tx with 3 Outputs and output idx 5 requested should return nil": { tx: func() *bt.Tx { tx := bt.NewTx() assert.NoError(t, tx.From( @@ -814,7 +814,7 @@ func TestTx_InputIdx(t *testing.T) { }(), idx: 5, expInput: nil, - }, "tx with 0 outputs and output idx 5 requested should return nil": { + }, "tx with 0 Outputs and output idx 5 requested should return nil": { tx: func() *bt.Tx { return bt.NewTx() }(), diff --git a/txchange.go b/txchange.go index a877b612..5f89bf7b 100644 --- a/txchange.go +++ b/txchange.go @@ -26,7 +26,7 @@ func (tx *Tx) Change(s *bscript.Script, f []*Fee) error { } if hasChange { // add rest of available sats to the change output - tx.Outputs()[tx.OutputCount()-1].Satoshis = available + tx.Outputs[tx.OutputCount()-1].Satoshis = available } return nil } @@ -35,14 +35,14 @@ func (tx *Tx) Change(s *bscript.Script, f []*Fee) error { // If an invalid index is supplied and error is returned. func (tx *Tx) ChangeToExistingOutput(index uint, f []*Fee) error { if int(index) > tx.OutputCount()-1 { - return errors.New("index is greater than number of inputs in transaction") + return errors.New("index is greater than number of Inputs in transaction") } - available, hasChange, err := tx.change(tx.Outputs()[index].LockingScript, f, false) + available, hasChange, err := tx.change(tx.Outputs[index].LockingScript, f, false) if err != nil { return err } if hasChange { - tx.Outputs()[index].Satoshis += available + tx.Outputs[index].Satoshis += available } return nil } @@ -101,7 +101,7 @@ func (tx *Tx) canAddChange(available uint64, standardFees *Fee) bool { varIntUpper := VarIntUpperLimitInc(uint64(tx.OutputCount())) if varIntUpper == -1 { - return false // upper limit of outputs in one tx reached + return false // upper limit of Outputs in one tx reached } changeOutputFee := uint64(varIntUpper) @@ -144,7 +144,7 @@ func (tx *Tx) getExpectedUnlockingScriptFees(f []*Fee) (uint64, error) { var expectedBytes int - for _, in := range tx.Inputs() { + for _, in := range tx.Inputs { if !in.PreviousTxScript.IsP2PKH() { return 0, errors.New("non-P2PKH input used in the tx - unsupported") } @@ -155,8 +155,8 @@ func (tx *Tx) getExpectedUnlockingScriptFees(f []*Fee) (uint64, error) { } func (tx *Tx) getStandardAndDataBytes() (standardBytes, dataBytes int) { - // Subtract the value of each output as well as keeping track of data outputs - for _, out := range tx.Outputs() { + // Subtract the value of each output as well as keeping track of data Outputs + for _, out := range tx.Outputs { if out.LockingScript.IsData() && len(*out.LockingScript) > 0 { dataBytes += len(*out.LockingScript) } diff --git a/txchange_test.go b/txchange_test.go index 09888aa3..d24b9a05 100644 --- a/txchange_test.go +++ b/txchange_test.go @@ -53,7 +53,7 @@ func TestTx_ChangeToAddress(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 1, tx.OutputCount()) - assert.Equal(t, "76a914a7a1a7fd7d279b57b84e596cbbf82608efdb441a88ac", tx.Outputs()[0].LockingScript.String()) + assert.Equal(t, "76a914a7a1a7fd7d279b57b84e596cbbf82608efdb441a88ac", tx.Outputs[0].LockingScript.String()) }) } @@ -113,7 +113,7 @@ func TestTx_Change(t *testing.T) { assert.NoError(t, err) // Correct fee for the tx - assert.Equal(t, uint64(3999904), tx.Outputs()[0].Satoshis) + assert.Equal(t, uint64(3999904), tx.Outputs[0].Satoshis) // Correct script hex string assert.Equal(t, @@ -193,7 +193,7 @@ func TestTx_Change(t *testing.T) { assert.Equal(t, "01000000010b94a1ef0fb352aa2adc54207ce47ba55d5a1c1609afda58fe9520e472299107000000006a473044022049ee0c0f26c00e6a6b3af5990fc8296c66eab3e3e42ab075069b89b1be6fefec02206079e49dd8c9e1117ef06fbe99714d822620b1f0f5d19f32a1128f5d29b7c3c4412102c8803fdd437d902f08e3c2344cb33065c99d7c99982018ff9f7219c3dd352ff0ffffffff01a0083d00000000001976a914af2590a45ae401651fdbdf59a76ad43d1862534088ac00000000", tx.String()) - assert.Equal(t, uint64(3999904), tx.Outputs()[0].Satoshis) + assert.Equal(t, uint64(3999904), tx.Outputs[0].Satoshis) }) t.Run("spend entire utxo - multi payouts - expected fee", func(t *testing.T) { @@ -226,8 +226,8 @@ func TestTx_Change(t *testing.T) { assert.Equal(t, "01000000010b94a1ef0fb352aa2adc54207ce47ba55d5a1c1609afda58fe9520e472299107000000006a47304402206bbb4b23349bdf86e6fbc9067226e9a7b15c977fa530999b39cd0a6d9c83360d02202dd8ffdc610e58b3fc92b44400d99e38c78866765f31acb40d98007a52e7a826412102c8803fdd437d902f08e3c2344cb33065c99d7c99982018ff9f7219c3dd352ff0ffffffff0240420f00000000001976a914b6aa34534d2b11e66b438c7525f819aee01e397c88acc0c62d00000000001976a914b6aa34534d2b11e66b438c7525f819aee01e397c88ac00000000", tx.String()) - assert.Equal(t, uint64(1000000), tx.Outputs()[0].Satoshis) - assert.Equal(t, uint64(3000000), tx.Outputs()[1].Satoshis) + 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) { @@ -260,13 +260,13 @@ func TestTx_Change(t *testing.T) { assert.Equal(t, "01000000010b94a1ef0fb352aa2adc54207ce47ba55d5a1c1609afda58fe9520e472299107000000006b483045022100fd07316603e9abf393e695192e8ce1e7f808d2735cc57039109a2210ad32d9a7022000e301e2a988b23ab3872b041df8b6eb0315238e0918944cbaf8b6abdde75cac412102c8803fdd437d902f08e3c2344cb33065c99d7c99982018ff9f7219c3dd352ff0ffffffff023b420f00000000001976a914b6aa34534d2b11e66b438c7525f819aee01e397c88acc0c62d00000000001976a914b6aa34534d2b11e66b438c7525f819aee01e397c88ac00000000", tx.String()) - // todo: expected the pay-to inputs to change based on the fee :P + // 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) + 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) { + t.Run("multiple Inputs, spend all", func(t *testing.T) { tx := bt.NewTx() assert.NotNil(t, tx) @@ -375,7 +375,7 @@ func TestTx_ChangeToOutput(t *testing.T) { }(), index: 1, fees: bt.DefaultFees(), - err: errors.New("index is greater than number of inputs in transaction"), + err: errors.New("index is greater than number of Inputs in transaction"), }, } for name, test := range tests { @@ -387,7 +387,7 @@ func TestTx_ChangeToOutput(t *testing.T) { return } assert.Equal(t, test.expOutputTotal, test.tx.TotalOutputSatoshis()) - assert.Equal(t, test.expChangeOutput, test.tx.Outputs()[test.index].Satoshis) + assert.Equal(t, test.expChangeOutput, test.tx.Outputs[test.index].Satoshis) }) } } @@ -412,7 +412,7 @@ func TestTx_CalculateChange(t *testing.T) { }(), fees: bt.DefaultFees(), expFees: 96, - }, "Transaction with one input 4 outputs should return 147": { + }, "Transaction with one input 4 Outputs should return 147": { tx: func() *bt.Tx { tx := bt.NewTx() assert.NoError(t, tx.From( diff --git a/txinput.go b/txinput.go index 04eec3cf..db3e89f0 100644 --- a/txinput.go +++ b/txinput.go @@ -36,20 +36,20 @@ func NewInputFromBytes(bytes []byte) (*Input, int, error) { // TotalInputSatoshis returns the total Satoshis inputted to the transaction. func (tx *Tx) TotalInputSatoshis() (total uint64) { - for _, in := range tx.inputs { + for _, in := range tx.Inputs { total += in.PreviousTxSatoshis } return } func (tx *Tx) addInput(input *Input) { - tx.inputs = append(tx.inputs, input) + tx.Inputs = append(tx.Inputs, input) } -// AddInputFromTx will add all outputs of given previous transaction +// AddP2PKHInputsFromTx will add all Outputs of given previous transaction // that match a specific public key to your transaction. -func (tx *Tx) AddInputFromTx(pvsTx *Tx, matchPK []byte) error { - for i, utxo := range pvsTx.outputs { +func (tx *Tx) AddP2PKHInputsFromTx(pvsTx *Tx, matchPK []byte) error { + for i, utxo := range pvsTx.Outputs { utxoPkHASH160, err := utxo.LockingScript.PublicKeyHash() if err != nil { return err @@ -88,7 +88,7 @@ func (tx *Tx) From(prevTxID string, vout uint32, prevTxLockingScript string, sat return nil } -// InputCount returns the number of transaction inputs. +// InputCount returns the number of transaction Inputs. func (tx *Tx) InputCount() int { - return len(tx.inputs) + return len(tx.Inputs) } diff --git a/txinput_test.go b/txinput_test.go index 383b794d..ee3f64fd 100644 --- a/txinput_test.go +++ b/txinput_test.go @@ -21,7 +21,7 @@ func TestAddInputFromTx(t *testing.T) { assert.NoError(t, err) newTx := bt.NewTx() - err = newTx.AddInputFromTx(prvTx, pubkey1) + err = newTx.AddP2PKHInputsFromTx(prvTx, pubkey1) assert.NoError(t, err) assert.Equal(t, newTx.InputCount(), 2) // only 2 utxos added assert.Equal(t, newTx.TotalInputSatoshis(), uint64(200000)) @@ -70,7 +70,7 @@ func TestTx_From(t *testing.T) { 4000000) assert.NoError(t, err) - inputs := tx.Inputs() + inputs := tx.Inputs assert.Equal(t, 1, len(inputs)) assert.Equal(t, "07912972e42095fe58daaf09161c5a5da57be47c2054dc2aaa52b30fefa1940b", hex.EncodeToString(inputs[0].PreviousTxID())) assert.Equal(t, uint32(0), inputs[0].PreviousTxOutIndex) diff --git a/txoutput.go b/txoutput.go index adac4cfb..479952d0 100644 --- a/txoutput.go +++ b/txoutput.go @@ -36,7 +36,7 @@ func NewOutputFromBytes(bytes []byte) (*Output, int, error) { // TotalOutputSatoshis returns the total Satoshis outputted from the transaction. func (tx *Tx) TotalOutputSatoshis() (total uint64) { - for _, o := range tx.outputs { + for _, o := range tx.Outputs { total += o.Satoshis } return @@ -178,14 +178,14 @@ func createOpReturnOutput(data [][]byte) (*Output, error) { return &Output{LockingScript: s}, nil } -// OutputCount returns the number of transaction inputs. +// OutputCount returns the number of transaction Inputs. func (tx *Tx) OutputCount() int { - return len(tx.outputs) + return len(tx.Outputs) } // AddOutput adds a new output to the transaction. func (tx *Tx) AddOutput(output *Output) { - tx.outputs = append(tx.outputs, output) + tx.Outputs = append(tx.Outputs, output) } // PayTo creates a new P2PKH output from a BitCoin address (base58) diff --git a/txoutput_test.go b/txoutput_test.go index 157f334f..fd406168 100644 --- a/txoutput_test.go +++ b/txoutput_test.go @@ -24,7 +24,7 @@ func TestNewP2PKHOutputFromPubKeyHashStr(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "76a91488ac", - tx.Outputs()[0].LockingScriptHexString(), + tx.Outputs[0].LockingScriptHexString(), ) }) @@ -47,7 +47,7 @@ func TestNewP2PKHOutputFromPubKeyHashStr(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "76a9148fe80c75c9560e8b56ed64ea3c26e18d2c52211b88ac", - tx.Outputs()[0].LockingScriptHexString(), + tx.Outputs[0].LockingScriptHexString(), ) }) } @@ -68,7 +68,7 @@ func TestNewHashPuzzleOutput(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "a914b472a266d0bd89c13706a4132ccfb16f7c3b9fcb8876a90088ac", - tx.Outputs()[0].LockingScriptHexString(), + tx.Outputs[0].LockingScriptHexString(), ) }) @@ -83,7 +83,7 @@ func TestNewHashPuzzleOutput(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "a914d3f9e3d971764be5838307b175ee4e08ba427b908876a914c28f832c3d539933e0c719297340b34eee0f4c3488ac", - tx.Outputs()[0].LockingScriptHexString(), + tx.Outputs[0].LockingScriptHexString(), ) }) } @@ -100,7 +100,7 @@ func TestNewOpReturnOutput(t *testing.T) { err := tx.AddOpReturnOutput(dataBytes) assert.NoError(t, err) - script := tx.Outputs()[0].LockingScriptHexString() + script := tx.Outputs[0].LockingScriptHexString() dataLength := bt.VarInt(uint64(len(dataBytes))) assert.Equal(t, "006a4d2201"+hex.EncodeToString(dataBytes), script) @@ -115,7 +115,7 @@ func TestNewOpReturnPartsOutput(t *testing.T) { err := tx.AddOpReturnPartsOutput(dataBytes) assert.NoError(t, err) - assert.Equal(t, "006a02686903686f770361726503796f75", tx.Outputs()[0].LockingScriptHexString()) + assert.Equal(t, "006a02686903686f770361726503796f75", tx.Outputs[0].LockingScriptHexString()) } func TestTx_TotalOutputSatoshis(t *testing.T) { @@ -128,7 +128,7 @@ func TestTx_TotalOutputSatoshis(t *testing.T) { assert.Equal(t, uint64((29.89999582+20.00)*1e8), tx.TotalOutputSatoshis()) }) - t.Run("zero outputs", func(t *testing.T) { + t.Run("zero Outputs", func(t *testing.T) { tx := bt.NewTx() assert.NotNil(t, tx) assert.Equal(t, uint64(0), tx.TotalOutputSatoshis()) diff --git a/txsign.go b/txsign.go index 22c086fe..34afcda1 100644 --- a/txsign.go +++ b/txsign.go @@ -5,8 +5,8 @@ import ( "encoding/hex" "fmt" - "github.com/libsv/go-bt/bscript" "github.com/libsv/go-bk/crypto" + "github.com/libsv/go-bt/bscript" "github.com/libsv/go-bt/sighash" ) @@ -66,15 +66,15 @@ func (tx *Tx) ApplyP2PKHUnlockingScript(index uint32, pubKey []byte, sig []byte, // ApplyUnlockingScript applies a script to the transaction at a specific index in // unlocking script field. func (tx *Tx) ApplyUnlockingScript(index uint32, s *bscript.Script) error { - if tx.inputs[index] != nil { - tx.inputs[index].UnlockingScript = s + if tx.Inputs[index] != nil { + tx.Inputs[index].UnlockingScript = s return nil } return fmt.Errorf("no input at index %d", index) } -// SignAuto is used to automatically check which P2PKH inputs are +// SignAuto is used to automatically check which P2PKH Inputs are // able to be signed (match the public key) and then sign them. // It takes a Signed interface as a parameter so that different // signing implementations can be used to sign the transaction - @@ -82,7 +82,7 @@ func (tx *Tx) ApplyUnlockingScript(index uint32, s *bscript.Script) error { func (tx *Tx) SignAuto(ctx context.Context, s AutoSigner) (inputsSigned []int, err error) { shf := sighash.AllForkID // use SIGHASHALLFORFORKID to sign automatically - for i, in := range tx.inputs { + for i, in := range tx.Inputs { pubKeyHash, _ := in.PreviousTxScript.PublicKeyHash() // doesn't matter if returns error (not p2pkh) pubKeyHashStr := hex.EncodeToString(pubKeyHash)