github.com/bchainhub/blockbook@v0.3.2/bchain/coins/digibyte/digibyteparser_test.go (about)

     1  // +build unittest
     2  
     3  package digibyte
     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 TestAddressToOutputScript_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: "DFDe9ne77eEUKUijjG4EpDwW9vDxckGgHN"},
    36  			want:    "76a9146e8d4f7f0dfeb5d69b9a2cf914a1a2e276312b2188ac",
    37  			wantErr: false,
    38  		},
    39  		{
    40  			name:    "pubkeyhash2",
    41  			args:    args{address: "DPUnoXeaSDnNtQTa7U3nEMTYBVgJ6wVgCh"},
    42  			want:    "76a914c92bc70927a752deb91cf0361dcdb60bdac6a1d588ac",
    43  			wantErr: false,
    44  		},
    45  		{
    46  			name:    "scripthash1",
    47  			args:    args{address: "SgbK2hJXBUccpQgj41fR4VMZqVPesPZgzC"},
    48  			want:    "a914d3b07c1aaea886f8ceddedec440623f812e49ddc87",
    49  			wantErr: false,
    50  		},
    51  		{
    52  			name:    "scripthash2",
    53  			args:    args{address: "SRrevBM5bfZNpFJ4MhzaNfkTghYKoTB6LV"},
    54  			want:    "a914320d7056c33fd8d0f5bb9cf42d74133dc28d89bb87",
    55  			wantErr: false,
    56  		},
    57  		{
    58  			name:    "witness_v0_keyhash",
    59  			args:    args{address: "dgb1qwdzu3cxvcte7g5f8ze4dsn83wf6x8zdvw927dx"},
    60  			want:    "00147345c8e0ccc2f3e45127166ad84cf172746389ac",
    61  			wantErr: false,
    62  		},
    63  	}
    64  	parser := NewDigiByteParser(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 = "006acfc28bb5a0fe3c01000000015952b77fedb7233936a48df90e5423d62539efcd3f61a2466501d6ca26554d5e000000006b483045022100fad176fc354e976e0250ca880085f98c7072d5ccd2978d462f981c7c14495a9102207e60e2fa7b58991909f7528aaf3efb4acc5000da299b84b5d7b065b2ef1a2ac601210322e516129a8e55c043a7ac1d01f3c8fc192b88c32f03746f558432fe6a601e05ffffffff0217930054020000001976a914d9eff329b03348487fb9688a77c6b8081794fd2288ac29a9750f0a0000001976a914ad554bde320ae4b3d755ec32e706d8b327ecdd5688acc0cf6a00"
    85  )
    86  
    87  func init() {
    88  	testTx1 = bchain.Tx{
    89  		Hex:       "01000000015952b77fedb7233936a48df90e5423d62539efcd3f61a2466501d6ca26554d5e000000006b483045022100fad176fc354e976e0250ca880085f98c7072d5ccd2978d462f981c7c14495a9102207e60e2fa7b58991909f7528aaf3efb4acc5000da299b84b5d7b065b2ef1a2ac601210322e516129a8e55c043a7ac1d01f3c8fc192b88c32f03746f558432fe6a601e05ffffffff0217930054020000001976a914d9eff329b03348487fb9688a77c6b8081794fd2288ac29a9750f0a0000001976a914ad554bde320ae4b3d755ec32e706d8b327ecdd5688acc0cf6a00",
    90  		Blocktime: 1532239774,
    91  		Txid:      "0dcf2530419b9ef525a69f6a15e4d699be1dc9a4ac643c9581b6c57acf25eabf",
    92  		LockTime:  7000000,
    93  		Version:   1,
    94  		Vin: []bchain.Vin{
    95  			{
    96  				ScriptSig: bchain.ScriptSig{
    97  					Hex: "483045022100fad176fc354e976e0250ca880085f98c7072d5ccd2978d462f981c7c14495a9102207e60e2fa7b58991909f7528aaf3efb4acc5000da299b84b5d7b065b2ef1a2ac601210322e516129a8e55c043a7ac1d01f3c8fc192b88c32f03746f558432fe6a601e05",
    98  				},
    99  				Txid:     "5e4d5526cad6016546a2613fcdef3925d623540ef98da4363923b7ed7fb75259",
   100  				Vout:     0,
   101  				Sequence: 4294967295,
   102  			},
   103  		},
   104  		Vout: []bchain.Vout{
   105  			{
   106  				ValueSat: *big.NewInt(9999258391),
   107  				N:        0,
   108  				ScriptPubKey: bchain.ScriptPubKey{
   109  					Hex: "76a914d9eff329b03348487fb9688a77c6b8081794fd2288ac",
   110  					Addresses: []string{
   111  						"DR1Scto7rRpTCAyNY6DFhRsZgG2vZ6rxLx",
   112  					},
   113  				},
   114  			},
   115  			{
   116  				ValueSat: *big.NewInt(43209042217),
   117  				N:        1,
   118  				ScriptPubKey: bchain.ScriptPubKey{
   119  					Hex: "76a914ad554bde320ae4b3d755ec32e706d8b327ecdd5688ac",
   120  					Addresses: []string{
   121  						"DLwbcwUY5dwsdF9ezk8Uoga9V8eMB1Kb6B",
   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    *DigiByteParser
   135  	}
   136  	tests := []struct {
   137  		name    string
   138  		args    args
   139  		want    string
   140  		wantErr bool
   141  	}{
   142  		{
   143  			name: "digibyte-1",
   144  			args: args{
   145  				tx:        testTx1,
   146  				height:    7000002,
   147  				blockTime: 1532239774,
   148  				parser:    NewDigiByteParser(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   *DigiByteParser
   173  	}
   174  	tests := []struct {
   175  		name    string
   176  		args    args
   177  		want    *bchain.Tx
   178  		want1   uint32
   179  		wantErr bool
   180  	}{
   181  		{
   182  			name: "digibyte-1",
   183  			args: args{
   184  				packedTx: testTxPacked1,
   185  				parser:   NewDigiByteParser(GetChainParams("main"), &btc.Configuration{}),
   186  			},
   187  			want:    &testTx1,
   188  			want1:   7000002,
   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  }