-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
205 lines (198 loc) · 6.97 KB
/
main.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
- Copyright 2014 Keshav Bhide. All rights reserved.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-*/
package main
import "os"
import "fmt"
import "bytes"
import "crypto/aes"
import "os/user"
import "strconv"
import "errors"
import "encoding/hex"
import "io/ioutil"
/*-
- Basic usage
-
- $zeroweight createWallet [password] :
- this creates a file in user's home directory "zeroweight.wal" please backup this file
- in your dropbox/gdrive account, since it contains your private keys. Losing this file
- would cause you to lose all your bitcoins. BE VERY CAREFULL.
-
- $zeroweight send [toAddress] [BTC amount] [password]:
- sends butcoin from your wallet to [toAddress]. only users with the right password can
- can make transactions. Transactions are brodcasted to the bitcoin network via
- https://blockchain.info/pushtx.
-
- $zeroweight balance [password]:
- shows the current blance and your public key (ie your wallet's address).
-*/
func main() {
printUsage := func() {
fmt.Println("# use => $zeroweight createWallet [encryptionKey|password]");
fmt.Println("# => $zeroweight send [toAddress] [BTC amount] [password]");
fmt.Println("# => $zeroweight balance [password]");
return;
}
if len(os.Args) < 3 {
printUsage();
return;
}
switch os.Args[1] {
/*- creates wallet -*/
case "createWallet":
file, err := encryptAndBuildWallet(GenRandPrivateKey(), os.Args[2]);
if err != nil {
fmt.Println("# error =>", err.Error());
return;
}
fmt.Println("# success => wallet built.")
fmt.Println("# => please backup", file, "with dropbox|gdrive");
/*- builds and broadcasts transaction -*/
case "send":
if len(os.Args) < 5 {
printUsage();
return;
}
walletPrivateKeyWif, err := decryptAndGetPrivateKey(os.Args[4]);
if err != nil {
fmt.Println("# error =>", err);
return;
}
amount,err := strconv.ParseFloat(os.Args[3], 64)
if (err != nil) || (amount == 0) {
fmt.Println("# error => unable to parse amount enterd");
return;
}
b, err := Balance(GetPublicKey(walletPrivateKeyWif));
if err != nil {
fmt.Println("# error =>", err.Error());
return;
}
if (b < amount) {
fmt.Println("# error => your wallet does not have enoughf balance");
fmt.Println("# => execute:$ zeroweight balance [password] to",
"check balance");
return;
}
tx, err := Tx(walletPrivateKeyWif, os.Args[2], amount);
if err != nil {
fmt.Println("# error =>", err.Error());
return;
}
res := SubmitTransaction(tx);
fmt.Println("# status =>", res);
/*- prints balance -*/
case "balance":
walletPrivateKeyWif, err := decryptAndGetPrivateKey(os.Args[2]);
if err != nil {
fmt.Println("# error =>", err.Error());
return;
}
walletPublicKeyWif := GetPublicKey(walletPrivateKeyWif);
fmt.Println("# public address =>", walletPublicKeyWif);
balance, err := Balance(walletPublicKeyWif);
if err != nil {
fmt.Println("# error =>", err.Error());
return;
}
fmt.Println("# balance => ", balance);
default:
printUsage();
return;
}
}
func pathExist(path string) (bool, os.FileInfo) {
info, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, info;
}
panic("panic => cannot determine if path exist");
}
return true, info ;
}
func encryptAndBuildWallet(privateKey string, password string) (string, error) {
var userDir string;
if user,err := user.Current(); err != nil {
return "", errors.New("unable to lookup user directory");
} else {
userDir = user.HomeDir;
}
if exist,_ := pathExist(userDir+"/zeroweight.wal"); exist {
return "", errors.New("wallet already exists");
}
walletFileContent := "key{"+privateKey+"}";
if len(password) < 6 {
return "", errors.New("password|encryption key should be atleast 6 characters");
}
encryptionKey := make([]byte, 16);
copy(encryptionKey, []byte(password));
aesCipher, err := aes.NewCipher(encryptionKey);
if err != nil {
return "", err;
}
cBlockLen := aesCipher.BlockSize();
toEncryptLen := len(walletFileContent) +
(cBlockLen - (len(walletFileContent) % cBlockLen));
toEncrypt := make([]byte, toEncryptLen);
copy(toEncrypt, walletFileContent);
for i := 0; i < toEncryptLen; i += cBlockLen {
slice := toEncrypt[i:(i+cBlockLen)];
aesCipher.Encrypt(slice, slice);
}
wallet := hex.EncodeToString(toEncrypt);
err = ioutil.WriteFile(userDir+"/zeroweight.wal", []byte(wallet), 0644);
return userDir+"/zeroweight.wal", err;
}
func decryptAndGetPrivateKey(pass string) (string, error) {
var userDir string;
if user, err := user.Current(); err != nil {
return "", errors.New("unable to lookup user directory");
} else {
userDir = user.HomeDir;
}
if exist,_ := pathExist(userDir+"/zeroweight.wal"); !exist {
return "", errors.New("no wallet created, exec $zeroweight createWallet");
}
file, err := ioutil.ReadFile(userDir+"/zeroweight.wal");
if err != nil {
return "", err;
}
wallet, err := hex.DecodeString(string(file));
if err != nil {
return "", err;
}
walletLen := len(wallet);
encryptionKey := make([]byte, 16);
copy(encryptionKey, []byte(pass));
aesCipher, err := aes.NewCipher(encryptionKey);
if err != nil {
return "", err;
}
cBlockLen := aesCipher.BlockSize();
for i := 0; i < walletLen; i += cBlockLen {
slice := wallet[i:(i+cBlockLen)];
aesCipher.Decrypt(slice, slice);
}
if string(wallet[0:4]) != "key{" {
return "", errors.New("wrong password|encryptionKey");
}
last := bytes.IndexByte(wallet, '}');
if last == -1 {
return "", errors.New("corrupt wallet file, try again with right password");
}
key := string(wallet[4:last]);
return key, nil;
}