decred.org/dcrwallet/v3@v3.1.0/wallet/udb/testdata/v1.db.go (about)

     1  // Copyright (c) 2017 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  // This file should compiled from the commit the file was introduced, otherwise
     6  // it may not compile due to API changes, or may not create the database with
     7  // the correct old version.  This file should not be updated for API changes.
     8  
     9  package main
    10  
    11  import (
    12  	"compress/gzip"
    13  	"fmt"
    14  	"io"
    15  	"os"
    16  
    17  	"github.com/decred/dcrd/chaincfg"
    18  	"github.com/decred/dcrutil/hdkeychain"
    19  	_ "github.com/decred/dcrwallet/wallet/internal/bdb"
    20  	"github.com/decred/dcrwallet/wallet/udb"
    21  	"github.com/decred/dcrwallet/wallet/walletdb"
    22  	"github.com/decred/dcrwallet/walletseed"
    23  )
    24  
    25  const dbname = "v1.db"
    26  
    27  var (
    28  	pubPass  = []byte("public")
    29  	privPass = []byte("private")
    30  )
    31  
    32  var chainParams = &chaincfg.TestNet2Params
    33  
    34  func main() {
    35  	err := setup()
    36  	if err != nil {
    37  		fmt.Fprintf(os.Stderr, "setup: %v\n", err)
    38  		os.Exit(1)
    39  	}
    40  	err = compress()
    41  	if err != nil {
    42  		fmt.Fprintf(os.Stderr, "compress: %v\n", err)
    43  		os.Exit(1)
    44  	}
    45  }
    46  
    47  func setup() error {
    48  	db, err := walletdb.Create("bdb", dbname)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	defer db.Close()
    53  	seed, err := walletseed.GenerateRandomSeed(hdkeychain.RecommendedSeedLen)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	err = udb.Initialize(db, chainParams, seed, pubPass, privPass, false)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	amgr, _, _, err := udb.Open(db, chainParams, pubPass)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	return walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
    68  		ns := tx.ReadWriteBucket([]byte("waddrmgr"))
    69  
    70  		err := amgr.Unlock(ns, privPass)
    71  		if err != nil {
    72  			return err
    73  		}
    74  
    75  		accounts := []struct {
    76  			numAddrs  uint32
    77  			usedAddrs []uint32
    78  		}{
    79  			{0, nil},
    80  			{20, []uint32{0, 10, 18}},
    81  			{20, []uint32{0, 10, 18, 19}},
    82  			{20, []uint32{19}},
    83  			{30, []uint32{0, 10, 20, 25}},
    84  			{30, []uint32{9, 18, 29}},
    85  			{30, []uint32{29}},
    86  			{200, []uint32{0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 185}},
    87  			{200, []uint32{199}},
    88  		}
    89  		for i, a := range accounts {
    90  			var account uint32
    91  			if i != 0 {
    92  				account, err = amgr.NewAccount(ns, fmt.Sprintf("account-%d", i))
    93  				if err != nil {
    94  					return err
    95  				}
    96  				if account != uint32(i) {
    97  					panic("NewAccount created wrong account")
    98  				}
    99  			}
   100  			for _, branch := range []uint32{0, 1} {
   101  				if a.numAddrs != 0 {
   102  					// There is an off-by-one in this version of API.  Normally
   103  					// we would pass a.numAddrs-1 here to sync through the last
   104  					// child index (BIP0044 child indexes are zero-indexed) but
   105  					// instead, the total number of addresses is passed as the
   106  					// sync index to work around a bug where the API only
   107  					// generated addresses in the range [0,syncToIndex) instead
   108  					// of [0,syncToIndex].  This has been fixed in the updated
   109  					// code after the DB upgrade has been performed, but that
   110  					// code can't be called here.
   111  					_, err = amgr.SyncAccountToAddrIndex(ns, account, a.numAddrs, branch)
   112  					if err != nil {
   113  						return err
   114  					}
   115  				}
   116  				for _, usedChild := range a.usedAddrs {
   117  					xpub, err := amgr.AccountBranchExtendedPubKey(tx, account, branch)
   118  					if err != nil {
   119  						return err
   120  					}
   121  					childXpub, err := xpub.Child(usedChild)
   122  					if err != nil {
   123  						return err
   124  					}
   125  					addr, err := childXpub.Address(chainParams)
   126  					if err != nil {
   127  						return err
   128  					}
   129  					err = amgr.MarkUsed(ns, addr)
   130  					if err != nil {
   131  						return err
   132  					}
   133  				}
   134  			}
   135  		}
   136  
   137  		return nil
   138  	})
   139  }
   140  
   141  func compress() error {
   142  	db, err := os.Open(dbname)
   143  	if err != nil {
   144  		return err
   145  	}
   146  	defer os.Remove(dbname)
   147  	defer db.Close()
   148  	dbgz, err := os.Create(dbname + ".gz")
   149  	if err != nil {
   150  		return err
   151  	}
   152  	defer dbgz.Close()
   153  	gz := gzip.NewWriter(dbgz)
   154  	_, err = io.Copy(gz, db)
   155  	if err != nil {
   156  		return err
   157  	}
   158  	return gz.Close()
   159  }