multi: add signet params

With this commit we add the signet test network parameters to all switch
cases where we distinguish between the different networks.
This commit is contained in:
Oliver Gugger 2021-03-04 16:07:01 +01:00
parent c7c36be14b
commit 1f1ffb56b5
No known key found for this signature in database
GPG key ID: 8E4256593F177720
7 changed files with 99 additions and 17 deletions

View file

@ -484,6 +484,8 @@ func getCurrentNet(client *rpcclient.Client) (wire.BitcoinNet, error) {
return chaincfg.TestNet3Params.Net, nil return chaincfg.TestNet3Params.Net, nil
case *chaincfg.RegressionNetParams.GenesisHash: case *chaincfg.RegressionNetParams.GenesisHash:
return chaincfg.RegressionNetParams.Net, nil return chaincfg.RegressionNetParams.Net, nil
case *chaincfg.SigNetParams.GenesisHash:
return chaincfg.SigNetParams.Net, nil
case *chaincfg.MainNetParams.GenesisHash: case *chaincfg.MainNetParams.GenesisHash:
return chaincfg.MainNetParams.Net, nil return chaincfg.MainNetParams.Net, nil
default: default:

View file

@ -5,6 +5,7 @@
package main package main
import ( import (
"encoding/hex"
"fmt" "fmt"
"net" "net"
"os" "os"
@ -15,6 +16,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/internal/cfgutil" "github.com/btcsuite/btcwallet/internal/cfgutil"
"github.com/btcsuite/btcwallet/internal/legacy/keystore" "github.com/btcsuite/btcwallet/internal/legacy/keystore"
@ -52,6 +54,9 @@ type config struct {
AppDataDir *cfgutil.ExplicitString `short:"A" long:"appdata" description:"Application data directory for wallet config, databases and logs"` AppDataDir *cfgutil.ExplicitString `short:"A" long:"appdata" description:"Application data directory for wallet config, databases and logs"`
TestNet3 bool `long:"testnet" description:"Use the test Bitcoin network (version 3) (default mainnet)"` TestNet3 bool `long:"testnet" description:"Use the test Bitcoin network (version 3) (default mainnet)"`
SimNet bool `long:"simnet" description:"Use the simulation test network (default mainnet)"` SimNet bool `long:"simnet" description:"Use the simulation test network (default mainnet)"`
SigNet bool `long:"signet" description:"Use the signet test network (default mainnet)"`
SigNetChallenge string `long:"signetchallenge" description:"Connect to a custom signet network defined by this challenge instead of using the global default signet test network -- Can be specified multiple times"`
SigNetSeedNode []string `long:"signetseednode" description:"Specify a seed node for the signet network instead of using the global default signet network seed nodes"`
NoInitialLoad bool `long:"noinitialload" description:"Defer wallet creation/opening on startup and enable loading wallets over RPC"` NoInitialLoad bool `long:"noinitialload" description:"Defer wallet creation/opening on startup and enable loading wallets over RPC"`
DebugLevel string `short:"d" long:"debuglevel" description:"Logging level {trace, debug, info, warn, error, critical}"` DebugLevel string `short:"d" long:"debuglevel" description:"Logging level {trace, debug, info, warn, error, critical}"`
LogDir string `long:"logdir" description:"Directory to log output."` LogDir string `long:"logdir" description:"Directory to log output."`
@ -365,9 +370,48 @@ func loadConfig() (*config, []string, error) {
activeNet = &netparams.SimNetParams activeNet = &netparams.SimNetParams
numNets++ numNets++
} }
if cfg.SigNet {
activeNet = &netparams.SigNetParams
numNets++
// Let the user overwrite the default signet parameters. The
// challenge defines the actual signet network to join and the
// seed nodes are needed for network discovery.
sigNetChallenge := chaincfg.DefaultSignetChallenge
sigNetSeeds := chaincfg.DefaultSignetDNSSeeds
if cfg.SigNetChallenge != "" {
challenge, err := hex.DecodeString(cfg.SigNetChallenge)
if err != nil {
str := "%s: Invalid signet challenge, hex " +
"decode failed: %v"
err := fmt.Errorf(str, funcName, err)
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err
}
sigNetChallenge = challenge
}
if len(cfg.SigNetSeedNode) > 0 {
sigNetSeeds = make(
[]chaincfg.DNSSeed, len(cfg.SigNetSeedNode),
)
for idx, seed := range cfg.SigNetSeedNode {
sigNetSeeds[idx] = chaincfg.DNSSeed{
Host: seed,
HasFiltering: false,
}
}
}
chainParams := chaincfg.CustomSignetParams(
sigNetChallenge, sigNetSeeds,
)
activeNet.Params = &chainParams
}
if numNets > 1 { if numNets > 1 {
str := "%s: The testnet and simnet params can't be used " + str := "%s: The testnet, signet and simnet params can't be " +
"together -- choose one" "used together -- choose one"
err := fmt.Errorf(str, "loadConfig") err := fmt.Errorf(str, "loadConfig")
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr) parser.WriteHelp(os.Stderr)

View file

@ -476,6 +476,11 @@ func (net *netParams) ReadFrom(r io.Reader) (int64, error) {
*net = (netParams)(chaincfg.TestNet3Params) *net = (netParams)(chaincfg.TestNet3Params)
case wire.SimNet: case wire.SimNet:
*net = (netParams)(chaincfg.SimNetParams) *net = (netParams)(chaincfg.SimNetParams)
// The legacy key store won't be compatible with custom signets, only
// the main public one.
case chaincfg.SigNetParams.Net:
*net = (netParams)(chaincfg.SigNetParams)
default: default:
return n64, errors.New("unknown network") return n64, errors.New("unknown network")
} }

View file

@ -4,7 +4,10 @@
package netparams package netparams
import "github.com/btcsuite/btcd/chaincfg" import (
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire"
)
// Params is used to group parameters for various networks such as the main // Params is used to group parameters for various networks such as the main
// network and test networks. // network and test networks.
@ -37,3 +40,23 @@ var SimNetParams = Params{
RPCClientPort: "18556", RPCClientPort: "18556",
RPCServerPort: "18554", RPCServerPort: "18554",
} }
// SigNetParams contains parameters specific to the signet test network
// (wire.SigNet).
var SigNetParams = Params{
Params: &chaincfg.SigNetParams,
RPCClientPort: "38334",
RPCServerPort: "38332",
}
// SigNetWire is a helper function that either returns the given chain
// parameter's net value if the parameter represents a signet network or 0 if
// it's not. This is necessary because there can be custom signet networks that
// have a different net value.
func SigNetWire(params *chaincfg.Params) wire.BitcoinNet {
if params.Name == chaincfg.SigNetParams.Name {
return params.Net
}
return 0
}

View file

@ -322,6 +322,10 @@ func populateBirthdayBlock(ns walletdb.ReadWriteBucket) error {
genesisTimestamp = genesisTimestamp =
chaincfg.SimNetParams.GenesisBlock.Header.Timestamp chaincfg.SimNetParams.GenesisBlock.Header.Timestamp
case *chaincfg.SigNetParams.GenesisHash:
genesisTimestamp =
chaincfg.SigNetParams.GenesisBlock.Header.Timestamp
default: default:
return fmt.Errorf("unknown genesis hash %v", genesisHash) return fmt.Errorf("unknown genesis hash %v", genesisHash)
} }

View file

@ -12,6 +12,7 @@ import (
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/hdkeychain" "github.com/btcsuite/btcutil/hdkeychain"
"github.com/btcsuite/btcwallet/internal/zero" "github.com/btcsuite/btcwallet/internal/zero"
"github.com/btcsuite/btcwallet/netparams"
"github.com/btcsuite/btcwallet/walletdb" "github.com/btcsuite/btcwallet/walletdb"
) )
@ -2231,7 +2232,9 @@ func (s *ScopedKeyManager) cloneKeyWithVersion(key *hdkeychain.ExtendedKey) (
return nil, fmt.Errorf("unsupported scope %v", s.scope) return nil, fmt.Errorf("unsupported scope %v", s.scope)
} }
case wire.TestNet, wire.TestNet3: case wire.TestNet, wire.TestNet3,
netparams.SigNetWire(s.rootManager.ChainParams()):
switch s.scope { switch s.scope {
case KeyScopeBIP0044: case KeyScopeBIP0044:
version = HDVersionTestNetBIP0044 version = HDVersionTestNetBIP0044

View file

@ -9,6 +9,7 @@ import (
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/hdkeychain" "github.com/btcsuite/btcutil/hdkeychain"
"github.com/btcsuite/btcwallet/netparams"
"github.com/btcsuite/btcwallet/waddrmgr" "github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/walletdb" "github.com/btcsuite/btcwallet/walletdb"
) )
@ -108,7 +109,7 @@ func (w *Wallet) isPubKeyForNet(pubKey *hdkeychain.ExtendedKey) bool {
version == waddrmgr.HDVersionMainNetBIP0049 || version == waddrmgr.HDVersionMainNetBIP0049 ||
version == waddrmgr.HDVersionMainNetBIP0084 version == waddrmgr.HDVersionMainNetBIP0084
case wire.TestNet, wire.TestNet3: case wire.TestNet, wire.TestNet3, netparams.SigNetWire(w.chainParams):
return version == waddrmgr.HDVersionTestNetBIP0044 || return version == waddrmgr.HDVersionTestNetBIP0044 ||
version == waddrmgr.HDVersionTestNetBIP0049 || version == waddrmgr.HDVersionTestNetBIP0049 ||
version == waddrmgr.HDVersionTestNetBIP0084 version == waddrmgr.HDVersionTestNetBIP0084