github.com/cerberus-wallet/blockbook@v0.3.2/bchain/coins/monetaryunit/monetaryunitparser_test.go (about)

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