Skip to content

Commit

Permalink
udb: add script storage testdata.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnldd committed Jul 8, 2019
1 parent 0988aa6 commit a82156a
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions wallet/udb/testdata/v12.db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (c) 2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

// This file should compiled from the commit the file was introduced, otherwise
// it may not compile due to API changes, or may not create the database with
// the correct old version. This file should not be updated for API changes.

package main

import (
"compress/gzip"
"encoding/hex"
"fmt"
"io"
"os"

"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/hdkeychain"
_ "github.com/decred/dcrwallet/wallet/internal/bdb"
"github.com/decred/dcrwallet/wallet/udb"
"github.com/decred/dcrwallet/wallet/walletdb"
"github.com/decred/dcrwallet/walletseed"
)

const dbname = "v12.db"

var (
pubPass = []byte("public")
privPass = []byte("private")
)

var chainParams = &chaincfg.TestNet3Params

func main() {
err := setup()
if err != nil {
fmt.Fprintf(os.Stderr, "setup: %v\n", err)
os.Exit(1)
}
err = compress()
if err != nil {
fmt.Fprintf(os.Stderr, "compress: %v\n", err)
os.Exit(1)
}
}

func hexToBytes(origHex string) []byte {
buf, err := hex.DecodeString(origHex)
if err != nil {
panic(err)
}
return buf
}

func setup() error {
db, err := walletdb.Create("bdb", dbname)
if err != nil {
return err
}
defer db.Close()
seed, err := walletseed.GenerateRandomSeed(hdkeychain.RecommendedSeedLen)
if err != nil {
return err
}
err = udb.Initialize(db, chainParams, seed, pubPass, privPass)
if err != nil {
return err
}

amgr, _, _, err := udb.Open(db, chainParams, pubPass)
if err != nil {
return err
}

err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket([]byte("waddrmgr"))
if err := amgr.Unlock(ns, privPass); err != nil {
return err
}

script := hexToBytes("51210373c717acda38b5aa4c00c33932e059cdbc" +
"11deceb5f00490a9101704cc444c5151ae")
_, err := amgr.ImportScript(ns, script)
return err
})

return err
}

func compress() error {
db, err := os.Open(dbname)
if err != nil {
return err
}
defer os.Remove(dbname)
defer db.Close()
dbgz, err := os.Create(dbname + ".gz")
if err != nil {
return err
}
defer dbgz.Close()
gz := gzip.NewWriter(dbgz)
_, err = io.Copy(gz, db)
if err != nil {
return err
}
return gz.Close()
}
Binary file added wallet/udb/testdata/v12.db.gz
Binary file not shown.

0 comments on commit a82156a

Please sign in to comment.