This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
verifyBitcoinSignedMessage.js
67 lines (60 loc) · 1.86 KB
/
verifyBitcoinSignedMessage.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
function verifyBitcoinAddress(ev) {
if (ev) {
let splitted = ev.content.split(/\r?\n/)
try {
if(bitcoinjs.address.fromBech32(splitted[0])){return splitted}
} catch (err) {
}
try {
if(bitcoinjs.address.fromBase58Check(splitted[0])){return splitted}
} catch (err) {
}
}
return false
}
function verifyBitcoinSignedEvent(npub, btcAddress, sig) {
let res = bitcoin.verify(npub, btcAddress, sig, '', true)
if (!res) (console.log(npub, btcAddress, "sig failed"))
return res
}
async function getBalance(address) {
p = new Promise((resolve, reject) => {
fetchBalance(address, 0).then(result => {
if (result !== "failed") {
resolve(result)
} else {
fetchBalance(address, 1).then(result => {
if (result !== "failed") {
resolve(result)
} else {
console.log(address)
resolve(false)
}
})
}
})
})
return p
}
async function fetchBalance(address, service) {
p = new Promise((resolve, reject) => {
let request = new XMLHttpRequest()
if (service === 0) {
request.open("GET", "https://api.blockcypher.com/v1/btc/main/addrs/" + address+"/balance");
}
if (service === 1) {
request.open("GET", "https://blockchain.info/rawaddr/" + address);
}
request.send();
request.onerror = function () {resolve("failed")}
request.onload = () => {
if (request.status === 200) {
data = JSON.parse(request.response)
resolve(Number(data["final_balance"]))
} else {
resolve("failed")
}
}
})
return p
}