-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlibs.js
315 lines (256 loc) · 11.6 KB
/
libs.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
simple rating and raputation system for BCH
version 0.1
mtrycz
*/
import {BITBOX} from "bitbox-sdk";
let bitbox = new BITBOX()
var claimOpreturnVersion = Buffer.from('0abcde1d', 'hex');
var transactOpreturnVersion = Buffer.from('0abcde1e', 'hex');
var rateOpreturnVersion = Buffer.from('0abcde1f', 'hex');
export function createWallet(seed) {
seed = seed || bitbox.Mnemonic.fromEntropy(bitbox.Crypto.randomBytes(32));
let seedBuffer = bitbox.Mnemonic.toSeed(seed)
return bitbox.HDNode.fromSeed(seedBuffer);
}
export async function getAnUtxo(anAddress, minAmount) {
minAmount = minAmount || 1000;
let utxos = await bitbox.Address.utxo(anAddress);
let utxo = utxos.utxos.find(u => u.satoshis >= 1000);
if (utxo)
return utxo;
else
throw "No suitable utxo for transaction. Send more funds.";
}
export async function claimAddress(wallet, network) {
network = network || 'mainnet';
var transactionBuilder = new bitbox.TransactionBuilder(network);
let redeemScript;
let cashAddress = bitbox.HDNode.toCashAddress(wallet);
let address160 = bitbox.Address.cashToHash160(cashAddress);
let messageBuffer = Buffer.from(address160, "hex");
let signature = bitbox.BitcoinCash.signMessageWithPrivKey(
wallet.keyPair.toWIF(),
address160
);
let contentBuffer = Buffer.concat([
claimOpreturnVersion,
messageBuffer,
Buffer.from(signature, 'base64')
]);
console.log(signature);
let opReturn = bitbox.Script.encode([
bitbox.Script.opcodes.OP_RETURN,
contentBuffer,
]);
console.log(opReturn);
var byteCount = bitbox.BitcoinCash.getByteCount({ P2PKH: 1 }, { P2PKH: 1 });
byteCount += contentBuffer.length;
byteCount += signature.length;
byteCount += 9 // some spare bytes for OP_RETURN etc.
var utxo = await getAnUtxo(cashAddress);
transactionBuilder.addInput(utxo.txid, utxo.vout);
transactionBuilder.addOutput(cashAddress, utxo.satoshis - byteCount);
transactionBuilder.addOutput(opReturn, 0);
transactionBuilder.setLockTime(0);
transactionBuilder.sign(0, wallet.keyPair, redeemScript, transactionBuilder.hashTypes.SIGHASH_ALL, utxo.satoshis, transactionBuilder.signatureAlgorithms.SCHNORR)
var tx = transactionBuilder.build();
var hex = tx.toHex();
console.log("Sending tx from "+ utxo.txid +" with op_return "+ claimOpreturnVersion + messageBuffer.toString() + signature + " contentBuffer length "+ contentBuffer.length)
let txid = await bitbox.RawTransactions.sendRawTransaction(hex);
console.log(txid);
return txid;
}
export async function getClaimDetails(transactionId) {
var transaction = await bitbox.Transaction.details(transactionId);
var opReturnOutput = transaction.vout.find(function(out) {return out.scriptPubKey.addresses === undefined;});
var message = opReturnOutput.scriptPubKey.asm;
let result = {};
result.version = message.substring(10, 18);
result.versionValid= result.version === '0abcde1d';
result.address160 = message.substring(18, 58);
result.address = bitbox.Address.hash160ToCash(result.address160);
result.signature = Buffer.from(message.substring(58), 'hex').toString('base64');
result.signatureValid = bitbox.BitcoinCash.verifyMessage(
result.address,
result.signature,
result.address160
);
return result;
}
export async function transact(fromWallet, toWallet, amount, network) {
network = network || 'mainnet';
var transactionBuilder = new bitbox.TransactionBuilder(network);
let redeemScript;
let cashAddress1 = bitbox.HDNode.toCashAddress(fromWallet);
let cashAddress2 = bitbox.HDNode.toCashAddress(toWallet);
let utxo = await getAnUtxo(cashAddress1, amount);
var byteCount = bitbox.BitcoinCash.getByteCount({ P2PKH: 1 }, { P2PKH: 2 });
let originalAmount = utxo.satoshis;
let amountToSend = amount;
let fee = byteCount;
transactionBuilder.addInput(utxo.txid, utxo.vout);
transactionBuilder.addOutput(cashAddress2, amountToSend);
transactionBuilder.addOutput(cashAddress1, originalAmount - amountToSend - fee);
transactionBuilder.setLockTime(0);
transactionBuilder.sign(0, fromWallet.keyPair, redeemScript, transactionBuilder.hashTypes.SIGHASH_ALL, originalAmount, transactionBuilder.signatureAlgorithms.SCHNORR)
var tx = transactionBuilder.build();
var hex = tx.toHex();
return await bitbox.RawTransactions.sendRawTransaction(hex);
}
export function createMessage(reviewee, reviewer, transactionId, rating) {
var reviewee160 = Buffer.from(bitbox.Address.cashToHash160(reviewee), 'hex');
var reviewer160 = Buffer.from(bitbox.Address.cashToHash160(reviewer), 'hex');
var ratedTx = Buffer.from(transactionId, 'hex');
rating = rating.toString().length == 2 ? rating : "0"+rating;
var rating = Buffer.from(rating.toString(), 'hex');
var message = Buffer.concat([reviewee160, reviewer160, ratedTx, rating]);
return message;
}
export async function rateTransaction(reviewee, reviewer, transactionId, rating, wallet, network) {
network = network || 'mainnet';
var transactionBuilder = new bitbox.TransactionBuilder(network)
let redeemScript;
let message = createMessage(reviewee, reviewer, transactionId, rating);
var signature = bitbox.BitcoinCash.signMessageWithPrivKey(wallet.keyPair.toWIF(), message.toString());
var contentBuffer = Buffer.concat([
rateOpreturnVersion,
message,
Buffer.from(signature, 'base64')
]);
var opReturn = bitbox.Script.encode([
bitbox.Script.opcodes.OP_RETURN,
contentBuffer
]);
console.log(opReturn);
let utxo = await getAnUtxo(reviewer);
var byteCount = bitbox.BitcoinCash.getByteCount({ P2PKH: 1 }, { P2PKH: 1 });
byteCount += contentBuffer.length;
byteCount += 9 // some spare bytes for OP_RETURN, version etc.
var originalAmount = utxo.satoshis;
transactionBuilder.addInput(utxo.txid, utxo.vout);
transactionBuilder.addOutput(reviewer, originalAmount - byteCount);
transactionBuilder.addOutput(opReturn, 0);
transactionBuilder.setLockTime(0);
transactionBuilder.sign(0, wallet.keyPair, redeemScript, transactionBuilder.hashTypes.SIGHASH_ALL, originalAmount, transactionBuilder.signatureAlgorithms.SCHNORR)
var tx = transactionBuilder.build();
var hex = tx.toHex();
console.log("Sending tx from "+ utxo.txid +" with op_return "+ rateOpreturnVersion + message + signature + " contentBuffer length "+ contentBuffer.length)
return await bitbox.RawTransactions.sendRawTransaction(hex);
}
export async function checkRatedTransactionConditions(details) {
var ratedTransaction = await bitbox.Transaction.details(details.ratedTransaction);
console.log(ratedTransaction);
if (ratedTransaction.vin[0].cashAddress === details.reviewer) {
console.log("from matched with reviewer")
return ratedTransaction.vout.some(out => {
return out.scriptPubKey.cashAddrs.some(addr => addr === details.reviewee)
});
} else if (ratedTransaction.vin[0].cashAddress === details.reviewee) {
console.log("from matched with reviewee")
return ratedTransaction.vout.some(out => {
return out.scriptPubKey.cashAddrs.some(addr => addr === details.reviewer)
});
} else {
console.log("from matched none")
return false; //
}
}
export async function getRatingDetails(transactionId) {
var transaction = await bitbox.Transaction.details(transactionId);
var opReturnOutput = transaction.vout.find(function(out) {return out.scriptPubKey.addresses === undefined;});
var message = opReturnOutput.scriptPubKey.asm;
let details = {};
details.version = message.substring(10, 18);
details.versionValid= details.version === '0abcde1e';
details.reviewee160 = message.substring(18, 58);
details.reviewee = bitbox.Address.hash160ToCash(details.reviewee160);
details.reviewer160 = message.substring(58, 98);
details.reviewer = bitbox.Address.hash160ToCash(details.reviewer160);
details.ratedTransaction = message.substring(98, 162);
details.ratingHex = message.substring(162, 164);
details.rating = details.ratingHex.toString('16')
details.signature = Buffer.from(message.substring(164), 'hex').toString('base64');
console.log(details);
var message = createMessage(details.reviewee, details.reviewer, details.ratedTransaction, details.rating);
details.signatureValid = bitbox.BitcoinCash.verifyMessage(
details.reviewer,
details.signature,
message.toString()
);
details.ratedTransactionValid = await checkRatedTransactionConditions(details)
return details;
}
export async function sendAllToCashAddress(wallet, toAddress, network) {
network = network || 'mainnet';
var transactionBuilder = new bitbox.TransactionBuilder(network)
var u = await bitbox.Address.utxo(bitbox.HDNode.toCashAddress(wallet));
var sendAmount = 0
var inputs = [];
// Loop through each UTXO assigned to this address.
for (let i = 0; i < u.utxos.length; i++) {
var thisUtxo = u.utxos[i]
inputs.push(thisUtxo)
sendAmount += thisUtxo.satoshis
transactionBuilder.addInput(thisUtxo.txid, thisUtxo.vout)
}
// get byte count to calculate fee. paying 1.2 sat/byte
var byteCount = bitbox.BitcoinCash.getByteCount(
{ P2PKH: inputs.length },
{ P2PKH: 1 }
)
if (sendAmount - byteCount < 0) {
console.log(
`Transaction fee costs more combined UTXOs. Can't send transaction.`
)
return
}
transactionBuilder.addOutput(toAddress, sendAmount - byteCount);
let redeemScript
inputs.forEach((input, index) => {
transactionBuilder.sign(
index,
wallet.keyPair,
redeemScript,
transactionBuilder.hashTypes.SIGHASH_ALL,
input.satoshis
)
});
var tx = transactionBuilder.build();
var hex = tx.toHex();
var txid = await bitbox.RawTransactions.sendRawTransaction([hex]);
return txid;
}
export async function getSimpleAverage(wallet, network) {
network = network || 'mainnet';
var cashAddress = bitbox.HDNode.toCashAddress(wallet);
var hash160 = bitbox.Address.cashToHash160(cashAddress);
var toencode = "0abcde1f" + hash160;
var prefix = "^"+ Buffer.from(toencode, 'hex').toString('base64');
var query = {
"v": 3,
"q": {
"find": {
"out.b1": {
"$regex": prefix
}
},
"limit": 999
},
"r": {
"f": "[ length as $array_length | reduce (.[] | .out[1] | .h1[152:154] | tonumber) as $item (0; . + $item) / $array_length | tostring ]"
}
};
return bitbox.BitDB.get(query);
}
export async function test() {
let wallet = createWallet('sponsor access milk want fossil govern plate head stuff session banner spice attract dentist dilemma public real common what jar mad world again online');
let wallet2 = createWallet('excite orbit grit offer soon license city hybrid bring illness forward there false victory access input limb cement creek pumpkin source kitchen butter sea');
var claimTx1 = await claimAddress(wallet)
var claimTx2 = await claimAddress(wallet2)
await getClaimDetails(claimTx1);
await getClaimDetails(claimTx2);
var tradeTx = await transact(wallet, wallet2, 546)
var rateTx = await rateTransaction(bitbox.HDNode.toCashAddress(wallet2),bitbox.HDNode.toCashAddress(wallet),tradeTx,99,wallet,'mainnet')
return await getRatingDetails(rateTx);
}