Additions for btcutil.

ok @davecgh
This commit is contained in:
Josh Rickmar 2014-05-26 17:55:13 -05:00
parent fb8ab4200f
commit 6f76171a82

View file

@ -228,26 +228,19 @@ var (
ErrDuplicateNet = errors.New("duplicate Bitcoin network") ErrDuplicateNet = errors.New("duplicate Bitcoin network")
) )
var nets = map[btcwire.BitcoinNet]*Params{ var (
btcwire.MainNet: &MainNetParams, registeredNets = map[btcwire.BitcoinNet]struct{}{}
btcwire.TestNet: &RegressionNetParams,
btcwire.TestNet3: &TestNet3Params, pubKeyHashAddrIDs = map[byte]struct{}{
MainNetParams.PubKeyHashAddrID: struct{}{},
TestNet3Params.PubKeyHashAddrID: struct{}{}, // shared with regtest
} }
// ParamsForNet returns the network parameters for a Bitcoin network, or scriptHashAddrIDs = map[byte]struct{}{
// ErrUnknownNet if the network is not a default network (mainnet, regtest, MainNetParams.ScriptHashAddrID: struct{}{},
// or testnet3) and not registered into the package with Register. TestNet3Params.ScriptHashAddrID: struct{}{}, // shared with regtest
//
// 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 // 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.
@ -256,18 +249,38 @@ func ParamsForNet(net btcwire.BitcoinNet) (*Params, error) {
// as early as possible. Then, library packages may lookup networks or network // as early as possible. Then, library packages may lookup networks or network
// parameters based on inputs and work regardless of the network being standard // parameters based on inputs and work regardless of the network being standard
// or not. // 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 { func Register(params *Params) error {
if _, ok := nets[params.Net]; ok { if _, ok := registeredNets[params.Net]; ok {
return ErrDuplicateNet return ErrDuplicateNet
} }
nets[params.Net] = params registeredNets[params.Net] = struct{}{}
pubKeyHashAddrIDs[params.ScriptHashAddrID] = struct{}{}
scriptHashAddrIDs[params.ScriptHashAddrID] = struct{}{}
return nil 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 // newShaHashFromStr converts the passed big-endian hex string into a
// btcwire.ShaHash. It only differs from the one available in btcwire in that // 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 // it panics on an error since it will only (and must only) be called with