55
66namespace NBitcoin
77{
8+ public record struct SignetSettings ( string Name , string Challenge , string GenesisBlock , string [ ] NetworkSeeds )
9+ {
10+ public const string DEFAULT_SIGNET_NAME = "signet" ;
11+
12+ public const string DEFAULT_SIGNET_CHALLENGE =
13+ "512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be430210359ef5021964fe22d6f8e05b2463c9540ce96883fe3b278760f048f5189f2e6c452ae" ;
14+
15+ public const string DEFAULT_SIGNET_GENESIS_BLOCK = "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a008f4d5fae77031e8ad222030101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000" ;
16+
17+ public static readonly string [ ] DEFAULT_SIGNET_NETWORK_SEEDS = new [ ]
18+ {
19+ "178.128.221.177" ,
20+ "2a01:7c8:d005:390::5"
21+ } ;
22+
23+ public static readonly SignetSettings Default =
24+ new ( DEFAULT_SIGNET_NAME , DEFAULT_SIGNET_CHALLENGE , DEFAULT_SIGNET_GENESIS_BLOCK , DEFAULT_SIGNET_NETWORK_SEEDS ) ;
25+ }
26+
827 public partial class Bitcoin
928 {
10- static readonly ChainName SignetName = new ChainName ( "Signet" ) ;
29+ static readonly ChainName SignetName = new ChainName ( SignetSettings . DEFAULT_SIGNET_NAME ) ;
1130
1231 public Network Signet => _Networks [ SignetName ] ;
1332
14- private Network CreateSignet ( )
33+ private Network CreateSignet (
34+ string name = SignetSettings . DEFAULT_SIGNET_NAME ,
35+ string challenge = SignetSettings . DEFAULT_SIGNET_CHALLENGE ,
36+ string genesisBlock = SignetSettings . DEFAULT_SIGNET_GENESIS_BLOCK ,
37+ string [ ] networkSeeds = null )
1538 {
39+ networkSeeds ??= SignetSettings . DEFAULT_SIGNET_NETWORK_SEEDS ;
1640 NetworkBuilder builder = new NetworkBuilder ( ) ;
17- builder . SetChainName ( SignetName ) ;
41+ builder . SetChainName ( name == SignetSettings . DEFAULT_SIGNET_NAME ? SignetName : new ChainName ( name ) ) ;
1842 builder . SetNetworkSet ( this ) ;
1943 builder . SetConsensus ( new Consensus ( )
2044 {
@@ -43,39 +67,45 @@ private Network CreateSignet()
4367 . SetBech32 ( Bech32Type . WITNESS_PUBKEY_ADDRESS , "tb" )
4468 . SetBech32 ( Bech32Type . WITNESS_SCRIPT_ADDRESS , "tb" )
4569 . SetBech32 ( Bech32Type . TAPROOT_ADDRESS , "tb" )
46- . SetMagic ( GetSignetMagic ( ) )
70+ . SetMagic ( GetSignetMagic ( challenge ) )
4771 . SetPort ( 38333 )
4872 . SetRPCPort ( 38332 )
49- . SetName ( "signet" )
50- . AddAlias ( "bitcoin-signet" )
51- . AddAlias ( "btc-signet" )
73+ . SetName ( name )
74+ . SetGenesis ( genesisBlock ) ;
75+
76+ if ( name == SignetSettings . DEFAULT_SIGNET_NAME )
77+ {
78+ builder . AddAlias ( "bitcoin-signet" ) ;
79+ builder . AddAlias ( "btc-signet" ) ;
80+ }
81+
5282#if ! NOSOCKET
53- . AddSeeds ( new [ ]
54- {
55- "178.128.221.177" ,
56- "2a01:7c8:d005:390::5"
57- } . Select ( o => new Protocol . NetworkAddress ( System . Net . IPAddress . Parse ( o ) ) ) )
83+ if ( networkSeeds != null )
84+ {
85+ builder . AddSeeds ( networkSeeds . Select ( o => new Protocol . NetworkAddress ( System . Net . IPAddress . Parse ( o ) ) ) ) ;
86+ }
87+ else if ( name == SignetSettings . DEFAULT_SIGNET_NAME )
88+ {
89+ builder . AddSeeds ( SignetSettings . DEFAULT_SIGNET_NETWORK_SEEDS . Select ( o => new Protocol . NetworkAddress ( System . Net . IPAddress . Parse ( o ) ) ) ) ;
90+ }
5891#endif
59- . SetGenesis (
60- "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a008f4d5fae77031e8ad222030101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000" ) ;
6192
6293 var network = builder . BuildAndRegister ( ) ;
6394#if ! NOFILEIO
6495 var data = Network . GetDefaultDataFolder ( "bitcoin" ) ;
6596 if ( data != null )
6697 {
67- var signetCookie = Path . Combine ( data , "signet" , ".cookie" ) ;
98+ var signetCookie = Path . Combine ( data , name , ".cookie" ) ;
6899 RPC . RPCClient . RegisterDefaultCookiePath ( network , signetCookie ) ;
69100 }
70101#endif
71102 _Networks . TryAdd ( SignetName , network ) ;
72103 return network ;
73104 }
74105
75- private static uint GetSignetMagic ( )
106+ private static uint GetSignetMagic ( string challengeHex )
76107 {
77- var challengeBytes = DataEncoders . Encoders . Hex . DecodeData (
78- "512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be430210359ef5021964fe22d6f8e05b2463c9540ce96883fe3b278760f048f5189f2e6c452ae" ) ;
108+ var challengeBytes = DataEncoders . Encoders . Hex . DecodeData ( challengeHex ) ;
79109 var challenge = new Script ( challengeBytes ) ;
80110 MemoryStream ms = new MemoryStream ( ) ;
81111 BitcoinStream bitcoinStream = new BitcoinStream ( ms , true ) ;
@@ -84,5 +114,16 @@ private static uint GetSignetMagic()
84114 return Utils . ToUInt32 ( h , true ) ;
85115 }
86116
117+ public Network InitCustomSignet ( SignetSettings ? settings = null )
118+ {
119+ var s = settings ?? SignetSettings . Default ;
120+
121+ if ( _Networks . TryGetValue ( new ChainName ( string . IsNullOrWhiteSpace ( s . Name ) ? SignetSettings . DEFAULT_SIGNET_NAME : s . Name ) , out var network ) )
122+ return network ;
123+
124+ var customSignet = CreateSignet ( s . Name , s . Challenge , s . GenesisBlock , s . NetworkSeeds ) ;
125+ _Networks . TryAdd ( customSignet . ChainName , customSignet ) ;
126+ return customSignet ;
127+ }
87128 }
88129}
0 commit comments