Skip to content

Commit ff4f4a2

Browse files
committed
derivekey+genimportscript: add p2tr address support
1 parent fd6c672 commit ff4f4a2

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

btc/bitcoind.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,15 @@ func (i *ImportWallet) Format(hdKey *hdkeychain.ExtendedKey,
209209
if err != nil {
210210
return "", fmt.Errorf("could not create address: %w", err)
211211
}
212+
addrP2TR, err := lnd.P2TRAddr(privKey.PubKey(), params)
213+
if err != nil {
214+
return "", fmt.Errorf("could not create address: %w", err)
215+
}
212216

213217
return fmt.Sprintf("%s 1970-01-01T00:00:01Z label=%s/%d/%d/ "+
214-
"# addr=%s,%s,%s", wif.String(), path, branch, index,
218+
"# addr=%s,%s,%s,%s", wif.String(), path, branch, index,
215219
addrP2PKH.EncodeAddress(), addrNP2WKH.EncodeAddress(),
216-
addrP2WKH.EncodeAddress(),
220+
addrP2WKH.EncodeAddress(), addrP2TR.EncodeAddress(),
217221
), nil
218222
}
219223

cmd/chantools/derivekey.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
65
"github.com/btcsuite/btcd/btcutil"
76
"github.com/btcsuite/btcd/btcutil/hdkeychain"
87
"github.com/guggero/chantools/lnd"
@@ -16,6 +15,7 @@ Public key: %x
1615
Extended public key (xpub): %v
1716
Address: %v
1817
Legacy address: %v
18+
Taproot address: %v
1919
Private key (WIF): %s
2020
Extended private key (xprv): %s
2121
`
@@ -99,6 +99,11 @@ func deriveKey(extendedKey *hdkeychain.ExtendedKey, path string,
9999
return fmt.Errorf("could not create address: %w", err)
100100
}
101101

102+
addrP2TR, err := lnd.P2TRAddr(pubKey, chainParams)
103+
if err != nil {
104+
return fmt.Errorf("could not create address: %w", err)
105+
}
106+
102107
privKey, xPriv := na, na
103108
if !neuter {
104109
privKey, xPriv = wif.String(), child.String()
@@ -107,7 +112,7 @@ func deriveKey(extendedKey *hdkeychain.ExtendedKey, path string,
107112
result := fmt.Sprintf(
108113
deriveKeyFormat, path, chainParams.Name,
109114
pubKey.SerializeCompressed(), neutered, addrP2WKH, addrP2PKH,
110-
privKey, xPriv,
115+
addrP2TR, privKey, xPriv,
111116
)
112117
fmt.Println(result)
113118

cmd/chantools/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
const (
2828
defaultAPIURL = "https://blockstream.info/api"
29-
version = "0.10.5"
29+
version = "0.10.6"
3030
na = "n/a"
3131

3232
Commit = ""

lnd/hdkeychain.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lnd
33
import (
44
"crypto/sha256"
55
"fmt"
6+
"github.com/btcsuite/btcd/btcec/v2/schnorr"
67
"strconv"
78
"strings"
89

@@ -21,6 +22,7 @@ const (
2122
HardenedKeyStart = uint32(hdkeychain.HardenedKeyStart)
2223
WalletDefaultDerivationPath = "m/84'/0'/0'"
2324
WalletBIP49DerivationPath = "m/49'/0'/0'"
25+
WalletBIP86DerivationPath = "m/86'/0'/0'"
2426
LndDerivationPath = "m/1017'/%d'/%d'"
2527
)
2628

@@ -199,6 +201,7 @@ func AllDerivationPaths(params *chaincfg.Params) ([]string, [][]uint32, error) {
199201
pathStrings := []string{
200202
WalletBIP49DerivationPath,
201203
WalletDefaultDerivationPath,
204+
WalletBIP86DerivationPath,
202205
mkPath(keychain.KeyFamilyPaymentBase),
203206
}
204207
paths := make([][]uint32, len(pathStrings))
@@ -350,6 +353,15 @@ func NP2WKHAddr(pubKey *btcec.PublicKey,
350353
return btcutil.NewAddressScriptHash(script, params)
351354
}
352355

356+
func P2TRAddr(pubKey *btcec.PublicKey,
357+
params *chaincfg.Params) (*btcutil.AddressTaproot, error) {
358+
359+
taprootKey := txscript.ComputeTaprootKeyNoScript(pubKey)
360+
return btcutil.NewAddressTaproot(
361+
schnorr.SerializePubKey(taprootKey), params,
362+
)
363+
}
364+
353365
func P2AnchorStaticRemote(pubKey *btcec.PublicKey,
354366
params *chaincfg.Params) (*btcutil.AddressWitnessScriptHash, []byte,
355367
error) {

0 commit comments

Comments
 (0)