-
Notifications
You must be signed in to change notification settings - Fork 284
/
address.js
49 lines (40 loc) · 1.26 KB
/
address.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
const bs58check = require('bs58check')
const configManager = require('../managers/config')
const utils = require('../crypto/utils')
const PublicKey = require('./public-key')
module.exports = class Address {
static fromPassphrase(passphrase, networkVersion) {
return Address.fromPublicKey(
PublicKey.fromPassphrase(passphrase),
networkVersion,
)
}
static fromPublicKey(publicKey, networkVersion) {
const pubKeyRegex = /^[0-9A-Fa-f]{66}$/
if (!pubKeyRegex.test(publicKey)) {
throw new Error(`publicKey '${publicKey}' is invalid`)
}
if (!networkVersion) {
networkVersion = configManager.get('pubKeyHash')
}
const buffer = utils.ripemd160(Buffer.from(publicKey, 'hex'))
const payload = Buffer.alloc(21)
payload.writeUInt8(networkVersion, 0)
buffer.copy(payload, 1)
return bs58check.encode(payload)
}
static fromPrivateKey(privateKey, networkVersion) {
return Address.fromPublicKey(privateKey.publicKey, networkVersion)
}
static validate(address, networkVersion) {
if (!networkVersion) {
networkVersion = configManager.get('pubKeyHash')
}
try {
const decode = bs58check.decode(address)
return decode[0] === networkVersion
} catch (e) {
return false
}
}
}