github.com/bchainhub/blockbook@v0.3.2/bchain/coins/monacoin/monacoinparser_test.go (about)

     1  // +build unittest
     2  
     3  package monacoin
     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: "mptvgSbAs4iwxQ7JQZdEN6Urpt3dtjbawd"},
    36  			want:    "76a91466e0ef980c8ff8129e8d0f716b2ce1df2f97bbbf88ac",
    37  			wantErr: false,
    38  		},
    39  		{
    40  			name:    "P2SH1",
    41  			args:    args{address: "pJwLxfRRUhAaYJsKzKCk9cATAn8Do2SS7L"},
    42  			want:    "a91492e825fa92f4aa873c6caf4b20f6c7e949b456a987",
    43  			wantErr: false,
    44  		},
    45  		{
    46  			name:    "P2SH2",
    47  			args:    args{address: "pHNnBm6ECsh5QsUyXMzdoAXV8qV68wj2M4"},
    48  			want:    "a91481c75a711f23443b44d70b10ddf856e39a6b254d87",
    49  			wantErr: false,
    50  		},
    51  	}
    52  	parser := NewMonacoinParser(GetChainParams("test"), &btc.Configuration{})
    53  
    54  	for _, tt := range tests {
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			got, err := parser.GetAddrDescFromAddress(tt.args.address)
    57  			if (err != nil) != tt.wantErr {
    58  				t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr)
    59  				return
    60  			}
    61  			h := hex.EncodeToString(got)
    62  			if !reflect.DeepEqual(h, tt.want) {
    63  				t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want)
    64  			}
    65  		})
    66  	}
    67  }
    68  
    69  func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) {
    70  	type args struct {
    71  		address string
    72  	}
    73  	tests := []struct {
    74  		name    string
    75  		args    args
    76  		want    string
    77  		wantErr bool
    78  	}{
    79  		{
    80  			name:    "P2PKH1",
    81  			args:    args{address: "MFMy9FwJsV6HiN5eZDqDETw4pw52q3UGrb"},
    82  			want:    "76a91451dadacc7021440cbe4ca148a5db563b329b4c0388ac",
    83  			wantErr: false,
    84  		},
    85  		{
    86  			name:    "P2PKH2",
    87  			args:    args{address: "MVELZC3ks1Xk59kvKWuSN3mpByNwaxeaBJ"},
    88  			want:    "76a914e9fb298e72e29ebc2b89864a5e4ae10e0b84726088ac",
    89  			wantErr: false,
    90  		},
    91  		{
    92  			name:    "P2SH1",
    93  			args:    args{address: "PHjTKtgYLTJ9D2Bzw2f6xBB41KBm2HeGfg"},
    94  			want:    "a9146449f568c9cd2378138f2636e1567112a184a9e887",
    95  			wantErr: false,
    96  		},
    97  		{
    98  			name:    "P2SH2",
    99  			args:    args{address: "PUfP1H9f2RhFf6wbuK16RKHDKBeTmRMMcU"},
   100  			want:    "a914dc2e124d590b4dd34b9c564fe281474531cdc21987",
   101  			wantErr: false,
   102  		},
   103  		{
   104  			name:    "witness_v0_keyhash",
   105  			args:    args{address: "mona1q49knemcefarfkvuqr7rgajdu3x5gx8pzdnurgq"},
   106  			want:    "0014a96d3cef194f469b33801f868ec9bc89a8831c22",
   107  			wantErr: false,
   108  		},
   109  		{
   110  			name:    "witness_v0_scripthashx",
   111  			args:    args{address: "mona1qp8f842ywwr9h5rdxyzggex7q3trvvvaarfssxccju52rj6htfzfsqr79j2"},
   112  			want:    "002009d27aa88e70cb7a0da620908c9bc08ac6c633bd1a61036312e514396aeb4893",
   113  			wantErr: false,
   114  		},
   115  	}
   116  	parser := NewMonacoinParser(GetChainParams("main"), &btc.Configuration{})
   117  
   118  	for _, tt := range tests {
   119  		t.Run(tt.name, func(t *testing.T) {
   120  			got, err := parser.GetAddrDescFromAddress(tt.args.address)
   121  			if (err != nil) != tt.wantErr {
   122  				t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr)
   123  				return
   124  			}
   125  			h := hex.EncodeToString(got)
   126  			if !reflect.DeepEqual(h, tt.want) {
   127  				t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want)
   128  			}
   129  		})
   130  	}
   131  }
   132  
   133  func Test_GetAddressesFromAddrDesc(t *testing.T) {
   134  	type args struct {
   135  		script string
   136  	}
   137  	tests := []struct {
   138  		name    string
   139  		args    args
   140  		want    []string
   141  		want2   bool
   142  		wantErr bool
   143  	}{
   144  		{
   145  			name:    "P2PKH",
   146  			args:    args{script: "76a91451dadacc7021440cbe4ca148a5db563b329b4c0388ac"},
   147  			want:    []string{"MFMy9FwJsV6HiN5eZDqDETw4pw52q3UGrb"},
   148  			want2:   true,
   149  			wantErr: false,
   150  		},
   151  		{
   152  			name:    "P2SH",
   153  			args:    args{script: "a9146449f568c9cd2378138f2636e1567112a184a9e887"},
   154  			want:    []string{"PHjTKtgYLTJ9D2Bzw2f6xBB41KBm2HeGfg"},
   155  			want2:   true,
   156  			wantErr: false,
   157  		},
   158  		{
   159  			name:    "P2WPKH",
   160  			args:    args{script: "0014a96d3cef194f469b33801f868ec9bc89a8831c22"},
   161  			want:    []string{"mona1q49knemcefarfkvuqr7rgajdu3x5gx8pzdnurgq"},
   162  			want2:   true,
   163  			wantErr: false,
   164  		},
   165  		{
   166  			name:    "P2WSH",
   167  			args:    args{script: "002009d27aa88e70cb7a0da620908c9bc08ac6c633bd1a61036312e514396aeb4893"},
   168  			want:    []string{"mona1qp8f842ywwr9h5rdxyzggex7q3trvvvaarfssxccju52rj6htfzfsqr79j2"},
   169  			want2:   true,
   170  			wantErr: false,
   171  		},
   172  		{
   173  			name:    "OP_RETURN ascii",
   174  			args:    args{script: "6a0461686f6a"},
   175  			want:    []string{"OP_RETURN (ahoj)"},
   176  			want2:   false,
   177  			wantErr: false,
   178  		},
   179  		{
   180  			name:    "OP_RETURN hex",
   181  			args:    args{script: "6a072020f1686f6a20"},
   182  			want:    []string{"OP_RETURN 2020f1686f6a20"},
   183  			want2:   false,
   184  			wantErr: false,
   185  		},
   186  	}
   187  
   188  	parser := NewMonacoinParser(GetChainParams("main"), &btc.Configuration{})
   189  
   190  	for _, tt := range tests {
   191  		t.Run(tt.name, func(t *testing.T) {
   192  			b, _ := hex.DecodeString(tt.args.script)
   193  			got, got2, err := parser.GetAddressesFromAddrDesc(b)
   194  			if (err != nil) != tt.wantErr {
   195  				t.Errorf("outputScriptToAddresses() error = %v, wantErr %v", err, tt.wantErr)
   196  				return
   197  			}
   198  			if !reflect.DeepEqual(got, tt.want) {
   199  				t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got, tt.want)
   200  			}
   201  			if !reflect.DeepEqual(got2, tt.want2) {
   202  				t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got2, tt.want2)
   203  			}
   204  		})
   205  	}
   206  }
   207  
   208  var (
   209  	testTx1 bchain.Tx
   210  
   211  	testTxPacked1 = "0014fd278bb3fde2620200000003e44ef4e5fe2e4345f1e1340afe396c780773e3834a5bffb153a2faf510e2845e000000006a47304402205ebd735621eaaf512441998727a37e99be94e5ecded54601ea3eebac9282bc2502207d48da44e1c883579c6cd8c2b8ccfb5380e5ac71affe70b475d2b558e0f7bd4b01210391f72b34c04855ce16b97dd79b0ba78fc4b26f40abce853c33788e348cb79c3bfeffffff0ad690a74c43c0df9527c516d26e31fa47e15471a2ead65757b672522888e920010000006b48304502210091a473124bf506edbb095951aa1a32c76bea7eba4020ae2858314961b1a83de602205c3818e517cf830a95a1208fc84aa343faaeeaaa96eab76238379769598ab2d40121038c217e5de8e375ed6cf648e96ec6bfb9e0fbcf5ae3945a5ea60d16919d9c8b68feffffffb9aa4aed4ad4c4b95419e132a43db34aa03a7ec35ef0beecdd627f9ca07bda03010000006a47304402204906d973ac9b4786403f8f8fc2b2ad2e6745ea01a93336b4b67af1d7d1b625cc022016820be905ffd6e11949da79e7a1c7eb97939421a04e0645c8caef8fc585f7ca012102b5f647c4eb677e952913c0b6934c12b29dc50afba8b558b1677ffd2d78c84a88feffffff02f6da4601000000001976a914fb69fe6dcfe88557dc0ce0ea65bd7cf02f5e4f5b88ac8bfd8c57000000001976a914628d603ac50d656e3311ff0cd5490b4c5cdd92ea88ac25fd1400"
   212  )
   213  
   214  func init() {
   215  	testTx1 = bchain.Tx{
   216  		Hex:       "0200000003e44ef4e5fe2e4345f1e1340afe396c780773e3834a5bffb153a2faf510e2845e000000006a47304402205ebd735621eaaf512441998727a37e99be94e5ecded54601ea3eebac9282bc2502207d48da44e1c883579c6cd8c2b8ccfb5380e5ac71affe70b475d2b558e0f7bd4b01210391f72b34c04855ce16b97dd79b0ba78fc4b26f40abce853c33788e348cb79c3bfeffffff0ad690a74c43c0df9527c516d26e31fa47e15471a2ead65757b672522888e920010000006b48304502210091a473124bf506edbb095951aa1a32c76bea7eba4020ae2858314961b1a83de602205c3818e517cf830a95a1208fc84aa343faaeeaaa96eab76238379769598ab2d40121038c217e5de8e375ed6cf648e96ec6bfb9e0fbcf5ae3945a5ea60d16919d9c8b68feffffffb9aa4aed4ad4c4b95419e132a43db34aa03a7ec35ef0beecdd627f9ca07bda03010000006a47304402204906d973ac9b4786403f8f8fc2b2ad2e6745ea01a93336b4b67af1d7d1b625cc022016820be905ffd6e11949da79e7a1c7eb97939421a04e0645c8caef8fc585f7ca012102b5f647c4eb677e952913c0b6934c12b29dc50afba8b558b1677ffd2d78c84a88feffffff02f6da4601000000001976a914fb69fe6dcfe88557dc0ce0ea65bd7cf02f5e4f5b88ac8bfd8c57000000001976a914628d603ac50d656e3311ff0cd5490b4c5cdd92ea88ac25fd1400",
   217  		Blocktime: 1530902705,
   218  		Txid:      "7533fa6651cc96762e27bf496e00262671312244ff0c8bfe56a3c0ef688a49b5",
   219  		LockTime:  1375525,
   220  		Version:   2,
   221  		Vin: []bchain.Vin{
   222  			{
   223  				ScriptSig: bchain.ScriptSig{
   224  					Hex: "47304402205ebd735621eaaf512441998727a37e99be94e5ecded54601ea3eebac9282bc2502207d48da44e1c883579c6cd8c2b8ccfb5380e5ac71affe70b475d2b558e0f7bd4b01210391f72b34c04855ce16b97dd79b0ba78fc4b26f40abce853c33788e348cb79c3b",
   225  				},
   226  				Txid:     "5e84e210f5faa253b1ff5b4a83e37307786c39fe0a34e1f145432efee5f44ee4",
   227  				Vout:     0,
   228  				Sequence: 4294967294,
   229  			},
   230  			{
   231  				ScriptSig: bchain.ScriptSig{
   232  					Hex: "48304502210091a473124bf506edbb095951aa1a32c76bea7eba4020ae2858314961b1a83de602205c3818e517cf830a95a1208fc84aa343faaeeaaa96eab76238379769598ab2d40121038c217e5de8e375ed6cf648e96ec6bfb9e0fbcf5ae3945a5ea60d16919d9c8b68",
   233  				},
   234  				Txid:     "20e988285272b65757d6eaa27154e147fa316ed216c52795dfc0434ca790d60a",
   235  				Vout:     1,
   236  				Sequence: 4294967294,
   237  			},
   238  			{
   239  				ScriptSig: bchain.ScriptSig{
   240  					Hex: "47304402204906d973ac9b4786403f8f8fc2b2ad2e6745ea01a93336b4b67af1d7d1b625cc022016820be905ffd6e11949da79e7a1c7eb97939421a04e0645c8caef8fc585f7ca012102b5f647c4eb677e952913c0b6934c12b29dc50afba8b558b1677ffd2d78c84a88",
   241  				},
   242  				Txid:     "03da7ba09c7f62ddecbef05ec37e3aa04ab33da432e11954b9c4d44aed4aaab9",
   243  				Vout:     1,
   244  				Sequence: 4294967294,
   245  			},
   246  		},
   247  		Vout: []bchain.Vout{
   248  			{
   249  				ValueSat: *big.NewInt(21420790),
   250  				N:        0,
   251  				ScriptPubKey: bchain.ScriptPubKey{
   252  					Hex: "76a914fb69fe6dcfe88557dc0ce0ea65bd7cf02f5e4f5b88ac",
   253  					Addresses: []string{
   254  						"MWpWpANNQRskQHcuY5ZQpN4BVynQxmSxRb",
   255  					},
   256  				},
   257  			},
   258  			{
   259  				ValueSat: *big.NewInt(1468857739),
   260  				N:        1,
   261  				ScriptPubKey: bchain.ScriptPubKey{
   262  					Hex: "76a914628d603ac50d656e3311ff0cd5490b4c5cdd92ea88ac",
   263  					Addresses: []string{
   264  						"MGtFpCVyKEHNtpVNesxPMxYuQayoEBX5yZ",
   265  					},
   266  				},
   267  			},
   268  		},
   269  	}
   270  }
   271  
   272  func Test_PackTx(t *testing.T) {
   273  	type args struct {
   274  		tx        bchain.Tx
   275  		height    uint32
   276  		blockTime int64
   277  		parser    *MonacoinParser
   278  	}
   279  	tests := []struct {
   280  		name    string
   281  		args    args
   282  		want    string
   283  		wantErr bool
   284  	}{
   285  		{
   286  			name: "Monacoin-1",
   287  			args: args{
   288  				tx:        testTx1,
   289  				height:    1375527,
   290  				blockTime: 1530902705,
   291  				parser:    NewMonacoinParser(GetChainParams("main"), &btc.Configuration{}),
   292  			},
   293  			want:    testTxPacked1,
   294  			wantErr: false,
   295  		},
   296  	}
   297  	for _, tt := range tests {
   298  		t.Run(tt.name, func(t *testing.T) {
   299  			got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime)
   300  			if (err != nil) != tt.wantErr {
   301  				t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr)
   302  				return
   303  			}
   304  			h := hex.EncodeToString(got)
   305  			if !reflect.DeepEqual(h, tt.want) {
   306  				t.Errorf("packTx() = %v, want %v", h, tt.want)
   307  			}
   308  		})
   309  	}
   310  }
   311  
   312  func Test_UnpackTx(t *testing.T) {
   313  	type args struct {
   314  		packedTx string
   315  		parser   *MonacoinParser
   316  	}
   317  	tests := []struct {
   318  		name    string
   319  		args    args
   320  		want    *bchain.Tx
   321  		want1   uint32
   322  		wantErr bool
   323  	}{
   324  		{
   325  			name: "Monacoin-1",
   326  			args: args{
   327  				packedTx: testTxPacked1,
   328  				parser:   NewMonacoinParser(GetChainParams("main"), &btc.Configuration{}),
   329  			},
   330  			want:    &testTx1,
   331  			want1:   1375527,
   332  			wantErr: false,
   333  		},
   334  	}
   335  	for _, tt := range tests {
   336  		t.Run(tt.name, func(t *testing.T) {
   337  			b, _ := hex.DecodeString(tt.args.packedTx)
   338  			got, got1, err := tt.args.parser.UnpackTx(b)
   339  			if (err != nil) != tt.wantErr {
   340  				t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr)
   341  				return
   342  			}
   343  			if !reflect.DeepEqual(got, tt.want) {
   344  				t.Errorf("unpackTx() got = %v, want %v", got, tt.want)
   345  			}
   346  			if got1 != tt.want1 {
   347  				t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1)
   348  			}
   349  		})
   350  	}
   351  }