-
Notifications
You must be signed in to change notification settings - Fork 1
/
wallet.go
183 lines (151 loc) · 3.87 KB
/
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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// wallet.go
// Autor: NeironTeam
// Licencia: MIT License, Copyright (c) 2018 Neiron
package ncoin_wallet
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"encoding/hex"
"fmt"
internal "github.com/NeironTeam/ncoin/internal"
"os"
"runtime"
)
const WALLET_FOLDER = ".ncoin"
const SEED_SIZE = 512 //bytes
func getWalletFolder() string {
var base string = "HOME"
if runtime.GOOS == "windows" {
base = "USERPROFILE"
} else if runtime.GOOS == "plan9" {
base = "home"
}
var path string = internal.Getenv("WALLET_FOLDER", WALLET_FOLDER)
return fmt.Sprintf("%s/%s", os.Getenv(base), path)
}
// Cartera
type Wallet struct {
address []byte
privateKey *rsa.PrivateKey
publicKey *rsa.PublicKey
balance float64
}
func (w *Wallet) SetBalance(balance float64) {
w.balance = balance
}
func (w *Wallet) SetPublicKey(publicKey *rsa.PublicKey) {
w.publicKey = publicKey
}
func (w *Wallet) SetPrivateKey(privateKey *rsa.PrivateKey) {
w.privateKey = privateKey
}
func (w *Wallet) SetAddress(address []byte) {
w.address = address
}
func (w *Wallet) PublicKey() *rsa.PublicKey {
return w.publicKey
}
// Desbloquea la wallet, requiere una private_key como parametro.
func (w *Wallet) Unlock(pk uint64) {
}
// Envía la cantidad de NCoin indicada a la dirección indicada. Requiere
// candidad y dirección como parámetros .
func (w *Wallet) SendTransaction(amount float64, address uint64) {
}
// Devuelve la dirección de la cartera.
func (w *Wallet) Address() []byte {
return w.address
}
// Devuelve el saldo de la cartera.
func (w *Wallet) Balance() float64 {
return w.balance
}
// Devuelve la private_key de la cartera.
func (w *Wallet) PrivateKey() *rsa.PrivateKey {
return w.privateKey
}
//GenerateKeys generate the rsa private and public keys, return a err if somewhat goes wrong.
func (w *Wallet) generateKeys() (e error) {
if w.privateKey, e = rsa.GenerateKey(rand.Reader, 2048); e != nil {
return
}
w.publicKey = &w.privateKey.PublicKey
return
}
// generateAddress function return random seed hashed with sha256 to use as
// wallet address
func (w *Wallet) generateAddress() (e error) {
var randAddress []byte = make([]byte, 512)
if _, e = rand.Read(randAddress); e != nil {
return
}
h := sha256.New()
h.Write(randAddress)
w.address = []byte(hex.EncodeToString(h.Sum(nil)))
return
}
func (w *Wallet) storeWallet() (e error) {
// Check WALLET_FOLDER from enviroment
var walletsPath string = getWalletFolder()
// Check if walletsPath exits, else create it.
if _, e = os.Stat(walletsPath); os.IsNotExist(e) {
if e = os.Mkdir(walletsPath, os.ModePerm); e != nil {
return
}
} else if e != nil {
return
}
// Create walletFolder to store key pair if not exists
var walletFolder string = fmt.Sprintf("%s/%s", walletsPath, w.address)
if e = os.Mkdir(walletFolder, os.ModePerm); e != nil {
return
}
// Generate public key pem and store it
var pubPem []byte
if pubPem, e = x509.MarshalPKIXPublicKey(w.publicKey); e != nil {
return
} else {
if e = w.storePem(pubPem, walletFolder, true); e != nil {
return
}
}
// Generate private key pem and store it
var privPem []byte = x509.MarshalPKCS1PrivateKey(w.privateKey)
e = w.storePem(privPem, walletFolder, false)
return
}
func (w *Wallet) storePem(key []byte, folder string, public bool) (e error) {
var bType, file string
if bType, file = "RSA PRIVATE KEY", "id_rsa"; public {
bType = "RSA PUBLIC KEY"
file = "id_rsa.pub"
}
var pemRaw []byte = pem.EncodeToMemory(
&pem.Block{
Type: bType,
Bytes: key,
},
)
var fd *os.File
var pemPath string = fmt.Sprintf("%s/%s", folder, file)
if fd, e = os.Create(pemPath); e != nil {
return
}
if _, e = fd.Write(pemRaw); e != nil {
return e
}
fd.Close()
return
}
func NewWallet() (w *Wallet, e error) {
w = &Wallet{}
if e = w.generateKeys(); e != nil {
return
}
w.generateAddress()
e = w.storeWallet()
return
}