Skip to content

Commit f478242

Browse files
committed
derivekey: show master fingerprint
1 parent ff331d3 commit f478242

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

cmd/chantools/derivekey.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
const deriveKeyFormat = `
1313
Path: %s
1414
Network: %s
15+
Master Fingerprint: %x
1516
Public key: %x
1617
Extended public key (xpub): %v
1718
Address: %v
@@ -110,8 +111,13 @@ func deriveKey(extendedKey *hdkeychain.ExtendedKey, path string,
110111
privKey, xPriv = wif.String(), child.String()
111112
}
112113

114+
_, fingerPrintBytes, err := fingerprint(extendedKey)
115+
if err != nil {
116+
return fmt.Errorf("could not get fingerprint: %w", err)
117+
}
118+
113119
result := fmt.Sprintf(
114-
deriveKeyFormat, path, chainParams.Name,
120+
deriveKeyFormat, path, chainParams.Name, fingerPrintBytes,
115121
pubKey.SerializeCompressed(), neutered, addrP2WKH, addrP2PKH,
116122
addrP2TR, privKey, xPriv,
117123
)

cmd/chantools/signpsbt.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,11 @@ func signPsbt(rootKey *hdkeychain.ExtendedKey,
221221
func findMatchingDerivationPath(rootKey *hdkeychain.ExtendedKey,
222222
pIn *psbt.PInput) ([]uint32, error) {
223223

224-
pubKey, err := rootKey.ECPubKey()
224+
masterFingerprint, _, err := fingerprint(rootKey)
225225
if err != nil {
226226
return nil, fmt.Errorf("error getting public key: %w", err)
227227
}
228228

229-
pubKeyHash := btcutil.Hash160(pubKey.SerializeCompressed())
230-
fingerprint := binary.LittleEndian.Uint32(pubKeyHash[:4])
231-
232229
if len(pIn.Bip32Derivation) == 0 {
233230
return nil, errNoPathFound
234231
}
@@ -246,10 +243,21 @@ func findMatchingDerivationPath(rootKey *hdkeychain.ExtendedKey,
246243

247244
// The normal case, where a derivation path has the master
248245
// fingerprint set.
249-
if derivation.MasterKeyFingerprint == fingerprint {
246+
if derivation.MasterKeyFingerprint == masterFingerprint {
250247
return derivation.Bip32Path, nil
251248
}
252249
}
253250

254251
return nil, errNoPathFound
255252
}
253+
254+
func fingerprint(rootKey *hdkeychain.ExtendedKey) (uint32, []byte, error) {
255+
pubKey, err := rootKey.ECPubKey()
256+
if err != nil {
257+
return 0, nil, fmt.Errorf("error getting public key: %w", err)
258+
}
259+
260+
pubKeyHash := btcutil.Hash160(pubKey.SerializeCompressed())
261+
fpBytes := pubKeyHash[:4]
262+
return binary.LittleEndian.Uint32(fpBytes), fpBytes, nil
263+
}

0 commit comments

Comments
 (0)