2018-06-17 23:32:57 +03:00
|
|
|
"use strict";
|
|
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
|
if (mod && mod.__esModule) return mod;
|
|
|
|
var result = {};
|
|
|
|
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
|
|
result["default"] = mod;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2018-09-24 23:07:14 +03:00
|
|
|
var constants_1 = require("../constants");
|
|
|
|
var errors = __importStar(require("../errors"));
|
2018-07-31 01:59:52 +03:00
|
|
|
var secp256k1_1 = require("./secp256k1");
|
2018-06-17 23:32:57 +03:00
|
|
|
var address_1 = require("./address");
|
|
|
|
var bignumber_1 = require("./bignumber");
|
2018-06-17 23:47:28 +03:00
|
|
|
var bytes_1 = require("./bytes");
|
2018-06-17 23:32:57 +03:00
|
|
|
var keccak256_1 = require("./keccak256");
|
2018-10-15 02:05:38 +03:00
|
|
|
var properties_1 = require("./properties");
|
2018-06-17 23:32:57 +03:00
|
|
|
var RLP = __importStar(require("./rlp"));
|
2018-10-15 02:05:38 +03:00
|
|
|
var abstract_provider_1 = require("../providers/abstract-provider");
|
2018-07-31 01:59:52 +03:00
|
|
|
///////////////////////////////
|
2018-06-17 23:32:57 +03:00
|
|
|
function handleAddress(value) {
|
|
|
|
if (value === '0x') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return address_1.getAddress(value);
|
|
|
|
}
|
|
|
|
function handleNumber(value) {
|
|
|
|
if (value === '0x') {
|
2018-08-03 04:35:39 +03:00
|
|
|
return constants_1.Zero;
|
2018-06-17 23:32:57 +03:00
|
|
|
}
|
|
|
|
return bignumber_1.bigNumberify(value);
|
|
|
|
}
|
|
|
|
var transactionFields = [
|
|
|
|
{ name: 'nonce', maxLength: 32 },
|
|
|
|
{ name: 'gasPrice', maxLength: 32 },
|
|
|
|
{ name: 'gasLimit', maxLength: 32 },
|
|
|
|
{ name: 'to', length: 20 },
|
|
|
|
{ name: 'value', maxLength: 32 },
|
|
|
|
{ name: 'data' },
|
|
|
|
];
|
2018-10-15 02:05:38 +03:00
|
|
|
var allowedTransactionKeys = {
|
|
|
|
chainId: true, data: true, gasLimit: true, gasPrice: true, nonce: true, to: true, value: true
|
|
|
|
};
|
2018-07-12 09:42:46 +03:00
|
|
|
function serialize(transaction, signature) {
|
2018-10-15 02:05:38 +03:00
|
|
|
properties_1.checkProperties(transaction, allowedTransactionKeys);
|
2018-06-17 23:32:57 +03:00
|
|
|
var raw = [];
|
|
|
|
transactionFields.forEach(function (fieldInfo) {
|
|
|
|
var value = transaction[fieldInfo.name] || ([]);
|
2018-06-17 23:47:28 +03:00
|
|
|
value = bytes_1.arrayify(bytes_1.hexlify(value));
|
2018-06-17 23:32:57 +03:00
|
|
|
// Fixed-width field
|
|
|
|
if (fieldInfo.length && value.length !== fieldInfo.length && value.length > 0) {
|
2018-06-26 04:02:20 +03:00
|
|
|
errors.throwError('invalid length for ' + fieldInfo.name, errors.INVALID_ARGUMENT, { arg: ('transaction' + fieldInfo.name), value: value });
|
2018-06-17 23:32:57 +03:00
|
|
|
}
|
|
|
|
// Variable-width (with a maximum)
|
|
|
|
if (fieldInfo.maxLength) {
|
2018-06-17 23:47:28 +03:00
|
|
|
value = bytes_1.stripZeros(value);
|
2018-06-17 23:32:57 +03:00
|
|
|
if (value.length > fieldInfo.maxLength) {
|
2018-06-26 04:02:20 +03:00
|
|
|
errors.throwError('invalid length for ' + fieldInfo.name, errors.INVALID_ARGUMENT, { arg: ('transaction' + fieldInfo.name), value: value });
|
2018-06-17 23:32:57 +03:00
|
|
|
}
|
|
|
|
}
|
2018-06-17 23:47:28 +03:00
|
|
|
raw.push(bytes_1.hexlify(value));
|
2018-06-17 23:32:57 +03:00
|
|
|
});
|
2018-07-12 09:42:46 +03:00
|
|
|
if (transaction.chainId != null && transaction.chainId !== 0) {
|
2018-06-17 23:47:28 +03:00
|
|
|
raw.push(bytes_1.hexlify(transaction.chainId));
|
2018-06-17 23:32:57 +03:00
|
|
|
raw.push('0x');
|
|
|
|
raw.push('0x');
|
|
|
|
}
|
2018-07-12 09:42:46 +03:00
|
|
|
var unsignedTransaction = RLP.encode(raw);
|
2018-06-26 04:02:20 +03:00
|
|
|
// Requesting an unsigned transation
|
2018-07-12 09:42:46 +03:00
|
|
|
if (!signature) {
|
|
|
|
return unsignedTransaction;
|
2018-06-26 04:02:20 +03:00
|
|
|
}
|
2018-07-12 09:42:46 +03:00
|
|
|
// The splitSignature will ensure the transaction has a recoveryParam in the
|
|
|
|
// case that the signTransaction function only adds a v.
|
2018-07-31 01:59:52 +03:00
|
|
|
var sig = bytes_1.splitSignature(signature);
|
2018-06-26 04:02:20 +03:00
|
|
|
// We pushed a chainId and null r, s on for hashing only; remove those
|
2018-07-31 01:59:52 +03:00
|
|
|
var v = 27 + sig.recoveryParam;
|
2018-06-26 04:02:20 +03:00
|
|
|
if (raw.length === 9) {
|
2018-06-17 23:32:57 +03:00
|
|
|
raw.pop();
|
|
|
|
raw.pop();
|
|
|
|
raw.pop();
|
|
|
|
v += transaction.chainId * 2 + 8;
|
|
|
|
}
|
2018-06-17 23:47:28 +03:00
|
|
|
raw.push(bytes_1.hexlify(v));
|
2018-07-31 01:59:52 +03:00
|
|
|
raw.push(bytes_1.stripZeros(bytes_1.arrayify(sig.r)));
|
|
|
|
raw.push(bytes_1.stripZeros(bytes_1.arrayify(sig.s)));
|
2018-06-17 23:32:57 +03:00
|
|
|
return RLP.encode(raw);
|
|
|
|
}
|
2018-06-26 04:02:20 +03:00
|
|
|
exports.serialize = serialize;
|
2018-06-17 23:32:57 +03:00
|
|
|
function parse(rawTransaction) {
|
2018-06-26 04:02:20 +03:00
|
|
|
var transaction = RLP.decode(rawTransaction);
|
|
|
|
if (transaction.length !== 9 && transaction.length !== 6) {
|
|
|
|
errors.throwError('invalid raw transaction', errors.INVALID_ARGUMENT, { arg: 'rawTransactin', value: rawTransaction });
|
2018-06-17 23:32:57 +03:00
|
|
|
}
|
|
|
|
var tx = {
|
2018-06-26 04:02:20 +03:00
|
|
|
nonce: handleNumber(transaction[0]).toNumber(),
|
|
|
|
gasPrice: handleNumber(transaction[1]),
|
|
|
|
gasLimit: handleNumber(transaction[2]),
|
|
|
|
to: handleAddress(transaction[3]),
|
|
|
|
value: handleNumber(transaction[4]),
|
|
|
|
data: transaction[5],
|
2018-06-17 23:32:57 +03:00
|
|
|
chainId: 0
|
|
|
|
};
|
2018-06-26 04:02:20 +03:00
|
|
|
// Legacy unsigned transaction
|
|
|
|
if (transaction.length === 6) {
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
tx.v = bignumber_1.bigNumberify(transaction[6]).toNumber();
|
|
|
|
}
|
|
|
|
catch (error) {
|
2018-12-27 23:53:00 +03:00
|
|
|
errors.info(error);
|
2018-06-26 04:02:20 +03:00
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
tx.r = bytes_1.hexZeroPad(transaction[7], 32);
|
|
|
|
tx.s = bytes_1.hexZeroPad(transaction[8], 32);
|
|
|
|
if (bignumber_1.bigNumberify(tx.r).isZero() && bignumber_1.bigNumberify(tx.s).isZero()) {
|
|
|
|
// EIP-155 unsigned transaction
|
|
|
|
tx.chainId = tx.v;
|
|
|
|
tx.v = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Signed Tranasaction
|
|
|
|
tx.chainId = Math.floor((tx.v - 35) / 2);
|
|
|
|
if (tx.chainId < 0) {
|
|
|
|
tx.chainId = 0;
|
2018-06-17 23:32:57 +03:00
|
|
|
}
|
|
|
|
var recoveryParam = tx.v - 27;
|
2018-06-26 04:02:20 +03:00
|
|
|
var raw = transaction.slice(0, 6);
|
|
|
|
if (tx.chainId !== 0) {
|
|
|
|
raw.push(bytes_1.hexlify(tx.chainId));
|
2018-06-17 23:32:57 +03:00
|
|
|
raw.push('0x');
|
|
|
|
raw.push('0x');
|
2018-06-26 04:02:20 +03:00
|
|
|
recoveryParam -= tx.chainId * 2 + 8;
|
2018-06-17 23:32:57 +03:00
|
|
|
}
|
|
|
|
var digest = keccak256_1.keccak256(RLP.encode(raw));
|
|
|
|
try {
|
2018-06-26 04:02:20 +03:00
|
|
|
tx.from = secp256k1_1.recoverAddress(digest, { r: bytes_1.hexlify(tx.r), s: bytes_1.hexlify(tx.s), recoveryParam: recoveryParam });
|
2018-06-17 23:32:57 +03:00
|
|
|
}
|
|
|
|
catch (error) {
|
2018-12-27 23:53:00 +03:00
|
|
|
errors.info(error);
|
2018-06-17 23:32:57 +03:00
|
|
|
}
|
|
|
|
tx.hash = keccak256_1.keccak256(rawTransaction);
|
|
|
|
}
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
exports.parse = parse;
|
2018-10-15 02:05:38 +03:00
|
|
|
function populateTransaction(transaction, provider, from) {
|
|
|
|
if (!abstract_provider_1.Provider.isProvider(provider)) {
|
|
|
|
errors.throwError('missing provider', errors.INVALID_ARGUMENT, {
|
|
|
|
argument: 'provider',
|
|
|
|
value: provider
|
|
|
|
});
|
|
|
|
}
|
|
|
|
properties_1.checkProperties(transaction, allowedTransactionKeys);
|
|
|
|
var tx = properties_1.shallowCopy(transaction);
|
|
|
|
if (tx.to != null) {
|
|
|
|
tx.to = provider.resolveName(tx.to);
|
|
|
|
}
|
|
|
|
if (tx.gasPrice == null) {
|
|
|
|
tx.gasPrice = provider.getGasPrice();
|
|
|
|
}
|
|
|
|
if (tx.nonce == null) {
|
|
|
|
tx.nonce = provider.getTransactionCount(from);
|
|
|
|
}
|
|
|
|
if (tx.gasLimit == null) {
|
|
|
|
var estimate = properties_1.shallowCopy(tx);
|
|
|
|
estimate.from = from;
|
|
|
|
tx.gasLimit = provider.estimateGas(estimate);
|
|
|
|
}
|
|
|
|
if (tx.chainId == null) {
|
|
|
|
tx.chainId = provider.getNetwork().then(function (network) { return network.chainId; });
|
|
|
|
}
|
|
|
|
return properties_1.resolveProperties(tx);
|
|
|
|
}
|
|
|
|
exports.populateTransaction = populateTransaction;
|