2017-10-18 21:28:45 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var assert = require('assert');
|
|
|
|
|
2018-07-23 03:02:39 -04:00
|
|
|
var utils = require('./utils');
|
|
|
|
var ethers = utils.getEthers(__filename);
|
2018-03-04 19:31:09 -05:00
|
|
|
|
2017-10-18 21:28:45 -04:00
|
|
|
describe('Test JSON Wallets', function() {
|
2018-03-04 19:31:09 -05:00
|
|
|
var Wallet = ethers.Wallet;
|
2017-10-18 21:28:45 -04:00
|
|
|
var tests = utils.loadTests('wallets');
|
|
|
|
tests.forEach(function(test) {
|
|
|
|
it(('decrypts wallet - ' + test.name), function() {
|
2018-07-16 03:30:09 -04:00
|
|
|
this.timeout(1200000);
|
2017-10-22 17:47:17 -04:00
|
|
|
|
2019-08-22 15:22:20 -04:00
|
|
|
if (test.hasAddress) {
|
|
|
|
assert.ok((ethers.utils.getJsonWalletAddress(test.json) !== null),
|
|
|
|
'detect encrypted JSON wallet');
|
|
|
|
}
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-22 02:31:07 -04:00
|
|
|
return Wallet.fromEncryptedJson(test.json, test.password).then(function(wallet) {
|
2017-10-18 21:28:45 -04:00
|
|
|
assert.equal(wallet.privateKey, test.privateKey,
|
|
|
|
'generated correct private key - ' + wallet.privateKey);
|
|
|
|
assert.equal(wallet.address.toLowerCase(), test.address,
|
|
|
|
'generate correct address - ' + wallet.address);
|
2018-01-26 21:56:20 -05:00
|
|
|
if (test.mnemonic) {
|
|
|
|
assert.equal(wallet.mnemonic, test.mnemonic,
|
|
|
|
'mnemonic enabled encrypted wallet has a mnemonic');
|
|
|
|
}
|
2017-10-18 21:28:45 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// A few extra test cases to test encrypting/decrypting
|
|
|
|
['one', 'two', 'three'].forEach(function(i) {
|
|
|
|
var password = 'foobar' + i;
|
2019-01-17 16:32:51 -05:00
|
|
|
var wallet = Wallet.createRandom({ path: "m/56'/82", extraEntropy: utils.randomHexString('test-' + i, 32) });
|
2017-10-18 21:28:45 -04:00
|
|
|
it('encrypts and decrypts a random wallet - ' + i, function() {
|
2018-07-16 03:30:09 -04:00
|
|
|
this.timeout(1200000);
|
2017-10-22 17:47:17 -04:00
|
|
|
|
2017-10-18 21:28:45 -04:00
|
|
|
return wallet.encrypt(password).then(function(json) {
|
2018-06-22 02:31:07 -04:00
|
|
|
return Wallet.fromEncryptedJson(json, password).then(function(decryptedWallet) {
|
2017-10-18 21:28:45 -04:00
|
|
|
assert.equal(decryptedWallet.address, wallet.address,
|
|
|
|
'decrypted wallet - ' + wallet.privateKey);
|
2019-01-17 16:32:51 -05:00
|
|
|
assert.equal(decryptedWallet.mnemonic, wallet.mnemonic,
|
|
|
|
"decrypted wallet menonic - " + wallet.privateKey);
|
|
|
|
assert.equal(decryptedWallet.path, wallet.path,
|
|
|
|
"decrypted wallet path - " + wallet.privateKey);
|
2017-10-18 21:28:45 -04:00
|
|
|
return decryptedWallet.encrypt(password).then(function(encryptedWallet) {
|
|
|
|
var parsedWallet = JSON.parse(encryptedWallet);
|
|
|
|
assert.equal(decryptedWallet.address.toLowerCase().substring(2), parsedWallet.address,
|
|
|
|
're-encrypted wallet - ' + wallet.privateKey);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
function checkTransaction(parsedTransaction, test) {
|
|
|
|
var transaction = {};
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
['nonce', 'gasLimit', 'gasPrice', 'to', 'value', 'data'].forEach(function(key) {
|
|
|
|
var expected = test[key];
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
var value = parsedTransaction[key];
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
if ({ gasLimit: 1, gasPrice: 1, value: 1 }[key]) {
|
2018-09-24 16:07:14 -04:00
|
|
|
assert.ok((ethers.utils.BigNumber.isBigNumber(value)),
|
2018-06-25 21:02:20 -04:00
|
|
|
'parsed into a big number - ' + key);
|
|
|
|
value = value.toHexString();
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
if (!expected || expected === '0x') { expected = '0x00'; }
|
|
|
|
|
|
|
|
} else if (key === 'nonce') {
|
|
|
|
assert.equal(typeof(value), 'number',
|
|
|
|
'parse into a number - nonce');
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
value = utils.hexlify(value);
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
if (!expected || expected === '0x') { expected = '0x00'; }
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
} else if (key === 'data') {
|
|
|
|
if (!expected) { expected = '0x'; }
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
} else if (key === 'to') {
|
|
|
|
if (value) {
|
|
|
|
// Make sure teh address is valid
|
|
|
|
ethers.utils.getAddress(value);
|
|
|
|
value = value.toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
assert.equal(value, expected, 'parses ' + key + ' (legacy)');
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
transaction[key] = test[key];
|
|
|
|
});
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
return transaction;
|
|
|
|
}
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
describe('Test Transaction Signing and Parsing', function() {
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
var tests = utils.loadTests('transactions');
|
|
|
|
tests.forEach(function(test) {
|
|
|
|
it(('parses and signs transaction - ' + test.name), function() {
|
2018-07-16 00:20:36 -04:00
|
|
|
this.timeout(120000);
|
|
|
|
|
2018-09-24 16:07:14 -04:00
|
|
|
var signingKey = new ethers.utils.SigningKey(test.privateKey);
|
2018-06-25 21:02:20 -04:00
|
|
|
var signDigest = signingKey.signDigest.bind(signingKey);
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
// Legacy parsing unsigned transaction
|
|
|
|
checkTransaction(ethers.utils.parseTransaction(test.unsignedTransaction), test);
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
var parsedTransaction = ethers.utils.parseTransaction(test.signedTransaction);
|
|
|
|
var transaction = checkTransaction(parsedTransaction, test);
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
// Legacy signed transaction ecrecover
|
|
|
|
assert.equal(parsedTransaction.from, ethers.utils.getAddress(test.accountAddress),
|
2017-10-18 21:28:45 -04:00
|
|
|
'computed from');
|
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
// Legacy transaction chain ID
|
|
|
|
assert.equal(parsedTransaction.chainId, 0, 'parses chainId (legacy)');
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
// Legacy serializes unsigned transaction
|
2018-07-12 03:25:59 -04:00
|
|
|
(function() {
|
|
|
|
var unsignedTx = ethers.utils.serializeTransaction(transaction);
|
2018-07-12 02:42:46 -04:00
|
|
|
assert.equal(unsignedTx, test.unsignedTransaction,
|
|
|
|
'serializes undsigned transaction (legacy)');
|
|
|
|
|
|
|
|
// Legacy signed serialized transaction
|
2018-07-12 03:25:59 -04:00
|
|
|
var signature = signDigest(ethers.utils.keccak256(unsignedTx));
|
2018-07-12 02:42:46 -04:00
|
|
|
assert.equal(ethers.utils.serializeTransaction(transaction, signature), test.signedTransaction,
|
|
|
|
'signs transaction (legacy)');
|
2018-07-12 03:25:59 -04:00
|
|
|
})();
|
2018-06-18 14:53:41 -04:00
|
|
|
|
2017-10-18 21:28:45 -04:00
|
|
|
|
|
|
|
// EIP155
|
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
// 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
|
2018-06-17 16:32:57 -04:00
|
|
|
var parsedTransactionChainId5 = ethers.utils.parseTransaction(test.signedTransactionChainId5);
|
2017-10-18 21:28:45 -04:00
|
|
|
['data', 'from', 'nonce', 'to'].forEach(function (key) {
|
|
|
|
assert.equal(parsedTransaction[key], parsedTransactionChainId5[key],
|
2018-06-25 21:02:20 -04:00
|
|
|
'parses ' + key + ' (eip155)');
|
2017-10-18 21:28:45 -04:00
|
|
|
});
|
2018-06-25 21:02:20 -04:00
|
|
|
|
2017-10-18 21:28:45 -04:00
|
|
|
['gasLimit', 'gasPrice', 'value'].forEach(function (key) {
|
|
|
|
assert.ok(parsedTransaction[key].eq(parsedTransactionChainId5[key]),
|
2018-06-25 21:02:20 -04:00
|
|
|
'parses ' + key + ' (eip155)');
|
2017-10-18 21:28:45 -04:00
|
|
|
});
|
2018-06-25 21:02:20 -04:00
|
|
|
|
|
|
|
// EIP-155 chain ID
|
2017-10-18 21:28:45 -04:00
|
|
|
assert.equal(parsedTransactionChainId5.chainId, 5,
|
2018-06-25 21:02:20 -04:00
|
|
|
'parses chainId (eip155)');
|
2017-10-18 21:28:45 -04:00
|
|
|
|
2018-06-25 21:02:20 -04:00
|
|
|
transaction.chainId = 5;
|
2018-06-18 14:53:41 -04:00
|
|
|
|
2018-07-12 03:25:59 -04:00
|
|
|
(function() {
|
2018-07-12 02:42:46 -04:00
|
|
|
// EIP-155 serialized unsigned transaction
|
2018-07-12 03:25:59 -04:00
|
|
|
var unsignedTx = ethers.utils.serializeTransaction(transaction);
|
2018-07-12 02:42:46 -04:00
|
|
|
assert.equal(unsignedTx, test.unsignedTransactionChainId5,
|
|
|
|
'serializes unsigned transaction (eip155) ');
|
|
|
|
|
|
|
|
// EIP-155 signed serialized transaction
|
2018-07-12 03:25:59 -04:00
|
|
|
var signature = signDigest(ethers.utils.keccak256(unsignedTx));
|
2018-07-12 02:42:46 -04:00
|
|
|
assert.equal(ethers.utils.serializeTransaction(transaction, signature), test.signedTransactionChainId5,
|
|
|
|
'signs transaction (eip155)');
|
2018-07-12 03:25:59 -04:00
|
|
|
})();
|
2018-06-18 14:53:41 -04:00
|
|
|
|
2017-10-18 21:28:45 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-10-19 02:52:55 -04:00
|
|
|
|
|
|
|
describe('Test Signing Messages', function() {
|
2018-03-04 19:31:09 -05:00
|
|
|
var Wallet = ethers.Wallet;
|
2017-10-19 02:52:55 -04:00
|
|
|
|
2018-03-04 19:31:09 -05:00
|
|
|
var arrayify = ethers.utils.arrayify;
|
|
|
|
var id = ethers.utils.id;
|
|
|
|
var toUtf8Bytes = ethers.utils.toUtf8Bytes;
|
2017-12-01 22:25:19 -05:00
|
|
|
|
2017-10-19 02:52:55 -04:00
|
|
|
var tests = [
|
2017-10-19 02:56:40 -04:00
|
|
|
// See: https://etherscan.io/verifySig/57
|
2017-10-19 02:52:55 -04:00
|
|
|
{
|
|
|
|
address: '0x14791697260E4c9A71f18484C9f997B308e59325',
|
2017-12-01 22:25:19 -05:00
|
|
|
name: 'string("hello world")',
|
2017-10-19 02:52:55 -04:00
|
|
|
message: 'hello world',
|
2018-05-19 17:47:45 +02:00
|
|
|
messageHash: '0xd9eba16ed0ecae432b71fe008c98cc872bb4cc214d3220a36f365326cf807d68',
|
2017-10-19 02:52:55 -04:00
|
|
|
privateKey: '0x0123456789012345678901234567890123456789012345678901234567890123',
|
|
|
|
signature: '0xddd0a7290af9526056b4e35a077b9a11b513aa0028ec6c9880948544508f3c63265e99e47ad31bb2cab9646c504576b3abc6939a1710afc08cbf3034d73214b81c'
|
2017-12-01 22:25:19 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// See: https://github.com/ethers-io/ethers.js/issues/80
|
|
|
|
{
|
|
|
|
address: '0xD351c7c627ad5531Edb9587f4150CaF393c33E87',
|
|
|
|
name: 'bytes(0x47173285...4cb01fad)',
|
|
|
|
message: arrayify('0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'),
|
2018-05-19 17:47:45 +02:00
|
|
|
messageHash: '0x93100cc9477ba6522a2d7d5e83d0e075b167224ed8aa0c5860cfd47fa9f22797',
|
2017-12-01 22:25:19 -05:00
|
|
|
privateKey: '0x51d1d6047622bca92272d36b297799ecc152dc2ef91b229debf84fc41e8c73ee',
|
|
|
|
signature: '0x546f0c996fa4cfbf2b68fd413bfb477f05e44e66545d7782d87d52305831cd055fc9943e513297d0f6755ad1590a5476bf7d1761d4f9dc07dfe473824bbdec751b'
|
2017-12-05 01:59:37 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// See: https://github.com/ethers-io/ethers.js/issues/85
|
|
|
|
{
|
|
|
|
address: '0xe7deA7e64B62d1Ca52f1716f29cd27d4FE28e3e1',
|
|
|
|
name: 'zero-prefixed signature',
|
|
|
|
message: arrayify(id('0x7f23b5eed5bc7e89f267f339561b2697faab234a2')),
|
2018-05-19 17:47:45 +02:00
|
|
|
messageHash: '0x06c9d148d268f9a13d8f94f4ce351b0beff3b9ba69f23abbf171168202b2dd67',
|
2017-12-05 01:59:37 -05:00
|
|
|
privateKey: '0x09a11afa58d6014843fd2c5fd4e21e7fadf96ca2d8ce9934af6b8e204314f25c',
|
|
|
|
signature: '0x7222038446034a0425b6e3f0cc3594f0d979c656206408f937c37a8180bb1bea047d061e4ded4aeac77fa86eb02d42ba7250964ac3eb9da1337090258ce798491c'
|
2017-10-19 02:52:55 -04:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
tests.forEach(function(test) {
|
2017-12-01 22:25:19 -05:00
|
|
|
it(('signs a message "' + test.name + '"'), function() {
|
2018-07-16 00:20:36 -04:00
|
|
|
this.timeout(120000);
|
2017-10-19 02:52:55 -04:00
|
|
|
var wallet = new Wallet(test.privateKey);
|
2018-06-18 05:42:41 -04:00
|
|
|
return wallet.signMessage(test.message).then(function(signature) {
|
|
|
|
assert.equal(signature, test.signature, 'computes message signature');
|
|
|
|
});
|
2017-10-19 02:52:55 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tests.forEach(function(test) {
|
2017-12-01 22:25:19 -05:00
|
|
|
it(('verifies a message "' + test.name + '"'), function() {
|
2018-07-16 00:20:36 -04:00
|
|
|
this.timeout(120000);
|
2018-07-17 01:46:27 -04:00
|
|
|
var address = ethers.utils.verifyMessage(test.message, test.signature);
|
2017-10-19 02:52:55 -04:00
|
|
|
assert.equal(address, test.address, 'verifies message signature');
|
|
|
|
});
|
|
|
|
});
|
2018-05-19 17:47:45 +02:00
|
|
|
|
|
|
|
tests.forEach(function(test) {
|
|
|
|
it(('hashes a message "' + test.name + '"'), function() {
|
2018-07-16 00:20:36 -04:00
|
|
|
this.timeout(120000);
|
2018-06-18 05:42:41 -04:00
|
|
|
var hash = ethers.utils.hashMessage(test.message);
|
2018-05-19 17:47:45 +02:00
|
|
|
assert.equal(hash, test.messageHash, 'calculates message hash');
|
|
|
|
});
|
|
|
|
});
|
2017-10-19 02:52:55 -04:00
|
|
|
});
|