Skip to content

Commit f6a2b48

Browse files
authored
Avoid logging sensitive key material in examples (#274)
1 parent fdec0cd commit f6a2b48

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

docs/examples/create_wallet/create_wallet.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"crypto/sha256"
45
"fmt"
56
"log"
67

@@ -79,7 +80,8 @@ func main() {
7980

8081
// Get the public key from the private key
8182
publicKey := privateKey.PubKey()
82-
fmt.Printf("Derived Public Key (Hex): %x\n", publicKey.Compressed())
83+
publicKeyHash := sha256.Sum256(publicKey.Compressed())
84+
fmt.Printf("Derived public key fingerprint: %x\n", publicKeyHash[:8])
8385

8486
// Get the P2PKH address from the public key
8587
// This is one way to get the address.

docs/examples/generate_hd_key/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This example demonstrates how to use the `bip32` compatibility package to genera
77
The `generate_hd_key` example showcases:
88
1. Calling `bip32.GenerateHDKeyPair` with a specified seed length (`bip32.SecureSeedLength`).
99
2. Receiving the generated extended private key (xPriv) and extended public key (xPub).
10-
3. Printing both keys.
10+
3. Verifying the public key via a fingerprint without exposing key material.
1111

1212
## Code Walkthrough
1313

@@ -21,9 +21,9 @@ if err != nil {
2121
log.Fatalf("Error generating HD key pair: %s", err.Error())
2222
}
2323

24-
// Print the generated keys
25-
log.Printf("xPrivateKey: %s\n", xPrivateKey)
26-
log.Printf("xPublicKey: %s\n", xPublicKey)
24+
// Never log raw keys. Use a small fingerprint to confirm success.
25+
fingerprint := sha256.Sum256([]byte(xPublicKey))
26+
log.Printf("Generated HD key pair (xPriv length: %d, xPub fingerprint: %x)", len(xPrivateKey), fingerprint[:8])
2727
```
2828

2929
This section shows the direct use of `bip32.GenerateHDKeyPair`. This function creates a new master HD key from a randomly generated seed of the given length. It returns the extended private key (xPriv) and the corresponding extended public key (xPub) as strings.
@@ -35,11 +35,11 @@ To run this example:
3535
```bash
3636
go run generate_hd_key.go
3737
```
38-
The output will be the newly generated xPrivateKey and xPublicKey strings. Each run will produce a different key pair.
38+
The output will confirm the generated key lengths and show a short fingerprint of the xPub. Each run will produce a different key pair, so securely store the raw keys instead of logging them.
3939

4040
**Note**:
41-
- The generated xPrivateKey is the master private key for an HD wallet structure. It should be kept extremely secure.
42-
- The xPublicKey can be used to derive child public keys without exposing the private key.
41+
- The generated xPrivateKey is the master private key for an HD wallet structure. It should be kept extremely secure and never logged in plaintext.
42+
- The xPublicKey can be used to derive child public keys without exposing the private key. Only expose fingerprints when confirming values in logs.
4343
- `bip32.SecureSeedLength` is typically 32 bytes (256 bits) or 64 bytes (512 bits) for strong security.
4444

4545
## Integration Steps

docs/examples/generate_hd_key/generate_hd_key.go

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

33
import (
4+
"crypto/sha256"
45
"log"
56

67
bip32 "github.com/bsv-blockchain/go-sdk/compat/bip32"
@@ -12,6 +13,8 @@ func main() {
1213
log.Fatalf("error occurred: %s", err.Error())
1314
}
1415

15-
// Success!
16-
log.Printf("xPrivateKey: %s \n xPublicKey: %s", xPrivateKey, xPublicKey)
16+
// Success! Avoid logging sensitive key material. Use a fingerprint of the public key
17+
// for verification instead of printing the full keys.
18+
publicKeyFingerprint := sha256.Sum256([]byte(xPublicKey))
19+
log.Printf("Generated HD key pair (xPriv length: %d, xPub fingerprint: %x)", len(xPrivateKey), publicKeyFingerprint[:8])
1720
}

0 commit comments

Comments
 (0)