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

     1  // +build unittest
     2  
     3  package vertcoin
     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:    "pubkeyhash1",
    35  			args:    args{address: "Ve7SkjuVgVm1z3X8aqqxnzGV6GraUcde6K"},
    36  			want:    "76a9142d20ebccc60a8b7dcba607701c6e5bda05eb6d9a88ac",
    37  			wantErr: false,
    38  		},
    39  		{
    40  			name:    "pubkeyhash2",
    41  			args:    args{address: "Vavx3LENdGctDMsaVm426McFE5iDDrSuk5"},
    42  			want:    "76a9140a3c6a3970a5b6b94ee0c1da82d6a87899d169da88ac",
    43  			wantErr: false,
    44  		},
    45  		{
    46  			name:    "scripthash1",
    47  			args:    args{address: "36c8VAv74dPZZa4cFayb92hzozkPL4fBPe"},
    48  			want:    "a91435ec06fa05f2d3b16e88cd7eda7651a10ca2e01987",
    49  			wantErr: false,
    50  		},
    51  		{
    52  			name:    "scripthash2",
    53  			args:    args{address: "38A1RNvbA5c9wNRfyLVn1FCH5TPKJVG8YR"},
    54  			want:    "a91446eb90e002f137f05385896c882fe000cc2e967f87",
    55  			wantErr: false,
    56  		},
    57  		{
    58  			name:    "witness_v0_keyhash",
    59  			args:    args{address: "vtc1qd80qaputavyhtvszlz9zprueqch0qd003g520j"},
    60  			want:    "001469de0e878beb0975b202f88a208f99062ef035ef",
    61  			wantErr: false,
    62  		},
    63  	}
    64  	parser := NewVertcoinParser(GetChainParams("main"), &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  var (
    82  	testTx1 bchain.Tx
    83  
    84  	testTxPacked1 = "000e87768bb386b878010000000146fd781834a34e0399ccda1edf9ec47d715e17d904ad0958d533a240b3605ad6000000006a473044022026b352a0c35c232342339e2b50ec9f04587b990d5213174e368cc76dc82686f002207d0787461ad846825872a50d3d6fc748d5a836575c1daf6ad0ca602f9c4a8826012103d36b6b829c571ed7caa565eca9bdc2aa36519b7ab8551ace5edb0356d477ad3cfdffffff020882a400000000001976a91499b16da88a7e29b913b6131df2644d6d06cb331b88ac80f0fa020000000017a91446eb90e002f137f05385896c882fe000cc2e967f8774870e00"
    85  )
    86  
    87  func init() {
    88  	testTx1 = bchain.Tx{
    89  		Hex:       "010000000146fd781834a34e0399ccda1edf9ec47d715e17d904ad0958d533a240b3605ad6000000006a473044022026b352a0c35c232342339e2b50ec9f04587b990d5213174e368cc76dc82686f002207d0787461ad846825872a50d3d6fc748d5a836575c1daf6ad0ca602f9c4a8826012103d36b6b829c571ed7caa565eca9bdc2aa36519b7ab8551ace5edb0356d477ad3cfdffffff020882a400000000001976a91499b16da88a7e29b913b6131df2644d6d06cb331b88ac80f0fa020000000017a91446eb90e002f137f05385896c882fe000cc2e967f8774870e00",
    90  		Blocktime: 1529925180,
    91  		Txid:      "d58c11aa970449c3e0ee5e0cdf78532435a9d2b28a2da284a8dd4dd6bdd0331c",
    92  		LockTime:  952180,
    93  		Version:   1,
    94  		Vin: []bchain.Vin{
    95  			{
    96  				ScriptSig: bchain.ScriptSig{
    97  					Hex: "473044022026b352a0c35c232342339e2b50ec9f04587b990d5213174e368cc76dc82686f002207d0787461ad846825872a50d3d6fc748d5a836575c1daf6ad0ca602f9c4a8826012103d36b6b829c571ed7caa565eca9bdc2aa36519b7ab8551ace5edb0356d477ad3c",
    98  				},
    99  				Txid:     "d65a60b340a233d55809ad04d9175e717dc49edf1edacc99034ea3341878fd46",
   100  				Vout:     0,
   101  				Sequence: 4294967293,
   102  			},
   103  		},
   104  		Vout: []bchain.Vout{
   105  			{
   106  				ValueSat: *big.NewInt(10781192),
   107  				N:        0,
   108  				ScriptPubKey: bchain.ScriptPubKey{
   109  					Hex: "76a91499b16da88a7e29b913b6131df2644d6d06cb331b88ac",
   110  					Addresses: []string{
   111  						"Vp1UqzsmVecaexfbWFGSFFL5x1g2XQnrGR",
   112  					},
   113  				},
   114  			},
   115  			{
   116  				ValueSat: *big.NewInt(50000000),
   117  				N:        1,
   118  				ScriptPubKey: bchain.ScriptPubKey{
   119  					Hex: "a91446eb90e002f137f05385896c882fe000cc2e967f87",
   120  					Addresses: []string{
   121  						"38A1RNvbA5c9wNRfyLVn1FCH5TPKJVG8YR",
   122  					},
   123  				},
   124  			},
   125  		},
   126  	}
   127  }
   128  
   129  func Test_PackTx(t *testing.T) {
   130  	type args struct {
   131  		tx        bchain.Tx
   132  		height    uint32
   133  		blockTime int64
   134  		parser    *VertcoinParser
   135  	}
   136  	tests := []struct {
   137  		name    string
   138  		args    args
   139  		want    string
   140  		wantErr bool
   141  	}{
   142  		{
   143  			name: "vertcoin-1",
   144  			args: args{
   145  				tx:        testTx1,
   146  				height:    952182,
   147  				blockTime: 1529925180,
   148  				parser:    NewVertcoinParser(GetChainParams("main"), &btc.Configuration{}),
   149  			},
   150  			want:    testTxPacked1,
   151  			wantErr: false,
   152  		},
   153  	}
   154  	for _, tt := range tests {
   155  		t.Run(tt.name, func(t *testing.T) {
   156  			got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime)
   157  			if (err != nil) != tt.wantErr {
   158  				t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr)
   159  				return
   160  			}
   161  			h := hex.EncodeToString(got)
   162  			if !reflect.DeepEqual(h, tt.want) {
   163  				t.Errorf("packTx() = %v, want %v", h, tt.want)
   164  			}
   165  		})
   166  	}
   167  }
   168  
   169  func Test_UnpackTx(t *testing.T) {
   170  	type args struct {
   171  		packedTx string
   172  		parser   *VertcoinParser
   173  	}
   174  	tests := []struct {
   175  		name    string
   176  		args    args
   177  		want    *bchain.Tx
   178  		want1   uint32
   179  		wantErr bool
   180  	}{
   181  		{
   182  			name: "vertcoin-1",
   183  			args: args{
   184  				packedTx: testTxPacked1,
   185  				parser:   NewVertcoinParser(GetChainParams("main"), &btc.Configuration{}),
   186  			},
   187  			want:    &testTx1,
   188  			want1:   952182,
   189  			wantErr: false,
   190  		},
   191  	}
   192  	for _, tt := range tests {
   193  		t.Run(tt.name, func(t *testing.T) {
   194  			b, _ := hex.DecodeString(tt.args.packedTx)
   195  			got, got1, err := tt.args.parser.UnpackTx(b)
   196  			if (err != nil) != tt.wantErr {
   197  				t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr)
   198  				return
   199  			}
   200  			if !reflect.DeepEqual(got, tt.want) {
   201  				t.Errorf("unpackTx() got = %v, want %v", got, tt.want)
   202  			}
   203  			if got1 != tt.want1 {
   204  				t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1)
   205  			}
   206  		})
   207  	}
   208  }