-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (83 loc) · 2.51 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
'use strict'
var ravencoin = require('ravencoinjs-lib');
var decodeFormat = function(tx){
var result = {
txid: tx.getId(),
version: tx.version,
locktime: tx.locktime,
size: tx.byteLength(),
vsize: tx.virtualSize(),
};
return result;
}
var decodeInput = function(tx){
var result = tx.ins.map(function(input, n){
var vin = {
txid: input.hash.reverse().toString('hex'),
n : input.index,
script: ravencoin.script.toASM(input.script),
sequence: input.sequence,
}
return vin
})
return result
}
var decodeOutput = function(tx, network){
var format = function(out, n, network){
var vout = {
satoshi: out.value,
value: (1e-8 * out.value).toFixed(8),
n: n,
scriptPubKey: {
asm: ravencoin.script.toASM(out.script),
hex: out.script.toString('hex'),
type: ravencoin.script.classifyOutput(out.script),
addresses: [],
},
};
switch(vout.scriptPubKey.type){
case 'pubkeyhash':
case 'scripthash':
vout.scriptPubKey.addresses.push(ravencoin.address.fromOutputScript(out.script, network));
break;
case 'witnesspubkeyhash': // NATIVE SEGWIT
break;
case 'pubkey': // There really is no address
var pubKeyBuffer = ravencoin.script.pubKey.output.decode(out.script);
vout.scriptPubKey.addresses.push(ravencoin.ECPair.fromPublicKeyBuffer(pubKeyBuffer, network).getAddress());
break;
case 'nulldata': // OP_RETURN
break;
default :
break;
}
return vout
}
var result = tx.outs.map(function(out, n){
return format(out, n, network);
})
return result
}
var TxDecoder = module.exports = function(rawtx, network){
this.network = network;
this.tx = ravencoin.Transaction.fromHex(rawtx);
this.format = decodeFormat(this.tx);
this.inputs = decodeInput(this.tx);
this.outputs = decodeOutput(this.tx, this.network);
}
TxDecoder.prototype.toObject = function(){
return {
format : this.format,
inputs : this.inputs,
outputs : this.outputs,
}
}
TxDecoder.prototype.decode = function(){
var result = {}
var self = this;
Object.keys(self.format).forEach(function(key){
result[key] = self.format[key]
})
result.outputs = self.outputs
return result;
}