Skip to content

Commit

Permalink
Merge pull request #17 from libsv/feat/PreviousTxIDBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
jadwahab authored Feb 25, 2021
2 parents e5e4bc0 + d10c320 commit d907793
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
14 changes: 10 additions & 4 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sequence_no normally 0xFFFFFFFF; irrelevant unless transaction's
// DO NOT CHANGE ORDER - Optimized for memory via maligned
//
type Input struct {
PreviousTxIDBytes []byte
PreviousTxID string
PreviousTxSatoshis uint64
PreviousTxScript *bscript.Script
Expand Down Expand Up @@ -61,6 +62,7 @@ func NewInputFromBytes(bytes []byte) (*Input, int, error) {
}

return &Input{
PreviousTxIDBytes: ReverseBytes(bytes[0:32]),
PreviousTxID: hex.EncodeToString(ReverseBytes(bytes[0:32])),
PreviousTxOutIndex: binary.LittleEndian.Uint32(bytes[32:36]),
SequenceNumber: binary.LittleEndian.Uint32(bytes[offset+int(l):]),
Expand All @@ -71,12 +73,19 @@ func NewInputFromBytes(bytes []byte) (*Input, int, error) {
// NewInputFromUTXO returns a transaction input from the UTXO fields provided.
func NewInputFromUTXO(prevTxID string, prevTxIndex uint32, prevTxSats uint64,
prevTxScript string, nSeq uint32) (*Input, error) {

pts, err := bscript.NewFromHexString(prevTxScript)
if err != nil {
return nil, err
}

ptxid, err := hex.DecodeString(prevTxID)
if err != nil {
return nil, err
}

return &Input{
PreviousTxIDBytes: ptxid,
PreviousTxID: prevTxID,
PreviousTxOutIndex: prevTxIndex,
PreviousTxSatoshis: prevTxSats,
Expand Down Expand Up @@ -105,10 +114,7 @@ sequence: %x
func (i *Input) ToBytes(clear bool) []byte {
h := make([]byte, 0)

// todo: not checking error
pid, _ := hex.DecodeString(i.PreviousTxID)

h = append(h, ReverseBytes(pid)...)
h = append(h, ReverseBytes(i.PreviousTxIDBytes)...)
h = append(h, GetLittleEndianBytes(i.PreviousTxOutIndex, 4)...)
if clear {
h = append(h, 0x00)
Expand Down
10 changes: 3 additions & 7 deletions signaturehash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bt

import (
"encoding/binary"
"encoding/hex"
"errors"

"github.com/libsv/go-bt/crypto"
Expand Down Expand Up @@ -30,7 +29,7 @@ func (tx *Tx) GetInputPreimage(inputNumber uint32, sigHashFlag sighash.Flag) ([]

in := tx.Inputs[inputNumber]

if in.PreviousTxID == "" {
if len(in.PreviousTxIDBytes) == 0 {
return nil, errors.New("'PreviousTxID' not supplied")
}
if in.PreviousTxScript == nil {
Expand Down Expand Up @@ -73,8 +72,7 @@ func (tx *Tx) GetInputPreimage(inputNumber uint32, sigHashFlag sighash.Flag) ([]
buf = append(buf, hashSequence...)

// outpoint (32-byte hash + 4-byte little endian)
txid, _ := hex.DecodeString(in.PreviousTxID[:])
buf = append(buf, ReverseBytes(txid)...)
buf = append(buf, ReverseBytes(in.PreviousTxIDBytes)...)
oi := make([]byte, 4)
binary.LittleEndian.PutUint32(oi, in.PreviousTxOutIndex)
buf = append(buf, oi...)
Expand Down Expand Up @@ -114,9 +112,7 @@ func (tx *Tx) getPreviousOutHash() []byte {
buf := make([]byte, 0)

for _, in := range tx.Inputs {
// todo: error ignored?
txid, _ := hex.DecodeString(in.PreviousTxID[:])
buf = append(buf, ReverseBytes(txid)...)
buf = append(buf, ReverseBytes(in.PreviousTxIDBytes)...)
oi := make([]byte, 4)
binary.LittleEndian.PutUint32(oi, in.PreviousTxOutIndex)
buf = append(buf, oi...)
Expand Down
7 changes: 7 additions & 0 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func (tx *Tx) AddInputFromTx(pvsTx *Tx, matchPK []byte) error {
continue
}
tx.AddInput(&Input{
PreviousTxIDBytes: pvsTx.GetTxIDAsBytes(),
PreviousTxID: pvsTx.GetTxID(),
PreviousTxOutIndex: uint32(i),
PreviousTxSatoshis: utxo.Satoshis,
Expand All @@ -163,7 +164,13 @@ func (tx *Tx) From(txID string, vout uint32, prevTxLockingScript string, satoshi
return err
}

ptid, err := hex.DecodeString(txID)
if err != nil {
return err
}

tx.AddInput(&Input{
PreviousTxIDBytes: ptid,
PreviousTxID: txID,
PreviousTxOutIndex: vout,
PreviousTxSatoshis: satoshis,
Expand Down
2 changes: 2 additions & 0 deletions tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func TestNewTxFromString(t *testing.T) {
assert.Equal(t, 1, len(tx.Inputs))

// Create a new unlocking script
ptid, _ := hex.DecodeString("9c5b1428aaad5e9b0196c89be8628b366f33c7b22933da0489b921d487a7cb1c")
i := bt.Input{
PreviousTxIDBytes: ptid,
PreviousTxID: "9c5b1428aaad5e9b0196c89be8628b366f33c7b22933da0489b921d487a7cb1c",
PreviousTxOutIndex: 0,
SequenceNumber: bt.DefaultSequenceNumber,
Expand Down

0 comments on commit d907793

Please sign in to comment.