vspd/database/addressindex.go
2020-08-19 20:22:12 +00:00

48 lines
1.0 KiB
Go

// Copyright (c) 2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package database
import (
"encoding/binary"
bolt "go.etcd.io/bbolt"
)
// GetLastAddressIndex retrieves the last index used to derive a new fee
// address from the fee xpub key.
func (vdb *VspDatabase) GetLastAddressIndex() (uint32, error) {
var idx uint32
err := vdb.db.View(func(tx *bolt.Tx) error {
vspBkt := tx.Bucket(vspBktK)
idxBytes := vspBkt.Get(lastAddressIndexK)
if idxBytes == nil {
return nil
}
idx = binary.LittleEndian.Uint32(idxBytes)
return nil
})
return idx, err
}
// SetLastAddressIndex updates the last index used to derive a new fee address
// from the fee xpub key.
func (vdb *VspDatabase) SetLastAddressIndex(idx uint32) error {
err := vdb.db.Update(func(tx *bolt.Tx) error {
vspBkt := tx.Bucket(vspBktK)
idxBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(idxBytes, idx)
return vspBkt.Put(lastAddressIndexK, idxBytes)
})
return err
}