github.com/bchainhub/blockbook@v0.3.2/bchain/coins/fujicoin/fujicoinparser_test.go (about) 1 // +build unittest 2 3 package fujicoin 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: "pubkeyhash1", 35 args: args{address: "FgKfZA7QB5xHGTtpPqTF1RQRyHCCTr5NXg"}, 36 want: "76a9147a5ab68256ac6bd8dc6d78b6b94af69e414dffd588ac", 37 wantErr: false, 38 }, 39 { 40 name: "pubkeyhash2", 41 args: args{address: "FsjXKBTE9nQinJnc1r2iLEb8SX7ok1eC7V"}, 42 want: "76a914f787334ddbda3e65fe7d579f03842b5607cf0db888ac", 43 wantErr: false, 44 }, 45 { 46 name: "scripthash1", 47 args: args{address: "7ojX5QT534BBDPwNoYXjx1MdbHWS2ueMk3"}, 48 want: "a914e9ec22a509631fa3c8c09ebb1b41f59c81ec669f87", 49 wantErr: false, 50 }, 51 { 52 name: "scripthash2", 53 args: args{address: "7XJah1ejjogvQS7UkfiH577R1yzkVzNBn4"}, 54 want: "a91435b2bd909a0b9b6ca35fef9276675f568f42a4e387", 55 wantErr: false, 56 }, 57 { 58 name: "witness_v0_keyhash", 59 args: args{address: "fc1q2ze0qtna2a42qqam3k9xsu3xamrrcp8rsczdve"}, 60 want: "001450b2f02e7d576aa003bb8d8a687226eec63c04e3", 61 wantErr: false, 62 }, 63 } 64 parser := NewFujicoinParser(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 = "0023f3648bc2d3dd5c0100000001ec5666d4e842926c6e872fd7f12a87183b298464716dec1b03fc956777e06a70010000006a473044022062949ffe184af6bdc8d77e07c2caf1bd4e36d977bc27e24ab6d24c9e735d76b802201f42950f6fa005126f1fbd5cb9d6f0838d94adb70a05517b3eebd144a0ec2fd6012102534f95a0a13bbc3e261f966edd057a1dc9819a4e6bb21d81a633d5500d184a7bfeffffff0200e876481700000017a9146b8a69f8b43bc06b886c0668c93197be5152e8598740494d552e0000001976a914df783f980b38d7a64cee23202af95609b547fde888ac62f32300" 85 ) 86 87 func init() { 88 testTx1 = bchain.Tx{ 89 Hex: "0100000001ec5666d4e842926c6e872fd7f12a87183b298464716dec1b03fc956777e06a70010000006a473044022062949ffe184af6bdc8d77e07c2caf1bd4e36d977bc27e24ab6d24c9e735d76b802201f42950f6fa005126f1fbd5cb9d6f0838d94adb70a05517b3eebd144a0ec2fd6012102534f95a0a13bbc3e261f966edd057a1dc9819a4e6bb21d81a633d5500d184a7bfeffffff0200e876481700000017a9146b8a69f8b43bc06b886c0668c93197be5152e8598740494d552e0000001976a914df783f980b38d7a64cee23202af95609b547fde888ac62f32300", 90 Blocktime: 1546286958, 91 Txid: "7367db1a3073146f0b6060ce4a6dbb96b67b7aadcbaa450bff8d7dedda52ec13", 92 LockTime: 2356066, 93 Version: 1, 94 Vin: []bchain.Vin{ 95 { 96 ScriptSig: bchain.ScriptSig{ 97 Hex: "473044022062949ffe184af6bdc8d77e07c2caf1bd4e36d977bc27e24ab6d24c9e735d76b802201f42950f6fa005126f1fbd5cb9d6f0838d94adb70a05517b3eebd144a0ec2fd6012102534f95a0a13bbc3e261f966edd057a1dc9819a4e6bb21d81a633d5500d184a7b", 98 }, 99 Txid: "706ae0776795fc031bec6d716484293b18872af1d72f876e6c9242e8d46656ec", 100 Vout: 1, 101 Sequence: 4294967294, 102 }, 103 }, 104 Vout: []bchain.Vout{ 105 { 106 ValueSat: *big.NewInt(100000000000), 107 N: 0, 108 ScriptPubKey: bchain.ScriptPubKey{ 109 Hex: "a9146b8a69f8b43bc06b886c0668c93197be5152e85987", 110 Addresses: []string{ 111 "7cDGsS8Dum191ZMD19UaLzstMtf29YMXBL", 112 }, 113 }, 114 }, 115 { 116 ValueSat: *big.NewInt(198999624000), 117 N: 1, 118 ScriptPubKey: bchain.ScriptPubKey{ 119 Hex: "76a914df783f980b38d7a64cee23202af95609b547fde888ac", 120 Addresses: []string{ 121 "FqYKBhEF4zKkDGkVyufGVGajhttuD4Usc2", 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 *FujicoinParser 135 } 136 tests := []struct { 137 name string 138 args args 139 want string 140 wantErr bool 141 }{ 142 { 143 name: "fujicoin-1", 144 args: args{ 145 tx: testTx1, 146 height: 2356068, 147 blockTime: 1546286958, 148 parser: NewFujicoinParser(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 *FujicoinParser 173 } 174 tests := []struct { 175 name string 176 args args 177 want *bchain.Tx 178 want1 uint32 179 wantErr bool 180 }{ 181 { 182 name: "fujicoin-1", 183 args: args{ 184 packedTx: testTxPacked1, 185 parser: NewFujicoinParser(GetChainParams("main"), &btc.Configuration{}), 186 }, 187 want: &testTx1, 188 want1: 2356068, 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 }