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.
The address_from_wif example showcases:
- Importing a private key from its WIF string representation using
ec.PrivateKeyFromWif. - Getting the corresponding public key from the private key using
privateKey.PubKey(). - Generating a P2PKH (Pay-to-Public-Key-Hash) address object from this public key using
script.NewAddressFromPublicKey. - Printing the serialized private key, the string representation of the derived address, and its public key hash.
// Import private key from WIF string
privKey, _ := ec.PrivateKeyFromWif("Kxfd8ABTYZHBH3y1jToJ2AUJTMVbsNaqQsrkpo9gnnc1JXfBH8mn")
// Log the serialized private key (hexadecimal)
log.Printf("Private key (hex): %x\n", privKey.Serialize())
// Get the public key associated with the private key
pubKey := privKey.PubKey()
// Create a new address object from the public key
// The 'true' argument typically indicates if it's for mainnet (compressed pubkey used for address)
address, _ := script.NewAddressFromPublicKey(pubKey, true)
// Print the address string and the underlying public key hash
fmt.Printf("Address: %s\n", address.AddressString)
fmt.Printf("Public Key Hash: %s\n", address.PublicKeyHash) // Or however you wish to display the hashThis section shows the step-by-step process:
ec.PrivateKeyFromWifparses the WIF string and returns an*ec.PrivateKeyobject.privKey.PubKey()returns the corresponding*ec.PublicKey.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.Addressobject. This object contains the familiar base58check encoded address string and the raw public key hash.
To run this example:
go run address_from_wif.goThe output will display the hexadecimal representation of the private key, the derived P2PKH address string, and the public key hash component of the address.
Note:
- The WIF string is hardcoded. In a real application, this would come from a secure source.
- The example derives a P2PKH address, which is the most common address type.
- The boolean argument
trueinNewAddressFromPublicKeytypically signifies that the address should be generated for the main network, often implying the use of a compressed public key for the hash.
To derive an address from a WIF in your application:
- Obtain the WIF string for the private key.
- Use
priv, err := ec.PrivateKeyFromWif(wifString)to get the private key object. Handle any errors. - Get the public key:
pubKey := priv.PubKey(). - Generate the address:
addr, err := script.NewAddressFromPublicKey(pubKey, isMainnet), whereisMainnetis true for mainnet addresses. Handle errors. - You can then use
addr.AddressStringfor display or in transactions, andaddr.PublicKeyHashif you need the raw hash.
For more information, see:
- Package Documentation - EC primitives
- Package Documentation - Script
- Generate HD Key Example (for creating master keys from which WIFs can be derived)