2015-12-01 19:44:58 +01:00
|
|
|
// Copyright (c) 2014 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-06-13 19:14:44 +02:00
|
|
|
|
|
|
|
package votingpool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2017-06-06 02:54:35 +02:00
|
|
|
"github.com/roasbeef/btcd/txscript"
|
|
|
|
"github.com/roasbeef/btcutil"
|
|
|
|
"github.com/roasbeef/btcutil/hdkeychain"
|
|
|
|
"github.com/roasbeef/btcwallet/internal/zero"
|
|
|
|
"github.com/roasbeef/btcwallet/waddrmgr"
|
|
|
|
"github.com/roasbeef/btcwallet/walletdb"
|
2014-06-13 19:14:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
minSeriesPubKeys = 3
|
2014-11-04 18:22:13 +01:00
|
|
|
// CurrentVersion is the version used for newly created Series.
|
|
|
|
CurrentVersion = 1
|
2014-06-13 19:14:44 +02:00
|
|
|
)
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
// Branch is the type used to represent a branch number in a series.
|
|
|
|
type Branch uint32
|
|
|
|
|
|
|
|
// Index is the type used to represent an index number in a series.
|
|
|
|
type Index uint32
|
|
|
|
|
2014-06-13 19:14:44 +02:00
|
|
|
// SeriesData represents a Series for a given Pool.
|
|
|
|
type SeriesData struct {
|
|
|
|
version uint32
|
|
|
|
// Whether or not a series is active. This is serialized/deserialized but
|
|
|
|
// for now there's no way to deactivate a series.
|
|
|
|
active bool
|
|
|
|
// A.k.a. "m" in "m of n signatures needed".
|
|
|
|
reqSigs uint32
|
|
|
|
publicKeys []*hdkeychain.ExtendedKey
|
|
|
|
privateKeys []*hdkeychain.ExtendedKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pool represents an arrangement of notary servers to securely
|
|
|
|
// store and account for customer cryptocurrency deposits and to redeem
|
|
|
|
// valid withdrawals. For details about how the arrangement works, see
|
|
|
|
// http://opentransactions.org/wiki/index.php?title=Category:Voting_Pools
|
|
|
|
type Pool struct {
|
|
|
|
ID []byte
|
|
|
|
seriesLookup map[uint32]*SeriesData
|
|
|
|
manager *waddrmgr.Manager
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
// PoolAddress represents a voting pool P2SH address, generated by
|
|
|
|
// deriving public HD keys from the series' master keys using the given
|
|
|
|
// branch/index and constructing a M-of-N multi-sig script.
|
|
|
|
type PoolAddress interface {
|
|
|
|
SeriesID() uint32
|
|
|
|
Branch() Branch
|
|
|
|
Index() Index
|
|
|
|
}
|
|
|
|
|
|
|
|
type poolAddress struct {
|
|
|
|
pool *Pool
|
|
|
|
addr btcutil.Address
|
|
|
|
script []byte
|
|
|
|
seriesID uint32
|
|
|
|
branch Branch
|
|
|
|
index Index
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeAddress is a votingpool address meant to be used on transaction change
|
|
|
|
// outputs. All change addresses have branch==0.
|
|
|
|
type ChangeAddress struct {
|
|
|
|
*poolAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithdrawalAddress is a votingpool address that may contain unspent outputs
|
|
|
|
// available for use in a withdrawal.
|
|
|
|
type WithdrawalAddress struct {
|
|
|
|
*poolAddress
|
|
|
|
}
|
|
|
|
|
2014-06-13 19:14:44 +02:00
|
|
|
// Create creates a new entry in the database with the given ID
|
|
|
|
// and returns the Pool representing it.
|
2017-01-18 21:29:34 +01:00
|
|
|
func Create(ns walletdb.ReadWriteBucket, m *waddrmgr.Manager, poolID []byte) (*Pool, error) {
|
|
|
|
err := putPool(ns, poolID)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
str := fmt.Sprintf("unable to add voting pool %v to db", poolID)
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, newError(ErrPoolAlreadyExists, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
2017-01-18 21:29:34 +01:00
|
|
|
return newPool(m, poolID), nil
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load fetches the entry in the database with the given ID and returns the Pool
|
|
|
|
// representing it.
|
2017-01-18 21:29:34 +01:00
|
|
|
func Load(ns walletdb.ReadBucket, m *waddrmgr.Manager, poolID []byte) (*Pool, error) {
|
|
|
|
if !existsPool(ns, poolID) {
|
|
|
|
str := fmt.Sprintf("unable to find voting pool %v in db", poolID)
|
|
|
|
return nil, newError(ErrPoolNotExists, str, nil)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
2017-01-18 21:29:34 +01:00
|
|
|
p := newPool(m, poolID)
|
|
|
|
if err := p.LoadAllSeries(ns); err != nil {
|
2014-06-13 19:14:44 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-04 18:22:13 +01:00
|
|
|
return p, nil
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newPool creates a new Pool instance.
|
2017-01-18 21:29:34 +01:00
|
|
|
func newPool(m *waddrmgr.Manager, poolID []byte) *Pool {
|
2014-06-13 19:14:44 +02:00
|
|
|
return &Pool{
|
|
|
|
ID: poolID,
|
|
|
|
seriesLookup: make(map[uint32]*SeriesData),
|
|
|
|
manager: m,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadAndGetDepositScript generates and returns a deposit script for the given seriesID,
|
|
|
|
// branch and index of the Pool identified by poolID.
|
2017-01-18 21:29:34 +01:00
|
|
|
func LoadAndGetDepositScript(ns walletdb.ReadBucket, m *waddrmgr.Manager, poolID string, seriesID uint32, branch Branch, index Index) ([]byte, error) {
|
2014-06-13 19:14:44 +02:00
|
|
|
pid := []byte(poolID)
|
2017-01-18 21:29:34 +01:00
|
|
|
p, err := Load(ns, m, pid)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-04 18:22:13 +01:00
|
|
|
script, err := p.DepositScript(seriesID, branch, index)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return script, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadAndCreateSeries loads the Pool with the given ID, creating a new one if it doesn't
|
|
|
|
// yet exist, and then creates and returns a Series with the given seriesID, rawPubKeys
|
|
|
|
// and reqSigs. See CreateSeries for the constraints enforced on rawPubKeys and reqSigs.
|
2017-01-18 21:29:34 +01:00
|
|
|
func LoadAndCreateSeries(ns walletdb.ReadWriteBucket, m *waddrmgr.Manager, version uint32,
|
2014-06-13 19:14:44 +02:00
|
|
|
poolID string, seriesID, reqSigs uint32, rawPubKeys []string) error {
|
|
|
|
pid := []byte(poolID)
|
2017-01-18 21:29:34 +01:00
|
|
|
p, err := Load(ns, m, pid)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
2014-11-04 18:22:13 +01:00
|
|
|
vpErr := err.(Error)
|
|
|
|
if vpErr.ErrorCode == ErrPoolNotExists {
|
2017-01-18 21:29:34 +01:00
|
|
|
p, err = Create(ns, m, pid)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-01-18 21:29:34 +01:00
|
|
|
return p.CreateSeries(ns, version, seriesID, reqSigs, rawPubKeys)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadAndReplaceSeries loads the voting pool with the given ID and calls ReplaceSeries,
|
|
|
|
// passing the given series ID, public keys and reqSigs to it.
|
2017-01-18 21:29:34 +01:00
|
|
|
func LoadAndReplaceSeries(ns walletdb.ReadWriteBucket, m *waddrmgr.Manager, version uint32,
|
2014-06-13 19:14:44 +02:00
|
|
|
poolID string, seriesID, reqSigs uint32, rawPubKeys []string) error {
|
|
|
|
pid := []byte(poolID)
|
2017-01-18 21:29:34 +01:00
|
|
|
p, err := Load(ns, m, pid)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-18 21:29:34 +01:00
|
|
|
return p.ReplaceSeries(ns, version, seriesID, reqSigs, rawPubKeys)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadAndEmpowerSeries loads the voting pool with the given ID and calls EmpowerSeries,
|
|
|
|
// passing the given series ID and private key to it.
|
2017-01-18 21:29:34 +01:00
|
|
|
func LoadAndEmpowerSeries(ns walletdb.ReadWriteBucket, m *waddrmgr.Manager,
|
2014-06-13 19:14:44 +02:00
|
|
|
poolID string, seriesID uint32, rawPrivKey string) error {
|
|
|
|
pid := []byte(poolID)
|
2017-01-18 21:29:34 +01:00
|
|
|
pool, err := Load(ns, m, pid)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-18 21:29:34 +01:00
|
|
|
return pool.EmpowerSeries(ns, seriesID, rawPrivKey)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
// Series returns the series with the given ID, or nil if it doesn't
|
2014-06-13 19:14:44 +02:00
|
|
|
// exist.
|
2014-11-04 18:22:13 +01:00
|
|
|
func (p *Pool) Series(seriesID uint32) *SeriesData {
|
|
|
|
series, exists := p.seriesLookup[seriesID]
|
2014-06-13 19:14:44 +02:00
|
|
|
if !exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return series
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
// Manager returns the waddrmgr.Manager used by this Pool.
|
|
|
|
func (p *Pool) Manager() *waddrmgr.Manager {
|
|
|
|
return p.manager
|
|
|
|
}
|
|
|
|
|
2014-06-13 19:14:44 +02:00
|
|
|
// saveSeriesToDisk stores the given series ID and data in the database,
|
|
|
|
// first encrypting the public/private extended keys.
|
2014-11-04 18:22:13 +01:00
|
|
|
//
|
|
|
|
// This method must be called with the Pool's manager unlocked.
|
2017-01-18 21:29:34 +01:00
|
|
|
func (p *Pool) saveSeriesToDisk(ns walletdb.ReadWriteBucket, seriesID uint32, data *SeriesData) error {
|
2014-06-13 19:14:44 +02:00
|
|
|
var err error
|
|
|
|
encryptedPubKeys := make([][]byte, len(data.publicKeys))
|
|
|
|
for i, pubKey := range data.publicKeys {
|
2014-11-04 18:22:13 +01:00
|
|
|
encryptedPubKeys[i], err = p.manager.Encrypt(
|
2014-06-13 19:14:44 +02:00
|
|
|
waddrmgr.CKTPublic, []byte(pubKey.String()))
|
|
|
|
if err != nil {
|
|
|
|
str := fmt.Sprintf("key %v failed encryption", pubKey)
|
2014-11-04 18:22:13 +01:00
|
|
|
return newError(ErrCrypto, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
encryptedPrivKeys := make([][]byte, len(data.privateKeys))
|
|
|
|
for i, privKey := range data.privateKeys {
|
|
|
|
if privKey == nil {
|
|
|
|
encryptedPrivKeys[i] = nil
|
|
|
|
} else {
|
2014-11-04 18:22:13 +01:00
|
|
|
encryptedPrivKeys[i], err = p.manager.Encrypt(
|
2014-06-13 19:14:44 +02:00
|
|
|
waddrmgr.CKTPrivate, []byte(privKey.String()))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
str := fmt.Sprintf("key %v failed encryption", privKey)
|
2014-11-04 18:22:13 +01:00
|
|
|
return newError(ErrCrypto, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 21:29:34 +01:00
|
|
|
err = putSeries(ns, p.ID, data.version, seriesID, data.active,
|
|
|
|
data.reqSigs, encryptedPubKeys, encryptedPrivKeys)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
str := fmt.Sprintf("cannot put series #%d into db", seriesID)
|
2014-11-04 18:22:13 +01:00
|
|
|
return newError(ErrSeriesSerialization, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CanonicalKeyOrder will return a copy of the input canonically
|
|
|
|
// ordered which is defined to be lexicographical.
|
|
|
|
func CanonicalKeyOrder(keys []string) []string {
|
|
|
|
orderedKeys := make([]string, len(keys))
|
|
|
|
copy(orderedKeys, keys)
|
|
|
|
sort.Sort(sort.StringSlice(orderedKeys))
|
|
|
|
return orderedKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the given slice of strings into a slice of ExtendedKeys,
|
|
|
|
// checking that all of them are valid public (and not private) keys,
|
|
|
|
// and that there are no duplicates.
|
|
|
|
func convertAndValidatePubKeys(rawPubKeys []string) ([]*hdkeychain.ExtendedKey, error) {
|
|
|
|
seenKeys := make(map[string]bool)
|
|
|
|
keys := make([]*hdkeychain.ExtendedKey, len(rawPubKeys))
|
|
|
|
for i, rawPubKey := range rawPubKeys {
|
|
|
|
if _, seen := seenKeys[rawPubKey]; seen {
|
|
|
|
str := fmt.Sprintf("duplicated public key: %v", rawPubKey)
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, newError(ErrKeyDuplicate, str, nil)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
seenKeys[rawPubKey] = true
|
|
|
|
|
|
|
|
key, err := hdkeychain.NewKeyFromString(rawPubKey)
|
|
|
|
if err != nil {
|
|
|
|
str := fmt.Sprintf("invalid extended public key %v", rawPubKey)
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, newError(ErrKeyChain, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if key.IsPrivate() {
|
|
|
|
str := fmt.Sprintf("private keys not accepted: %v", rawPubKey)
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, newError(ErrKeyIsPrivate, str, nil)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
keys[i] = key
|
|
|
|
}
|
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// putSeries creates a new seriesData with the given arguments, ordering the
|
|
|
|
// given public keys (using CanonicalKeyOrder), validating and converting them
|
|
|
|
// to hdkeychain.ExtendedKeys, saves that to disk and adds it to this voting
|
|
|
|
// pool's seriesLookup map. It also ensures inRawPubKeys has at least
|
|
|
|
// minSeriesPubKeys items and reqSigs is not greater than the number of items in
|
|
|
|
// inRawPubKeys.
|
2014-11-04 18:22:13 +01:00
|
|
|
//
|
|
|
|
// This method must be called with the Pool's manager unlocked.
|
2017-01-18 21:29:34 +01:00
|
|
|
func (p *Pool) putSeries(ns walletdb.ReadWriteBucket, version, seriesID, reqSigs uint32, inRawPubKeys []string) error {
|
2014-06-13 19:14:44 +02:00
|
|
|
if len(inRawPubKeys) < minSeriesPubKeys {
|
|
|
|
str := fmt.Sprintf("need at least %d public keys to create a series", minSeriesPubKeys)
|
2014-11-04 18:22:13 +01:00
|
|
|
return newError(ErrTooFewPublicKeys, str, nil)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if reqSigs > uint32(len(inRawPubKeys)) {
|
|
|
|
str := fmt.Sprintf(
|
|
|
|
"the number of required signatures cannot be more than the number of keys")
|
2014-11-04 18:22:13 +01:00
|
|
|
return newError(ErrTooManyReqSignatures, str, nil)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rawPubKeys := CanonicalKeyOrder(inRawPubKeys)
|
|
|
|
|
|
|
|
keys, err := convertAndValidatePubKeys(rawPubKeys)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
data := &SeriesData{
|
|
|
|
version: version,
|
|
|
|
active: false,
|
|
|
|
reqSigs: reqSigs,
|
|
|
|
publicKeys: keys,
|
|
|
|
privateKeys: make([]*hdkeychain.ExtendedKey, len(keys)),
|
|
|
|
}
|
|
|
|
|
2017-01-18 21:29:34 +01:00
|
|
|
err = p.saveSeriesToDisk(ns, seriesID, data)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-04 18:22:13 +01:00
|
|
|
p.seriesLookup[seriesID] = data
|
2014-06-13 19:14:44 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSeries will create and return a new non-existing series.
|
|
|
|
//
|
2014-11-04 18:22:13 +01:00
|
|
|
// - seriesID must be greater than or equal 1;
|
2014-06-13 19:14:44 +02:00
|
|
|
// - rawPubKeys has to contain three or more public keys;
|
|
|
|
// - reqSigs has to be less or equal than the number of public keys in rawPubKeys.
|
2017-01-18 21:29:34 +01:00
|
|
|
func (p *Pool) CreateSeries(ns walletdb.ReadWriteBucket, version, seriesID, reqSigs uint32, rawPubKeys []string) error {
|
2014-11-04 18:22:13 +01:00
|
|
|
if seriesID == 0 {
|
|
|
|
return newError(ErrSeriesIDInvalid, "series ID cannot be 0", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if series := p.Series(seriesID); series != nil {
|
2014-06-13 19:14:44 +02:00
|
|
|
str := fmt.Sprintf("series #%d already exists", seriesID)
|
2014-11-04 18:22:13 +01:00
|
|
|
return newError(ErrSeriesAlreadyExists, str, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if seriesID != 1 {
|
|
|
|
if _, ok := p.seriesLookup[seriesID-1]; !ok {
|
|
|
|
str := fmt.Sprintf("series #%d cannot be created because series #%d does not exist",
|
|
|
|
seriesID, seriesID-1)
|
|
|
|
return newError(ErrSeriesIDNotSequential, str, nil)
|
|
|
|
}
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
2017-01-18 21:29:34 +01:00
|
|
|
return p.putSeries(ns, version, seriesID, reqSigs, rawPubKeys)
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ActivateSeries marks the series with the given ID as active.
|
2017-01-18 21:29:34 +01:00
|
|
|
func (p *Pool) ActivateSeries(ns walletdb.ReadWriteBucket, seriesID uint32) error {
|
2014-11-04 18:22:13 +01:00
|
|
|
series := p.Series(seriesID)
|
|
|
|
if series == nil {
|
|
|
|
str := fmt.Sprintf("series #%d does not exist, cannot activate it", seriesID)
|
|
|
|
return newError(ErrSeriesNotExists, str, nil)
|
|
|
|
}
|
|
|
|
series.active = true
|
2017-01-18 21:29:34 +01:00
|
|
|
err := p.saveSeriesToDisk(ns, seriesID, series)
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.seriesLookup[seriesID] = series
|
|
|
|
return nil
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReplaceSeries will replace an already existing series.
|
|
|
|
//
|
|
|
|
// - rawPubKeys has to contain three or more public keys
|
|
|
|
// - reqSigs has to be less or equal than the number of public keys in rawPubKeys.
|
2017-01-18 21:29:34 +01:00
|
|
|
func (p *Pool) ReplaceSeries(ns walletdb.ReadWriteBucket, version, seriesID, reqSigs uint32, rawPubKeys []string) error {
|
2014-11-04 18:22:13 +01:00
|
|
|
series := p.Series(seriesID)
|
2014-06-13 19:14:44 +02:00
|
|
|
if series == nil {
|
|
|
|
str := fmt.Sprintf("series #%d does not exist, cannot replace it", seriesID)
|
2014-11-04 18:22:13 +01:00
|
|
|
return newError(ErrSeriesNotExists, str, nil)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if series.IsEmpowered() {
|
|
|
|
str := fmt.Sprintf("series #%d has private keys and cannot be replaced", seriesID)
|
2014-11-04 18:22:13 +01:00
|
|
|
return newError(ErrSeriesAlreadyEmpowered, str, nil)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
2017-01-18 21:29:34 +01:00
|
|
|
return p.putSeries(ns, version, seriesID, reqSigs, rawPubKeys)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// decryptExtendedKey uses Manager.Decrypt() to decrypt the encrypted byte slice and return
|
|
|
|
// an extended (public or private) key representing it.
|
2014-11-04 18:22:13 +01:00
|
|
|
//
|
|
|
|
// This method must be called with the Pool's manager unlocked.
|
|
|
|
func (p *Pool) decryptExtendedKey(keyType waddrmgr.CryptoKeyType, encrypted []byte) (*hdkeychain.ExtendedKey, error) {
|
|
|
|
decrypted, err := p.manager.Decrypt(keyType, encrypted)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
str := fmt.Sprintf("cannot decrypt key %v", encrypted)
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, newError(ErrCrypto, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
result, err := hdkeychain.NewKeyFromString(string(decrypted))
|
2015-02-04 02:42:40 +01:00
|
|
|
zero.Bytes(decrypted)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
str := fmt.Sprintf("cannot get key from string %v", decrypted)
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, newError(ErrKeyChain, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateAndDecryptSeriesKeys checks that the length of the public and private key
|
|
|
|
// slices is the same, decrypts them, ensures the non-nil private keys have a matching
|
|
|
|
// public key and returns them.
|
2014-11-04 18:22:13 +01:00
|
|
|
//
|
|
|
|
// This function must be called with the Pool's manager unlocked.
|
|
|
|
func validateAndDecryptKeys(rawPubKeys, rawPrivKeys [][]byte, p *Pool) (pubKeys, privKeys []*hdkeychain.ExtendedKey, err error) {
|
2014-06-13 19:14:44 +02:00
|
|
|
pubKeys = make([]*hdkeychain.ExtendedKey, len(rawPubKeys))
|
|
|
|
privKeys = make([]*hdkeychain.ExtendedKey, len(rawPrivKeys))
|
|
|
|
if len(pubKeys) != len(privKeys) {
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, nil, newError(ErrKeysPrivatePublicMismatch,
|
2014-06-13 19:14:44 +02:00
|
|
|
"the pub key and priv key arrays should have the same number of elements",
|
|
|
|
nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, encryptedPub := range rawPubKeys {
|
2014-11-04 18:22:13 +01:00
|
|
|
pubKey, err := p.decryptExtendedKey(waddrmgr.CKTPublic, encryptedPub)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
pubKeys[i] = pubKey
|
|
|
|
|
|
|
|
encryptedPriv := rawPrivKeys[i]
|
|
|
|
var privKey *hdkeychain.ExtendedKey
|
|
|
|
if encryptedPriv == nil {
|
|
|
|
privKey = nil
|
|
|
|
} else {
|
2014-11-04 18:22:13 +01:00
|
|
|
privKey, err = p.decryptExtendedKey(waddrmgr.CKTPrivate, encryptedPriv)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
privKeys[i] = privKey
|
|
|
|
|
|
|
|
if privKey != nil {
|
|
|
|
checkPubKey, err := privKey.Neuter()
|
|
|
|
if err != nil {
|
|
|
|
str := fmt.Sprintf("cannot neuter key %v", privKey)
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, nil, newError(ErrKeyNeuter, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
if pubKey.String() != checkPubKey.String() {
|
|
|
|
str := fmt.Sprintf("public key %v different than expected %v",
|
|
|
|
pubKey, checkPubKey)
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, nil, newError(ErrKeyMismatch, str, nil)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pubKeys, privKeys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadAllSeries fetches all series (decrypting their public and private
|
|
|
|
// extended keys) for this Pool from the database and populates the
|
|
|
|
// seriesLookup map with them. If there are any private extended keys for
|
|
|
|
// a series, it will also ensure they have a matching extended public key
|
|
|
|
// in that series.
|
2014-11-04 18:22:13 +01:00
|
|
|
//
|
|
|
|
// This method must be called with the Pool's manager unlocked.
|
|
|
|
// FIXME: We should be able to get rid of this (and loadAllSeries/seriesLookup)
|
|
|
|
// by making Series() load the series data directly from the DB.
|
2017-01-18 21:29:34 +01:00
|
|
|
func (p *Pool) LoadAllSeries(ns walletdb.ReadBucket) error {
|
|
|
|
series, err := loadAllSeries(ns, p.ID)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for id, series := range series {
|
|
|
|
pubKeys, privKeys, err := validateAndDecryptKeys(
|
2014-11-04 18:22:13 +01:00
|
|
|
series.pubKeysEncrypted, series.privKeysEncrypted, p)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-04 18:22:13 +01:00
|
|
|
p.seriesLookup[id] = &SeriesData{
|
2014-06-13 19:14:44 +02:00
|
|
|
publicKeys: pubKeys,
|
|
|
|
privateKeys: privKeys,
|
|
|
|
reqSigs: series.reqSigs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the order of the pubkeys based on branch number.
|
|
|
|
// Given the three pubkeys ABC, this would mean:
|
|
|
|
// - branch 0: CBA (reversed)
|
|
|
|
// - branch 1: ABC (first key priority)
|
|
|
|
// - branch 2: BAC (second key priority)
|
|
|
|
// - branch 3: CAB (third key priority)
|
2014-11-04 18:22:13 +01:00
|
|
|
func branchOrder(pks []*hdkeychain.ExtendedKey, branch Branch) ([]*hdkeychain.ExtendedKey, error) {
|
2014-06-13 19:14:44 +02:00
|
|
|
if pks == nil {
|
|
|
|
// This really shouldn't happen, but we want to be good citizens, so we
|
|
|
|
// return an error instead of crashing.
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, newError(ErrInvalidValue, "pks cannot be nil", nil)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
if branch > Branch(len(pks)) {
|
|
|
|
return nil, newError(
|
|
|
|
ErrInvalidBranch, "branch number is bigger than number of public keys", nil)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if branch == 0 {
|
|
|
|
numKeys := len(pks)
|
|
|
|
res := make([]*hdkeychain.ExtendedKey, numKeys)
|
|
|
|
copy(res, pks)
|
|
|
|
// reverse pk
|
|
|
|
for i, j := 0, numKeys-1; i < j; i, j = i+1, j-1 {
|
|
|
|
res[i], res[j] = res[j], res[i]
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp := make([]*hdkeychain.ExtendedKey, len(pks))
|
|
|
|
tmp[0] = pks[branch-1]
|
|
|
|
j := 1
|
|
|
|
for i := 0; i < len(pks); i++ {
|
|
|
|
if i != int(branch-1) {
|
|
|
|
tmp[j] = pks[i]
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tmp, nil
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
// DepositScriptAddress calls DepositScript to get a multi-signature
|
|
|
|
// redemption script and returns the pay-to-script-hash-address for that script.
|
|
|
|
func (p *Pool) DepositScriptAddress(seriesID uint32, branch Branch, index Index) (btcutil.Address, error) {
|
|
|
|
script, err := p.DepositScript(seriesID, branch, index)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-04 18:22:13 +01:00
|
|
|
return p.addressFor(script)
|
|
|
|
}
|
2014-06-13 19:14:44 +02:00
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
func (p *Pool) addressFor(script []byte) (btcutil.Address, error) {
|
|
|
|
scriptHash := btcutil.Hash160(script)
|
|
|
|
return btcutil.NewAddressScriptHashFromHash(scriptHash, p.manager.ChainParams())
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DepositScript constructs and returns a multi-signature redemption script where
|
|
|
|
// a certain number (Series.reqSigs) of the public keys belonging to the series
|
|
|
|
// with the given ID are required to sign the transaction for it to be successful.
|
2014-11-04 18:22:13 +01:00
|
|
|
func (p *Pool) DepositScript(seriesID uint32, branch Branch, index Index) ([]byte, error) {
|
|
|
|
series := p.Series(seriesID)
|
2014-06-13 19:14:44 +02:00
|
|
|
if series == nil {
|
|
|
|
str := fmt.Sprintf("series #%d does not exist", seriesID)
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, newError(ErrSeriesNotExists, str, nil)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pubKeys, err := branchOrder(series.publicKeys, branch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pks := make([]*btcutil.AddressPubKey, len(pubKeys))
|
|
|
|
for i, key := range pubKeys {
|
2014-11-04 18:22:13 +01:00
|
|
|
child, err := key.Child(uint32(index))
|
2014-06-13 19:14:44 +02:00
|
|
|
// TODO: implement getting the next index until we find a valid one,
|
|
|
|
// in case there is a hdkeychain.ErrInvalidChild.
|
|
|
|
if err != nil {
|
|
|
|
str := fmt.Sprintf("child #%d for this pubkey %d does not exist", index, i)
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, newError(ErrKeyChain, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
pubkey, err := child.ECPubKey()
|
|
|
|
if err != nil {
|
|
|
|
str := fmt.Sprintf("child #%d for this pubkey %d does not exist", index, i)
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, newError(ErrKeyChain, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
2015-02-06 06:51:37 +01:00
|
|
|
pks[i], err = btcutil.NewAddressPubKey(pubkey.SerializeCompressed(),
|
2014-11-04 18:22:13 +01:00
|
|
|
p.manager.ChainParams())
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
str := fmt.Sprintf(
|
|
|
|
"child #%d for this pubkey %d could not be converted to an address",
|
|
|
|
index, i)
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, newError(ErrKeyChain, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 19:31:29 +01:00
|
|
|
script, err := txscript.MultiSigScript(pks, int(series.reqSigs))
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
str := fmt.Sprintf("error while making multisig script hash, %d", len(pks))
|
2014-11-04 18:22:13 +01:00
|
|
|
return nil, newError(ErrScriptCreation, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return script, nil
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
// ChangeAddress returns a new votingpool address for the given seriesID and
|
|
|
|
// index, on the 0th branch (which is reserved for change addresses). The series
|
|
|
|
// with the given ID must be active.
|
|
|
|
func (p *Pool) ChangeAddress(seriesID uint32, index Index) (*ChangeAddress, error) {
|
|
|
|
series := p.Series(seriesID)
|
|
|
|
if series == nil {
|
|
|
|
return nil, newError(ErrSeriesNotExists,
|
|
|
|
fmt.Sprintf("series %d does not exist", seriesID), nil)
|
|
|
|
}
|
|
|
|
if !series.active {
|
|
|
|
str := fmt.Sprintf("ChangeAddress must be on active series; series #%d is not", seriesID)
|
|
|
|
return nil, newError(ErrSeriesNotActive, str, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
script, err := p.DepositScript(seriesID, Branch(0), index)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pAddr, err := p.poolAddress(seriesID, Branch(0), index, script)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ChangeAddress{poolAddress: pAddr}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithdrawalAddress queries the address manager for the P2SH address
|
|
|
|
// of the redeem script generated with the given series/branch/index and uses
|
|
|
|
// that to populate the returned WithdrawalAddress. This is done because we
|
|
|
|
// should only withdraw from previously used addresses but also because when
|
|
|
|
// processing withdrawals we may iterate over a huge number of addresses and
|
|
|
|
// it'd be too expensive to re-generate the redeem script for all of them.
|
|
|
|
// This method must be called with the manager unlocked.
|
2017-01-18 21:29:34 +01:00
|
|
|
func (p *Pool) WithdrawalAddress(ns, addrmgrNs walletdb.ReadBucket, seriesID uint32, branch Branch, index Index) (
|
2014-11-04 18:22:13 +01:00
|
|
|
*WithdrawalAddress, error) {
|
|
|
|
// TODO: Ensure the given series is hot.
|
2017-01-18 21:29:34 +01:00
|
|
|
addr, err := p.getUsedAddr(ns, addrmgrNs, seriesID, branch, index)
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if addr == nil {
|
|
|
|
str := fmt.Sprintf("cannot withdraw from unused addr (series: %d, branch: %d, index: %d)",
|
|
|
|
seriesID, branch, index)
|
|
|
|
return nil, newError(ErrWithdrawFromUnusedAddr, str, nil)
|
|
|
|
}
|
|
|
|
script, err := addr.Script()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pAddr, err := p.poolAddress(seriesID, branch, index, script)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &WithdrawalAddress{poolAddress: pAddr}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pool) poolAddress(seriesID uint32, branch Branch, index Index, script []byte) (
|
|
|
|
*poolAddress, error) {
|
|
|
|
addr, err := p.addressFor(script)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &poolAddress{
|
|
|
|
pool: p, seriesID: seriesID, branch: branch, index: index, addr: addr,
|
|
|
|
script: script},
|
|
|
|
nil
|
|
|
|
}
|
|
|
|
|
2014-06-13 19:14:44 +02:00
|
|
|
// EmpowerSeries adds the given extended private key (in raw format) to the
|
|
|
|
// series with the given ID, thus allowing it to sign deposit/withdrawal
|
|
|
|
// scripts. The series with the given ID must exist, the key must be a valid
|
|
|
|
// private extended key and must match one of the series' extended public keys.
|
2014-11-04 18:22:13 +01:00
|
|
|
//
|
|
|
|
// This method must be called with the Pool's manager unlocked.
|
2017-01-18 21:29:34 +01:00
|
|
|
func (p *Pool) EmpowerSeries(ns walletdb.ReadWriteBucket, seriesID uint32, rawPrivKey string) error {
|
2014-06-13 19:14:44 +02:00
|
|
|
// make sure this series exists
|
2014-11-04 18:22:13 +01:00
|
|
|
series := p.Series(seriesID)
|
2014-06-13 19:14:44 +02:00
|
|
|
if series == nil {
|
|
|
|
str := fmt.Sprintf("series %d does not exist for this voting pool",
|
|
|
|
seriesID)
|
2014-11-04 18:22:13 +01:00
|
|
|
return newError(ErrSeriesNotExists, str, nil)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the private key is valid.
|
|
|
|
privKey, err := hdkeychain.NewKeyFromString(rawPrivKey)
|
|
|
|
if err != nil {
|
|
|
|
str := fmt.Sprintf("invalid extended private key %v", rawPrivKey)
|
2014-11-04 18:22:13 +01:00
|
|
|
return newError(ErrKeyChain, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
if !privKey.IsPrivate() {
|
|
|
|
str := fmt.Sprintf(
|
|
|
|
"to empower a series you need the extended private key, not an extended public key %v",
|
|
|
|
privKey)
|
2014-11-04 18:22:13 +01:00
|
|
|
return newError(ErrKeyIsPublic, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pubKey, err := privKey.Neuter()
|
|
|
|
if err != nil {
|
|
|
|
str := fmt.Sprintf("invalid extended private key %v, can't convert to public key",
|
|
|
|
rawPrivKey)
|
2014-11-04 18:22:13 +01:00
|
|
|
return newError(ErrKeyNeuter, str, err)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lookingFor := pubKey.String()
|
|
|
|
found := false
|
|
|
|
|
|
|
|
// Make sure the private key has the corresponding public key in the series,
|
|
|
|
// to be able to empower it.
|
|
|
|
for i, publicKey := range series.publicKeys {
|
|
|
|
if publicKey.String() == lookingFor {
|
|
|
|
found = true
|
|
|
|
series.privateKeys[i] = privKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
str := fmt.Sprintf(
|
|
|
|
"private Key does not have a corresponding public key in this series")
|
2014-11-04 18:22:13 +01:00
|
|
|
return newError(ErrKeysPrivatePublicMismatch, str, nil)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|
|
|
|
|
2017-01-18 21:29:34 +01:00
|
|
|
if err = p.saveSeriesToDisk(ns, seriesID, series); err != nil {
|
2014-11-04 18:22:13 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnsureUsedAddr ensures we have entries in our used addresses DB for the given
|
|
|
|
// seriesID, branch and all indices up to the given one. It must be called with
|
|
|
|
// the manager unlocked.
|
2017-01-18 21:29:34 +01:00
|
|
|
func (p *Pool) EnsureUsedAddr(ns, addrmgrNs walletdb.ReadWriteBucket, seriesID uint32, branch Branch, index Index) error {
|
|
|
|
lastIdx, err := p.highestUsedIndexFor(ns, seriesID, branch)
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if lastIdx == 0 {
|
|
|
|
// highestUsedIndexFor() returns 0 when there are no used addresses for a
|
|
|
|
// given seriesID/branch, so we do this to ensure there is an entry with
|
|
|
|
// index==0.
|
2017-01-18 21:29:34 +01:00
|
|
|
if err := p.addUsedAddr(ns, addrmgrNs, seriesID, branch, lastIdx); err != nil {
|
2014-11-04 18:22:13 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastIdx++
|
|
|
|
for lastIdx <= index {
|
2017-01-18 21:29:34 +01:00
|
|
|
if err := p.addUsedAddr(ns, addrmgrNs, seriesID, branch, lastIdx); err != nil {
|
2014-11-04 18:22:13 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
lastIdx++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-06-13 19:14:44 +02:00
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
// addUsedAddr creates a deposit script for the given seriesID/branch/index,
|
|
|
|
// ensures it is imported into the address manager and finaly adds the script
|
|
|
|
// hash to our used addresses DB. It must be called with the manager unlocked.
|
2017-01-18 21:29:34 +01:00
|
|
|
func (p *Pool) addUsedAddr(ns, addrmgrNs walletdb.ReadWriteBucket, seriesID uint32, branch Branch, index Index) error {
|
2014-11-04 18:22:13 +01:00
|
|
|
script, err := p.DepositScript(seriesID, branch, index)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
// First ensure the address manager has our script. That way there's no way
|
|
|
|
// to have it in the used addresses DB but not in the address manager.
|
|
|
|
// TODO: Decide how far back we want the addr manager to rescan and set the
|
|
|
|
// BlockStamp height according to that.
|
2017-01-18 21:29:34 +01:00
|
|
|
_, err = p.manager.ImportScript(addrmgrNs, script, &waddrmgr.BlockStamp{})
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil && err.(waddrmgr.ManagerError).ErrorCode != waddrmgr.ErrDuplicateAddress {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptedHash, err := p.manager.Encrypt(waddrmgr.CKTPublic, btcutil.Hash160(script))
|
|
|
|
if err != nil {
|
|
|
|
return newError(ErrCrypto, "failed to encrypt script hash", err)
|
|
|
|
}
|
2017-01-18 21:29:34 +01:00
|
|
|
err = putUsedAddrHash(ns, p.ID, seriesID, branch, index, encryptedHash)
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return newError(ErrDatabase, "failed to store used addr script hash", err)
|
|
|
|
}
|
|
|
|
|
2014-06-13 19:14:44 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
// getUsedAddr gets the script hash for the given series, branch and index from
|
|
|
|
// the used addresses DB and uses that to look up the ManagedScriptAddress
|
|
|
|
// from the address manager. It must be called with the manager unlocked.
|
2017-01-18 21:29:34 +01:00
|
|
|
func (p *Pool) getUsedAddr(ns, addrmgrNs walletdb.ReadBucket, seriesID uint32, branch Branch, index Index) (
|
2014-11-04 18:22:13 +01:00
|
|
|
waddrmgr.ManagedScriptAddress, error) {
|
|
|
|
|
|
|
|
mgr := p.manager
|
2017-01-18 21:29:34 +01:00
|
|
|
encryptedHash := getUsedAddrHash(ns, p.ID, seriesID, branch, index)
|
2014-11-04 18:22:13 +01:00
|
|
|
if encryptedHash == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
hash, err := p.manager.Decrypt(waddrmgr.CKTPublic, encryptedHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError(ErrCrypto, "failed to decrypt stored script hash", err)
|
|
|
|
}
|
|
|
|
addr, err := btcutil.NewAddressScriptHashFromHash(hash, mgr.ChainParams())
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError(ErrInvalidScriptHash, "failed to parse script hash", err)
|
|
|
|
}
|
2017-01-18 21:29:34 +01:00
|
|
|
mAddr, err := mgr.Address(addrmgrNs, addr)
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return mAddr.(waddrmgr.ManagedScriptAddress), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// highestUsedIndexFor returns the highest index from this Pool's used addresses
|
|
|
|
// with the given seriesID and branch. It returns 0 if there are no used
|
|
|
|
// addresses with the given seriesID and branch.
|
2017-01-18 21:29:34 +01:00
|
|
|
func (p *Pool) highestUsedIndexFor(ns walletdb.ReadBucket, seriesID uint32, branch Branch) (Index, error) {
|
|
|
|
return getMaxUsedIdx(ns, p.ID, seriesID, branch)
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string encoding of the underlying bitcoin payment address.
|
|
|
|
func (a *poolAddress) String() string {
|
|
|
|
return a.addr.EncodeAddress()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *poolAddress) addrIdentifier() string {
|
|
|
|
return fmt.Sprintf("PoolAddress seriesID:%d, branch:%d, index:%d", a.seriesID, a.branch,
|
|
|
|
a.index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *poolAddress) redeemScript() []byte {
|
|
|
|
return a.script
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *poolAddress) series() *SeriesData {
|
|
|
|
return a.pool.Series(a.seriesID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *poolAddress) SeriesID() uint32 {
|
|
|
|
return a.seriesID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *poolAddress) Branch() Branch {
|
|
|
|
return a.branch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *poolAddress) Index() Index {
|
|
|
|
return a.index
|
|
|
|
}
|
|
|
|
|
2014-06-13 19:14:44 +02:00
|
|
|
// IsEmpowered returns true if this series is empowered (i.e. if it has
|
|
|
|
// at least one private key loaded).
|
|
|
|
func (s *SeriesData) IsEmpowered() bool {
|
|
|
|
for _, key := range s.privateKeys {
|
|
|
|
if key != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
func (s *SeriesData) getPrivKeyFor(pubKey *hdkeychain.ExtendedKey) (*hdkeychain.ExtendedKey, error) {
|
|
|
|
for i, key := range s.publicKeys {
|
|
|
|
if key.String() == pubKey.String() {
|
|
|
|
return s.privateKeys[i], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, newError(ErrUnknownPubKey, fmt.Sprintf("unknown public key '%s'",
|
|
|
|
pubKey.String()), nil)
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|