-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet.js
296 lines (252 loc) · 11.1 KB
/
wallet.js
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const back = require('androidjs').back;
const _ = require("lodash");
//try {
const path = require('path');
var elliptic_1 = require("elliptic");
const fs_1 = require("fs");
var EC = new elliptic_1.ec('secp256k1');
const transaction_1 = require("./transaction");
const blockchain_1 = require("./blockchain");
var privateKeyLocation = 'node/wallet/private_key'; // We skipped process.env.PRIVATE_KEY ||
const getPrivateFromWallet = () => {
const buffer = fs_1.readFileSync(privateKeyLocation, 'utf8');
return buffer.toString();
};
exports.getPrivateFromWallet = getPrivateFromWallet;
const getPublicFromWallet = () => {
back.send('debug-info','wallet.getPublicFromWallet()');
back.send('debug-info','wallet.getPublicFromWallet: getPrivateFromWallet()');
const privateKey = getPrivateFromWallet();
back.send('debug-info','wallet.getPublicFromWallet: EC.keyFromPrivate(privateKey, "hex")');
const key = EC.keyFromPrivate(privateKey, 'hex');
back.send('debug-info','wallet.getPublicFromWallet: key.getPublic().encode("hex")');
const encoded = key.getPublic().encode('hex');
back.send('debug-info','/wallet.getPublicFromWallet');
return encoded;
/*Following code was originally there I switched
with above in a way which Android.js support
//const privateKey = getPrivateFromWallet();
//const key = EC.keyFromPrivate(privateKey, 'hex');
//return key.getPublic().encode('hex');*/
};
exports.getPublicFromWallet = getPublicFromWallet;
const generatePrivateKey = () => {
back.send('debug-info','wallet.generatePrivateKey()');
try{
if(!elliptic_1){
back.send('debug-warn','wallet.generatePrivateKey: elliptic not initialized');
elliptic_1 = require("elliptic");
}
if(!EC){
back.send('debug-warn','wallet.generatePrivateKey: EC not initialized');
EC = new elliptic_1.ec('secp256k1');
}
} catch(e){
back.send('debug-error', 'wallet.generatePrivateKey: Error: '+e.message);
}
back.send('debug-info','wallet.generatePrivateKey: EC.genKeyPair()');
const keyPair = EC.genKeyPair();
back.send('debug-info','wallet.generatePrivateKey: keyPair.getPrivate()');
const privateKey = keyPair.getPrivate();
back.send('debug-info','/wallet.generatePrivateKey');
return privateKey.toString(16);
};
exports.generatePrivateKey = generatePrivateKey;
const initWallet = (usr_datapath) => {
back.send('debug-info','wallet.initWallet('+usr_datapath+')');
try{
privateKeyLocation = path.join(usr_datapath,'private_key');
} catch(e){
back.send('debug-error', 'wallet.initWallet: Error: '+e.message);
}
back.send('debug-info','wallet.initWallet: privateKeyLocation = '+privateKeyLocation);
try {
back.send('debug-info','wallet.initWallet: fs.existsSync('+privateKeyLocation+')');
if (!fs_1.existsSync(privateKeyLocation)) {
back.send('debug-info', 'wallet.initWallet: No private key at '+privateKeyLocation+' (fs.existsSync -> false)');
back.send('debug-info','wallet.initWallet: generatePrivateKey()');
const newPrivateKey = generatePrivateKey();
back.send('debug-info','wallet.initWallet: Generated new private key');
back.send('debug-info','wallet.initWallet: fs.writeFileSync(...)');
fs_1.writeFileSync(privateKeyLocation, newPrivateKey);
back.send('debug-info','wallet.initWallet: Generated new wallet with private key to '+privateKeyLocation);
}
back.send('debug-info','wallet.initWallet: generatePrivateKey()');
const newPrivateKey = generatePrivateKey();
back.send('debug-info','wallet.initWallet: Generated new private key');
back.send('debug-info','wallet.initWallet: fs.writeFileSync(...)');
fs_1.writeFileSync(privateKeyLocation, newPrivateKey);
back.send('debug-info','wallet.initWallet: Generated new wallet with private key to '+privateKeyLocation);
back.send('debug-info','wallet.initWallet: blockchain.getAccounts()');
const accounts = blockchain_1.getAccounts();
back.send('debug-info','wallet.initWallet: getPublicFromWallet()');
const address = getPublicFromWallet();
back.send('debug-info','wallet.initWallet: received address "'+address+'"');
back.send('debug-info','wallet.initWallet: transaction.existAccount(...)');
if (!transaction_1.existAccount(address, accounts)) {
back.send('debug-info','wallet.initWallet: transaction.createAccount(...)');
transaction_1.createAccount(address, accounts);
back.send('debug-info','wallet.initWallet: An account was created');
}
back.send('start-indication', 'wallet');
back.send('debug-info','/wallet.initWallet');
} catch(e){
back.send('debug-error', 'wallet.initWallet: Error: '+e.message);
}
back.send('debug-info','/wallet.initWallet');
};
exports.initWallet = initWallet;
const deleteWallet = () => {
if (fs_1.existsSync(privateKeyLocation)) {
fs_1.unlinkSync(privateKeyLocation);
}
};
exports.deleteWallet = deleteWallet;
/*
const getBalance = (address: string, unspentTxOuts: UnspentTxOut[]): number => {
return _(findUnspentTxOuts(address, unspentTxOuts))
.map((uTxO: UnspentTxOut) => uTxO.amount)
.sum();
};
*/
const getBalance = (address, accounts) => {
let acct = transaction_1.findAccount(address, accounts);
if (acct == undefined) {
console.log('getBalance: no account found.');
back.send('debug-info','No Account Found');
return 0;
}
return acct.balance;
};
exports.getBalance = getBalance;
const createTransaction = (receiverAddress, amount, privateKey, accounts, txPool) => {
//console.log('txPool: %s', JSON.stringify(txPool));
// txPool did not get used in anywhere in this class other than a parameter.
const myAddress = transaction_1.getPublicKey(privateKey);
const myAccount = transaction_1.findAccount(myAddress, accounts);
if (amount > myAccount.balance) {
console.log('No enough coins.');
back.send('debug-info', 'Not Enough Coins to create a Transaction');
return undefined;
}
const tx = new transaction_1.Transaction(myAddress, receiverAddress, amount);
tx.timestamp = blockchain_1.getCurrentTimestamp();
tx.id = transaction_1.getTransactionId(tx);
tx.signature = transaction_1.signTransaction(tx, privateKey);
//what is the role of transaction pool?
return tx;
};
exports.createTransaction = createTransaction;
// } catch(e) {
// back.send('debug-error','wallet: Major Error: '+e.message);
// back.send('debug-error','wallet: Major Error: '+e.toString());
// back.send('debug-error','wallet: Major Error: '+e.fileName);
// back.send('debug-error','wallet: Major Error: '+e.lineNumber);
// back.send('debug-error','wallet: Major Error: '+e.stack);
// }
//# sourceMappingURL=wallet.js.map
//let's not override existing private keys
//Following was orginally there I replaced them with above.
/*if (fs_1.existsSync(privateKeyLocation)) {
if (!transaction_1.existAccount(getPublicFromWallet(), blockchain_1.getAccounts())) {
transaction_1.createAccount(getPublicFromWallet(), blockchain_1.getAccounts());
console.log('an account was created.');
}
return;
}
const newPrivateKey = generatePrivateKey();
fs_1.writeFileSync(privateKeyLocation, newPrivateKey);
console.log('new wallet with private key created to : %s', privateKeyLocation);
if (!transaction_1.existAccount(getPublicFromWallet(), blockchain_1.getAccounts())) {
transaction_1.createAccount(getPublicFromWallet(), blockchain_1.getAccounts());
console.log('an account was created.');
}**/
// const newPrivateKey = generatePrivateKey();
// fs_1.writeFileSync(privateKeyLocation, newPrivateKey);
// console.log('new wallet with private key created to : %s', privateKeyLocation);
// if (!transaction_1.existAccount(getPublicFromWallet(), blockchain_1.getAccounts())) {
// transaction_1.createAccount(getPublicFromWallet(), blockchain_1.getAccounts());
// console.log('an account was created.');
// }
// };
/*
const findUnspentTxOuts = (ownerAddress: string, unspentTxOuts: UnspentTxOut[]) => {
return _.filter(unspentTxOuts, (uTxO: UnspentTxOut) => uTxO.address === ownerAddress);
};
*/
/*
const findTxOutsForAmount = (amount: number, myUnspentTxOuts: UnspentTxOut[]) => {
let currentAmount = 0;
const includedUnspentTxOuts = [];
for (const myUnspentTxOut of myUnspentTxOuts) {
includedUnspentTxOuts.push(myUnspentTxOut);
currentAmount = currentAmount + myUnspentTxOut.amount;
if (currentAmount >= amount) {
const leftOverAmount = currentAmount - amount;
return {includedUnspentTxOuts, leftOverAmount};
}
}
const eMsg = 'Cannot create transaction from the available unspent transaction outputs.' +
' Required amount:' + amount + '. Available unspentTxOuts:' + JSON.stringify(myUnspentTxOuts);
throw Error(eMsg);
};
*/
/*
const createTxOuts = (receiverAddress: string, myAddress: string, amount, leftOverAmount: number) => {
const txOut1: TxOut = new TxOut(receiverAddress, amount);
if (leftOverAmount === 0) {
return [txOut1];
} else {
const leftOverTx = new TxOut(myAddress, leftOverAmount);
return [txOut1, leftOverTx];
}
};
*/
/*
const filterTxPoolTxs = (unspentTxOuts: UnspentTxOut[], transactionPool: Transaction[]): UnspentTxOut[] => {
const txIns: TxIn[] = _(transactionPool)
.map((tx: Transaction) => tx.txIns)
.flatten()
.value();
const removable: UnspentTxOut[] = [];
for (const unspentTxOut of unspentTxOuts) {
const txIn = _.find(txIns, (aTxIn: TxIn) => {
return aTxIn.txOutIndex === unspentTxOut.txOutIndex && aTxIn.txOutId === unspentTxOut.txOutId;
});
if (txIn === undefined) {
} else {
removable.push(unspentTxOut);
}
}
return _.without(unspentTxOuts, ...removable);
};
*/
/*
const createTransaction = (receiverAddress: string, amount: number, privateKey: string,
unspentTxOuts: UnspentTxOut[], txPool: Transaction[]): Transaction => {
console.log('txPool: %s', JSON.stringify(txPool));
const myAddress: string = getPublicKey(privateKey);
const myUnspentTxOutsA = unspentTxOuts.filter((uTxO: UnspentTxOut) => uTxO.address === myAddress);
const myUnspentTxOuts = filterTxPoolTxs(myUnspentTxOutsA, txPool);
// filter from unspentOutputs such inputs that are referenced in pool
const {includedUnspentTxOuts, leftOverAmount} = findTxOutsForAmount(amount, myUnspentTxOuts);
const toUnsignedTxIn = (unspentTxOut: UnspentTxOut) => {
const txIn: TxIn = new TxIn();
txIn.txOutId = unspentTxOut.txOutId;
txIn.txOutIndex = unspentTxOut.txOutIndex;
return txIn;
};
const unsignedTxIns: TxIn[] = includedUnspentTxOuts.map(toUnsignedTxIn);
const tx: Transaction = new Transaction();
tx.txIns = unsignedTxIns;
tx.txOuts = createTxOuts(receiverAddress, myAddress, amount, leftOverAmount);
tx.id = getTransactionId(tx);
tx.txIns = tx.txIns.map((txIn: TxIn, index: number) => {
txIn.signature = signTxIn(tx, index, privateKey, unspentTxOuts);
return txIn;
});
return tx;
};
*/