-
Notifications
You must be signed in to change notification settings - Fork 0
/
TMutils.js
165 lines (134 loc) · 3.72 KB
/
TMutils.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
/* BMutils.js */
'use strict';
const bitcore = require('bitcore-lib')
const explorers = require('bitcore-explorers')
const fs = require('fs')
module.exports = {
log: prefixLog,
isNum: isNum,
hexToAscii: hexToAscii,
getBTCNetwork: getBTCNetwork,
createBTCKey: createBTCKey,
getBTCAddr: getBTCAddr,
isValidAddr: isValidAddr,
getBTCAddrBalance: getBTCAddrBalance,
chunkMessage: chunkMessage,
assembleChunks: assembleChunks,
existsFileDir: existsFileDir,
createDirectory: createDirectory,
getFileExtension: getFileExtension,
saveObject: saveObject,
loadObject: loadObject
};
module.exports.DBG = false
module.exports.MODE = {
DEFAULT: 1,
NEW : 2,
TMP : 3
}
/* Prints log with BM prefix */
function prefixLog(prefix, msg){
return console.log('['+prefix+'] '+msg);
}
/*__________ FILESYSTEM __________*/
/* Save an object to file */
function saveObject(dir, name, data){
/* Write to disk */
if (!fs.existsSync(dir))
fs.mkdirSync(dir);
fs.writeFileSync(dir+'/'+name, JSON.stringify(data, null, 2));
}
/* Read object from file */
function loadObject(fname){
if (!fs.existsSync(fname)) throw "ERR: filename does not exist"
var fileData = fs.readFileSync(fname);
var obj = JSON.parse(fileData);
return obj
}
/* Returns the extension of a file */
function getFileExtension(fname){
return fname.substr((~-fname.lastIndexOf(".") >>> 0) + 2);
}
/* Return true is filename exists */
function existsFileDir(fname){
return fs.existsSync(fname)
}
/* Creates a directory */
function createDirectory(dir){
if (!fs.existsSync(dir))
fs.mkdirSync(dir);
}
/*__________ STRINGS __________*/
/* Split message 'str' in chunks of 'size' letters */
function chunkMessage(str, size) {
var numChunks = Math.ceil(str.length / size),
chunks = new Array(numChunks);
for(var i = 0, o = 0; i < numChunks; ++i, o += size) {
chunks[i] = str.substr(o, size);
}
return chunks;
}
/* Put string chunks together */
function assembleChunks(chunks){
var str = ""
for(var i=0; i<chunks.length; i++){
str += chunks[i]
}
return str
}
/*__________ NUMBERS __________*/
/* Convert hex string to ASCII */
function hexToAscii (str1){
var hex = str1.toString();
var str = '';
for (var n = 0; n < hex.length; n += 2)
str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
return str;
}
/* Creates a random hexadecimal value of 'len' length */
function randHex(len) {
var chars = '0123456789abcdef';
var result = '';
for (var i = len; i > 0; --i) result += chars[Math.floor(Math.random() * chars.len)];
return result;
}
/* Check if 'num' is a number */
function isNum(num){
return !isNaN(num)
}
/*__________ BITCOIN __________*/
/* Create new Bitcoin address */
function createBTCKey(){
return new bitcore.PrivateKey().toWIF();
}
/* Returns the address for the key */
function getBTCAddr(privKey, BTCnet){
var pk = new bitcore.PrivateKey(privKey)
return pk.toAddress(BTCnet).toString()
}
/* Return the network for */
function getBTCNetwork(addr){
var addrObj = new bitcore.Address(addr)
return addrObj.network
}
/* Check if 'addr' is a valid address for 'net' network*/
function isValidAddr(addr, net){
return bitcore.Address.isValid(addr, net)
}
/* Check if 'addr' is a valid Bitcoin address */
function isValidBTCAddr(addr){
var net = getBTCNetwork(addr)
return bitcore.Address.isValid(addr, net)
}
/* Returns the total spendable satoshis for 'addr' */
function getBTCAddrBalance(addr, callback){
var network = getBTCNetwork(addr)
var insight = new explorers.Insight(network)
insight.getUnspentUtxos(addr, function(err, utxos){
if(err) return callback(err)
var balance = 0
for(var i in utxos)
balance += utxos[i].satoshis
return callback(null, balance)
});
}