Skip to content

Commit 2521b8d

Browse files
committed
Multiple fixes for genimportscript
1 parent 32df611 commit 2521b8d

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

cmd/chantools/genimportscript.go

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package main
22

33
import (
44
"fmt"
5-
"strconv"
6-
"strings"
75
"time"
86

97
"github.com/btcsuite/btcd/chaincfg"
8+
"github.com/btcsuite/btcd/txscript"
109
"github.com/btcsuite/btcutil"
1110
"github.com/btcsuite/btcutil/hdkeychain"
1211
"github.com/guggero/chantools/lnd"
@@ -64,28 +63,9 @@ func (c *genImportScriptCommand) Execute(_ []string) error {
6463
c.DerivationPath = defaultDerivationPath
6564
}
6665

67-
// Process derivation path
68-
levels := strings.Split(c.DerivationPath, "/")
69-
if len(levels) == 0 || levels[0] != "m" {
70-
return fmt.Errorf("error reading derivationpath: path \"%s\" not in "+
71-
"correct format, e.g. \"m/purpose'/coin_type'/account'\"", c.DerivationPath)
72-
}
73-
levels = levels[1:] // removes masterseed purposed "m"
74-
75-
derivationPath := make([]uint32, len(levels))
76-
for i := range levels {
77-
unHardened := strings.TrimSuffix(levels[i], "'")
78-
d, err := strconv.Atoi(unHardened)
79-
if err != nil {
80-
return fmt.Errorf("error reading derivationpath: <%s> is not a valid "+
81-
"derivation", unHardened)
82-
}
83-
84-
if levels[i] == unHardened {
85-
derivationPath[i] = uint32(d)
86-
} else {
87-
derivationPath[i] = lnd.HardenedKeyStart + uint32(d)
88-
}
66+
derivationPath, err := lnd.ParsePath(c.DerivationPath)
67+
if err != nil {
68+
return fmt.Errorf("error parsing path: %v", err)
8969
}
9070

9171
fmt.Printf("# Wallet dump created by chantools on %s\n",
@@ -115,7 +95,7 @@ func (c *genImportScriptCommand) Execute(_ []string) error {
11595

11696
// External branch first (<DerivationPath>/0/i).
11797
for i := uint32(0); i < c.RecoveryWindow; i++ {
118-
path := append(derivationPath, []uint32{0, i}...)
98+
path := append(derivationPath, 0, i)
11999
derivedKey, err := lnd.DeriveChildren(extendedKey, path)
120100
if err != nil {
121101
return err
@@ -128,7 +108,7 @@ func (c *genImportScriptCommand) Execute(_ []string) error {
128108

129109
// Now the internal branch (<DerivationPath>/1/i).
130110
for i := uint32(0); i < c.RecoveryWindow; i++ {
131-
path := append(derivationPath, []uint32{1, i}...)
111+
path := append(derivationPath, 1, i)
132112
derivedKey, err := lnd.DeriveChildren(extendedKey, path)
133113
if err != nil {
134114
return err
@@ -192,17 +172,30 @@ func printBitcoinImportWallet(hdKey *hdkeychain.ExtendedKey, path string,
192172
return fmt.Errorf("could not derive private key: %v",
193173
err)
194174
}
195-
addrPubkey, err := btcutil.NewAddressPubKey(
196-
pubKey.SerializeCompressed(), chainParams,
175+
hash160 := btcutil.Hash160(pubKey.SerializeCompressed())
176+
addrP2PKH, err := btcutil.NewAddressPubKeyHash(hash160, chainParams)
177+
if err != nil {
178+
return fmt.Errorf("could not create address: %v", err)
179+
}
180+
addrP2WKH, err := btcutil.NewAddressWitnessPubKeyHash(
181+
hash160, chainParams,
197182
)
198183
if err != nil {
199184
return fmt.Errorf("could not create address: %v", err)
200185
}
201-
addr := addrPubkey.AddressPubKeyHash()
186+
script, err := txscript.PayToAddrScript(addrP2WKH)
187+
if err != nil {
188+
return fmt.Errorf("could not create script: %v", err)
189+
}
190+
addrNP2WKH, err := btcutil.NewAddressScriptHash(script, chainParams)
191+
if err != nil {
192+
return fmt.Errorf("could not create address: %v", err)
193+
}
202194

203195
fmt.Printf("%s 1970-01-01T00:00:01Z label=%s/%d/%d/ "+
204-
"# addr=%s\n", wif.String(), path, branch, index,
205-
addr.EncodeAddress(),
196+
"# addr=%s,%s,%s\n", wif.String(), path, branch, index,
197+
addrP2PKH.EncodeAddress(), addrNP2WKH.EncodeAddress(),
198+
addrP2WKH.EncodeAddress(),
206199
)
207200
return nil
208201
}
@@ -214,10 +207,13 @@ func seedBirthdayToBlock(birthdayTimestamp time.Time) uint32 {
214207
genesisTimestamp =
215208
chaincfg.MainNetParams.GenesisBlock.Header.Timestamp
216209

217-
case "testnet":
210+
case "testnet3":
218211
genesisTimestamp =
219212
chaincfg.TestNet3Params.GenesisBlock.Header.Timestamp
220213

214+
case "regtest", "simnet":
215+
return 0
216+
221217
default:
222218
panic(fmt.Errorf("unimplemented network %v", chainParams.Name))
223219
}

0 commit comments

Comments
 (0)