github.com/bchainhub/blockbook@v0.3.2/bchain/coins/myriad/myriadparser_test.go (about) 1 // +build unittest 2 3 package myriad 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: "MUs3PnZLdBQyct2emEc7QJvVnjeQj52kug"}, 36 want: "76a914e5f419d3b464c67152fb9d3ecc36932d5280673f88ac", 37 wantErr: false, 38 }, 39 { 40 name: "P2SH1", 41 args: args{address: "4ijSZESajWvhhJAz1APdzGivwc31WCjxHD"}, 42 want: "a9143e69d8c4772eb34d77c96aae58c041e887b404f387", 43 wantErr: false, 44 }, 45 { 46 name: "witness_v0_keyhash", 47 args: args{address: "my1qr9y3pd7wy7jjpqf87qsmp08ecppc0p2jxhfcfc"}, 48 want: "0014194910b7ce27a5208127f021b0bcf9c043878552", 49 wantErr: false, 50 }, 51 } 52 parser := NewMyriadParser(GetChainParams("main"), &btc.Configuration{}) 53 54 for _, tt := range tests { 55 t.Run(tt.name, func(t *testing.T) { 56 got, err := parser.GetAddrDescFromAddress(tt.args.address) 57 if (err != nil) != tt.wantErr { 58 t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr) 59 return 60 } 61 h := hex.EncodeToString(got) 62 if !reflect.DeepEqual(h, tt.want) { 63 t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want) 64 } 65 }) 66 } 67 } 68 69 func Test_GetAddressesFromAddrDesc(t *testing.T) { 70 type args struct { 71 script string 72 } 73 tests := []struct { 74 name string 75 args args 76 want []string 77 want2 bool 78 wantErr bool 79 }{ 80 { 81 name: "P2PKH1", 82 args: args{script: "76a914e5f419d3b464c67152fb9d3ecc36932d5280673f88ac"}, 83 want: []string{"MUs3PnZLdBQyct2emEc7QJvVnjeQj52kug"}, 84 want2: true, 85 wantErr: false, 86 }, 87 { 88 name: "P2SH1", 89 args: args{script: "a9143e69d8c4772eb34d77c96aae58c041e887b404f387"}, 90 want: []string{"4ijSZESajWvhhJAz1APdzGivwc31WCjxHD"}, 91 want2: true, 92 wantErr: false, 93 }, 94 { 95 name: "witness_v0_keyhash", 96 args: args{script: "0014194910b7ce27a5208127f021b0bcf9c043878552"}, 97 want: []string{"my1qr9y3pd7wy7jjpqf87qsmp08ecppc0p2jxhfcfc"}, 98 want2: true, 99 wantErr: false, 100 }, 101 { 102 name: "pubkey", 103 args: args{script: "2102c5c7165eb66f35a120f2f9d97fa61b1be6c621f9b868454b35a284fa7ecc831eac"}, 104 want: []string{"MDac4WH5jxqWLwe6kmWzR3ERu6BgszXUwn"}, 105 want2: false, 106 wantErr: false, 107 }, 108 } 109 110 parser := NewMyriadParser(GetChainParams("main"), &btc.Configuration{}) 111 112 for _, tt := range tests { 113 t.Run(tt.name, func(t *testing.T) { 114 b, _ := hex.DecodeString(tt.args.script) 115 got, got2, err := parser.GetAddressesFromAddrDesc(b) 116 if (err != nil) != tt.wantErr { 117 t.Errorf("GetAddressesFromAddrDesc() error = %v, wantErr %v", err, tt.wantErr) 118 return 119 } 120 if !reflect.DeepEqual(got, tt.want) { 121 t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got, tt.want) 122 } 123 if !reflect.DeepEqual(got2, tt.want2) { 124 t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got2, tt.want2) 125 } 126 }) 127 } 128 } 129 130 var ( 131 testTx1 bchain.Tx 132 testTxPacked1 = "00004e208ab194a1180100000001163465df9bb21d89e90056f11887a398d5a313aef71e3974306459661a91588c000000006b4830450220129c9e9a27406796f3f7d7edcc446037b38ddb3ef94745cec8e7cde618a811140221008eb3b893cdd3725e99b74c020867821e1f74199065260586f5ef3c22b133dd2a012103e2e23d38dc8fa493cde4077f650ab9f22eacafd14a10b123994f38c9f35dfee9ffffffff025e90ec28050000001976a9141cba92fe1510b8c73550fd4d3e0b44acdffcd12d88ac79c268ba0a0000001976a9142f86cdfa98cac89143cf9e3d309cc072caccdf6f88ac00000000" 133 ) 134 135 func init() { 136 testTx1 = bchain.Tx{ 137 Hex: "0100000001163465df9bb21d89e90056f11887a398d5a313aef71e3974306459661a91588c000000006b4830450220129c9e9a27406796f3f7d7edcc446037b38ddb3ef94745cec8e7cde618a811140221008eb3b893cdd3725e99b74c020867821e1f74199065260586f5ef3c22b133dd2a012103e2e23d38dc8fa493cde4077f650ab9f22eacafd14a10b123994f38c9f35dfee9ffffffff025e90ec28050000001976a9141cba92fe1510b8c73550fd4d3e0b44acdffcd12d88ac79c268ba0a0000001976a9142f86cdfa98cac89143cf9e3d309cc072caccdf6f88ac00000000", 138 Blocktime: 1393723468, 139 Txid: "b01e2eb866ed101ed117b4ad18b753929e85c42e3d8add76bdd16e5c00519dcc", 140 LockTime: 0, 141 Version: 1, 142 Vin: []bchain.Vin{ 143 { 144 ScriptSig: bchain.ScriptSig{ 145 Hex: "4830450220129c9e9a27406796f3f7d7edcc446037b38ddb3ef94745cec8e7cde618a811140221008eb3b893cdd3725e99b74c020867821e1f74199065260586f5ef3c22b133dd2a012103e2e23d38dc8fa493cde4077f650ab9f22eacafd14a10b123994f38c9f35dfee9", 146 }, 147 Txid: "8c58911a6659643074391ef7ae13a3d598a38718f15600e9891db29bdf653416", 148 Vout: 0, 149 Sequence: 4294967295, 150 }, 151 }, 152 Vout: []bchain.Vout{ 153 { 154 ValueSat: *big.NewInt(22161428574), 155 N: 0, 156 ScriptPubKey: bchain.ScriptPubKey{ 157 Hex: "76a9141cba92fe1510b8c73550fd4d3e0b44acdffcd12d88ac", 158 Addresses: []string{ 159 "MAX4fCkTJwaRzbA3xzJp9DjrMwnnK32T6Z", 160 }, 161 }, 162 }, 163 { 164 ValueSat: *big.NewInt(46077100665), 165 N: 1, 166 ScriptPubKey: bchain.ScriptPubKey{ 167 Hex: "76a9142f86cdfa98cac89143cf9e3d309cc072caccdf6f88ac", 168 Addresses: []string{ 169 "MCETUqM7MH6NietcsPY3w2sVUKz255m1yY", 170 }, 171 }, 172 }, 173 }, 174 } 175 } 176 177 func Test_PackTx(t *testing.T) { 178 type args struct { 179 tx bchain.Tx 180 height uint32 181 blockTime int64 182 parser *MyriadParser 183 } 184 tests := []struct { 185 name string 186 args args 187 want string 188 wantErr bool 189 }{ 190 { 191 name: "myriad-1", 192 args: args{ 193 tx: testTx1, 194 height: 20000, 195 blockTime: 1393723468, 196 parser: NewMyriadParser(GetChainParams("main"), &btc.Configuration{}), 197 }, 198 want: testTxPacked1, 199 wantErr: false, 200 }, 201 } 202 for _, tt := range tests { 203 t.Run(tt.name, func(t *testing.T) { 204 got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime) 205 if (err != nil) != tt.wantErr { 206 t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr) 207 return 208 } 209 h := hex.EncodeToString(got) 210 if !reflect.DeepEqual(h, tt.want) { 211 t.Errorf("packTx() = %v, want %v", h, tt.want) 212 } 213 }) 214 } 215 } 216 217 func Test_UnpackTx(t *testing.T) { 218 type args struct { 219 packedTx string 220 parser *MyriadParser 221 } 222 tests := []struct { 223 name string 224 args args 225 want *bchain.Tx 226 want1 uint32 227 wantErr bool 228 }{ 229 { 230 name: "myriad-1", 231 args: args{ 232 packedTx: testTxPacked1, 233 parser: NewMyriadParser(GetChainParams("main"), &btc.Configuration{}), 234 }, 235 want: &testTx1, 236 want1: 20000, 237 wantErr: false, 238 }, 239 } 240 for _, tt := range tests { 241 t.Run(tt.name, func(t *testing.T) { 242 b, _ := hex.DecodeString(tt.args.packedTx) 243 got, got1, err := tt.args.parser.UnpackTx(b) 244 if (err != nil) != tt.wantErr { 245 t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr) 246 return 247 } 248 if !reflect.DeepEqual(got, tt.want) { 249 t.Errorf("unpackTx() got = %v, want %v", got, tt.want) 250 } 251 if got1 != tt.want1 { 252 t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1) 253 } 254 }) 255 } 256 }