channeldb: remove hardcoded netparams

This commit is contained in:
Olaoluwa Osuntokun 2016-04-24 12:35:52 -07:00
parent 1e35018e89
commit fa1e7a332f
4 changed files with 13 additions and 9 deletions

@ -10,12 +10,15 @@ import (
"github.com/LightningNetwork/lnd/elkrem" "github.com/LightningNetwork/lnd/elkrem"
"github.com/Roasbeef/btcd/txscript" "github.com/Roasbeef/btcd/txscript"
"github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
_ "github.com/btcsuite/btcwallet/walletdb/bdb" _ "github.com/btcsuite/btcwallet/walletdb/bdb"
) )
var ( var (
netParams = &chaincfg.SegNet4Params
key = [wire.HashSize]byte{ key = [wire.HashSize]byte{
0x81, 0xb6, 0x37, 0xd8, 0xfc, 0xd2, 0xc6, 0xda, 0x81, 0xb6, 0x37, 0xd8, 0xfc, 0xd2, 0xc6, 0xda,
0x68, 0x59, 0xe6, 0x96, 0x31, 0x13, 0xa1, 0x17, 0x68, 0x59, 0xe6, 0x96, 0x31, 0x13, 0xa1, 0x17,
@ -95,7 +98,7 @@ func TestOpenChannelEncodeDecode(t *testing.T) {
// Next, create channeldb for the first time, also setting a mock // Next, create channeldb for the first time, also setting a mock
// EncryptorDecryptor implementation for testing purposes. // EncryptorDecryptor implementation for testing purposes.
cdb, err := Open(tempDirName) cdb, err := Open(tempDirName, netParams)
if err != nil { if err != nil {
t.Fatalf("unable to create channeldb: %v", err) t.Fatalf("unable to create channeldb: %v", err)
} }
@ -103,7 +106,7 @@ func TestOpenChannelEncodeDecode(t *testing.T) {
defer cdb.Close() defer cdb.Close()
privKey, pubKey := btcec.PrivKeyFromBytes(btcec.S256(), key[:]) privKey, pubKey := btcec.PrivKeyFromBytes(btcec.S256(), key[:])
addr, err := btcutil.NewAddressPubKey(pubKey.SerializeCompressed(), ActiveNetParams) addr, err := btcutil.NewAddressPubKey(pubKey.SerializeCompressed(), netParams)
if err != nil { if err != nil {
t.Fatalf("unable to create delivery address") t.Fatalf("unable to create delivery address")
} }

@ -9,6 +9,7 @@ import (
"sync" "sync"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
"github.com/btcsuite/btcd/chaincfg"
) )
const ( const (
@ -37,13 +38,15 @@ type EncryptorDecryptor interface {
type DB struct { type DB struct {
store *bolt.DB store *bolt.DB
netParams *chaincfg.Params
cryptoSystem EncryptorDecryptor cryptoSystem EncryptorDecryptor
} }
// Open opens an existing channeldb created under the passed namespace with // Open opens an existing channeldb created under the passed namespace with
// sensitive data encrypted by the passed EncryptorDecryptor implementation. // sensitive data encrypted by the passed EncryptorDecryptor implementation.
// TODO(roasbeef): versioning? // TODO(roasbeef): versioning?
func Open(dbPath string) (*DB, error) { func Open(dbPath string, netParams *chaincfg.Params) (*DB, error) {
path := filepath.Join(dbPath, dbName) path := filepath.Join(dbPath, dbName)
if !fileExists(path) { if !fileExists(path) {
@ -57,7 +60,7 @@ func Open(dbPath string) (*DB, error) {
return nil, err return nil, err
} }
return &DB{store: bdb}, nil return &DB{store: bdb, netParams: netParams}, nil
} }
// RegisterCryptoSystem... // RegisterCryptoSystem...

@ -18,7 +18,7 @@ func TestOpenWithCreate(t *testing.T) {
// Next, open thereby creating channeldb for the first time. // Next, open thereby creating channeldb for the first time.
dbPath := filepath.Join(tempDirName, "cdb") dbPath := filepath.Join(tempDirName, "cdb")
cdb, err := Open(dbPath) cdb, err := Open(dbPath, netParams)
if err != nil { if err != nil {
t.Fatalf("unable to create channeldb: %v", err) t.Fatalf("unable to create channeldb: %v", err)
} }

@ -6,13 +6,11 @@ import (
"golang.org/x/crypto/ripemd160" "golang.org/x/crypto/ripemd160"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
) )
var ( var (
idBucket = []byte("i") idBucket = []byte("i")
ActiveNetParams = &chaincfg.TestNet3Params
) )
// PutIdKey saves the hash160 of the public key used for our identity within // PutIdKey saves the hash160 of the public key used for our identity within
@ -52,5 +50,5 @@ func (d *DB) GetIdAdr() (*btcutil.AddressPubKeyHash, error) {
} }
log.Infof("identity key has length %d", len(pkh)) log.Infof("identity key has length %d", len(pkh))
return btcutil.NewAddressPubKeyHash(pkh, ActiveNetParams) return btcutil.NewAddressPubKeyHash(pkh, d.netParams)
} }