Skip to content

Commit

Permalink
Merge pull request #17 from lightningnetwork/wallet-interface-refactor
Browse files Browse the repository at this point in the history
Refactor the lnwallet package to directly use the WalletController interface
  • Loading branch information
Roasbeef authored Sep 8, 2016
2 parents a28c011 + bccb1b9 commit dfe37cd
Show file tree
Hide file tree
Showing 26 changed files with 1,743 additions and 1,020 deletions.
60 changes: 20 additions & 40 deletions channeldb/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type OpenChannel struct {
//ReserveAmount btcutil.Amount

// Keys for both sides to be used for the commitment transactions.
OurCommitKey *btcec.PrivateKey
OurCommitKey *btcec.PublicKey
TheirCommitKey *btcec.PublicKey

// Tracking total channel capacity, and the amount of funds allocated
Expand All @@ -123,7 +123,7 @@ type OpenChannel struct {
// The outpoint of the final funding transaction.
FundingOutpoint *wire.OutPoint

OurMultiSigKey *btcec.PrivateKey
OurMultiSigKey *btcec.PublicKey
TheirMultiSigKey *btcec.PublicKey
FundingRedeemScript []byte

Expand Down Expand Up @@ -200,7 +200,7 @@ func (c *OpenChannel) FullSync() error {
chanIDBucket.Put(b.Bytes(), nil)
}

return putOpenChannel(chanBucket, nodeChanBucket, c, c.Db.cryptoSystem)
return putOpenChannel(chanBucket, nodeChanBucket, c)
})
}

Expand Down Expand Up @@ -362,7 +362,7 @@ func putClosedChannelSummary(tx *bolt.Tx, chanID []byte) error {
// putChannel serializes, and stores the current state of the channel in its
// entirety.
func putOpenChannel(openChanBucket *bolt.Bucket, nodeChanBucket *bolt.Bucket,
channel *OpenChannel, encryptor EncryptorDecryptor) error {
channel *OpenChannel) error {

// First write out all the "common" fields using the field's prefix
// appened with the channel's ID. These fields go into a top-level bucket
Expand All @@ -387,13 +387,13 @@ func putOpenChannel(openChanBucket *bolt.Bucket, nodeChanBucket *bolt.Bucket,
if err := putChannelIDs(nodeChanBucket, channel); err != nil {
return err
}
if err := putChanCommitKeys(nodeChanBucket, channel, encryptor); err != nil {
if err := putChanCommitKeys(nodeChanBucket, channel); err != nil {
return err
}
if err := putChanCommitTxns(nodeChanBucket, channel); err != nil {
return err
}
if err := putChanFundingInfo(nodeChanBucket, channel, encryptor); err != nil {
if err := putChanFundingInfo(nodeChanBucket, channel); err != nil {
return err
}
if err := putChanEklremState(nodeChanBucket, channel); err != nil {
Expand All @@ -411,7 +411,7 @@ func putOpenChannel(openChanBucket *bolt.Bucket, nodeChanBucket *bolt.Bucket,
// An EncryptorDecryptor is required to decrypt sensitive information stored
// within the database.
func fetchOpenChannel(openChanBucket *bolt.Bucket, nodeChanBucket *bolt.Bucket,
chanID *wire.OutPoint, decryptor EncryptorDecryptor) (*OpenChannel, error) {
chanID *wire.OutPoint) (*OpenChannel, error) {

channel := &OpenChannel{
ChanID: chanID,
Expand All @@ -421,13 +421,13 @@ func fetchOpenChannel(openChanBucket *bolt.Bucket, nodeChanBucket *bolt.Bucket,
if err := fetchChannelIDs(nodeChanBucket, channel); err != nil {
return nil, err
}
if err := fetchChanCommitKeys(nodeChanBucket, channel, decryptor); err != nil {
if err := fetchChanCommitKeys(nodeChanBucket, channel); err != nil {
return nil, err
}
if err := fetchChanCommitTxns(nodeChanBucket, channel); err != nil {
return nil, err
}
if err := fetchChanFundingInfo(nodeChanBucket, channel, decryptor); err != nil {
if err := fetchChanFundingInfo(nodeChanBucket, channel); err != nil {
return nil, err
}
if err := fetchChanEklremState(nodeChanBucket, channel); err != nil {
Expand Down Expand Up @@ -791,8 +791,7 @@ func fetchChannelIDs(nodeChanBucket *bolt.Bucket, channel *OpenChannel) error {
return nil
}

func putChanCommitKeys(nodeChanBucket *bolt.Bucket, channel *OpenChannel,
ed EncryptorDecryptor) error {
func putChanCommitKeys(nodeChanBucket *bolt.Bucket, channel *OpenChannel) error {

// Construct the key which stores the commitment keys: ckk || channelID.
// TODO(roasbeef): factor into func
Expand All @@ -810,12 +809,7 @@ func putChanCommitKeys(nodeChanBucket *bolt.Bucket, channel *OpenChannel,
return err
}

encryptedPriv, err := ed.Encrypt(channel.OurCommitKey.Serialize())
if err != nil {
return err
}

if _, err := b.Write(encryptedPriv); err != nil {
if _, err := b.Write(channel.OurCommitKey.SerializeCompressed()); err != nil {
return err
}

Expand All @@ -829,8 +823,7 @@ func deleteChanCommitKeys(nodeChanBucket *bolt.Bucket, chanID []byte) error {
return nodeChanBucket.Delete(commitKey)
}

func fetchChanCommitKeys(nodeChanBucket *bolt.Bucket, channel *OpenChannel,
ed EncryptorDecryptor) error {
func fetchChanCommitKeys(nodeChanBucket *bolt.Bucket, channel *OpenChannel) error {

// Construct the key which stores the commitment keys: ckk || channelID.
// TODO(roasbeef): factor into func
Expand All @@ -850,12 +843,7 @@ func fetchChanCommitKeys(nodeChanBucket *bolt.Bucket, channel *OpenChannel,
return err
}

decryptedPriv, err := ed.Decrypt(keyBytes[33:])
if err != nil {
return err
}

channel.OurCommitKey, _ = btcec.PrivKeyFromBytes(btcec.S256(), decryptedPriv)
channel.OurCommitKey, err = btcec.ParsePubKey(keyBytes[33:], btcec.S256())
if err != nil {
return err
}
Expand Down Expand Up @@ -939,9 +927,7 @@ func fetchChanCommitTxns(nodeChanBucket *bolt.Bucket, channel *OpenChannel) erro
return nil
}

func putChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel,
ed EncryptorDecryptor) error {

func putChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel) error {
var bc bytes.Buffer
if err := writeOutpoint(&bc, channel.ChanID); err != nil {
return err
Expand All @@ -956,11 +942,8 @@ func putChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel,
return err
}

encryptedPriv, err := ed.Encrypt(channel.OurMultiSigKey.Serialize())
if err != nil {
return err
}
if err := wire.WriteVarBytes(&b, 0, encryptedPriv); err != nil {
ourSerKey := channel.OurMultiSigKey.SerializeCompressed()
if err := wire.WriteVarBytes(&b, 0, ourSerKey); err != nil {
return err
}
theirSerKey := channel.TheirMultiSigKey.SerializeCompressed()
Expand Down Expand Up @@ -989,9 +972,7 @@ func deleteChanFundingInfo(nodeChanBucket *bolt.Bucket, chanID []byte) error {
return nodeChanBucket.Delete(fundTxnKey)
}

func fetchChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel,
ed EncryptorDecryptor) error {

func fetchChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel) error {
var b bytes.Buffer
if err := writeOutpoint(&b, channel.ChanID); err != nil {
return err
Expand All @@ -1008,17 +989,16 @@ func fetchChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel,
return err
}

encryptedPrivBytes, err := wire.ReadVarBytes(infoBytes, 0, 100, "")
ourKeyBytes, err := wire.ReadVarBytes(infoBytes, 0, 34, "")
if err != nil {
return err
}
decryptedPriv, err := ed.Decrypt(encryptedPrivBytes)
channel.OurMultiSigKey, err = btcec.ParsePubKey(ourKeyBytes, btcec.S256())
if err != nil {
return err
}
channel.OurMultiSigKey, _ = btcec.PrivKeyFromBytes(btcec.S256(), decryptedPriv)

theirKeyBytes, err := wire.ReadVarBytes(infoBytes, 0, 33, "")
theirKeyBytes, err := wire.ReadVarBytes(infoBytes, 0, 34, "")
if err != nil {
return err
}
Expand Down
30 changes: 6 additions & 24 deletions channeldb/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,6 @@ var (
}
)

type MockEncryptorDecryptor struct {
}

func (m *MockEncryptorDecryptor) Encrypt(n []byte) ([]byte, error) {
return n, nil
}

func (m *MockEncryptorDecryptor) Decrypt(n []byte) ([]byte, error) {
return n, nil
}

func (m *MockEncryptorDecryptor) OverheadSize() uint32 {
return 0
}

var _ EncryptorDecryptor = (*MockEncryptorDecryptor)(nil)

func TestOpenChannelPutGetDelete(t *testing.T) {
// First, create a temporary directory to be used for the duration of
// this test.
Expand All @@ -111,7 +94,6 @@ func TestOpenChannelPutGetDelete(t *testing.T) {
if err != nil {
t.Fatalf("unable to create channeldb: %v", err)
}
cdb.RegisterCryptoSystem(&MockEncryptorDecryptor{})
defer cdb.Close()

privKey, pubKey := btcec.PrivKeyFromBytes(btcec.S256(), key[:])
Expand Down Expand Up @@ -144,7 +126,7 @@ func TestOpenChannelPutGetDelete(t *testing.T) {
TheirLNID: key,
ChanID: id,
MinFeePerKb: btcutil.Amount(5000),
OurCommitKey: privKey,
OurCommitKey: privKey.PubKey(),
TheirCommitKey: pubKey,
Capacity: btcutil.Amount(10000),
OurBalance: btcutil.Amount(3000),
Expand All @@ -154,7 +136,7 @@ func TestOpenChannelPutGetDelete(t *testing.T) {
LocalElkrem: sender,
RemoteElkrem: receiver,
FundingOutpoint: testOutpoint,
OurMultiSigKey: privKey,
OurMultiSigKey: privKey.PubKey(),
TheirMultiSigKey: privKey.PubKey(),
FundingRedeemScript: script,
TheirCurrentRevocation: privKey.PubKey(),
Expand Down Expand Up @@ -195,8 +177,8 @@ func TestOpenChannelPutGetDelete(t *testing.T) {
t.Fatalf("fee/kb doens't match")
}

if !bytes.Equal(state.OurCommitKey.Serialize(),
newState.OurCommitKey.Serialize()) {
if !bytes.Equal(state.OurCommitKey.SerializeCompressed(),
newState.OurCommitKey.SerializeCompressed()) {
t.Fatalf("our commit key dont't match")
}
if !bytes.Equal(state.TheirCommitKey.SerializeCompressed(),
Expand Down Expand Up @@ -234,8 +216,8 @@ func TestOpenChannelPutGetDelete(t *testing.T) {
t.Fatalf("funding outpoint doesn't match")
}

if !bytes.Equal(state.OurMultiSigKey.Serialize(),
newState.OurMultiSigKey.Serialize()) {
if !bytes.Equal(state.OurMultiSigKey.SerializeCompressed(),
newState.OurMultiSigKey.SerializeCompressed()) {
t.Fatalf("our multisig key doesn't match")
}
if !bytes.Equal(state.TheirMultiSigKey.SerializeCompressed(),
Expand Down
18 changes: 1 addition & 17 deletions channeldb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,13 @@ var bufPool = &sync.Pool{
New: func() interface{} { return new(bytes.Buffer) },
}

// EncryptorDecryptor...
// TODO(roasbeef): ability to rotate EncryptorDecryptor's across DB
type EncryptorDecryptor interface {
Encrypt(in []byte) ([]byte, error)
Decrypt(in []byte) ([]byte, error)
OverheadSize() uint32
}

// DB is the primary datastore for the LND daemon. The database stores
// information related to nodes, routing data, open/closed channels, fee
// schedules, and reputation data.
type DB struct {
store *bolt.DB

netParams *chaincfg.Params

cryptoSystem EncryptorDecryptor
}

// Open opens an existing channeldb created under the passed namespace with
Expand All @@ -66,12 +56,6 @@ func Open(dbPath string, netParams *chaincfg.Params) (*DB, error) {
return &DB{store: bdb, netParams: netParams}, nil
}

// RegisterCryptoSystem registers an implementation of the EncryptorDecryptor
// interface for use within the database to encrypt/decrypt sensitive data.
func (d *DB) RegisterCryptoSystem(ed EncryptorDecryptor) {
d.cryptoSystem = ed
}

// Wipe completely deletes all saved state within all used buckets within the
// database. The deletion is done in a single transaction, therefore this
// operation is fully atomic.
Expand Down Expand Up @@ -179,7 +163,7 @@ func (d *DB) FetchOpenChannels(nodeID *wire.ShaHash) ([]*OpenChannel, error) {
}

oChannel, err := fetchOpenChannel(openChanBucket,
nodeChanBucket, chanID, d.cryptoSystem)
nodeChanBucket, chanID)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions fundingmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,6 @@ func (f *fundingManager) handleFundingComplete(fmsg *fundingCompleteMsg) {
// Append a sighash type of SigHashAll to the signature as it's the
// sighash type used implicitly within this type of channel for
// commitment transactions.
commitSig = append(commitSig, byte(txscript.SigHashAll))
revokeKey := fmsg.msg.RevocationKey
if err := resCtx.reservation.CompleteReservationSingle(revokeKey, fundingOut, commitSig); err != nil {
// TODO(roasbeef): better error logging: peerID, channelID, etc.
Expand Down Expand Up @@ -521,7 +520,7 @@ func (f *fundingManager) handleFundingSignComplete(fmsg *fundingSignCompleteMsg)
// The remote peer has responded with a signature for our commitment
// transaction. We'll verify the signature for validity, then commit
// the state to disk as we can now open the channel.
commitSig := append(fmsg.msg.CommitSignature.Serialize(), byte(txscript.SigHashAll))
commitSig := fmsg.msg.CommitSignature.Serialize()
if err := resCtx.reservation.CompleteReservation(nil, commitSig); err != nil {
fndgLog.Errorf("unable to complete reservation sign complete: %v", err)
fmsg.peer.Disconnect()
Expand Down
14 changes: 7 additions & 7 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import:
- hdkeychain
- txsort
- package: github.com/roasbeef/btcwallet
version: master
subpackages:
- chain
- waddrmgr
Expand Down
Loading

0 comments on commit dfe37cd

Please sign in to comment.