github.com/bchainhub/blockbook@v0.3.2/bchain/coins/bch/bcashparser_test.go (about)

     1  // +build unittest
     2  
     3  package bch
     4  
     5  import (
     6  	"blockbook/bchain"
     7  	"blockbook/bchain/coins/btc"
     8  	"encoding/hex"
     9  	"math/big"
    10  	"os"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/martinboehm/btcutil/chaincfg"
    15  )
    16  
    17  func TestMain(m *testing.M) {
    18  	c := m.Run()
    19  	chaincfg.ResetParams()
    20  	os.Exit(c)
    21  }
    22  
    23  func Test_GetAddrDescFromAddress(t *testing.T) {
    24  	mainParserCashAddr, mainParserLegacy, testParserCashAddr, _ := setupParsers(t)
    25  	tests := []struct {
    26  		name      string
    27  		parser    *BCashParser
    28  		addresses []string
    29  		hex       string
    30  		wantErr   bool
    31  	}{
    32  		{
    33  			name:      "test-P2PKH-0",
    34  			parser:    testParserCashAddr,
    35  			addresses: []string{"mnnAKPTSrWjgoi3uEYaQkHA1QEC5btFeBr"},
    36  			hex:       "76a9144fa927fd3bcf57d4e3c582c3d2eb2bd3df8df47c88ac",
    37  			wantErr:   false,
    38  		},
    39  		{
    40  			name:      "test-P2PKH-1",
    41  			parser:    testParserCashAddr,
    42  			addresses: []string{"bchtest:qp86jfla8084048rckpv85ht90falr050s03ejaesm"},
    43  			hex:       "76a9144fa927fd3bcf57d4e3c582c3d2eb2bd3df8df47c88ac",
    44  			wantErr:   false,
    45  		},
    46  		{
    47  			name:      "main-P2PKH-0",
    48  			parser:    mainParserLegacy,
    49  			addresses: []string{"129HiRqekqPVucKy2M8zsqvafGgKypciPp"},
    50  			hex:       "76a9140c8967e6382c7a2ca64d8e850bfc99b7736e1a0d88ac",
    51  			wantErr:   false,
    52  		},
    53  		{
    54  			name:      "main-P2PKH-0",
    55  			parser:    mainParserCashAddr,
    56  			addresses: []string{"bitcoincash:qqxgjelx8qk85t9xfk8g2zlunxmhxms6p55xarv2r5"},
    57  			hex:       "76a9140c8967e6382c7a2ca64d8e850bfc99b7736e1a0d88ac",
    58  			wantErr:   false,
    59  		},
    60  		{
    61  			name:      "main-P2SH-0",
    62  			parser:    mainParserCashAddr,
    63  			addresses: []string{"3EBEFWPtDYWCNszQ7etoqtWmmygccayLiH"},
    64  			hex:       "a91488f772450c830a30eddfdc08a93d5f2ae1a30e1787",
    65  			wantErr:   false,
    66  		},
    67  		{
    68  			name:      "main-P2SH-1",
    69  			parser:    mainParserLegacy,
    70  			addresses: []string{"bitcoincash:pzy0wuj9pjps5v8dmlwq32fatu4wrgcwzuayq5nfhh"},
    71  			hex:       "a91488f772450c830a30eddfdc08a93d5f2ae1a30e1787",
    72  			wantErr:   false,
    73  		},
    74  	}
    75  
    76  	for _, tt := range tests {
    77  		t.Run(tt.name, func(t *testing.T) {
    78  			got, err := tt.parser.GetAddrDescFromAddress(tt.addresses[0])
    79  			if (err != nil) != tt.wantErr {
    80  				t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr)
    81  				return
    82  			}
    83  			h := hex.EncodeToString(got)
    84  			if !reflect.DeepEqual(h, tt.hex) {
    85  				t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.hex)
    86  			}
    87  		})
    88  	}
    89  }
    90  
    91  func Test_GetAddressesFromAddrDesc(t *testing.T) {
    92  	mainParserCashAddr, mainParserLegacy, testParserCashAddr, testParserLegacy := setupParsers(t)
    93  	tests := []struct {
    94  		name       string
    95  		parser     *BCashParser
    96  		addresses  []string
    97  		searchable bool
    98  		hex        string
    99  		wantErr    bool
   100  	}{
   101  		{
   102  			name:       "test-P2PKH-0",
   103  			parser:     testParserLegacy,
   104  			addresses:  []string{"mnnAKPTSrWjgoi3uEYaQkHA1QEC5btFeBr"},
   105  			searchable: true,
   106  			hex:        "76a9144fa927fd3bcf57d4e3c582c3d2eb2bd3df8df47c88ac",
   107  
   108  			wantErr: false,
   109  		},
   110  		{
   111  			name:       "test-P2PKH-1",
   112  			parser:     testParserCashAddr,
   113  			addresses:  []string{"bchtest:qp86jfla8084048rckpv85ht90falr050s03ejaesm"},
   114  			searchable: true,
   115  			hex:        "76a9144fa927fd3bcf57d4e3c582c3d2eb2bd3df8df47c88ac",
   116  			wantErr:    false,
   117  		},
   118  		{
   119  			name:       "main-P2PKH-0",
   120  			parser:     mainParserLegacy,
   121  			addresses:  []string{"129HiRqekqPVucKy2M8zsqvafGgKypciPp"},
   122  			searchable: true,
   123  			hex:        "76a9140c8967e6382c7a2ca64d8e850bfc99b7736e1a0d88ac",
   124  			wantErr:    false,
   125  		},
   126  		{
   127  			name:       "main-P2PKH-0",
   128  			parser:     mainParserCashAddr,
   129  			addresses:  []string{"bitcoincash:qqxgjelx8qk85t9xfk8g2zlunxmhxms6p55xarv2r5"},
   130  			searchable: true,
   131  			hex:        "76a9140c8967e6382c7a2ca64d8e850bfc99b7736e1a0d88ac",
   132  			wantErr:    false,
   133  		},
   134  		{
   135  			name:       "main-P2SH-0",
   136  			parser:     mainParserLegacy,
   137  			addresses:  []string{"3EBEFWPtDYWCNszQ7etoqtWmmygccayLiH"},
   138  			searchable: true,
   139  			hex:        "a91488f772450c830a30eddfdc08a93d5f2ae1a30e1787",
   140  			wantErr:    false,
   141  		},
   142  		{
   143  			name:       "main-P2SH-1",
   144  			parser:     mainParserCashAddr,
   145  			addresses:  []string{"bitcoincash:pzy0wuj9pjps5v8dmlwq32fatu4wrgcwzuayq5nfhh"},
   146  			searchable: true,
   147  			hex:        "a91488f772450c830a30eddfdc08a93d5f2ae1a30e1787",
   148  			wantErr:    false,
   149  		},
   150  		{
   151  			name:       "main-P2PK",
   152  			parser:     mainParserCashAddr,
   153  			addresses:  []string{"bitcoincash:qqr95pwp0w5jqnh9vcjl4qm4x45atr0er57n49pq75"},
   154  			searchable: true,
   155  			hex:        "2103db3c3977c5165058bf38c46f72d32f4e872112dbafc13083a948676165cd1603ac",
   156  			wantErr:    false,
   157  		},
   158  		{
   159  			name:       "OP_RETURN ascii",
   160  			parser:     mainParserCashAddr,
   161  			addresses:  []string{"OP_RETURN (ahoj)"},
   162  			searchable: false,
   163  			hex:        "6a0461686f6a",
   164  			wantErr:    false,
   165  		},
   166  		{
   167  			name:       "OP_RETURN hex",
   168  			parser:     mainParserCashAddr,
   169  			addresses:  []string{"OP_RETURN 2020f1686f6a20"},
   170  			searchable: false,
   171  			hex:        "6a072020f1686f6a20",
   172  			wantErr:    false,
   173  		},
   174  		{
   175  			name:       "empty",
   176  			parser:     mainParserCashAddr,
   177  			addresses:  []string{},
   178  			searchable: false,
   179  			hex:        "",
   180  			wantErr:    false,
   181  		},
   182  	}
   183  
   184  	for _, tt := range tests {
   185  		t.Run(tt.name, func(t *testing.T) {
   186  			b, _ := hex.DecodeString(tt.hex)
   187  			got, got2, err := tt.parser.GetAddressesFromAddrDesc(b)
   188  			if (err != nil) != tt.wantErr {
   189  				t.Errorf("GetAddressesFromAddrDesc() error = %v, wantErr %v", err, tt.wantErr)
   190  				return
   191  			}
   192  			if !reflect.DeepEqual(got, tt.addresses) {
   193  				t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got, tt.addresses)
   194  			}
   195  			if !reflect.DeepEqual(got2, tt.searchable) {
   196  				t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got2, tt.searchable)
   197  			}
   198  		})
   199  	}
   200  }
   201  
   202  var (
   203  	testTx1, testTx2 bchain.Tx
   204  	testTxPacked1    = "0001e2408ba8d7af5401000000017f9a22c9cbf54bd902400df746f138f37bcf5b4d93eb755820e974ba43ed5f42040000006a4730440220037f4ed5427cde81d55b9b6a2fd08c8a25090c2c2fff3a75c1a57625ca8a7118022076c702fe55969fa08137f71afd4851c48e31082dd3c40c919c92cdbc826758d30121029f6da5623c9f9b68a9baf9c1bc7511df88fa34c6c2f71f7c62f2f03ff48dca80feffffff019c9700000000000017a9146144d57c8aff48492c9dfb914e120b20bad72d6f8773d00700"
   205  	testTxPacked2    = "0007c91a899ab7da6a010000000001019d64f0c72a0d206001decbffaa722eb1044534c74eee7a5df8318e42a4323ec10000000017160014550da1f5d25a9dae2eafd6902b4194c4c6500af6ffffffff02809698000000000017a914cd668d781ece600efa4b2404dc91fd26b8b8aed8870553d7360000000017a914246655bdbd54c7e477d0ea2375e86e0db2b8f80a8702473044022076aba4ad559616905fa51d4ddd357fc1fdb428d40cb388e042cdd1da4a1b7357022011916f90c712ead9a66d5f058252efd280439ad8956a967e95d437d246710bc9012102a80a5964c5612bb769ef73147b2cf3c149bc0fd4ecb02f8097629c94ab013ffd00000000"
   206  )
   207  
   208  func setupParsers(t *testing.T) (mainParserCashAddr, mainParserLegacy, testParserCashAddr, testParserLegacy *BCashParser) {
   209  	parser1, err := NewBCashParser(GetChainParams("main"), &btc.Configuration{AddressFormat: "cashaddr"})
   210  	if err != nil {
   211  		t.Fatalf("NewBCashParser() error = %v", err)
   212  	}
   213  	parser2, err := NewBCashParser(GetChainParams("main"), &btc.Configuration{AddressFormat: "legacy"})
   214  	if err != nil {
   215  		t.Fatalf("NewBCashParser() error = %v", err)
   216  	}
   217  	parser3, err := NewBCashParser(GetChainParams("test"), &btc.Configuration{AddressFormat: "cashaddr"})
   218  	if err != nil {
   219  		t.Fatalf("NewBCashParser() error = %v", err)
   220  	}
   221  	parser4, err := NewBCashParser(GetChainParams("test"), &btc.Configuration{AddressFormat: "legacy"})
   222  	if err != nil {
   223  		t.Fatalf("NewBCashParser() error = %v", err)
   224  	}
   225  	return parser1, parser2, parser3, parser4
   226  }
   227  
   228  func init() {
   229  
   230  	testTx1 = bchain.Tx{
   231  		Hex:       "01000000017f9a22c9cbf54bd902400df746f138f37bcf5b4d93eb755820e974ba43ed5f42040000006a4730440220037f4ed5427cde81d55b9b6a2fd08c8a25090c2c2fff3a75c1a57625ca8a7118022076c702fe55969fa08137f71afd4851c48e31082dd3c40c919c92cdbc826758d30121029f6da5623c9f9b68a9baf9c1bc7511df88fa34c6c2f71f7c62f2f03ff48dca80feffffff019c9700000000000017a9146144d57c8aff48492c9dfb914e120b20bad72d6f8773d00700",
   232  		Blocktime: 1519053802,
   233  		Txid:      "056e3d82e5ffd0e915fb9b62797d76263508c34fe3e5dbed30dd3e943930f204",
   234  		LockTime:  512115,
   235  		Version:   1,
   236  		Vin: []bchain.Vin{
   237  			{
   238  				ScriptSig: bchain.ScriptSig{
   239  					Hex: "4730440220037f4ed5427cde81d55b9b6a2fd08c8a25090c2c2fff3a75c1a57625ca8a7118022076c702fe55969fa08137f71afd4851c48e31082dd3c40c919c92cdbc826758d30121029f6da5623c9f9b68a9baf9c1bc7511df88fa34c6c2f71f7c62f2f03ff48dca80",
   240  				},
   241  				Txid:     "425fed43ba74e9205875eb934d5bcf7bf338f146f70d4002d94bf5cbc9229a7f",
   242  				Vout:     4,
   243  				Sequence: 4294967294,
   244  			},
   245  		},
   246  		Vout: []bchain.Vout{
   247  			{
   248  				ValueSat: *big.NewInt(38812),
   249  				N:        0,
   250  				ScriptPubKey: bchain.ScriptPubKey{
   251  					Hex: "a9146144d57c8aff48492c9dfb914e120b20bad72d6f87",
   252  					Addresses: []string{
   253  						"bitcoincash:pps5f4tu3tl5sjfvnhaeznsjpvst44eddugfcnqpy9",
   254  					},
   255  				},
   256  			},
   257  		},
   258  	}
   259  
   260  	testTx2 = bchain.Tx{
   261  		Hex:       "010000000001019d64f0c72a0d206001decbffaa722eb1044534c74eee7a5df8318e42a4323ec10000000017160014550da1f5d25a9dae2eafd6902b4194c4c6500af6ffffffff02809698000000000017a914cd668d781ece600efa4b2404dc91fd26b8b8aed8870553d7360000000017a914246655bdbd54c7e477d0ea2375e86e0db2b8f80a8702473044022076aba4ad559616905fa51d4ddd357fc1fdb428d40cb388e042cdd1da4a1b7357022011916f90c712ead9a66d5f058252efd280439ad8956a967e95d437d246710bc9012102a80a5964c5612bb769ef73147b2cf3c149bc0fd4ecb02f8097629c94ab013ffd00000000",
   262  		Blocktime: 1235678901,
   263  		Txid:      "474e6795760ebe81cb4023dc227e5a0efe340e1771c89a0035276361ed733de7",
   264  		LockTime:  0,
   265  		Version:   1,
   266  		Vin: []bchain.Vin{
   267  			{
   268  				ScriptSig: bchain.ScriptSig{
   269  					Hex: "160014550da1f5d25a9dae2eafd6902b4194c4c6500af6",
   270  				},
   271  				Txid:     "c13e32a4428e31f85d7aee4ec7344504b12e72aaffcbde0160200d2ac7f0649d",
   272  				Vout:     0,
   273  				Sequence: 4294967295,
   274  			},
   275  		},
   276  		Vout: []bchain.Vout{
   277  			{
   278  				ValueSat: *big.NewInt(10000000),
   279  				N:        0,
   280  				ScriptPubKey: bchain.ScriptPubKey{
   281  					Hex: "a914cd668d781ece600efa4b2404dc91fd26b8b8aed887",
   282  					Addresses: []string{
   283  						"bchtest:prxkdrtcrm8xqrh6fvjqfhy3l5nt3w9wmq9fmsvkmz",
   284  					},
   285  				},
   286  			},
   287  			{
   288  				ValueSat: *big.NewInt(920081157),
   289  				N:        1,
   290  				ScriptPubKey: bchain.ScriptPubKey{
   291  					Hex: "a914246655bdbd54c7e477d0ea2375e86e0db2b8f80a87",
   292  					Addresses: []string{
   293  						"bchtest:pqjxv4dah42v0erh6r4zxa0gdcxm9w8cpg0qw8tqf6",
   294  					},
   295  				},
   296  			},
   297  		},
   298  	}
   299  }
   300  
   301  func Test_UnpackTx(t *testing.T) {
   302  	mainParser, _, testParser, _ := setupParsers(t)
   303  
   304  	type args struct {
   305  		packedTx string
   306  		parser   *BCashParser
   307  	}
   308  	tests := []struct {
   309  		name    string
   310  		args    args
   311  		want    *bchain.Tx
   312  		want1   uint32
   313  		wantErr bool
   314  	}{
   315  		{
   316  			name: "bcash-1",
   317  			args: args{
   318  				packedTx: testTxPacked1,
   319  				parser:   mainParser,
   320  			},
   321  			want:    &testTx1,
   322  			want1:   123456,
   323  			wantErr: false,
   324  		},
   325  		{
   326  			name: "testnet-1",
   327  			args: args{
   328  				packedTx: testTxPacked2,
   329  				parser:   testParser,
   330  			},
   331  			want:    &testTx2,
   332  			want1:   510234,
   333  			wantErr: false,
   334  		},
   335  	}
   336  	for _, tt := range tests {
   337  		t.Run(tt.name, func(t *testing.T) {
   338  			b, _ := hex.DecodeString(tt.args.packedTx)
   339  			got, got1, err := tt.args.parser.UnpackTx(b)
   340  			if (err != nil) != tt.wantErr {
   341  				t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr)
   342  				return
   343  			}
   344  			if !reflect.DeepEqual(got, tt.want) {
   345  				t.Errorf("unpackTx() got = %v, want %v", got, tt.want)
   346  			}
   347  			if got1 != tt.want1 {
   348  				t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1)
   349  			}
   350  		})
   351  	}
   352  }