2014-06-13 19:14:44 +02:00
|
|
|
/*
|
2017-01-12 15:47:46 +01:00
|
|
|
* Copyright (c) 2014-2017 The btcsuite developers
|
2014-06-13 19:14:44 +02:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package votingpool_test
|
|
|
|
|
|
|
|
import (
|
2014-11-04 18:22:13 +01:00
|
|
|
"bytes"
|
2014-06-13 19:14:44 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-04-28 01:31:27 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/btcsuite/btcwallet/votingpool"
|
|
|
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
|
|
|
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
|
|
|
|
"github.com/btcsuite/btcwallet/wtxmgr"
|
2014-06-13 19:14:44 +02:00
|
|
|
)
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
var (
|
|
|
|
pubPassphrase = []byte("pubPassphrase")
|
|
|
|
privPassphrase = []byte("privPassphrase")
|
2016-03-17 15:05:11 +01:00
|
|
|
seed = bytes.Repeat([]byte{0x2a, 0x64, 0xdf, 0x08}, 8)
|
|
|
|
fastScrypt = &waddrmgr.ScryptOptions{N: 16, R: 8, P: 1}
|
2014-11-04 18:22:13 +01:00
|
|
|
)
|
2014-06-13 19:14:44 +02:00
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
func createWaddrmgr(ns walletdb.ReadWriteBucket, params *chaincfg.Params) (*waddrmgr.Manager, error) {
|
2016-03-17 15:05:11 +01:00
|
|
|
err := waddrmgr.Create(ns, seed, pubPassphrase, privPassphrase, params,
|
|
|
|
fastScrypt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-19 21:24:57 +01:00
|
|
|
return waddrmgr.Open(ns, pubPassphrase, params)
|
2016-03-17 15:05:11 +01:00
|
|
|
}
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
func ExampleCreate() {
|
|
|
|
// Create a new walletdb.DB. See the walletdb docs for instructions on how
|
|
|
|
// to do that.
|
|
|
|
db, dbTearDown, err := createWalletDB()
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
2014-11-04 18:22:13 +01:00
|
|
|
fmt.Println(err)
|
2014-06-13 19:14:44 +02:00
|
|
|
return
|
|
|
|
}
|
2014-11-04 18:22:13 +01:00
|
|
|
defer dbTearDown()
|
2014-06-13 19:14:44 +02:00
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
dbtx, err := db.BeginReadWriteTx()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer dbtx.Commit()
|
|
|
|
|
2014-06-13 19:14:44 +02:00
|
|
|
// Create a new walletdb namespace for the address manager.
|
2017-01-19 21:24:57 +01:00
|
|
|
mgrNamespace, err := dbtx.CreateTopLevelBucket([]byte("waddrmgr"))
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
2014-11-04 18:22:13 +01:00
|
|
|
fmt.Println(err)
|
2014-06-13 19:14:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
// Create the address manager.
|
2016-03-17 15:05:11 +01:00
|
|
|
mgr, err := createWaddrmgr(mgrNamespace, &chaincfg.MainNetParams)
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
2014-11-04 18:22:13 +01:00
|
|
|
fmt.Println(err)
|
2014-06-13 19:14:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
// Create a walletdb namespace for votingpools.
|
2017-01-19 21:24:57 +01:00
|
|
|
vpNamespace, err := dbtx.CreateTopLevelBucket([]byte("votingpool"))
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
2014-11-04 18:22:13 +01:00
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a voting pool.
|
|
|
|
_, err = votingpool.Create(vpNamespace, mgr, []byte{0x00})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
// This example demonstrates how to create a voting pool with one
|
|
|
|
// series and get a deposit address for that series.
|
|
|
|
func Example_depositAddress() {
|
|
|
|
// Create the address manager and votingpool DB namespace. See the example
|
|
|
|
// for the Create() function for more info on how this is done.
|
2017-01-19 21:24:57 +01:00
|
|
|
teardown, db, mgr := exampleCreateDBAndMgr()
|
|
|
|
defer teardown()
|
|
|
|
|
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := votingpoolNamespace(tx)
|
|
|
|
|
|
|
|
// Create the voting pool.
|
|
|
|
pool, err := votingpool.Create(ns, mgr, []byte{0x00})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a 2-of-3 series.
|
|
|
|
seriesID := uint32(1)
|
|
|
|
requiredSignatures := uint32(2)
|
|
|
|
pubKeys := []string{
|
|
|
|
"xpub661MyMwAqRbcFDDrR5jY7LqsRioFDwg3cLjc7tML3RRcfYyhXqqgCH5SqMSQdpQ1Xh8EtVwcfm8psD8zXKPcRaCVSY4GCqbb3aMEs27GitE",
|
|
|
|
"xpub661MyMwAqRbcGsxyD8hTmJFtpmwoZhy4NBBVxzvFU8tDXD2ME49A6JjQCYgbpSUpHGP1q4S2S1Pxv2EqTjwfERS5pc9Q2yeLkPFzSgRpjs9",
|
|
|
|
"xpub661MyMwAqRbcEbc4uYVXvQQpH9L3YuZLZ1gxCmj59yAhNy33vXxbXadmRpx5YZEupNSqWRrR7PqU6duS2FiVCGEiugBEa5zuEAjsyLJjKCh",
|
|
|
|
}
|
|
|
|
err = pool.CreateSeries(ns, votingpool.CurrentVersion, seriesID, requiredSignatures, pubKeys)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a deposit address.
|
|
|
|
addr, err := pool.DepositScriptAddress(seriesID, votingpool.Branch(0), votingpool.Index(1))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("Generated deposit address:", addr.EncodeAddress())
|
|
|
|
return nil
|
|
|
|
})
|
2014-06-13 19:14:44 +02:00
|
|
|
if err != nil {
|
2014-11-04 18:22:13 +01:00
|
|
|
fmt.Println(err)
|
2014-06-13 19:14:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
// Output:
|
|
|
|
// Generated deposit address: 3QTzpc9d3tTbNLJLB7xwt87nWM38boAhAw
|
|
|
|
}
|
|
|
|
|
|
|
|
// This example demonstrates how to empower a series by loading the private
|
|
|
|
// key for one of the series' public keys.
|
|
|
|
func Example_empowerSeries() {
|
|
|
|
// Create the address manager and votingpool DB namespace. See the example
|
|
|
|
// for the Create() function for more info on how this is done.
|
2017-01-19 21:24:57 +01:00
|
|
|
teardown, db, mgr := exampleCreateDBAndMgr()
|
|
|
|
defer teardown()
|
2014-11-04 18:22:13 +01:00
|
|
|
|
|
|
|
// Create a pool and a series. See the DepositAddress example for more info
|
|
|
|
// on how this is done.
|
2017-01-19 21:24:57 +01:00
|
|
|
pool, seriesID := exampleCreatePoolAndSeries(db, mgr)
|
|
|
|
|
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := votingpoolNamespace(tx)
|
|
|
|
addrmgrNs := addrmgrNamespace(tx)
|
|
|
|
|
|
|
|
// Now empower the series with one of its private keys. Notice that in order
|
|
|
|
// to do that we need to unlock the address manager.
|
|
|
|
err := mgr.Unlock(addrmgrNs, privPassphrase)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer mgr.Lock()
|
|
|
|
privKey := "xprv9s21ZrQH143K2j9PK4CXkCu8sgxkpUxCF7p1KVwiV5tdnkeYzJXReUkxz5iB2FUzTXC1L15abCDG4RMxSYT5zhm67uvsnLYxuDhZfoFcB6a"
|
|
|
|
return pool.EmpowerSeries(ns, seriesID, privKey)
|
|
|
|
})
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2014-06-13 19:14:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
2014-11-04 18:22:13 +01:00
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:40:13 +02:00
|
|
|
// This example demonstrates how to use the Pool.StartWithdrawal method.
|
2014-11-04 18:22:13 +01:00
|
|
|
func Example_startWithdrawal() {
|
|
|
|
// Create the address manager and votingpool DB namespace. See the example
|
|
|
|
// for the Create() function for more info on how this is done.
|
2017-01-19 21:24:57 +01:00
|
|
|
teardown, db, mgr := exampleCreateDBAndMgr()
|
|
|
|
defer teardown()
|
2014-11-04 18:22:13 +01:00
|
|
|
|
|
|
|
// Create a pool and a series. See the DepositAddress example for more info
|
|
|
|
// on how this is done.
|
2017-01-19 21:24:57 +01:00
|
|
|
pool, seriesID := exampleCreatePoolAndSeries(db, mgr)
|
|
|
|
|
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := votingpoolNamespace(tx)
|
|
|
|
addrmgrNs := addrmgrNamespace(tx)
|
|
|
|
txmgrNs := txmgrNamespace(tx)
|
|
|
|
|
|
|
|
// Create the transaction store for later use.
|
|
|
|
txstore := exampleCreateTxStore(txmgrNs)
|
|
|
|
|
|
|
|
// Unlock the manager
|
|
|
|
err := mgr.Unlock(addrmgrNs, privPassphrase)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer mgr.Lock()
|
|
|
|
|
|
|
|
addr, _ := btcutil.DecodeAddress("1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", mgr.ChainParams())
|
|
|
|
pkScript, _ := txscript.PayToAddrScript(addr)
|
|
|
|
requests := []votingpool.OutputRequest{
|
|
|
|
{
|
|
|
|
PkScript: pkScript,
|
|
|
|
Address: addr,
|
|
|
|
Amount: 1e6,
|
|
|
|
Server: "server-id",
|
|
|
|
Transaction: 123,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
changeStart, err := pool.ChangeAddress(seriesID, votingpool.Index(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// This is only needed because we have not used any deposit addresses from
|
|
|
|
// the series, and we cannot create a WithdrawalAddress for an unused
|
|
|
|
// branch/idx pair.
|
|
|
|
err = pool.EnsureUsedAddr(ns, addrmgrNs, seriesID, votingpool.Branch(1), votingpool.Index(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
startAddr, err := pool.WithdrawalAddress(ns, addrmgrNs, seriesID, votingpool.Branch(1), votingpool.Index(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lastSeriesID := seriesID
|
|
|
|
dustThreshold := btcutil.Amount(1e4)
|
|
|
|
currentBlock := int32(19432)
|
|
|
|
roundID := uint32(0)
|
|
|
|
_, err = pool.StartWithdrawal(ns, addrmgrNs,
|
|
|
|
roundID, requests, *startAddr, lastSeriesID, *changeStart, txstore, txmgrNs, currentBlock,
|
|
|
|
dustThreshold)
|
|
|
|
return err
|
|
|
|
})
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
func createWalletDB() (walletdb.DB, func(), error) {
|
|
|
|
dir, err := ioutil.TempDir("", "votingpool_example")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
db, err := walletdb.Create("bdb", filepath.Join(dir, "wallet.db"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
dbTearDown := func() {
|
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}
|
|
|
|
return db, dbTearDown, nil
|
|
|
|
}
|
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
var (
|
|
|
|
addrmgrNamespaceKey = []byte("addrmgr")
|
|
|
|
txmgrNamespaceKey = []byte("txmgr")
|
|
|
|
votingpoolNamespaceKey = []byte("votingpool")
|
|
|
|
)
|
|
|
|
|
|
|
|
func addrmgrNamespace(dbtx walletdb.ReadWriteTx) walletdb.ReadWriteBucket {
|
|
|
|
return dbtx.ReadWriteBucket(addrmgrNamespaceKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func txmgrNamespace(dbtx walletdb.ReadWriteTx) walletdb.ReadWriteBucket {
|
|
|
|
return dbtx.ReadWriteBucket(txmgrNamespaceKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func votingpoolNamespace(dbtx walletdb.ReadWriteTx) walletdb.ReadWriteBucket {
|
|
|
|
return dbtx.ReadWriteBucket(votingpoolNamespaceKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func exampleCreateDBAndMgr() (teardown func(), db walletdb.DB, mgr *waddrmgr.Manager) {
|
2014-11-04 18:22:13 +01:00
|
|
|
db, dbTearDown, err := createWalletDB()
|
|
|
|
if err != nil {
|
2017-01-19 21:50:28 +01:00
|
|
|
dbTearDown()
|
2017-01-19 21:24:57 +01:00
|
|
|
panic(err)
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new walletdb namespace for the address manager.
|
2017-01-19 21:24:57 +01:00
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
addrmgrNs, err := tx.CreateTopLevelBucket(addrmgrNamespaceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = tx.CreateTopLevelBucket(votingpoolNamespaceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = tx.CreateTopLevelBucket(txmgrNamespaceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Create the address manager
|
|
|
|
mgr, err = createWaddrmgr(addrmgrNs, &chaincfg.MainNetParams)
|
|
|
|
return err
|
|
|
|
})
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
|
|
|
dbTearDown()
|
2017-01-19 21:24:57 +01:00
|
|
|
panic(err)
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
teardown = func() {
|
2014-11-04 18:22:13 +01:00
|
|
|
mgr.Close()
|
|
|
|
dbTearDown()
|
|
|
|
}
|
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
return teardown, db, mgr
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
func exampleCreatePoolAndSeries(db walletdb.DB, mgr *waddrmgr.Manager) (pool *votingpool.Pool, seriesID uint32) {
|
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := votingpoolNamespace(tx)
|
|
|
|
var err error
|
|
|
|
pool, err = votingpool.Create(ns, mgr, []byte{0x00})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a 2-of-3 series.
|
|
|
|
seriesID = uint32(1)
|
|
|
|
requiredSignatures := uint32(2)
|
|
|
|
pubKeys := []string{
|
|
|
|
"xpub661MyMwAqRbcFDDrR5jY7LqsRioFDwg3cLjc7tML3RRcfYyhXqqgCH5SqMSQdpQ1Xh8EtVwcfm8psD8zXKPcRaCVSY4GCqbb3aMEs27GitE",
|
|
|
|
"xpub661MyMwAqRbcGsxyD8hTmJFtpmwoZhy4NBBVxzvFU8tDXD2ME49A6JjQCYgbpSUpHGP1q4S2S1Pxv2EqTjwfERS5pc9Q2yeLkPFzSgRpjs9",
|
|
|
|
"xpub661MyMwAqRbcEbc4uYVXvQQpH9L3YuZLZ1gxCmj59yAhNy33vXxbXadmRpx5YZEupNSqWRrR7PqU6duS2FiVCGEiugBEa5zuEAjsyLJjKCh",
|
|
|
|
}
|
|
|
|
err = pool.CreateSeries(ns, votingpool.CurrentVersion, seriesID, requiredSignatures, pubKeys)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return pool.ActivateSeries(ns, seriesID)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pool, seriesID
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
func exampleCreateTxStore(ns walletdb.ReadWriteBucket) *wtxmgr.Store {
|
|
|
|
err := wtxmgr.Create(ns)
|
2016-03-17 15:05:11 +01:00
|
|
|
if err != nil {
|
2017-01-19 21:24:57 +01:00
|
|
|
panic(err)
|
2016-03-17 15:05:11 +01:00
|
|
|
}
|
2017-01-19 21:24:57 +01:00
|
|
|
s, err := wtxmgr.Open(ns, &chaincfg.MainNetParams)
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
2017-01-19 21:24:57 +01:00
|
|
|
panic(err)
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
2017-01-19 21:24:57 +01:00
|
|
|
return s
|
2014-06-13 19:14:44 +02:00
|
|
|
}
|