From 1f1ffb56b5fea0801237928ed1407aa90d872234 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 4 Mar 2021 16:07:01 +0100 Subject: [PATCH] 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. --- chain/bitcoind_conn.go | 2 + config.go | 72 ++++++++++++++++++++++------ internal/legacy/keystore/keystore.go | 5 ++ netparams/params.go | 25 +++++++++- waddrmgr/migrations.go | 4 ++ waddrmgr/scoped_manager.go | 5 +- wallet/import.go | 3 +- 7 files changed, 99 insertions(+), 17 deletions(-) diff --git a/chain/bitcoind_conn.go b/chain/bitcoind_conn.go index 71474c5..08ecc17 100644 --- a/chain/bitcoind_conn.go +++ b/chain/bitcoind_conn.go @@ -484,6 +484,8 @@ func getCurrentNet(client *rpcclient.Client) (wire.BitcoinNet, error) { return chaincfg.TestNet3Params.Net, nil case *chaincfg.RegressionNetParams.GenesisHash: return chaincfg.RegressionNetParams.Net, nil + case *chaincfg.SigNetParams.GenesisHash: + return chaincfg.SigNetParams.Net, nil case *chaincfg.MainNetParams.GenesisHash: return chaincfg.MainNetParams.Net, nil default: diff --git a/config.go b/config.go index eccf4b7..6a8263f 100644 --- a/config.go +++ b/config.go @@ -5,6 +5,7 @@ package main import ( + "encoding/hex" "fmt" "net" "os" @@ -15,6 +16,7 @@ import ( "strings" "time" + "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcutil" "github.com/btcsuite/btcwallet/internal/cfgutil" "github.com/btcsuite/btcwallet/internal/legacy/keystore" @@ -45,18 +47,21 @@ var ( type config struct { // General application behavior - ConfigFile *cfgutil.ExplicitString `short:"C" long:"configfile" description:"Path to configuration file"` - ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"` - Create bool `long:"create" description:"Create the wallet if it does not exist"` - CreateTemp bool `long:"createtemp" description:"Create a temporary simulation wallet (pass=password) in the data directory indicated; must call with --datadir"` - 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)"` - SimNet bool `long:"simnet" description:"Use the simulation test network (default mainnet)"` - 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}"` - LogDir string `long:"logdir" description:"Directory to log output."` - Profile string `long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65536"` - DBTimeout time.Duration `long:"dbtimeout" description:"The timeout value to use when opening the wallet database."` + ConfigFile *cfgutil.ExplicitString `short:"C" long:"configfile" description:"Path to configuration file"` + ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"` + Create bool `long:"create" description:"Create the wallet if it does not exist"` + CreateTemp bool `long:"createtemp" description:"Create a temporary simulation wallet (pass=password) in the data directory indicated; must call with --datadir"` + 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)"` + 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"` + DebugLevel string `short:"d" long:"debuglevel" description:"Logging level {trace, debug, info, warn, error, critical}"` + LogDir string `long:"logdir" description:"Directory to log output."` + Profile string `long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65536"` + DBTimeout time.Duration `long:"dbtimeout" description:"The timeout value to use when opening the wallet database."` // Wallet options WalletPass string `long:"walletpass" default-mask:"-" description:"The public wallet password -- Only required if the wallet was created with one"` @@ -365,9 +370,48 @@ func loadConfig() (*config, []string, error) { activeNet = &netparams.SimNetParams 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 { - str := "%s: The testnet and simnet params can't be used " + - "together -- choose one" + str := "%s: The testnet, signet and simnet params can't be " + + "used together -- choose one" err := fmt.Errorf(str, "loadConfig") fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) diff --git a/internal/legacy/keystore/keystore.go b/internal/legacy/keystore/keystore.go index c1430e3..2a58f0b 100644 --- a/internal/legacy/keystore/keystore.go +++ b/internal/legacy/keystore/keystore.go @@ -476,6 +476,11 @@ func (net *netParams) ReadFrom(r io.Reader) (int64, error) { *net = (netParams)(chaincfg.TestNet3Params) case wire.SimNet: *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: return n64, errors.New("unknown network") } diff --git a/netparams/params.go b/netparams/params.go index c15047e..f54781e 100644 --- a/netparams/params.go +++ b/netparams/params.go @@ -4,7 +4,10 @@ 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 // network and test networks. @@ -37,3 +40,23 @@ var SimNetParams = Params{ RPCClientPort: "18556", 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 +} diff --git a/waddrmgr/migrations.go b/waddrmgr/migrations.go index 77e0080..5ed0072 100644 --- a/waddrmgr/migrations.go +++ b/waddrmgr/migrations.go @@ -322,6 +322,10 @@ func populateBirthdayBlock(ns walletdb.ReadWriteBucket) error { genesisTimestamp = chaincfg.SimNetParams.GenesisBlock.Header.Timestamp + case *chaincfg.SigNetParams.GenesisHash: + genesisTimestamp = + chaincfg.SigNetParams.GenesisBlock.Header.Timestamp + default: return fmt.Errorf("unknown genesis hash %v", genesisHash) } diff --git a/waddrmgr/scoped_manager.go b/waddrmgr/scoped_manager.go index ee010f6..327be2f 100644 --- a/waddrmgr/scoped_manager.go +++ b/waddrmgr/scoped_manager.go @@ -12,6 +12,7 @@ import ( "github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil/hdkeychain" "github.com/btcsuite/btcwallet/internal/zero" + "github.com/btcsuite/btcwallet/netparams" "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) } - case wire.TestNet, wire.TestNet3: + case wire.TestNet, wire.TestNet3, + netparams.SigNetWire(s.rootManager.ChainParams()): + switch s.scope { case KeyScopeBIP0044: version = HDVersionTestNetBIP0044 diff --git a/wallet/import.go b/wallet/import.go index 42bfe1c..b1b699a 100644 --- a/wallet/import.go +++ b/wallet/import.go @@ -9,6 +9,7 @@ import ( "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil/hdkeychain" + "github.com/btcsuite/btcwallet/netparams" "github.com/btcsuite/btcwallet/waddrmgr" "github.com/btcsuite/btcwallet/walletdb" ) @@ -108,7 +109,7 @@ func (w *Wallet) isPubKeyForNet(pubKey *hdkeychain.ExtendedKey) bool { version == waddrmgr.HDVersionMainNetBIP0049 || version == waddrmgr.HDVersionMainNetBIP0084 - case wire.TestNet, wire.TestNet3: + case wire.TestNet, wire.TestNet3, netparams.SigNetWire(w.chainParams): return version == waddrmgr.HDVersionTestNetBIP0044 || version == waddrmgr.HDVersionTestNetBIP0049 || version == waddrmgr.HDVersionTestNetBIP0084