decred.org/dcrdex@v1.0.3/dex/networks/bch/cashaddr_test.go (about) 1 // This code is available on the terms of the project LICENSE.md file, 2 // also available online at https://blueoakcouncil.org/license/1.0.0. 3 4 package bch 5 6 import ( 7 "testing" 8 9 "decred.org/dcrdex/dex/encode" 10 "github.com/btcsuite/btcd/btcec/v2" 11 "github.com/btcsuite/btcd/btcutil" 12 "github.com/btcsuite/btcd/chaincfg" 13 "github.com/gcash/bchutil" 14 ) 15 16 func TestCashAddr(t *testing.T) { 17 lowB := make([]byte, 20) 18 highB := make([]byte, 20) 19 for i := range highB { 20 highB[i] = 255 21 } 22 23 lowPubKey := make([]byte, 33) 24 lowPubKey[0] = 2 25 lowPubKey[32] = 1 26 27 checkHash := func(net *chaincfg.Params, h []byte) { 28 t.Helper() 29 30 switch len(h) { 31 case 20: 32 var bchAddr bchutil.Address 33 bchAddr, err := bchutil.NewAddressPubKeyHash(h, convertParams(net)) 34 if err != nil { 35 t.Fatalf("bchutil.AddressScriptHash error: %v", err) 36 } 37 testRoundTripFromBCH(t, withPrefix(bchAddr, net), net) 38 39 bchAddr, err = bchutil.NewAddressScriptHashFromHash(h, convertParams(net)) 40 if err != nil { 41 t.Fatalf("bchutil.AddressScriptHash error: %v", err) 42 } 43 testRoundTripFromBCH(t, withPrefix(bchAddr, net), net) 44 45 var btcAddr btcutil.Address 46 btcAddr, err = btcutil.NewAddressPubKeyHash(h, net) 47 if err != nil { 48 t.Fatalf("btcutil.NewAddressPubkeyHash error: %v", err) 49 } 50 51 testRoundTripFromBTC(t, btcAddr.String(), net) 52 53 btcAddr, err = btcutil.NewAddressScriptHashFromHash(h, net) 54 if err != nil { 55 t.Fatalf("btcutil.NewAddressPubkeyHash error: %v", err) 56 } 57 testRoundTripFromBTC(t, btcAddr.String(), net) 58 59 case 33, 65: // See btcec.PubKeyBytesLen(Un)Compressed 60 var bchAddr bchutil.Address 61 bchAddr, err := bchutil.NewAddressPubKey(h, convertParams(net)) 62 if err != nil { 63 t.Fatalf("bchutil.NewAddressPubKey error: %v", err) 64 } 65 testRoundTripFromBCH(t, withPrefix(bchAddr, net), net) 66 67 var btcAddr btcutil.Address 68 btcAddr, err = btcutil.NewAddressPubKey(h, net) 69 if err != nil { 70 t.Fatalf("btcutil.NewAddressPubKey error: %v", err) 71 } 72 73 testRoundTripFromBTC(t, btcAddr.String(), net) 74 75 default: 76 t.Fatalf("unknown address data length %d", len(h)) 77 } 78 79 } 80 81 nets := []*chaincfg.Params{MainNetParams, TestNet4Params, RegressionNetParams} 82 for _, net := range nets { 83 // Check the lowest and highest possible hashes. 84 checkHash(net, lowB) 85 checkHash(net, highB) 86 // Check a bunch of random addresses. 87 for i := 0; i < 1000; i++ { 88 checkHash(net, encode.RandomBytes(20)) 89 } 90 // Check Pubkey addresses. These just encode to the hex encoding of the 91 // serialized pubkey for both bch and btc, so there's little that can go 92 // wrong. 93 checkHash(net, lowPubKey) 94 for i := 0; i < 100; i++ { 95 priv, err := btcec.NewPrivateKey() 96 if err != nil { 97 panic(err.Error()) 98 } 99 pubKey := priv.PubKey() 100 checkHash(net, pubKey.SerializeUncompressed()) 101 checkHash(net, pubKey.SerializeCompressed()) 102 } 103 104 } 105 } 106 107 func testRoundTripFromBCH(t *testing.T, bchAddrStr string, net *chaincfg.Params) { 108 t.Helper() 109 110 btcAddr, err := DecodeCashAddress(bchAddrStr, net) 111 if err != nil { 112 t.Fatalf("DecodeCashAddress error: %v", err) 113 } 114 115 reAddr, err := RecodeCashAddress(btcAddr.String(), net) 116 if err != nil { 117 t.Fatalf("RecodeCashAddr error: %v", err) 118 } 119 120 if reAddr != bchAddrStr { 121 t.Fatalf("Recoded address mismatch: %s != %s", reAddr, bchAddrStr) 122 } 123 } 124 125 func testRoundTripFromBTC(t *testing.T, btcAddrStr string, net *chaincfg.Params) { 126 t.Helper() 127 128 bchAddrStr, err := RecodeCashAddress(btcAddrStr, net) 129 if err != nil { 130 t.Fatalf("RecodeCashAddr error: %v", err) 131 } 132 133 btcAddr, err := DecodeCashAddress(bchAddrStr, net) 134 if err != nil { 135 t.Fatalf("DecodeCashAddress error: %v", err) 136 } 137 138 reAddr := btcAddr.String() 139 if reAddr != btcAddrStr { 140 t.Fatalf("Decoded address mismatch: %s != %s", reAddr, btcAddrStr) 141 } 142 }