forked from jdh7190/bitpipeTesting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (140 loc) · 3.87 KB
/
index.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
const datapay = require('datapay')
const express = require('express')
const RpcClient = require('bitcoind-rpc')
const fs = require('fs')
const ip = require('ip')
const keyfile = fs.readFileSync(process.cwd() + '/.env', 'utf8')
const KEYS = keyfile.split("\n").filter(function(i) {
return i
}).map(function(i) {
let o = {}
o.key = i.split("=")[0];
o.val = i.split("=")[1];
return o
})
var KEY_MAPPING = {}
KEYS.forEach(function(k) {
KEY_MAPPING[k.key] = k.val
})
const PRIVATE_KEYS = KEYS.filter(function(p) {
try {
datapay.bsv.PrivateKey.fromWIF(p.val)
return p
} catch (e) {
// not a key
}
}).map(function(k) {
return k.val
})
var app = null
var current_index = 0
var rpc
var start = function(o) {
var lambda = null
var port = 8081
if (o) {
lambda = o.lambda
if (o.app) {
app = o.app
} else {
app = express()
}
if (o.port) {
port = o.port
} else if (KEY_MAPPING.PORT) {
port = KEY_MAPPING.PORT
}
} else {
lambda = null
app = express()
}
app.use(express.urlencoded({extended: true}));
app.use(express.json())
app.use(express.static('public'))
if (KEY_MAPPING.LOCAL) {
// rpc
rpc = new RpcClient({
'protocol': 'http',
'user': KEY_MAPPING.rpc_user ? KEY_MAPPING.rpc_user : 'root',
'pass': KEY_MAPPING.rpc_pass ? KEY_MAPPING.rpc_pass : 'bitcoin',
'host': KEY_MAPPING.host ? KEY_MAPPING.host : ip.address(),
'port': '8332',
'limit': 15
})
} else {
// use remote insight
}
app.get('/', function (req, res) {
res.send('Hello Bitpipe!\n\nMake a POST request to:' + req.protocol + "://" + req.headers.host + req.originalUrl + "/bitpipe with a datapay payload.\n\nMore at https://github.com/unwriter/datapay")
})
app.post('/bitpipe', function (req, res) {
// "tx" is Always treated as signed
let signed = req.body.tx ? true : false
let payload = req.body
if (!signed) {
if (!PRIVATE_KEYS || PRIVATE_KEYS.length === 0) {
console.log("Please add private keys to .env file")
console.log("Example:\n")
console.log("PRIVATEKEY1=17HTwz6MnLoFCHad3M4Z8dC9XZAHMW4YPm")
console.log("PRIVATEKEY2=1AatAGkeVN9RFk8qqrmg8BmFiyouHuMsgY")
res.json({success: false, message: "The server doesn't have a signer key"})
return
}
}
current_index = (current_index < PRIVATE_KEYS.length-1 ? current_index+1 : 0) // shuffle through
if (lambda) {
lambda(req, payload, function(err, transformed) {
run(err, transformed, res)
})
} else {
run(null, payload, res)
}
})
if (o && o.onconnect) {
app.listen(port, o.onconnect)
} else {
app.listen(port)
}
}
var run = function(err, payload, res) {
let current_key = PRIVATE_KEYS[current_index]
if (err) {
console.log("Error", err)
} else {
if (payload.pay) {
payload.pay.key = current_key
} else {
payload.pay = { key : current_key }
}
if (KEY_MAPPING.DEBUG) console.log('payload = ', payload)
if (KEY_MAPPING.LOCAL) {
datapay.build(payload, function(err, signed_tx) {
if (KEY_MAPPING.DEBUG) console.log("signed tx = ", signed_tx)
rpc.sendRawTransaction(signed_tx, function(err, r) {
if (err) {
console.log("error: ", err)
res.json({success: false, message: err.toString()})
} else {
if (KEY_MAPPING.DEBUG) console.log("success: ", r)
res.json({success: true, r: r})
}
})
})
} else {
datapay.send(payload, function(e, r) {
if (e) {
console.log("error: ", e)
res.json({success: false, message: e.toString()})
} else {
if (KEY_MAPPING.DEBUG) console.log("success: ", r)
res.json({success: true, r: r})
}
})
}
}
}
if (require.main === module) {
start()
} else {
module.exports = { start: start }
}