-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbackends.js
190 lines (180 loc) · 4.45 KB
/
backends.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
/*
This module handles calling out to other backends.
- query information
- download transactions
- broadcast transactions
*/
const mattercloud = require('mattercloudjs').instance({
api_key: '4ZiBSwCzjgkCzDbX9vVV2TGqe951CBrwZytbbWiGqDuzkDETEkLJ9DDXuNMLsr8Bpj'
})
const bsv = require('bsv')
const bitbus = require('./bitbus.js')
const axios = require('axios')
async function get_bitquery (query)
{
return bitbus.get_array(query)
}
async function get_rawtx (identifier)
{
// TODO STUB: return transaction from mattercloud or whatsonchain or anything
}
async function get_utxos (address)
{
// TODO STUB: return transactions, coins spendable by address
}
async function broadcast (transaction)
{
// TODO STUB: broadcast transaction to network, return identifier of result
}
// Functions that were originally in bitdb.js
async function findTx (id) {
var queryTx = {
'v': 3,
'q': {
'find': {
'tx.h': id
},
'project': {
'tx.h': 1,
'blk.h': 1
}
}
}
var r = await get_bitquery(queryTx)
return r
}
async function findMightExist (buffer) {
// findExist is renamed to findMightExist because the sha1 could mismatch the data
var hash = bsv.crypto.Hash.sha1(buffer).toString('hex')
// B or Bcat
var queryHash = {
'v': 3,
'q': {
'find': {
'$or': [{ 'out.s1': '15DHFxWZJT58f9nhyGnsRBqrgwK4W6h4Up' }, { 'out.s1': '19HxigV4QyBv3tHpQVcUEQyq1pzZVdoAut' }],
'out.s5': hash
},
'project': {
'out.s1': 1,
'out.s3': 1,
'tx.h': 1
}
}
}
var queryHashGenesis = {
'v': 3,
'q': {
'find': {
'$or': [{ 'out.s2': '15DHFxWZJT58f9nhyGnsRBqrgwK4W6h4Up' }, { 'out.s2': '19HxigV4QyBv3tHpQVcUEQyq1pzZVdoAut' }],
'out.s6': hash
},
'project': {
'out.s2': 1,
'out.s4': 1,
'tx.h': 1
}
}
}
var r = await get_bitquery(queryHash)
var r2 = await get_bitquery(queryHashGenesis)
for (var index = 0; index < r.length; ++ index) {
data = r[index]
r[index] = {
'prefix': data.out[0].s1,
'contenttype': data.out[0].s3,
'txid': data.tx.h
}
}
for (var index = 0; index < r2.length; ++ index) {
data = r2[index]
r2[index] = null
r.push({
'prefix': data.out[0].s2,
'contenttype': data.out[0].s4,
'txid': data.tx.h
})
}
return r.filter(record => record.prefix && (
// TODO: since the query already performed this comparison, this is probably where we want to check the data, and only return transactions with correct data. the comparison is migrated from bitdb.
record.prefix === '15DHFxWZJT58f9nhyGnsRBqrgwK4W6h4Up' || record.prefix === '19HxigV4QyBv3tHpQVcUEQyq1pzZVdoAut'
))
}
async function findD (key, address) {
var queryD = {
'v': 3,
'q': {
'find': {
'in.e.a': address || undefined,
'out.s2': key || undefined,
'out.s1': '19iG3WTYSsbyos3uJ733yK4zEioi1FesNU'
},
'project': {
'out.s2': 1,
'out.s3': 1,
'out.s4': 1,
'out.s5': 1,
'in.e.a': 1,
'blk.i': 1
}
}
}
var queryDGenesis = {
'v': 3,
'q': {
'find': {
'in.e.a': address || undefined,
'out.s3': key || undefined,
'out.s2': '19iG3WTYSsbyos3uJ733yK4zEioi1FesNU'
},
'project': {
'out.s3': 1,
'out.s4': 1,
'out.s5': 1,
'out.s6': 1,
'in.e.a': 1,
'blk.i': 1
}
}
}
var r = await get_bitquery(queryD)
var r2 = await get_bitquery(queryDGenesis)
for (var index = 0; index < r.length; ++ index) {
data = r[index]
r[index] = {
'key': data.out[0].s2,
'value': data.out[0].s3,
'type': data.out[0].s4,
'sequence': data.out[0].s5,
'address': data.in[0].e.a,
'height': data.blk.i
}
}
for (var index = 0; index < r2.length; ++ index) {
data = r2[index]
r2[index] = null
r.push({
'key': data.out[0].s3,
'value': data.out[0].s4,
'type': data.out[0].s5,
'sequence': data.out[0].s6,
'address': data.in[0].e.a,
'height': data.blk.i
})
}
return r.sort(function(a, b) {
if (b.height == a.height) {
return b.sequence - a.sequence
} else {
return b.height - a.height
}
})
}
module.exports = {
get_bitquery: get_bitquery,
get_rawtx: get_rawtx,
get_utxos: get_utxos,
broadcast: broadcast,
findTx: findTx,
findMightExist: findMightExist,
findD: findD
}