forked from 7THStage/idigdoge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddresses.js
70 lines (51 loc) · 1.55 KB
/
addresses.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
var crypto = require('crypto');
var Big = require('bignumber.js');
var base58 = {
zero: Big(0)
, base: Big(58)
, chars: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.split('')
, decode: function(string) {
var r = Big(0);
var m = Big(1);
for (var i = (string.length - 1), c; i >= 0; i -= 1) {
c = base58.chars.indexOf(string[i]);
// Invalid character
if (c < 0) return false;
r = r.plus(m.times(c));
m = m.times(base58.base);
}
r = r.toString(16);
// Make sure it's a proper hex string
if (r.length % 2) r = '0' + r;
return r;
}
};
function sha256(buf) {
var hash = crypto.createHash('sha256');
hash.update(buf);
return hash.digest();
};
var dogeRegExp = /^D([123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{33})$/;
function validAddress(string) {
// Do a very basic regex check first
if (!dogeRegExp.test(string)) return false;
var decoded = base58.decode(string);
// It failed, which means it's not valid
if (!decoded) return false;
// Get it to the correct length
while (decoded.length < 50) {
decoded = '0' + decoded;
}
// Make sure it's a Dogecoin address
if (decoded.substr(0, 2) != '1e') return false;
// Split into key and signature
var key = decoded.substr(0, 42);
var sig = decoded.substr(42, 8);
// Calculate the checksum
var checksum = sha256(sha256(new Buffer(key, 'hex')));
// Check if they match
if (sig == checksum.slice(0, 4).toString('hex')) return true;
// It didn't, so there's something wrong
return false;
};
module.exports = validAddress;