Skip to content

Commit fd6c672

Browse files
committed
walletinfo: add --dumpaddrs flag
1 parent afffafe commit fd6c672

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

cmd/chantools/walletinfo.go

+41-3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ var (
5656
type walletInfoCommand struct {
5757
WalletDB string
5858
WithRootKey bool
59+
DumpAddrs bool
5960

6061
cmd *cobra.Command
6162
}
@@ -85,6 +86,10 @@ in the wallet.db.`,
8586
&cc.WithRootKey, "withrootkey", false, "print BIP32 HD root "+
8687
"key of wallet to standard out",
8788
)
89+
cc.cmd.Flags().BoolVar(
90+
&cc.DumpAddrs, "dumpaddrs", false, "print all addresses, "+
91+
"including private keys",
92+
)
8893

8994
return cc.cmd
9095
}
@@ -161,7 +166,7 @@ func (c *walletInfoCommand) Execute(_ *cobra.Command, _ []string) error {
161166
}
162167

163168
// Print the wallet info and if requested the root key.
164-
identityKey, scopeInfo, err := walletInfo(w)
169+
identityKey, scopeInfo, err := walletInfo(w, c.DumpAddrs)
165170
if err != nil {
166171
return err
167172
}
@@ -187,7 +192,9 @@ func (c *walletInfoCommand) Execute(_ *cobra.Command, _ []string) error {
187192
return nil
188193
}
189194

190-
func walletInfo(w *wallet.Wallet) (*btcec.PublicKey, string, error) {
195+
func walletInfo(w *wallet.Wallet, dumpAddrs bool) (*btcec.PublicKey, string,
196+
error) {
197+
191198
keyRing := keychain.NewBtcWalletKeyRing(w, chainParams.HDCoinType)
192199
idPrivKey, err := keyRing.DerivePrivKey(keychain.KeyDescriptor{
193200
KeyLocator: keychain.KeyLocator{
@@ -218,7 +225,38 @@ func walletInfo(w *wallet.Wallet) (*btcec.PublicKey, string, error) {
218225
return nil, "", err
219226
}
220227

221-
return idPrivKey.PubKey(), scopeNp2wkh + scopeP2wkh, nil
228+
scopeAddrs := "\n"
229+
if dumpAddrs {
230+
printAddr := func(a waddrmgr.ManagedAddress) error {
231+
pka := a.(waddrmgr.ManagedPubKeyAddress)
232+
scope, path, _ := pka.DerivationInfo()
233+
scopeAddrs += fmt.Sprintf(
234+
"path=m/%d'/%d'/%d'/%d/%d, pubkey=%x, "+
235+
"addr=%s, hash160=%x\n",
236+
scope.Purpose, scope.Coin, path.InternalAccount,
237+
path.Branch, path.Index,
238+
pka.PubKey().SerializeCompressed(),
239+
pka.Address().String(), a.AddrHash(),
240+
)
241+
return nil
242+
}
243+
for _, mgr := range w.Manager.ActiveScopedKeyManagers() {
244+
err = walletdb.View(
245+
w.Database(), func(tx walletdb.ReadTx) error {
246+
247+
waddrmgrNs := tx.ReadBucket(
248+
waddrmgrNamespaceKey,
249+
)
250+
251+
return mgr.ForEachAccountAddress(
252+
waddrmgrNs, 0, printAddr,
253+
)
254+
},
255+
)
256+
}
257+
}
258+
259+
return idPrivKey.PubKey(), scopeNp2wkh + scopeP2wkh + scopeAddrs, nil
222260
}
223261

224262
func printScopeInfo(name string, w *wallet.Wallet,

0 commit comments

Comments
 (0)