-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.js
42 lines (35 loc) · 879 Bytes
/
utils.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
const bitcoin = require('bitcoinjs-lib')
function isCompressedWIF (privateKey) {
if (privateKey.length === 52) {
return true
}
if (privateKey.length === 51) {
return privateKey.charAt(0) !== '5'
}
if (privateKey.length < 51) {
return privateKey.charAt(0) !== '5'
}
}
function isRealWIF (publicAddress, WIF) {
try {
const keyPair = bitcoin.ECPair.fromWIF(WIF)
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey })
return address === publicAddress
} catch (e) {
return false
}
}
function log (msg) {
if (process.env.NODE_ENV !== 'test') {
console.log(msg)
}
}
function replaceAt (string, index, replace) {
return string.substring(0, index) + replace + string.substring(index + 1)
}
module.exports = {
isCompressedWIF: isCompressedWIF,
isRealWIF: isRealWIF,
log: log,
replaceAt: replaceAt
}