github.com/bchainhub/blockbook@v0.3.2/bchain/coins/litecoin/litecoinparser_test.go (about)

     1  // +build unittest
     2  
     3  package litecoin
     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_Testnet(t *testing.T) {
    24  	type args struct {
    25  		address string
    26  	}
    27  	tests := []struct {
    28  		name    string
    29  		args    args
    30  		want    string
    31  		wantErr bool
    32  	}{
    33  		{
    34  			name:    "P2PKH1",
    35  			args:    args{address: "mgPdTgEq6YqUJ4yzQgR8jH5TCX5c5yRwCP"},
    36  			want:    "76a91409957dfdb3eb620a94b99857e13949551584c33688ac",
    37  			wantErr: false,
    38  		},
    39  		{
    40  			name:    "P2SH1-legacy",
    41  			args:    args{address: "2MvGVySztevmycxrSmMRjJaVj2iJin7qpap"},
    42  			want:    "a9142126232e3f47ae0f1246ec5f05fc400d83c86a0d87",
    43  			wantErr: false,
    44  		},
    45  		{
    46  			name:    "P2SH2-legacy",
    47  			args:    args{address: "2N9a2TNzWz1FEKGFxUdMEh62V83URdZ5QAZ"},
    48  			want:    "a914b31049e7ee51501fe19e3e0cdb803dc84cf99f9e87",
    49  			wantErr: false,
    50  		},
    51  		{
    52  			name:    "P2SH1",
    53  			args:    args{address: "QPdG6Ts8g2q4m9cVPTTkPGwAB6kYgXB7Hc"},
    54  			want:    "a9142126232e3f47ae0f1246ec5f05fc400d83c86a0d87",
    55  			wantErr: false,
    56  		},
    57  		{
    58  			name:    "P2SH2",
    59  			args:    args{address: "QcvnaPrm17JKTT216jPFmnTvGRvFX2fWzN"},
    60  			want:    "a914b31049e7ee51501fe19e3e0cdb803dc84cf99f9e87",
    61  			wantErr: false,
    62  		},
    63  	}
    64  	parser := NewLitecoinParser(GetChainParams("test"), &btc.Configuration{})
    65  
    66  	for _, tt := range tests {
    67  		t.Run(tt.name, func(t *testing.T) {
    68  			got, err := parser.GetAddrDescFromAddress(tt.args.address)
    69  			if (err != nil) != tt.wantErr {
    70  				t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr)
    71  				return
    72  			}
    73  			h := hex.EncodeToString(got)
    74  			if !reflect.DeepEqual(h, tt.want) {
    75  				t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want)
    76  			}
    77  		})
    78  	}
    79  }
    80  
    81  func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) {
    82  	type args struct {
    83  		address string
    84  	}
    85  	tests := []struct {
    86  		name    string
    87  		args    args
    88  		want    string
    89  		wantErr bool
    90  	}{
    91  		{
    92  			name:    "P2PKH1",
    93  			args:    args{address: "LgJGe7aKy1wfXESKhiKeRWj6z4KjzCfXNW"},
    94  			want:    "76a914e72ba56ab6afccac045d696b979e3b5077e88d1988ac",
    95  			wantErr: false,
    96  		},
    97  		{
    98  			name:    "P2PKH2",
    99  			args:    args{address: "LiTVReQ6N8rWc2pNg2XMwCWq7A9P15teWg"},
   100  			want:    "76a914feda50542e61108cf53b93dbffa0959f91ccb32588ac",
   101  			wantErr: false,
   102  		},
   103  		{
   104  			name:    "P2PKH3 - bech32 prefix",
   105  			args:    args{address: "LTC1eqUzePT9uvpvb413Ejd6P8Cx1Ei8Di"},
   106  			want:    "76a91457630115300a625f5deaab64100faa5506c1422f88ac",
   107  			wantErr: false,
   108  		},
   109  		{
   110  			name:    "P2PKH4 - bech32 prefix",
   111  			args:    args{address: "LTC1f9gtb7bU6B4VjHXvPGDi8ACNZhkKPo"},
   112  			want:    "76a9145763023d3f02509644dacbfc45f2c9102129749788ac",
   113  			wantErr: false,
   114  		},
   115  		{
   116  			name:    "P2SH1",
   117  			args:    args{address: "MLTQ8niHMnpJLNvK72zBeY91hQmUtoo8nX"},
   118  			want:    "a91489ba6cf45546f91f1bdf553e695d63fc6b8795bd87",
   119  			wantErr: false,
   120  		},
   121  		{
   122  			name:    "P2SH2",
   123  			args:    args{address: "MAVWzxXm8KGkZTesqLtqywzrvbs96FEoKy"},
   124  			want:    "a9141c6fbaf46d64221e80cbae182c33ddf81b9294ac87",
   125  			wantErr: false,
   126  		},
   127  		{
   128  			name:    "witness_v0_keyhash",
   129  			args:    args{address: "ltc1q5fgkuac9s2ry56jka5s6zqsyfcugcchrqgz2yl"},
   130  			want:    "0014a2516e770582864a6a56ed21a102044e388c62e3",
   131  			wantErr: false,
   132  		},
   133  		{
   134  			name:    "witness_v0_scripthashx",
   135  			args:    args{address: "ltc1qu9dgdg330r6r84g5mw7wqshg04exv2uttmw2elfwx74h5tgntuzsk3x5nd"},
   136  			want:    "0020e15a86a23178f433d514dbbce042e87d72662b8b5edcacfd2e37ab7a2d135f05",
   137  			wantErr: false,
   138  		},
   139  	}
   140  	parser := NewLitecoinParser(GetChainParams("main"), &btc.Configuration{})
   141  
   142  	for _, tt := range tests {
   143  		t.Run(tt.name, func(t *testing.T) {
   144  			got, err := parser.GetAddrDescFromAddress(tt.args.address)
   145  			if (err != nil) != tt.wantErr {
   146  				t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr)
   147  				return
   148  			}
   149  			h := hex.EncodeToString(got)
   150  			if !reflect.DeepEqual(h, tt.want) {
   151  				t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want)
   152  			}
   153  		})
   154  	}
   155  }
   156  
   157  func TestGetAddressesFromAddrDesc_Mainnet(t *testing.T) {
   158  	type args struct {
   159  		script string
   160  	}
   161  	tests := []struct {
   162  		name    string
   163  		args    args
   164  		want    []string
   165  		want2   bool
   166  		wantErr bool
   167  	}{
   168  		{
   169  			name:    "P2PKH1",
   170  			args:    args{script: "76a914feda50542e61108cf53b93dbffa0959f91ccb32588ac"},
   171  			want:    []string{"LiTVReQ6N8rWc2pNg2XMwCWq7A9P15teWg"},
   172  			want2:   true,
   173  			wantErr: false,
   174  		},
   175  		{
   176  			name:    "P2PKH2",
   177  			args:    args{script: "76A9145763023D3F02509644DACBFC45F2C9102129749788AC"},
   178  			want:    []string{"LTC1f9gtb7bU6B4VjHXvPGDi8ACNZhkKPo"},
   179  			want2:   true,
   180  			wantErr: false,
   181  		},
   182  		{
   183  			name:    "P2SH1",
   184  			args:    args{script: "a9141c6fbaf46d64221e80cbae182c33ddf81b9294ac87"},
   185  			want:    []string{"MAVWzxXm8KGkZTesqLtqywzrvbs96FEoKy"},
   186  			want2:   true,
   187  			wantErr: false,
   188  		},
   189  		{
   190  			name:    "witness_v0_keyhash",
   191  			args:    args{script: "0014a2516e770582864a6a56ed21a102044e388c62e3"},
   192  			want:    []string{"ltc1q5fgkuac9s2ry56jka5s6zqsyfcugcchrqgz2yl"},
   193  			want2:   true,
   194  			wantErr: false,
   195  		},
   196  		{
   197  			name:    "witness_v0_scripthashx",
   198  			args:    args{script: "0020e15a86a23178f433d514dbbce042e87d72662b8b5edcacfd2e37ab7a2d135f05"},
   199  			want:    []string{"ltc1qu9dgdg330r6r84g5mw7wqshg04exv2uttmw2elfwx74h5tgntuzsk3x5nd"},
   200  			want2:   true,
   201  			wantErr: false,
   202  		},
   203  	}
   204  	parser := NewLitecoinParser(GetChainParams("main"), &btc.Configuration{})
   205  
   206  	for _, tt := range tests {
   207  		t.Run(tt.name, func(t *testing.T) {
   208  			b, _ := hex.DecodeString(tt.args.script)
   209  			got, got2, err := parser.GetAddressesFromAddrDesc(b)
   210  			if (err != nil) != tt.wantErr {
   211  				t.Errorf("GetAddressesFromAddrDesc() error = %v, wantErr %v", err, tt.wantErr)
   212  				return
   213  			}
   214  			if !reflect.DeepEqual(got, tt.want) {
   215  				t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got, tt.want)
   216  			}
   217  			if !reflect.DeepEqual(got2, tt.want2) {
   218  				t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got2, tt.want2)
   219  			}
   220  		})
   221  	}
   222  }
   223  
   224  var (
   225  	testTx1 bchain.Tx
   226  
   227  	testTxPacked1 = "0001e4538ba8d7aa2002000000031e1977dc524bec5929e95d8d0946812944b7b5bda12f5b99fdf557773f2ee65e0100000000ffffffff8a398e44546dce0245452b90130e86832b21fd68f26662bc33aeb7c6c115d23c1900000000ffffffffb807ab93a7fcdff7af6d24581a4a18aa7c1db1ebecba2617a6805b009513940f0c00000000ffffffff020001a04a000000001976a9141ae882e788091732da6910595314447c9e38bd8d88ac27440f00000000001976a9146b474cbf0f6004329b630bdd4798f2c23d1751b688ac00000000"
   228  )
   229  
   230  func init() {
   231  	testTx1 = bchain.Tx{
   232  		Hex:       "02000000031e1977dc524bec5929e95d8d0946812944b7b5bda12f5b99fdf557773f2ee65e0100000000ffffffff8a398e44546dce0245452b90130e86832b21fd68f26662bc33aeb7c6c115d23c1900000000ffffffffb807ab93a7fcdff7af6d24581a4a18aa7c1db1ebecba2617a6805b009513940f0c00000000ffffffff020001a04a000000001976a9141ae882e788091732da6910595314447c9e38bd8d88ac27440f00000000001976a9146b474cbf0f6004329b630bdd4798f2c23d1751b688ac00000000",
   233  		Blocktime: 1519053456,
   234  		Txid:      "1c50c1770374d7de2f81a87463a5225bb620d25fd467536223a5b715a47c9e32",
   235  		LockTime:  0,
   236  		Version:   2,
   237  		Vin: []bchain.Vin{
   238  			{
   239  				ScriptSig: bchain.ScriptSig{
   240  					Hex: "",
   241  				},
   242  				Txid:     "5ee62e3f7757f5fd995b2fa1bdb5b744298146098d5de92959ec4b52dc77191e",
   243  				Vout:     1,
   244  				Sequence: 4294967295,
   245  			},
   246  			{
   247  				ScriptSig: bchain.ScriptSig{
   248  					Hex: "",
   249  				},
   250  				Txid:     "3cd215c1c6b7ae33bc6266f268fd212b83860e13902b454502ce6d54448e398a",
   251  				Vout:     25,
   252  				Sequence: 4294967295,
   253  			},
   254  			{
   255  				ScriptSig: bchain.ScriptSig{
   256  					Hex: "",
   257  				},
   258  				Txid:     "0f941395005b80a61726baecebb11d7caa184a1a58246daff7dffca793ab07b8",
   259  				Vout:     12,
   260  				Sequence: 4294967295,
   261  			},
   262  		},
   263  		Vout: []bchain.Vout{
   264  			{
   265  				ValueSat: *big.NewInt(1252000000),
   266  				N:        0,
   267  				ScriptPubKey: bchain.ScriptPubKey{
   268  					Hex: "76a9141ae882e788091732da6910595314447c9e38bd8d88ac",
   269  					Addresses: []string{
   270  						"LMgENNXzzuPxp7vfMjDrCU44bsmrEMgqvc",
   271  					},
   272  				},
   273  			},
   274  			{
   275  				ValueSat: *big.NewInt(1000487),
   276  				N:        1,
   277  				ScriptPubKey: bchain.ScriptPubKey{
   278  					Hex: "76a9146b474cbf0f6004329b630bdd4798f2c23d1751b688ac",
   279  					Addresses: []string{
   280  						"LV1ByjbJNFTHyFQqwqwdJXKJznYDzXzg4B",
   281  					},
   282  				},
   283  			},
   284  		},
   285  	}
   286  }
   287  
   288  func Test_PackTx(t *testing.T) {
   289  	type args struct {
   290  		tx        bchain.Tx
   291  		height    uint32
   292  		blockTime int64
   293  		parser    *LitecoinParser
   294  	}
   295  	tests := []struct {
   296  		name    string
   297  		args    args
   298  		want    string
   299  		wantErr bool
   300  	}{
   301  		{
   302  			name: "litecoin-1",
   303  			args: args{
   304  				tx:        testTx1,
   305  				height:    123987,
   306  				blockTime: 1519053456,
   307  				parser:    NewLitecoinParser(GetChainParams("main"), &btc.Configuration{}),
   308  			},
   309  			want:    testTxPacked1,
   310  			wantErr: false,
   311  		},
   312  	}
   313  	for _, tt := range tests {
   314  		t.Run(tt.name, func(t *testing.T) {
   315  			got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime)
   316  			if (err != nil) != tt.wantErr {
   317  				t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr)
   318  				return
   319  			}
   320  			h := hex.EncodeToString(got)
   321  			if !reflect.DeepEqual(h, tt.want) {
   322  				t.Errorf("packTx() = %v, want %v", h, tt.want)
   323  			}
   324  		})
   325  	}
   326  }
   327  
   328  func Test_UnpackTx(t *testing.T) {
   329  	type args struct {
   330  		packedTx string
   331  		parser   *LitecoinParser
   332  	}
   333  	tests := []struct {
   334  		name    string
   335  		args    args
   336  		want    *bchain.Tx
   337  		want1   uint32
   338  		wantErr bool
   339  	}{
   340  		{
   341  			name: "litecoin-1",
   342  			args: args{
   343  				packedTx: testTxPacked1,
   344  				parser:   NewLitecoinParser(GetChainParams("main"), &btc.Configuration{}),
   345  			},
   346  			want:    &testTx1,
   347  			want1:   123987,
   348  			wantErr: false,
   349  		},
   350  	}
   351  	for _, tt := range tests {
   352  		t.Run(tt.name, func(t *testing.T) {
   353  			b, _ := hex.DecodeString(tt.args.packedTx)
   354  			got, got1, err := tt.args.parser.UnpackTx(b)
   355  			if (err != nil) != tt.wantErr {
   356  				t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr)
   357  				return
   358  			}
   359  			if !reflect.DeepEqual(got, tt.want) {
   360  				t.Errorf("unpackTx() got = %v, want %v", got, tt.want)
   361  			}
   362  			if got1 != tt.want1 {
   363  				t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1)
   364  			}
   365  		})
   366  	}
   367  }