github.com/trezor/blockbook@v0.4.1-0.20240328132726-e9a08582ee2c/bchain/coins/grs/grsparser.go (about) 1 package grs 2 3 import ( 4 "github.com/martinboehm/btcd/wire" 5 "github.com/martinboehm/btcutil/base58" 6 "github.com/martinboehm/btcutil/chaincfg" 7 "github.com/trezor/blockbook/bchain" 8 "github.com/trezor/blockbook/bchain/coins/btc" 9 ) 10 11 // magic numbers 12 const ( 13 MainnetMagic wire.BitcoinNet = 0xd4b4bef9 14 TestnetMagic wire.BitcoinNet = 0x0709110b 15 RegtestMagic wire.BitcoinNet = 0xdab5bffa 16 SignetMagic wire.BitcoinNet = 0x7696b422 17 ) 18 19 // chain parameters 20 var ( 21 MainNetParams chaincfg.Params 22 TestNetParams chaincfg.Params 23 RegTestParams chaincfg.Params 24 SigNetParams chaincfg.Params 25 ) 26 27 func init() { 28 MainNetParams = chaincfg.MainNetParams 29 MainNetParams.Net = MainnetMagic 30 31 // Address encoding magics 32 MainNetParams.PubKeyHashAddrID = []byte{36} 33 MainNetParams.ScriptHashAddrID = []byte{5} 34 MainNetParams.Bech32HRPSegwit = "grs" 35 MainNetParams.Base58CksumHasher = base58.Groestl512D 36 37 TestNetParams = chaincfg.TestNet3Params 38 TestNetParams.Net = TestnetMagic 39 40 // Address encoding magics 41 TestNetParams.PubKeyHashAddrID = []byte{111} 42 TestNetParams.ScriptHashAddrID = []byte{196} 43 TestNetParams.Bech32HRPSegwit = "tgrs" 44 TestNetParams.Base58CksumHasher = base58.Groestl512D 45 46 RegTestParams = chaincfg.RegressionNetParams 47 RegTestParams.Net = RegtestMagic 48 49 // Address encoding magics 50 RegTestParams.PubKeyHashAddrID = []byte{111} 51 RegTestParams.ScriptHashAddrID = []byte{196} 52 RegTestParams.Bech32HRPSegwit = "grsrt" 53 RegTestParams.Base58CksumHasher = base58.Groestl512D 54 55 SigNetParams = chaincfg.SigNetParams 56 SigNetParams.Net = SignetMagic 57 58 // Address encoding magics 59 SigNetParams.PubKeyHashAddrID = []byte{111} 60 SigNetParams.ScriptHashAddrID = []byte{196} 61 SigNetParams.Bech32HRPSegwit = "tgrs" 62 SigNetParams.Base58CksumHasher = base58.Groestl512D 63 } 64 65 // GroestlcoinParser handle 66 type GroestlcoinParser struct { 67 *btc.BitcoinParser 68 baseparser *bchain.BaseParser 69 } 70 71 // NewGroestlcoinParser returns new GroestlcoinParser instance 72 func NewGroestlcoinParser(params *chaincfg.Params, c *btc.Configuration) *GroestlcoinParser { 73 return &GroestlcoinParser{ 74 BitcoinParser: btc.NewBitcoinParser(params, c), 75 baseparser: &bchain.BaseParser{}, 76 } 77 } 78 79 // GetChainParams contains network parameters for the main Groestlcoin network, 80 // the regression test Groestlcoin network, the test Groestlcoin network and 81 // the simulation test Groestlcoin network, in this order 82 func GetChainParams(chain string) *chaincfg.Params { 83 if !chaincfg.IsRegistered(&MainNetParams) { 84 err := chaincfg.Register(&MainNetParams) 85 if err == nil { 86 err = chaincfg.Register(&TestNetParams) 87 } 88 if err != nil { 89 panic(err) 90 } 91 } 92 switch chain { 93 case "test": 94 return &TestNetParams 95 case "regtest": 96 return &RegTestParams 97 case "signet": 98 return &SigNetParams 99 default: 100 return &MainNetParams 101 } 102 } 103 104 // PackTx packs transaction to byte array using protobuf 105 func (p *GroestlcoinParser) PackTx(tx *bchain.Tx, height uint32, blockTime int64) ([]byte, error) { 106 return p.baseparser.PackTx(tx, height, blockTime) 107 } 108 109 // UnpackTx unpacks transaction from protobuf byte array 110 func (p *GroestlcoinParser) UnpackTx(buf []byte) (*bchain.Tx, uint32, error) { 111 return p.baseparser.UnpackTx(buf) 112 }