github.com/bchainhub/blockbook@v0.3.2/bchain/coins/qtum/qtumparser_test.go (about)

     1  // +build unittest
     2  
     3  package qtum
     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_Mainnet(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: "QiZfqrMLAtfzLCjXTHyLSiNDV6xydoocme"},
    36  			want:    "76a914f0e2aff6730b53b9986a5db8ca17c59426134a0988ac",
    37  			wantErr: false,
    38  		},
    39  		{
    40  			name:    "P2PKH2",
    41  			args:    args{address: "Qcy8wvP1StA3cB9JcPdivXvqUqwai6t1tC"},
    42  			want:    "76a914b3822026c7f758b43a0882d7f2cbfa954702e45688ac",
    43  			wantErr: false,
    44  		},
    45  		{
    46  			name:    "P2SH1",
    47  			args:    args{address: "MHx35M7aq4FcudwVSYziTkpbm9Hz8wJL7y"},
    48  			want:    "a9146e3c881d51d62a668362a5bba28be438f9c548e287",
    49  			wantErr: false,
    50  		},
    51  		{
    52  			name:    "P2SH2",
    53  			args:    args{address: "MSNLeFGHY8iY2Kio57PnKuwWdCQinjuPDC"},
    54  			want:    "a914ca962f788569443b33ec673208ccdcfaac6873b487",
    55  			wantErr: false,
    56  		},
    57  		{
    58  			name:    "witness_v0_keyhash",
    59  			args:    args{address: "qc1q3msdh3npg5ufvwm2sxltxcet6hll9tjzu8ym0d"},
    60  			want:    "00148ee0dbc6614538963b6a81beb3632bd5fff2ae42",
    61  			wantErr: false,
    62  		},
    63  		{
    64  			name:    "witness_v0_scripthashx",
    65  			args:    args{address: "qc1q0uvgqxwqt0u4esawwcff7652jreqye30kmp4sj5dtydee50pze0s0ljwxp"},
    66  			want:    "00207f188019c05bf95cc3ae76129f6a8a90f202662fb6c3584a8d591b9cd1e1165f",
    67  			wantErr: false,
    68  		},
    69  	}
    70  	parser := NewQtumParser(GetChainParams("main"), &btc.Configuration{})
    71  
    72  	for _, tt := range tests {
    73  		t.Run(tt.name, func(t *testing.T) {
    74  			got, err := parser.GetAddrDescFromAddress(tt.args.address)
    75  			if (err != nil) != tt.wantErr {
    76  				t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr)
    77  				return
    78  			}
    79  			h := hex.EncodeToString(got)
    80  			if !reflect.DeepEqual(h, tt.want) {
    81  				t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want)
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  func Test_GetAddressesFromAddrDesc(t *testing.T) {
    88  	type args struct {
    89  		script string
    90  	}
    91  	tests := []struct {
    92  		name    string
    93  		args    args
    94  		want    []string
    95  		want2   bool
    96  		wantErr bool
    97  	}{
    98  		{
    99  			name:    "P2PKH",
   100  			args:    args{script: "76a914f0e2aff6730b53b9986a5db8ca17c59426134a0988ac"},
   101  			want:    []string{"QiZfqrMLAtfzLCjXTHyLSiNDV6xydoocme"},
   102  			want2:   true,
   103  			wantErr: false,
   104  		},
   105  		{
   106  			name:    "P2SH",
   107  			args:    args{script: "a9146e3c881d51d62a668362a5bba28be438f9c548e287"},
   108  			want:    []string{"MHx35M7aq4FcudwVSYziTkpbm9Hz8wJL7y"},
   109  			want2:   true,
   110  			wantErr: false,
   111  		},
   112  		{
   113  			name:    "P2WPKH",
   114  			args:    args{script: "00148ee0dbc6614538963b6a81beb3632bd5fff2ae42"},
   115  			want:    []string{"qc1q3msdh3npg5ufvwm2sxltxcet6hll9tjzu8ym0d"},
   116  			want2:   true,
   117  			wantErr: false,
   118  		},
   119  		{
   120  			name:    "P2WSH",
   121  			args:    args{script: "00207f188019c05bf95cc3ae76129f6a8a90f202662fb6c3584a8d591b9cd1e1165f"},
   122  			want:    []string{"qc1q0uvgqxwqt0u4esawwcff7652jreqye30kmp4sj5dtydee50pze0s0ljwxp"},
   123  			want2:   true,
   124  			wantErr: false,
   125  		},
   126  		{
   127  			name:    "OP_RETURN ascii",
   128  			args:    args{script: "6a0461686f6a"},
   129  			want:    []string{"OP_RETURN (ahoj)"},
   130  			want2:   false,
   131  			wantErr: false,
   132  		},
   133  		{
   134  			name:    "OP_RETURN hex",
   135  			args:    args{script: "6a072020f1686f6a20"},
   136  			want:    []string{"OP_RETURN 2020f1686f6a20"},
   137  			want2:   false,
   138  			wantErr: false,
   139  		},
   140  	}
   141  
   142  	parser := NewQtumParser(GetChainParams("main"), &btc.Configuration{})
   143  
   144  	for _, tt := range tests {
   145  		t.Run(tt.name, func(t *testing.T) {
   146  			b, _ := hex.DecodeString(tt.args.script)
   147  			got, got2, err := parser.GetAddressesFromAddrDesc(b)
   148  			if (err != nil) != tt.wantErr {
   149  				t.Errorf("outputScriptToAddresses() error = %v, wantErr %v", err, tt.wantErr)
   150  				return
   151  			}
   152  			if !reflect.DeepEqual(got, tt.want) {
   153  				t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got, tt.want)
   154  			}
   155  			if !reflect.DeepEqual(got2, tt.want2) {
   156  				t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got2, tt.want2)
   157  			}
   158  		})
   159  	}
   160  }
   161  
   162  var (
   163  	testTx1       bchain.Tx
   164  	testTxPacked1 = "00050fc08bc88ede00010000000336e691ab7f236d7c772b18e967c324b92ad1ba79e4641fd868f737d08f11857a000000006b483045022100bdef630a30ea681be3d2a66bbbc994100509effe9b85b384a0f8d75685eca97802206b7a5e58115deffe3f8f35d4c22dc52eb2cc1632ef18a48498731c09255c2fa9812102cc0f9a0906c0e8dffb3262778be1b3bc75e2d636fa01a7c2c129b1f3f30f21d3fffffffffa3ba1bd605b4db3594a8f5fd3cdec34d3044e3e26dee66908235e8643e9f50f010000006b483045022100f1889232cae3860876025317002bbc9a7e68b172c0595df5db8a1e59a12254150220557bebe548bae1b8fe3474375caca12cab1789e8d6e2c9bd6ab1b4a2c3e3691a812102cc0f9a0906c0e8dffb3262778be1b3bc75e2d636fa01a7c2c129b1f3f30f21d3ffffffff06fb670847bea092c352198f327e39fac3f568d57a100cb4a7db991485dda546170000006b483045022100a2fba32aebca4eaa261f9ebd2b956ac22d9c29e7f65868acd60165077dcfbc85022011864df322178f515260c9f4c098112bfbc23bf257f38005b80a34271d08149b812102cc0f9a0906c0e8dffb3262778be1b3bc75e2d636fa01a7c2c129b1f3f30f21d3ffffffff020088526a740000001976a914b61ba6aa3cc8be40e7553c8728ab3a303cbd4f2188acec1e0923000000001976a9148e896f90d402cdb5517f7d1f32a3d9d400e4bbcb88ac00000000"
   165  )
   166  
   167  func init() {
   168  	testTx1 = bchain.Tx{
   169  		Hex:       "010000000336e691ab7f236d7c772b18e967c324b92ad1ba79e4641fd868f737d08f11857a000000006b483045022100bdef630a30ea681be3d2a66bbbc994100509effe9b85b384a0f8d75685eca97802206b7a5e58115deffe3f8f35d4c22dc52eb2cc1632ef18a48498731c09255c2fa9812102cc0f9a0906c0e8dffb3262778be1b3bc75e2d636fa01a7c2c129b1f3f30f21d3fffffffffa3ba1bd605b4db3594a8f5fd3cdec34d3044e3e26dee66908235e8643e9f50f010000006b483045022100f1889232cae3860876025317002bbc9a7e68b172c0595df5db8a1e59a12254150220557bebe548bae1b8fe3474375caca12cab1789e8d6e2c9bd6ab1b4a2c3e3691a812102cc0f9a0906c0e8dffb3262778be1b3bc75e2d636fa01a7c2c129b1f3f30f21d3ffffffff06fb670847bea092c352198f327e39fac3f568d57a100cb4a7db991485dda546170000006b483045022100a2fba32aebca4eaa261f9ebd2b956ac22d9c29e7f65868acd60165077dcfbc85022011864df322178f515260c9f4c098112bfbc23bf257f38005b80a34271d08149b812102cc0f9a0906c0e8dffb3262778be1b3bc75e2d636fa01a7c2c129b1f3f30f21d3ffffffff020088526a740000001976a914b61ba6aa3cc8be40e7553c8728ab3a303cbd4f2188acec1e0923000000001976a9148e896f90d402cdb5517f7d1f32a3d9d400e4bbcb88ac00000000",
   170  		Blocktime: 1552013184,
   171  		Txid:      "40cc76f3d9747472c49a7c162628d5794e1fb3e5c28e5787b3c6c1178c794e8c",
   172  		LockTime:  0,
   173  		Version:   1,
   174  		Vin: []bchain.Vin{
   175  			{
   176  				ScriptSig: bchain.ScriptSig{
   177  					Hex: "483045022100bdef630a30ea681be3d2a66bbbc994100509effe9b85b384a0f8d75685eca97802206b7a5e58115deffe3f8f35d4c22dc52eb2cc1632ef18a48498731c09255c2fa9812102cc0f9a0906c0e8dffb3262778be1b3bc75e2d636fa01a7c2c129b1f3f30f21d3",
   178  				},
   179  				Txid:     "7a85118fd037f768d81f64e479bad12ab924c367e9182b777c6d237fab91e636",
   180  				Vout:     0,
   181  				Sequence: 4294967295,
   182  			},
   183  			{
   184  				ScriptSig: bchain.ScriptSig{
   185  					Hex: "483045022100f1889232cae3860876025317002bbc9a7e68b172c0595df5db8a1e59a12254150220557bebe548bae1b8fe3474375caca12cab1789e8d6e2c9bd6ab1b4a2c3e3691a812102cc0f9a0906c0e8dffb3262778be1b3bc75e2d636fa01a7c2c129b1f3f30f21d3",
   186  				},
   187  				Txid:     "0ff5e943865e230869e6de263e4e04d334eccdd35f8f4a59b34d5b60bda13bfa",
   188  				Vout:     1,
   189  				Sequence: 4294967295,
   190  			},
   191  			{
   192  				ScriptSig: bchain.ScriptSig{
   193  					Hex: "483045022100a2fba32aebca4eaa261f9ebd2b956ac22d9c29e7f65868acd60165077dcfbc85022011864df322178f515260c9f4c098112bfbc23bf257f38005b80a34271d08149b812102cc0f9a0906c0e8dffb3262778be1b3bc75e2d636fa01a7c2c129b1f3f30f21d3",
   194  				},
   195  				Txid:     "46a5dd851499dba7b40c107ad568f5c3fa397e328f1952c392a0be470867fb06",
   196  				Vout:     23,
   197  				Sequence: 4294967295,
   198  			},
   199  		},
   200  		Vout: []bchain.Vout{
   201  			{
   202  				ValueSat: *big.NewInt(500000000000),
   203  				N:        0,
   204  				ScriptPubKey: bchain.ScriptPubKey{
   205  					Hex: "76a914b61ba6aa3cc8be40e7553c8728ab3a303cbd4f2188ac",
   206  					Addresses: []string{
   207  						"QdCtDST9o3JzQN1tchkpakAgGT4oSRhJec",
   208  					},
   209  				},
   210  			},
   211  			{
   212  				ValueSat: *big.NewInt(587800300),
   213  				N:        1,
   214  				ScriptPubKey: bchain.ScriptPubKey{
   215  					Hex: "76a9148e896f90d402cdb5517f7d1f32a3d9d400e4bbcb88ac",
   216  					Addresses: []string{
   217  						"QZbehkDJekWHZeyKGwXDWqxy8m7RGWPeGn",
   218  					},
   219  				},
   220  			},
   221  		},
   222  	}
   223  }
   224  
   225  func Test_PackTx(t *testing.T) {
   226  	type args struct {
   227  		tx        bchain.Tx
   228  		height    uint32
   229  		blockTime int64
   230  		parser    *QtumParser
   231  	}
   232  	tests := []struct {
   233  		name    string
   234  		args    args
   235  		want    string
   236  		wantErr bool
   237  	}{
   238  		{
   239  			name: "Qtum-1",
   240  			args: args{
   241  				tx:        testTx1,
   242  				height:    331712,
   243  				blockTime: 1552013184,
   244  				parser:    NewQtumParser(GetChainParams("main"), &btc.Configuration{}),
   245  			},
   246  			want:    testTxPacked1,
   247  			wantErr: false,
   248  		},
   249  	}
   250  	for _, tt := range tests {
   251  		t.Run(tt.name, func(t *testing.T) {
   252  			got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime)
   253  			if (err != nil) != tt.wantErr {
   254  				t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr)
   255  				return
   256  			}
   257  			h := hex.EncodeToString(got)
   258  			if !reflect.DeepEqual(h, tt.want) {
   259  				t.Errorf("packTx() = %v, want %v", h, tt.want)
   260  			}
   261  		})
   262  	}
   263  }
   264  
   265  func Test_UnpackTx(t *testing.T) {
   266  	type args struct {
   267  		packedTx string
   268  		parser   *QtumParser
   269  	}
   270  	tests := []struct {
   271  		name    string
   272  		args    args
   273  		want    *bchain.Tx
   274  		want1   uint32
   275  		wantErr bool
   276  	}{
   277  		{
   278  			name: "Qtum-1",
   279  			args: args{
   280  				packedTx: testTxPacked1,
   281  				parser:   NewQtumParser(GetChainParams("main"), &btc.Configuration{}),
   282  			},
   283  			want:    &testTx1,
   284  			want1:   331712,
   285  			wantErr: false,
   286  		},
   287  	}
   288  	for _, tt := range tests {
   289  		t.Run(tt.name, func(t *testing.T) {
   290  			b, _ := hex.DecodeString(tt.args.packedTx)
   291  			got, got1, err := tt.args.parser.UnpackTx(b)
   292  			if (err != nil) != tt.wantErr {
   293  				t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr)
   294  				return
   295  			}
   296  			if !reflect.DeepEqual(got, tt.want) {
   297  				t.Errorf("unpackTx() got = %v, want %v", got, tt.want)
   298  			}
   299  			if got1 != tt.want1 {
   300  				t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1)
   301  			}
   302  		})
   303  	}
   304  }