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

     1  // +build unittest
     2  
     3  package deeponion
     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: "DYPyxvq57iSRA5xUXzSVfsTENPz4DKFr5S"},
    36  			want:    "76a9142afc25b8b5d4ed490026d38b3b464c140a32dc7588ac",
    37  			wantErr: false,
    38  		},
    39  		{
    40  			name:    "P2PKH2",
    41  			args:    args{address: "DshhBSub7vexDFNm45UtG2wBJFt8cm5Uwr"},
    42  			want:    "76a914fec0038b0db67c1b304f6c25b3e860277a96226188ac",
    43  			wantErr: false,
    44  		},
    45  		{
    46  			name:    "P2SH1",
    47  			args:    args{address: "YYDTMNJmKqajnWjFPjenzs2awwE4cwYHtC"},
    48  			want:    "a91461190c0272b059b2c09b352da81b1712dd83305e87",
    49  			wantErr: false,
    50  		},
    51  		{
    52  			name:    "P2SH2",
    53  			args:    args{address: "Yh1qpMEA4EFMTB4BmhkeyivJ92WiGr3ETX"},
    54  			want:    "a914c19ff0bfc8f4387bee48e2cd3628bf72f7053cd787",
    55  			wantErr: false,
    56  		},
    57  	}
    58  	parser := NewDeepOnionParser(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  var (
    76  	testTx1 bchain.Tx
    77  
    78  	testTxPacked1 = "0a206ba18524d81af732d0226ffdb63d2bcdc0d58a35ac97b5ad731057932d324e1412b401010000001134415d0114caae2bf9a7808aee0798e6245a347405d46c8131dbf55cbbbc689bbee367e902000000484730440220280f3fa80b4e93834fe0a8d9884105310eaa8d36d77b9aff113b6c498138e5bb02204578409f0a14fa1950ea4951314fd495fd503b42a6325efb5c139a6c8253912401ffffffff0200000000000000000005f22f5904000000232102bdb95d89f07e3a29305f3c8de86ec211ed77b7e15cf314c85c532a6b71c2ce07ac000000001891e884ea05200028b88a5432760a001220e967e3be9b68bcbb5cf5db31816cd40574345a24e69807ee8a80a7f92baeca14180222484730440220280f3fa80b4e93834fe0a8d9884105310eaa8d36d77b9aff113b6c498138e5bb02204578409f0a14fa1950ea4951314fd495fd503b42a6325efb5c139a6c825391240128ffffffff0f3a0210003a520a0504583af7fb10011a232102bdb95d89f07e3a29305f3c8de86ec211ed77b7e15cf314c85c532a6b71c2ce07ac2222446d343835624e4a6169474a6d4556746832426e5a345931796763756644736934454001"
    79  )
    80  
    81  func init() {
    82  	testTx1 = bchain.Tx{
    83  		Hex:       "010000001134415d0114caae2bf9a7808aee0798e6245a347405d46c8131dbf55cbbbc689bbee367e902000000484730440220280f3fa80b4e93834fe0a8d9884105310eaa8d36d77b9aff113b6c498138e5bb02204578409f0a14fa1950ea4951314fd495fd503b42a6325efb5c139a6c8253912401ffffffff0200000000000000000005f22f5904000000232102bdb95d89f07e3a29305f3c8de86ec211ed77b7e15cf314c85c532a6b71c2ce07ac00000000",
    84  		Blocktime: 1564554257,
    85  		Txid:      "6ba18524d81af732d0226ffdb63d2bcdc0d58a35ac97b5ad731057932d324e14",
    86  		LockTime:  0,
    87  		Time:      1564554257,
    88  		Version:   1,
    89  		Vin: []bchain.Vin{
    90  			{
    91  				ScriptSig: bchain.ScriptSig{
    92  					Hex: "4730440220280f3fa80b4e93834fe0a8d9884105310eaa8d36d77b9aff113b6c498138e5bb02204578409f0a14fa1950ea4951314fd495fd503b42a6325efb5c139a6c8253912401",
    93  				},
    94  				Txid:     "e967e3be9b68bcbb5cf5db31816cd40574345a24e69807ee8a80a7f92baeca14",
    95  				Vout:     2,
    96  				Sequence: 4294967295,
    97  			},
    98  		},
    99  		Vout: []bchain.Vout{
   100  			{
   101  				ValueSat: *big.NewInt(0),
   102  				N:        0,
   103  				ScriptPubKey: bchain.ScriptPubKey{
   104  					Hex: "",
   105  				},
   106  			},
   107  			{
   108  				ValueSat: *big.NewInt(18660128763),
   109  				N:        1,
   110  				ScriptPubKey: bchain.ScriptPubKey{
   111  					Hex: "2102bdb95d89f07e3a29305f3c8de86ec211ed77b7e15cf314c85c532a6b71c2ce07ac",
   112  					Addresses: []string{
   113  						"Dm485bNJaiGJmEVth2BnZ4Y1ygcufDsi4E",
   114  					},
   115  				},
   116  			},
   117  		},
   118  	}
   119  }
   120  
   121  func Test_PackTx(t *testing.T) {
   122  	type args struct {
   123  		tx        bchain.Tx
   124  		height    uint32
   125  		blockTime int64
   126  		parser    *DeepOnionParser
   127  	}
   128  	tests := []struct {
   129  		name    string
   130  		args    args
   131  		want    string
   132  		wantErr bool
   133  	}{
   134  		{
   135  			name: "deeponion-1",
   136  			args: args{
   137  				tx:        testTx1,
   138  				height:    1377592,
   139  				blockTime: 1564554257,
   140  				parser:    NewDeepOnionParser(GetChainParams("main"), &btc.Configuration{}),
   141  			},
   142  			want:    testTxPacked1,
   143  			wantErr: false,
   144  		},
   145  	}
   146  	for _, tt := range tests {
   147  		t.Run(tt.name, func(t *testing.T) {
   148  			got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime)
   149  			if (err != nil) != tt.wantErr {
   150  				t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr)
   151  				return
   152  			}
   153  			h := hex.EncodeToString(got)
   154  			if !reflect.DeepEqual(h, tt.want) {
   155  				t.Errorf("packTx() = %v, want %v", h, tt.want)
   156  			}
   157  		})
   158  	}
   159  }
   160  
   161  func Test_UnpackTx(t *testing.T) {
   162  	type args struct {
   163  		packedTx string
   164  		parser   *DeepOnionParser
   165  	}
   166  	tests := []struct {
   167  		name    string
   168  		args    args
   169  		want    *bchain.Tx
   170  		want1   uint32
   171  		wantErr bool
   172  	}{
   173  		{
   174  			name: "deeponion-1",
   175  			args: args{
   176  				packedTx: testTxPacked1,
   177  				parser:   NewDeepOnionParser(GetChainParams("main"), &btc.Configuration{}),
   178  			},
   179  			want:    &testTx1,
   180  			want1:   1377592,
   181  			wantErr: false,
   182  		},
   183  	}
   184  	for _, tt := range tests {
   185  		t.Run(tt.name, func(t *testing.T) {
   186  			b, _ := hex.DecodeString(tt.args.packedTx)
   187  			got, got1, err := tt.args.parser.UnpackTx(b)
   188  			if (err != nil) != tt.wantErr {
   189  				t.Errorf("unpackTx(1) error = %v, wantErr %v", err, tt.wantErr)
   190  				return
   191  			}
   192  			if !reflect.DeepEqual(got, tt.want) {
   193  				t.Errorf("unpackTx(2) got = %v, want %v", got, tt.want)
   194  			}
   195  			if got1 != tt.want1 {
   196  				t.Errorf("unpackTx(3) got1 = %v, want %v", got1, tt.want1)
   197  			}
   198  		})
   199  	}
   200  }