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

     1  // +build unittest
     2  
     3  package ritocoin
     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: "BPXJUs9jo15TTJA6EhS6WAJkEMWtXtWqGf"},
    36  			want:    "76a914d136cae89e81911acd8c7b47b4cfc7ea7e56537188ac",
    37  			wantErr: false,
    38  		},
    39  		{
    40  			name:    "P2PKH2",
    41  			args:    args{address: "BGmHxkXaaRMNdNin7a13MipcbZNHZzeEsi"},
    42  			want:    "76a91487134d5b626c428db9518f7e83f839852b0947c588ac",
    43  			wantErr: false,
    44  		},
    45  		{
    46  			name:    "P2SH1",
    47  			args:    args{address: "jdRbWCgeJ6hJXbPLLvABkydaBWkS73rX8J"},
    48  			want:    "a914f09db8fe493b1f36806e90de7bde9a4e6c1f57d787",
    49  			wantErr: false,
    50  		},
    51  	}
    52  	parser := NewRitocoinParser(GetChainParams("main"), &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  var (
    70  	testTx1       bchain.Tx
    71  	testTxPacked1 = "0003d1478bceb6c13802000000019d5b660bcf1e33116fc5b7991da0cbdd848d3bb8fe2d98fd8efb1c2002f5acdc010000006b483045022100e97ac763e605e9605159ef7a12279e275cc455dbb7db54511e41384f49918b1c0220470f11c765a8262d4b941a87cbc3407f175ba0f3465f44dfdcf29977e2aea259012103eabae25fd82dfa35f66490a8ad5619ccd2492092c8653a79a3fb7a97f2153f43feffffff0240ac8b460e0000001976a9148f87576aad8ba0b42387b6c46f4ec3701eae1b6288ac82c89c40010000001976a914adeec4782f2d95082ceb04d34be90063bc2f221088ac46d10300"
    72  
    73  	testTx2       bchain.Tx
    74  	testTxPacked2 = "0003d10d8bceb69108020000000213aa0749de3e9139cd5352d7037b2cc0ee1eedc89bd74cd698d87f2a62b41127010000006b483045022100a64c3a3df8d5e74e28a19401e0eab65c713ddf04ba9666584e9157ed4648bf4a022009d857df4b8a36d371316daf72db4bcaa002bcf7318f898baa71039b52330095012102355d477bfe43e3a884f3fc81e4c81bd7a79371457974479f901222392c883cadfeffffffac4ebcdaa62454a2bf77e73db68e81452dc2e63f01bb787c63f62a9abd4a7406010000006b483045022100f0fa2cf9134acfdd0124543fc5bc1db7bc7f60023dffdca0f6f87bdd8849cb6e0220319840af61ad2b37294e3532ffd191c57954982a2f1d4738cc639999e9752622012103a7e716d712aa4d6075507daf9b62717d89c2e624236a4bdee7a97c4cf34b6b69feffffff0260b9b543ba0000001976a91456b3aa9d5d2bdc1658406d3e8272d92f1cb05a2088ac06502200000000001976a914b328e94bb96666f774d387afe329588b5e2fc35d88ac0cd10300"
    75  )
    76  
    77  func init() {
    78  	testTx1 = bchain.Tx{
    79  		Hex:       "02000000019d5b660bcf1e33116fc5b7991da0cbdd848d3bb8fe2d98fd8efb1c2002f5acdc010000006b483045022100e97ac763e605e9605159ef7a12279e275cc455dbb7db54511e41384f49918b1c0220470f11c765a8262d4b941a87cbc3407f175ba0f3465f44dfdcf29977e2aea259012103eabae25fd82dfa35f66490a8ad5619ccd2492092c8653a79a3fb7a97f2153f43feffffff0240ac8b460e0000001976a9148f87576aad8ba0b42387b6c46f4ec3701eae1b6288ac82c89c40010000001976a914adeec4782f2d95082ceb04d34be90063bc2f221088ac46d10300",
    80  		Blocktime: 1558630492,
    81  		Txid:      "535e470daf1a4eb2097e6adaddd81972b010e33417747536f19ed29371f9713f",
    82  		LockTime:  250182,
    83  		Version:   2,
    84  		Vin: []bchain.Vin{
    85  			{
    86  				ScriptSig: bchain.ScriptSig{
    87  					Hex: "483045022100e97ac763e605e9605159ef7a12279e275cc455dbb7db54511e41384f49918b1c0220470f11c765a8262d4b941a87cbc3407f175ba0f3465f44dfdcf29977e2aea259012103eabae25fd82dfa35f66490a8ad5619ccd2492092c8653a79a3fb7a97f2153f43",
    88  				},
    89  				Txid:     "dcacf502201cfb8efd982dfeb83b8d84ddcba01d99b7c56f11331ecf0b665b9d",
    90  				Vout:     1,
    91  				Sequence: 4294967294,
    92  			},
    93  		},
    94  		Vout: []bchain.Vout{
    95  			{
    96  				ValueSat: *big.NewInt(61313100864),
    97  				N:        0,
    98  				ScriptPubKey: bchain.ScriptPubKey{
    99  					Hex: "76a9148f87576aad8ba0b42387b6c46f4ec3701eae1b6288ac",
   100  					Addresses: []string{
   101  						"BHXzNqXn3rj8WCKg3mzdZCha7YPyXJnqta",
   102  					},
   103  				},
   104  			},
   105  			{
   106  				ValueSat: *big.NewInt(5378984066),
   107  				N:        1,
   108  				ScriptPubKey: bchain.ScriptPubKey{
   109  					Hex: "76a914adeec4782f2d95082ceb04d34be90063bc2f221088ac",
   110  					Addresses: []string{
   111  						"BLJkYkobSHGonwfRpo3Tu9bAFHoZpTSbfH",
   112  					},
   113  				},
   114  			},
   115  		},
   116  	}
   117  
   118  	testTx2 = bchain.Tx{
   119  		Hex:       "020000000213aa0749de3e9139cd5352d7037b2cc0ee1eedc89bd74cd698d87f2a62b41127010000006b483045022100a64c3a3df8d5e74e28a19401e0eab65c713ddf04ba9666584e9157ed4648bf4a022009d857df4b8a36d371316daf72db4bcaa002bcf7318f898baa71039b52330095012102355d477bfe43e3a884f3fc81e4c81bd7a79371457974479f901222392c883cadfeffffffac4ebcdaa62454a2bf77e73db68e81452dc2e63f01bb787c63f62a9abd4a7406010000006b483045022100f0fa2cf9134acfdd0124543fc5bc1db7bc7f60023dffdca0f6f87bdd8849cb6e0220319840af61ad2b37294e3532ffd191c57954982a2f1d4738cc639999e9752622012103a7e716d712aa4d6075507daf9b62717d89c2e624236a4bdee7a97c4cf34b6b69feffffff0260b9b543ba0000001976a91456b3aa9d5d2bdc1658406d3e8272d92f1cb05a2088ac06502200000000001976a914b328e94bb96666f774d387afe329588b5e2fc35d88ac0cd10300",
   120  		Blocktime: 1558627396,
   121  		Txid:      "7f0745611b4cf48f611a26873cc3c5c01eff7bdf8df7427f379bc7963792f966",
   122  		LockTime:  250124,
   123  		Version:   2,
   124  		Vin: []bchain.Vin{
   125  			{
   126  				ScriptSig: bchain.ScriptSig{
   127  					Hex: "483045022100a64c3a3df8d5e74e28a19401e0eab65c713ddf04ba9666584e9157ed4648bf4a022009d857df4b8a36d371316daf72db4bcaa002bcf7318f898baa71039b52330095012102355d477bfe43e3a884f3fc81e4c81bd7a79371457974479f901222392c883cad",
   128  				},
   129  				Txid:     "2711b4622a7fd898d64cd79bc8ed1eeec02c7b03d75253cd39913ede4907aa13",
   130  				Vout:     1,
   131  				Sequence: 4294967294,
   132  			},
   133  			{
   134  				ScriptSig: bchain.ScriptSig{
   135  					Hex: "483045022100f0fa2cf9134acfdd0124543fc5bc1db7bc7f60023dffdca0f6f87bdd8849cb6e0220319840af61ad2b37294e3532ffd191c57954982a2f1d4738cc639999e9752622012103a7e716d712aa4d6075507daf9b62717d89c2e624236a4bdee7a97c4cf34b6b69",
   136  				},
   137  				Txid:     "06744abd9a2af6637c78bb013fe6c22d45818eb63de777bfa25424a6dabc4eac",
   138  				Vout:     1,
   139  				Sequence: 4294967294,
   140  			},
   141  		},
   142  		Vout: []bchain.Vout{
   143  			{
   144  				ValueSat: *big.NewInt(799999900000),
   145  				N:        0,
   146  				ScriptPubKey: bchain.ScriptPubKey{
   147  					Hex: "76a91456b3aa9d5d2bdc1658406d3e8272d92f1cb05a2088ac",
   148  					Addresses: []string{
   149  						"BCMWxf6dvLXQv3ZNXAxeQLMvrXvSVYfubt",
   150  					},
   151  				},
   152  			},
   153  			{
   154  				ValueSat: *big.NewInt(2248710),
   155  				N:        1,
   156  				ScriptPubKey: bchain.ScriptPubKey{
   157  					Hex: "76a914b328e94bb96666f774d387afe329588b5e2fc35d88ac",
   158  					Addresses: []string{
   159  						"BLnPacy2LCSEgBT2Wi2WgjGhaMfpV1ykf3",
   160  					},
   161  				},
   162  			},
   163  		},
   164  	}
   165  }
   166  
   167  func Test_PackTx(t *testing.T) {
   168  	type args struct {
   169  		tx        bchain.Tx
   170  		height    uint32
   171  		blockTime int64
   172  		parser    *RitocoinParser
   173  	}
   174  	tests := []struct {
   175  		name    string
   176  		args    args
   177  		want    string
   178  		wantErr bool
   179  	}{
   180  		{
   181  			name: "ritocoin-1",
   182  			args: args{
   183  				tx:        testTx1,
   184  				height:    250183,
   185  				blockTime: 1558630492,
   186  				parser:    NewRitocoinParser(GetChainParams("main"), &btc.Configuration{}),
   187  			},
   188  			want:    testTxPacked1,
   189  			wantErr: false,
   190  		},
   191  		{
   192  			name: "ritocoin-2",
   193  			args: args{
   194  				tx:        testTx2,
   195  				height:    250125,
   196  				blockTime: 1558627396,
   197  				parser:    NewRitocoinParser(GetChainParams("main"), &btc.Configuration{}),
   198  			},
   199  			want:    testTxPacked2,
   200  			wantErr: false,
   201  		},
   202  	}
   203  	for _, tt := range tests {
   204  		t.Run(tt.name, func(t *testing.T) {
   205  			got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime)
   206  			if (err != nil) != tt.wantErr {
   207  				t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr)
   208  				return
   209  			}
   210  			h := hex.EncodeToString(got)
   211  			if !reflect.DeepEqual(h, tt.want) {
   212  				t.Errorf("packTx() = %v, want %v", h, tt.want)
   213  			}
   214  		})
   215  	}
   216  }
   217  
   218  func Test_UnpackTx(t *testing.T) {
   219  	type args struct {
   220  		packedTx string
   221  		parser   *RitocoinParser
   222  	}
   223  	tests := []struct {
   224  		name    string
   225  		args    args
   226  		want    *bchain.Tx
   227  		want1   uint32
   228  		wantErr bool
   229  	}{
   230  		{
   231  			name: "ritocoin-1",
   232  			args: args{
   233  				packedTx: testTxPacked1,
   234  				parser:   NewRitocoinParser(GetChainParams("main"), &btc.Configuration{}),
   235  			},
   236  			want:    &testTx1,
   237  			want1:   250183,
   238  			wantErr: false,
   239  		},
   240  		{
   241  			name: "ritocoin-2",
   242  			args: args{
   243  				packedTx: testTxPacked2,
   244  				parser:   NewRitocoinParser(GetChainParams("main"), &btc.Configuration{}),
   245  			},
   246  			want:    &testTx2,
   247  			want1:   250125,
   248  			wantErr: false,
   249  		},
   250  	}
   251  	for _, tt := range tests {
   252  		t.Run(tt.name, func(t *testing.T) {
   253  			b, _ := hex.DecodeString(tt.args.packedTx)
   254  			got, got1, err := tt.args.parser.UnpackTx(b)
   255  			if (err != nil) != tt.wantErr {
   256  				t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr)
   257  				return
   258  			}
   259  			if !reflect.DeepEqual(got, tt.want) {
   260  				t.Errorf("unpackTx() got = %v, want %v", got, tt.want)
   261  			}
   262  			if got1 != tt.want1 {
   263  				t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1)
   264  			}
   265  		})
   266  	}
   267  }