forked from rohenaz/bmap-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.ts
153 lines (143 loc) · 3.65 KB
/
actions.ts
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
import { bmap } from 'bmapjs'
import chalk from 'chalk'
import { getDbo } from './db.js'
const { TransformTx } = bmap
const saveTx = async (tx) => {
let t
let dbo
// Transform
try {
dbo = await getDbo()
} catch (e) {
// await closeDb()
let txid = tx && tx.tx ? tx.tx.h : undefined
throw new Error('Failed to get dbo ' + txid + ' : ' + e)
}
try {
t = await TransformTx(tx)
} catch (e) {
throw new Error('Failed to transform tx ' + tx)
}
// get BAP IDs for given social op
if (t.AIP) {
let bap
// multiple AIP outputs
if (Array.isArray(t.AIP)) {
for (let i = 0; i < t.AIP.length; i++) {
const { address } = t.AIP[i]
bap = await getBAPIdByAddress(
address,
t.blk.i || undefined,
t.timestamp
)
//TODO: add && bap.valid === true when BAP API returns this correctly
if (bap) {
console.log('bap ID found', bap.idKey)
t.AIP[i].bapId = bap.idKey
if (bap.identity) {
t.AIP[i].identity = JSON.parse(bap.identity)
}
}
}
} else {
const { address } = t.AIP
bap = await getBAPIdByAddress(address, t.blk.i || undefined, t.timestamp)
if (bap) {
console.log('bap ID found', bap.idKey)
t.AIP.bapId = bap.idKey
if (bap.identity) {
t.AIP.identity = JSON.parse(bap.identity)
}
}
}
}
if (t) {
let collection = t.blk ? 'c' : 'u'
let txId = tx && tx.tx ? tx.tx.h : undefined
t._id = txId
try {
let timestamp = t.timestamp
delete t.timestamp
await dbo.collection(collection).updateOne(
{ _id: t._id },
{
$set: t,
$setOnInsert: {
timestamp: timestamp || Math.round(new Date().getTime() / 1000),
},
},
{
upsert: true,
}
)
return t
} catch (e) {
console.log('not inserted', e)
console.log(
collection === 'u'
? (chalk.green('saved'), chalk.magenta('unconfirmed'))
: '',
(chalk.cyan('saved'), chalk.green(t.tx.h))
)
throw new Error('Failed to insert tx ' + txId + ' : ' + e)
}
} else {
throw new Error('Invalid tx')
}
}
const clearUnconfirmed = () => {
return new Promise<void>(async (res, rej) => {
let dbo = await getDbo()
dbo
.listCollections({ name: 'u' })
.toArray(async function (err, collections) {
if (
collections
.map((c) => {
return c.name
})
.indexOf('u') !== -1
) {
try {
// ToDo - This can throw errors during sync
await dbo.collection('u').drop(async function (err, delOK) {
if (err) {
// await closeDb()
rej(err)
return
}
if (delOK) res()
})
// await closeDb()
res()
} catch (e) {
// await closeDb()
rej(e)
}
}
})
})
}
export { saveTx, clearUnconfirmed }
const bapApiUrl = `https://bap-api.com/v1`
const getBAPIdByAddress = async function (address, block, timestamp) {
if (bapApiUrl) {
const result = await fetch(`${bapApiUrl}/identity/validByAddress`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
address,
block,
timestamp,
}),
})
const data = await result.json()
if (data && data.status === 'OK' && data.result) {
return data.result
}
}
return false
}