github.com/bchainhub/blockbook@v0.3.2/bchain/coins/vipstarcoin/vipstarcoinparser_test.go (about) 1 // +build unittest 2 3 package vipstarcoin 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: "VLhjpFadC3pkyydKfMTbdg6JFaYojkubik"}, 36 want: "76a9146e2b0a8655786c8c5ea7b9ce478f03e00ecb2f5588ac", 37 wantErr: false, 38 }, 39 { 40 name: "P2PKH2", 41 args: args{address: "VEUMnC1iPpr64u4HwE9DRjhwnF98xbS6n4"}, 42 want: "76a91429d27c0c33c87830dda711aa878574bcd9c5247188ac", 43 wantErr: false, 44 }, 45 { 46 name: "P2SH1", 47 args: args{address: "MAyVr99hsthBJin9sosjdyfeAP3ByxXFxz"}, 48 want: "a91421ba6a62ac1d74d2ba921bbc8c9a3ca6e1420a0087", 49 wantErr: false, 50 }, 51 { 52 name: "P2SH2", 53 args: args{address: "MV71zj5jJ1b9ijt198JAJrCwDHxu3tCn6g"}, 54 want: "a914e898c1f90c736de7c3570c3391bf5e726c8b59aa87", 55 wantErr: false, 56 }, 57 { 58 name: "P2WPKH", 59 args: args{address: "vips1q0v3tadxj6pm3ym9j06v9rfyw0jeh5f8s87sgtw"}, 60 want: "00147b22beb4d2d077126cb27e9851a48e7cb37a24f0", 61 wantErr: false, 62 }, 63 { 64 name: "P2WSH", 65 args: args{address: "vips1qumpyvyxz25kfjjrvyxn3zlyc2wfc0m3l3gm5pg99c4mxylemfqhsnj023l"}, 66 want: "0020e6c24610c2552c99486c21a7117c98539387ee3f8a3740a0a5c576627f3b482f", 67 wantErr: false, 68 }, 69 } 70 parser := NewVIPSTARCOINParser(GetChainParams("main"), &btc.Configuration{}) 71 72 for _, tt := range tests { 73 t.Run(tt.name, func(t *testing.T) { 74 got, err := parser.GetAddrDescFromAddress(tt.args.address) 75 if (err != nil) != tt.wantErr { 76 t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr) 77 return 78 } 79 h := hex.EncodeToString(got) 80 if !reflect.DeepEqual(h, tt.want) { 81 t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want) 82 } 83 }) 84 } 85 } 86 87 func Test_GetAddressesFromAddrDesc(t *testing.T) { 88 type args struct { 89 script string 90 } 91 tests := []struct { 92 name string 93 args args 94 want []string 95 want2 bool 96 wantErr bool 97 }{ 98 { 99 name: "P2PKH", 100 args: args{script: "76a9146e2b0a8655786c8c5ea7b9ce478f03e00ecb2f5588ac"}, 101 want: []string{"VLhjpFadC3pkyydKfMTbdg6JFaYojkubik"}, 102 want2: true, 103 wantErr: false, 104 }, 105 { 106 name: "P2SH", 107 args: args{script: "a91421ba6a62ac1d74d2ba921bbc8c9a3ca6e1420a0087"}, 108 want: []string{"MAyVr99hsthBJin9sosjdyfeAP3ByxXFxz"}, 109 want2: true, 110 wantErr: false, 111 }, 112 { 113 name: "P2WPKH", 114 args: args{script: "00147b22beb4d2d077126cb27e9851a48e7cb37a24f0"}, 115 want: []string{"vips1q0v3tadxj6pm3ym9j06v9rfyw0jeh5f8s87sgtw"}, 116 want2: true, 117 wantErr: false, 118 }, 119 { 120 name: "P2WSH", 121 args: args{script: "0020e6c24610c2552c99486c21a7117c98539387ee3f8a3740a0a5c576627f3b482f"}, 122 want: []string{"vips1qumpyvyxz25kfjjrvyxn3zlyc2wfc0m3l3gm5pg99c4mxylemfqhsnj023l"}, 123 want2: true, 124 wantErr: false, 125 }, 126 { 127 name: "OP_RETURN ascii", 128 args: args{script: "6a0461686f6a"}, 129 want: []string{"OP_RETURN (ahoj)"}, 130 want2: false, 131 wantErr: false, 132 }, 133 { 134 name: "OP_RETURN hex", 135 args: args{script: "6a072020f1686f6a20"}, 136 want: []string{"OP_RETURN 2020f1686f6a20"}, 137 want2: false, 138 wantErr: false, 139 }, 140 } 141 142 parser := NewVIPSTARCOINParser(GetChainParams("main"), &btc.Configuration{}) 143 144 for _, tt := range tests { 145 t.Run(tt.name, func(t *testing.T) { 146 b, _ := hex.DecodeString(tt.args.script) 147 got, got2, err := parser.GetAddressesFromAddrDesc(b) 148 if (err != nil) != tt.wantErr { 149 t.Errorf("outputScriptToAddresses() error = %v, wantErr %v", err, tt.wantErr) 150 return 151 } 152 if !reflect.DeepEqual(got, tt.want) { 153 t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got, tt.want) 154 } 155 if !reflect.DeepEqual(got2, tt.want2) { 156 t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got2, tt.want2) 157 } 158 }) 159 } 160 } 161 162 var ( 163 testTx1 bchain.Tx 164 165 testTxPacked1 = "000831508bcbe1ae6002000000000103a733281698ae7d80da17d5a201ab8b6077e83f780cb417c8d12eb9da8343735900000000171600141ee6fc3b04fdd080c03eeb513498b38c2621f4fcfdffffff3976295b67ed10a28016171da4f9a5833fe4ca1f83ffb56f95856aa445ebf46a000000006a473044022012315ff56ad254ed8b099623bd84a106819770970c41c2a138b6cfe4bb332aa602206f5679570c968b77a3f7e8dd14663d0af9aba5b354f113d3a4321b3aeafc03080121030d52fc12b11b9288490ed78b8b07ff025a33fe1577f402ddf30c0f73769363a3fdffffffdd5a7a9852c8ebe1c417a26c54bb58339eb6e1ea1416b9b11314f05f93e69ea1010000006a4730440220149942b3971fb655bd5d76630bb1c8993d3083e4ad631c972156927173e6afb802204590399049f77530251c86ab569f45d2248aa222372b9d1d95963bd67213a5f80121030d52fc12b11b9288490ed78b8b07ff025a33fe1577f402ddf30c0f73769363a3fdffffff0200ca9a3b000000001976a91493c052c292e366221f9ee709c36a9ea441eb984488acb0b20e000000000017a9149695605a5a5e9349e1c99e01b175c5c3baf39bc4870247304402207a2a1cc2f314c8c659a4bcbce099c5adfb217c03fa2b0cfc95bef48c1507901a0220324ab06cf2fe4c9e446a3a12c00fa611a479b5734f62c20b66e919e173a2c699012102bbe6f37b4c44303b2186de6784d02cc5b86a65ca1203821b06a98e243b44c76400004e310800" 166 ) 167 168 func init() { 169 testTx1 = bchain.Tx{ 170 Hex: "02000000000103a733281698ae7d80da17d5a201ab8b6077e83f780cb417c8d12eb9da8343735900000000171600141ee6fc3b04fdd080c03eeb513498b38c2621f4fcfdffffff3976295b67ed10a28016171da4f9a5833fe4ca1f83ffb56f95856aa445ebf46a000000006a473044022012315ff56ad254ed8b099623bd84a106819770970c41c2a138b6cfe4bb332aa602206f5679570c968b77a3f7e8dd14663d0af9aba5b354f113d3a4321b3aeafc03080121030d52fc12b11b9288490ed78b8b07ff025a33fe1577f402ddf30c0f73769363a3fdffffffdd5a7a9852c8ebe1c417a26c54bb58339eb6e1ea1416b9b11314f05f93e69ea1010000006a4730440220149942b3971fb655bd5d76630bb1c8993d3083e4ad631c972156927173e6afb802204590399049f77530251c86ab569f45d2248aa222372b9d1d95963bd67213a5f80121030d52fc12b11b9288490ed78b8b07ff025a33fe1577f402ddf30c0f73769363a3fdffffff0200ca9a3b000000001976a91493c052c292e366221f9ee709c36a9ea441eb984488acb0b20e000000000017a9149695605a5a5e9349e1c99e01b175c5c3baf39bc4870247304402207a2a1cc2f314c8c659a4bcbce099c5adfb217c03fa2b0cfc95bef48c1507901a0220324ab06cf2fe4c9e446a3a12c00fa611a479b5734f62c20b66e919e173a2c699012102bbe6f37b4c44303b2186de6784d02cc5b86a65ca1203821b06a98e243b44c76400004e310800", 171 Blocktime: 1555835824, 172 Txid: "93aae65e87ec46cd13b3032e1588c7db75e2b712514696efca5f2bfd80c16632", 173 LockTime: 536910, 174 Version: 2, 175 Vin: []bchain.Vin{ 176 { 177 ScriptSig: bchain.ScriptSig{ 178 Hex: "1600141ee6fc3b04fdd080c03eeb513498b38c2621f4fc", 179 }, 180 Txid: "59734383dab92ed1c817b40c783fe877608bab01a2d517da807dae98162833a7", 181 Vout: 0, 182 Sequence: 4294967293, 183 }, 184 { 185 ScriptSig: bchain.ScriptSig{ 186 Hex: "473044022012315ff56ad254ed8b099623bd84a106819770970c41c2a138b6cfe4bb332aa602206f5679570c968b77a3f7e8dd14663d0af9aba5b354f113d3a4321b3aeafc03080121030d52fc12b11b9288490ed78b8b07ff025a33fe1577f402ddf30c0f73769363a3", 187 }, 188 Txid: "6af4eb45a46a85956fb5ff831fcae43f83a5f9a41d171680a210ed675b297639", 189 Vout: 0, 190 Sequence: 4294967293, 191 }, 192 { 193 ScriptSig: bchain.ScriptSig{ 194 Hex: "4730440220149942b3971fb655bd5d76630bb1c8993d3083e4ad631c972156927173e6afb802204590399049f77530251c86ab569f45d2248aa222372b9d1d95963bd67213a5f80121030d52fc12b11b9288490ed78b8b07ff025a33fe1577f402ddf30c0f73769363a3", 195 }, 196 Txid: "a19ee6935ff01413b1b91614eae1b69e3358bb546ca217c4e1ebc852987a5add", 197 Vout: 1, 198 Sequence: 4294967293, 199 }, 200 }, 201 Vout: []bchain.Vout{ 202 { 203 ValueSat: *big.NewInt(1000000000), 204 N: 0, 205 ScriptPubKey: bchain.ScriptPubKey{ 206 Hex: "76a91493c052c292e366221f9ee709c36a9ea441eb984488ac", 207 Addresses: []string{ 208 "VQ8Tef3y1hKQnAq5baiyxiScsVnEcPqjdy", 209 }, 210 }, 211 }, 212 { 213 ValueSat: *big.NewInt(963248), 214 N: 1, 215 ScriptPubKey: bchain.ScriptPubKey{ 216 Hex: "a9149695605a5a5e9349e1c99e01b175c5c3baf39bc487", 217 Addresses: []string{ 218 "MMdNY4HfcntfZ1EB6zn5Zz7i25yYfocaFT", 219 }, 220 }, 221 }, 222 }, 223 } 224 } 225 226 func Test_PackTx(t *testing.T) { 227 type args struct { 228 tx bchain.Tx 229 height uint32 230 blockTime int64 231 parser *VIPSTARCOINParser 232 } 233 tests := []struct { 234 name string 235 args args 236 want string 237 wantErr bool 238 }{ 239 { 240 name: "VIPSTARCOIN-1", 241 args: args{ 242 tx: testTx1, 243 height: 536912, 244 blockTime: 1555835824, 245 parser: NewVIPSTARCOINParser(GetChainParams("main"), &btc.Configuration{}), 246 }, 247 want: testTxPacked1, 248 wantErr: false, 249 }, 250 } 251 for _, tt := range tests { 252 t.Run(tt.name, func(t *testing.T) { 253 got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime) 254 if (err != nil) != tt.wantErr { 255 t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr) 256 return 257 } 258 h := hex.EncodeToString(got) 259 if !reflect.DeepEqual(h, tt.want) { 260 t.Errorf("packTx() = %v, want %v", h, tt.want) 261 } 262 }) 263 } 264 } 265 266 func Test_UnpackTx(t *testing.T) { 267 type args struct { 268 packedTx string 269 parser *VIPSTARCOINParser 270 } 271 tests := []struct { 272 name string 273 args args 274 want *bchain.Tx 275 want1 uint32 276 wantErr bool 277 }{ 278 { 279 name: "VIPSTARCOIN-1", 280 args: args{ 281 packedTx: testTxPacked1, 282 parser: NewVIPSTARCOINParser(GetChainParams("main"), &btc.Configuration{}), 283 }, 284 want: &testTx1, 285 want1: 536912, 286 wantErr: false, 287 }, 288 } 289 for _, tt := range tests { 290 t.Run(tt.name, func(t *testing.T) { 291 b, _ := hex.DecodeString(tt.args.packedTx) 292 got, got1, err := tt.args.parser.UnpackTx(b) 293 if (err != nil) != tt.wantErr { 294 t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr) 295 return 296 } 297 if !reflect.DeepEqual(got, tt.want) { 298 t.Errorf("unpackTx() got = %v, want %v", got, tt.want) 299 } 300 if got1 != tt.want1 { 301 t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1) 302 } 303 }) 304 } 305 }