-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
200 lines (159 loc) · 5.08 KB
/
script.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { WalletStore } from './bitcoin/bitcoin.js';
import { BitcoinLink } from './bitcoin/bitcoin-link.js';
import { formatAmountSatoshis, formatAmountUsd, formatUSD } from './ui/formats.js';
import { whatsappShareUri } from './ui/share-uris.js';
import { Views } from './ui/views.js';
import './ui/clipboard.js';
import { parseBitcoinUri } from './bitcoin/bitcoin-uri.js';
let balance = 0;
let txAmount = 0;
let exchangeRate = 0;
const isTestnet = true;
const wallet = WalletStore.loadOrCreate(isTestnet);
function walletHook(url) {
const baseUrl = document.location.origin
return `${baseUrl}/#${url}`
}
function parseBitcoinUriRoute(route) {
const uri = decodeURIComponent(route);
return parseBitcoinUri(uri);
}
function onHashChange() {
const hash = location.hash.substr(1);
if (hash.startsWith('pay')) {
processReceive(hash);
return;
}
if (hash.startsWith('request')) {
processRequest(hash);
return;
}
}
async function processRequest(hash) {
const bitcoinUri = hash.replace('request/', '');
const request = parseBitcoinUriRoute(bitcoinUri);
window.request = request;
const exchangeRate = await wallet.exchangeRate
setRequestAmount(request.amount, exchangeRate);
Views.Request.show();
}
async function processReceive(hash) {
const bitcoinUri = hash.replace('pay/', '');
const receive = parseBitcoinUriRoute(bitcoinUri);
const secret = receive.send;
const tx = receive.tx;
console.log('Received', receive)
const exchangeRate = await wallet.exchangeRate
setReceiveAmount(receive.amount, exchangeRate);
Views.Receive.show();
try {
await BitcoinLink.redeem(secret, wallet.address, tx, isTestnet);
wallet.poll();
} catch (e) {
Views.Receive.hide();
console.error(e)
alert('Link is empty');
location = '#';
}
}
function setBalance(balance) {
const formatted = formatAmountSatoshis(balance, exchangeRate);
Views.Home.balanceFiat = formatted.fiat;
Views.Home.balanceCrypto = formatted.crypto;
}
function setValue(amountUsd) {
txAmount = amountUsd
const formatted = formatAmountUsd(amountUsd, exchangeRate);
Views.Home.amountFiat = formatted.fiat;
Views.Home.amountCrypto = formatted.crypto;
}
function setRequestAmount(amount, exchangeRate) {
const formatted = formatAmountSatoshis(amount, exchangeRate);
Views.Request.amountFiat = formatted.fiat;
Views.Request.amountCrypto = formatted.crypto;
}
function setReceiveAmount(amount, exchangeRate) {
const formatted = formatAmountSatoshis(amount, exchangeRate);
Views.Receive.amountFiat = formatted.fiat;
Views.Receive.amountCrypto = formatted.crypto;
}
Views.Home.onPay(async _ => {
if (!txAmount) {
console.warn('Amount is zero');
return;
}
const amountUsd = formatUSD(txAmount);
const amountCrypto = Math.round(txAmount / exchangeRate * 1e8);
if (amountCrypto > balance) {
console.warn('Amount too large');
return;
}
// generate a temporary wallet
const tempWallet = BitcoinLink.generate(amountCrypto, isTestnet);
console.log(tempWallet.serialize());
await tempWallet.fundWith(wallet, amountCrypto);
const hookUri = walletHook(`pay/${encodeURIComponent(tempWallet.serialize())}`);
// descriptive text and link
const text = `${hookUri}
Here are *~${amountUsd}* in Bitcoin for you.
`
// open Whatsapp
const uri = whatsappShareUri(text);
navigator.clipboard.writeText(hookUri);
if (uri.startsWith('whatsapp:')) {
window.location = uri;
} else {
window.open(uri, '_blank')
}
})
Views.Home.onRequest(_ => {
const usdValue = formatUSD(txAmount);
const btcValue = Math.round(1e8 * Number(txAmount) / exchangeRate);
const myAddress = wallet.address;
const bitcoinUri = `bitcoin:${ myAddress }?amount=${ btcValue }`;
const hookUri = walletHook(`request/${encodeURIComponent( bitcoinUri ) }`)
const text = `${ hookUri }
Please pay me *~${usdValue}* in Bitcoin.
`;
navigator.clipboard.writeText(hookUri);
setTimeout(_ => alert('copied to clipboard'), 1000)
// open Whatsapp
const uri = whatsappShareUri(text);
if (uri.startsWith('whatsapp:')) {
window.location = uri;
} else {
window.open(uri, '_blank')
}
});
Views.Home.onAmountChanged(amount => {
setValue(amount);
});
Views.Request.onPaymentConfirmed(_ => {
const request = window.request;
const recipient = request.address;
const amount = request.amount;
const fee = 300;
wallet.sendTransaction(recipient, amount, fee);
wallet.poll();
Views.Request.hide();
location = '#';
});
Views.Request.onDecline(_ => {
Views.Request.hide()
location = '#';
})
Views.Receive.onConfirmed(_ => {
Views.Receive.hide();
location = '#';
})
wallet.onExchangeRate(rate => {
exchangeRate = rate
setBalance(balance)
});
wallet.onBalance(b => {
balance = b.total
setBalance(balance)
});
wallet.startWatching();
window.addEventListener('hashchange', onHashChange);
onHashChange()