11package transaction_test
22
33import (
4+ "encoding/hex"
45 "testing"
56
7+ "github.com/bitcoin-sv/go-sdk/chainhash"
68 ec "github.com/bitcoin-sv/go-sdk/primitives/ec"
79 "github.com/bitcoin-sv/go-sdk/script"
810 "github.com/bitcoin-sv/go-sdk/transaction"
@@ -57,6 +59,19 @@ func TestNewTransaction(t *testing.T) {
5759 })
5860}
5961
62+ func TestIsCoinbase (t * testing.T ) {
63+ tx , err := transaction .NewTransactionFromHex ("01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff17033f250d2f43555656452f2c903fb60859897700d02700ffffffff01d864a012000000001976a914d648686cf603c11850f39600e37312738accca8f88ac00000000" )
64+ require .NoError (t , err )
65+ require .True (t , tx .IsCoinbase ())
66+ }
67+
68+ func TestIsValidTxID (t * testing.T ) {
69+ valid , _ := hex .DecodeString ("fe77aa03d5563d3ec98455a76655ea3b58e19a4eb102baf7b2a47af37e94b295" )
70+ require .True (t , transaction .IsValidTxID (valid ))
71+ invalid , _ := hex .DecodeString ("fe77aa03d5563d3ec98455a76655ea3b58e19a4eb102baf7b2a47af37e94b2" )
72+ require .False (t , transaction .IsValidTxID (invalid ))
73+ }
74+
6075func TestBEEF (t * testing.T ) {
6176 t .Parallel ()
6277 t .Run ("deserialize and serialize" , func (t * testing.T ) {
@@ -79,13 +94,97 @@ func TestEF(t *testing.T) {
7994 })
8095}
8196
82- func Benchmark_ShallowClone (b * testing.B ) {
97+ func TestClone (t * testing.T ) {
98+ tx , err := transaction .NewTransactionFromBEEFHex (BRC62Hex )
99+ require .NoError (t , err )
100+
101+ clone := tx .Clone ()
102+ require .Equal (t , tx .Bytes (), clone .Bytes ())
103+ }
104+
105+ func BenchmarkClone (b * testing.B ) {
83106 tx , _ := transaction .NewTransactionFromHex ("0200000003a9bc457fdc6a54d99300fb137b23714d860c350a9d19ff0f571e694a419ff3a0010000006b48304502210086c83beb2b2663e4709a583d261d75be538aedcafa7766bd983e5c8db2f8b2fc02201a88b178624ab0ad1748b37c875f885930166237c88f5af78ee4e61d337f935f412103e8be830d98bb3b007a0343ee5c36daa48796ae8bb57946b1e87378ad6e8a090dfeffffff0092bb9a47e27bf64fc98f557c530c04d9ac25e2f2a8b600e92a0b1ae7c89c20010000006b483045022100f06b3db1c0a11af348401f9cebe10ae2659d6e766a9dcd9e3a04690ba10a160f02203f7fbd7dfcfc70863aface1a306fcc91bbadf6bc884c21a55ef0d32bd6b088c8412103e8be830d98bb3b007a0343ee5c36daa48796ae8bb57946b1e87378ad6e8a090dfeffffff9d0d4554fa692420a0830ca614b6c60f1bf8eaaa21afca4aa8c99fb052d9f398000000006b483045022100d920f2290548e92a6235f8b2513b7f693a64a0d3fa699f81a034f4b4608ff82f0220767d7d98025aff3c7bd5f2a66aab6a824f5990392e6489aae1e1ae3472d8dffb412103e8be830d98bb3b007a0343ee5c36daa48796ae8bb57946b1e87378ad6e8a090dfeffffff02807c814a000000001976a9143a6bf34ebfcf30e8541bbb33a7882845e5a29cb488ac76b0e60e000000001976a914bd492b67f90cb85918494767ebb23102c4f06b7088ac67000000" )
84107
85108 b .Run ("clone" , func (b * testing.B ) {
86109 for i := 0 ; i < b .N ; i ++ {
87- clone := tx .ShallowClone ()
110+ clone := tx .Clone ()
88111 _ = clone
89112 }
90113 })
91114}
115+
116+ func TestUncomputedFee (t * testing.T ) {
117+ tx , _ := transaction .NewTransactionFromBEEFHex (BRC62Hex )
118+
119+ tx .AddOutput (& transaction.TransactionOutput {
120+ Change : true ,
121+ LockingScript : tx .Outputs [0 ].LockingScript ,
122+ })
123+
124+ err := tx .Sign ()
125+ require .Error (t , err )
126+
127+ err = tx .SignUnsigned ()
128+ require .Error (t , err )
129+ }
130+
131+ func TestSignUnsigned (t * testing.T ) {
132+ tx , err := transaction .NewTransactionFromBEEFHex (BRC62Hex )
133+ require .NoError (t , err )
134+
135+ cloneTx := tx .Clone ()
136+ pk , _ := ec .NewPrivateKey ()
137+
138+ // Adding a script template with random key so sigs will be different
139+ for i := range tx .Inputs {
140+ cloneTx .Inputs [i ].UnlockingScriptTemplate , err = p2pkh .Unlock (pk , nil )
141+ require .NoError (t , err )
142+ }
143+
144+ // This should do nothing because the inputs from hex are already signed
145+ err = cloneTx .SignUnsigned ()
146+ require .NoError (t , err )
147+ for i := range cloneTx .Inputs {
148+ require .Equal (t , tx .Inputs [i ].UnlockingScript , cloneTx .Inputs [i ].UnlockingScript )
149+ }
150+
151+ // This should sign the inputs with the incorrect key which should change the sigs
152+ cloneTx .Sign ()
153+ for i := range tx .Inputs {
154+ require .NotEqual (t , tx .Inputs [i ].UnlockingScript , cloneTx .Inputs [i ].UnlockingScript )
155+ }
156+ }
157+
158+ func TestSignUnsignedNew (t * testing.T ) {
159+ pk , _ := ec .PrivateKeyFromWif ("L1y6DgX4TuonxXzRPuk9reK2TD2THjwQReNUwVrvWN3aRkjcbauB" )
160+ address , _ := script .NewAddressFromPublicKey (pk .PubKey (), true )
161+ tx := transaction .NewTransaction ()
162+ lockingScript , err := p2pkh .Lock (address )
163+ require .NoError (t , err )
164+ sourceTxID , _ := chainhash .NewHashFromHex ("fe77aa03d5563d3ec98455a76655ea3b58e19a4eb102baf7b2a47af37e94b295" )
165+ unlockingScript , _ := p2pkh .Unlock (pk , nil )
166+ tx .AddInput (& transaction.TransactionInput {
167+ SourceTransaction : & transaction.Transaction {
168+ Outputs : []* transaction.TransactionOutput {
169+ {
170+ Satoshis : 1 ,
171+ LockingScript : lockingScript ,
172+ },
173+ },
174+ },
175+ SourceTXID : sourceTxID ,
176+ UnlockingScriptTemplate : unlockingScript ,
177+ })
178+
179+ tx .AddOutput (& transaction.TransactionOutput {
180+ Satoshis : 1 ,
181+ LockingScript : lockingScript ,
182+ })
183+
184+ err = tx .SignUnsigned ()
185+ require .NoError (t , err )
186+
187+ for _ , input := range tx .Inputs {
188+ require .Positive (t , len (input .UnlockingScript .Bytes ()))
189+ }
190+ }
0 commit comments