-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.js
321 lines (262 loc) · 11.6 KB
/
model.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
const { Address, PrivateKey, Transaction } = require("bitcore-lib")
const Mnemonic = require('bitcore-mnemonic')
const __URL__ = 'https://dogecoin.earlyordies.com/';
Transaction.DUST_AMOUNT = 1000000
const DERIVATION = "m/44'/3'/0'/0/0"
const NUM_RETRIES = 3
class Model {
constructor() {
this.hasAllPermissions = undefined
this.credentials = undefined
this.acceptedTerms = undefined
this.numUnconfirmed = undefined
this.utxos = undefined
this.inscriptions = undefined
}
async requestPermissions() {
await browser.permissions.request({ origins: ["*://dogechain.info/*", "*://api.blockchair.com/*"] })
await this.loadPermissions()
if (!this.hasAllPermissions) {
throw new Error('necessary permissions not granted')
}
}
async loadPermissions() {
const permissions = await browser.permissions.getAll()
if (!permissions.origins.includes("*://dogechain.info/*")) {
this.hasAllPermissions = false
return
}
if (!permissions.origins.includes("*://api.blockchair.com/*")) {
this.hasAllPermissions = false
return
}
this.hasAllPermissions = true
}
async load() {
await this.loadPermissions()
const values = await browser.storage.local.get(["privkey", "mnemonic", "derivation", "accepted_terms", "utxos"])
if (values.privkey) {
this.credentials = {
privateKey: new PrivateKey(values.privkey),
mnemonic: values.mnemonic && new Mnemonic(values.mnemonic),
derivation: values.derivation
}
}
this.acceptedTerms = values.accepted_terms
this.utxos = values.utxos
}
async acceptTerms() {
await browser.storage.local.set({ accepted_terms: true })
this.acceptedTerms = true
}
generateRandomCredentials() {
const mnemonic = new Mnemonic(Mnemonic.Words.ENGLISH);
const privateKey = mnemonic.toHDPrivateKey().deriveChild(DERIVATION).privateKey
return { privateKey, mnemonic, derivation: DERIVATION }
}
createCredentialsFromMnemonic(mnemonicText) {
const mnemonic = new Mnemonic(mnemonicText);
const privateKey = mnemonic.toHDPrivateKey().deriveChild(DERIVATION).privateKey
return { privateKey, mnemonic, derivation: DERIVATION }
}
createCredentialsFromPrivateKey(privateKeyWIF) {
const privateKey = new PrivateKey(privateKeyWIF);
return { privateKey, mnemonic: null, derivation: null }
}
async storeCredentials(credentials) {
await browser.storage.local.set({
privkey: credentials.privateKey.toWIF(),
mnemonic: credentials.mnemonic && credentials.mnemonic.toString(),
derivation: credentials.derivation
})
this.credentials = credentials
}
async refreshUtxos() {
let utxos = []
let round = 1
let done = false
while (!done) {
for (let retry = 0; retry < NUM_RETRIES; retry++) {
try {
// query latest utxos
const address = this.credentials.privateKey.toAddress().toString()
const resp = await fetch(`https://dogechain.info/api/v1/address/unspent/${address}/${round}`)
const json = await resp.json()
if (!json.success) throw new Error('dogechain.info error')
// convert response to our utxo format
const partial_utxos = json.unspent_outputs.map(unspent_output => {
return {
txid: unspent_output.tx_hash,
vout: unspent_output.tx_output_n,
script: unspent_output.script,
satoshis: unspent_output.value,
confirmations: unspent_output.confirmations
}
})
if (partial_utxos.length == 0) {
done = true
}
partial_utxos.forEach(utxo => utxos.push(utxo))
round += 1
break
}
catch (e) {
console.log(e)
if (retry == NUM_RETRIES - 1) throw e
}
}
}
// sort in order of newest to oldest
utxos.sort((a, b) => (a.confirmations || 0) - (b.confirmations || 0))
// log the utxos
utxos.forEach(utxo => {
console.log(utxo.txid + ":" + utxo.vout + ` (sats=${utxo.satoshis} confs=${utxo.confirmations})`)
})
// filter out unconfirmed because they wont be indexed
const unconfirmedUtxos = utxos.filter(x => !x.confirmations)
const confirmedUtxos = utxos.filter(x => x.confirmations > 0)
this.numUnconfirmed = unconfirmedUtxos.length
this.utxos = confirmedUtxos
// check if these utxos are the same
if (JSON.stringify(confirmedUtxos) == JSON.stringify(this.utxos)) {
return
}
// check that the utxos are in sync with indexer
const resp2 = await fetch("https://dogechain.info/api/v1/block/besthash")
const json2 = await resp2.json()
if (!json2.success) throw new Error('bad request')
const resp3 = await fetch(`${__URL__}block/${json2.hash}`)
if (resp3.status != 200) throw new Error(""+__URL__+" is out of sync")
// save them for next time
await browser.storage.local.set({ utxos: confirmedUtxos })
}
async refreshDoginals() {
const { inscriptionIds, inscriptionOutpoints } = await this.refreshInscriptionIds()
await this.refreshInscriptionContent(inscriptionIds, inscriptionOutpoints)
}
async refreshInscriptionIds() {
// read the inscriptions we have for each output
const keys = this.utxos.map(utxo => `inscriptions_at_${utxo.txid}:${utxo.vout}`)
const inscriptionIdsPerOutput = await browser.storage.local.get(keys)
const allInscriptionIds = []
const inscriptionOutpoints = []
// if are missing any, download them
for (const utxo of this.utxos) {
const key = `inscriptions_at_${utxo.txid}:${utxo.vout}`
if (!inscriptionIdsPerOutput[key]) {
const resp = await fetch(`${__URL__}output/${utxo.txid}:${utxo.vout}`)
const html = await resp.text()
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const main = doc.getElementsByTagName("main")[0]
const list = main.getElementsByTagName("dl")[0]
const thumbnails = Array.from(list.getElementsByTagName("dd")).filter(x => x.className == "thumbnails")
const inscriptionIds = thumbnails.map(x => x.getElementsByTagName("a")[0].getAttribute("href").split("/shibescription/")[1])
inscriptionIdsPerOutput[key] = inscriptionIds
inscriptionIds.forEach(x => {
allInscriptionIds.push(x)
inscriptionOutpoints.push(`${utxo.txid}:${utxo.vout}`)
})
if (inscriptionIds.length || utxo.confirmations > 10) {
await browser.storage.local.set({ [key]: inscriptionIds })
}
} else {
inscriptionIdsPerOutput[key].forEach(x => {
allInscriptionIds.push(x)
inscriptionOutpoints.push(`${utxo.txid}:${utxo.vout}`)
})
}
}
return { inscriptionIds: allInscriptionIds, inscriptionOutpoints }
}
async refreshInscriptionContent(inscriptionIds, inscriptionOutpoints) {
const keys = inscriptionIds.map(x => `inscription_${x}`)
const inscriptions = await browser.storage.local.get(keys)
// download missing content
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
if (!inscriptions[key]) {
const inscriptionId = inscriptionIds[i];
const url = `${__URL__}content/${inscriptionId}`
// const resp = await fetch(url)
// const blob = await resp.blob()
const data = url;
// const data = await new Promise((resolve) => {
// const reader = new FileReader()
// reader.onload = function() { resolve(this.result) }
// reader.readAsDataURL(blob);
// })
const url2 = `${__URL__}shibescription/${inscriptionId}`
const resp2 = await fetch(url2)
const html = await resp2.text()
const parser = new DOMParser()
const doc = parser.parseFromString(html, 'text/html')
const main = doc.getElementsByTagName("main")[0]
const h1 = main.getElementsByTagName("h1")[0]
const number = h1.innerHTML.split(" ")[1]
const inscription = {
id: inscriptionId,
data,
outpoint: inscriptionOutpoints[i],
number
}
inscriptions[key] = inscription
await browser.storage.local.set({ [key]: inscription })
}
}
this.inscriptions = inscriptions
}
async sendDoginal(inscription, address) {
let countInOutput = 0;
for (const entry of Object.values(this.inscriptions)) {
if (entry.outpoint == inscription.outpoint) {
countInOutput++
}
}
if (countInOutput == 0) throw new Error("inscription not found")
if (countInOutput > 1) throw new Error("multi-doginal outputs not supported")
const inscriptionUtxo = this.utxos.filter(x => `${x.txid}:${x.vout}` == inscription.outpoint)[0]
if (!inscriptionUtxo) throw new Error("inscription utxo not found")
const change = model.credentials.privateKey.toAddress().toString()
const fundingUtxos = this.utxos.filter(x => {
return !Object.values(this.inscriptions).find(y => y.outpoint == `${x.txid}:${x.vout}`)
})
const tx = new Transaction()
tx.from(inscriptionUtxo)
tx.from(fundingUtxos)
tx.to(address, Transaction.DUST_AMOUNT)
tx.change(change)
tx.sign(model.credentials.privateKey)
tx.toString()
if (tx.inputAmount < tx.outputAmount) {
throw new Error("Not enough funds")
}
console.log("funding utxos:", fundingUtxos)
console.log("tx:", tx.toJSON())
const resp = await fetch("https://api.blockchair.com/dogecoin/push/transaction", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
data: tx.toString()
})
})
if (resp.status != 200) {
let json
try {
json = await resp.json()
} catch {
throw new Error(resp.status.toString() + ": " + resp.statusText)
}
if (json.context && json.context.error) {
throw new Error(json.context.error)
} else {
throw new Error(resp.status.toString() + ": " + resp.statusText)
}
}
return tx.hash
}
}
window.model = new Model()