-
Notifications
You must be signed in to change notification settings - Fork 7
/
keypair.go
70 lines (53 loc) · 2.09 KB
/
keypair.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Every file in go is a part of some package. Since we want to create a program
// from this file we use 'package main'
package main
import (
"encoding/hex"
"fmt"
"log"
"github.com/btcsuite/btcec"
"github.com/btcsuite/btcnet"
"github.com/btcsuite/btcutil"
)
// generateKeyPair creates a key pair based on the eliptic curve
// secp256k1. The key pair returned by this function is two points
// on this curve. Bitcoin requires that the public and private keys
// used to generate signatures are generated using this curve.
func generateKeyPair() (*btcec.PublicKey, *btcec.PrivateKey) {
// Generate a private key, use the curve secpc256k1 and kill the program on
// any errors
priv, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
// There was an error. Log it and bail out
log.Fatal(err)
}
return priv.PubKey(), priv
}
// generateAddr computes the associated bitcon address from the provided
// public key. We compute ripemd160(sha256(b)) of the pubkey and then
// shimmy the hashed bytes into btcsuite's AddressPubKeyHash type
func generateAddr(pub *btcec.PublicKey) *btcutil.AddressPubKeyHash {
net := &btcnet.MainNetParams
// Serialize the public key into bytes and then run ripemd160(sha256(b)) on it
b := btcutil.Hash160(pub.SerializeCompressed())
// Convert the hashed public key into the btcsuite type so that the library
// will handle the base58 encoding when we call addr.String()
addr, err := btcutil.NewAddressPubKeyHash(b, net)
if err != nil {
log.Fatal(err)
}
return addr
}
func main() {
// In order to recieve coins we must generate a public/private key pair.
pub, priv := generateKeyPair()
// To use this address we must store our private key somewhere. Everything
// else can be recovered from the private key.
fmt.Printf("This is a private key in hex:\t[%s]\n",
hex.EncodeToString(priv.Serialize()))
fmt.Printf("This is a public key in hex:\t[%s]\n",
hex.EncodeToString(pub.SerializeCompressed()))
addr := generateAddr(pub)
// Output the bitcoin address derived from the public key
fmt.Printf("This is the associated Bitcoin address:\t[%s]\n", addr.String())
}