Compare commits

..

3 Commits

Author SHA1 Message Date
ricmoo
736b08e016 Updated parseTransaction to format parameters more meaningfully. 2016-09-16 14:46:19 +08:00
ricmoo
d8013cae37 Added transaction parsing with address recovery. 2016-09-16 14:08:36 +08:00
ricmoo
b26b1b9c53 Moved getContractAddress to utils. 2016-08-23 22:06:26 -04:00
8 changed files with 152 additions and 22 deletions

83
dist/ethers-wallet.js vendored
View File

@@ -2,7 +2,6 @@
(function (Buffer){
'use strict';
var rlp = require('rlp');
var scrypt = require('scrypt-js');
var Contract = require('./lib/contract.js');
@@ -26,13 +25,7 @@ utils.defineProperty(exportUtils, 'Buffer', Buffer);
utils.defineProperty(exportUtils, 'sha3', utils.sha3);
utils.defineProperty(exportUtils, 'sha256', utils.sha256);
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
utils.defineProperty(exportUtils, 'getContractAddress', function(transaction) {
return utils.getAddress('0x' + utils.sha3(rlp.encode([
utils.hexOrBuffer(utils.getAddress(transaction.from)),
utils.hexOrBuffer(utils.hexlify(transaction.nonce, 'nonce'))
])).slice(12).toString('hex'));
});
utils.defineProperty(exportUtils, 'getContractAddress', utils.getContractAddress);
module.exports = Wallet;
@@ -105,7 +98,7 @@ utils.defineProperty(Wallet, 'randomish', new Randomish());
module.exports = Wallet;
}).call(this,require("buffer").Buffer)
},{"./lib/contract.js":4,"./lib/providers.js":5,"./lib/randomish.js":6,"./lib/secret-storage.js":7,"./lib/signing-key.js":8,"./lib/units.js":9,"./lib/utils.js":10,"./lib/wallet.js":11,"buffer":41,"rlp":85,"scrypt-js":86}],2:[function(require,module,exports){
},{"./lib/contract.js":4,"./lib/providers.js":5,"./lib/randomish.js":6,"./lib/secret-storage.js":7,"./lib/signing-key.js":8,"./lib/units.js":9,"./lib/utils.js":10,"./lib/wallet.js":11,"buffer":41,"scrypt-js":86}],2:[function(require,module,exports){
(function (global,Buffer){
'use strict';
@@ -1589,6 +1582,7 @@ var utils = require('./utils.js');
var secp256k1 = new (elliptic.ec)('secp256k1');
function SigningKey(privateKey) {
if (!(this instanceof SigningKey)) { throw new Error('missing new'); }
@@ -1610,6 +1604,11 @@ function SigningKey(privateKey) {
});
}
utils.defineProperty(SigningKey, 'recover', function(digest, r, s, recoveryParam) {
var publicKey = secp256k1.recoverPubKey(digest, {r: r, s: s}, recoveryParam);
publicKey = (new Buffer(publicKey.encode('hex', false), 'hex')).slice(1);
return utils.getAddress(utils.sha3(publicKey).slice(12).toString('hex'));
});
module.exports = SigningKey;
@@ -1698,6 +1697,8 @@ module.exports = {
(function (Buffer){
'use strict';
var rlp = require('rlp');
var BN = require('../node_modules/elliptic/node_modules/bn.js/lib/bn.js');
var hash = require('../node_modules/elliptic/node_modules/hash.js/lib/hash.js');
@@ -2010,6 +2011,14 @@ function getIcapAddress(address) {
return 'XE' + ibanChecksum('XE00' + base36) + base36;
}
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
function getContractAddress(transaction) {
return getAddress('0x' + sha3(rlp.encode([
hexOrBuffer(getAddress(transaction.from)),
hexOrBuffer(hexlify(transaction.nonce, 'nonce'))
])).slice(12).toString('hex'));
}
function cloneObject(object) {
var clone = {};
for (var key in object) { clone[key] = object[key]; }
@@ -2075,6 +2084,8 @@ module.exports = {
getAddress: getAddress,
getIcapAddress: getIcapAddress,
getContractAddress: getContractAddress,
cloneObject: cloneObject,
bnToBuffer: bnToBuffer,
@@ -2088,7 +2099,7 @@ module.exports = {
}
}).call(this,require("buffer").Buffer)
},{"../node_modules/elliptic/node_modules/bn.js/lib/bn.js":29,"../node_modules/elliptic/node_modules/hash.js/lib/hash.js":31,"buffer":41}],11:[function(require,module,exports){
},{"../node_modules/elliptic/node_modules/bn.js/lib/bn.js":29,"../node_modules/elliptic/node_modules/hash.js/lib/hash.js":31,"buffer":41,"rlp":85}],11:[function(require,module,exports){
(function (global,Buffer){
'use strict';
@@ -2203,6 +2214,58 @@ function Wallet(privateKey, provider) {
});
}
utils.defineProperty(Wallet, 'parseTransaction', function(rawTransaction) {
rawTransaction = utils.hexOrBuffer(rawTransaction, 'rawTransaction');
var signedTransaction = rlp.decode(rawTransaction);
var raw = [];
var transaction = {};
transactionFields.forEach(function(fieldInfo, index) {
transaction[fieldInfo.name] = signedTransaction[index];
raw.push(signedTransaction[index]);
});
if (transaction.to) {
if (transaction.to.length === 0) {
delete transaction.to;
} else {
transaction.to = utils.getAddress('0x' + transaction.to.toString('hex'));
}
}
['gasPrice', 'gasLimit', 'nonce', 'value'].forEach(function(name) {
if (!transaction[name]) { return; }
if (transaction[name].length === 0) {
transaction[name] = new utils.BN(0);
} else {
transaction[name] = new utils.BN(transaction[name].toString('hex'), 16);
}
});
/* @TODO: Maybe? In the future, all nonces stored as numbers? (obviously, major version change)
if (transaction.nonce) {
transaction.nonce = transaction.nonce.toNumber()
}
*/
if (signedTransaction.length > 6 && signedTransaction[6].length === 1 &&
signedTransaction[7].length >= 1 && signedTransaction[7].length <= 32 &&
signedTransaction[8].length >= 1 && signedTransaction[7].length <= 32) {
transaction.v = signedTransaction[6][0];
transaction.r = signedTransaction[7];
transaction.s = signedTransaction[8];
var digest = utils.sha3(rlp.encode(raw));
try {
transaction.from = SigningKey.recover(digest, transaction.r, transaction.s, transaction.v - 27);
} catch (error) { }
}
return transaction;
});
utils.defineProperty(Wallet.prototype, 'getBalance', function(blockNumber) {
var provider = this._provider;

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,5 @@
'use strict';
var rlp = require('rlp');
var scrypt = require('scrypt-js');
var Contract = require('./lib/contract.js');
@@ -24,13 +23,7 @@ utils.defineProperty(exportUtils, 'Buffer', Buffer);
utils.defineProperty(exportUtils, 'sha3', utils.sha3);
utils.defineProperty(exportUtils, 'sha256', utils.sha256);
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
utils.defineProperty(exportUtils, 'getContractAddress', function(transaction) {
return utils.getAddress('0x' + utils.sha3(rlp.encode([
utils.hexOrBuffer(utils.getAddress(transaction.from)),
utils.hexOrBuffer(utils.hexlify(transaction.nonce, 'nonce'))
])).slice(12).toString('hex'));
});
utils.defineProperty(exportUtils, 'getContractAddress', utils.getContractAddress);
module.exports = Wallet;

View File

@@ -7,6 +7,7 @@ var utils = require('./utils.js');
var secp256k1 = new (elliptic.ec)('secp256k1');
function SigningKey(privateKey) {
if (!(this instanceof SigningKey)) { throw new Error('missing new'); }
@@ -28,5 +29,10 @@ function SigningKey(privateKey) {
});
}
utils.defineProperty(SigningKey, 'recover', function(digest, r, s, recoveryParam) {
var publicKey = secp256k1.recoverPubKey(digest, {r: r, s: s}, recoveryParam);
publicKey = (new Buffer(publicKey.encode('hex', false), 'hex')).slice(1);
return utils.getAddress(utils.sha3(publicKey).slice(12).toString('hex'));
});
module.exports = SigningKey;

View File

@@ -1,5 +1,7 @@
'use strict';
var rlp = require('rlp');
var BN = require('../node_modules/elliptic/node_modules/bn.js/lib/bn.js');
var hash = require('../node_modules/elliptic/node_modules/hash.js/lib/hash.js');
@@ -312,6 +314,14 @@ function getIcapAddress(address) {
return 'XE' + ibanChecksum('XE00' + base36) + base36;
}
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
function getContractAddress(transaction) {
return getAddress('0x' + sha3(rlp.encode([
hexOrBuffer(getAddress(transaction.from)),
hexOrBuffer(hexlify(transaction.nonce, 'nonce'))
])).slice(12).toString('hex'));
}
function cloneObject(object) {
var clone = {};
for (var key in object) { clone[key] = object[key]; }
@@ -377,6 +387,8 @@ module.exports = {
getAddress: getAddress,
getIcapAddress: getIcapAddress,
getContractAddress: getContractAddress,
cloneObject: cloneObject,
bnToBuffer: bnToBuffer,

View File

@@ -111,6 +111,58 @@ function Wallet(privateKey, provider) {
});
}
utils.defineProperty(Wallet, 'parseTransaction', function(rawTransaction) {
rawTransaction = utils.hexOrBuffer(rawTransaction, 'rawTransaction');
var signedTransaction = rlp.decode(rawTransaction);
var raw = [];
var transaction = {};
transactionFields.forEach(function(fieldInfo, index) {
transaction[fieldInfo.name] = signedTransaction[index];
raw.push(signedTransaction[index]);
});
if (transaction.to) {
if (transaction.to.length === 0) {
delete transaction.to;
} else {
transaction.to = utils.getAddress('0x' + transaction.to.toString('hex'));
}
}
['gasPrice', 'gasLimit', 'nonce', 'value'].forEach(function(name) {
if (!transaction[name]) { return; }
if (transaction[name].length === 0) {
transaction[name] = new utils.BN(0);
} else {
transaction[name] = new utils.BN(transaction[name].toString('hex'), 16);
}
});
/* @TODO: Maybe? In the future, all nonces stored as numbers? (obviously, major version change)
if (transaction.nonce) {
transaction.nonce = transaction.nonce.toNumber()
}
*/
if (signedTransaction.length > 6 && signedTransaction[6].length === 1 &&
signedTransaction[7].length >= 1 && signedTransaction[7].length <= 32 &&
signedTransaction[8].length >= 1 && signedTransaction[7].length <= 32) {
transaction.v = signedTransaction[6][0];
transaction.r = signedTransaction[7];
transaction.s = signedTransaction[8];
var digest = utils.sha3(rlp.encode(raw));
try {
transaction.from = SigningKey.recover(digest, transaction.r, transaction.s, transaction.v - 27);
} catch (error) { }
}
return transaction;
});
utils.defineProperty(Wallet.prototype, 'getBalance', function(blockNumber) {
var provider = this._provider;

View File

@@ -1,6 +1,6 @@
{
"name": "ethers-wallet",
"version": "1.0.1",
"version": "1.0.4",
"description": "Ethereum wallet library.",
"main": "index.js",
"scripts": {

View File

@@ -17,9 +17,13 @@ module.exports = function(test) {
rawTransaction.sign(privateKey);
var ethereumLib = '0x' + rawTransaction.serialize().toString('hex');
var ethers = (new Wallet(privateKey)).sign(transaction);
var wallet = new Wallet(privateKey);
var ethers = wallet.sign(transaction);
test.equal(ethers, ethereumLib, 'invalid transaction');
// @TODO: More testing on parsed transaction.
test.equal(wallet.address, Wallet.parseTransaction(ethers).from, 'invalid parseTransaction');
}
for (var i = 0; i < 10000; i++) {