github.com/cerberus-wallet/blockbook@v0.3.2/bchain/coins/bellcoin/bellcoinparser_test.go (about) 1 // +build unittest 2 3 package bellcoin 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: "BSQmMGWpjwP5Lu8feSypSaPFiTTrC3EdEx"}, 36 want: "76a914f0e2aff6730b53b9986a5db8ca17c59426134a0988ac", 37 wantErr: false, 38 }, 39 { 40 name: "P2PKH2", 41 args: args{address: "BLpETLYW1vs8csYSoYeCvPwsiCSTJUjx6T"}, 42 want: "76a914b3822026c7f758b43a0882d7f2cbfa954702e45688ac", 43 wantErr: false, 44 }, 45 { 46 name: "P2SH1", 47 args: args{address: "bNn9Y9YfgNUHXopXJEesS9M8noJzzrWTmP"}, 48 want: "a9146e3c881d51d62a668362a5bba28be438f9c548e287", 49 wantErr: false, 50 }, 51 { 52 name: "P2SH2", 53 args: args{address: "bXCT73hNPSwCeVbpvo3wJJU3erRjawUGSu"}, 54 want: "a914ca962f788569443b33ec673208ccdcfaac6873b487", 55 wantErr: false, 56 }, 57 { 58 name: "witness_v0_keyhash", 59 args: args{address: "bm1q3msdh3npg5ufvwm2sxltxcet6hll9tjzkzxzqt"}, 60 want: "00148ee0dbc6614538963b6a81beb3632bd5fff2ae42", 61 wantErr: false, 62 }, 63 { 64 name: "witness_v0_scripthashx", 65 args: args{address: "bm1q0uvgqxwqt0u4esawwcff7652jreqye30kmp4sj5dtydee50pze0sjx6wn5"}, 66 want: "00207f188019c05bf95cc3ae76129f6a8a90f202662fb6c3584a8d591b9cd1e1165f", 67 wantErr: false, 68 }, 69 } 70 parser := NewBellcoinParser(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: "76a914f0e2aff6730b53b9986a5db8ca17c59426134a0988ac"}, 101 want: []string{"BSQmMGWpjwP5Lu8feSypSaPFiTTrC3EdEx"}, 102 want2: true, 103 wantErr: false, 104 }, 105 { 106 name: "P2SH", 107 args: args{script: "a9146e3c881d51d62a668362a5bba28be438f9c548e287"}, 108 want: []string{"bNn9Y9YfgNUHXopXJEesS9M8noJzzrWTmP"}, 109 want2: true, 110 wantErr: false, 111 }, 112 { 113 name: "P2WPKH", 114 args: args{script: "00148ee0dbc6614538963b6a81beb3632bd5fff2ae42"}, 115 want: []string{"bm1q3msdh3npg5ufvwm2sxltxcet6hll9tjzkzxzqt"}, 116 want2: true, 117 wantErr: false, 118 }, 119 { 120 name: "P2WSH", 121 args: args{script: "00207f188019c05bf95cc3ae76129f6a8a90f202662fb6c3584a8d591b9cd1e1165f"}, 122 want: []string{"bm1q0uvgqxwqt0u4esawwcff7652jreqye30kmp4sj5dtydee50pze0sjx6wn5"}, 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 := NewBellcoinParser(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 = "0003238c8bc5aac144020000000001026f3631c4db09d48354d12cd2e780b2aa0f92198fca7d044a6b073e9a06b4135d0200000017160014dfc67c36c26ea8036042c56b8ce5d5027d1fb81ffeffffffac13cfbe663797aced3134006adec863e5b3f9480ab65a4dda112de875f8dd6e010000006a473044022066e4d5f99fec12f076d7d912d1e530e4b950d23cc3de2b8127f98109f510c9d502207a74f2e7a753262fdd29756193493d47ead7880871cbc6c55cc6d20e229c0c1201210294fc5b3928335caddc7f4c536e0db85c736cbc7c164de0319976da90b65d288ffeffffff05f4c16020000000001976a91416e20046e69c396aed18c6663ddcbbf1dde9082f88ac0aa47113000000001976a91426fa5e6c4e579058d3f4d1dd83d99e291d4dc0c588acbcb11a00000000001976a9145a53bd436b5c19a42b1518ef18443e8403bcaeed88ac0c04693b0000000017a9140576053f982117afcff024013ed61270189c4fbc87358bad07000000001976a914e00ec357dfee124ac68b3c10dcf82e2ed0993ccc88ac02483045022100ed4b0e9b140850951ffbc10349e3ac56a18b80c600a77b95cfac274e10228eb602207c876b9b134e63b8a01ba28720dc4c1c2c67bb7e547fb71d31440cd365c674260121027aa4243e82c73c9c15d544a0b61a828eed1128464952bfbdc9235d4380f2767d008a230300" 166 ) 167 168 func init() { 169 testTx1 = bchain.Tx{ 170 Hex: "020000000001026f3631c4db09d48354d12cd2e780b2aa0f92198fca7d044a6b073e9a06b4135d0200000017160014dfc67c36c26ea8036042c56b8ce5d5027d1fb81ffeffffffac13cfbe663797aced3134006adec863e5b3f9480ab65a4dda112de875f8dd6e010000006a473044022066e4d5f99fec12f076d7d912d1e530e4b950d23cc3de2b8127f98109f510c9d502207a74f2e7a753262fdd29756193493d47ead7880871cbc6c55cc6d20e229c0c1201210294fc5b3928335caddc7f4c536e0db85c736cbc7c164de0319976da90b65d288ffeffffff05f4c16020000000001976a91416e20046e69c396aed18c6663ddcbbf1dde9082f88ac0aa47113000000001976a91426fa5e6c4e579058d3f4d1dd83d99e291d4dc0c588acbcb11a00000000001976a9145a53bd436b5c19a42b1518ef18443e8403bcaeed88ac0c04693b0000000017a9140576053f982117afcff024013ed61270189c4fbc87358bad07000000001976a914e00ec357dfee124ac68b3c10dcf82e2ed0993ccc88ac02483045022100ed4b0e9b140850951ffbc10349e3ac56a18b80c600a77b95cfac274e10228eb602207c876b9b134e63b8a01ba28720dc4c1c2c67bb7e547fb71d31440cd365c674260121027aa4243e82c73c9c15d544a0b61a828eed1128464952bfbdc9235d4380f2767d008a230300", 171 Blocktime: 1549095010, 172 Txid: "e7ef52bbf3d9cb1ca5dfdb02eabf108e2b0b7757b009d1cfb24a06e4126e67f2", 173 LockTime: 205706, 174 Version: 2, 175 Vin: []bchain.Vin{ 176 { 177 ScriptSig: bchain.ScriptSig{ 178 Hex: "160014dfc67c36c26ea8036042c56b8ce5d5027d1fb81f", 179 }, 180 Txid: "5d13b4069a3e076b4a047dca8f19920faab280e7d22cd15483d409dbc431366f", 181 Vout: 2, 182 Sequence: 4294967294, 183 }, 184 { 185 ScriptSig: bchain.ScriptSig{ 186 Hex: "473044022066e4d5f99fec12f076d7d912d1e530e4b950d23cc3de2b8127f98109f510c9d502207a74f2e7a753262fdd29756193493d47ead7880871cbc6c55cc6d20e229c0c1201210294fc5b3928335caddc7f4c536e0db85c736cbc7c164de0319976da90b65d288f", 187 }, 188 Txid: "6eddf875e82d11da4d5ab60a48f9b3e563c8de6a003431edac973766becf13ac", 189 Vout: 1, 190 Sequence: 4294967294, 191 }, 192 }, 193 Vout: []bchain.Vout{ 194 { 195 ValueSat: *big.NewInt(543212020), 196 N: 0, 197 ScriptPubKey: bchain.ScriptPubKey{ 198 Hex: "76a91416e20046e69c396aed18c6663ddcbbf1dde9082f88ac", 199 Addresses: []string{ 200 "B6Y5DmPr1LPUP95YDEnX6FbJHERVipkRcg", 201 }, 202 }, 203 }, 204 { 205 ValueSat: *big.NewInt(326214666), 206 N: 1, 207 ScriptPubKey: bchain.ScriptPubKey{ 208 Hex: "76a91426fa5e6c4e579058d3f4d1dd83d99e291d4dc0c588ac", 209 Addresses: []string{ 210 "B81BDwJTasemPgnHBxQ67wX2WV48b2XmEc", 211 }, 212 }, 213 }, 214 { 215 ValueSat: *big.NewInt(1749436), 216 N: 2, 217 ScriptPubKey: bchain.ScriptPubKey{ 218 Hex: "76a9145a53bd436b5c19a42b1518ef18443e8403bcaeed88ac", 219 Addresses: []string{ 220 "BCggkHk8ZTVd9T4yseNmURj6w56XY47EUG", 221 }, 222 }, 223 }, 224 { 225 ValueSat: *big.NewInt(996738060), 226 N: 3, 227 ScriptPubKey: bchain.ScriptPubKey{ 228 Hex: "a9140576053f982117afcff024013ed61270189c4fbc87", 229 Addresses: []string{ 230 "bDE9TQ3W5zF4ej8hLwYVdK5w8n5zhd9Qxj", 231 }, 232 }, 233 }, 234 { 235 ValueSat: *big.NewInt(128813877), 236 N: 4, 237 ScriptPubKey: bchain.ScriptPubKey{ 238 Hex: "76a914e00ec357dfee124ac68b3c10dcf82e2ed0993ccc88ac", 239 Addresses: []string{ 240 "BQsnfSXNonZZMs1G6ndrVLbUJAK5tFG2bN", 241 }, 242 }, 243 }, 244 }, 245 } 246 } 247 248 func Test_PackTx(t *testing.T) { 249 type args struct { 250 tx bchain.Tx 251 height uint32 252 blockTime int64 253 parser *BellcoinParser 254 } 255 tests := []struct { 256 name string 257 args args 258 want string 259 wantErr bool 260 }{ 261 { 262 name: "Bellcoin-1", 263 args: args{ 264 tx: testTx1, 265 height: 205708, 266 blockTime: 1549095010, 267 parser: NewBellcoinParser(GetChainParams("main"), &btc.Configuration{}), 268 }, 269 want: testTxPacked1, 270 wantErr: false, 271 }, 272 } 273 for _, tt := range tests { 274 t.Run(tt.name, func(t *testing.T) { 275 got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime) 276 if (err != nil) != tt.wantErr { 277 t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr) 278 return 279 } 280 h := hex.EncodeToString(got) 281 if !reflect.DeepEqual(h, tt.want) { 282 t.Errorf("packTx() = %v, want %v", h, tt.want) 283 } 284 }) 285 } 286 } 287 288 func Test_UnpackTx(t *testing.T) { 289 type args struct { 290 packedTx string 291 parser *BellcoinParser 292 } 293 tests := []struct { 294 name string 295 args args 296 want *bchain.Tx 297 want1 uint32 298 wantErr bool 299 }{ 300 { 301 name: "Bellcoin-1", 302 args: args{ 303 packedTx: testTxPacked1, 304 parser: NewBellcoinParser(GetChainParams("main"), &btc.Configuration{}), 305 }, 306 want: &testTx1, 307 want1: 205708, 308 wantErr: false, 309 }, 310 } 311 for _, tt := range tests { 312 t.Run(tt.name, func(t *testing.T) { 313 b, _ := hex.DecodeString(tt.args.packedTx) 314 got, got1, err := tt.args.parser.UnpackTx(b) 315 if (err != nil) != tt.wantErr { 316 t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr) 317 return 318 } 319 if !reflect.DeepEqual(got, tt.want) { 320 t.Errorf("unpackTx() got = %v, want %v", got, tt.want) 321 } 322 if got1 != tt.want1 { 323 t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1) 324 } 325 }) 326 } 327 }