ethers.js/packages/transactions/src.ts/index.ts

230 lines
6.9 KiB
TypeScript
Raw Permalink Normal View History

2019-05-14 18:25:46 -04:00
"use strict";
import { getAddress } from "@ethersproject/address";
import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import { arrayify, BytesLike, DataOptions, hexDataSlice, hexlify, hexZeroPad, isBytesLike, SignatureLike, splitSignature, stripZeros, } from "@ethersproject/bytes";
2019-05-14 18:25:46 -04:00
import { Zero } from "@ethersproject/constants";
import { keccak256 } from "@ethersproject/keccak256";
import { checkProperties } from "@ethersproject/properties";
import * as RLP from "@ethersproject/rlp";
import { computePublicKey, recoverPublicKey } from "@ethersproject/signing-key";
2019-08-01 18:04:06 -04:00
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
2019-05-14 18:25:46 -04:00
///////////////////////////////
// Exported Types
export type UnsignedTransaction = {
to?: string;
nonce?: number;
gasLimit?: BigNumberish;
gasPrice?: BigNumberish;
data?: BytesLike;
value?: BigNumberish;
chainId?: number;
}
export interface Transaction {
hash?: string;
to?: string;
from?: string;
nonce: number;
gasLimit: BigNumber;
gasPrice: BigNumber;
data: string;
value: BigNumber;
chainId: number;
r?: string;
s?: string;
v?: number;
}
///////////////////////////////
function handleAddress(value: string): string {
if (value === "0x") { return null; }
return getAddress(value);
}
function handleNumber(value: string): BigNumber {
if (value === "0x") { return Zero; }
return BigNumber.from(value);
}
const transactionFields = [
{ name: "nonce", maxLength: 32, numeric: true },
{ name: "gasPrice", maxLength: 32, numeric: true },
{ name: "gasLimit", maxLength: 32, numeric: true },
2019-05-14 18:25:46 -04:00
{ name: "to", length: 20 },
{ name: "value", maxLength: 32, numeric: true },
2019-05-14 18:25:46 -04:00
{ name: "data" },
];
const allowedTransactionKeys: { [ key: string ]: boolean } = {
chainId: true, data: true, gasLimit: true, gasPrice:true, nonce: true, to: true, value: true
}
export function computeAddress(key: BytesLike | string): string {
const publicKey = computePublicKey(key);
2019-05-14 18:25:46 -04:00
return getAddress(hexDataSlice(keccak256(hexDataSlice(publicKey, 1)), 12));
}
export function recoverAddress(digest: BytesLike, signature: SignatureLike): string {
return computeAddress(recoverPublicKey(arrayify(digest), signature));
}
export function serialize(transaction: UnsignedTransaction, signature?: SignatureLike): string {
checkProperties(transaction, allowedTransactionKeys);
const raw: Array<string | Uint8Array> = [];
2019-05-14 18:25:46 -04:00
transactionFields.forEach(function(fieldInfo) {
let value = (<any>transaction)[fieldInfo.name] || ([]);
const options: DataOptions = { };
if (fieldInfo.numeric) { options.hexPad = "left"; }
value = arrayify(hexlify(value, options));
2019-05-14 18:25:46 -04:00
// Fixed-width field
if (fieldInfo.length && value.length !== fieldInfo.length && value.length > 0) {
2019-08-01 18:04:06 -04:00
logger.throwArgumentError("invalid length for " + fieldInfo.name, ("transaction:" + fieldInfo.name), value);
2019-05-14 18:25:46 -04:00
}
// Variable-width (with a maximum)
if (fieldInfo.maxLength) {
value = stripZeros(value);
if (value.length > fieldInfo.maxLength) {
2019-08-01 18:04:06 -04:00
logger.throwArgumentError("invalid length for " + fieldInfo.name, ("transaction:" + fieldInfo.name), value );
2019-05-14 18:25:46 -04:00
}
}
raw.push(hexlify(value));
});
let chainId = 0;
if (transaction.chainId != null) {
// A chainId was provided; if non-zero we'll use EIP-155
chainId = transaction.chainId;
if (typeof(chainId) !== "number") {
logger.throwArgumentError("invalid transaction.chainId", "transaction", transaction);
}
} else if (signature && !isBytesLike(signature) && signature.v > 28) {
// No chainId provided, but the signature is signing with EIP-155; derive chainId
chainId = Math.floor((signature.v - 35) / 2);
}
// We have an EIP-155 transaction (chainId was specified and non-zero)
if (chainId !== 0) {
raw.push(hexlify(chainId));
2019-05-14 18:25:46 -04:00
raw.push("0x");
raw.push("0x");
}
// Requesting an unsigned transation
if (!signature) {
return RLP.encode(raw);
2019-05-14 18:25:46 -04:00
}
// The splitSignature will ensure the transaction has a recoveryParam in the
// case that the signTransaction function only adds a v.
const sig = splitSignature(signature);
2019-05-14 18:25:46 -04:00
// We pushed a chainId and null r, s on for hashing only; remove those
let v = 27 + sig.recoveryParam
if (chainId !== 0) {
2019-05-14 18:25:46 -04:00
raw.pop();
raw.pop();
raw.pop();
v += chainId * 2 + 8;
// If an EIP-155 v (directly or indirectly; maybe _vs) was provided, check it!
if (sig.v > 28 && sig.v !== v) {
logger.throwArgumentError("transaction.chainId/signature.v mismatch", "signature", signature);
}
} else if (sig.v !== v) {
logger.throwArgumentError("transaction.chainId/signature.v mismatch", "signature", signature);
2019-05-14 18:25:46 -04:00
}
raw.push(hexlify(v));
raw.push(stripZeros(arrayify(sig.r)));
raw.push(stripZeros(arrayify(sig.s)));
return RLP.encode(raw);
}
export function parse(rawTransaction: BytesLike): Transaction {
const transaction = RLP.decode(rawTransaction);
2019-05-14 18:25:46 -04:00
if (transaction.length !== 9 && transaction.length !== 6) {
logger.throwArgumentError("invalid raw transaction", "rawTransaction", rawTransaction);
2019-05-14 18:25:46 -04:00
}
const tx: Transaction = {
2019-05-14 18:25:46 -04: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],
chainId: 0
};
// Legacy unsigned transaction
if (transaction.length === 6) { return tx; }
try {
tx.v = BigNumber.from(transaction[6]).toNumber();
} catch (error) {
console.log(error);
return tx;
}
tx.r = hexZeroPad(transaction[7], 32);
tx.s = hexZeroPad(transaction[8], 32);
if (BigNumber.from(tx.r).isZero() && BigNumber.from(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; }
let recoveryParam = tx.v - 27;
const raw = transaction.slice(0, 6);
2019-05-14 18:25:46 -04:00
if (tx.chainId !== 0) {
raw.push(hexlify(tx.chainId));
raw.push("0x");
raw.push("0x");
recoveryParam -= tx.chainId * 2 + 8;
}
const digest = keccak256(RLP.encode(raw));
2019-05-14 18:25:46 -04:00
try {
tx.from = recoverAddress(digest, { r: hexlify(tx.r), s: hexlify(tx.s), recoveryParam: recoveryParam });
} catch (error) {
console.log(error);
}
tx.hash = keccak256(rawTransaction);
}
return tx;
}