github.com/bchainhub/blockbook@v0.3.2/bchain/coins/bitcore/bitcoreparser_test.go (about) 1 // +build unittest 2 3 package bitcore 4 5 import ( 6 "blockbook/bchain" 7 "blockbook/bchain/coins/btc" 8 "encoding/hex" 9 "github.com/martinboehm/btcutil/chaincfg" 10 "math/big" 11 "os" 12 "reflect" 13 "testing" 14 ) 15 16 func TestMain(m *testing.M) { 17 c := m.Run() 18 chaincfg.ResetParams() 19 os.Exit(c) 20 } 21 22 func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) { 23 type args struct { 24 address string 25 } 26 tests := []struct { 27 name string 28 args args 29 want string 30 wantErr bool 31 }{ 32 { 33 name: "pubkeyhash1", 34 args: args{address: "2HbJfcGD6NTm318VeBjfd2hLf44hHkzHVV"}, 35 want: "76a9143236327ebad3be5e336777bb3562439720f38dc488ac", 36 wantErr: false, 37 }, 38 { 39 name: "pubkeyhash2", 40 args: args{address: "2XFmLkpyBzJncnnkALQ3qnMqSqUdqcBdc4"}, 41 want: "76a914c815e7f760bbd5f109d58e848cf78ba808d972e088ac", 42 wantErr: false, 43 }, 44 { 45 name: "scripthash1", 46 args: args{address: "scUVfntFvnyTHRCvBwUNyKGVFLiBb2iHVK"}, 47 want: "a914c7ec567ef583a96a74c02980cc42f728cc987c3287", 48 wantErr: false, 49 }, 50 { 51 name: "scripthash2", 52 args: args{address: "sVeAXe1CMWVAuq5174hG49QRfkBp4GFvAu"}, 53 want: "a9147cf7a3a6b1305871ff5f0f064aaa634880ff67ab87", 54 wantErr: false, 55 }, 56 { 57 name: "witness_v0_keyhash", 58 args: args{address: "btx1qnfkmarp8pe8q05690zd48qma3gmp0pp66gqsv3"}, 59 want: "00149a6dbe8c270e4e07d345789b53837d8a3617843a", 60 wantErr: false, 61 }, 62 } 63 parser := NewBitcoreParser(GetChainParams("main"), &btc.Configuration{}) 64 65 for _, tt := range tests { 66 t.Run(tt.name, func(t *testing.T) { 67 got, err := parser.GetAddrDescFromAddress(tt.args.address) 68 if (err != nil) != tt.wantErr { 69 t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr) 70 return 71 } 72 h := hex.EncodeToString(got) 73 if !reflect.DeepEqual(h, tt.want) { 74 t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want) 75 } 76 }) 77 } 78 } 79 80 var ( 81 testTx1 bchain.Tx 82 83 testTxPacked1 = "0a20fcd4f2e45787a33571bc9b2ce939d6e8e51fa053296de9240f05455702bd954012e2010200000001f69bd1fd76e52a426f21332e3b7cfbc3350eacbd21c6e0c11a7ae11919803ef0010000006b483045022100d1fa62b9d7860a03e1dcd4734fe42457cb508ebb49e896d7a77748d997d09fba022005f1657b39451afe97076d8667fe5f6f18ca76391521ab84d09d5b82137d933b0121035aaf032f13761f27465467dc73f1998a80dd4d85a6353d2832a7244d7b591d3effffffff02a87322b3010000001976a914d0c320db3fbd0abe2b6fe31a3bca4fed8ce8669588ac94b94f37000000001976a9145584ee07090af59938e991c9d8e9e945c99a449f88ac0000000018858a8ce205200028f9f3133299010a001220f03e801919e17a1ac1e0c621bdac0e35c3fb7c3b2e33216f422ae576fdd19bf61801226b483045022100d1fa62b9d7860a03e1dcd4734fe42457cb508ebb49e896d7a77748d997d09fba022005f1657b39451afe97076d8667fe5f6f18ca76391521ab84d09d5b82137d933b0121035aaf032f13761f27465467dc73f1998a80dd4d85a6353d2832a7244d7b591d3e28ffffffff0f3a480a0501b32273a810001a1976a914d0c320db3fbd0abe2b6fe31a3bca4fed8ce8669588ac22223259336546797741414673617039757139726942474143684e326858356a6e7268753a470a04374fb99410011a1976a9145584ee07090af59938e991c9d8e9e945c99a449f88ac2222324c6f7a646b704450723562356b6a66445042315a76454c597735734475684139594002" 84 ) 85 86 func init() { 87 testTx1 = bchain.Tx{ 88 Hex: "0200000001f69bd1fd76e52a426f21332e3b7cfbc3350eacbd21c6e0c11a7ae11919803ef0010000006b483045022100d1fa62b9d7860a03e1dcd4734fe42457cb508ebb49e896d7a77748d997d09fba022005f1657b39451afe97076d8667fe5f6f18ca76391521ab84d09d5b82137d933b0121035aaf032f13761f27465467dc73f1998a80dd4d85a6353d2832a7244d7b591d3effffffff02a87322b3010000001976a914d0c320db3fbd0abe2b6fe31a3bca4fed8ce8669588ac94b94f37000000001976a9145584ee07090af59938e991c9d8e9e945c99a449f88ac00000000", 89 Blocktime: 1547896069, 90 Time: 1547896069, 91 Txid: "fcd4f2e45787a33571bc9b2ce939d6e8e51fa053296de9240f05455702bd9540", 92 LockTime: 0, 93 Version: 2, 94 Vin: []bchain.Vin{ 95 { 96 ScriptSig: bchain.ScriptSig{ 97 Hex: "483045022100d1fa62b9d7860a03e1dcd4734fe42457cb508ebb49e896d7a77748d997d09fba022005f1657b39451afe97076d8667fe5f6f18ca76391521ab84d09d5b82137d933b0121035aaf032f13761f27465467dc73f1998a80dd4d85a6353d2832a7244d7b591d3e", 98 }, 99 Txid: "f03e801919e17a1ac1e0c621bdac0e35c3fb7c3b2e33216f422ae576fdd19bf6", 100 Vout: 1, 101 Sequence: 4294967295, 102 }, 103 }, 104 Vout: []bchain.Vout{ 105 { 106 ValueSat: *big.NewInt(7300346792), 107 N: 0, 108 ScriptPubKey: bchain.ScriptPubKey{ 109 Hex: "76a914d0c320db3fbd0abe2b6fe31a3bca4fed8ce8669588ac", 110 Addresses: []string{ 111 "2Y3eFywAAFsap9uq9riBGAChN2hX5jnrhu", 112 }, 113 }, 114 }, 115 { 116 ValueSat: *big.NewInt(927971732), 117 N: 1, 118 ScriptPubKey: bchain.ScriptPubKey{ 119 Hex: "76a9145584ee07090af59938e991c9d8e9e945c99a449f88ac", 120 Addresses: []string{ 121 "2LozdkpDPr5b5kjfDPB1ZvELYw5sDuhA9Y", 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 *BitcoreParser 135 } 136 tests := []struct { 137 name string 138 args args 139 want string 140 wantErr bool 141 }{ 142 { 143 name: "bitcore-1", 144 args: args{ 145 tx: testTx1, 146 height: 326137, 147 blockTime: 1547896069, 148 parser: NewBitcoreParser(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 *BitcoreParser 173 } 174 tests := []struct { 175 name string 176 args args 177 want *bchain.Tx 178 want1 uint32 179 wantErr bool 180 }{ 181 { 182 name: "bitcore-1", 183 args: args{ 184 packedTx: testTxPacked1, 185 parser: NewBitcoreParser(GetChainParams("main"), &btc.Configuration{}), 186 }, 187 want: &testTx1, 188 want1: 326137, 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 }