-
Notifications
You must be signed in to change notification settings - Fork 8
/
tx.js
102 lines (102 loc) · 3.71 KB
/
tx.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
const bsv = require('bsv')
const axios = require('axios')
const fs = require('fs')
const sqlite3 = require('better-sqlite3')
const path = require('path')
/*************************************************************************************
const wallet = new Wallet({ xpriv: <xprivKey string> })
const tx = new Tx(wallet)
tx.add(<bsv.Transaction object>) // Add to tx db
tx.get({ id: <txid> }) // get tx by id
tx.get() // get all unsent txs
tx.push({ id: <txid> }) // push tx by id
tx.push() // push all unsent txs
tx.reset() // clear all txs
tx.size() // get total number of items on the table
tx.clone(<bsv.Transaction object>) // Clone a transaction ('genesis': 1)
*************************************************************************************/
class Tx {
constructor(wallet) {
if (wallet.id) {
let dbpath = wallet.meta.storage.fullPath
if (!fs.existsSync(dbpath)) {
fs.mkdirSync(dbpath, { recursive: true })
}
this.DB = sqlite3(dbpath + "/tx.db")
const stmt = this.DB.prepare("SELECT * FROM sqlite_master WHERE type='table'");
const tables = stmt.all();
const tablenames = tables.map((t) => t.name)
if (!tablenames.includes("tx")) {
this.DB.prepare("CREATE TABLE tx (id TEXT, tx TEXT, genesis INTEGER, sent INTEGER, PRIMARY KEY (id))").run()
}
}
}
reset() {
this.DB.prepare("DELETE FROM tx").run()
}
size() {
let count = this.DB.prepare("SELECT count(*) from tx WHERE sent=0").get();
return count ? count["count(*)"] : 0;
}
clone(transaction) {
return this.DB.prepare("INSERT INTO tx (id, tx, genesis, sent) VALUES (@id, @tx, @genesis, @sent)").run({
id: transaction.id,
genesis: 1,
tx: transaction.toString("hex"),
sent: 0
})
}
add(transaction) {
return this.DB.prepare("INSERT INTO tx (id, tx, genesis, sent) VALUES (@id, @tx, @genesis, @sent)").run({
id: transaction.id,
genesis: 0,
tx: transaction.toString("hex"),
sent: 0
})
}
get(o) {
if (o && ("id" in o)) {
if(Array.isArray(o.id)) {
const sql = `SELECT * FROM tx WHERE id IN (${o.id.map(() => '?').join(',')})`;
return this.DB.prepare(sql).all(o.id);
} else {
return this.DB.prepare("SELECT * from tx WHERE id=?").all(o.id);
}
} else {
return this.DB.prepare("SELECT * from tx WHERE sent=0").all();
}
}
async push(o) {
let items = this.get(o)
let fitems = items.filter(item => !item.genesis)
let counter = 0;
console.log("Pushing", fitems.length)
for(let item of fitems) {
let raw = item.tx
if (o && o.except && o.except.includes(item.id)) {
console.log("already sent", item.id)
} else {
console.log('pushing', raw)
try {
if (o && o.rpc) {
let res = await axios.post(o.rpc, {
method: 'sendrawtransaction',
params: [raw],
})
} else {
let res = await axios.post('https://api.whatsonchain.com/v1/bsv/main/tx/raw', { txhex: raw })
}
console.log("Updating to sent:", item.id)
// Change to sent
this.DB.prepare("UPDATE tx SET sent=1 WHERE id=?").run(item.id)
counter++;
} catch (e) {
console.log("Error", e)
console.log("Sent", counter)
process.exit();
}
}
}
}
}
module.exports = Tx