forked from ethereumjs/ethereumjs-tx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
184 lines (167 loc) · 3.88 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
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
const BN = require('bn.js');
const rlp = require('rlp');
const utils = require('ethereumjs-util');
const fees = require('ethereum-common').fees;
const ecdsaOps = require('./ecdsaOps.js');
//give browser access to Buffers
// global.Buffer = Buffer;
/**
* Represents a transaction
* @constructor
* @param {Buffer|Array} data raw data, deserialized
*/
var Transaction = module.exports = function(data) {
var self = this;
//Define Properties
var fields = [{
name: 'nonce',
word: true,
noZero: true,
default: new Buffer([])
}, {
name: 'gasPrice',
word: true,
default: new Buffer([])
}, {
name: 'gasLimit',
word: true,
default: new Buffer([])
}, {
name: 'to',
empty: true,
length: 20,
default: new Buffer([])
}, {
name: 'value',
empty: true,
word: true,
noZero: true,
default: new Buffer([])
}, {
name: 'data',
empty: true,
default: new Buffer([0])
}, {
name: 'v',
length: 1,
default: new Buffer([0x1c])
}, {
name: 'r',
pad: true,
length: 32,
default: utils.zeros(32)
}, {
name: 's',
pad: true,
length: 32,
default: utils.zeros(32)
}];
Object.defineProperty(this, 'from', {
enumerable: false,
configurable: true,
get: function() {
if(this._from) return this._from;
return this._from = this.getSenderAddress();
},
set: function(v) {
this._from = v;
}
});
utils.defineProperties(this, fields, data);
};
/**
* Returns the rlp encoding of the transaction
* @method serialize
* @return {Buffer}
*/
Transaction.prototype.serialize = function() {
return rlp.encode(this.raw);
};
/**
* Computes a sha3-256 hash of the tx
* @method hash
* @param {Boolean} [true] signature - whether or not to inculde the signature
* @return {Buffer}
*/
Transaction.prototype.hash = function(signature) {
var toHash;
if (typeof signature === 'undefined') {
signature = true;
}
if (signature) {
toHash = this.raw;
} else {
toHash = this.raw.slice(0, 6);
}
//create hash
return utils.sha3(rlp.encode(toHash));
};
/**
* gets the senders address
* @method getSenderAddress
* @return {Buffer}
*/
Transaction.prototype.getSenderAddress = function() {
var pubKey = this.getSenderPublicKey();
return utils.pubToAddress(pubKey);
};
/**
* gets the senders public key
* @method getSenderPublicKey
* @return {Buffer}
*/
Transaction.prototype.getSenderPublicKey = ecdsaOps.txGetSenderPublicKey;
/**
* @method verifySignature
* @return {Boolean}
*/
Transaction.prototype.verifySignature = ecdsaOps.txVerifySignature;
/**
* sign a transaction with a given a private key
* @method sign
* @param {Buffer} privateKey
*/
Transaction.prototype.sign = ecdsaOps.txSign;
/**
* The amount of gas paid for the data in this tx
* @method getDataFee
* @return {bn.js}
*/
Transaction.prototype.getDataFee = function() {
var data = this.raw[5];
var cost = new BN(0);
for (var i = 0; i < data.length; i++) {
if (data[i] === 0) {
cost = cost.add(new BN(fees.txDataZeroGas.v));
} else {
cost = cost.add(new BN(fees.txDataNonZeroGas.v));
}
}
return cost;
};
/**
* the base amount of gas it takes to be a valid tx
* @method getBaseFee
* @return {bn.js}
*/
Transaction.prototype.getBaseFee = function() {
return this.getDataFee().add(new BN(fees.txGas.v));
};
/**
* the up front amount that an account must have for this transaction to be valid
* @method getUpfrontCost
* @return {BN}
*/
Transaction.prototype.getUpfrontCost = function() {
return new BN(this.gasLimit)
.mul(new BN(this.gasPrice))
.add(new BN(this.value));
};
/**
* validates the signature and checks to see if it has enough gas
* @method validate
* @return {Boolean}
*/
Transaction.prototype.validate = function() {
return this.verifySignature() && (Number(this.getBaseFee().toString()) <= utils.bufferToInt(this.gasLimit));
};