Skip to content

Instantly share code, notes, and snippets.

@zvodd
Last active January 3, 2023 10:59
Show Gist options
  • Save zvodd/425f430d7c50e01d34b1eb2f1621af29 to your computer and use it in GitHub Desktop.
Save zvodd/425f430d7c50e01d34b1eb2f1621af29 to your computer and use it in GitHub Desktop.
Go BTC brainwallet - Generate wallet address and WIF from bitcoin brainwallet password
package main
import (
"crypto/sha256"
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcutil/base58"
"golang.org/x/crypto/ripemd160"
)
const (
verifyBrainWalPW = "qwertyuiopasdfghjkl"
verifyPublicKey = "[4 126 232 74 121 20 85 46 30 108 135 172 131 126 169 182 30 63 109 198 48 161 211 46 126 227 143 156 145 102 164 132 104 108 227 88 200 217 114 19 213 103 238 6 141 224 114 225 43 224 105 158 110 198 30 46 27 68 51 169 172 247 104 92 197]"
verifyShaPub = "[69 25 96 127 176 77 242 43 17 202 77 219 70 198 42 20 189 171 202 33 103 82 193 189 54 222 32 85 186 82 116 169]"
verifyRipeShaPub = "[87 117 43 165 181 111 26 136 225 163 206 15 69 215 215 228 117 115 213 35]"
verifyBTCWalletPubAddr_b58 = "18yS9qcSLrK5VXyDvF2SyedwWDRKwdE99Y"
verifyBTCWalletPrivWIF_b58 = "5KcgjWqQhQW6g6cJHsTJeDsh8p2XWVL5R9RS3oqA26ywJkXqDgD"
)
func main() {
pw := []byte(verifyBrainWalPW)
pwhash := sha256.Sum256(pw)
fmt.Printf("SHA256(Brain Wallet Password) : %X\n", pwhash)
fmt.Printf("SHA256(Brain Wallet Password) : %d\n", pwhash)
fmt.Println("")
priv, pub := btcec.PrivKeyFromBytes(btcec.S256(), pwhash[:])
fmt.Println("Private Key:", fmt.Sprintf("%x", priv))
fmt.Println("Public Key:", fmt.Sprintf("%x", pub))
fmt.Println("")
fmt.Println("Public Key:", fmt.Sprintf("%d", pub.SerializeUncompressed()))
fmt.Println(" verify:", verifyPublicKey)
fmt.Println("")
shapub := sha256.Sum256(pub.SerializeUncompressed())
fmt.Printf("SHA(pub): %d\n", shapub)
fmt.Printf(" verify: %s\n", verifyShaPub)
fmt.Println("")
r1 := ripemd160.New()
r1.Write(shapub[:])
rsp := r1.Sum([]byte{})
fmt.Printf("RIPE(SHA(pub)): %d\n", rsp)
fmt.Printf(" verify: %s\n", verifyRipeShaPub)
fmt.Println("")
fmt.Println("Uncompressed:", fmt.Sprintf("%x", pub.SerializeUncompressed()))
fmt.Println(":", fmt.Sprintf("%d", pub.SerializeUncompressed()))
fmt.Println("")
fmt.Println("Compressed:", fmt.Sprintf("%x", pub.SerializeCompressed()))
fmt.Println(":", fmt.Sprintf("%d", pub.SerializeCompressed()))
fmt.Println("")
fmt.Println("====")
fmt.Println("Wallet Address:", WalletAddressB58(pub.SerializeUncompressed()))
fmt.Println(" verify:", verifyBTCWalletPubAddr_b58)
fmt.Println("====")
fmt.Println("")
fmt.Println("====")
fmt.Println("Wallet WIF:", WalletWifB58(pwhash[:]))
fmt.Println(" verify:", verifyBTCWalletPrivWIF_b58)
fmt.Println("====")
}
func ShaRipe(b []byte) []byte {
s := sha256.Sum256(b)
r := ripemd160.New()
r.Write(s[:])
return r.Sum([]byte{})
}
func ShaSha(b []byte) []byte {
x := sha256.Sum256(b)
y := sha256.Sum256(x[:])
return y[:]
}
func WalletAddress(pubkey []byte) []byte {
// Elliptic public key = ECDSA( Private key)
// --Public key = 'OxO4' + Elliptic public key // already there
// Encrypted public key = RIPEMD-160 ( SHA-256 ( Public key) )
// Mainnet encrypted public key = 'OxOO' + Encrypted public key
// C = SHA-256 ( SHA-256 ( Mainnet encrypted public key ) )
// Checksum = first 4 bytes of C
// Hex address = Mainnet encrypted public key + Checksum
if len(pubkey) == 64 {
pubkey = append([]byte{4}, pubkey...)
}
hash := ShaRipe(pubkey)
hash = append([]byte{0}, hash...)
checksum := ShaSha(hash)
return append(hash, checksum[0:4]...)
}
func WalletAddressB58(pubkey []byte) string {
// Address = Base58 ( address bytes )
return base58.Encode(WalletAddress(pubkey))
}
func WalletWif(privkey []byte) []byte {
// 1) Take a private key.
// 2) Add a 0x80 byte in front of it for mainnet addresses.
// 3) (X) Append a 0x01 byte after it if it should be used with compressed public keys.
// Nothing is appended if it is used with uncompressed public keys.
// 4) (X) Perform a SHA-256 hash on the extended key. // already done.
// 5) Double SHA-256 ammanded privatekey.
// 6) Take the first four bytes of the Double SHA-256 hash; this is the checksum.
// 7) Add the four checksum bytes from point 5 at the end of the extended key from point 2.
bytes := append([]byte{0x80}, privkey...)
checksum := ShaSha(bytes)
return append(bytes, checksum[0:4]...)
}
func WalletWifB58(privkey []byte) string {
// 8) Convert the result from a byte string into a Base58 string using Base58Check
return base58.Encode(WalletWif(privkey))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment