|
| 1 | +# Address From WIF Example |
| 2 | + |
| 3 | +This example demonstrates how to derive a Bitcoin SV address from a Wallet Import Format (WIF) private key string using the `ec` and `script` packages. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The `address_from_wif` example showcases: |
| 8 | +1. Importing a private key from its WIF string representation using `ec.PrivateKeyFromWif`. |
| 9 | +2. Getting the corresponding public key from the private key using `privateKey.PubKey()`. |
| 10 | +3. Generating a P2PKH (Pay-to-Public-Key-Hash) address object from this public key using `script.NewAddressFromPublicKey`. |
| 11 | +4. Printing the serialized private key, the string representation of the derived address, and its public key hash. |
| 12 | + |
| 13 | +## Code Walkthrough |
| 14 | + |
| 15 | +### Deriving Address from WIF |
| 16 | + |
| 17 | +```go |
| 18 | +// Import private key from WIF string |
| 19 | +privKey, _ := ec.PrivateKeyFromWif("Kxfd8ABTYZHBH3y1jToJ2AUJTMVbsNaqQsrkpo9gnnc1JXfBH8mn") |
| 20 | + |
| 21 | +// Log the serialized private key (hexadecimal) |
| 22 | +log.Printf("Private key (hex): %x\n", privKey.Serialize()) |
| 23 | + |
| 24 | +// Get the public key associated with the private key |
| 25 | +pubKey := privKey.PubKey() |
| 26 | + |
| 27 | +// Create a new address object from the public key |
| 28 | +// The 'true' argument typically indicates if it's for mainnet (compressed pubkey used for address) |
| 29 | +address, _ := script.NewAddressFromPublicKey(pubKey, true) |
| 30 | + |
| 31 | +// Print the address string and the underlying public key hash |
| 32 | +fmt.Printf("Address: %s\n", address.AddressString) |
| 33 | +fmt.Printf("Public Key Hash: %s\n", address.PublicKeyHash) // Or however you wish to display the hash |
| 34 | +``` |
| 35 | + |
| 36 | +This section shows the step-by-step process: |
| 37 | +- `ec.PrivateKeyFromWif` parses the WIF string and returns an `*ec.PrivateKey` object. |
| 38 | +- `privKey.PubKey()` returns the corresponding `*ec.PublicKey`. |
| 39 | +- `script.NewAddressFromPublicKey(pubKey, true)` takes the public key and a boolean (often indicating network or if the key should be compressed for address generation) to produce an `*script.Address` object. This object contains the familiar base58check encoded address string and the raw public key hash. |
| 40 | + |
| 41 | +## Running the Example |
| 42 | + |
| 43 | +To run this example: |
| 44 | + |
| 45 | +```bash |
| 46 | +go run address_from_wif.go |
| 47 | +``` |
| 48 | +The output will display the hexadecimal representation of the private key, the derived P2PKH address string, and the public key hash component of the address. |
| 49 | + |
| 50 | +**Note**: |
| 51 | +- The WIF string is hardcoded. In a real application, this would come from a secure source. |
| 52 | +- The example derives a P2PKH address, which is the most common address type. |
| 53 | +- The boolean argument `true` in `NewAddressFromPublicKey` typically signifies that the address should be generated for the main network, often implying the use of a compressed public key for the hash. |
| 54 | + |
| 55 | +## Integration Steps |
| 56 | + |
| 57 | +To derive an address from a WIF in your application: |
| 58 | +1. Obtain the WIF string for the private key. |
| 59 | +2. Use `priv, err := ec.PrivateKeyFromWif(wifString)` to get the private key object. Handle any errors. |
| 60 | +3. Get the public key: `pubKey := priv.PubKey()`. |
| 61 | +4. Generate the address: `addr, err := script.NewAddressFromPublicKey(pubKey, isMainnet)`, where `isMainnet` is true for mainnet addresses. Handle errors. |
| 62 | +5. You can then use `addr.AddressString` for display or in transactions, and `addr.PublicKeyHash` if you need the raw hash. |
| 63 | + |
| 64 | +## Additional Resources |
| 65 | + |
| 66 | +For more information, see: |
| 67 | +- [Package Documentation - EC primitives](https://pkg.go.dev/github.com/bsv-blockchain/go-sdk/primitives/ec) |
| 68 | +- [Package Documentation - Script](https://pkg.go.dev/github.com/bsv-blockchain/go-sdk/script) |
| 69 | +- [Generate HD Key Example](../generate_hd_key/) (for creating master keys from which WIFs can be derived) |
0 commit comments