-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsqlite-handle.js
More file actions
executable file
·86 lines (77 loc) · 2.71 KB
/
sqlite-handle.js
File metadata and controls
executable file
·86 lines (77 loc) · 2.71 KB
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
"use strict";
const sqlite3 = require('sqlite3').verbose();
const fs = require('fs');
const path = __dirname + "/../../../keystore";
const fileName = path + "/wallet.dat";
const AccountHandle = require('./account-handle');
class SqliteHandle {
constructor() {}
init() {
if(!fs.existsSync(path)) fs.mkdirSync(path);
if(!fs.existsSync(fileName)) {
var db = new sqlite3.Database(fileName,function(err) {
db.run("create table account_data (" +
"private_key varchar(600) PRIMARY KEY NOT NULL," +
"address varchar(100) NOT NULL," +
"name varchar(50) NOT NULL)",function(){
for(let i=1; i<=10; i++) {
let account = new AccountHandle().createAccount();
new SqliteHandle().add(account.secretKey, account.addr, 'Account' + i);
}
db.close();
});
});
}
}
add(private_key, address, name) {
let db = new sqlite3.Database(fileName, function(err){
db.run("insert into account_data(" +
"private_key, address, name) " +
"values($key,$address,$name)",
{
$key:private_key,
$address:address,
$name:name
}, function(err){
db.close();
});
});
}
getOne(addr, callback) {
let db = new sqlite3.Database(fileName, function(err){
db.get("select * from account_data " +
"where address=$addr",
{
$addr: addr
},
function(err,row){
db.close();
if(callback != undefined) callback(row);
});
});
}
getAll(callback) {
let db = new sqlite3.Database(fileName, function(err) {
db.all("select * from account_data",
function(err, rows) {
db.close();
if(callback != undefined) callback(rows);
});
});
}
modifyName(private_key, name) {
let db = new sqlite3.Database(fileName, function(err){
db.run("update account_data " +
"set name=$name " +
"where private_key = $key",
{
$key: private_key,
$name: name
}),
function(err){
db.close();
}
});
}
}
module.exports = SqliteHandle;