-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
109 lines (92 loc) · 3.11 KB
/
index.html
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
<!DOCTYPE HTML>
<html>
<head>
<meta name="description" content="flight wallet - Pay Bitcoin securely">
<script>
// from https://stackoverflow.com/questions/247483/http-get-request-in-javascript
function httpGetAsync(theUrl, callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
function parseURLParams(location) {
var query = location.search || "?"
var params = query.slice(1).split("&").map((str) => str.split("=")).reduce((obj, [key, value]) => ({ [key]: value, ...obj }), {})
console.log('params', params)
return params
}
function redirectTo(uri) {
console.log('redirect to', uri)
try {
window.location = uri
} catch (err) {
console.error(err)
}
}
function printQR(data) {
document.querySelector('#link').href = data
document.querySelector('#qrcode').innerText = data
}
function bitcoinUnspentsToFWFormat(unspent) {
var tx_hash = unspent.txid
var vout = unspent.vout
var script = unspent.scriptPubKey
var value = unspent.satoshis
var height = unspent.height
var confirmations = unspent.confirmations
return {
tx_hash,
vout,
script,
value,
height,
confirmations,
}
}
var blockchainAPI = 'https://test-insight.swap.online/insight-api'
var etherscanAPI = 'https://api.etherscan.io/api'
var rinkebyAPI = 'https://api-rinkeby.etherscan.io/api'
var params = parseURLParams(window.location)
if (!params.address) {
handleError()
}
if (params.chain == 'bitcoin' || !params.chain) {
httpGetAsync(blockchainAPI + '/addr/' + params.address + '/utxo', function (utxo) {
var parsedUnspents = JSON.parse(utxo).map(bitcoinUnspentsToFWFormat)
var dataURI = "flightwallet://update?chain=bitcoin&data=" + JSON.stringify(parsedUnspents)
printQR(dataURI)
redirectTo(dataURI)
})
} else if (params.chain == 'ethereum') {
var apiUrl = etherscanAPI
+ '?module=proxy&action=eth_getTransactionCount&tag=latest&address='
+ params.address
httpGetAsync(apiUrl, function (response) {
var nonce = JSON.parse(response).result
var dataURI = "flightwallet://update?chain=ethereum&data=" + nonce
printQR(dataURI)
redirectTo(dataURI)
})
} else if (params.chain == 'rinkeby') {
var apiUrl = rinkebyAPI
+ '?module=proxy&action=eth_getTransactionCount&tag=latest&address='
+ params.address
httpGetAsync(apiUrl, function (response) {
var nonce = JSON.parse(response).result
var dataURI = "flightwallet://update?chain=ethereum&data=" + nonce
printQR(dataURI)
redirectTo(dataURI)
})
}
</script>
</head>
<body>
<h1>Sync your wallet</h1>
<a id="link" href="#">Open in app</a>
<div id="qrcode"></div>
</body>
</html>