-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtransaction.js
68 lines (56 loc) · 2.43 KB
/
transaction.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
var bitcoinjs = require('bitcoinjs-lib');
const fs = require('fs');
var mime = require('mime-types')
function Transaction(hash, transactionRaw) {
this.isGenesis = false;
this.hash = hash;
this.decodedTransaction = bitcoinjs.Transaction.fromHex(Buffer.from(transactionRaw, 'hex'));
let rawWitness = this.decodedTransaction.ins[0].witness[1]
this.type = null;
try {
if (rawWitness[35] == 99 && rawWitness[37] == 111 && rawWitness[38] == 114 && rawWitness[39] == 100 && rawWitness[40] == 1 && rawWitness[41] == 1) {
let length = rawWitness[42]
this.contentType = ""
for (let index = 0; index < length; index++) {
this.contentType += String.fromCharCode(rawWitness[43 + index])
}
this.contentStart = 44 + length;
this.isGenesis = true;
}
} catch (error) {
}
}
Transaction.prototype._getAllContent = function (existing, start_index) {
let rawWitness = this.decodedTransaction.ins[0].witness[1]
let op_1 = rawWitness[start_index];
if (op_1 > 0 && op_1 < 76) { // The next opcode bytes is data to be pushed onto the stack
existing = Buffer.concat([existing, rawWitness.slice(start_index + 1, rawWitness.length - 1)]);
} else if (op_1 == 76) {
existing = Buffer.concat([existing, rawWitness.slice(start_index + 2, rawWitness.length - 2)]);
} else if (op_1 == 77) { // The next byte contains the number of bytes to be pushed onto the stack.
let nb = rawWitness[start_index + 1] + (rawWitness[start_index + 2] * 255)
let new_index = start_index + 5 + (nb);
existing = Buffer.concat([existing, rawWitness.slice(start_index + 3, new_index)]);
return (this._getAllContent(existing, new_index))
}
return existing;
}
Transaction.prototype.isOrdinalGenesis = function () {
return this.isGenesis;
}
Transaction.prototype.getContentType = function () {
return this.contentType;
}
Transaction.prototype.getContentLength = function () {
return this.contentLength;
}
Transaction.prototype.getContentData = function () {
return this._getAllContent(Buffer.alloc(0), this.contentStart);
}
Transaction.prototype.getHash = function () {
return this.hash;
}
Transaction.prototype.contentToFile = function () {
fs.writeFileSync('./tests/' + this.getHash() + "." + mime.extension(this.contentType), this.getContentData(), { flag: 'w' });
}
module.exports = Transaction;