-
Notifications
You must be signed in to change notification settings - Fork 373
/
index.js
115 lines (98 loc) · 3.77 KB
/
index.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
// builtin
var crypto = require('crypto');
// local
var base58 = require('./base58');
var address_types = {
bitcoin: '00', // 0 Decimal 1 prefix
bitcoinTestnet: '6f', //111 Decimal mn prefix
litecoin: '30', // 48 Decimal L prefix
litecoinTestnet: '6f', //111 Decimal mn prefix
peercoin: '37', // 55 Decimal P prefix
peercoinTestnet: '6f', //111 Decimal mn prefix
dogecoin: '1e', // 30 Decimal D prefix
dogecoinTestnet: '71', //113 Decimal n prefix
beavercoin: '19', // 25 Decimal B prefix
beavercoinTestnet: '6f', //111 Decimal mn prefix
freicoin: '00', // 0 Decimal 1 prefix
freicoinTestnet: '6f', //111 Decimal mn prefix
protoshares: '38', // 56 Decimal P prefix
protosharesTestnet: '6f', //111 Decimal mn prefix
megacoin: '32', // 50 Decimal M prefix
megacoinTestnet: '6f', //111 Decimal mn prefix
primecoin: '17', // 23 Decimal A prefix
primecoinTestnet: '6f', //111 Decimal mn prefix
auroracoin: '17', // 23 Decimal A prefix
auroracoinTestnet: '6f', //111 Decimal mn prefix
namecoin: '34'
//namecoinTestnet: '' //TODO
//That's all for now, to add more just send a pull request
};
var p2sh_types = {
bitcoin: '05', // 5 Decimal 3 prefix
bitcoinTestnet: 'c4', //196 Decimal 2 prefix
litecoin: '05', // 5 Decimal 3 prefix
litecoinTestnet: 'c4', //196 Decimal 2 prefix
peercoin: '75', //117 Decimal p prefix
peercoinTestnet: 'c4', //196 Decimal 2 prefix
dogecoin: '16', //22 Decimal 9A prefix
dogecoinTestnet: 'c4', //196 Decimal 2 prefix
beavercoin: '05', // 5 Decimal 3 prefix
beavercoinTestnet: 'c4', //196 Decimal 2 prefix
freicoin: '05', // 5 Decimal 3 prefix
freicoinTestnet: 'c4', //196 Decimal 2 prefix
protoshares: '05', // 5 Decimal 3 prefix
protosharesTestnet: 'c4',//196 Decimal 2 prefix
megacoin: '05', // 5 Decimal 3 prefix
megacoinTestnet: 'c4', //196 Decimal 2 prefix
primecoin: '53', // 83 Decimal a prefix
primecoinTestnet: 'c4', //196 Decimal 2 prefix
auroracoin: '05', // 83 Decimal a prefix
auroracoinTestnet: 'c4' //196 Decimal 2 prefix
//namecoin: '', //TODO
//namecoinTestnet: '' //TODO
};
/// return address type if valid base58 address, otherwise null
function get_address_type(address) {
var decoded_hex;
try {
decoded_hex = base58.decode(address);
} catch (e) {
// if decoding fails, assume invalid address
return null;
}
// make a usable buffer from the decoded data
var decoded = new Buffer(decoded_hex, 'hex');
// should be 25 bytes per btc address spec
if (decoded.length != 25) {
return null;
}
var length = decoded.length;
var cksum = decoded.slice(length - 4, length).toString('binary');
var body = decoded.slice(0, length - 4);
var good_cksum = sha256_digest(sha256_digest(body)).toString('binary').substr(0,4);
return (cksum === good_cksum ? decoded_hex.slice(0, 2) : null);
}
module.exports.get_address_type = get_address_type;
/// check if a wallet address is valid
/// if address_type is supplied
/// also checks that the address matches that expected version
/// return {boolean} true if valid, false otherwise
function validate(address, address_type) {
// default is to check that address is regular production address
address_type = address_type || 'bitcoin';
var type = get_address_type(address);
if (type === null) {
return false;
}
if (type !== address_types[address_type] &&
type !== p2sh_types[address_type]) {
return false;
}
return true;
}
module.exports.validate = validate;
/// private methods
// helper to perform sha256 digest
function sha256_digest(payload) {
return crypto.createHash('sha256').update(payload).digest();
}