forked from sCrypt-Inc/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelectrumx.js
More file actions
127 lines (113 loc) · 3.5 KB
/
Copy pathelectrumx.js
File metadata and controls
127 lines (113 loc) · 3.5 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
const ElectrumCli = require('electrum-client')
var isTest = process.env.NETWORK === 'testnet' ? true : false;
var eclreuse = null
var curHost = ""
var curPort = ""
var serverpool = [
'electrumx.radiantlayerone.com:50010',
]
var serverpoolTestnet = [
'electrumx-testnet.radiantlayerone.com:50010',
]
function Connection(){}
function getCliFromPool(){
if(eclreuse!=null)return (async()=>{return eclreuse})()
// get the fastest server
else return Promise.race((isTest ? serverpoolTestnet : serverpool).map(server=>{
var esHost = server.split(':')[0]
var esPort = parseInt(server.split(':')[1])
var ecl = new ElectrumCli(esPort, esHost, 'tcp')
return ecl.connect().then(()=>{
return ecl
})
})).then((ecl)=>{
curHost = ecl.host
curPort = ecl.port
eclreuse = ecl
ecl.onClose = ()=>{
console.log("Presisted Connection to ElectrumX Server Closed.")
eclreuse = null
}
return ecl.server_version(`RAD Electrumx Client`, "1.4").then(()=>{
return ecl.serverDonation_address().then(address=>{
console.log("Presisted Connection to ElectrumX Server Established.")
console.log(` ElectrumX Server Connected: ${curHost + ":" + curPort}`)
console.log(` ElectrumX Server Donations: ${ address }`)
console.log(`You can help electrumX server staying alive by making a donation to it.\n`)
})
}).then(()=>{
return ecl
})
})
}
async function broadcastTx(tx, checkFeeRate = false) {
const hex = tx.toString();
return getCliFromPool().then(ecl=>{
return ecl.connect().then(()=>{
return ecl
})
}).then(ecl=>{
return ecl.blockchainTransaction_broadcast(hex)
}).catch(e=>{
console.log(`Fail to send tx`, e)
return false;
}).then(txid=> {
return txid;
})
}
async function fetchUtxos(address) {
return getCliFromPool().then(ecl=>{
return ecl.connect().then(()=>{
return ecl
})
}).then(ecl=>{
return ecl.blockchainScripthash_listunspent(getScriptHash(address))
}).catch(e=>{
console.log(`Fail to get ${address}'s latest record.`, e)
return []
}).then(utxos=> {
return utxos.map((utxo) => ({
txId: utxo.tx_hash,
outputIndex: utxo.tx_pos,
satoshis: utxo.value,
height: utxo.height,
script: radjs.Script.buildPublicKeyHashOut(address).toHex(),
}))
})
}
async function fetchTX(txid){
var fetchedTX = null
var ecl = null
// check if tx cached
if(program.cache){
var cachedTX = getCache(txid)
if(cachedTX){
console.log(` - Cache matched ${txid}`)
fetchedTX = (async()=>{return cachedTX})()
}
}
// fetch TX from electrumX server
if(fetchedTX == null){
fetchedTX = getCliFromPool().then(result=>{
ecl = result
return ecl.connect()
}).then(r=>{
console.log(` - Getting ${txid}`)
return ecl.blockchainTransaction_get(txid, false).then(tx=>{
if(!tx.code){
tx = bsv.Transaction(tx)
//console.log(tx.id)
setCache(tx.id,tx.toString())
}
return tx
})
})
}
return fetchedTX
}
module.exports = {
getCliFromPool,
fetchTX,
fetchUtxos,
broadcastTx
}