forked from unwriter/datapay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (124 loc) · 3.81 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
143
144
145
146
147
148
149
150
const axios = require("axios");
const bsv = require("bsv");
const callbackWrapper = func => {
return async (options, callback) => {
try {
result = await func(options);
if (callback) callback(null, result);
else return result;
} catch (err) {
if (callback) callback(err);
else throw err;
}
};
};
const getErrorMessage = err => {
let message = err.message;
if (err.response && err.response.data) {
message = err.response.data;
}
if (message.message) {
message = message.message;
}
if (message.context) {
message = message.context;
}
if (message.error) {
message = message.error;
}
return message;
}
let insight;
const connect = options => {
insight = axios.create(options);
};
connect({ baseURL: "https://api.mattercloud.net/api/v3/main" });
module.exports.getUTXOs = async address => {
console.log('Getting utxos');
try {
const res = await insight.post("/addrs/utxo", {
addrs: address.toString()
});
return res.data;
} catch (err) {
throw new Error(`Failed to retrieve utxo's for ${address}: ${getErrorMessage(err)}`);
}
};
module.exports.broadcastBlockchair = async (data) => {
try {
const res = await axios.post("https://api.blockchair.com/bitcoin-sv/push/transaction", { data });
return res.data ? res.data.data.transaction_hash : null;
} catch (err) {
throw new Error(`Failed to broadcast transaction: ${getErrorMessage(err)}`);
}
}
module.exports.broadcastWhatsOnChain = async (txhex) => {
try {
const res = await axios.post('https://api.whatsonchain.com/v1/bsv/main/tx/raw', { txhex });
return res.data ? res.data : null;
} catch (err) {
throw new Error(`Failed to broadcast transaction: ${getErrorMessage(err)}`);
}
}
module.exports.broadcastBitIndex = async (rawtx) => {
try {
const res = await insight.post("/tx/send", { rawtx });
return res.data ? res.data.txid : null;
} catch (err) {
throw new Error(`Failed to broadcast transaction: ${getErrorMessage(err)}`);
}
};
module.exports.build = callbackWrapper(async ({ data, safe, pay, utxos }) => {
const tx = new bsv.Transaction();
if (data && data.length) {
const script = module.exports.createDataScript(data, safe);
tx.addOutput(new bsv.Transaction.Output({ script, satoshis: 0 }));
}
if (pay) {
const { fee, feeb = 1.0, key, to = [], filter } = pay;
if (fee) tx.fee(fee);
else tx.feePerKb(feeb * 1000);
to.forEach(receiver => tx.to(receiver.address, receiver.value));
if (key) {
const privateKey = new bsv.PrivateKey(key);
const address = privateKey.toAddress();
tx.change(address);
if (!utxos) utxos = await module.exports.getUTXOs(address);
if (filter) utxos = filter(utxos);
tx.from(utxos);
tx.sign(privateKey);
}
}
return tx;
});
module.exports.send = callbackWrapper(async options => {
const tx = options.tx || (await module.exports.build(options));
if (options.provider === 'blockchair') {
return await module.exports.broadcastBlockchair(tx.serialize());
}
if (options.provider === 'whatsonchain') {
return await module.exports.broadcastWhatsOnChain(tx.serialize());
}
return await module.exports.broadcastBitIndex(tx.serialize());
});
module.exports.createDataScript = (data, safe) => {
if (typeof data === "string") return bsv.Script.fromHex(data);
const s = new bsv.Script();
// Add OP_RETURN
if (safe) s.add(bsv.Opcode.OP_FALSE);
s.add(bsv.Opcode.OP_RETURN);
// Add data
data.forEach(item => {
if (typeof item === "object" && item.hasOwnProperty("op")) {
s.add({ opcodenum: item.op });
return;
}
if (typeof item === "string" && /^0x/i.test(item)) {
// e.g. 0x6d02
s.add(Buffer.from(item.slice(2), "hex"));
return;
}
s.add(Buffer.from(item));
});
return s;
};