> For the complete documentation index, see [llms.txt](https://hub.bsvblockchain.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hub.bsvblockchain.org/higher-learning/bsv-academy/bitcoin-primitives-hash-functions/ripemd-160-overview/how-to-create-a-bitcoin-wif-in-golang.md).

# How to Create a Bitcoin WIF in GoLang

{% embed url="<https://youtu.be/6cHmEEIxMX0>" %}

A **WIF (Wallet Import Format)** key is a Base58Check-encoded version of a **private key**. It allows private keys to be **easily imported and exported** across wallets while including a **checksum** to catch errors.

***

#### Example Implementation in GoLang

```
package main 

import ( 
    "fmt" 
    "github.com/libsv/go-bk/base58" 
    "github.com/libsv/go-bk/bec" 
    "github.com/libsv/go-bk/crypto" 
)   

func main() { 
    // 1. Generate a new private key
    privKey, _ := bec.NewPrivateKey(bec.S256()) 

    // 2. Serialize the private key
    pKey := privKey.Serialise() 

    // 3. Add version byte (0x80 for mainnet, 0xef for testnet)
    version := []byte{0x80}  
    pKey = append(version, pKey...) 

    // 4. Append compression flag (0x01 means compressed public key will be used)
    pKey = append(pKey, 0x01) 

    // 5. Double SHA-256 and take first 4 bytes (checksum)
    chksum := crypto.Sha256d(pKey)[:4] 

    // 6. Append checksum
    pKey = append(pKey, chksum...) 

    // 7. Encode in Base58Check
    w := base58.Encode(pKey)   

    // 8. Print result
    fmt.Printf("WIF: %s\n", w)   
}
```

#### Step-by-Step Process

1. **Serialize the private key (`d`)**

   &#x20;`pKey := privKey.Serialise()`&#x20;
2. **Add version byte**

   * `0x80` for **mainnet**
   * `0xef` for **testnet**

   &#x20;`version := []byte{0x80} pKey = append(version, pKey...)`&#x20;
3. **Append compression byte (0x01)**

   * Marks that the **corresponding public key** is compressed.

   &#x20;`pKey = append(pKey, 0x01)`&#x20;
4. **Generate checksum** (first 4 bytes of double SHA-256)

   &#x20;`chksum := crypto.Sha256d(pKey)[:4]`
5. **Append checksum**

   &#x20;`pKey = append(pKey, chksum...)`&#x20;
6. **Encode with Base58**

   &#x20;`w := base58.Encode(pKey)`&#x20;
7. **Print the final WIF**

   &#x20;`fmt.Printf("WIF: %s\n", w)`

***

✅ **Summary:**\
A **WIF key** is a Base58Check-encoded private key with a **version byte**, **compression flag**, and **checksum**. This format makes private keys portable, error-resistant, and widely supported across Bitcoin wallets.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hub.bsvblockchain.org/higher-learning/bsv-academy/bitcoin-primitives-hash-functions/ripemd-160-overview/how-to-create-a-bitcoin-wif-in-golang.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
