This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbackground.js
125 lines (97 loc) · 3.32 KB
/
background.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
/*
Copyright (C) Paul Burke 2021
Github: @ipaulpro/bitcloutplus
Distributed under the MIT License (license terms are at http://opensource.org/licenses/MIT).
*/
const PUBLIC_KEY_PREFIX = 'BC1YL'
const PUBLIC_KEY_LENGTH = 55
const DELAY_MS_MEMPOOL_CALL = 5 * 60 * 1000 // 5 min
const KEY_LATEST_MEMPOOL_CALL = 'latest-mempool-call'
const KEY_LATEST_MEMPOOL_USERS = 'latest-mempool-users'
const MSG_GET_MEMPOOL_TRANSACTORS = 'get-mempool-transactors'
// Omnibox
chrome.omnibox.onInputEntered.addListener(async text => {
// Encode user input for special characters , / ? : @ & = + $ #
const query = encodeURIComponent(text)
if (isPublicKeyBase58Check(query)) {
let username
try {
username = await getUsernameForPublicKey(query)
} catch (e) {
console.error(`No username found for '${query}'`, e)
}
if (username) {
await openUserInTab(username)
return
}
}
await openUserInTab(query)
})
const isPublicKeyBase58Check = (query) => {
return query.startsWith(PUBLIC_KEY_PREFIX) && query.length === PUBLIC_KEY_LENGTH
}
const getUsernameForPublicKey = (publicKey) => {
if (!publicKey) return Promise.reject('Missing required parameter publicKey')
return fetch(`https://node.deso.org/api/v0/get-user-name-for-public-key/${publicKey}`)
.then(res => res.json())
.then(atob)
}
const openUserInTab = async (username) => {
const newURL = 'https://node.deso.org/u/' + username
await chrome.tabs.create({url: newURL})
}
// Worker thread
chrome.runtime.onMessage.addListener( (message, sender, sendResponse) => {
switch (message.type) {
case MSG_GET_MEMPOOL_TRANSACTORS:
getMempoolTransactors()
.then(mempoolTransactors => sendResponse({mempoolTransactors}))
.catch(() => sendResponse({mempoolTransactors: []}))
}
return true
})
const getMempoolTransactors = async () => {
const savedTransactors = await getSavedMempoolTransactors()
if (savedTransactors) return savedTransactors
const transactions = await getMempoolTransactions()
const transactors = extractTransactors(transactions)
await saveTransactors(transactors)
return transactors
}
const getSavedMempoolTransactors = async () => {
const savedItems = await chrome.storage.local.get([KEY_LATEST_MEMPOOL_CALL, KEY_LATEST_MEMPOOL_USERS])
const latestCall = savedItems[KEY_LATEST_MEMPOOL_CALL] ?? 0
if (Date.now() - latestCall < DELAY_MS_MEMPOOL_CALL) {
const savedUsers = savedItems[KEY_LATEST_MEMPOOL_USERS]
return JSON.parse(savedUsers)
}
return null
}
const getMempoolTransactions = () => {
const request = {
'headers': {
'content-type': 'application/json',
},
'method': 'POST',
'body': JSON.stringify({
IsMempool: true
})
}
return fetch(`https://node.deso.org/api/v1/transaction-info`, request)
.then(res => res.json())
.then(res => res['Transactions'])
}
const extractTransactors = (transactions) => {
const transactors = new Set()
transactions.forEach(transaction => {
const transactor = transaction['TransactionMetadata']['TransactorPublicKeyBase58Check']
transactors.add(transactor)
})
return [...transactors]
}
const saveTransactors = async (transactors) => {
const items = {}
items[KEY_LATEST_MEMPOOL_CALL] = Date.now()
items[KEY_LATEST_MEMPOOL_USERS] = JSON.stringify(transactors)
await chrome.storage.local.set(items)
}