Skip to content

Instantly share code, notes, and snippets.

@Emyrk
Last active April 30, 2017 03:00
Show Gist options
  • Save Emyrk/0041629d14fd0d264eb3d6570e05ec5f to your computer and use it in GitHub Desktop.
Save Emyrk/0041629d14fd0d264eb3d6570e05ec5f to your computer and use it in GitHub Desktop.
Read all addresses from v1 wallet
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/FactomProject/factom/wallet"
)
var _ = wallet.ApiVersion
func main() {
var (
// Optionally find files in other directory
dir = flag.String("dir", ".", "Path to directory")
)
flag.Parse()
os.Remove("new.db")
path := *dir
if *dir == "." {
path = ""
}
// Find all files in this directory
files, err := ioutil.ReadDir(*dir)
if err != nil {
log.Fatalf("Had an issue reading the directory: %s\n", err.Error())
}
var dbfiles []os.FileInfo
for _, f := range files {
if strings.Contains(path+f.Name(), ".db") {
dbfiles = append(dbfiles, f)
}
}
fmt.Printf("Found %d db files...\n", len(dbfiles))
for _, dbf := range dbfiles {
fmt.Printf(" |- %s\n", dbf.Name())
}
// Go through files
for _, f := range dbfiles {
openFile(path, f)
}
}
func openFile(path string, f os.FileInfo) {
defer func() {
if r := recover(); r != nil {
fmt.Printf("Panic with file %s: %v\n", f.Name(), r)
}
}()
// Only open *.db files
if strings.Contains(f.Name(), ".db") {
if _, err := os.Stat(f.Name()); err == nil {
} else {
// file does not exist, just skipt it
return
}
// Import V1 wallet
wal, err := wallet.ImportV1Wallet(f.Name(), "new.db")
if err != nil {
log.Printf("%s could not be opened: %s\n", f.Name(), err.Error())
} else {
fas, _, err := wal.GetAllAddresses()
if err != nil {
log.Printf("%s was opened, but addresses could not be retrieved: %s\n", f.Name(), err.Error())
} else {
fmt.Printf("- %s has %d factoid addresses\n", f.Name(), len(fas))
for _, fa := range fas {
fmt.Printf("-- Factoid Address from %s:\n %s\n %s\n", f.Name(), fa.SecString(), fa.String())
}
}
// Clean up files created
wal.Close()
os.Remove("new.db")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment