mirror of
https://github.com/LBRYFoundation/lbcwallet.git
synced 2025-08-28 16:01:29 +00:00
Add wallet func to return a change address.
Previous to this commit, all change addresses were indistinguishable from manually requested addresses. This adds a new function to return the new address, setting a new change flag to true, and return the change status with the AddressInfo. This is needed as part of resolving #41 (getrawchangeaddress).
This commit is contained in:
parent
6a08c7de07
commit
b3bb0481b0
2 changed files with 36 additions and 7 deletions
|
@ -240,7 +240,7 @@ func (a *Account) txToPairs(pairs map[string]int64, minconf int) (*CreatedTx, er
|
||||||
|
|
||||||
// Get a new change address if one has not already been found.
|
// Get a new change address if one has not already been found.
|
||||||
if changeAddr == nil {
|
if changeAddr == nil {
|
||||||
changeAddr, err = a.NextChainedAddress(&bs, cfg.KeypoolSize)
|
changeAddr, err = a.ChangeAddress(&bs, cfg.KeypoolSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get next address: %s", err)
|
return nil, fmt.Errorf("failed to get next address: %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -894,9 +894,29 @@ func (w *Wallet) IsLocked() bool {
|
||||||
// is used. If not and the wallet is unlocked, the keypool is extended.
|
// is used. If not and the wallet is unlocked, the keypool is extended.
|
||||||
// If locked, a new address's pubkey is chained off the last pubkey
|
// If locked, a new address's pubkey is chained off the last pubkey
|
||||||
// and added to the wallet.
|
// and added to the wallet.
|
||||||
func (w *Wallet) NextChainedAddress(bs *BlockStamp,
|
func (w *Wallet) NextChainedAddress(bs *BlockStamp, keypoolSize uint) (*btcutil.AddressPubKeyHash, error) {
|
||||||
keypoolSize uint) (*btcutil.AddressPubKeyHash, error) {
|
addr, err := w.nextChainedAddress(bs, keypoolSize)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create and return payment address for address hash.
|
||||||
|
return addr.address(w.net), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Wallet) ChangeAddress(bs *BlockStamp, keypoolSize uint) (*btcutil.AddressPubKeyHash, error) {
|
||||||
|
addr, err := w.nextChainedAddress(bs, keypoolSize)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
addr.flags.change = true
|
||||||
|
|
||||||
|
// Create and return payment address for address hash.
|
||||||
|
return addr.address(w.net), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Wallet) nextChainedAddress(bs *BlockStamp, keypoolSize uint) (*btcAddress, error) {
|
||||||
// Attempt to get address hash of next chained address.
|
// Attempt to get address hash of next chained address.
|
||||||
nextAPKH, ok := w.chainIdxMap[w.highestUsed+1]
|
nextAPKH, ok := w.chainIdxMap[w.highestUsed+1]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -927,8 +947,7 @@ func (w *Wallet) NextChainedAddress(bs *BlockStamp,
|
||||||
|
|
||||||
w.highestUsed++
|
w.highestUsed++
|
||||||
|
|
||||||
// Create and return payment address for address hash.
|
return addr, nil
|
||||||
return addr.address(w.net), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LastChainedAddress returns the most recently requested chained
|
// LastChainedAddress returns the most recently requested chained
|
||||||
|
@ -1382,6 +1401,7 @@ type AddressInfo struct {
|
||||||
FirstBlock int32
|
FirstBlock int32
|
||||||
Imported bool
|
Imported bool
|
||||||
Pubkey string
|
Pubkey string
|
||||||
|
Change bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// SortedActiveAddresses returns all wallet addresses that have been
|
// SortedActiveAddresses returns all wallet addresses that have been
|
||||||
|
@ -1491,6 +1511,7 @@ type addrFlags struct {
|
||||||
encrypted bool
|
encrypted bool
|
||||||
createPrivKeyNextUnlock bool
|
createPrivKeyNextUnlock bool
|
||||||
compressed bool
|
compressed bool
|
||||||
|
change bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (af *addrFlags) ReadFrom(r io.Reader) (int64, error) {
|
func (af *addrFlags) ReadFrom(r io.Reader) (int64, error) {
|
||||||
|
@ -1505,6 +1526,7 @@ func (af *addrFlags) ReadFrom(r io.Reader) (int64, error) {
|
||||||
af.encrypted = b[0]&(1<<2) != 0
|
af.encrypted = b[0]&(1<<2) != 0
|
||||||
af.createPrivKeyNextUnlock = b[0]&(1<<3) != 0
|
af.createPrivKeyNextUnlock = b[0]&(1<<3) != 0
|
||||||
af.compressed = b[0]&(1<<4) != 0
|
af.compressed = b[0]&(1<<4) != 0
|
||||||
|
af.change = b[0]&(1<<5) != 0
|
||||||
|
|
||||||
// Currently (at least until watching-only wallets are implemented)
|
// Currently (at least until watching-only wallets are implemented)
|
||||||
// btcwallet shall refuse to open any unencrypted addresses. This
|
// btcwallet shall refuse to open any unencrypted addresses. This
|
||||||
|
@ -1539,6 +1561,9 @@ func (af *addrFlags) WriteTo(w io.Writer) (int64, error) {
|
||||||
if af.compressed {
|
if af.compressed {
|
||||||
b[0] |= 1 << 4
|
b[0] |= 1 << 4
|
||||||
}
|
}
|
||||||
|
if af.change {
|
||||||
|
b[0] |= 1 << 5
|
||||||
|
}
|
||||||
|
|
||||||
n, err := w.Write(b[:])
|
n, err := w.Write(b[:])
|
||||||
return int64(n), err
|
return int64(n), err
|
||||||
|
@ -1896,9 +1921,10 @@ func newBtcAddress(privkey, iv []byte, bs *BlockStamp, compressed bool) (addr *b
|
||||||
flags: addrFlags{
|
flags: addrFlags{
|
||||||
hasPrivKey: true,
|
hasPrivKey: true,
|
||||||
hasPubKey: true,
|
hasPubKey: true,
|
||||||
|
encrypted: false, // will be, but isn't yet.
|
||||||
createPrivKeyNextUnlock: false,
|
createPrivKeyNextUnlock: false,
|
||||||
compressed: compressed,
|
compressed: compressed,
|
||||||
encrypted: false, // will be, but isn't yet.
|
change: false,
|
||||||
},
|
},
|
||||||
firstSeen: time.Now().Unix(),
|
firstSeen: time.Now().Unix(),
|
||||||
firstBlock: bs.Height,
|
firstBlock: bs.Height,
|
||||||
|
@ -1940,9 +1966,10 @@ func newBtcAddressWithoutPrivkey(pubkey, iv []byte, bs *BlockStamp) (addr *btcAd
|
||||||
flags: addrFlags{
|
flags: addrFlags{
|
||||||
hasPrivKey: false,
|
hasPrivKey: false,
|
||||||
hasPubKey: true,
|
hasPubKey: true,
|
||||||
|
encrypted: false,
|
||||||
createPrivKeyNextUnlock: true,
|
createPrivKeyNextUnlock: true,
|
||||||
compressed: compressed,
|
compressed: compressed,
|
||||||
encrypted: false,
|
change: false,
|
||||||
},
|
},
|
||||||
firstSeen: time.Now().Unix(),
|
firstSeen: time.Now().Unix(),
|
||||||
firstBlock: bs.Height,
|
firstBlock: bs.Height,
|
||||||
|
@ -2244,6 +2271,7 @@ func (a *btcAddress) info(net btcwire.BitcoinNet) (*AddressInfo, error) {
|
||||||
FirstBlock: a.firstBlock,
|
FirstBlock: a.firstBlock,
|
||||||
Imported: a.chainIndex == importedKeyChainIdx,
|
Imported: a.chainIndex == importedKeyChainIdx,
|
||||||
Pubkey: hex.EncodeToString(a.pubKey),
|
Pubkey: hex.EncodeToString(a.pubKey),
|
||||||
|
Change: a.flags.change,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2259,6 +2287,7 @@ func (a *btcAddress) watchingCopy() *btcAddress {
|
||||||
encrypted: false,
|
encrypted: false,
|
||||||
createPrivKeyNextUnlock: false,
|
createPrivKeyNextUnlock: false,
|
||||||
compressed: a.flags.compressed,
|
compressed: a.flags.compressed,
|
||||||
|
change: a.flags.change,
|
||||||
},
|
},
|
||||||
chaincode: a.chaincode,
|
chaincode: a.chaincode,
|
||||||
chainIndex: a.chainIndex,
|
chainIndex: a.chainIndex,
|
||||||
|
|
Loading…
Add table
Reference in a new issue