From 55ef07ca61faaaa1dfe8adaf65ca321658e570cd Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Thu, 22 May 2014 12:45:08 -0500 Subject: [PATCH 01/36] Initial commit. --- README.md | 92 ++++++++++++++++++++++++++++++ doc.go | 63 +++++++++++++++++++++ params.go | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 318 insertions(+) create mode 100644 README.md create mode 100644 doc.go create mode 100644 params.go diff --git a/README.md b/README.md new file mode 100644 index 00000000..6baad9ce --- /dev/null +++ b/README.md @@ -0,0 +1,92 @@ +btcnet +====== + +Package btcnet defines the network parameters for the three standard Bitcoin +networks and provides the ability for callers to define their own custom +Bitcoin networks. + +This package is one of the core packages from btcd, an alternative full-node +implementation of Bitcoin which is under active development by Conformal. +Although it was primarily written for btcd, this package has intentionally been +designed so it can be used as a standalone package for any projects needing to +use parameters for the standard Bitcoin networks or for projects needing to +define their own network. + +## Sample Use + +```Go +package main + +import ( + "flag" + "fmt" + + "github.com/conformal/btcnet" + "github.com/conformal/btcutil" +) + +var testnet = flag.Bool("testnet", false, "operate on the testnet Bitcoin network") + +// By default (without -testnet), use mainnet. +var netParams = &btcnet.MainNetParams + +func main() { + flag.Parse() + + // Modify active network parameters if operating on testnet. + if *testnet { + netParams = &btcnet.TestNet3Params + } + + // later... + + // Create and print new payment address, specific to the active network. + pubKeyHash := make([]byte, 20) + addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, netParams.Net) + if err != nil { + // badness + return + } + fmt.Println(addr) +} +``` + +## Documentation + +Full `go doc` style documentation for the project can be viewed online without +installing this package by using the GoDoc site +[here](http://godoc.org/github.com/conformal/btcnet). + +You can also view the documentation locally once the package is installed with +the `godoc` tool by running `godoc -http=":6060"` and pointing your browser to +http://localhost:6060/pkg/github.com/conformal/btcnet + +## Installation + +```bash +$ go get github.com/conformal/btcnet +``` + +## GPG Verification Key + +All official release tags are signed by Conformal so users can ensure the code +has not been tampered with and is coming from Conformal. To verify the +signature perform the following: + +- Download the public key from the Conformal website at + https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt + +- Import the public key into your GPG keyring: + ```bash + gpg --import GIT-GPG-KEY-conformal.txt + ``` + +- Verify the release tag with the following command where `TAG_NAME` is a + placeholder for the specific tag: + ```bash + git tag -v TAG_NAME + ``` + +## License + +Package btcnet is licensed under the liberal ISC License. diff --git a/doc.go b/doc.go new file mode 100644 index 00000000..f3d8d2b1 --- /dev/null +++ b/doc.go @@ -0,0 +1,63 @@ +// Package btcnet defines the network parameters for the three standard Bitcoin +// networks and provides the ability for callers to define their own custom +// Bitcoin networks. +// +// In addition to the main Bitcoin network, which is intended for the transfer +// of monetary value, there also exists two currently active standard networks: +// regression test and testnet (version 3). These networks are incompatible +// with each other (each sharing a different genesis block) and software should +// handle errors where input intended for one network is used on an application +// instance running on a different network. +// +// For library packages, btcnet provides the ability to lookup chain parameters +// and encoding magics when passed a *Params. Older APIs not updated to the new +// convention of passing a *Params may lookup the parameters for a +// btcwire.BitcoinNet using ParamsForNet, but be aware that this usage is +// deprecated and will be removed from btcnet in the future. +// +// For main packages, a (typically global) var may be assigned the address of +// one of the standard Param vars for use as the application's "active" network. +// When a network parameter is needed, it may then be looked up through this +// variable (either directly, or hidden in a library call). +// +// package main +// +// import ( +// "flag" +// "fmt" +// "log" +// +// "github.com/conformal/btcnet" +// "github.com/conformal/btcutil" +// ) +// +// var testnet = flag.Bool("testnet", false, "operate on the testnet Bitcoin network") +// +// // By default (without -testnet), use mainnet. +// var netParams = &btcnet.MainNetParams +// +// func main() { +// flag.Parse() +// +// // Modify active network parameters if operating on testnet. +// if *testnet { +// netParams = &btcnet.TestNet3Params +// } +// +// // later... +// +// // Create and print new payment address, specific to the active network. +// pubKeyHash := make([]byte, 20) +// addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, netParams.Net) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println(addr) +// } +// +// If an application does not use one of the three standard Bitcoin networks, +// a new Params struct may be created which defines the parameters for the +// non-standard network. As a general rule of thumb, all network parameters +// should be unique to the network, but parameter collisions can still occur +// (unfortunately, this is the case with regtest and testnet3 sharing magics). +package btcnet diff --git a/params.go b/params.go new file mode 100644 index 00000000..c067e720 --- /dev/null +++ b/params.go @@ -0,0 +1,163 @@ +package btcnet + +import ( + "errors" + "math/big" + + "github.com/conformal/btcwire" +) + +// These variables are the chain proof-of-work limit parameters for each default +// network. +var ( + // bigOne is 1 represented as a big.Int. It is defined here to avoid + // the overhead of creating it multiple times. + bigOne = big.NewInt(1) + + // mainPowLimit is the highest proof of work value a Bitcoin block can + // have for the main network. It is the value 2^224 - 1. + mainPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne) + + // regressionPowLimit is the highest proof of work value a Bitcoin block + // can have for the regression test network. It is the value 2^255 - 1. + regressionPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne) + + // testNet3PowLimit is the highest proof of work value a Bitcoin block + // can have for the test network (version 3). It is the value + // 2^224 - 1. + testNet3PowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne) +) + +// Params defines a Bitcoin network by its parameters. These parameters may be +// used by Bitcoin applications to differentiate networks as well as addresses +// and keys for one network from those intended for use on another network. +type Params struct { + Name string + Net btcwire.BitcoinNet + + // Chain parameters + GenesisBlock *btcwire.MsgBlock + GenesisHash *btcwire.ShaHash + PowLimit *big.Int + PowLimitBits uint32 + SubsidyHalvingInterval int32 + + // Encoding magics + PubKeyHashAddrID byte // First byte of a P2PKH address + ScriptHashAddrID byte // First byte of a P2SH address + PrivateKeyID byte // First byte of a WIF private key +} + +// MainNetParams defines the network parameters for the main Bitcoin network. +var MainNetParams = Params{ + Name: "mainnet", + Net: btcwire.MainNet, + + // Chain parameters + GenesisBlock: &btcwire.GenesisBlock, + GenesisHash: &btcwire.GenesisHash, + PowLimit: mainPowLimit, + PowLimitBits: 0x1d00ffff, + SubsidyHalvingInterval: 210000, + + // Encoding magics + PubKeyHashAddrID: 0x00, + ScriptHashAddrID: 0x05, + PrivateKeyID: 0x80, +} + +// RegressionNetParams defines the network parameters for the regression test +// Bitcoin network. Not to be confused with the test Bitcoin network (version +// 3), this network is sometimes simply called "testnet". +var RegressionNetParams = Params{ + Name: "regtest", + Net: btcwire.TestNet, + + // Chain parameters + GenesisBlock: &btcwire.TestNetGenesisBlock, + GenesisHash: &btcwire.TestNetGenesisHash, + PowLimit: regressionPowLimit, + PowLimitBits: 0x207fffff, + SubsidyHalvingInterval: 150, + + // Encoding magics + PubKeyHashAddrID: 0x6f, + ScriptHashAddrID: 0xc4, + PrivateKeyID: 0xef, +} + +// TestNet3Params defines the network parameters for the test Bitcoin network +// (version 3). Not to be confused with the regression test network, this +// network is sometimes simply called "testnet". +var TestNet3Params = Params{ + Name: "testnet", + Net: btcwire.TestNet3, + + // Chain parameters + GenesisBlock: &btcwire.TestNet3GenesisBlock, + GenesisHash: &btcwire.TestNet3GenesisHash, + PowLimit: testNet3PowLimit, + PowLimitBits: 0x1d00ffff, + SubsidyHalvingInterval: 210000, + + // Encoding magics + PubKeyHashAddrID: 0x6f, + ScriptHashAddrID: 0xc4, + PrivateKeyID: 0xef, +} + +var ( + // ErrUnknownNet describes an error where the network parameters for a + // network cannot be looked up because the network is non-standard and + // is not registered into this package. + // + // This will be removed when ParamsForNet is eventually removed. + ErrUnknownNet = errors.New("unknown Bitcoin network") + + // ErrDuplicateNet describes an error where the parameters for a Bitcoin + // network could not be set due to the network already being a standard + // network or previously-registered into this package. + // + // This will be removed when Register is eventually removed. + ErrDuplicateNet = errors.New("duplicate Bitcoin network") +) + +var nets = map[btcwire.BitcoinNet]*Params{ + btcwire.MainNet: &MainNetParams, + btcwire.TestNet: &RegressionNetParams, + btcwire.TestNet3: &TestNet3Params, +} + +// ParamsForNet returns the network parameters for a Bitcoin network, or +// ErrUnknownNet if the network is not a default network (mainnet, regtest, +// or testnet3) and not registered into the package with Register. +// +// This should be considered an unstable API and will be removed when all other +// Conformal btc* packages (btcwire not included) are updated from using +// btcwire.BitcoinNet to *Params. +func ParamsForNet(net btcwire.BitcoinNet) (*Params, error) { + params, ok := nets[net] + if !ok { + return nil, ErrUnknownNet + } + return params, nil +} + +// Register registers the network parameters for a Bitcoin network. This may +// error with ErrDuplicateNet if the network is already registered. +// +// Network parameters should be registered into this package by a main package +// as early as possible. Then, library packages may lookup networks or network +// parameters based on inputs and work regardless of the network being standard +// or not. +// +// This should be considered an unstable API and will be removed when all other +// Conformal btc* packages (btcwire not included) are updated from using +// btcwire.BitcoinNet to *Params. +func Register(params *Params) error { + if _, ok := nets[params.Net]; ok { + return ErrDuplicateNet + } + nets[params.Net] = params + return nil +} From 536c1e3c298cd868e83f0158d9f56e5849468692 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Thu, 22 May 2014 23:13:44 -0500 Subject: [PATCH 02/36] Add parameters used by btcd. This change adds two parameters to the Params struct which are used by btcd to alter behavior based on the active network, rather than hardcoding checks for a particular network. The first, ResetMinDifficulty, specifies whether the target difficulty can change between the retarget intervals. The second, RelayNonStdTxs, specifies whether standard transaction checks should not be performed when accepting mempool transactions. The zero values (false) for both of these bools are the correct parameter values for mainnet. While here, rename the test network version 3 to "testnet3", as both btcd and btcwallet will include additional handling to rename this to "testnet" for directory names, and a switch to "testnet3" is planned in the future. --- params.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/params.go b/params.go index c067e720..04afb756 100644 --- a/params.go +++ b/params.go @@ -41,6 +41,10 @@ type Params struct { PowLimit *big.Int PowLimitBits uint32 SubsidyHalvingInterval int32 + ResetMinDifficulty bool + + // Mempool parameters + RelayNonStdTxs bool // Encoding magics PubKeyHashAddrID byte // First byte of a P2PKH address @@ -59,6 +63,10 @@ var MainNetParams = Params{ PowLimit: mainPowLimit, PowLimitBits: 0x1d00ffff, SubsidyHalvingInterval: 210000, + ResetMinDifficulty: false, + + // Mempool parameters + RelayNonStdTxs: false, // Encoding magics PubKeyHashAddrID: 0x00, @@ -79,6 +87,10 @@ var RegressionNetParams = Params{ PowLimit: regressionPowLimit, PowLimitBits: 0x207fffff, SubsidyHalvingInterval: 150, + ResetMinDifficulty: true, + + // Mempool parameters + RelayNonStdTxs: true, // Encoding magics PubKeyHashAddrID: 0x6f, @@ -90,7 +102,7 @@ var RegressionNetParams = Params{ // (version 3). Not to be confused with the regression test network, this // network is sometimes simply called "testnet". var TestNet3Params = Params{ - Name: "testnet", + Name: "testnet3", Net: btcwire.TestNet3, // Chain parameters @@ -99,6 +111,10 @@ var TestNet3Params = Params{ PowLimit: testNet3PowLimit, PowLimitBits: 0x1d00ffff, SubsidyHalvingInterval: 210000, + ResetMinDifficulty: true, + + // Mempool parameters + RelayNonStdTxs: true, // Encoding magics PubKeyHashAddrID: 0x6f, From fb8ab4200f4448f2b2347449d4e3ae0ee746f69d Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Mon, 26 May 2014 10:03:38 -0500 Subject: [PATCH 03/36] Add more chain-related fields and checkpoints. This commit adds more chain-related fields to the parameters as a part of converting btcchain to work with btcnet parameters instead of coding the network-specific knowledge into the package itself. It also moves the checkpoints from btcchain since checkpoints are a network parameter and make more sense here. ok @jrick --- params.go | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/params.go b/params.go index 04afb756..a909145a 100644 --- a/params.go +++ b/params.go @@ -28,6 +28,18 @@ var ( testNet3PowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne) ) +// Checkpoint identifies a known good point in the block chain. Using +// checkpoints allows a few optimizations for old blocks during initial download +// and also prevents forks from old blocks. +// +// Each checkpoint is selected based upon several factors. See the +// documentation for btcchain.IsCheckpointCandidate for details on the selection +// criteria. +type Checkpoint struct { + Height int64 + Hash *btcwire.ShaHash +} + // Params defines a Bitcoin network by its parameters. These parameters may be // used by Bitcoin applications to differentiate networks as well as addresses // and keys for one network from those intended for use on another network. @@ -43,6 +55,19 @@ type Params struct { SubsidyHalvingInterval int32 ResetMinDifficulty bool + // Checkpoints ordered from oldest to newest. + Checkpoints []Checkpoint + + // Reject version 1 blocks once a majority of the network has upgraded. + // This is part of BIP0034. + BlockV1RejectNumRequired uint64 + BlockV1RejectNumToCheck uint64 + + // Ensure coinbase starts with serialized block heights for version 2 + // blocks or newer once a majority of the network has upgraded. + CoinbaseBlockHeightNumRequired uint64 + CoinbaseBlockHeightNumToCheck uint64 + // Mempool parameters RelayNonStdTxs bool @@ -65,6 +90,37 @@ var MainNetParams = Params{ SubsidyHalvingInterval: 210000, ResetMinDifficulty: false, + // Checkpoints ordered from oldest to newest. + Checkpoints: []Checkpoint{ + {11111, newShaHashFromStr("0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d")}, + {33333, newShaHashFromStr("000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6")}, + {74000, newShaHashFromStr("0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20")}, + {105000, newShaHashFromStr("00000000000291ce28027faea320c8d2b054b2e0fe44a773f3eefb151d6bdc97")}, + {134444, newShaHashFromStr("00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe")}, + {168000, newShaHashFromStr("000000000000099e61ea72015e79632f216fe6cb33d7899acb35b75c8303b763")}, + {193000, newShaHashFromStr("000000000000059f452a5f7340de6682a977387c17010ff6e6c3bd83ca8b1317")}, + {210000, newShaHashFromStr("000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e")}, + {216116, newShaHashFromStr("00000000000001b4f4b433e81ee46494af945cf96014816a4e2370f11b23df4e")}, + {225430, newShaHashFromStr("00000000000001c108384350f74090433e7fcf79a606b8e797f065b130575932")}, + {250000, newShaHashFromStr("000000000000003887df1f29024b06fc2200b55f8af8f35453d7be294df2d214")}, + {267300, newShaHashFromStr("000000000000000a83fbd660e918f218bf37edd92b748ad940483c7c116179ac")}, + {279000, newShaHashFromStr("0000000000000001ae8c72a0b0c301f67e3afca10e819efa9041e458e9bd7e40")}, + {300255, newShaHashFromStr("0000000000000000162804527c6e9b9f0563a280525f9d08c12041def0a0f3b2")}, + }, + + // Reject version 1 blocks once a majority of the network has upgraded. + // 95% (950 / 1000) + // This is part of BIP0034. + BlockV1RejectNumRequired: 950, + BlockV1RejectNumToCheck: 1000, + + // Ensure coinbase starts with serialized block heights for version 2 + // blocks or newer once a majority of the network has upgraded. + // 75% (750 / 1000) + // This is part of BIP0034. + CoinbaseBlockHeightNumRequired: 750, + CoinbaseBlockHeightNumToCheck: 1000, + // Mempool parameters RelayNonStdTxs: false, @@ -89,6 +145,22 @@ var RegressionNetParams = Params{ SubsidyHalvingInterval: 150, ResetMinDifficulty: true, + // Checkpoints ordered from oldest to newest. + Checkpoints: nil, + + // Reject version 1 blocks once a majority of the network has upgraded. + // 75% (75 / 100) + // This is part of BIP0034. + BlockV1RejectNumRequired: 75, + BlockV1RejectNumToCheck: 100, + + // Ensure coinbase starts with serialized block heights for version 2 + // blocks or newer once a majority of the network has upgraded. + // 51% (51 / 100) + // This is part of BIP0034. + CoinbaseBlockHeightNumRequired: 51, + CoinbaseBlockHeightNumToCheck: 100, + // Mempool parameters RelayNonStdTxs: true, @@ -113,6 +185,24 @@ var TestNet3Params = Params{ SubsidyHalvingInterval: 210000, ResetMinDifficulty: true, + // Checkpoints ordered from oldest to newest. + Checkpoints: []Checkpoint{ + {546, newShaHashFromStr("000000002a936ca763904c3c35fce2f3556c559c0214345d31b1bcebf76acb70")}, + }, + + // Reject version 1 blocks once a majority of the network has upgraded. + // 75% (75 / 100) + // This is part of BIP0034. + BlockV1RejectNumRequired: 75, + BlockV1RejectNumToCheck: 100, + + // Ensure coinbase starts with serialized block heights for version 2 + // blocks or newer once a majority of the network has upgraded. + // 51% (51 / 100) + // This is part of BIP0034. + CoinbaseBlockHeightNumRequired: 51, + CoinbaseBlockHeightNumToCheck: 100, + // Mempool parameters RelayNonStdTxs: true, @@ -177,3 +267,22 @@ func Register(params *Params) error { nets[params.Net] = params return nil } + +// newShaHashFromStr converts the passed big-endian hex string into a +// btcwire.ShaHash. It only differs from the one available in btcwire in that +// it panics on an error since it will only (and must only) be called with +// hard-coded, and therefore known good, hashes. +func newShaHashFromStr(hexStr string) *btcwire.ShaHash { + sha, err := btcwire.NewShaHashFromStr(hexStr) + if err != nil { + // Ordinarily I don't like panics in library code since it + // can take applications down without them having a chance to + // recover which is extremely annoying, however an exception is + // being made in this case because the only way this can panic + // is if there is an error in the hard-coded hashes. Thus it + // will only ever potentially panic on init and therefore is + // 100% predictable. + panic(err) + } + return sha +} From 6f76171a8200e1e401096aa3b34547fd5adb165c Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Mon, 26 May 2014 17:55:13 -0500 Subject: [PATCH 04/36] Additions for btcutil. ok @davecgh --- params.go | 61 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/params.go b/params.go index a909145a..c5db6445 100644 --- a/params.go +++ b/params.go @@ -228,26 +228,19 @@ var ( ErrDuplicateNet = errors.New("duplicate Bitcoin network") ) -var nets = map[btcwire.BitcoinNet]*Params{ - btcwire.MainNet: &MainNetParams, - btcwire.TestNet: &RegressionNetParams, - btcwire.TestNet3: &TestNet3Params, -} +var ( + registeredNets = map[btcwire.BitcoinNet]struct{}{} -// ParamsForNet returns the network parameters for a Bitcoin network, or -// ErrUnknownNet if the network is not a default network (mainnet, regtest, -// or testnet3) and not registered into the package with Register. -// -// This should be considered an unstable API and will be removed when all other -// Conformal btc* packages (btcwire not included) are updated from using -// btcwire.BitcoinNet to *Params. -func ParamsForNet(net btcwire.BitcoinNet) (*Params, error) { - params, ok := nets[net] - if !ok { - return nil, ErrUnknownNet + pubKeyHashAddrIDs = map[byte]struct{}{ + MainNetParams.PubKeyHashAddrID: struct{}{}, + TestNet3Params.PubKeyHashAddrID: struct{}{}, // shared with regtest } - return params, nil -} + + scriptHashAddrIDs = map[byte]struct{}{ + MainNetParams.ScriptHashAddrID: struct{}{}, + TestNet3Params.ScriptHashAddrID: struct{}{}, // shared with regtest + } +) // Register registers the network parameters for a Bitcoin network. This may // error with ErrDuplicateNet if the network is already registered. @@ -256,18 +249,38 @@ func ParamsForNet(net btcwire.BitcoinNet) (*Params, error) { // as early as possible. Then, library packages may lookup networks or network // parameters based on inputs and work regardless of the network being standard // or not. -// -// This should be considered an unstable API and will be removed when all other -// Conformal btc* packages (btcwire not included) are updated from using -// btcwire.BitcoinNet to *Params. func Register(params *Params) error { - if _, ok := nets[params.Net]; ok { + if _, ok := registeredNets[params.Net]; ok { return ErrDuplicateNet } - nets[params.Net] = params + registeredNets[params.Net] = struct{}{} + pubKeyHashAddrIDs[params.ScriptHashAddrID] = struct{}{} + scriptHashAddrIDs[params.ScriptHashAddrID] = struct{}{} return nil } +// IsPubKeyHashAddrID returns whether the id is an identifier known to prefix a +// pay-to-pubkey-hash address on any default or registered network. This is +// used when decoding an address string into a specific address type. It is up +// to the caller to check both this and IsScriptHashAddrID and decide whether an +// address is a pubkey hash address, script hash address, neither, or +// undeterminable (if both return true). +func IsPubKeyHashAddrID(id byte) bool { + _, ok := pubKeyHashAddrIDs[id] + return ok +} + +// IsScriptHashAddrID returns whether the id is an identifier known to prefix a +// pay-to-script-hash address on any default or registered network. This is +// used when decoding an address string into a specific address type. It is up +// to the caller to check both this and IsPubKeyHashAddrID and decide whether an +// address is a pubkey hash address, script hash address, neither, or +// undeterminable (if both return true). +func IsScriptHashAddrID(id byte) bool { + _, ok := scriptHashAddrIDs[id] + return ok +} + // newShaHashFromStr converts the passed big-endian hex string into a // btcwire.ShaHash. It only differs from the one available in btcwire in that // it panics on an error since it will only (and must only) be called with From 18794e4cfce1e72fe8981303b45f8df1d8dd27cc Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Tue, 27 May 2014 20:35:35 -0500 Subject: [PATCH 05/36] Fix errors after deciding on the Register API. The ParamsForNet function was removed, so likewise this change removes ErrUnknownNet error that it used to return. As network registration is now necessary for correct handling of alternate network encoding magics, and therefore the ErrDuplicateNet error returned by Register is here to stay, kill the comment about the error being removed later. --- params.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/params.go b/params.go index c5db6445..235d2e7c 100644 --- a/params.go +++ b/params.go @@ -213,18 +213,9 @@ var TestNet3Params = Params{ } var ( - // ErrUnknownNet describes an error where the network parameters for a - // network cannot be looked up because the network is non-standard and - // is not registered into this package. - // - // This will be removed when ParamsForNet is eventually removed. - ErrUnknownNet = errors.New("unknown Bitcoin network") - // ErrDuplicateNet describes an error where the parameters for a Bitcoin // network could not be set due to the network already being a standard // network or previously-registered into this package. - // - // This will be removed when Register is eventually removed. ErrDuplicateNet = errors.New("duplicate Bitcoin network") ) From 81c37e551f1b66c1c732b8bf5640aac56d5e2349 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Tue, 27 May 2014 20:41:30 -0500 Subject: [PATCH 06/36] Fix documentation code examples. The examples uses a btcutil API that was recently updated from passing btcwire.BitcoinNet to *btcnet.Params. This change updates the btcutil call in the examples, as well as modifying the example in the README to match that in doc.go (where errors were handled slightly differently). --- README.md | 6 +++--- doc.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6baad9ce..7cdd4a84 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ package main import ( "flag" "fmt" + "log" "github.com/conformal/btcnet" "github.com/conformal/btcutil" @@ -42,10 +43,9 @@ func main() { // Create and print new payment address, specific to the active network. pubKeyHash := make([]byte, 20) - addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, netParams.Net) + addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, netParams) if err != nil { - // badness - return + log.Fatal(err) } fmt.Println(addr) } diff --git a/doc.go b/doc.go index f3d8d2b1..649d4a17 100644 --- a/doc.go +++ b/doc.go @@ -48,7 +48,7 @@ // // // Create and print new payment address, specific to the active network. // pubKeyHash := make([]byte, 20) -// addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, netParams.Net) +// addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, netParams) // if err != nil { // log.Fatal(err) // } From 6b3e878cd941ff6c888dcec28ad7edddd21c5b00 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Wed, 28 May 2014 00:15:11 -0500 Subject: [PATCH 07/36] Add ISC license. Spotted by @davecgh. --- LICENSE | 13 +++++++++++++ params.go | 4 ++++ 2 files changed, 17 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..405e51cd --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright (c) 2014 Conformal Systems LLC. + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/params.go b/params.go index 235d2e7c..de07e3b6 100644 --- a/params.go +++ b/params.go @@ -1,3 +1,7 @@ +// Copyright (c) 2014 Conformal Systems LLC. +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + package btcnet import ( From 01799eeff10f05cbc15a37d14ed913e0a93085f1 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 28 May 2014 00:23:10 -0500 Subject: [PATCH 08/36] Define genesis blocks internally. The genesis block for each network is a parameter for the network. As such, it makes more sense to define them in this package instead of in the wire protocol package. ok @jrick --- genesis.go | 142 ++++++++++++++++++++++++++++++ genesis_test.go | 224 ++++++++++++++++++++++++++++++++++++++++++++++++ params.go | 12 +-- 3 files changed, 372 insertions(+), 6 deletions(-) create mode 100644 genesis.go create mode 100644 genesis_test.go diff --git a/genesis.go b/genesis.go new file mode 100644 index 00000000..f61e572f --- /dev/null +++ b/genesis.go @@ -0,0 +1,142 @@ +// Copyright (c) 2014 Conformal Systems LLC. +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package btcnet + +import ( + "github.com/conformal/btcwire" + "time" +) + +// genesisCoinbaseTx is the coinbase transaction for the genesis blocks for +// the main network, regression test network, and test network (version 3). +var genesisCoinbaseTx = btcwire.MsgTx{ + Version: 1, + TxIn: []*btcwire.TxIn{ + { + PreviousOutpoint: btcwire.OutPoint{ + Hash: btcwire.ShaHash{}, + Index: 0xffffffff, + }, + SignatureScript: []byte{ + 0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, 0x45, /* |.......E| */ + 0x54, 0x68, 0x65, 0x20, 0x54, 0x69, 0x6d, 0x65, /* |The Time| */ + 0x73, 0x20, 0x30, 0x33, 0x2f, 0x4a, 0x61, 0x6e, /* |s 03/Jan| */ + 0x2f, 0x32, 0x30, 0x30, 0x39, 0x20, 0x43, 0x68, /* |/2009 Ch| */ + 0x61, 0x6e, 0x63, 0x65, 0x6c, 0x6c, 0x6f, 0x72, /* |ancellor| */ + 0x20, 0x6f, 0x6e, 0x20, 0x62, 0x72, 0x69, 0x6e, /* | on brin| */ + 0x6b, 0x20, 0x6f, 0x66, 0x20, 0x73, 0x65, 0x63, /* |k of sec|*/ + 0x6f, 0x6e, 0x64, 0x20, 0x62, 0x61, 0x69, 0x6c, /* |ond bail| */ + 0x6f, 0x75, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, /* |out for |*/ + 0x62, 0x61, 0x6e, 0x6b, 0x73, /* |banks| */ + }, + Sequence: 0xffffffff, + }, + }, + TxOut: []*btcwire.TxOut{ + { + Value: 0x12a05f200, + PkScript: []byte{ + 0x41, 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, /* |A.g....U| */ + 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, /* |H'.g..q0| */ + 0xb7, 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, /* |..\..(.9| */ + 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, /* |..yb...a| */ + 0xde, 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, /* |..I..?L.| */ + 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, /* |8..U....| */ + 0x12, 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, /* |..\8M...| */ + 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, /* |.W.Lp+k.| */ + 0x1d, 0x5f, 0xac, /* |._.| */ + }, + }, + }, + LockTime: 0, +} + +// genesisHash is the hash of the first block in the block chain for the main +// network (genesis block). +var genesisHash = btcwire.ShaHash{ + 0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72, + 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f, + 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c, + 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, +} + +// genesisMerkleRoot is the hash of the first transaction in the genesis block +// for the main network. +var genesisMerkleRoot = btcwire.ShaHash{ + 0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2, + 0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61, + 0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32, + 0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, +} + +// genesisBlock defines the genesis block of the block chain which serves as the +// public transaction ledger for the main network. +var genesisBlock = btcwire.MsgBlock{ + Header: btcwire.BlockHeader{ + Version: 1, + PrevBlock: btcwire.ShaHash{}, // 0000000000000000000000000000000000000000000000000000000000000000 + MerkleRoot: genesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b + Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 18:15:05 +0000 UTC + Bits: 0x1d00ffff, // 486604799 [00000000ffff0000000000000000000000000000000000000000000000000000] + Nonce: 0x7c2bac1d, // 2083236893 + }, + Transactions: []*btcwire.MsgTx{&genesisCoinbaseTx}, +} + +// regTestGenesisHash is the hash of the first block in the block chain for the +// regression test network (genesis block). +var regTestGenesisHash = btcwire.ShaHash{ + 0x06, 0x22, 0x6e, 0x46, 0x11, 0x1a, 0x0b, 0x59, + 0xca, 0xaf, 0x12, 0x60, 0x43, 0xeb, 0x5b, 0xbf, + 0x28, 0xc3, 0x4f, 0x3a, 0x5e, 0x33, 0x2a, 0x1f, + 0xc7, 0xb2, 0xb7, 0x3c, 0xf1, 0x88, 0x91, 0x0f, +} + +// regTestGenesisMerkleRoot is the hash of the first transaction in the genesis +// block for the regression test network. It is the same as the merkle root for +// the main network. +var regTestGenesisMerkleRoot = genesisMerkleRoot + +// regTestGenesisBlock defines the genesis block of the block chain which serves +// as the public transaction ledger for the regression test network. +var regTestGenesisBlock = btcwire.MsgBlock{ + Header: btcwire.BlockHeader{ + Version: 1, + PrevBlock: btcwire.ShaHash{}, // 0000000000000000000000000000000000000000000000000000000000000000 + MerkleRoot: regTestGenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b + Timestamp: time.Unix(1296688602, 0), // 2011-02-02 23:16:42 +0000 UTC + Bits: 0x207fffff, // 545259519 [7fffff0000000000000000000000000000000000000000000000000000000000] + Nonce: 2, + }, + Transactions: []*btcwire.MsgTx{&genesisCoinbaseTx}, +} + +// testNet3GenesisHash is the hash of the first block in the block chain for the +// test network (version 3). +var testNet3GenesisHash = btcwire.ShaHash{ + 0x43, 0x49, 0x7f, 0xd7, 0xf8, 0x26, 0x95, 0x71, + 0x08, 0xf4, 0xa3, 0x0f, 0xd9, 0xce, 0xc3, 0xae, + 0xba, 0x79, 0x97, 0x20, 0x84, 0xe9, 0x0e, 0xad, + 0x01, 0xea, 0x33, 0x09, 0x00, 0x00, 0x00, 0x00, +} + +// testNet3GenesisMerkleRoot is the hash of the first transaction in the genesis +// block for the test network (version 3). It is the same as the merkle root +// for the main network. +var testNet3GenesisMerkleRoot = genesisMerkleRoot + +// testNet3GenesisBlock defines the genesis block of the block chain which +// serves as the public transaction ledger for the test network (version 3). +var testNet3GenesisBlock = btcwire.MsgBlock{ + Header: btcwire.BlockHeader{ + Version: 1, + PrevBlock: btcwire.ShaHash{}, // 0000000000000000000000000000000000000000000000000000000000000000 + MerkleRoot: testNet3GenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b + Timestamp: time.Unix(1296688602, 0), // 2011-02-02 23:16:42 +0000 UTC + Bits: 0x1d00ffff, // 486604799 [00000000ffff0000000000000000000000000000000000000000000000000000] + Nonce: 0x18aea41a, // 414098458 + }, + Transactions: []*btcwire.MsgTx{&genesisCoinbaseTx}, +} diff --git a/genesis_test.go b/genesis_test.go new file mode 100644 index 00000000..1f4f993e --- /dev/null +++ b/genesis_test.go @@ -0,0 +1,224 @@ +// Copyright (c) 2014 Conformal Systems LLC. +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package btcnet_test + +import ( + "bytes" + "github.com/conformal/btcnet" + "github.com/davecgh/go-spew/spew" + "testing" +) + +// TestGenesisBlock tests the genesis block of the main network for validity by +// checking the encoded bytes and hashes. +func TestGenesisBlock(t *testing.T) { + // Encode the genesis block to raw bytes. + var buf bytes.Buffer + err := btcnet.MainNetParams.GenesisBlock.Serialize(&buf) + if err != nil { + t.Fatalf("TestGenesisBlock: %v", err) + } + + // Ensure the encoded block matches the expected bytes. + if !bytes.Equal(buf.Bytes(), genesisBlockBytes) { + t.Fatalf("TestGenesisBlock: Genesis block does not appear valid - "+ + "got %v, want %v", spew.Sdump(buf.Bytes()), + spew.Sdump(genesisBlockBytes)) + } + + // Check hash of the block against expected hash. + hash, err := btcnet.MainNetParams.GenesisBlock.BlockSha() + if err != nil { + t.Fatalf("BlockSha: %v", err) + } + if !btcnet.MainNetParams.GenesisHash.IsEqual(&hash) { + t.Fatalf("TestGenesisBlock: Genesis block hash does not "+ + "appear valid - got %v, want %v", spew.Sdump(hash), + spew.Sdump(btcnet.MainNetParams.GenesisHash)) + } +} + +// TestRegTestGenesisBlock tests the genesis block of the regression test +// network for validity by checking the encoded bytes and hashes. +func TestRegTestGenesisBlock(t *testing.T) { + // Encode the genesis block to raw bytes. + var buf bytes.Buffer + err := btcnet.RegressionNetParams.GenesisBlock.Serialize(&buf) + if err != nil { + t.Fatalf("TestRegTestGenesisBlock: %v", err) + } + + // Ensure the encoded block matches the expected bytes. + if !bytes.Equal(buf.Bytes(), regTestGenesisBlockBytes) { + t.Fatalf("TestRegTestGenesisBlock: Genesis block does not "+ + "appear valid - got %v, want %v", + spew.Sdump(buf.Bytes()), + spew.Sdump(regTestGenesisBlockBytes)) + } + + // Check hash of the block against expected hash. + hash, err := btcnet.RegressionNetParams.GenesisBlock.BlockSha() + if err != nil { + t.Errorf("BlockSha: %v", err) + } + if !btcnet.RegressionNetParams.GenesisHash.IsEqual(&hash) { + t.Fatalf("TestRegTestGenesisBlock: Genesis block hash does "+ + "not appear valid - got %v, want %v", spew.Sdump(hash), + spew.Sdump(btcnet.RegressionNetParams.GenesisHash)) + } +} + +// TestTestNet3GenesisBlock tests the genesis block of the test network (version +// 3) for validity by checking the encoded bytes and hashes. +func TestTestNet3GenesisBlock(t *testing.T) { + // Encode the genesis block to raw bytes. + var buf bytes.Buffer + err := btcnet.TestNet3Params.GenesisBlock.Serialize(&buf) + if err != nil { + t.Fatalf("TestTestNet3GenesisBlock: %v", err) + } + + // Ensure the encoded block matches the expected bytes. + if !bytes.Equal(buf.Bytes(), testNet3GenesisBlockBytes) { + t.Fatalf("TestTestNet3GenesisBlock: Genesis block does not "+ + "appear valid - got %v, want %v", + spew.Sdump(buf.Bytes()), + spew.Sdump(testNet3GenesisBlockBytes)) + } + + // Check hash of the block against expected hash. + hash, err := btcnet.TestNet3Params.GenesisBlock.BlockSha() + if err != nil { + t.Fatalf("BlockSha: %v", err) + } + if !btcnet.TestNet3Params.GenesisHash.IsEqual(&hash) { + t.Fatalf("TestTestNet3GenesisBlock: Genesis block hash does "+ + "not appear valid - got %v, want %v", spew.Sdump(hash), + spew.Sdump(btcnet.TestNet3Params.GenesisHash)) + } +} + +// genesisBlockBytes are the wire encoded bytes for the genesis block of the +// main network as of protocol version 60002. +var genesisBlockBytes = []byte{ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd, /* |....;...| */ + 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, /* |z{..z.,>| */ + 0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, /* |gv.a....| */ + 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, /* |..Q2:...| */ + 0x4b, 0x1e, 0x5e, 0x4a, 0x29, 0xab, 0x5f, 0x49, /* |K.^J)._I| */ + 0xff, 0xff, 0x00, 0x1d, 0x1d, 0xac, 0x2b, 0x7c, /* |......+|| */ + 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, /* |........| */ + 0xff, 0xff, 0x4d, 0x04, 0xff, 0xff, 0x00, 0x1d, /* |..M.....| */ + 0x01, 0x04, 0x45, 0x54, 0x68, 0x65, 0x20, 0x54, /* |..EThe T| */ + 0x69, 0x6d, 0x65, 0x73, 0x20, 0x30, 0x33, 0x2f, /* |imes 03/| */ + 0x4a, 0x61, 0x6e, 0x2f, 0x32, 0x30, 0x30, 0x39, /* |Jan/2009| */ + 0x20, 0x43, 0x68, 0x61, 0x6e, 0x63, 0x65, 0x6c, /* | Chancel| */ + 0x6c, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x20, 0x62, /* |lor on b| */ + 0x72, 0x69, 0x6e, 0x6b, 0x20, 0x6f, 0x66, 0x20, /* |rink of | */ + 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x62, /* |second b| */ + 0x61, 0x69, 0x6c, 0x6f, 0x75, 0x74, 0x20, 0x66, /* |ailout f| */ + 0x6f, 0x72, 0x20, 0x62, 0x61, 0x6e, 0x6b, 0x73, /* |or banks| */ + 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, /* |........| */ + 0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, /* |*....CA.| */ + 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, /* |g....UH'| */ + 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, /* |.g..q0..| */ + 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, /* |\..(.9..| */ + 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, /* |yb...a..| */ + 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, /* |I..?L.8.| */ + 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, /* |.U......| */ + 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, /* |\8M....W| */ + 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, /* |.Lp+k.._|*/ + 0xac, 0x00, 0x00, 0x00, 0x00, /* |.....| */ +} + +// regTestGenesisBlockBytes are the wire encoded bytes for the genesis block of +// the regression test network as of protocol version 60002. +var regTestGenesisBlockBytes = []byte{ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd, /* |....;...| */ + 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, /* |z{..z.,>| */ + 0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, /* |gv.a....| */ + 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, /* |..Q2:...| */ + 0x4b, 0x1e, 0x5e, 0x4a, 0xda, 0xe5, 0x49, 0x4d, /* |K.^J)._I| */ + 0xff, 0xff, 0x7f, 0x20, 0x02, 0x00, 0x00, 0x00, /* |......+|| */ + 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, /* |........| */ + 0xff, 0xff, 0x4d, 0x04, 0xff, 0xff, 0x00, 0x1d, /* |..M.....| */ + 0x01, 0x04, 0x45, 0x54, 0x68, 0x65, 0x20, 0x54, /* |..EThe T| */ + 0x69, 0x6d, 0x65, 0x73, 0x20, 0x30, 0x33, 0x2f, /* |imes 03/| */ + 0x4a, 0x61, 0x6e, 0x2f, 0x32, 0x30, 0x30, 0x39, /* |Jan/2009| */ + 0x20, 0x43, 0x68, 0x61, 0x6e, 0x63, 0x65, 0x6c, /* | Chancel| */ + 0x6c, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x20, 0x62, /* |lor on b| */ + 0x72, 0x69, 0x6e, 0x6b, 0x20, 0x6f, 0x66, 0x20, /* |rink of | */ + 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x62, /* |second b| */ + 0x61, 0x69, 0x6c, 0x6f, 0x75, 0x74, 0x20, 0x66, /* |ailout f| */ + 0x6f, 0x72, 0x20, 0x62, 0x61, 0x6e, 0x6b, 0x73, /* |or banks| */ + 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, /* |........| */ + 0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, /* |*....CA.| */ + 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, /* |g....UH'| */ + 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, /* |.g..q0..| */ + 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, /* |\..(.9..| */ + 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, /* |yb...a..| */ + 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, /* |I..?L.8.| */ + 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, /* |.U......| */ + 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, /* |\8M....W| */ + 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, /* |.Lp+k.._|*/ + 0xac, 0x00, 0x00, 0x00, 0x00, /* |.....| */ +} + +// testNet3GenesisBlockBytes are the wire encoded bytes for the genesis block of +// the test network (version 3) as of protocol version 60002. +var testNet3GenesisBlockBytes = []byte{ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd, /* |....;...| */ + 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, /* |z{..z.,>| */ + 0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, /* |gv.a....| */ + 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, /* |..Q2:...| */ + 0x4b, 0x1e, 0x5e, 0x4a, 0xda, 0xe5, 0x49, 0x4d, /* |K.^J)._I| */ + 0xff, 0xff, 0x00, 0x1d, 0x1a, 0xa4, 0xae, 0x18, /* |......+|| */ + 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, /* |........| */ + 0xff, 0xff, 0x4d, 0x04, 0xff, 0xff, 0x00, 0x1d, /* |..M.....| */ + 0x01, 0x04, 0x45, 0x54, 0x68, 0x65, 0x20, 0x54, /* |..EThe T| */ + 0x69, 0x6d, 0x65, 0x73, 0x20, 0x30, 0x33, 0x2f, /* |imes 03/| */ + 0x4a, 0x61, 0x6e, 0x2f, 0x32, 0x30, 0x30, 0x39, /* |Jan/2009| */ + 0x20, 0x43, 0x68, 0x61, 0x6e, 0x63, 0x65, 0x6c, /* | Chancel| */ + 0x6c, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x20, 0x62, /* |lor on b| */ + 0x72, 0x69, 0x6e, 0x6b, 0x20, 0x6f, 0x66, 0x20, /* |rink of | */ + 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x62, /* |second b| */ + 0x61, 0x69, 0x6c, 0x6f, 0x75, 0x74, 0x20, 0x66, /* |ailout f| */ + 0x6f, 0x72, 0x20, 0x62, 0x61, 0x6e, 0x6b, 0x73, /* |or banks| */ + 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, /* |........| */ + 0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, /* |*....CA.| */ + 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, /* |g....UH'| */ + 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, /* |.g..q0..| */ + 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, /* |\..(.9..| */ + 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, /* |yb...a..| */ + 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, /* |I..?L.8.| */ + 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, /* |.U......| */ + 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, /* |\8M....W| */ + 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, /* |.Lp+k.._|*/ + 0xac, 0x00, 0x00, 0x00, 0x00, /* |.....| */ +} diff --git a/params.go b/params.go index de07e3b6..778635f6 100644 --- a/params.go +++ b/params.go @@ -87,8 +87,8 @@ var MainNetParams = Params{ Net: btcwire.MainNet, // Chain parameters - GenesisBlock: &btcwire.GenesisBlock, - GenesisHash: &btcwire.GenesisHash, + GenesisBlock: &genesisBlock, + GenesisHash: &genesisHash, PowLimit: mainPowLimit, PowLimitBits: 0x1d00ffff, SubsidyHalvingInterval: 210000, @@ -142,8 +142,8 @@ var RegressionNetParams = Params{ Net: btcwire.TestNet, // Chain parameters - GenesisBlock: &btcwire.TestNetGenesisBlock, - GenesisHash: &btcwire.TestNetGenesisHash, + GenesisBlock: ®TestGenesisBlock, + GenesisHash: ®TestGenesisHash, PowLimit: regressionPowLimit, PowLimitBits: 0x207fffff, SubsidyHalvingInterval: 150, @@ -182,8 +182,8 @@ var TestNet3Params = Params{ Net: btcwire.TestNet3, // Chain parameters - GenesisBlock: &btcwire.TestNet3GenesisBlock, - GenesisHash: &btcwire.TestNet3GenesisHash, + GenesisBlock: &testNet3GenesisBlock, + GenesisHash: &testNet3GenesisHash, PowLimit: testNet3PowLimit, PowLimitBits: 0x1d00ffff, SubsidyHalvingInterval: 210000, From 218f8df5bac459e559500f09f81a87545cc58155 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 29 May 2014 11:59:36 -0500 Subject: [PATCH 09/36] Add a default net port parameter to each network. ok @jrick --- params.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/params.go b/params.go index 778635f6..10c612de 100644 --- a/params.go +++ b/params.go @@ -48,8 +48,9 @@ type Checkpoint struct { // used by Bitcoin applications to differentiate networks as well as addresses // and keys for one network from those intended for use on another network. type Params struct { - Name string - Net btcwire.BitcoinNet + Name string + Net btcwire.BitcoinNet + DefaultPort string // Chain parameters GenesisBlock *btcwire.MsgBlock @@ -83,8 +84,9 @@ type Params struct { // MainNetParams defines the network parameters for the main Bitcoin network. var MainNetParams = Params{ - Name: "mainnet", - Net: btcwire.MainNet, + Name: "mainnet", + Net: btcwire.MainNet, + DefaultPort: "8333", // Chain parameters GenesisBlock: &genesisBlock, @@ -138,8 +140,9 @@ var MainNetParams = Params{ // Bitcoin network. Not to be confused with the test Bitcoin network (version // 3), this network is sometimes simply called "testnet". var RegressionNetParams = Params{ - Name: "regtest", - Net: btcwire.TestNet, + Name: "regtest", + Net: btcwire.TestNet, + DefaultPort: "18444", // Chain parameters GenesisBlock: ®TestGenesisBlock, @@ -178,8 +181,9 @@ var RegressionNetParams = Params{ // (version 3). Not to be confused with the regression test network, this // network is sometimes simply called "testnet". var TestNet3Params = Params{ - Name: "testnet3", - Net: btcwire.TestNet3, + Name: "testnet3", + Net: btcwire.TestNet3, + DefaultPort: "18333", // Chain parameters GenesisBlock: &testNet3GenesisBlock, From dc967b7cc8309e0f420b926bdb98c084200b0275 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 28 May 2014 01:46:13 -0500 Subject: [PATCH 10/36] Add params for new simulation testing network. ok @jrick --- genesis.go | 28 +++++++++++++++++++ genesis_test.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ params.go | 49 ++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+) diff --git a/genesis.go b/genesis.go index f61e572f..135d0f28 100644 --- a/genesis.go +++ b/genesis.go @@ -140,3 +140,31 @@ var testNet3GenesisBlock = btcwire.MsgBlock{ }, Transactions: []*btcwire.MsgTx{&genesisCoinbaseTx}, } + +// simNetGenesisHash is the hash of the first block in the block chain for the +// simulation test network. +var simNetGenesisHash = btcwire.ShaHash{ + 0xf6, 0x7a, 0xd7, 0x69, 0x5d, 0x9b, 0x66, 0x2a, + 0x72, 0xff, 0x3d, 0x8e, 0xdb, 0xbb, 0x2d, 0xe0, + 0xbf, 0xa6, 0x7b, 0x13, 0x97, 0x4b, 0xb9, 0x91, + 0x0d, 0x11, 0x6d, 0x5c, 0xbd, 0x86, 0x3e, 0x68, +} + +// simNetGenesisMerkleRoot is the hash of the first transaction in the genesis +// block for the simulation test network. It is the same as the merkle root for +// the main network. +var simNetGenesisMerkleRoot = genesisMerkleRoot + +// simNetGenesisBlock defines the genesis block of the block chain which serves +// as the public transaction ledger for the simulation test network. +var simNetGenesisBlock = btcwire.MsgBlock{ + Header: btcwire.BlockHeader{ + Version: 1, + PrevBlock: btcwire.ShaHash{}, // 0000000000000000000000000000000000000000000000000000000000000000 + MerkleRoot: simNetGenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b + Timestamp: time.Unix(1401292357, 0), // 2014-05-28 15:52:37 +0000 UTC + Bits: 0x207fffff, // 545259519 [7fffff0000000000000000000000000000000000000000000000000000000000] + Nonce: 2, + }, + Transactions: []*btcwire.MsgTx{&genesisCoinbaseTx}, +} diff --git a/genesis_test.go b/genesis_test.go index 1f4f993e..fd791ed7 100644 --- a/genesis_test.go +++ b/genesis_test.go @@ -100,6 +100,36 @@ func TestTestNet3GenesisBlock(t *testing.T) { } } +// TestSimNetGenesisBlock tests the genesis block of the simulation test network +// for validity by checking the encoded bytes and hashes. +func TestSimNetGenesisBlock(t *testing.T) { + // Encode the genesis block to raw bytes. + var buf bytes.Buffer + err := btcnet.SimNetParams.GenesisBlock.Serialize(&buf) + if err != nil { + t.Fatalf("TestSimNetGenesisBlock: %v", err) + } + + // Ensure the encoded block matches the expected bytes. + if !bytes.Equal(buf.Bytes(), simNetGenesisBlockBytes) { + t.Fatalf("TestSimNetGenesisBlock: Genesis block does not "+ + "appear valid - got %v, want %v", + spew.Sdump(buf.Bytes()), + spew.Sdump(simNetGenesisBlockBytes)) + } + + // Check hash of the block against expected hash. + hash, err := btcnet.SimNetParams.GenesisBlock.BlockSha() + if err != nil { + t.Fatalf("BlockSha: %v", err) + } + if !btcnet.SimNetParams.GenesisHash.IsEqual(&hash) { + t.Fatalf("TestSimNetGenesisBlock: Genesis block hash does "+ + "not appear valid - got %v, want %v", spew.Sdump(hash), + spew.Sdump(btcnet.SimNetParams.GenesisHash)) + } +} + // genesisBlockBytes are the wire encoded bytes for the genesis block of the // main network as of protocol version 60002. var genesisBlockBytes = []byte{ @@ -222,3 +252,44 @@ var testNet3GenesisBlockBytes = []byte{ 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, /* |.Lp+k.._|*/ 0xac, 0x00, 0x00, 0x00, 0x00, /* |.....| */ } + +// simNetGenesisBlockBytes are the wire encoded bytes for the genesis block of +// the simulation test network as of protocol version 70002. +var simNetGenesisBlockBytes = []byte{ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd, /* |....;...| */ + 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, /* |z{..z.,>| */ + 0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, /* |gv.a....| */ + 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, /* |..Q2:...| */ + 0x4b, 0x1e, 0x5e, 0x4a, 0x45, 0x06, 0x86, 0x53, /* |K.^J)._I| */ + 0xff, 0xff, 0x7f, 0x20, 0x02, 0x00, 0x00, 0x00, /* |......+|| */ + 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* |........| */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, /* |........| */ + 0xff, 0xff, 0x4d, 0x04, 0xff, 0xff, 0x00, 0x1d, /* |..M.....| */ + 0x01, 0x04, 0x45, 0x54, 0x68, 0x65, 0x20, 0x54, /* |..EThe T| */ + 0x69, 0x6d, 0x65, 0x73, 0x20, 0x30, 0x33, 0x2f, /* |imes 03/| */ + 0x4a, 0x61, 0x6e, 0x2f, 0x32, 0x30, 0x30, 0x39, /* |Jan/2009| */ + 0x20, 0x43, 0x68, 0x61, 0x6e, 0x63, 0x65, 0x6c, /* | Chancel| */ + 0x6c, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x20, 0x62, /* |lor on b| */ + 0x72, 0x69, 0x6e, 0x6b, 0x20, 0x6f, 0x66, 0x20, /* |rink of | */ + 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x62, /* |second b| */ + 0x61, 0x69, 0x6c, 0x6f, 0x75, 0x74, 0x20, 0x66, /* |ailout f| */ + 0x6f, 0x72, 0x20, 0x62, 0x61, 0x6e, 0x6b, 0x73, /* |or banks| */ + 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, /* |........| */ + 0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, /* |*....CA.| */ + 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, /* |g....UH'| */ + 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, /* |.g..q0..| */ + 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, /* |\..(.9..| */ + 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, /* |yb...a..| */ + 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, /* |I..?L.8.| */ + 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, /* |.U......| */ + 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, /* |\8M....W| */ + 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, /* |.Lp+k.._|*/ + 0xac, 0x00, 0x00, 0x00, 0x00, /* |.....| */ +} diff --git a/params.go b/params.go index 10c612de..fb0c4908 100644 --- a/params.go +++ b/params.go @@ -30,6 +30,10 @@ var ( // can have for the test network (version 3). It is the value // 2^224 - 1. testNet3PowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne) + + // simNetPowLimit is the highest proof of work value a Bitcoin block + // can have for the simulation test network. It is the value 2^255 - 1. + simNetPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne) ) // Checkpoint identifies a known good point in the block chain. Using @@ -220,6 +224,49 @@ var TestNet3Params = Params{ PrivateKeyID: 0xef, } +// SimNetParams defines the network parameters for the simulation test Bitcoin +// network. This network is similar to the normal test network except it is +// intended for private use within a group of individuals doing simulation +// testing. The functionality is intended to differ in that the only nodes +// which are specifically specified are used to create the network rather than +// following normal discovery rules. This is important as otherwise it would +// just turn into another public testnet. +var SimNetParams = Params{ + Name: "simnet", + Net: btcwire.SimNet, + DefaultPort: "18555", + + // Chain parameters + GenesisBlock: &simNetGenesisBlock, + GenesisHash: &simNetGenesisHash, + PowLimit: simNetPowLimit, + PowLimitBits: 0x207fffff, + SubsidyHalvingInterval: 150, + ResetMinDifficulty: true, + + // Checkpoints ordered from oldest to newest. + Checkpoints: nil, + + // Reject version 1 blocks once a majority of the network has upgraded. + // 75% (75 / 100) + BlockV1RejectNumRequired: 75, + BlockV1RejectNumToCheck: 100, + + // Ensure coinbase starts with serialized block heights for version 2 + // blocks or newer once a majority of the network has upgraded. + // 51% (51 / 100) + CoinbaseBlockHeightNumRequired: 51, + CoinbaseBlockHeightNumToCheck: 100, + + // Mempool parameters + RelayNonStdTxs: true, + + // Encoding magics + PubKeyHashAddrID: 0x3f, // starts with S + ScriptHashAddrID: 0x7b, // starts with s + PrivateKeyID: 0x64, // starts with 4 (uncompressed) or F (compressed) +} + var ( // ErrDuplicateNet describes an error where the parameters for a Bitcoin // network could not be set due to the network already being a standard @@ -233,11 +280,13 @@ var ( pubKeyHashAddrIDs = map[byte]struct{}{ MainNetParams.PubKeyHashAddrID: struct{}{}, TestNet3Params.PubKeyHashAddrID: struct{}{}, // shared with regtest + SimNetParams.PubKeyHashAddrID: struct{}{}, } scriptHashAddrIDs = map[byte]struct{}{ MainNetParams.ScriptHashAddrID: struct{}{}, TestNet3Params.ScriptHashAddrID: struct{}{}, // shared with regtest + SimNetParams.ScriptHashAddrID: struct{}{}, } ) From a3091847b36199d1d543ca341f8402c9ae67c5ed Mon Sep 17 00:00:00 2001 From: GeertJohan Date: Fri, 13 Jun 2014 01:00:59 +0200 Subject: [PATCH 11/36] Fix adding wrong HashAddrID to pubKeyHashAddrIDs --- params.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/params.go b/params.go index fb0c4908..e3f30df9 100644 --- a/params.go +++ b/params.go @@ -302,7 +302,7 @@ func Register(params *Params) error { return ErrDuplicateNet } registeredNets[params.Net] = struct{}{} - pubKeyHashAddrIDs[params.ScriptHashAddrID] = struct{}{} + pubKeyHashAddrIDs[params.PubKeyHashAddrID] = struct{}{} scriptHashAddrIDs[params.ScriptHashAddrID] = struct{}{} return nil } From c71988f6685ad9fb9ee048c020cf8759eace21ca Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 12 Jun 2014 18:14:13 -0500 Subject: [PATCH 12/36] Add comments for prefixes the encoding IDs map to. --- params.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/params.go b/params.go index e3f30df9..76708370 100644 --- a/params.go +++ b/params.go @@ -135,9 +135,9 @@ var MainNetParams = Params{ RelayNonStdTxs: false, // Encoding magics - PubKeyHashAddrID: 0x00, - ScriptHashAddrID: 0x05, - PrivateKeyID: 0x80, + PubKeyHashAddrID: 0x00, // starts with 1 + ScriptHashAddrID: 0x05, // starts with 3 + PrivateKeyID: 0x80, // starts with 5 (uncompressed) or K (compressed) } // RegressionNetParams defines the network parameters for the regression test @@ -176,9 +176,9 @@ var RegressionNetParams = Params{ RelayNonStdTxs: true, // Encoding magics - PubKeyHashAddrID: 0x6f, - ScriptHashAddrID: 0xc4, - PrivateKeyID: 0xef, + PubKeyHashAddrID: 0x6f, // starts with m or n + ScriptHashAddrID: 0xc4, // starts with 2 + PrivateKeyID: 0xef, // starts with 9 (uncompressed) or c (compressed) } // TestNet3Params defines the network parameters for the test Bitcoin network @@ -219,9 +219,9 @@ var TestNet3Params = Params{ RelayNonStdTxs: true, // Encoding magics - PubKeyHashAddrID: 0x6f, - ScriptHashAddrID: 0xc4, - PrivateKeyID: 0xef, + PubKeyHashAddrID: 0x6f, // starts with m or n + ScriptHashAddrID: 0xc4, // starts with 2 + PrivateKeyID: 0xef, // starts with 9 (uncompressed) or c (compressed) } // SimNetParams defines the network parameters for the simulation test Bitcoin From 0f23236f43173726828adcb3fd518c08d56edbd2 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 12 Jun 2014 18:17:56 -0500 Subject: [PATCH 13/36] Make subsidy halving interval for simnet 210000. This value matches testnet and mainnet and makes more sense because it allows way more coins to be generated which is useful during simulation testing. NOTE: this will invalidate existing simnet chains, but since they are only intended to be short lived for the duration of a simulation test, this is a non-issue. --- params.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/params.go b/params.go index 76708370..caf2d672 100644 --- a/params.go +++ b/params.go @@ -241,7 +241,7 @@ var SimNetParams = Params{ GenesisHash: &simNetGenesisHash, PowLimit: simNetPowLimit, PowLimitBits: 0x207fffff, - SubsidyHalvingInterval: 150, + SubsidyHalvingInterval: 210000, ResetMinDifficulty: true, // Checkpoints ordered from oldest to newest. From 3cbba55822e556488b868c34fffe764bd889b3b1 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 12 Jun 2014 18:37:35 -0500 Subject: [PATCH 14/36] Add support for TravisCI. Also add TravisCI build status badge to README.md. --- .travis.yml | 6 ++++++ README.md | 3 +++ 2 files changed, 9 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..2cd23c40 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +language: go +go: + - release + - tip +install: + - go get -d -t -v ./... diff --git a/README.md b/README.md index 7cdd4a84..b28c5ee7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ btcnet ====== +[![Build Status](https://travis-ci.org/conformal/btcnet.png?branch=master)] +(https://travis-ci.org/conformal/btcnet) + Package btcnet defines the network parameters for the three standard Bitcoin networks and provides the ability for callers to define their own custom Bitcoin networks. From acda2044a0752de400c1dc2147ba84cb6cd41ff5 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 12 Jun 2014 18:40:44 -0500 Subject: [PATCH 15/36] Add godoc reference badge to README.md. --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index b28c5ee7..985c2d9c 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,9 @@ func main() { ## Documentation +[![GoDoc](https://godoc.org/github.com/conformal/btcnet?status.png)] +(http://godoc.org/github.com/conformal/btcnet) + Full `go doc` style documentation for the project can be viewed online without installing this package by using the GoDoc site [here](http://godoc.org/github.com/conformal/btcnet). From 41dce5796df08833326a4b67cbcc480b616243dc Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 12 Jun 2014 18:43:31 -0500 Subject: [PATCH 16/36] Update to coveralls.io for test coverage reporting. Also add a test coverage badge to the README.md. --- .travis.yml | 7 +++++++ README.md | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2cd23c40..7759ad70 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,3 +4,10 @@ go: - tip install: - go get -d -t -v ./... + - go get -v code.google.com/p/go.tools/cmd/cover +script: + - go test -v -covermode=count -coverprofile=profile.cov +after_success: + - go get -v github.com/mattn/goveralls + - export PATH=$PATH:$HOME/gopath/bin + - goveralls -coverprofile=profile.cov -service=travis-ci diff --git a/README.md b/README.md index 985c2d9c..1c27f27f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,9 @@ btcnet ====== [![Build Status](https://travis-ci.org/conformal/btcnet.png?branch=master)] -(https://travis-ci.org/conformal/btcnet) +(https://travis-ci.org/conformal/btcnet) [![Coverage Status] +(https://coveralls.io/repos/conformal/btcnet/badge.png?branch=master)] +(https://coveralls.io/r/conformal/btcnet?branch=master) Package btcnet defines the network parameters for the three standard Bitcoin networks and provides the ability for callers to define their own custom From 4bbcede9e0511dbdf37a91f8e51112dfa95d878c Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Fri, 13 Jun 2014 09:06:15 -0500 Subject: [PATCH 17/36] Add tests for network registration. While here, fix a bug found through testing. Register will now return ErrDuplicateNet if the caller attempts to register any of the standard network parameters provided by this package. ok @davecgh --- params.go | 11 +- register_test.go | 281 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 290 insertions(+), 2 deletions(-) create mode 100644 register_test.go diff --git a/params.go b/params.go index caf2d672..b3cec58e 100644 --- a/params.go +++ b/params.go @@ -275,7 +275,12 @@ var ( ) var ( - registeredNets = map[btcwire.BitcoinNet]struct{}{} + registeredNets = map[btcwire.BitcoinNet]struct{}{ + MainNetParams.Net: struct{}{}, + TestNet3Params.Net: struct{}{}, + RegressionNetParams.Net: struct{}{}, + SimNetParams.Net: struct{}{}, + } pubKeyHashAddrIDs = map[byte]struct{}{ MainNetParams.PubKeyHashAddrID: struct{}{}, @@ -291,7 +296,9 @@ var ( ) // Register registers the network parameters for a Bitcoin network. This may -// error with ErrDuplicateNet if the network is already registered. +// error with ErrDuplicateNet if the network is already registered (either +// due to a previous Register call, or the network being one of the default +// networks). // // Network parameters should be registered into this package by a main package // as early as possible. Then, library packages may lookup networks or network diff --git a/register_test.go b/register_test.go new file mode 100644 index 00000000..14873044 --- /dev/null +++ b/register_test.go @@ -0,0 +1,281 @@ +package btcnet_test + +import ( + . "github.com/conformal/btcnet" + "testing" +) + +// Define some of the required parameters for a user-registered +// network. This is necessary to test the registration of and +// lookup of encoding magics from the network. +var mockNetParams = Params{ + Name: "mocknet", + Net: 1<<32 - 1, + PubKeyHashAddrID: 0x9f, + ScriptHashAddrID: 0xf9, +} + +func TestRegister(t *testing.T) { + type registerTest struct { + name string + params *Params + err error + } + type magicTest struct { + magic byte + valid bool + } + + tests := []struct { + name string + register []registerTest + p2pkhMagics []magicTest + p2shMagics []magicTest + }{ + { + name: "default networks", + register: []registerTest{ + { + name: "duplicate mainnet", + params: &MainNetParams, + err: ErrDuplicateNet, + }, + { + name: "duplicate regtest", + params: &RegressionNetParams, + err: ErrDuplicateNet, + }, + { + name: "duplicate testnet3", + params: &TestNet3Params, + err: ErrDuplicateNet, + }, + { + name: "duplicate simnet", + params: &SimNetParams, + err: ErrDuplicateNet, + }, + }, + p2pkhMagics: []magicTest{ + { + magic: MainNetParams.PubKeyHashAddrID, + valid: true, + }, + { + magic: TestNet3Params.PubKeyHashAddrID, + valid: true, + }, + { + magic: RegressionNetParams.PubKeyHashAddrID, + valid: true, + }, + { + magic: SimNetParams.PubKeyHashAddrID, + valid: true, + }, + { + magic: mockNetParams.PubKeyHashAddrID, + valid: false, + }, + { + magic: 0xFF, + valid: false, + }, + }, + p2shMagics: []magicTest{ + { + magic: MainNetParams.ScriptHashAddrID, + valid: true, + }, + { + magic: TestNet3Params.ScriptHashAddrID, + valid: true, + }, + { + magic: RegressionNetParams.ScriptHashAddrID, + valid: true, + }, + { + magic: SimNetParams.ScriptHashAddrID, + valid: true, + }, + { + magic: mockNetParams.ScriptHashAddrID, + valid: false, + }, + { + magic: 0xFF, + valid: false, + }, + }, + }, + { + name: "register mocknet", + register: []registerTest{ + { + name: "mocknet", + params: &mockNetParams, + err: nil, + }, + }, + p2pkhMagics: []magicTest{ + { + magic: MainNetParams.PubKeyHashAddrID, + valid: true, + }, + { + magic: TestNet3Params.PubKeyHashAddrID, + valid: true, + }, + { + magic: RegressionNetParams.PubKeyHashAddrID, + valid: true, + }, + { + magic: SimNetParams.PubKeyHashAddrID, + valid: true, + }, + { + magic: mockNetParams.PubKeyHashAddrID, + valid: true, + }, + { + magic: 0xFF, + valid: false, + }, + }, + p2shMagics: []magicTest{ + { + magic: MainNetParams.ScriptHashAddrID, + valid: true, + }, + { + magic: TestNet3Params.ScriptHashAddrID, + valid: true, + }, + { + magic: RegressionNetParams.ScriptHashAddrID, + valid: true, + }, + { + magic: SimNetParams.ScriptHashAddrID, + valid: true, + }, + { + magic: mockNetParams.ScriptHashAddrID, + valid: true, + }, + { + magic: 0xFF, + valid: false, + }, + }, + }, + { + name: "more duplicates", + register: []registerTest{ + { + name: "duplicate mainnet", + params: &MainNetParams, + err: ErrDuplicateNet, + }, + { + name: "duplicate regtest", + params: &RegressionNetParams, + err: ErrDuplicateNet, + }, + { + name: "duplicate testnet3", + params: &TestNet3Params, + err: ErrDuplicateNet, + }, + { + name: "duplicate simnet", + params: &SimNetParams, + err: ErrDuplicateNet, + }, + { + name: "duplicate mocknet", + params: &mockNetParams, + err: ErrDuplicateNet, + }, + }, + p2pkhMagics: []magicTest{ + { + magic: MainNetParams.PubKeyHashAddrID, + valid: true, + }, + { + magic: TestNet3Params.PubKeyHashAddrID, + valid: true, + }, + { + magic: RegressionNetParams.PubKeyHashAddrID, + valid: true, + }, + { + magic: SimNetParams.PubKeyHashAddrID, + valid: true, + }, + { + magic: mockNetParams.PubKeyHashAddrID, + valid: true, + }, + { + magic: 0xFF, + valid: false, + }, + }, + p2shMagics: []magicTest{ + { + magic: MainNetParams.ScriptHashAddrID, + valid: true, + }, + { + magic: TestNet3Params.ScriptHashAddrID, + valid: true, + }, + { + magic: RegressionNetParams.ScriptHashAddrID, + valid: true, + }, + { + magic: SimNetParams.ScriptHashAddrID, + valid: true, + }, + { + magic: mockNetParams.ScriptHashAddrID, + valid: true, + }, + { + magic: 0xFF, + valid: false, + }, + }, + }, + } + + for _, test := range tests { + for _, regTest := range test.register { + err := Register(regTest.params) + if err != regTest.err { + t.Errorf("%s:%s: Registered network with unexpected error: got %v expected %v", + test.name, regTest.name, err, regTest.err) + } + } + for i, magTest := range test.p2pkhMagics { + valid := IsPubKeyHashAddrID(magTest.magic) + if valid != magTest.valid { + t.Errorf("%s: P2PKH magic %d valid mismatch: got %v expected %v", + test.name, i, valid, magTest.valid) + } + } + for i, magTest := range test.p2shMagics { + valid := IsScriptHashAddrID(magTest.magic) + if valid != magTest.valid { + t.Errorf("%s: P2SH magic %d valid mismatch: got %v expected %v", + test.name, i, valid, magTest.valid) + } + } + } +} From 6d79aa5ff12944a90fd7a9658741d0b9b2ec549e Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Fri, 13 Jun 2014 09:46:31 -0500 Subject: [PATCH 18/36] Test panic for converting invalid sha strings. ok @davecgh --- internal_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 internal_test.go diff --git a/internal_test.go b/internal_test.go new file mode 100644 index 00000000..bb470dcc --- /dev/null +++ b/internal_test.go @@ -0,0 +1,14 @@ +package btcnet + +import ( + "testing" +) + +func TestInvalidShaStr(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Errorf("Expected panic for invalid sha string, got nil") + } + }() + newShaHashFromStr("banana") +} From d7e4789eda0bc13610d9a63350b97198cc476301 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 2 Jul 2014 19:42:07 -0500 Subject: [PATCH 19/36] goimports -w . --- genesis.go | 3 ++- genesis_test.go | 3 ++- register_test.go | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/genesis.go b/genesis.go index 135d0f28..e3b6da18 100644 --- a/genesis.go +++ b/genesis.go @@ -5,8 +5,9 @@ package btcnet import ( - "github.com/conformal/btcwire" "time" + + "github.com/conformal/btcwire" ) // genesisCoinbaseTx is the coinbase transaction for the genesis blocks for diff --git a/genesis_test.go b/genesis_test.go index fd791ed7..cb4294e8 100644 --- a/genesis_test.go +++ b/genesis_test.go @@ -6,9 +6,10 @@ package btcnet_test import ( "bytes" + "testing" + "github.com/conformal/btcnet" "github.com/davecgh/go-spew/spew" - "testing" ) // TestGenesisBlock tests the genesis block of the main network for validity by diff --git a/register_test.go b/register_test.go index 14873044..f54ff3cc 100644 --- a/register_test.go +++ b/register_test.go @@ -1,8 +1,9 @@ package btcnet_test import ( - . "github.com/conformal/btcnet" "testing" + + . "github.com/conformal/btcnet" ) // Define some of the required parameters for a user-registered From 0994bcf4b2ef5f56a2493c15281c156c26dabf6c Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 18 Jul 2014 18:54:09 -0500 Subject: [PATCH 20/36] Add priv and public key bytes for HD wallet keys. This commit introduces an HDPrivateKeyID and HDPublicKeyID field to the params struct which are used by hierarchical deterministic extended keys as defined by BIP0032. In addition, a new function named HDPrivateKeyToPublicKeyID has been added to allow the caller to get the associated public key ID given a private key ID. The tests have also been updated to maintain 100% test coverage. ok @jrick --- params.go | 61 +++++++++++++++++++++++++++--- register_test.go | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+), 5 deletions(-) diff --git a/params.go b/params.go index b3cec58e..8b72cd01 100644 --- a/params.go +++ b/params.go @@ -80,10 +80,14 @@ type Params struct { // Mempool parameters RelayNonStdTxs bool - // Encoding magics + // Address encoding magics PubKeyHashAddrID byte // First byte of a P2PKH address ScriptHashAddrID byte // First byte of a P2SH address PrivateKeyID byte // First byte of a WIF private key + + // BIP32 hierarchical deterministic extended key magics + HDPrivateKeyID [4]byte + HDPublicKeyID [4]byte } // MainNetParams defines the network parameters for the main Bitcoin network. @@ -134,10 +138,14 @@ var MainNetParams = Params{ // Mempool parameters RelayNonStdTxs: false, - // Encoding magics + // Address encoding magics PubKeyHashAddrID: 0x00, // starts with 1 ScriptHashAddrID: 0x05, // starts with 3 PrivateKeyID: 0x80, // starts with 5 (uncompressed) or K (compressed) + + // BIP32 hierarchical deterministic extended key magics + HDPrivateKeyID: [4]byte{0x04, 0x88, 0xad, 0xe4}, // starts with xprv + HDPublicKeyID: [4]byte{0x04, 0x88, 0xb2, 0x1e}, // starts with xpub } // RegressionNetParams defines the network parameters for the regression test @@ -175,10 +183,14 @@ var RegressionNetParams = Params{ // Mempool parameters RelayNonStdTxs: true, - // Encoding magics + // Address encoding magics PubKeyHashAddrID: 0x6f, // starts with m or n ScriptHashAddrID: 0xc4, // starts with 2 PrivateKeyID: 0xef, // starts with 9 (uncompressed) or c (compressed) + + // BIP32 hierarchical deterministic extended key magics + HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94}, // starts with tprv + HDPublicKeyID: [4]byte{0x04, 0x35, 0x87, 0xcf}, // starts with tpub } // TestNet3Params defines the network parameters for the test Bitcoin network @@ -218,10 +230,14 @@ var TestNet3Params = Params{ // Mempool parameters RelayNonStdTxs: true, - // Encoding magics + // Address encoding magics PubKeyHashAddrID: 0x6f, // starts with m or n ScriptHashAddrID: 0xc4, // starts with 2 PrivateKeyID: 0xef, // starts with 9 (uncompressed) or c (compressed) + + // BIP32 hierarchical deterministic extended key magics + HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94}, // starts with tprv + HDPublicKeyID: [4]byte{0x04, 0x35, 0x87, 0xcf}, // starts with tpub } // SimNetParams defines the network parameters for the simulation test Bitcoin @@ -261,10 +277,14 @@ var SimNetParams = Params{ // Mempool parameters RelayNonStdTxs: true, - // Encoding magics + // Address encoding magics PubKeyHashAddrID: 0x3f, // starts with S ScriptHashAddrID: 0x7b, // starts with s PrivateKeyID: 0x64, // starts with 4 (uncompressed) or F (compressed) + + // BIP32 hierarchical deterministic extended key magics + HDPrivateKeyID: [4]byte{0x04, 0x20, 0xb9, 0x00}, // starts with sprv + HDPublicKeyID: [4]byte{0x04, 0x20, 0xbd, 0x3a}, // starts with spub } var ( @@ -272,6 +292,11 @@ var ( // network could not be set due to the network already being a standard // network or previously-registered into this package. ErrDuplicateNet = errors.New("duplicate Bitcoin network") + + // ErrUnknownHDKeyID describes an error where the provided id which + // is intended to identify the network for a hierarchical deterministic + // private extended key is not registered. + ErrUnknownHDKeyID = errors.New("unknown hd private extended key bytes") ) var ( @@ -293,6 +318,13 @@ var ( TestNet3Params.ScriptHashAddrID: struct{}{}, // shared with regtest SimNetParams.ScriptHashAddrID: struct{}{}, } + + // Testnet is shared with regtest. + hdPrivToPubKeyIDs = map[[4]byte][]byte{ + MainNetParams.HDPrivateKeyID: MainNetParams.HDPublicKeyID[:], + TestNet3Params.HDPrivateKeyID: TestNet3Params.HDPublicKeyID[:], + SimNetParams.HDPrivateKeyID: SimNetParams.HDPublicKeyID[:], + } ) // Register registers the network parameters for a Bitcoin network. This may @@ -311,6 +343,7 @@ func Register(params *Params) error { registeredNets[params.Net] = struct{}{} pubKeyHashAddrIDs[params.PubKeyHashAddrID] = struct{}{} scriptHashAddrIDs[params.ScriptHashAddrID] = struct{}{} + hdPrivToPubKeyIDs[params.HDPrivateKeyID] = params.HDPublicKeyID[:] return nil } @@ -336,6 +369,24 @@ func IsScriptHashAddrID(id byte) bool { return ok } +// HDPrivateKeyToPublicKeyID accepts a private hierarchical deterministic +// extended key id and returns the associated public key id. When the provided +// id is not registered, the ErrUnknownHDKeyID error will be returned. +func HDPrivateKeyToPublicKeyID(id []byte) ([]byte, error) { + if len(id) != 4 { + return nil, ErrUnknownHDKeyID + } + + var key [4]byte + copy(key[:], id) + pubBytes, ok := hdPrivToPubKeyIDs[key] + if !ok { + return nil, ErrUnknownHDKeyID + } + + return pubBytes, nil +} + // newShaHashFromStr converts the passed big-endian hex string into a // btcwire.ShaHash. It only differs from the one available in btcwire in that // it panics on an error since it will only (and must only) be called with diff --git a/register_test.go b/register_test.go index f54ff3cc..b3e57a9a 100644 --- a/register_test.go +++ b/register_test.go @@ -1,6 +1,8 @@ package btcnet_test import ( + "bytes" + "reflect" "testing" . "github.com/conformal/btcnet" @@ -14,6 +16,8 @@ var mockNetParams = Params{ Net: 1<<32 - 1, PubKeyHashAddrID: 0x9f, ScriptHashAddrID: 0xf9, + HDPrivateKeyID: [4]byte{0x01, 0x02, 0x03, 0x04}, + HDPublicKeyID: [4]byte{0x05, 0x06, 0x07, 0x08}, } func TestRegister(t *testing.T) { @@ -26,12 +30,18 @@ func TestRegister(t *testing.T) { magic byte valid bool } + type hdTest struct { + priv []byte + want []byte + err error + } tests := []struct { name string register []registerTest p2pkhMagics []magicTest p2shMagics []magicTest + hdMagics []hdTest }{ { name: "default networks", @@ -109,6 +119,40 @@ func TestRegister(t *testing.T) { valid: false, }, }, + hdMagics: []hdTest{ + { + priv: MainNetParams.HDPrivateKeyID[:], + want: MainNetParams.HDPublicKeyID[:], + err: nil, + }, + { + priv: TestNet3Params.HDPrivateKeyID[:], + want: TestNet3Params.HDPublicKeyID[:], + err: nil, + }, + { + priv: RegressionNetParams.HDPrivateKeyID[:], + want: RegressionNetParams.HDPublicKeyID[:], + err: nil, + }, + { + priv: SimNetParams.HDPrivateKeyID[:], + want: SimNetParams.HDPublicKeyID[:], + err: nil, + }, + { + priv: mockNetParams.HDPrivateKeyID[:], + err: ErrUnknownHDKeyID, + }, + { + priv: []byte{0xff, 0xff, 0xff, 0xff}, + err: ErrUnknownHDKeyID, + }, + { + priv: []byte{0xff}, + err: ErrUnknownHDKeyID, + }, + }, }, { name: "register mocknet", @@ -171,6 +215,13 @@ func TestRegister(t *testing.T) { valid: false, }, }, + hdMagics: []hdTest{ + { + priv: mockNetParams.HDPrivateKeyID[:], + want: mockNetParams.HDPublicKeyID[:], + err: nil, + }, + }, }, { name: "more duplicates", @@ -253,6 +304,41 @@ func TestRegister(t *testing.T) { valid: false, }, }, + hdMagics: []hdTest{ + { + priv: MainNetParams.HDPrivateKeyID[:], + want: MainNetParams.HDPublicKeyID[:], + err: nil, + }, + { + priv: TestNet3Params.HDPrivateKeyID[:], + want: TestNet3Params.HDPublicKeyID[:], + err: nil, + }, + { + priv: RegressionNetParams.HDPrivateKeyID[:], + want: RegressionNetParams.HDPublicKeyID[:], + err: nil, + }, + { + priv: SimNetParams.HDPrivateKeyID[:], + want: SimNetParams.HDPublicKeyID[:], + err: nil, + }, + { + priv: mockNetParams.HDPrivateKeyID[:], + want: mockNetParams.HDPublicKeyID[:], + err: nil, + }, + { + priv: []byte{0xff, 0xff, 0xff, 0xff}, + err: ErrUnknownHDKeyID, + }, + { + priv: []byte{0xff}, + err: ErrUnknownHDKeyID, + }, + }, }, } @@ -278,5 +364,17 @@ func TestRegister(t *testing.T) { test.name, i, valid, magTest.valid) } } + for i, magTest := range test.hdMagics { + pubKey, err := HDPrivateKeyToPublicKeyID(magTest.priv[:]) + if !reflect.DeepEqual(err, magTest.err) { + t.Errorf("%s: HD magic %d mismatched error: got %v expected %v ", + test.name, i, err, magTest.err) + continue + } + if magTest.err == nil && !bytes.Equal(pubKey, magTest.want[:]) { + t.Errorf("%s: HD magic %d private and public mismatch: got %v expected %v ", + test.name, i, pubKey, magTest.want[:]) + } + } } } From 0932dfeb1214856268c7984d017b4555342ed05c Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Tue, 12 Aug 2014 10:37:58 -0500 Subject: [PATCH 21/36] WIP coin type --- params.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/params.go b/params.go index 8b72cd01..f8a84b33 100644 --- a/params.go +++ b/params.go @@ -88,6 +88,10 @@ type Params struct { // BIP32 hierarchical deterministic extended key magics HDPrivateKeyID [4]byte HDPublicKeyID [4]byte + + // BIP44 coin type used in the hierarchical deterministic path for + // address generation. + HDCoinType uint32 } // MainNetParams defines the network parameters for the main Bitcoin network. @@ -146,6 +150,10 @@ var MainNetParams = Params{ // BIP32 hierarchical deterministic extended key magics HDPrivateKeyID: [4]byte{0x04, 0x88, 0xad, 0xe4}, // starts with xprv HDPublicKeyID: [4]byte{0x04, 0x88, 0xb2, 0x1e}, // starts with xpub + + // BIP44 coin type used in the hierarchical deterministic path for + // address generation. + HDCoinType: 0, } // RegressionNetParams defines the network parameters for the regression test @@ -191,6 +199,10 @@ var RegressionNetParams = Params{ // BIP32 hierarchical deterministic extended key magics HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94}, // starts with tprv HDPublicKeyID: [4]byte{0x04, 0x35, 0x87, 0xcf}, // starts with tpub + + // BIP44 coin type used in the hierarchical deterministic path for + // address generation. + HDCoinType: 1, } // TestNet3Params defines the network parameters for the test Bitcoin network @@ -238,6 +250,10 @@ var TestNet3Params = Params{ // BIP32 hierarchical deterministic extended key magics HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94}, // starts with tprv HDPublicKeyID: [4]byte{0x04, 0x35, 0x87, 0xcf}, // starts with tpub + + // BIP44 coin type used in the hierarchical deterministic path for + // address generation. + HDCoinType: 1, } // SimNetParams defines the network parameters for the simulation test Bitcoin @@ -285,6 +301,10 @@ var SimNetParams = Params{ // BIP32 hierarchical deterministic extended key magics HDPrivateKeyID: [4]byte{0x04, 0x20, 0xb9, 0x00}, // starts with sprv HDPublicKeyID: [4]byte{0x04, 0x20, 0xbd, 0x3a}, // starts with spub + + // BIP44 coin type used in the hierarchical deterministic path for + // address generation. + HDCoinType: 115, // ASCII for s } var ( From ecacc006cd68510d1b7f0a88e5e2cf73885d124d Mon Sep 17 00:00:00 2001 From: "John C. Vernaleo" Date: Mon, 8 Sep 2014 09:07:50 -0400 Subject: [PATCH 22/36] Add go vet and golint to TravisCI. --- .travis.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7759ad70..b32fe757 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,9 +5,14 @@ go: install: - go get -d -t -v ./... - go get -v code.google.com/p/go.tools/cmd/cover + - go get code.google.com/p/go.tools/cmd/vet + - go get github.com/GeertJohan/fgt + - go get github.com/golang/lint/golint script: + - export PATH=$PATH:$HOME/gopath/bin + - go vet *.go + - fgt golint *.go - go test -v -covermode=count -coverprofile=profile.cov after_success: - go get -v github.com/mattn/goveralls - - export PATH=$PATH:$HOME/gopath/bin - goveralls -coverprofile=profile.cov -service=travis-ci From 0631a00d91c0f38f4e80f30e411d599a640b296d Mon Sep 17 00:00:00 2001 From: "John C. Vernaleo" Date: Mon, 8 Sep 2014 09:16:09 -0400 Subject: [PATCH 23/36] Hold off on go vet for now. --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index b32fe757..dba5bab4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,12 +5,10 @@ go: install: - go get -d -t -v ./... - go get -v code.google.com/p/go.tools/cmd/cover - - go get code.google.com/p/go.tools/cmd/vet - go get github.com/GeertJohan/fgt - go get github.com/golang/lint/golint script: - export PATH=$PATH:$HOME/gopath/bin - - go vet *.go - fgt golint *.go - go test -v -covermode=count -coverprofile=profile.cov after_success: From 41c981348e7d33c2d4bc05d146cc55c875d5fbd2 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 12 Sep 2014 14:04:03 -0500 Subject: [PATCH 24/36] Use byte literals to make go vet happy. The go vet command complains about untagged struct initializers when defining a ShaHash directly. This seems to be a limitation where go vet does not exclude the warning for types which are a constant size byte array like it does for normal constant size byte array definition. This commit simply modifies the code to use a constant definition cast to a ShaHash to overcome the limitation of go vet. --- genesis.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/genesis.go b/genesis.go index e3b6da18..f1f8117e 100644 --- a/genesis.go +++ b/genesis.go @@ -56,21 +56,21 @@ var genesisCoinbaseTx = btcwire.MsgTx{ // genesisHash is the hash of the first block in the block chain for the main // network (genesis block). -var genesisHash = btcwire.ShaHash{ +var genesisHash = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy. 0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72, 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f, 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c, 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, -} +}) // genesisMerkleRoot is the hash of the first transaction in the genesis block // for the main network. -var genesisMerkleRoot = btcwire.ShaHash{ +var genesisMerkleRoot = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy. 0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, -} +}) // genesisBlock defines the genesis block of the block chain which serves as the // public transaction ledger for the main network. @@ -88,12 +88,12 @@ var genesisBlock = btcwire.MsgBlock{ // regTestGenesisHash is the hash of the first block in the block chain for the // regression test network (genesis block). -var regTestGenesisHash = btcwire.ShaHash{ +var regTestGenesisHash = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy. 0x06, 0x22, 0x6e, 0x46, 0x11, 0x1a, 0x0b, 0x59, 0xca, 0xaf, 0x12, 0x60, 0x43, 0xeb, 0x5b, 0xbf, 0x28, 0xc3, 0x4f, 0x3a, 0x5e, 0x33, 0x2a, 0x1f, 0xc7, 0xb2, 0xb7, 0x3c, 0xf1, 0x88, 0x91, 0x0f, -} +}) // regTestGenesisMerkleRoot is the hash of the first transaction in the genesis // block for the regression test network. It is the same as the merkle root for @@ -116,12 +116,12 @@ var regTestGenesisBlock = btcwire.MsgBlock{ // testNet3GenesisHash is the hash of the first block in the block chain for the // test network (version 3). -var testNet3GenesisHash = btcwire.ShaHash{ +var testNet3GenesisHash = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy. 0x43, 0x49, 0x7f, 0xd7, 0xf8, 0x26, 0x95, 0x71, 0x08, 0xf4, 0xa3, 0x0f, 0xd9, 0xce, 0xc3, 0xae, 0xba, 0x79, 0x97, 0x20, 0x84, 0xe9, 0x0e, 0xad, 0x01, 0xea, 0x33, 0x09, 0x00, 0x00, 0x00, 0x00, -} +}) // testNet3GenesisMerkleRoot is the hash of the first transaction in the genesis // block for the test network (version 3). It is the same as the merkle root @@ -144,12 +144,12 @@ var testNet3GenesisBlock = btcwire.MsgBlock{ // simNetGenesisHash is the hash of the first block in the block chain for the // simulation test network. -var simNetGenesisHash = btcwire.ShaHash{ +var simNetGenesisHash = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy. 0xf6, 0x7a, 0xd7, 0x69, 0x5d, 0x9b, 0x66, 0x2a, 0x72, 0xff, 0x3d, 0x8e, 0xdb, 0xbb, 0x2d, 0xe0, 0xbf, 0xa6, 0x7b, 0x13, 0x97, 0x4b, 0xb9, 0x91, 0x0d, 0x11, 0x6d, 0x5c, 0xbd, 0x86, 0x3e, 0x68, -} +}) // simNetGenesisMerkleRoot is the hash of the first transaction in the genesis // block for the simulation test network. It is the same as the merkle root for From 8e96f2cf55c039ee59c08ee54e6d25ee7b51b25b Mon Sep 17 00:00:00 2001 From: "John C. Vernaleo" Date: Fri, 12 Sep 2014 15:11:48 -0400 Subject: [PATCH 25/36] Add go vet back and cleanup travis file. --- .travis.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index dba5bab4..afd18be6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,11 +5,13 @@ go: install: - go get -d -t -v ./... - go get -v code.google.com/p/go.tools/cmd/cover - - go get github.com/GeertJohan/fgt - - go get github.com/golang/lint/golint + - go get -v code.google.com/p/go.tools/cmd/vet + - go get -v github.com/GeertJohan/fgt + - go get -v github.com/golang/lint/golint script: - export PATH=$PATH:$HOME/gopath/bin - - fgt golint *.go + - go vet + - fgt golint . - go test -v -covermode=count -coverprofile=profile.cov after_success: - go get -v github.com/mattn/goveralls From 3bbe8ff0ca63f8ce94982f7624f825f84ac10b0a Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 18 Sep 2014 19:02:59 -0500 Subject: [PATCH 26/36] Add checkpoint at block height 319400. --- params.go | 1 + 1 file changed, 1 insertion(+) diff --git a/params.go b/params.go index f8a84b33..c509c0ca 100644 --- a/params.go +++ b/params.go @@ -124,6 +124,7 @@ var MainNetParams = Params{ {267300, newShaHashFromStr("000000000000000a83fbd660e918f218bf37edd92b748ad940483c7c116179ac")}, {279000, newShaHashFromStr("0000000000000001ae8c72a0b0c301f67e3afca10e819efa9041e458e9bd7e40")}, {300255, newShaHashFromStr("0000000000000000162804527c6e9b9f0563a280525f9d08c12041def0a0f3b2")}, + {319400, newShaHashFromStr("000000000000000021c6052e9becade189495d1c539aa37c58917305fd15f13b")}, }, // Reject version 1 blocks once a majority of the network has upgraded. From 3eeb51ab54a3f70a2bbfd1de68ffd714b4cd7e28 Mon Sep 17 00:00:00 2001 From: Jonathan Gillham Date: Wed, 1 Oct 2014 13:48:07 +0100 Subject: [PATCH 27/36] Changed TxIn.PreviousOutpoint to TxIn.PreviousOutPoint after btcwire API change. --- genesis.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/genesis.go b/genesis.go index f1f8117e..e49ac673 100644 --- a/genesis.go +++ b/genesis.go @@ -16,7 +16,7 @@ var genesisCoinbaseTx = btcwire.MsgTx{ Version: 1, TxIn: []*btcwire.TxIn{ { - PreviousOutpoint: btcwire.OutPoint{ + PreviousOutPoint: btcwire.OutPoint{ Hash: btcwire.ShaHash{}, Index: 0xffffffff, }, From 2f74570188575e9dc0e138f6f2ba2a8ffa03d058 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Mon, 22 Dec 2014 21:43:32 -0600 Subject: [PATCH 28/36] Update TravisCI to goclean script. Also, update to use the new container-based builds. --- .travis.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index afd18be6..7e9d7381 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,17 +2,17 @@ language: go go: - release - tip +sudo: false +before_install: + - gocleandeps=c16c849abae90c23419d + - git clone https://gist.github.com/$gocleandeps.git + - goclean=71d0380287747d956a26 + - git clone https://gist.github.com/$goclean.git install: - go get -d -t -v ./... - - go get -v code.google.com/p/go.tools/cmd/cover - - go get -v code.google.com/p/go.tools/cmd/vet - - go get -v github.com/GeertJohan/fgt - - go get -v github.com/golang/lint/golint + - bash $gocleandeps/gocleandeps.sh script: - export PATH=$PATH:$HOME/gopath/bin - - go vet - - fgt golint . - - go test -v -covermode=count -coverprofile=profile.cov + - bash $goclean/goclean.sh after_success: - - go get -v github.com/mattn/goveralls - goveralls -coverprofile=profile.cov -service=travis-ci From 68242fbba779a38c51872a6cefd1083abb6682aa Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Mon, 22 Dec 2014 22:02:26 -0600 Subject: [PATCH 29/36] Update badges in README.md to SVG. Also, while here: - Add a license badge - Add link to the copyfree website for the license for consistency --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1c27f27f..b8a90340 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,11 @@ btcnet ====== -[![Build Status](https://travis-ci.org/conformal/btcnet.png?branch=master)] +[![Build Status](http://img.shields.io/travis/conformal/btcnet.svg)] (https://travis-ci.org/conformal/btcnet) [![Coverage Status] -(https://coveralls.io/repos/conformal/btcnet/badge.png?branch=master)] -(https://coveralls.io/r/conformal/btcnet?branch=master) +(https://img.shields.io/coveralls/conformal/btcnet.svg)] +(https://coveralls.io/r/conformal/btcnet?branch=master) [![ISC License] +(http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org) Package btcnet defines the network parameters for the three standard Bitcoin networks and provides the ability for callers to define their own custom @@ -58,7 +59,7 @@ func main() { ## Documentation -[![GoDoc](https://godoc.org/github.com/conformal/btcnet?status.png)] +[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)] (http://godoc.org/github.com/conformal/btcnet) Full `go doc` style documentation for the project can be viewed online without @@ -97,4 +98,5 @@ signature perform the following: ## License -Package btcnet is licensed under the liberal ISC License. +Package btcnet is licensed under the [copyfree](http://copyfree.org) ISC +License. From 320f5befd5a32633c14cb73caf244eb8a1dd0007 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 15 Jan 2015 10:39:54 -0600 Subject: [PATCH 30/36] Update btcutil import paths to new location. --- README.md | 2 +- doc.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b8a90340..8b675b0a 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,8 @@ import ( "fmt" "log" + "github.com/btcsuite/btcutil" "github.com/conformal/btcnet" - "github.com/conformal/btcutil" ) var testnet = flag.Bool("testnet", false, "operate on the testnet Bitcoin network") diff --git a/doc.go b/doc.go index 649d4a17..7fd4a079 100644 --- a/doc.go +++ b/doc.go @@ -27,8 +27,8 @@ // "fmt" // "log" // +// "github.com/btcsuite/btcutil" // "github.com/conformal/btcnet" -// "github.com/conformal/btcutil" // ) // // var testnet = flag.Bool("testnet", false, "operate on the testnet Bitcoin network") From 578e6159944c453622f1ba288be12746748ec6d1 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 16 Jan 2015 15:05:40 -0600 Subject: [PATCH 31/36] Update btcwire import paths to new location. --- genesis.go | 2 +- params.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/genesis.go b/genesis.go index e49ac673..f3756fcc 100644 --- a/genesis.go +++ b/genesis.go @@ -7,7 +7,7 @@ package btcnet import ( "time" - "github.com/conformal/btcwire" + "github.com/btcsuite/btcwire" ) // genesisCoinbaseTx is the coinbase transaction for the genesis blocks for diff --git a/params.go b/params.go index c509c0ca..afb12132 100644 --- a/params.go +++ b/params.go @@ -8,7 +8,7 @@ import ( "errors" "math/big" - "github.com/conformal/btcwire" + "github.com/btcsuite/btcwire" ) // These variables are the chain proof-of-work limit parameters for each default From 5bdb50ece69609e4d93fb171db16f12a92055c12 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 16 Jan 2015 17:23:38 -0600 Subject: [PATCH 32/36] Update btcnet import paths to new location. --- README.md | 10 +++++----- doc.go | 2 +- genesis_test.go | 2 +- register_test.go | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8b675b0a..d45425c9 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ import ( "log" "github.com/btcsuite/btcutil" - "github.com/conformal/btcnet" + "github.com/btcsuite/btcnet" ) var testnet = flag.Bool("testnet", false, "operate on the testnet Bitcoin network") @@ -60,20 +60,20 @@ func main() { ## Documentation [![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)] -(http://godoc.org/github.com/conformal/btcnet) +(http://godoc.org/github.com/btcsuite/btcnet) Full `go doc` style documentation for the project can be viewed online without installing this package by using the GoDoc site -[here](http://godoc.org/github.com/conformal/btcnet). +[here](http://godoc.org/github.com/btcsuite/btcnet). You can also view the documentation locally once the package is installed with the `godoc` tool by running `godoc -http=":6060"` and pointing your browser to -http://localhost:6060/pkg/github.com/conformal/btcnet +http://localhost:6060/pkg/github.com/btcsuite/btcnet ## Installation ```bash -$ go get github.com/conformal/btcnet +$ go get github.com/btcsuite/btcnet ``` ## GPG Verification Key diff --git a/doc.go b/doc.go index 7fd4a079..39ec53b8 100644 --- a/doc.go +++ b/doc.go @@ -28,7 +28,7 @@ // "log" // // "github.com/btcsuite/btcutil" -// "github.com/conformal/btcnet" +// "github.com/btcsuite/btcnet" // ) // // var testnet = flag.Bool("testnet", false, "operate on the testnet Bitcoin network") diff --git a/genesis_test.go b/genesis_test.go index cb4294e8..bf060d69 100644 --- a/genesis_test.go +++ b/genesis_test.go @@ -8,7 +8,7 @@ import ( "bytes" "testing" - "github.com/conformal/btcnet" + "github.com/btcsuite/btcnet" "github.com/davecgh/go-spew/spew" ) diff --git a/register_test.go b/register_test.go index b3e57a9a..2d742620 100644 --- a/register_test.go +++ b/register_test.go @@ -5,7 +5,7 @@ import ( "reflect" "testing" - . "github.com/conformal/btcnet" + . "github.com/btcsuite/btcnet" ) // Define some of the required parameters for a user-registered From 875d8e383cbea77f5caffbcec723f5c879f73d91 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 16 Jan 2015 23:22:13 -0600 Subject: [PATCH 33/36] Update to new location in README.md too. --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d45425c9..60b25d62 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ btcnet ====== -[![Build Status](http://img.shields.io/travis/conformal/btcnet.svg)] -(https://travis-ci.org/conformal/btcnet) [![Coverage Status] -(https://img.shields.io/coveralls/conformal/btcnet.svg)] -(https://coveralls.io/r/conformal/btcnet?branch=master) [![ISC License] +[![Build Status](http://img.shields.io/travis/btcsuite/btcnet.svg)] +(https://travis-ci.org/btcsuite/btcnet) [![Coverage Status] +(https://img.shields.io/coveralls/btcsuite/btcnet.svg)] +(https://coveralls.io/r/btcsuite/btcnet?branch=master) [![ISC License] (http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org) Package btcnet defines the network parameters for the three standard Bitcoin From 612ff7f8132e68381c407e4429d420dd3a054cba Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 30 Jan 2015 16:04:26 -0600 Subject: [PATCH 34/36] Update btcchain import paths to new location. --- params.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/params.go b/params.go index afb12132..4db29b1a 100644 --- a/params.go +++ b/params.go @@ -41,8 +41,8 @@ var ( // and also prevents forks from old blocks. // // Each checkpoint is selected based upon several factors. See the -// documentation for btcchain.IsCheckpointCandidate for details on the selection -// criteria. +// documentation for blockchain.IsCheckpointCandidate for details on the +// selection criteria. type Checkpoint struct { Height int64 Hash *btcwire.ShaHash From 4dd7c939ed4bcf5d68929a296aa638450c8d2220 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 5 Feb 2015 12:58:30 -0600 Subject: [PATCH 35/36] Update btcwire path import paths to new location. --- doc.go | 2 +- genesis.go | 54 +++++++++++++++++++++++++++--------------------------- params.go | 26 +++++++++++++------------- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/doc.go b/doc.go index 39ec53b8..cda9e445 100644 --- a/doc.go +++ b/doc.go @@ -12,7 +12,7 @@ // For library packages, btcnet provides the ability to lookup chain parameters // and encoding magics when passed a *Params. Older APIs not updated to the new // convention of passing a *Params may lookup the parameters for a -// btcwire.BitcoinNet using ParamsForNet, but be aware that this usage is +// wire.BitcoinNet using ParamsForNet, but be aware that this usage is // deprecated and will be removed from btcnet in the future. // // For main packages, a (typically global) var may be assigned the address of diff --git a/genesis.go b/genesis.go index f3756fcc..69021bc6 100644 --- a/genesis.go +++ b/genesis.go @@ -7,17 +7,17 @@ package btcnet import ( "time" - "github.com/btcsuite/btcwire" + "github.com/btcsuite/btcd/wire" ) // genesisCoinbaseTx is the coinbase transaction for the genesis blocks for // the main network, regression test network, and test network (version 3). -var genesisCoinbaseTx = btcwire.MsgTx{ +var genesisCoinbaseTx = wire.MsgTx{ Version: 1, - TxIn: []*btcwire.TxIn{ + TxIn: []*wire.TxIn{ { - PreviousOutPoint: btcwire.OutPoint{ - Hash: btcwire.ShaHash{}, + PreviousOutPoint: wire.OutPoint{ + Hash: wire.ShaHash{}, Index: 0xffffffff, }, SignatureScript: []byte{ @@ -35,7 +35,7 @@ var genesisCoinbaseTx = btcwire.MsgTx{ Sequence: 0xffffffff, }, }, - TxOut: []*btcwire.TxOut{ + TxOut: []*wire.TxOut{ { Value: 0x12a05f200, PkScript: []byte{ @@ -56,7 +56,7 @@ var genesisCoinbaseTx = btcwire.MsgTx{ // genesisHash is the hash of the first block in the block chain for the main // network (genesis block). -var genesisHash = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy. +var genesisHash = wire.ShaHash([wire.HashSize]byte{ // Make go vet happy. 0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72, 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f, 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c, @@ -65,7 +65,7 @@ var genesisHash = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy. // genesisMerkleRoot is the hash of the first transaction in the genesis block // for the main network. -var genesisMerkleRoot = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy. +var genesisMerkleRoot = wire.ShaHash([wire.HashSize]byte{ // Make go vet happy. 0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32, @@ -74,21 +74,21 @@ var genesisMerkleRoot = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet h // genesisBlock defines the genesis block of the block chain which serves as the // public transaction ledger for the main network. -var genesisBlock = btcwire.MsgBlock{ - Header: btcwire.BlockHeader{ +var genesisBlock = wire.MsgBlock{ + Header: wire.BlockHeader{ Version: 1, - PrevBlock: btcwire.ShaHash{}, // 0000000000000000000000000000000000000000000000000000000000000000 + PrevBlock: wire.ShaHash{}, // 0000000000000000000000000000000000000000000000000000000000000000 MerkleRoot: genesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 18:15:05 +0000 UTC Bits: 0x1d00ffff, // 486604799 [00000000ffff0000000000000000000000000000000000000000000000000000] Nonce: 0x7c2bac1d, // 2083236893 }, - Transactions: []*btcwire.MsgTx{&genesisCoinbaseTx}, + Transactions: []*wire.MsgTx{&genesisCoinbaseTx}, } // regTestGenesisHash is the hash of the first block in the block chain for the // regression test network (genesis block). -var regTestGenesisHash = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy. +var regTestGenesisHash = wire.ShaHash([wire.HashSize]byte{ // Make go vet happy. 0x06, 0x22, 0x6e, 0x46, 0x11, 0x1a, 0x0b, 0x59, 0xca, 0xaf, 0x12, 0x60, 0x43, 0xeb, 0x5b, 0xbf, 0x28, 0xc3, 0x4f, 0x3a, 0x5e, 0x33, 0x2a, 0x1f, @@ -102,21 +102,21 @@ var regTestGenesisMerkleRoot = genesisMerkleRoot // regTestGenesisBlock defines the genesis block of the block chain which serves // as the public transaction ledger for the regression test network. -var regTestGenesisBlock = btcwire.MsgBlock{ - Header: btcwire.BlockHeader{ +var regTestGenesisBlock = wire.MsgBlock{ + Header: wire.BlockHeader{ Version: 1, - PrevBlock: btcwire.ShaHash{}, // 0000000000000000000000000000000000000000000000000000000000000000 + PrevBlock: wire.ShaHash{}, // 0000000000000000000000000000000000000000000000000000000000000000 MerkleRoot: regTestGenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b Timestamp: time.Unix(1296688602, 0), // 2011-02-02 23:16:42 +0000 UTC Bits: 0x207fffff, // 545259519 [7fffff0000000000000000000000000000000000000000000000000000000000] Nonce: 2, }, - Transactions: []*btcwire.MsgTx{&genesisCoinbaseTx}, + Transactions: []*wire.MsgTx{&genesisCoinbaseTx}, } // testNet3GenesisHash is the hash of the first block in the block chain for the // test network (version 3). -var testNet3GenesisHash = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy. +var testNet3GenesisHash = wire.ShaHash([wire.HashSize]byte{ // Make go vet happy. 0x43, 0x49, 0x7f, 0xd7, 0xf8, 0x26, 0x95, 0x71, 0x08, 0xf4, 0xa3, 0x0f, 0xd9, 0xce, 0xc3, 0xae, 0xba, 0x79, 0x97, 0x20, 0x84, 0xe9, 0x0e, 0xad, @@ -130,21 +130,21 @@ var testNet3GenesisMerkleRoot = genesisMerkleRoot // testNet3GenesisBlock defines the genesis block of the block chain which // serves as the public transaction ledger for the test network (version 3). -var testNet3GenesisBlock = btcwire.MsgBlock{ - Header: btcwire.BlockHeader{ +var testNet3GenesisBlock = wire.MsgBlock{ + Header: wire.BlockHeader{ Version: 1, - PrevBlock: btcwire.ShaHash{}, // 0000000000000000000000000000000000000000000000000000000000000000 + PrevBlock: wire.ShaHash{}, // 0000000000000000000000000000000000000000000000000000000000000000 MerkleRoot: testNet3GenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b Timestamp: time.Unix(1296688602, 0), // 2011-02-02 23:16:42 +0000 UTC Bits: 0x1d00ffff, // 486604799 [00000000ffff0000000000000000000000000000000000000000000000000000] Nonce: 0x18aea41a, // 414098458 }, - Transactions: []*btcwire.MsgTx{&genesisCoinbaseTx}, + Transactions: []*wire.MsgTx{&genesisCoinbaseTx}, } // simNetGenesisHash is the hash of the first block in the block chain for the // simulation test network. -var simNetGenesisHash = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy. +var simNetGenesisHash = wire.ShaHash([wire.HashSize]byte{ // Make go vet happy. 0xf6, 0x7a, 0xd7, 0x69, 0x5d, 0x9b, 0x66, 0x2a, 0x72, 0xff, 0x3d, 0x8e, 0xdb, 0xbb, 0x2d, 0xe0, 0xbf, 0xa6, 0x7b, 0x13, 0x97, 0x4b, 0xb9, 0x91, @@ -158,14 +158,14 @@ var simNetGenesisMerkleRoot = genesisMerkleRoot // simNetGenesisBlock defines the genesis block of the block chain which serves // as the public transaction ledger for the simulation test network. -var simNetGenesisBlock = btcwire.MsgBlock{ - Header: btcwire.BlockHeader{ +var simNetGenesisBlock = wire.MsgBlock{ + Header: wire.BlockHeader{ Version: 1, - PrevBlock: btcwire.ShaHash{}, // 0000000000000000000000000000000000000000000000000000000000000000 + PrevBlock: wire.ShaHash{}, // 0000000000000000000000000000000000000000000000000000000000000000 MerkleRoot: simNetGenesisMerkleRoot, // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b Timestamp: time.Unix(1401292357, 0), // 2014-05-28 15:52:37 +0000 UTC Bits: 0x207fffff, // 545259519 [7fffff0000000000000000000000000000000000000000000000000000000000] Nonce: 2, }, - Transactions: []*btcwire.MsgTx{&genesisCoinbaseTx}, + Transactions: []*wire.MsgTx{&genesisCoinbaseTx}, } diff --git a/params.go b/params.go index 4db29b1a..752c9e7b 100644 --- a/params.go +++ b/params.go @@ -8,7 +8,7 @@ import ( "errors" "math/big" - "github.com/btcsuite/btcwire" + "github.com/btcsuite/btcd/wire" ) // These variables are the chain proof-of-work limit parameters for each default @@ -45,7 +45,7 @@ var ( // selection criteria. type Checkpoint struct { Height int64 - Hash *btcwire.ShaHash + Hash *wire.ShaHash } // Params defines a Bitcoin network by its parameters. These parameters may be @@ -53,12 +53,12 @@ type Checkpoint struct { // and keys for one network from those intended for use on another network. type Params struct { Name string - Net btcwire.BitcoinNet + Net wire.BitcoinNet DefaultPort string // Chain parameters - GenesisBlock *btcwire.MsgBlock - GenesisHash *btcwire.ShaHash + GenesisBlock *wire.MsgBlock + GenesisHash *wire.ShaHash PowLimit *big.Int PowLimitBits uint32 SubsidyHalvingInterval int32 @@ -97,7 +97,7 @@ type Params struct { // MainNetParams defines the network parameters for the main Bitcoin network. var MainNetParams = Params{ Name: "mainnet", - Net: btcwire.MainNet, + Net: wire.MainNet, DefaultPort: "8333", // Chain parameters @@ -162,7 +162,7 @@ var MainNetParams = Params{ // 3), this network is sometimes simply called "testnet". var RegressionNetParams = Params{ Name: "regtest", - Net: btcwire.TestNet, + Net: wire.TestNet, DefaultPort: "18444", // Chain parameters @@ -211,7 +211,7 @@ var RegressionNetParams = Params{ // network is sometimes simply called "testnet". var TestNet3Params = Params{ Name: "testnet3", - Net: btcwire.TestNet3, + Net: wire.TestNet3, DefaultPort: "18333", // Chain parameters @@ -266,7 +266,7 @@ var TestNet3Params = Params{ // just turn into another public testnet. var SimNetParams = Params{ Name: "simnet", - Net: btcwire.SimNet, + Net: wire.SimNet, DefaultPort: "18555", // Chain parameters @@ -321,7 +321,7 @@ var ( ) var ( - registeredNets = map[btcwire.BitcoinNet]struct{}{ + registeredNets = map[wire.BitcoinNet]struct{}{ MainNetParams.Net: struct{}{}, TestNet3Params.Net: struct{}{}, RegressionNetParams.Net: struct{}{}, @@ -409,11 +409,11 @@ func HDPrivateKeyToPublicKeyID(id []byte) ([]byte, error) { } // newShaHashFromStr converts the passed big-endian hex string into a -// btcwire.ShaHash. It only differs from the one available in btcwire in that +// wire.ShaHash. It only differs from the one available in wire in that // it panics on an error since it will only (and must only) be called with // hard-coded, and therefore known good, hashes. -func newShaHashFromStr(hexStr string) *btcwire.ShaHash { - sha, err := btcwire.NewShaHashFromStr(hexStr) +func newShaHashFromStr(hexStr string) *wire.ShaHash { + sha, err := wire.NewShaHashFromStr(hexStr) if err != nil { // Ordinarily I don't like panics in library code since it // can take applications down without them having a chance to From 40df13819310d10cd86191e9a48f4e9cf877053b Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 5 Feb 2015 21:36:42 -0600 Subject: [PATCH 36/36] Import btcnet repo into chaincfg directory. This commit contains the entire btcnet repository along with several changes needed to move all of the files into the chaincfg directory in order to prepare it for merging. This does NOT update btcd or any of the other packages to use the new location as that will be done separately. - All import paths in the old btcnet test files have been changed to the new location - All references to btcnet as the package name have been changed to chaincfg - The coveralls badge has been removed since it unfortunately doesn't support coverage of sub-packages This is ongoing work toward #214. --- .travis.yml | 18 ---------- LICENSE | 13 ------- README.md => chaincfg/README.md | 36 +++++++++---------- doc.go => chaincfg/doc.go | 22 ++++++------ genesis.go => chaincfg/genesis.go | 2 +- genesis_test.go => chaincfg/genesis_test.go | 36 +++++++++---------- internal_test.go => chaincfg/internal_test.go | 2 +- params.go => chaincfg/params.go | 2 +- register_test.go => chaincfg/register_test.go | 4 +-- 9 files changed, 49 insertions(+), 86 deletions(-) delete mode 100644 .travis.yml delete mode 100644 LICENSE rename README.md => chaincfg/README.md (63%) rename doc.go => chaincfg/doc.go (75%) rename genesis.go => chaincfg/genesis.go (99%) rename genesis_test.go => chaincfg/genesis_test.go (93%) rename internal_test.go => chaincfg/internal_test.go (92%) rename params.go => chaincfg/params.go (99%) rename register_test.go => chaincfg/register_test.go (99%) diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 7e9d7381..00000000 --- a/.travis.yml +++ /dev/null @@ -1,18 +0,0 @@ -language: go -go: - - release - - tip -sudo: false -before_install: - - gocleandeps=c16c849abae90c23419d - - git clone https://gist.github.com/$gocleandeps.git - - goclean=71d0380287747d956a26 - - git clone https://gist.github.com/$goclean.git -install: - - go get -d -t -v ./... - - bash $gocleandeps/gocleandeps.sh -script: - - export PATH=$PATH:$HOME/gopath/bin - - bash $goclean/goclean.sh -after_success: - - goveralls -coverprofile=profile.cov -service=travis-ci diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 405e51cd..00000000 --- a/LICENSE +++ /dev/null @@ -1,13 +0,0 @@ -Copyright (c) 2014 Conformal Systems LLC. - -Permission to use, copy, modify, and distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/chaincfg/README.md similarity index 63% rename from README.md rename to chaincfg/README.md index 60b25d62..c61aaab0 100644 --- a/README.md +++ b/chaincfg/README.md @@ -1,19 +1,15 @@ -btcnet -====== +chaincfg +======== -[![Build Status](http://img.shields.io/travis/btcsuite/btcnet.svg)] -(https://travis-ci.org/btcsuite/btcnet) [![Coverage Status] -(https://img.shields.io/coveralls/btcsuite/btcnet.svg)] -(https://coveralls.io/r/btcsuite/btcnet?branch=master) [![ISC License] +[![Build Status](http://img.shields.io/travis/btcsuite/btcd.svg)] +(https://travis-ci.org/btcsuite/btcd) [![ISC License] (http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org) -Package btcnet defines the network parameters for the three standard Bitcoin -networks and provides the ability for callers to define their own custom +Package chaincfg defines chain configuration parameters for the three standard +Bitcoin networks and provides the ability for callers to define their own custom Bitcoin networks. -This package is one of the core packages from btcd, an alternative full-node -implementation of Bitcoin which is under active development by Conformal. -Although it was primarily written for btcd, this package has intentionally been +Although this package was primarily written for btcd, it has intentionally been designed so it can be used as a standalone package for any projects needing to use parameters for the standard Bitcoin networks or for projects needing to define their own network. @@ -29,27 +25,27 @@ import ( "log" "github.com/btcsuite/btcutil" - "github.com/btcsuite/btcnet" + "github.com/btcsuite/btcd/chaincfg" ) var testnet = flag.Bool("testnet", false, "operate on the testnet Bitcoin network") // By default (without -testnet), use mainnet. -var netParams = &btcnet.MainNetParams +var chainParams = &chaincfg.MainNetParams func main() { flag.Parse() // Modify active network parameters if operating on testnet. if *testnet { - netParams = &btcnet.TestNet3Params + chainParams = &chaincfg.TestNet3Params } // later... // Create and print new payment address, specific to the active network. pubKeyHash := make([]byte, 20) - addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, netParams) + addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, chainParams) if err != nil { log.Fatal(err) } @@ -60,20 +56,20 @@ func main() { ## Documentation [![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)] -(http://godoc.org/github.com/btcsuite/btcnet) +(http://godoc.org/github.com/btcsuite/btcd/chaincfg) Full `go doc` style documentation for the project can be viewed online without installing this package by using the GoDoc site -[here](http://godoc.org/github.com/btcsuite/btcnet). +[here](http://godoc.org/github.com/btcsuite/btcd/chaincfg). You can also view the documentation locally once the package is installed with the `godoc` tool by running `godoc -http=":6060"` and pointing your browser to -http://localhost:6060/pkg/github.com/btcsuite/btcnet +http://localhost:6060/pkg/github.com/btcsuite/btcd/chaincfg ## Installation ```bash -$ go get github.com/btcsuite/btcnet +$ go get github.com/btcsuite/btcd/chaincfg ``` ## GPG Verification Key @@ -98,5 +94,5 @@ signature perform the following: ## License -Package btcnet is licensed under the [copyfree](http://copyfree.org) ISC +Package chaincfg is licensed under the [copyfree](http://copyfree.org) ISC License. diff --git a/doc.go b/chaincfg/doc.go similarity index 75% rename from doc.go rename to chaincfg/doc.go index cda9e445..3659adbf 100644 --- a/doc.go +++ b/chaincfg/doc.go @@ -1,6 +1,4 @@ -// Package btcnet defines the network parameters for the three standard Bitcoin -// networks and provides the ability for callers to define their own custom -// Bitcoin networks. +// Package chaincfg defines chain configuration parameters. // // In addition to the main Bitcoin network, which is intended for the transfer // of monetary value, there also exists two currently active standard networks: @@ -9,11 +7,11 @@ // handle errors where input intended for one network is used on an application // instance running on a different network. // -// For library packages, btcnet provides the ability to lookup chain parameters -// and encoding magics when passed a *Params. Older APIs not updated to the new -// convention of passing a *Params may lookup the parameters for a +// For library packages, chaincfg provides the ability to lookup chain +// parameters and encoding magics when passed a *Params. Older APIs not updated +// to the new convention of passing a *Params may lookup the parameters for a // wire.BitcoinNet using ParamsForNet, but be aware that this usage is -// deprecated and will be removed from btcnet in the future. +// deprecated and will be removed from chaincfg in the future. // // For main packages, a (typically global) var may be assigned the address of // one of the standard Param vars for use as the application's "active" network. @@ -28,27 +26,27 @@ // "log" // // "github.com/btcsuite/btcutil" -// "github.com/btcsuite/btcnet" +// "github.com/btcsuite/btcd/chaincfg" // ) // // var testnet = flag.Bool("testnet", false, "operate on the testnet Bitcoin network") // // // By default (without -testnet), use mainnet. -// var netParams = &btcnet.MainNetParams +// var chainParams = &chaincfg.MainNetParams // // func main() { // flag.Parse() // // // Modify active network parameters if operating on testnet. // if *testnet { -// netParams = &btcnet.TestNet3Params +// chainParams = &chaincfg.TestNet3Params // } // // // later... // // // Create and print new payment address, specific to the active network. // pubKeyHash := make([]byte, 20) -// addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, netParams) +// addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, chainParams) // if err != nil { // log.Fatal(err) // } @@ -60,4 +58,4 @@ // non-standard network. As a general rule of thumb, all network parameters // should be unique to the network, but parameter collisions can still occur // (unfortunately, this is the case with regtest and testnet3 sharing magics). -package btcnet +package chaincfg diff --git a/genesis.go b/chaincfg/genesis.go similarity index 99% rename from genesis.go rename to chaincfg/genesis.go index 69021bc6..ffdaaa0f 100644 --- a/genesis.go +++ b/chaincfg/genesis.go @@ -2,7 +2,7 @@ // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package btcnet +package chaincfg import ( "time" diff --git a/genesis_test.go b/chaincfg/genesis_test.go similarity index 93% rename from genesis_test.go rename to chaincfg/genesis_test.go index bf060d69..39304108 100644 --- a/genesis_test.go +++ b/chaincfg/genesis_test.go @@ -2,13 +2,13 @@ // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package btcnet_test +package chaincfg_test import ( "bytes" "testing" - "github.com/btcsuite/btcnet" + "github.com/btcsuite/btcd/chaincfg" "github.com/davecgh/go-spew/spew" ) @@ -17,7 +17,7 @@ import ( func TestGenesisBlock(t *testing.T) { // Encode the genesis block to raw bytes. var buf bytes.Buffer - err := btcnet.MainNetParams.GenesisBlock.Serialize(&buf) + err := chaincfg.MainNetParams.GenesisBlock.Serialize(&buf) if err != nil { t.Fatalf("TestGenesisBlock: %v", err) } @@ -30,14 +30,14 @@ func TestGenesisBlock(t *testing.T) { } // Check hash of the block against expected hash. - hash, err := btcnet.MainNetParams.GenesisBlock.BlockSha() + hash, err := chaincfg.MainNetParams.GenesisBlock.BlockSha() if err != nil { t.Fatalf("BlockSha: %v", err) } - if !btcnet.MainNetParams.GenesisHash.IsEqual(&hash) { + if !chaincfg.MainNetParams.GenesisHash.IsEqual(&hash) { t.Fatalf("TestGenesisBlock: Genesis block hash does not "+ "appear valid - got %v, want %v", spew.Sdump(hash), - spew.Sdump(btcnet.MainNetParams.GenesisHash)) + spew.Sdump(chaincfg.MainNetParams.GenesisHash)) } } @@ -46,7 +46,7 @@ func TestGenesisBlock(t *testing.T) { func TestRegTestGenesisBlock(t *testing.T) { // Encode the genesis block to raw bytes. var buf bytes.Buffer - err := btcnet.RegressionNetParams.GenesisBlock.Serialize(&buf) + err := chaincfg.RegressionNetParams.GenesisBlock.Serialize(&buf) if err != nil { t.Fatalf("TestRegTestGenesisBlock: %v", err) } @@ -60,14 +60,14 @@ func TestRegTestGenesisBlock(t *testing.T) { } // Check hash of the block against expected hash. - hash, err := btcnet.RegressionNetParams.GenesisBlock.BlockSha() + hash, err := chaincfg.RegressionNetParams.GenesisBlock.BlockSha() if err != nil { t.Errorf("BlockSha: %v", err) } - if !btcnet.RegressionNetParams.GenesisHash.IsEqual(&hash) { + if !chaincfg.RegressionNetParams.GenesisHash.IsEqual(&hash) { t.Fatalf("TestRegTestGenesisBlock: Genesis block hash does "+ "not appear valid - got %v, want %v", spew.Sdump(hash), - spew.Sdump(btcnet.RegressionNetParams.GenesisHash)) + spew.Sdump(chaincfg.RegressionNetParams.GenesisHash)) } } @@ -76,7 +76,7 @@ func TestRegTestGenesisBlock(t *testing.T) { func TestTestNet3GenesisBlock(t *testing.T) { // Encode the genesis block to raw bytes. var buf bytes.Buffer - err := btcnet.TestNet3Params.GenesisBlock.Serialize(&buf) + err := chaincfg.TestNet3Params.GenesisBlock.Serialize(&buf) if err != nil { t.Fatalf("TestTestNet3GenesisBlock: %v", err) } @@ -90,14 +90,14 @@ func TestTestNet3GenesisBlock(t *testing.T) { } // Check hash of the block against expected hash. - hash, err := btcnet.TestNet3Params.GenesisBlock.BlockSha() + hash, err := chaincfg.TestNet3Params.GenesisBlock.BlockSha() if err != nil { t.Fatalf("BlockSha: %v", err) } - if !btcnet.TestNet3Params.GenesisHash.IsEqual(&hash) { + if !chaincfg.TestNet3Params.GenesisHash.IsEqual(&hash) { t.Fatalf("TestTestNet3GenesisBlock: Genesis block hash does "+ "not appear valid - got %v, want %v", spew.Sdump(hash), - spew.Sdump(btcnet.TestNet3Params.GenesisHash)) + spew.Sdump(chaincfg.TestNet3Params.GenesisHash)) } } @@ -106,7 +106,7 @@ func TestTestNet3GenesisBlock(t *testing.T) { func TestSimNetGenesisBlock(t *testing.T) { // Encode the genesis block to raw bytes. var buf bytes.Buffer - err := btcnet.SimNetParams.GenesisBlock.Serialize(&buf) + err := chaincfg.SimNetParams.GenesisBlock.Serialize(&buf) if err != nil { t.Fatalf("TestSimNetGenesisBlock: %v", err) } @@ -120,14 +120,14 @@ func TestSimNetGenesisBlock(t *testing.T) { } // Check hash of the block against expected hash. - hash, err := btcnet.SimNetParams.GenesisBlock.BlockSha() + hash, err := chaincfg.SimNetParams.GenesisBlock.BlockSha() if err != nil { t.Fatalf("BlockSha: %v", err) } - if !btcnet.SimNetParams.GenesisHash.IsEqual(&hash) { + if !chaincfg.SimNetParams.GenesisHash.IsEqual(&hash) { t.Fatalf("TestSimNetGenesisBlock: Genesis block hash does "+ "not appear valid - got %v, want %v", spew.Sdump(hash), - spew.Sdump(btcnet.SimNetParams.GenesisHash)) + spew.Sdump(chaincfg.SimNetParams.GenesisHash)) } } diff --git a/internal_test.go b/chaincfg/internal_test.go similarity index 92% rename from internal_test.go rename to chaincfg/internal_test.go index bb470dcc..6db2da14 100644 --- a/internal_test.go +++ b/chaincfg/internal_test.go @@ -1,4 +1,4 @@ -package btcnet +package chaincfg import ( "testing" diff --git a/params.go b/chaincfg/params.go similarity index 99% rename from params.go rename to chaincfg/params.go index 752c9e7b..b0225615 100644 --- a/params.go +++ b/chaincfg/params.go @@ -2,7 +2,7 @@ // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package btcnet +package chaincfg import ( "errors" diff --git a/register_test.go b/chaincfg/register_test.go similarity index 99% rename from register_test.go rename to chaincfg/register_test.go index 2d742620..b5ac392b 100644 --- a/register_test.go +++ b/chaincfg/register_test.go @@ -1,11 +1,11 @@ -package btcnet_test +package chaincfg_test import ( "bytes" "reflect" "testing" - . "github.com/btcsuite/btcnet" + . "github.com/btcsuite/btcd/chaincfg" ) // Define some of the required parameters for a user-registered