Added support for unsigned transactions.

This commit is contained in:
Richard Moore 2018-06-25 21:02:20 -04:00
parent 40559b7044
commit 8fe19de3ce
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
16 changed files with 281 additions and 266 deletions

9
dist/ethers.d.ts vendored

@ -110,10 +110,10 @@ declare module 'ethers/utils' {
import { toUtf8Bytes, toUtf8String } from 'ethers/utils/utf8';
import { formatEther, parseEther, formatUnits, parseUnits } from 'ethers/utils/units';
import { fetchJson } from 'ethers/utils/web';
import { parse as parseTransaction } from 'ethers/utils/transaction';
import { parse as parseTransaction, serialize as serializeTransaction } from 'ethers/utils/transaction';
import * as errors from 'ethers/utils/errors';
const etherSymbol = "\u039E";
export { AbiCoder, defaultAbiCoder, formatSignature, formatParamType, parseSignature, parseParamType, RLP, fetchJson, defineReadOnly, defineFrozen, resolveProperties, shallowCopy, etherSymbol, arrayify, concat, padZeros, stripZeros, base64, bigNumberify, BigNumber, hexlify, toUtf8Bytes, toUtf8String, hashMessage, namehash, id, getAddress, getIcapAddress, getContractAddress, formatEther, parseEther, formatUnits, parseUnits, keccak256, sha256, randomBytes, solidityPack, solidityKeccak256, soliditySha256, splitSignature, joinSignature, parseTransaction, errors };
export { AbiCoder, defaultAbiCoder, formatSignature, formatParamType, parseSignature, parseParamType, RLP, fetchJson, defineReadOnly, defineFrozen, resolveProperties, shallowCopy, etherSymbol, arrayify, concat, padZeros, stripZeros, base64, bigNumberify, BigNumber, hexlify, toUtf8Bytes, toUtf8String, hashMessage, namehash, id, getAddress, getIcapAddress, getContractAddress, formatEther, parseEther, formatUnits, parseUnits, keccak256, sha256, randomBytes, solidityPack, solidityKeccak256, soliditySha256, splitSignature, joinSignature, parseTransaction, serializeTransaction, errors };
const _default: {
AbiCoder: typeof AbiCoder;
defaultAbiCoder: AbiCoder;
@ -155,6 +155,7 @@ declare module 'ethers/utils' {
splitSignature: typeof splitSignature;
joinSignature: typeof joinSignature;
parseTransaction: typeof parseTransaction;
serializeTransaction: typeof serializeTransaction;
errors: typeof errors;
};
export default _default;
@ -738,8 +739,8 @@ declare module 'ethers/utils/transaction' {
s?: string;
v?: number;
}
export type SignDigestFunc = (digest: Arrayish) => Signature;
export function sign(transaction: UnsignedTransaction, signDigest: SignDigestFunc): string;
export type SignDigestFunc = (digest: Uint8Array) => Signature;
export function serialize(transaction: UnsignedTransaction, signDigest?: SignDigestFunc): string;
export function parse(rawTransaction: Arrayish): Transaction;
}

113
dist/ethers.js vendored

@ -10951,7 +10951,7 @@ var ProviderSigner = /** @class */ (function (_super) {
return this._addressPromise;
};
ProviderSigner.prototype.signMessage = function (message) {
return Promise.resolve(bytes_1.joinSignature(this.signDigest(hash_1.hashMessage(message))));
return Promise.resolve(bytes_1.joinSignature(this.signDigest(bytes_1.arrayify(hash_1.hashMessage(message)))));
};
ProviderSigner.prototype.sendTransaction = function (transaction) {
var _this = this;
@ -10971,7 +10971,7 @@ var ProviderSigner = /** @class */ (function (_super) {
transaction.gasPrice = this.provider.getGasPrice();
}
return properties_1.resolveProperties(transaction).then(function (tx) {
var signedTx = transaction_1.sign(tx, _this.signDigest);
var signedTx = transaction_1.serialize(tx, _this.signDigest);
return _this._addressPromise.then(function (address) {
if (transaction_1.parse(signedTx).from !== address) {
errors.throwError('signing address does not match expected address', errors.UNKNOWN_ERROR, { address: transaction_1.parse(signedTx).from, expectedAddress: address, signedTransaction: signedTx });
@ -13361,6 +13361,7 @@ var web_1 = require("./web");
exports.fetchJson = web_1.fetchJson;
var transaction_1 = require("./transaction");
exports.parseTransaction = transaction_1.parse;
exports.serializeTransaction = transaction_1.serialize;
var errors = __importStar(require("./errors"));
exports.errors = errors;
// NFKD (decomposed)
@ -13409,6 +13410,7 @@ exports.default = {
splitSignature: bytes_1.splitSignature,
joinSignature: bytes_1.joinSignature,
parseTransaction: transaction_1.parse,
serializeTransaction: transaction_1.serialize,
errors: errors
};
@ -13800,6 +13802,7 @@ var bytes_1 = require("./bytes");
var keccak256_1 = require("./keccak256");
var secp256k1_1 = require("./secp256k1");
var RLP = __importStar(require("./rlp"));
var errors = __importStar(require("./errors"));
function handleAddress(value) {
if (value === '0x') {
return null;
@ -13820,53 +13823,38 @@ var transactionFields = [
{ name: 'value', maxLength: 32 },
{ name: 'data' },
];
function sign(transaction, signDigest) {
function serialize(transaction, signDigest) {
var raw = [];
transactionFields.forEach(function (fieldInfo) {
var value = transaction[fieldInfo.name] || ([]);
value = bytes_1.arrayify(bytes_1.hexlify(value));
// Fixed-width field
if (fieldInfo.length && value.length !== fieldInfo.length && value.length > 0) {
var error = new Error('invalid ' + fieldInfo.name);
error.reason = 'wrong length';
error.value = value;
throw error;
errors.throwError('invalid length for ' + fieldInfo.name, errors.INVALID_ARGUMENT, { arg: ('transaction' + fieldInfo.name), value: value });
}
// Variable-width (with a maximum)
if (fieldInfo.maxLength) {
value = bytes_1.stripZeros(value);
if (value.length > fieldInfo.maxLength) {
var error = new Error('invalid ' + fieldInfo.name);
error.reason = 'too long';
error.value = value;
throw error;
errors.throwError('invalid length for ' + fieldInfo.name, errors.INVALID_ARGUMENT, { arg: ('transaction' + fieldInfo.name), value: value });
}
}
raw.push(bytes_1.hexlify(value));
});
// @TOOD:
/*
// Requesting an unsigned transation
if (!signDigest) {
let v = 27 + signature.recoveryParam
if (transaction.chainId) {
v += transaction.chainId * 2 + 8;
}
//raw.push(hexlify(transaction.chainId));
raw.push(hexlify(v));
raw.push('0x');
raw.push('0x');
}
*/
if (transaction.chainId) {
if (transaction.chainId && transaction.chainId !== 0) {
raw.push(bytes_1.hexlify(transaction.chainId));
raw.push('0x');
raw.push('0x');
}
// Requesting an unsigned transation
if (!signDigest) {
return RLP.encode(raw);
}
var digest = keccak256_1.keccak256(RLP.encode(raw));
var signature = signDigest(digest);
var signature = signDigest(bytes_1.arrayify(digest));
// We pushed a chainId and null r, s on for hashing only; remove those
var v = 27 + signature.recoveryParam;
if (transaction.chainId) {
if (raw.length === 9) {
raw.pop();
raw.pop();
raw.pop();
@ -13877,45 +13865,56 @@ function sign(transaction, signDigest) {
raw.push(bytes_1.stripZeros(bytes_1.arrayify(signature.s)));
return RLP.encode(raw);
}
exports.sign = sign;
exports.serialize = serialize;
function parse(rawTransaction) {
var signedTransaction = RLP.decode(rawTransaction);
if (signedTransaction.length !== 9) {
throw new Error('invalid transaction');
var transaction = RLP.decode(rawTransaction);
if (transaction.length !== 9 && transaction.length !== 6) {
errors.throwError('invalid raw transaction', errors.INVALID_ARGUMENT, { arg: 'rawTransactin', value: rawTransaction });
}
var tx = {
nonce: handleNumber(signedTransaction[0]).toNumber(),
gasPrice: handleNumber(signedTransaction[1]),
gasLimit: handleNumber(signedTransaction[2]),
to: handleAddress(signedTransaction[3]),
value: handleNumber(signedTransaction[4]),
data: signedTransaction[5],
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
};
var v = bytes_1.arrayify(signedTransaction[6]);
var r = bytes_1.arrayify(signedTransaction[7]);
var s = bytes_1.arrayify(signedTransaction[8]);
if (v.length >= 1 && r.length >= 1 && r.length <= 32 && s.length >= 1 && s.length <= 32) {
tx.v = bignumber_1.bigNumberify(v).toNumber();
tx.r = bytes_1.hexZeroPad(signedTransaction[7], 32);
tx.s = bytes_1.hexZeroPad(signedTransaction[8], 32);
var chainId = (tx.v - 35) / 2;
if (chainId < 0) {
chainId = 0;
// Legacy unsigned transaction
if (transaction.length === 6) {
return tx;
}
try {
tx.v = bignumber_1.bigNumberify(transaction[6]).toNumber();
}
catch (error) {
console.log(error);
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;
}
chainId = Math.floor(chainId);
tx.chainId = chainId;
var recoveryParam = tx.v - 27;
var raw = signedTransaction.slice(0, 6);
if (chainId) {
raw.push(bytes_1.hexlify(chainId));
var raw = transaction.slice(0, 6);
if (tx.chainId !== 0) {
raw.push(bytes_1.hexlify(tx.chainId));
raw.push('0x');
raw.push('0x');
recoveryParam -= chainId * 2 + 8;
recoveryParam -= tx.chainId * 2 + 8;
}
var digest = keccak256_1.keccak256(RLP.encode(raw));
try {
tx.from = secp256k1_1.recoverAddress(digest, { r: bytes_1.hexlify(r), s: bytes_1.hexlify(s), recoveryParam: recoveryParam });
tx.from = secp256k1_1.recoverAddress(digest, { r: bytes_1.hexlify(tx.r), s: bytes_1.hexlify(tx.s), recoveryParam: recoveryParam });
}
catch (error) {
console.log(error);
@ -13926,7 +13925,7 @@ function parse(rawTransaction) {
}
exports.parse = parse;
},{"./address":59,"./bignumber":60,"./bytes":61,"./keccak256":65,"./rlp":67,"./secp256k1":68}],72:[function(require,module,exports){
},{"./address":59,"./bignumber":60,"./bytes":61,"./errors":62,"./keccak256":65,"./rlp":67,"./secp256k1":68}],72:[function(require,module,exports){
'use strict';
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
@ -15126,7 +15125,7 @@ var Wallet = /** @class */ (function (_super) {
Wallet.prototype.sign = function (transaction) {
var _this = this;
return properties_1.resolveProperties(transaction).then(function (tx) {
return transaction_1.sign(tx, _this.signingKey.signDigest.bind(_this.signingKey));
return transaction_1.serialize(tx, _this.signingKey.signDigest.bind(_this.signingKey));
});
};
Wallet.prototype.signMessage = function (message) {

2
dist/ethers.min.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -32,6 +32,8 @@
"browserify": "^16.2.2",
"browserify-zlib": "^0.2.0",
"dts-bundle": "^0.7.3",
"ethereumjs-tx": "^1.3.5",
"ethereumjs-util": "^5.2.0",
"gulp": "^3.9.1",
"gulp-cli": "^2.0.1",
"gulp-sourcemaps": "^2.6.4",

@ -493,7 +493,7 @@ var ProviderSigner = /** @class */ (function (_super) {
return this._addressPromise;
};
ProviderSigner.prototype.signMessage = function (message) {
return Promise.resolve(bytes_1.joinSignature(this.signDigest(hash_1.hashMessage(message))));
return Promise.resolve(bytes_1.joinSignature(this.signDigest(bytes_1.arrayify(hash_1.hashMessage(message)))));
};
ProviderSigner.prototype.sendTransaction = function (transaction) {
var _this = this;
@ -513,7 +513,7 @@ var ProviderSigner = /** @class */ (function (_super) {
transaction.gasPrice = this.provider.getGasPrice();
}
return properties_1.resolveProperties(transaction).then(function (tx) {
var signedTx = transaction_1.sign(tx, _this.signDigest);
var signedTx = transaction_1.serialize(tx, _this.signDigest);
return _this._addressPromise.then(function (address) {
if (transaction_1.parse(signedTx).from !== address) {
errors.throwError('signing address does not match expected address', errors.UNKNOWN_ERROR, { address: transaction_1.parse(signedTx).from, expectedAddress: address, signedTransaction: signedTx });

@ -6,13 +6,13 @@ import { Signer } from '../wallet/wallet';
import { getAddress, getContractAddress } from '../utils/address';
import { BigNumber, bigNumberify, BigNumberish } from '../utils/bignumber';
import { Arrayish, hexDataLength, hexDataSlice, hexlify, hexStripZeros, isHexString, joinSignature, stripZeros } from '../utils/bytes';
import { arrayify, Arrayish, hexDataLength, hexDataSlice, hexlify, hexStripZeros, isHexString, joinSignature, stripZeros } from '../utils/bytes';
import { toUtf8String } from '../utils/utf8';
import { decode as rlpDecode, encode as rlpEncode } from '../utils/rlp';
import { hashMessage, namehash } from '../utils/hash';
import { getNetwork, Network, Networkish } from './networks';
import { defineReadOnly, resolveProperties, shallowCopy } from '../utils/properties';
import { parse as parseTransaction, sign as signTransaction, SignDigestFunc, Transaction } from '../utils/transaction';
import { parse as parseTransaction, serialize as serializeTransaction, SignDigestFunc, Transaction } from '../utils/transaction';
import * as errors from '../utils/errors';
@ -634,7 +634,7 @@ export class ProviderSigner extends Signer {
}
signMessage(message: Arrayish | string): Promise<string> {
return Promise.resolve(joinSignature(this.signDigest(hashMessage(message))));
return Promise.resolve(joinSignature(this.signDigest(arrayify(hashMessage(message)))));
}
sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse> {
@ -659,7 +659,7 @@ export class ProviderSigner extends Signer {
}
return resolveProperties(transaction).then((tx) => {
let signedTx = signTransaction(tx, this.signDigest);
let signedTx = serializeTransaction(tx, this.signDigest);
return this._addressPromise.then((address) => {
if (parseTransaction(signedTx).from !== address) {
errors.throwError('signing address does not match expected address', errors.UNKNOWN_ERROR, { address: parseTransaction(signedTx).from, expectedAddress: address, signedTransaction: signedTx });

@ -18,7 +18,7 @@ import * as RLP from './rlp';
import { toUtf8Bytes, toUtf8String } from './utf8';
import { formatEther, parseEther, formatUnits, parseUnits } from './units';
import { fetchJson } from './web';
import { parse as parseTransaction } from './transaction';
import { parse as parseTransaction, serialize as serializeTransaction } from './transaction';
import * as errors from './errors';
@ -90,6 +90,7 @@ export {
joinSignature,
parseTransaction,
serializeTransaction,
errors
}
@ -154,6 +155,7 @@ export default {
joinSignature,
parseTransaction,
serializeTransaction,
errors
}

@ -6,6 +6,8 @@ import { keccak256 } from './keccak256';
import { recoverAddress, Signature } from './secp256k1';
import * as RLP from './rlp';
import * as errors from './errors';
export type UnsignedTransaction = {
to?: string;
nonce?: number;
@ -59,9 +61,9 @@ var transactionFields = [
];
export type SignDigestFunc = (digest: Arrayish) => Signature;
export type SignDigestFunc = (digest: Uint8Array) => Signature;
export function sign(transaction: UnsignedTransaction, signDigest: SignDigestFunc): string {
export function serialize(transaction: UnsignedTransaction, signDigest?: SignDigestFunc): string {
var raw: Array<string | Uint8Array> = [];
@ -71,53 +73,38 @@ export function sign(transaction: UnsignedTransaction, signDigest: SignDigestFun
// Fixed-width field
if (fieldInfo.length && value.length !== fieldInfo.length && value.length > 0) {
let error: any = new Error('invalid ' + fieldInfo.name);
error.reason = 'wrong length';
error.value = value;
throw error;
errors.throwError('invalid length for ' + fieldInfo.name, errors.INVALID_ARGUMENT, { arg: ('transaction' + fieldInfo.name), value: value });
}
// Variable-width (with a maximum)
if (fieldInfo.maxLength) {
value = stripZeros(value);
if (value.length > fieldInfo.maxLength) {
let error: any = new Error('invalid ' + fieldInfo.name);
error.reason = 'too long';
error.value = value;
throw error;
errors.throwError('invalid length for ' + fieldInfo.name, errors.INVALID_ARGUMENT, { arg: ('transaction' + fieldInfo.name), value: value });
}
}
raw.push(hexlify(value));
});
// @TOOD:
/*
// Requesting an unsigned transation
if (!signDigest) {
let v = 27 + signature.recoveryParam
if (transaction.chainId) {
v += transaction.chainId * 2 + 8;
}
//raw.push(hexlify(transaction.chainId));
raw.push(hexlify(v));
raw.push('0x');
raw.push('0x');
}
*/
if (transaction.chainId) {
if (transaction.chainId && transaction.chainId !== 0) {
raw.push(hexlify(transaction.chainId));
raw.push('0x');
raw.push('0x');
}
// Requesting an unsigned transation
if (!signDigest) {
return RLP.encode(raw);
}
var digest = keccak256(RLP.encode(raw));
var signature = signDigest(digest);
var signature = signDigest(arrayify(digest));
// We pushed a chainId and null r, s on for hashing only; remove those
var v = 27 + signature.recoveryParam
if (transaction.chainId) {
if (raw.length === 9) {
raw.pop();
raw.pop();
raw.pop();
@ -132,48 +119,60 @@ export function sign(transaction: UnsignedTransaction, signDigest: SignDigestFun
}
export function parse(rawTransaction: Arrayish): Transaction {
var signedTransaction = RLP.decode(rawTransaction);
if (signedTransaction.length !== 9) { throw new Error('invalid transaction'); }
let transaction = RLP.decode(rawTransaction);
if (transaction.length !== 9 && transaction.length !== 6) {
errors.throwError('invalid raw transaction', errors.INVALID_ARGUMENT, { arg: 'rawTransactin', value: rawTransaction });
}
let tx: Transaction = {
nonce: handleNumber(signedTransaction[0]).toNumber(),
gasPrice: handleNumber(signedTransaction[1]),
gasLimit: handleNumber(signedTransaction[2]),
to: handleAddress(signedTransaction[3]),
value: handleNumber(signedTransaction[4]),
data: signedTransaction[5],
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
};
var v = arrayify(signedTransaction[6]);
var r = arrayify(signedTransaction[7]);
var s = arrayify(signedTransaction[8]);
// Legacy unsigned transaction
if (transaction.length === 6) { return tx; }
if (v.length >= 1 && r.length >= 1 && r.length <= 32 && s.length >= 1 && s.length <= 32) {
tx.v = bigNumberify(v).toNumber();
tx.r = hexZeroPad(signedTransaction[7], 32);
tx.s = hexZeroPad(signedTransaction[8], 32);
try {
tx.v = bigNumberify(transaction[6]).toNumber();
var chainId = (tx.v - 35) / 2;
if (chainId < 0) { chainId = 0; }
chainId = Math.floor(chainId);
} catch (error) {
console.log(error);
return tx;
}
tx.chainId = chainId;
tx.r = hexZeroPad(transaction[7], 32);
tx.s = hexZeroPad(transaction[8], 32);
if (bigNumberify(tx.r).isZero() && 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; }
var recoveryParam = tx.v - 27;
let raw = signedTransaction.slice(0, 6);
let raw = transaction.slice(0, 6);
if (chainId) {
raw.push(hexlify(chainId));
if (tx.chainId !== 0) {
raw.push(hexlify(tx.chainId));
raw.push('0x');
raw.push('0x');
recoveryParam -= chainId * 2 + 8;
recoveryParam -= tx.chainId * 2 + 8;
}
var digest = keccak256(RLP.encode(raw));
try {
tx.from = recoverAddress(digest, { r: hexlify(r), s: hexlify(s), recoveryParam: recoveryParam });
tx.from = recoverAddress(digest, { r: hexlify(tx.r), s: hexlify(tx.s), recoveryParam: recoveryParam });
} catch (error) {
console.log(error);
}

@ -14,7 +14,7 @@ import { hashMessage } from '../utils/hash';
import { keccak256 } from '../utils/keccak256';
import { defineReadOnly, resolveProperties, shallowCopy } from '../utils/properties';
import { randomBytes } from '../utils/random-bytes';
import { sign as signTransaction } from '../utils/transaction';
import { serialize as serializeTransaction } from '../utils/transaction';
import * as errors from '../utils/errors';
@ -71,7 +71,7 @@ export class Wallet extends Signer {
sign(transaction: TransactionRequest): Promise<string> {
return resolveProperties(transaction).then((tx) => {
return signTransaction(tx, this.signingKey.signDigest.bind(this.signingKey));
return serializeTransaction(tx, this.signingKey.signDigest.bind(this.signingKey));
});
}

@ -3,6 +3,23 @@
var ethereumUtil = require('ethereumjs-util');
var ethereumTx = require('ethereumjs-tx');
ethereumTx.prototype.serializeUnsigned = function() {
let items
if (this._chainId > 0) {
const raw = this.raw.slice()
this.v = this._chainId
this.r = 0
this.s = 0
items = this.raw
this.raw = raw
} else {
items = this.raw.slice(0, 6)
}
// create hash
return ethereumUtil.rlp.encode(items)
}
var utils = require('../utils.js');
@ -16,12 +33,17 @@ function addTransaction(privateKey, name, transaction, signature) {
var rawTransactionEip155 = new ethereumTx(transaction);
delete transaction['chainId'];
console.log(rawTransaction, rawTransaction.serialize().toString('hex'));
console.log(rawTransactionEip155, rawTransactionEip155.serialize().toString('hex'));
console.log(rawTransactionEip155.serialize().toString('hex') === rawTransaction.serialize().toString('hex'));
console.log('------');
var test = {
accountAddress: '0x' + ethereumUtil.privateToAddress(privateKey).toString('hex'),
name: name,
privateKey: '0x' + privateKey.toString('hex'),
unsignedTransaction: '0x' + rawTransaction.serialize().toString('hex'),
unsignedTransactionChainId5: '0x' + rawTransactionEip155.serialize().toString('hex'),
unsignedTransaction: '0x' + rawTransaction.serializeUnsigned().toString('hex'),
unsignedTransactionChainId5: '0x' + rawTransactionEip155.serializeUnsigned().toString('hex'),
}
rawTransaction.sign(privateKey);

@ -10,31 +10,6 @@ if (global.ethers) {
var ethers = require('..');
}
/* // Brain Wallets are discontinued
describe('Test Brain Wallets', function() {
var Wallet = ethers.Wallet;
var tests = [
{
address: '0xbed9d2E41BdD066f702C4bDB86eB3A3740101acC',
name: 'simple brain wallet',
password: 'password',
username: 'ricmoo'
}
];
tests.forEach(function(test) {
it(('computes the brain wallet for ' + test.name), function() {
this.timeout(1000000);
return Wallet.fromBrainWallet(test.username, test.password).then(function(wallet) {
assert.equal(wallet.address, test.address,
'computed brain wallet for ' + test.username + ':' + test.password);
});
});
});
});
*/
describe('Test JSON Wallets', function() {
var Wallet = ethers.Wallet;
var tests = utils.loadTests('wallets');
@ -80,94 +55,110 @@ describe('Test JSON Wallets', function() {
});
});
describe('Test Transaction Signing and Parsing', function() {
var Wallet = ethers.Wallet;
function checkTransaction(parsedTransaction, test) {
var transaction = {};
var getAddress = ethers.utils.getAddress;
['nonce', 'gasLimit', 'gasPrice', 'to', 'value', 'data'].forEach(function(key) {
var expected = test[key];
var value = parsedTransaction[key];
if ({ gasLimit: 1, gasPrice: 1, value: 1 }[key]) {
assert.ok((!!value._bn),
'parsed into a big number - ' + key);
value = value.toHexString();
if (!expected || expected === '0x') { expected = '0x00'; }
} else if (key === 'nonce') {
assert.equal(typeof(value), 'number',
'parse into a number - nonce');
value = utils.hexlify(value);
if (!expected || expected === '0x') { expected = '0x00'; }
} else if (key === 'data') {
if (!expected) { expected = '0x'; }
} else if (key === 'to') {
if (value) {
// Make sure teh address is valid
ethers.utils.getAddress(value);
value = value.toLowerCase();
}
}
assert.equal(value, expected, 'parses ' + key + ' (legacy)');
transaction[key] = test[key];
});
return transaction;
}
describe('Test Transaction Signing and Parsing', function() {
var tests = utils.loadTests('transactions');
tests.forEach(function(test) {
it(('parses and signs transaction - ' + test.name), function() {
var wallet = new Wallet(test.privateKey);
var signingKey = new ethers.SigningKey(test.privateKey);
var signDigest = signingKey.signDigest.bind(signingKey);
var transaction = {};
// Legacy parsing unsigned transaction
checkTransaction(ethers.utils.parseTransaction(test.unsignedTransaction), test);
var parsedTransaction = ethers.utils.parseTransaction(test.signedTransaction);
var transaction = checkTransaction(parsedTransaction, test);
['nonce', 'gasLimit', 'gasPrice', 'to', 'value', 'data'].forEach(function(key) {
var expected = test[key];
var value = parsedTransaction[key];
if ({ gasLimit: 1, gasPrice: 1, value: 1 }[key]) {
assert.ok((!!value._bn),
'parsed into a big number - ' + key);
value = value.toHexString();
if (!expected || expected === '0x') { expected = '0x00'; }
} else if (key === 'nonce') {
assert.equal(typeof(value), 'number',
'parse into a number - nonce');
value = utils.hexlify(value);
if (!expected || expected === '0x') { expected = '0x00'; }
} else if (key === 'data') {
if (!expected) { expected = '0x'; }
} else if (key === 'to') {
if (value) {
// Make sure teh address is valid
getAddress(value);
value = value.toLowerCase();
}
}
assert.equal(value, expected, 'parsed ' + key);
transaction[key] = test[key];
});
assert.equal(parsedTransaction.from, getAddress(test.accountAddress),
// Legacy signed transaction ecrecover
assert.equal(parsedTransaction.from, ethers.utils.getAddress(test.accountAddress),
'computed from');
assert.equal(parsedTransaction.chainId, 0, 'parsed chainId');
// Legacy transaction chain ID
assert.equal(parsedTransaction.chainId, 0, 'parses chainId (legacy)');
var seq = Promise.resolve();
// Legacy serializes unsigned transaction
assert.equal(ethers.utils.serializeTransaction(transaction), test.unsignedTransaction,
'serializes undsigned transaction (legacy)');
// Legacy signed serialized transaction
assert.equal(ethers.utils.serializeTransaction(transaction, signDigest), test.signedTransaction,
'signs transaction (legacy)');
var async1 = ethers.utils.shallowCopy(transaction);
seq = seq.then(function() {
var signedTransaction = wallet.sign(async1).then(function(signedTransaction) {
assert.equal(signedTransaction, test.signedTransaction, 'signed transaction');
});
});
// EIP155
// EIP-155 parsing unsigned transaction
var parsedUnsignedTransactionChainId5 = ethers.utils.parseTransaction(test.unsignedTransactionChainId5);
checkTransaction(parsedUnsignedTransactionChainId5, test);
assert.equal(parsedUnsignedTransactionChainId5.chainId, 5, 'parses chainId (eip155)');
// EIP-155 fields
var parsedTransactionChainId5 = ethers.utils.parseTransaction(test.signedTransactionChainId5);
['data', 'from', 'nonce', 'to'].forEach(function (key) {
assert.equal(parsedTransaction[key], parsedTransactionChainId5[key],
'eip155 parsed ' + key);
'parses ' + key + ' (eip155)');
});
['gasLimit', 'gasPrice', 'value'].forEach(function (key) {
assert.ok(parsedTransaction[key].eq(parsedTransactionChainId5[key]),
'eip155 parsed ' + key);
'parses ' + key + ' (eip155)');
});
// EIP-155 chain ID
assert.equal(parsedTransactionChainId5.chainId, 5,
'eip155 parsed chainId');
'parses chainId (eip155)');
transaction.chainId = 5;
var async2 = ethers.utils.shallowCopy(transaction);
seq = seq.then(function() {
async2.chainId = 5;
var signedTransaction = wallet.sign(async2).then(function(signedTransactionChainId5) {
assert.equal(signedTransactionChainId5, test.signedTransactionChainId5, 'eip155 signed transaction');
});
});
// EIP-155 signed serialized transaction
assert.equal(ethers.utils.serializeTransaction(transaction, signDigest), test.signedTransactionChainId5,
'signs transaction (eip155)');
return seq;
// EIP-155 serialized unsigned transaction
assert.equal(ethers.utils.serializeTransaction(transaction), test.unsignedTransactionChainId5,
'serializes unsigned transaction (eip155) ');
});
});
});

Binary file not shown.

@ -66,6 +66,7 @@ var web_1 = require("./web");
exports.fetchJson = web_1.fetchJson;
var transaction_1 = require("./transaction");
exports.parseTransaction = transaction_1.parse;
exports.serializeTransaction = transaction_1.serialize;
var errors = __importStar(require("./errors"));
exports.errors = errors;
// NFKD (decomposed)
@ -114,5 +115,6 @@ exports.default = {
splitSignature: bytes_1.splitSignature,
joinSignature: bytes_1.joinSignature,
parseTransaction: transaction_1.parse,
serializeTransaction: transaction_1.serialize,
errors: errors
};

@ -13,6 +13,7 @@ var bytes_1 = require("./bytes");
var keccak256_1 = require("./keccak256");
var secp256k1_1 = require("./secp256k1");
var RLP = __importStar(require("./rlp"));
var errors = __importStar(require("./errors"));
function handleAddress(value) {
if (value === '0x') {
return null;
@ -33,53 +34,38 @@ var transactionFields = [
{ name: 'value', maxLength: 32 },
{ name: 'data' },
];
function sign(transaction, signDigest) {
function serialize(transaction, signDigest) {
var raw = [];
transactionFields.forEach(function (fieldInfo) {
var value = transaction[fieldInfo.name] || ([]);
value = bytes_1.arrayify(bytes_1.hexlify(value));
// Fixed-width field
if (fieldInfo.length && value.length !== fieldInfo.length && value.length > 0) {
var error = new Error('invalid ' + fieldInfo.name);
error.reason = 'wrong length';
error.value = value;
throw error;
errors.throwError('invalid length for ' + fieldInfo.name, errors.INVALID_ARGUMENT, { arg: ('transaction' + fieldInfo.name), value: value });
}
// Variable-width (with a maximum)
if (fieldInfo.maxLength) {
value = bytes_1.stripZeros(value);
if (value.length > fieldInfo.maxLength) {
var error = new Error('invalid ' + fieldInfo.name);
error.reason = 'too long';
error.value = value;
throw error;
errors.throwError('invalid length for ' + fieldInfo.name, errors.INVALID_ARGUMENT, { arg: ('transaction' + fieldInfo.name), value: value });
}
}
raw.push(bytes_1.hexlify(value));
});
// @TOOD:
/*
// Requesting an unsigned transation
if (!signDigest) {
let v = 27 + signature.recoveryParam
if (transaction.chainId) {
v += transaction.chainId * 2 + 8;
}
//raw.push(hexlify(transaction.chainId));
raw.push(hexlify(v));
raw.push('0x');
raw.push('0x');
}
*/
if (transaction.chainId) {
if (transaction.chainId && transaction.chainId !== 0) {
raw.push(bytes_1.hexlify(transaction.chainId));
raw.push('0x');
raw.push('0x');
}
// Requesting an unsigned transation
if (!signDigest) {
return RLP.encode(raw);
}
var digest = keccak256_1.keccak256(RLP.encode(raw));
var signature = signDigest(digest);
var signature = signDigest(bytes_1.arrayify(digest));
// We pushed a chainId and null r, s on for hashing only; remove those
var v = 27 + signature.recoveryParam;
if (transaction.chainId) {
if (raw.length === 9) {
raw.pop();
raw.pop();
raw.pop();
@ -90,45 +76,56 @@ function sign(transaction, signDigest) {
raw.push(bytes_1.stripZeros(bytes_1.arrayify(signature.s)));
return RLP.encode(raw);
}
exports.sign = sign;
exports.serialize = serialize;
function parse(rawTransaction) {
var signedTransaction = RLP.decode(rawTransaction);
if (signedTransaction.length !== 9) {
throw new Error('invalid transaction');
var transaction = RLP.decode(rawTransaction);
if (transaction.length !== 9 && transaction.length !== 6) {
errors.throwError('invalid raw transaction', errors.INVALID_ARGUMENT, { arg: 'rawTransactin', value: rawTransaction });
}
var tx = {
nonce: handleNumber(signedTransaction[0]).toNumber(),
gasPrice: handleNumber(signedTransaction[1]),
gasLimit: handleNumber(signedTransaction[2]),
to: handleAddress(signedTransaction[3]),
value: handleNumber(signedTransaction[4]),
data: signedTransaction[5],
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
};
var v = bytes_1.arrayify(signedTransaction[6]);
var r = bytes_1.arrayify(signedTransaction[7]);
var s = bytes_1.arrayify(signedTransaction[8]);
if (v.length >= 1 && r.length >= 1 && r.length <= 32 && s.length >= 1 && s.length <= 32) {
tx.v = bignumber_1.bigNumberify(v).toNumber();
tx.r = bytes_1.hexZeroPad(signedTransaction[7], 32);
tx.s = bytes_1.hexZeroPad(signedTransaction[8], 32);
var chainId = (tx.v - 35) / 2;
if (chainId < 0) {
chainId = 0;
// Legacy unsigned transaction
if (transaction.length === 6) {
return tx;
}
try {
tx.v = bignumber_1.bigNumberify(transaction[6]).toNumber();
}
catch (error) {
console.log(error);
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;
}
chainId = Math.floor(chainId);
tx.chainId = chainId;
var recoveryParam = tx.v - 27;
var raw = signedTransaction.slice(0, 6);
if (chainId) {
raw.push(bytes_1.hexlify(chainId));
var raw = transaction.slice(0, 6);
if (tx.chainId !== 0) {
raw.push(bytes_1.hexlify(tx.chainId));
raw.push('0x');
raw.push('0x');
recoveryParam -= chainId * 2 + 8;
recoveryParam -= tx.chainId * 2 + 8;
}
var digest = keccak256_1.keccak256(RLP.encode(raw));
try {
tx.from = secp256k1_1.recoverAddress(digest, { r: bytes_1.hexlify(r), s: bytes_1.hexlify(s), recoveryParam: recoveryParam });
tx.from = secp256k1_1.recoverAddress(digest, { r: bytes_1.hexlify(tx.r), s: bytes_1.hexlify(tx.s), recoveryParam: recoveryParam });
}
catch (error) {
console.log(error);

@ -80,7 +80,7 @@ var Wallet = /** @class */ (function (_super) {
Wallet.prototype.sign = function (transaction) {
var _this = this;
return properties_1.resolveProperties(transaction).then(function (tx) {
return transaction_1.sign(tx, _this.signingKey.signDigest.bind(_this.signingKey));
return transaction_1.serialize(tx, _this.signingKey.signDigest.bind(_this.signingKey));
});
};
Wallet.prototype.signMessage = function (message) {