Skip to content

Commit fd711ce

Browse files
guggerokcalvinalvin
authored andcommitted
wire+chaincfg: add signet params
This commit adds all necessary chain parameters for connecting to the public signet network. Reference: bitcoin/bitcoin#18267
1 parent 2e57ff8 commit fd711ce

File tree

4 files changed

+277
-0
lines changed

4 files changed

+277
-0
lines changed

chaincfg/genesis.go

+28
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,31 @@ var simNetGenesisBlock = wire.MsgBlock{
170170
},
171171
Transactions: []*wire.MsgTx{&genesisCoinbaseTx},
172172
}
173+
174+
// sigNetGenesisHash is the hash of the first block in the block chain for the
175+
// signet test network.
176+
var sigNetGenesisHash = chainhash.Hash{
177+
0xf6, 0x1e, 0xee, 0x3b, 0x63, 0xa3, 0x80, 0xa4,
178+
0x77, 0xa0, 0x63, 0xaf, 0x32, 0xb2, 0xbb, 0xc9,
179+
0x7c, 0x9f, 0xf9, 0xf0, 0x1f, 0x2c, 0x42, 0x25,
180+
0xe9, 0x73, 0x98, 0x81, 0x08, 0x00, 0x00, 0x00,
181+
}
182+
183+
// sigNetGenesisMerkleRoot is the hash of the first transaction in the genesis
184+
// block for the signet test network. It is the same as the merkle root for
185+
// the main network.
186+
var sigNetGenesisMerkleRoot = genesisMerkleRoot
187+
188+
// sigNetGenesisBlock defines the genesis block of the block chain which serves
189+
// as the public transaction ledger for the signet test network.
190+
var sigNetGenesisBlock = wire.MsgBlock{
191+
Header: wire.BlockHeader{
192+
Version: 1,
193+
PrevBlock: chainhash.Hash{}, // 0000000000000000000000000000000000000000000000000000000000000000
194+
MerkleRoot: sigNetGenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
195+
Timestamp: time.Unix(1598918400, 0), // 2020-09-01 00:00:00 +0000 UTC
196+
Bits: 0x1e0377ae, // 503543726 [00000377ae000000000000000000000000000000000000000000000000000000]
197+
Nonce: 52613770,
198+
},
199+
Transactions: []*wire.MsgTx{&genesisCoinbaseTx},
200+
}

chaincfg/genesis_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,33 @@ func TestSimNetGenesisBlock(t *testing.T) {
118118
}
119119
}
120120

121+
// TestSigNetGenesisBlock tests the genesis block of the signet test network for
122+
// validity by checking the encoded bytes and hashes.
123+
func TestSigNetGenesisBlock(t *testing.T) {
124+
// Encode the genesis block to raw bytes.
125+
var buf bytes.Buffer
126+
err := SigNetParams.GenesisBlock.Serialize(&buf)
127+
if err != nil {
128+
t.Fatalf("TestSigNetGenesisBlock: %v", err)
129+
}
130+
131+
// Ensure the encoded block matches the expected bytes.
132+
if !bytes.Equal(buf.Bytes(), sigNetGenesisBlockBytes) {
133+
t.Fatalf("TestSigNetGenesisBlock: Genesis block does not "+
134+
"appear valid - got %v, want %v",
135+
spew.Sdump(buf.Bytes()),
136+
spew.Sdump(sigNetGenesisBlockBytes))
137+
}
138+
139+
// Check hash of the block against expected hash.
140+
hash := SigNetParams.GenesisBlock.BlockHash()
141+
if !SigNetParams.GenesisHash.IsEqual(&hash) {
142+
t.Fatalf("TestSigNetGenesisBlock: Genesis block hash does "+
143+
"not appear valid - got %v, want %v", spew.Sdump(hash),
144+
spew.Sdump(SigNetParams.GenesisHash))
145+
}
146+
}
147+
121148
// genesisBlockBytes are the wire encoded bytes for the genesis block of the
122149
// main network as of protocol version 60002.
123150
var genesisBlockBytes = []byte{
@@ -281,3 +308,44 @@ var simNetGenesisBlockBytes = []byte{
281308
0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, /* |.Lp+k.._|*/
282309
0xac, 0x00, 0x00, 0x00, 0x00, /* |.....| */
283310
}
311+
312+
// sigNetGenesisBlockBytes are the wire encoded bytes for the genesis block of
313+
// the signet test network as of protocol version 70002.
314+
var sigNetGenesisBlockBytes = []byte{
315+
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |...@....| */
316+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */
317+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */
318+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */
319+
0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd, /* |........| */
320+
0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, /* |....;...| */
321+
0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, /* |z{..z.,>| */
322+
0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, /* |gv.a....| */
323+
0x4b, 0x1e, 0x5e, 0x4a, 0x00, 0x8f, 0x4d, 0x5f, /* |..Q2:...| */
324+
0xae, 0x77, 0x03, 0x1e, 0x8a, 0xd2, 0x22, 0x03, /* |K.^J..M_| */
325+
0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, /* |.w....".| */
326+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */
327+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */
328+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */
329+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, /* |........| */
330+
0xff, 0xff, 0x4d, 0x04, 0xff, 0xff, 0x00, 0x1d, /* |........| */
331+
0x01, 0x04, 0x45, 0x54, 0x68, 0x65, 0x20, 0x54, /* |..M.....| */
332+
0x69, 0x6d, 0x65, 0x73, 0x20, 0x30, 0x33, 0x2f, /* |..EThe T| */
333+
0x4a, 0x61, 0x6e, 0x2f, 0x32, 0x30, 0x30, 0x39, /* |imes 03/| */
334+
0x20, 0x43, 0x68, 0x61, 0x6e, 0x63, 0x65, 0x6c, /* |Jan/2009| */
335+
0x6c, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x20, 0x62, /* | Chancel| */
336+
0x72, 0x69, 0x6e, 0x6b, 0x20, 0x6f, 0x66, 0x20, /* |lor on b| */
337+
0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x62, /* |rink of| */
338+
0x61, 0x69, 0x6c, 0x6f, 0x75, 0x74, 0x20, 0x66, /* |second b| */
339+
0x6f, 0x72, 0x20, 0x62, 0x61, 0x6e, 0x6b, 0x73, /* |ailout f| */
340+
0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, /* |or banks| */
341+
0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, /* |........| */
342+
0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, /* |*....CA.| */
343+
0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, /* |g....UH'| */
344+
0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, /* |.g..q0..| */
345+
0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, /* |\..(.9..| */
346+
0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, /* |yb...a..| */
347+
0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, /* |I..?L.8.| */
348+
0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, /* |.U......| */
349+
0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, /* |\8M....W| */
350+
0xac, 0x00, 0x00, 0x00, 0x00, /* |.....| */
351+
}

chaincfg/params.go

+132
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package chaincfg
66

77
import (
8+
"encoding/binary"
9+
"encoding/hex"
810
"errors"
911
"math"
1012
"math/big"
@@ -38,6 +40,30 @@ var (
3840
// simNetPowLimit is the highest proof of work value a Bitcoin block
3941
// can have for the simulation test network. It is the value 2^255 - 1.
4042
simNetPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne)
43+
44+
// sigNetPowLimit is the highest proof of work value a bitcoin block can
45+
// have for the signet test network. It is the value 0x0377ae << 216.
46+
sigNetPowLimit = new(big.Int).Lsh(new(big.Int).SetInt64(0x0377ae), 216)
47+
48+
// DefaultSignetChallenge is the byte representation of the signet
49+
// challenge for the default (public, Taproot enabled) signet network.
50+
// This is the binary equivalent of the bitcoin script
51+
// 1 03ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be430
52+
// 0359ef5021964fe22d6f8e05b2463c9540ce96883fe3b278760f048f5189f2e6c4 2
53+
// OP_CHECKMULTISIG
54+
DefaultSignetChallenge, _ = hex.DecodeString(
55+
"512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d" +
56+
"1e086be430210359ef5021964fe22d6f8e05b2463c9540ce9688" +
57+
"3fe3b278760f048f5189f2e6c452ae",
58+
)
59+
60+
// DefaultSignetDNSSeeds is the list of seed nodes for the default
61+
// (public, Taproot enabled) signet network.
62+
DefaultSignetDNSSeeds = []DNSSeed{
63+
{"178.128.221.177", false},
64+
{"2a01:7c8:d005:390::5", false},
65+
{"v7ajjeirttkbnt32wpy3c6w3emwnfr3fkla7hpxcfokr3ysd3kqtzmqd.onion:38333", false},
66+
}
4167
)
4268

4369
// Checkpoint identifies a known good point in the block chain. Using
@@ -96,6 +122,11 @@ const (
96122
// includes the deployment of BIPS 141, 142, 144, 145, 147 and 173.
97123
DeploymentSegwit
98124

125+
// DeploymentTaproot defines the rule change deployment ID for the
126+
// Taproot (+Schnorr) soft-fork package. The taproot package includes
127+
// the deployment of BIPS 340, 341 and 342.
128+
DeploymentTaproot
129+
99130
// NOTE: DefinedDeployments must always come last since it is used to
100131
// determine how many defined deployments there currently are.
101132

@@ -578,6 +609,107 @@ var SimNetParams = Params{
578609
HDCoinType: 115, // ASCII for s
579610
}
580611

612+
// SigNetParams defines the network parameters for the default public signet
613+
// Bitcoin network. Not to be confused with the regression test network, this
614+
// network is sometimes simply called "signet" or "taproot signet".
615+
var SigNetParams = CustomSignetParams(
616+
DefaultSignetChallenge, DefaultSignetDNSSeeds,
617+
)
618+
619+
// CustomSignetParams creates network parameters for a custom signet network
620+
// from a challenge. The challenge is the binary compiled version of the block
621+
// challenge script.
622+
func CustomSignetParams(challenge []byte, dnsSeeds []DNSSeed) Params {
623+
// The message start is defined as the first four bytes of the sha256d
624+
// of the challenge script, as a single push (i.e. prefixed with the
625+
// challenge script length).
626+
challengeLength := byte(len(challenge))
627+
hashDouble := chainhash.DoubleHashB(
628+
append([]byte{challengeLength}, challenge...),
629+
)
630+
631+
// We use little endian encoding of the hash prefix to be in line with
632+
// the other wire network identities.
633+
net := binary.LittleEndian.Uint32(hashDouble[0:4])
634+
return Params{
635+
Name: "signet",
636+
Net: wire.BitcoinNet(net),
637+
DefaultPort: "38333",
638+
DNSSeeds: dnsSeeds,
639+
640+
// Chain parameters
641+
GenesisBlock: &sigNetGenesisBlock,
642+
GenesisHash: &sigNetGenesisHash,
643+
PowLimit: sigNetPowLimit,
644+
PowLimitBits: 0x1e0377ae,
645+
BIP0034Height: 1,
646+
BIP0065Height: 1,
647+
BIP0066Height: 1,
648+
CoinbaseMaturity: 100,
649+
SubsidyReductionInterval: 210000,
650+
TargetTimespan: time.Hour * 24 * 14, // 14 days
651+
TargetTimePerBlock: time.Minute * 10, // 10 minutes
652+
RetargetAdjustmentFactor: 4, // 25% less, 400% more
653+
ReduceMinDifficulty: false,
654+
MinDiffReductionTime: time.Minute * 20, // TargetTimePerBlock * 2
655+
GenerateSupported: false,
656+
657+
// Checkpoints ordered from oldest to newest.
658+
Checkpoints: nil,
659+
660+
// Consensus rule change deployments.
661+
//
662+
// The miner confirmation window is defined as:
663+
// target proof of work timespan / target proof of work spacing
664+
RuleChangeActivationThreshold: 1916, // 95% of 2016
665+
MinerConfirmationWindow: 2016,
666+
Deployments: [DefinedDeployments]ConsensusDeployment{
667+
DeploymentTestDummy: {
668+
BitNumber: 28,
669+
StartTime: 1199145601, // January 1, 2008 UTC
670+
ExpireTime: 1230767999, // December 31, 2008 UTC
671+
},
672+
DeploymentCSV: {
673+
BitNumber: 0,
674+
StartTime: 0, // Always available for vote
675+
ExpireTime: math.MaxInt64, // Never expires
676+
},
677+
DeploymentSegwit: {
678+
BitNumber: 1,
679+
StartTime: 0, // Always available for vote
680+
ExpireTime: math.MaxInt64, // Never expires.
681+
},
682+
DeploymentTaproot: {
683+
BitNumber: 2,
684+
StartTime: 0, // Always available for vote
685+
ExpireTime: math.MaxInt64, // Never expires.
686+
},
687+
},
688+
689+
// Mempool parameters
690+
RelayNonStdTxs: false,
691+
692+
// Human-readable part for Bech32 encoded segwit addresses, as defined in
693+
// BIP 173.
694+
Bech32HRPSegwit: "tb", // always tb for test net
695+
696+
// Address encoding magics
697+
PubKeyHashAddrID: 0x6f, // starts with m or n
698+
ScriptHashAddrID: 0xc4, // starts with 2
699+
WitnessPubKeyHashAddrID: 0x03, // starts with QW
700+
WitnessScriptHashAddrID: 0x28, // starts with T7n
701+
PrivateKeyID: 0xef, // starts with 9 (uncompressed) or c (compressed)
702+
703+
// BIP32 hierarchical deterministic extended key magics
704+
HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94}, // starts with tprv
705+
HDPublicKeyID: [4]byte{0x04, 0x35, 0x87, 0xcf}, // starts with tpub
706+
707+
// BIP44 coin type used in the hierarchical deterministic path for
708+
// address generation.
709+
HDCoinType: 1,
710+
}
711+
}
712+
581713
var (
582714
// ErrDuplicateNet describes an error where the parameters for a Bitcoin
583715
// network could not be set due to the network already being a standard

chaincfg/params_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package chaincfg
66

77
import (
88
"bytes"
9+
"encoding/hex"
10+
"math/big"
911
"testing"
1012
)
1113

@@ -84,3 +86,50 @@ func TestInvalidHDKeyID(t *testing.T) {
8486
t.Fatalf("HDPrivateKeyToPublicKeyID: want err ErrUnknownHDKeyID, got %v", err)
8587
}
8688
}
89+
90+
func TestSigNetPowLimit(t *testing.T) {
91+
sigNetPowLimitHex, _ := hex.DecodeString(
92+
"00000377ae000000000000000000000000000000000000000000000000000000",
93+
)
94+
powLimit := new(big.Int).SetBytes(sigNetPowLimitHex)
95+
if sigNetPowLimit.Cmp(powLimit) != 0 {
96+
t.Fatalf("Signet PoW limit bits (%s) not equal to big int (%s)",
97+
sigNetPowLimit.Text(16), powLimit.Text(16))
98+
}
99+
100+
if compactToBig(sigNetGenesisBlock.Header.Bits).Cmp(powLimit) != 0 {
101+
t.Fatalf("Signet PoW limit header bits (%d) not equal to big "+
102+
"int (%s)", sigNetGenesisBlock.Header.Bits,
103+
powLimit.Text(16))
104+
}
105+
}
106+
107+
// compactToBig is a copy of the blockchain.CompactToBig function. We copy it
108+
// here so we don't run into a circular dependency just because of a test.
109+
func compactToBig(compact uint32) *big.Int {
110+
// Extract the mantissa, sign bit, and exponent.
111+
mantissa := compact & 0x007fffff
112+
isNegative := compact&0x00800000 != 0
113+
exponent := uint(compact >> 24)
114+
115+
// Since the base for the exponent is 256, the exponent can be treated
116+
// as the number of bytes to represent the full 256-bit number. So,
117+
// treat the exponent as the number of bytes and shift the mantissa
118+
// right or left accordingly. This is equivalent to:
119+
// N = mantissa * 256^(exponent-3)
120+
var bn *big.Int
121+
if exponent <= 3 {
122+
mantissa >>= 8 * (3 - exponent)
123+
bn = big.NewInt(int64(mantissa))
124+
} else {
125+
bn = big.NewInt(int64(mantissa))
126+
bn.Lsh(bn, 8*(exponent-3))
127+
}
128+
129+
// Make it negative if the sign bit is set.
130+
if isNegative {
131+
bn = bn.Neg(bn)
132+
}
133+
134+
return bn
135+
}

0 commit comments

Comments
 (0)