func newWallet(votingEnabled bool, addressReuse bool, ticketAddress hcashutil.Address,
	poolAddress hcashutil.Address, pf float64, relayFee, ticketFee hcashutil.Amount,
	gapLimit int, stakePoolColdAddrs map[string]struct{}, AllowHighFees bool,
	mgr *udb.Manager, txs *udb.Store, smgr *udb.StakeStore, db *walletdb.DB,
	params *chaincfg.Params, privpass []byte) (*Wallet, error) {
	w := &Wallet{
		db:                       *db,
		Manager:                  mgr,
		TxStore:                  txs,
		StakeMgr:                 smgr,
		votingEnabled:            votingEnabled,
		lockedOutpoints:          map[wire.OutPoint]struct{}{},
		relayFee:                 relayFee,
		ticketFeeIncrement:       ticketFee,
		AllowHighFees:            AllowHighFees,
		consolidateRequests:      make(chan consolidateRequest),
		createTxRequests:         make(chan createTxRequest),
		createMultisigTxRequests: make(chan createMultisigTxRequest),
		createSStxRequests:       make(chan createSStxRequest),
		createSSGenRequests:      make(chan createSSGenRequest),
		createSSRtxRequests:      make(chan createSSRtxRequest),
		purchaseTicketRequests:   make(chan purchaseTicketRequest),
		addressReuse:             addressReuse,
		ticketAddress:            ticketAddress,
		addressBuffers:           make(map[uint32]*bip0044AccountData),
		poolAddress:              poolAddress,
		poolFees:                 pf,
		gapLimit:                 gapLimit,
		stakePoolEnabled:         len(stakePoolColdAddrs) > 0,
		stakePoolColdAddrs:       stakePoolColdAddrs,
		subsidyCache:             blockchain.NewSubsidyCache(0, params),
		initiallyUnlocked:        false,
		unlockRequests:           make(chan unlockRequest),
		lockRequests:             make(chan struct{}),
		holdUnlockRequests:       make(chan chan heldUnlock),
		lockState:                make(chan bool),
		changePassphrase:         make(chan changePassphraseRequest),
		chainParams:              params,
		quit:                     make(chan struct{}),
	}
	w.wg.Add(1)
	go w.walletLocker()
	var vb stake.VoteBits
	var unlockAfter <-chan time.Time
	var extKey, intKey *hdkeychain.ExtendedKey
	err := w.Unlock(privpass, unlockAfter)
	if err != nil {
		return  nil, err
	}
	walletdb.View(w.db, func(tx walletdb.ReadTx) error {
		ns := tx.ReadBucket(waddrmgrNamespaceKey)
		lastAcct, err := w.Manager.LastAccount(ns)
		if err != nil {
			return err
		}
		for acct := uint32(0); acct <= lastAcct; acct++ {
			xpub, err := w.Manager.AccountExtendedPubKey(tx, acct)
			if err != nil {
				return err
			}
			props, err := w.Manager.AccountProperties(ns, acct)
			if err != nil {
				return err
			}
			if props.AccountType == udb.AcctypeEc {
				extKey, intKey, err = deriveBranches(xpub)
				if err != nil {
					return err
				}
			}
			if props.AccountType == udb.AcctypeBliss {
				xpriv, err := w.Manager.AccountExtendedPrivKey(tx, acct)
				if err != nil {
					return err
				}
				extpriv, err := xpriv.Child(udb.ExternalBranch)
				if err != nil {
					return err
				}
				extKey, err = extpriv.Neuter()
				if err != nil {
					return err
				}
				intpriv, err := xpriv.Child(udb.InternalBranch)
				if err != nil {
					return err
				}
				intKey, err = intpriv.Neuter()
				if err != nil {
					return err
				}
				xpriv.Zero()
				intpriv.Zero()
				extpriv.Zero()
			}
			w.addressBuffers[acct] = &bip0044AccountData{
				albExternal: addressBuffer{
					branchXpub: extKey,
					lastUsed:   props.LastUsedExternalIndex,
					cursor:     props.LastReturnedExternalIndex - props.LastUsedExternalIndex,
				},
				albInternal: addressBuffer{
					branchXpub: intKey,
					lastUsed:   props.LastUsedInternalIndex,
					cursor:     props.LastReturnedInternalIndex - props.LastUsedInternalIndex,
				},
			}
		}
		vb = w.readDBVoteBits(tx)
		return nil
	})
	w.NtfnServer = newNotificationServer(w)
	w.voteBits = vb
	return w, nil
}
newWallet creates a new Wallet structure with the provided address manager
and transaction store.
package wallet
import (
	"bytes"
	"encoding/binary"
	"encoding/hex"
	"errors"
	"fmt"
	"sort"
	"strconv"
	"strings"
	"sync"
	"time"
	"github.com/HcashOrg/bitset"
	"github.com/HcashOrg/hcashd/blockchain"
	"github.com/HcashOrg/hcashd/blockchain/stake"
	"github.com/HcashOrg/hcashd/chaincfg"
	"github.com/HcashOrg/hcashd/chaincfg/chainec"
	"github.com/HcashOrg/hcashd/chaincfg/chainhash"
	"github.com/HcashOrg/hcashd/hcashec/secp256k1"
	"github.com/HcashOrg/hcashd/hcashjson"
	"github.com/HcashOrg/hcashd/txscript"
	"github.com/HcashOrg/hcashd/wire"
	"github.com/HcashOrg/hcashrpcclient"
	"github.com/HcashOrg/hcashutil"
	"github.com/HcashOrg/hcashutil/hdkeychain"
	"github.com/HcashOrg/hcashwallet/apperrors"
	"github.com/HcashOrg/hcashwallet/chain"
	"github.com/HcashOrg/hcashwallet/wallet/txauthor"
	"github.com/HcashOrg/hcashwallet/wallet/txrules"
	"github.com/HcashOrg/hcashwallet/wallet/udb"
	"github.com/HcashOrg/hcashwallet/walletdb"
	"github.com/HcashOrg/hcashd/crypto/bliss"
)
const (
	InsecurePubPassphrase = "public"
)
var (
	SendBits 	   = 0x20
	ReceiveBits    = 0x10
	CoinbaseBits   = 0x8
	TicketBits	   = 0x4
	VotesBits	   = 0x2
	RevocationBits = 0x1
)
var (
	SimulationPassphrase = []byte("password")
)
var (
	waddrmgrNamespaceKey  = []byte("waddrmgr")
	wtxmgrNamespaceKey    = []byte("wtxmgr")
	wstakemgrNamespaceKey = []byte("wstakemgr")
)
type StakeDifficultyInfo struct {
	BlockHash       *chainhash.Hash
	BlockHeight     int64
	StakeDifficulty int64
}
type Wallet struct {
	db       walletdb.DB
	Manager  *udb.Manager
	TxStore  *udb.Store
	StakeMgr *udb.StakeStore
	stakeSettingsLock       sync.Mutex
	voteBits                stake.VoteBits
	ticketPurchasingEnabled bool
	votingEnabled           bool
	balanceToMaintain       hcashutil.Amount
	poolAddress             hcashutil.Address
	poolFees                float64
	stakePoolEnabled        bool
	stakePoolColdAddrs      map[string]struct{}
	subsidyCache            *blockchain.SubsidyCache
	initiallyUnlocked bool
	gapLimit          int
	chainClient     *chain.RPCClient
	chainClientLock sync.Mutex
	lockedOutpoints map[wire.OutPoint]struct{}
	relayFee               hcashutil.Amount
	relayFeeMu             sync.Mutex
	ticketFeeIncrementLock sync.Mutex
	ticketFeeIncrement     hcashutil.Amount
	DisallowFree           bool
	AllowHighFees          bool
	consolidateRequests      chan consolidateRequest
	createTxRequests         chan createTxRequest
	createMultisigTxRequests chan createMultisigTxRequest
	createSStxRequests     chan createSStxRequest
	createSSGenRequests    chan createSSGenRequest
	createSSRtxRequests    chan createSSRtxRequest
	purchaseTicketRequests chan purchaseTicketRequest
	addressReuse     bool
	ticketAddress    hcashutil.Address
	addressBuffers   map[uint32]*bip0044AccountData
	addressBuffersMu sync.Mutex
	unlockRequests     chan unlockRequest
	lockRequests       chan struct{}
	holdUnlockRequests chan chan heldUnlock
	lockState          chan bool
	changePassphrase   chan changePassphraseRequest
	reorganizingLock sync.Mutex
	reorganizeToHash chainhash.Hash
	sideChain        []sideChainBlock
	reorganizing     bool
	NtfnServer *NotificationServer
	chainParams *chaincfg.Params
	wg sync.WaitGroup
	started bool
	quit    chan struct{}
	quitMu  sync.Mutex
}