-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
74 lines (57 loc) · 1.78 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
// builtin
var crypto = require('crypto');
// local
var base58 = require('./base58');
var address_types = {
prod: '00',
testnet: '6f'
};
var p2sh_types = {
prod: '05',
testnet: 'c4'
};
/// 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 || 'prod';
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();
}