aboutsummaryrefslogtreecommitdiffstats
path: root/utils/keys.go
blob: 1c0f6a9d62afa082acc1c940126dfa5ca4b37941 (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package utils

import (
    "fmt"
    "github.com/ethereum/eth-go/ethutil"
    "github.com/obscuren/secp256k1-go"
)

func CreateKeyPair(force bool) {
    if force {
        ethutil.GetKeyRing().Reset()
        fmt.Println("resetting")
    }

    if ethutil.GetKeyRing().Get(0) == nil {
        _, prv := secp256k1.GenerateKeyPair()

        keyPair, err := ethutil.GetKeyRing().NewKeyPair(prv)
        if err != nil {
            panic(err)
        }

        mne := ethutil.MnemonicEncode(ethutil.Hex(keyPair.PrivateKey))

        fmt.Printf(`
Generating new address and keypair.
Please keep your keys somewhere save.

++++++++++++++++ KeyRing +++++++++++++++++++
addr: %x
prvk: %x
pubk: %x
++++++++++++++++++++++++++++++++++++++++++++
save these words so you can restore your account later: %s
`, keyPair.Address(), keyPair.PrivateKey, keyPair.PublicKey, mne)
    }
}

func ImportPrivateKey(sec string) {
    ethutil.GetKeyRing().Reset()

    keyPair, err := ethutil.GetKeyRing().NewKeyPair(ethutil.FromHex(sec))
    if err != nil {
        panic(err)
    }

    mne := ethutil.MnemonicEncode(ethutil.Hex(keyPair.PrivateKey))

    fmt.Printf(`
Generating new address and keypair.
Please keep your keys somewhere save.

++++++++++++++++ KeyRing +++++++++++++++++++
addr: %x
prvk: %x
pubk: %x
++++++++++++++++++++++++++++++++++++++++++++
save these words so you can restore your account later: %s
`, keyPair.Address(), keyPair.PrivateKey, keyPair.PublicKey, mne)
}

/*
func CreateKeyPair(force bool) {
    data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
    if len(data) == 0 || force {
        pub, prv := secp256k1.GenerateKeyPair()
        pair := &ethutil.Key{PrivateKey: prv, PublicKey: pub}
        ethutil.Config.Db.Put([]byte("KeyRing"), pair.RlpEncode())
        mne := ethutil.MnemonicEncode(ethutil.Hex(prv))

        fmt.Printf(`
Generating new address and keypair.
Please keep your keys somewhere save.

++++++++++++++++ KeyRing +++++++++++++++++++
addr: %x
prvk: %x
pubk: %x
++++++++++++++++++++++++++++++++++++++++++++
save these words so you can restore your account later: %s
`, pair.Address(), prv, pub, mne)

    }
}
*/

/*
func ImportPrivateKey(prvKey string) {
    key := ethutil.FromHex(prvKey)
    msg := []byte("tmp")
    // Couldn't think of a better way to get the pub key
    sig, _ := secp256k1.Sign(msg, key)
    pub, _ := secp256k1.RecoverPubkey(msg, sig)
    pair := &ethutil.Key{PrivateKey: key, PublicKey: pub}
    ethutil.Config.Db.Put([]byte("KeyRing"), pair.RlpEncode())

    fmt.Printf(`
Importing private key

++++++++++++++++ KeyRing +++++++++++++++++++
addr: %x
prvk: %x
pubk: %x
++++++++++++++++++++++++++++++++++++++++++++

`, pair.Address(), key, pub)
}
*/