cipher-ethereum
Version:
An Ethereum library used by Cipher Browser, a mobile Ethereum client
81 lines (74 loc) • 3.27 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Transaction = undefined;
var _rlp = require('rlp');
var rlp = _interopRequireWildcard(_rlp);
var _util = require('./util');
var _elliptic = require('elliptic');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
var secp256k1 = new _elliptic.ec('secp256k1');
var Transaction = /** @class */function () {
function Transaction(params) {
this.nonce = (0, _util.numberToBuffer)(params.nonce);
this.gasPriceWei = (0, _util.bnToBuffer)(params.gasPriceWei);
this.gasLimit = (0, _util.bnToBuffer)(params.gasLimit);
this.toAddress = params.toAddress ? (0, _util.hexToBuffer)(params.toAddress) : Buffer.alloc(0);
this.valueWei = (0, _util.bnToBuffer)(params.valueWei);
this.data = params.data ? (0, _util.hexToBuffer)(params.data) : Buffer.alloc(0);
this.chainId = params.chainId ? params.chainId : undefined; // disallow 0
this.v = params.v || 28;
this.r = params.r ? (0, _util.hexToBuffer)(params.r) : Buffer.alloc(0);
this.s = params.s ? (0, _util.hexToBuffer)(params.s) : Buffer.alloc(0);
}
Object.defineProperty(Transaction.prototype, "fields", {
get: function () {
return [this.nonce, this.gasPriceWei, this.gasLimit, this.toAddress, this.valueWei, this.data, (0, _util.numberToBuffer)(this.v), this.r, this.s // 8: s
];
},
enumerable: false,
configurable: true
});
Object.defineProperty(Transaction.prototype, "rlp", {
get: function () {
return rlp.encode(this.fields);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Transaction.prototype, "hash", {
get: function () {
return (0, _util.keccak256)(this.rlp);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Transaction.prototype, "fieldsForSigning", {
get: function () {
var fields = this.fields.slice(0, 6);
return this.chainId ? fields.concat([
// EIP155
(0, _util.numberToBuffer)(this.chainId), (0, _util.numberToBuffer)(0), (0, _util.numberToBuffer)(0) // 8: s = 0
]) : fields;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Transaction.prototype, "hashForSigning", {
get: function () {
return (0, _util.keccak256)(rlp.encode(this.fieldsForSigning));
},
enumerable: false,
configurable: true
});
Transaction.prototype.sign = function (privateKey) {
var sig = secp256k1.keyFromPrivate(privateKey).sign(this.hashForSigning, { canonical: true });
this.v = (sig.recoveryParam || 0) + 27 + (this.chainId ? this.chainId * 2 + 8 : 0);
this.r = (0, _util.bnToBuffer)(sig.r);
this.s = (0, _util.bnToBuffer)(sig.s);
return this.rlp;
};
return Transaction;
}();
exports.Transaction = Transaction;