2015-12-26 21:35:15 +03:00
|
|
|
package channeldb
|
2016-03-23 04:46:54 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/ripemd160"
|
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
2016-05-15 17:17:44 +03:00
|
|
|
"github.com/roasbeef/btcutil"
|
2016-03-23 04:46:54 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-04-24 22:35:52 +03:00
|
|
|
idBucket = []byte("i")
|
2016-03-23 04:46:54 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// PutIdKey saves the hash160 of the public key used for our identity within
|
|
|
|
// the Lightning Network.
|
|
|
|
func (d *DB) PutIdKey(pkh []byte) error {
|
2016-03-24 08:39:52 +03:00
|
|
|
return d.store.Update(func(tx *bolt.Tx) error {
|
2016-03-23 04:46:54 +03:00
|
|
|
// Get the bucket dedicated to storing the meta-data for open
|
|
|
|
// channels.
|
|
|
|
bucket, err := tx.CreateBucketIfNotExists(idBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bucket.Put(identityKey, pkh)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIdKey returns the hash160 of the public key used for out identity within
|
|
|
|
// the Lightning Network as a p2pkh bitcoin address.
|
|
|
|
func (d *DB) GetIdAdr() (*btcutil.AddressPubKeyHash, error) {
|
|
|
|
pkh := make([]byte, ripemd160.Size)
|
2016-03-24 08:39:52 +03:00
|
|
|
err := d.store.View(func(tx *bolt.Tx) error {
|
2016-03-23 04:46:54 +03:00
|
|
|
// Get the bucket dedicated to storing the meta-data for open
|
|
|
|
// channels.
|
|
|
|
bucket := tx.Bucket(idBucket)
|
|
|
|
if bucket == nil {
|
|
|
|
return fmt.Errorf("id bucket not created")
|
|
|
|
}
|
|
|
|
|
|
|
|
pkBytes := bucket.Get(identityKey)
|
|
|
|
copy(pkh, pkBytes)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("identity key has length %d", len(pkh))
|
2016-04-24 22:35:52 +03:00
|
|
|
return btcutil.NewAddressPubKeyHash(pkh, d.netParams)
|
2016-03-23 04:46:54 +03:00
|
|
|
}
|