diff --git a/transaction.go b/transaction.go index d33162d..95ec35b 100644 --- a/transaction.go +++ b/transaction.go @@ -26,6 +26,11 @@ type PayToAddress struct { // OpReturnData is the op return data to include in the tx type OpReturnData [][]byte +// TxFromHex will return a libsv.tx from a raw hex string +func TxFromHex(rawHex string) (*transaction.Transaction, error) { + return transaction.NewFromString(rawHex) +} + // CreateTx will create a basic transaction func CreateTx(utxos []*Utxo, addresses []*PayToAddress, opReturns []OpReturnData, wif string) (string, error) { diff --git a/transaction_test.go b/transaction_test.go index 3c9b6b3..38c44b5 100644 --- a/transaction_test.go +++ b/transaction_test.go @@ -1,6 +1,61 @@ package bitcoin -import "testing" +import ( + "fmt" + "testing" +) + +// TestTxFromHex will test the method TxFromHex() +func TestTxFromHex(t *testing.T) { + t.Parallel() + + // Create the list of tests + var tests = []struct { + inputHex string + expectedTxID string + expectedNil bool + expectedError bool + }{ + {"", "", true, true}, + {"0", "", true, true}, + {"000", "", true, true}, + {"bad-hex", "", true, true}, + {"01000000012adda020db81f2155ebba69e7c841275517ebf91674268c32ff2f5c7e2853b2c010000006b483045022100872051ef0b6c47714130c12a067db4f38b988bfc22fe270731c2146f5229386b02207abf68bbf092ec03e2c616defcc4c868ad1fc3cdbffb34bcedfab391a1274f3e412102affe8c91d0a61235a3d07b1903476a2e2f7a90451b2ed592fea9937696a07077ffffffff02ed1a0000000000001976a91491b3753cf827f139d2dc654ce36f05331138ddb588acc9670300000000001976a914da036233873cc6489ff65a0185e207d243b5154888ac00000000", "64cd12102af20195d54a107e0ee5989ac5db3491893a0b9d42e24354732a22a5", false, false}, + } + + // Run tests + for _, test := range tests { + if rawTx, err := TxFromHex(test.inputHex); err != nil && !test.expectedError { + t.Errorf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.inputHex, err.Error()) + } else if err == nil && test.expectedError { + t.Errorf("%s Failed: [%s] inputted and error was expected", t.Name(), test.inputHex) + } else if rawTx == nil && !test.expectedNil { + t.Errorf("%s Failed: [%s] inputted and was nil but not expected", t.Name(), test.inputHex) + } else if rawTx != nil && test.expectedNil { + t.Errorf("%s Failed: [%s] inputted and was NOT nil but expected to be nil", t.Name(), test.inputHex) + } else if rawTx != nil && rawTx.GetTxID() != test.expectedTxID { + t.Errorf("%s Failed: [%s] inputted [%s] expected but failed comparison of txIDs, got: %s", t.Name(), test.inputHex, test.expectedTxID, rawTx.GetTxID()) + } + } +} + +// ExampleTxFromHex example using TxFromHex() +func ExampleTxFromHex() { + tx, err := TxFromHex("01000000012adda020db81f2155ebba69e7c841275517ebf91674268c32ff2f5c7e2853b2c010000006b483045022100872051ef0b6c47714130c12a067db4f38b988bfc22fe270731c2146f5229386b02207abf68bbf092ec03e2c616defcc4c868ad1fc3cdbffb34bcedfab391a1274f3e412102affe8c91d0a61235a3d07b1903476a2e2f7a90451b2ed592fea9937696a07077ffffffff02ed1a0000000000001976a91491b3753cf827f139d2dc654ce36f05331138ddb588acc9670300000000001976a914da036233873cc6489ff65a0185e207d243b5154888ac00000000") + if err != nil { + fmt.Printf("error occurred: %s", err.Error()) + return + } + fmt.Printf("txID: %s", tx.GetTxID()) + // Output:txID: 64cd12102af20195d54a107e0ee5989ac5db3491893a0b9d42e24354732a22a5 +} + +// BenchmarkTxFromHex benchmarks the method TxFromHex() +func BenchmarkTxFromHex(b *testing.B) { + for i := 0; i < b.N; i++ { + _, _ = TxFromHex("01000000012adda020db81f2155ebba69e7c841275517ebf91674268c32ff2f5c7e2853b2c010000006b483045022100872051ef0b6c47714130c12a067db4f38b988bfc22fe270731c2146f5229386b02207abf68bbf092ec03e2c616defcc4c868ad1fc3cdbffb34bcedfab391a1274f3e412102affe8c91d0a61235a3d07b1903476a2e2f7a90451b2ed592fea9937696a07077ffffffff02ed1a0000000000001976a91491b3753cf827f139d2dc654ce36f05331138ddb588acc9670300000000001976a914da036233873cc6489ff65a0185e207d243b5154888ac00000000") + } +} // TestCreateTx will test the method CreateTx() func TestCreateTx(t *testing.T) {