-
Notifications
You must be signed in to change notification settings - Fork 7
/
wallet.go
47 lines (40 loc) · 1016 Bytes
/
wallet.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
package main
import (
"crypto/rand"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/sha256"
"encoding/base64"
)
type Wallet struct {
// super insecure keydump, address:privkey
keyDump map[string]ecdsa.PrivateKey
}
func NewWallet() *Wallet{
return &Wallet{
keyDump: make(map[string]ecdsa.PrivateKey),
}
}
func newKey() *ecdsa.PrivateKey {
privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
panic(err)
}
return privKey
}
func ToAddress(pubkey ecdsa.PublicKey) string {
pubBytes := elliptic.Marshal(elliptic.P256(), pubkey.X, pubkey.Y)
addr := sha256.Sum256(pubBytes[1:])
addrString := base64.StdEncoding.EncodeToString(addr[:])[12:]
return addrString
}
func (wallet *Wallet) GetNewAddress() string {
new := newKey()
addr := ToAddress(new.PublicKey)
wallet.keyDump[addr] = *new
return addr
}
func (wallet *Wallet) hasKey(addr string) bool {
_, ok := wallet.keyDump[addr]
return ok
}