Skip to content

Commit

Permalink
derivekey+genimportscript: add p2tr address support
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Oct 29, 2022
1 parent fd6c672 commit ff4f4a2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
8 changes: 6 additions & 2 deletions btc/bitcoind.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,15 @@ func (i *ImportWallet) Format(hdKey *hdkeychain.ExtendedKey,
if err != nil {
return "", fmt.Errorf("could not create address: %w", err)
}
addrP2TR, err := lnd.P2TRAddr(privKey.PubKey(), params)
if err != nil {
return "", fmt.Errorf("could not create address: %w", err)
}

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

Expand Down
9 changes: 7 additions & 2 deletions cmd/chantools/derivekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/guggero/chantools/lnd"
Expand All @@ -16,6 +15,7 @@ Public key: %x
Extended public key (xpub): %v
Address: %v
Legacy address: %v
Taproot address: %v
Private key (WIF): %s
Extended private key (xprv): %s
`
Expand Down Expand Up @@ -99,6 +99,11 @@ func deriveKey(extendedKey *hdkeychain.ExtendedKey, path string,
return fmt.Errorf("could not create address: %w", err)
}

addrP2TR, err := lnd.P2TRAddr(pubKey, chainParams)
if err != nil {
return fmt.Errorf("could not create address: %w", err)
}

privKey, xPriv := na, na
if !neuter {
privKey, xPriv = wif.String(), child.String()
Expand All @@ -107,7 +112,7 @@ func deriveKey(extendedKey *hdkeychain.ExtendedKey, path string,
result := fmt.Sprintf(
deriveKeyFormat, path, chainParams.Name,
pubKey.SerializeCompressed(), neutered, addrP2WKH, addrP2PKH,
privKey, xPriv,
addrP2TR, privKey, xPriv,
)
fmt.Println(result)

Expand Down
2 changes: 1 addition & 1 deletion cmd/chantools/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

const (
defaultAPIURL = "https://blockstream.info/api"
version = "0.10.5"
version = "0.10.6"
na = "n/a"

Commit = ""
Expand Down
12 changes: 12 additions & 0 deletions lnd/hdkeychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lnd
import (
"crypto/sha256"
"fmt"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"strconv"
"strings"

Expand All @@ -21,6 +22,7 @@ const (
HardenedKeyStart = uint32(hdkeychain.HardenedKeyStart)
WalletDefaultDerivationPath = "m/84'/0'/0'"
WalletBIP49DerivationPath = "m/49'/0'/0'"
WalletBIP86DerivationPath = "m/86'/0'/0'"
LndDerivationPath = "m/1017'/%d'/%d'"
)

Expand Down Expand Up @@ -199,6 +201,7 @@ func AllDerivationPaths(params *chaincfg.Params) ([]string, [][]uint32, error) {
pathStrings := []string{
WalletBIP49DerivationPath,
WalletDefaultDerivationPath,
WalletBIP86DerivationPath,
mkPath(keychain.KeyFamilyPaymentBase),
}
paths := make([][]uint32, len(pathStrings))
Expand Down Expand Up @@ -350,6 +353,15 @@ func NP2WKHAddr(pubKey *btcec.PublicKey,
return btcutil.NewAddressScriptHash(script, params)
}

func P2TRAddr(pubKey *btcec.PublicKey,
params *chaincfg.Params) (*btcutil.AddressTaproot, error) {

taprootKey := txscript.ComputeTaprootKeyNoScript(pubKey)
return btcutil.NewAddressTaproot(
schnorr.SerializePubKey(taprootKey), params,
)
}

func P2AnchorStaticRemote(pubKey *btcec.PublicKey,
params *chaincfg.Params) (*btcutil.AddressWitnessScriptHash, []byte,
error) {
Expand Down

0 comments on commit ff4f4a2

Please sign in to comment.