76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
package bch
|
|
|
|
import (
|
|
"blockbook/bchain/coins/btc"
|
|
"strings"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
"github.com/btcsuite/btcd/txscript"
|
|
"github.com/btcsuite/btcutil"
|
|
"github.com/cpacia/bchutil"
|
|
)
|
|
|
|
var prefixes []string
|
|
|
|
func init() {
|
|
prefixes = make([]string, 0, len(bchutil.Prefixes))
|
|
for _, prefix := range bchutil.Prefixes {
|
|
prefixes = append(prefixes, prefix)
|
|
}
|
|
}
|
|
|
|
// BCashParser handle
|
|
type BCashParser struct {
|
|
*btc.BitcoinParser
|
|
}
|
|
|
|
// GetChainParams contains network parameters for the main Bitcoin Cash network,
|
|
// the regression test Bitcoin Cash network, the test Bitcoin Cash network and
|
|
// the simulation test Bitcoin Cash network, in this order
|
|
func GetChainParams(chain string) *chaincfg.Params {
|
|
var params *chaincfg.Params
|
|
switch chain {
|
|
case "test":
|
|
params = &chaincfg.TestNet3Params
|
|
params.Net = bchutil.TestnetMagic
|
|
case "regtest":
|
|
params = &chaincfg.RegressionNetParams
|
|
params.Net = bchutil.Regtestmagic
|
|
default:
|
|
params = &chaincfg.MainNetParams
|
|
params.Net = bchutil.MainnetMagic
|
|
}
|
|
|
|
return params
|
|
}
|
|
|
|
// GetAddrIDFromAddress returns internal address representation of given address
|
|
func (p *BCashParser) GetAddrIDFromAddress(address string) ([]byte, error) {
|
|
return p.AddressToOutputScript(address)
|
|
}
|
|
|
|
// AddressToOutputScript converts bitcoin address to ScriptPubKey
|
|
func (p *BCashParser) AddressToOutputScript(address string) ([]byte, error) {
|
|
if strings.Contains(address, ":") {
|
|
da, err := bchutil.DecodeAddress(address, p.Params)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
script, err := bchutil.PayToAddrScript(da)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return script, nil
|
|
} else {
|
|
da, err := btcutil.DecodeAddress(address, p.Params)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
script, err := txscript.PayToAddrScript(da)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return script, nil
|
|
}
|
|
}
|