Added test case generation files.
This commit is contained in:
parent
26958d74d8
commit
6aceee8486
58
tests/make-tests/make-accounts.js
Normal file
58
tests/make-tests/make-accounts.js
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var fs = require('fs');
|
||||||
|
|
||||||
|
var ethereumUtil = require('ethereumjs-util');
|
||||||
|
var iban = require('web3/lib/web3/iban.js');
|
||||||
|
|
||||||
|
var utils = require('./utils.js');
|
||||||
|
|
||||||
|
function icap(address) {
|
||||||
|
address = (iban.fromAddress(address))._iban;
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
var Tests = [
|
||||||
|
// Edge-cases
|
||||||
|
'0x0000000000000000000000000000000000000000',
|
||||||
|
'0x0000000000000000000000000000000000000001',
|
||||||
|
'0xfffffffffffffffffffffffffffffffffffffffe',
|
||||||
|
'0xffffffffffffffffffffffffffffffffffffffff',
|
||||||
|
|
||||||
|
// Exposes a padding bug in Web3
|
||||||
|
// See: https://github.com/ethereum/web3.js/pull/417
|
||||||
|
'0x03582910e5bc7a12793da58559aba9a6c4138e44',
|
||||||
|
]
|
||||||
|
|
||||||
|
var OutputAddresses = [];
|
||||||
|
var OutputPrivateKeys = [];
|
||||||
|
|
||||||
|
Tests.forEach(function(address) {
|
||||||
|
OutputAddresses.push({
|
||||||
|
address: address,
|
||||||
|
checksumAddress: ethereumUtil.toChecksumAddress(address),
|
||||||
|
icapAddress: icap(address)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
for (var i = 0; i < 10000; i++) {
|
||||||
|
var privateKey = utils.randomBytes('accounts-' + i, 32);
|
||||||
|
var address = '0x' + ethereumUtil.privateToAddress(new Buffer(privateKey)).toString('hex');
|
||||||
|
|
||||||
|
OutputAddresses.push({
|
||||||
|
address: address,
|
||||||
|
checksumAddress: ethereumUtil.toChecksumAddress(address),
|
||||||
|
icapAddress: icap(address),
|
||||||
|
});
|
||||||
|
|
||||||
|
OutputPrivateKeys.push({
|
||||||
|
address: address,
|
||||||
|
checksumAddress: ethereumUtil.toChecksumAddress(address),
|
||||||
|
icapAddress: icap(address),
|
||||||
|
privateKey: utils.hexlify(privateKey),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.saveTestcase('addresses', OutputAddresses);
|
||||||
|
utils.saveTestcase('private-keys', OutputPrivateKeys);
|
||||||
|
|
536
tests/make-tests/make-contract-interface.js
Normal file
536
tests/make-tests/make-contract-interface.js
Normal file
@ -0,0 +1,536 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var BN = require('bn.js');
|
||||||
|
var solc = require('solc');
|
||||||
|
var ethereumVm = require('ethereumjs-vm');
|
||||||
|
var ethereumUtil = require('ethereumjs-util');
|
||||||
|
var promiseRationing = require('promise-rationing');
|
||||||
|
var Web3 = require('web3');
|
||||||
|
|
||||||
|
var contracts = require('../../contracts/index.js');
|
||||||
|
|
||||||
|
var bigNumber = require('../../utils/bignumber.js');
|
||||||
|
var convert = require('../../utils/convert.js');
|
||||||
|
|
||||||
|
var utils = require('./utils.js');
|
||||||
|
|
||||||
|
// Create the indent given a tabstop
|
||||||
|
function indent(tabs) {
|
||||||
|
var indent = new Buffer(tabs * 4);
|
||||||
|
indent.fill(32);
|
||||||
|
return indent.toString('utf8')
|
||||||
|
}
|
||||||
|
|
||||||
|
function recursiveHexlify(object) {
|
||||||
|
if (typeof(object) === 'number') {
|
||||||
|
object = new BN(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(object)) {
|
||||||
|
var result = [];
|
||||||
|
object.forEach(function(object) {
|
||||||
|
result.push(recursiveHexlify(object));
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
|
||||||
|
} else if (BN.isBN(object)) {
|
||||||
|
return {type: 'number', value: object.toString(10)};
|
||||||
|
|
||||||
|
} else if (typeof(object) === 'string') {
|
||||||
|
return {type: 'string', value: object};
|
||||||
|
|
||||||
|
} else if (typeof(object) === 'boolean') {
|
||||||
|
return {type: 'boolean', value: object};
|
||||||
|
|
||||||
|
} else if (Buffer.isBuffer(object)) {
|
||||||
|
return {type: 'buffer', value: utils.hexlify(object)};
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('unsupported type - ' + object + ' ' + typeof(object));
|
||||||
|
}
|
||||||
|
|
||||||
|
var web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:8545'));
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function getValue(value) {
|
||||||
|
if (Buffer.isBuffer(value)) {
|
||||||
|
value = utils.hexlify(value);
|
||||||
|
} else if (BN.isBN(value)) {
|
||||||
|
value = value.toString(10);
|
||||||
|
} else if (typeof(value) !== 'string' && typeof(value) !== 'number' && typeof(value) !== 'boolean') {
|
||||||
|
throw new Error('invalid type - ' + value + ' ' + typeof(value));
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
function createContractOutput(types, values) {
|
||||||
|
var source = 'contract Test {\n';
|
||||||
|
source += ' function test() constant returns (' + types.join(', ') + ') {\n';
|
||||||
|
|
||||||
|
var returns = [];
|
||||||
|
for (var i = 0; i < types.length; i++) {
|
||||||
|
var name = String.fromCharCode(97 + i);
|
||||||
|
|
||||||
|
// Array type; do a deep copy
|
||||||
|
if (types[i].indexOf('[') >= 0) {
|
||||||
|
|
||||||
|
// Each count (or optionally empty) array type
|
||||||
|
var arrays = types[i].match(/\[[0-9]*\]/g);
|
||||||
|
|
||||||
|
// Allocate the space (only dynamic arrays require new)
|
||||||
|
source += indent(2) + types[i] + ' memory ' + name;
|
||||||
|
if (arrays[arrays.length - 1] === '[]') {
|
||||||
|
source += ' = new ' + types[i] + '(' + values[i].length+ ')';
|
||||||
|
}
|
||||||
|
source +=';\n';
|
||||||
|
|
||||||
|
var baseType = types[i].substring(0, types[i].indexOf('['));
|
||||||
|
|
||||||
|
function recursiveSet(item, indices) {
|
||||||
|
if (Array.isArray(item)) {
|
||||||
|
item.forEach(function(item, index) {
|
||||||
|
var i = indices.slice();
|
||||||
|
i.unshift(index);
|
||||||
|
recursiveSet(item, i);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
var loc = '';
|
||||||
|
indices.forEach(function(index) {
|
||||||
|
loc = '[' + index + ']' + loc;
|
||||||
|
})
|
||||||
|
|
||||||
|
item = getValue(item);
|
||||||
|
|
||||||
|
//if (item instanceof BN) { item = item.toString(10); }
|
||||||
|
source += indent(2) + name + loc + ' = ' + baseType + '(' + item + ');\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
recursiveSet(values[i], []);
|
||||||
|
|
||||||
|
// Dynamic type: bytes
|
||||||
|
} else if (types[i] === 'bytes') {
|
||||||
|
source += indent(2) + 'bytes memory ' + name + ' = new bytes(' + values[i].length + ');\n';
|
||||||
|
source += indent(2) + 'assembly {\n'
|
||||||
|
source += indent(3) + 'mstore(' + name + ', ' + values[i].length + ')\n';
|
||||||
|
for (var j = 0; j < values[i].length; j++) {
|
||||||
|
source += indent(3) + 'mstore8(add(' + name + ', ' + (32 + j) + '), ' + values[i][j] + ')\n';
|
||||||
|
}
|
||||||
|
source += indent(2) + '}\n'
|
||||||
|
/*
|
||||||
|
var value = '';
|
||||||
|
for (var j = 0; j < values[i].length; j++) {
|
||||||
|
value += '\\' + 'x' + values[i].slice(j, j + 1).toString('hex');
|
||||||
|
}
|
||||||
|
source += ' bytes memory ' + name + ' = "' + value + '";\n';
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Dynamic type: string
|
||||||
|
} else if (types[i] === 'string') {
|
||||||
|
source += ' string memory ' + name + ' = "' + values[i] + '";\n';
|
||||||
|
|
||||||
|
// Static type; just use the stack
|
||||||
|
} else {
|
||||||
|
var value = getValue(values[i]);
|
||||||
|
source += ' ' + types[i] + ' ' + name + ' = ' + types[i] + '(' + value + ');\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Track the name to return
|
||||||
|
returns.push(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the values
|
||||||
|
source += ' return (' + returns.join(', ') + ');\n';
|
||||||
|
|
||||||
|
source += ' }\n';
|
||||||
|
source += '}\n';
|
||||||
|
|
||||||
|
try {
|
||||||
|
var contract = solc.compile(source, 0);
|
||||||
|
contract = contract.contracts.Test;
|
||||||
|
contract.sourceCode = source;
|
||||||
|
return contract;
|
||||||
|
} catch (error) {
|
||||||
|
console.log('Failed to compile ========');
|
||||||
|
console.log({types: types, values: values, contract: contract});
|
||||||
|
console.log(source);
|
||||||
|
console.log('========');
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var Address = '0xbe764deeec446f1c6e9d4c891b0f87148a2f9a00';
|
||||||
|
|
||||||
|
var Output = [];
|
||||||
|
|
||||||
|
function web3Promise(method, params) {
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
params.push(function(error, result) {
|
||||||
|
if (error) {
|
||||||
|
console.log(error);
|
||||||
|
return reject(error);
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
});
|
||||||
|
|
||||||
|
web3.eth[method].apply(web3, params);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendTransaction(transaction) {
|
||||||
|
var address = '0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e';
|
||||||
|
transaction.from = address;
|
||||||
|
|
||||||
|
console.log('Sending...');
|
||||||
|
|
||||||
|
return Promise.all([
|
||||||
|
web3Promise('getGasPrice', []),
|
||||||
|
web3Promise('getTransactionCount', [address, 'pending'])
|
||||||
|
]).then(function(result) {
|
||||||
|
transaction.gasPrice = '0x' + result[0].toString(16);
|
||||||
|
transaction.gas = "0x55d4a80";
|
||||||
|
//transaction.nonce = result[1];
|
||||||
|
return web3Promise('sendTransaction', [transaction]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeTests() {
|
||||||
|
|
||||||
|
function _check(name, types, values, normalizedValues) {
|
||||||
|
if (!normalizedValues) { normalizedValues = values; }
|
||||||
|
|
||||||
|
var contract = createContractOutput(types, values);
|
||||||
|
var transaction = { data: '0x' + contract.bytecode };
|
||||||
|
|
||||||
|
return sendTransaction(transaction).then(function(hash) {
|
||||||
|
console.log('Transaction', hash);
|
||||||
|
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
function check() {
|
||||||
|
web3Promise('getTransaction', [hash]).then(function(transaction) {
|
||||||
|
if (transaction.blockHash) {
|
||||||
|
console.log('Done', hash);
|
||||||
|
resolve(transaction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log('Waiting', hash);
|
||||||
|
setTimeout(check, 1000);
|
||||||
|
}, function(error) {
|
||||||
|
reject(error);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
check();
|
||||||
|
});
|
||||||
|
|
||||||
|
}).then(function(transaction) {
|
||||||
|
return new web3Promise('call', [{
|
||||||
|
to: transaction.creates,
|
||||||
|
data: '0xf8a8fd6d',
|
||||||
|
}]);
|
||||||
|
|
||||||
|
}).then(function(result) {
|
||||||
|
console.log('Result', result);
|
||||||
|
|
||||||
|
var output = {
|
||||||
|
bytecode: '0x' + contract.bytecode,
|
||||||
|
result: result,
|
||||||
|
interface: contract.interface,
|
||||||
|
name: name,
|
||||||
|
runtimeBytecode: '0x' + contract.runtimeBytecode,
|
||||||
|
source: contract.sourceCode,
|
||||||
|
types: JSON.stringify(types),
|
||||||
|
values: JSON.stringify(recursiveHexlify(values)),
|
||||||
|
normalizedValues: JSON.stringify(recursiveHexlify(normalizedValues)),
|
||||||
|
};
|
||||||
|
return output;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var promiseFuncs = [];
|
||||||
|
|
||||||
|
function check(name, types, values, normalizedValues) {
|
||||||
|
promiseFuncs.push(function(resolve, reject) {
|
||||||
|
_check(name, types, values, normalizedValues).then(function(result) {
|
||||||
|
resolve(result);
|
||||||
|
}, function(error) {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Test cases: https://github.com/ethereum/solidity.js/blob/master/test/coder.decodeParam.js
|
||||||
|
check('sol-1', ['int'], [new BN(1)]);
|
||||||
|
check('sol-2', ['int'], [new BN(16)]);
|
||||||
|
|
||||||
|
check('sol-3', ['int'], [new BN(-1)]);
|
||||||
|
check('sol-4', ['int256'], [new BN(1)]);
|
||||||
|
check('sol-5', ['int256'], [new BN(16)]);
|
||||||
|
check('sol-6', ['int256'], [new BN(-1)]);
|
||||||
|
check('sol-7', ['int8'], [new BN(16)]);
|
||||||
|
check('sol-8', ['int32'], [new BN(16)]);
|
||||||
|
check('sol-9', ['int64'], [new BN(16)]);
|
||||||
|
check('sol-10', ['int128'], [new BN(16)]);
|
||||||
|
|
||||||
|
check('sol-11', ['uint'], [new BN(1)]);
|
||||||
|
check('sol-12', ['uint'], [new BN(16)]);
|
||||||
|
check('sol-13', ['uint'], [new BN(-1)], [new BN('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)]);
|
||||||
|
check('sol-14', ['uint256'], [new BN(1)]);
|
||||||
|
check('sol-15', ['uint256'], [new BN(16)]);
|
||||||
|
check('sol-16', ['uint256'], [new BN(-1)], [new BN('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)]);
|
||||||
|
check('sol-17', ['uint8'], [new BN(16)]);
|
||||||
|
check('sol-18', ['uint32'], [new BN(16)]);
|
||||||
|
check('sol-19', ['uint64'], [new BN(16)]);
|
||||||
|
check('sol-20', ['uint128'], [new BN(16)]);
|
||||||
|
|
||||||
|
check('sol-21', ['int', 'int'], [new BN(1), new BN(2)]);
|
||||||
|
check('sol-22', ['int', 'int'], [new BN(1), new BN(2)]);
|
||||||
|
check('sol-23', ['int[2]', 'int'], [[new BN(12), new BN(22)], new BN(3)]);
|
||||||
|
check('sol-24', ['int[2]', 'int[]'], [[new BN(32), new BN(42)], [new BN(3), new BN(4), new BN(5)]]);
|
||||||
|
|
||||||
|
check('sol-25',
|
||||||
|
['bytes32'],
|
||||||
|
[new Buffer('6761766f66796f726b0000000000000000000000000000000000000000000000', 'hex')]
|
||||||
|
);
|
||||||
|
check('sol-26',
|
||||||
|
['bytes'],
|
||||||
|
[new Buffer('6761766f66796f726b', 'hex')]
|
||||||
|
);
|
||||||
|
|
||||||
|
check('sol-27',
|
||||||
|
['string'],
|
||||||
|
['\uD835\uDF63']
|
||||||
|
);
|
||||||
|
|
||||||
|
check('sol-28',
|
||||||
|
['address', 'string', 'bytes6[4]', 'int'],
|
||||||
|
[
|
||||||
|
"0x97916ef549947a3e0d321485a31dd2715a97d455",
|
||||||
|
"foobar2",
|
||||||
|
[
|
||||||
|
new Buffer("a165ab0173c6", 'hex'),
|
||||||
|
new Buffer("f0f37bee9244", 'hex'),
|
||||||
|
new Buffer("c8dc0bf08d2b", 'hex'),
|
||||||
|
new Buffer("c8dc0bf08d2b", 'hex')
|
||||||
|
],
|
||||||
|
34
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
check('sol-29',
|
||||||
|
['bytes32'],
|
||||||
|
[new Buffer('731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 'hex')]
|
||||||
|
);
|
||||||
|
check('sol-30',
|
||||||
|
['bytes'],
|
||||||
|
[new Buffer('731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 'hex')]
|
||||||
|
);
|
||||||
|
|
||||||
|
check('sol-31',
|
||||||
|
['bytes32[2]'],
|
||||||
|
[[
|
||||||
|
new Buffer('731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 'hex'),
|
||||||
|
new Buffer('731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 'hex')
|
||||||
|
]]
|
||||||
|
);
|
||||||
|
|
||||||
|
check('sol-32',
|
||||||
|
['bytes'],
|
||||||
|
[new Buffer('131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
|
||||||
|
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
|
||||||
|
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 'hex')]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Some extra checks for width and sign tests
|
||||||
|
check('sol-33', ['uint32'], [14], [new BN(14)]);
|
||||||
|
check('sol-34', ['uint32'], [14], [new BN(14)]);
|
||||||
|
check('sol-35', ['uint32'], [-14], [new BN(0xfffffff2)]);
|
||||||
|
check('sol-36', ['int32'], [14], [new BN(14)]);
|
||||||
|
check('sol-37', ['int32'], [-14], [new BN(-14)]);
|
||||||
|
|
||||||
|
check('sol-38', ['int8'], [new BN(1)], [new BN(1)]);
|
||||||
|
check('sol-39', ['int8'], [new BN(-1)], [new BN(-1)]);
|
||||||
|
check('sol-40', ['int8'], [new BN(189)], [new BN(-67)]);
|
||||||
|
check('sol-41', ['int8'], [new BN(-189)], [new BN(67)]);
|
||||||
|
check('sol-42', ['int8'], [new BN(257)], [new BN(1)]);
|
||||||
|
|
||||||
|
check('sol-43', ['uint8'], [new BN(343)], [new BN(87)]);
|
||||||
|
check('sol-44', ['uint8'], [new BN(-1)], [new BN(255)]);
|
||||||
|
|
||||||
|
check('sol-45', ['uint56[5]'], [[new BN(639), new BN(227), new BN(727), new BN(325), new BN(146)]]);
|
||||||
|
|
||||||
|
function randomTypeValue(seed, onlyStatic) {
|
||||||
|
switch (utils.randomNumber(seed + '-type', 0, (onlyStatic ? 5: 8))) {
|
||||||
|
|
||||||
|
// Fixed-size bytes
|
||||||
|
case 0:
|
||||||
|
var size = utils.randomNumber(seed + '-type-0', 1, 33);
|
||||||
|
return {
|
||||||
|
type: 'bytes' + size,
|
||||||
|
value: function(extraSeed) {
|
||||||
|
var value = new Buffer(utils.randomBytes(seed + '-' + extraSeed + '-type-0-value', size));
|
||||||
|
return {
|
||||||
|
value: value,
|
||||||
|
normalized: value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// uint and int
|
||||||
|
case 1:
|
||||||
|
var signed = (utils.randomNumber(seed + '-type-1a', 0, 2) === 0);
|
||||||
|
var type = (!signed ? 'u': '') + 'int';
|
||||||
|
var size = 32;
|
||||||
|
if (utils.randomNumber(seed + '-type-1b', 0, 4) > 0) {
|
||||||
|
size = utils.randomNumber(seed + '-type-1c', 1, 33)
|
||||||
|
type += (8 * size);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: type,
|
||||||
|
value: function(extraSeed) {
|
||||||
|
var mask = '';
|
||||||
|
for (var i = 0; i < size; i++) { mask += 'ff'; }
|
||||||
|
|
||||||
|
var value = utils.randomNumber(seed + '-' + extraSeed + '-type-1d', -500, 1000);
|
||||||
|
var normalized = (new BN(value)).toTwos(size * 8).and(new BN(mask, 16));
|
||||||
|
if (signed) {
|
||||||
|
normalized = normalized.fromTwos(size * 8);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
value: value,
|
||||||
|
normalized: normalized
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// address
|
||||||
|
case 2:
|
||||||
|
return {
|
||||||
|
type: 'address',
|
||||||
|
value: function(extraSeed) {
|
||||||
|
var value = utils.randomHexString(seed + '-' + extraSeed + '-type-2', 20);
|
||||||
|
return {
|
||||||
|
value: value,
|
||||||
|
normalized: value
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// bool
|
||||||
|
case 3:
|
||||||
|
return {
|
||||||
|
type: 'bool',
|
||||||
|
value: function(extraSeed) {
|
||||||
|
var value = (utils.randomNumber(seed + '-' + extraSeed + '-type-3', 0, 2) === 0);
|
||||||
|
return {
|
||||||
|
value: value,
|
||||||
|
normalized: value
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixed-length array of subtype
|
||||||
|
case 4:
|
||||||
|
// @TODO: Support random(0, 6)... Why is that even possible?
|
||||||
|
var size = utils.randomNumber(seed + '-type-4a', 1, 6);
|
||||||
|
|
||||||
|
var subTypeValue = randomTypeValue(seed + '-type-4b', true);
|
||||||
|
return {
|
||||||
|
type: subTypeValue.type + '[' + size + ']',
|
||||||
|
value: function(extraSeed) {
|
||||||
|
var values = [];
|
||||||
|
var normalized = [];
|
||||||
|
for (var i = 0; i < size; i++) {
|
||||||
|
var value = subTypeValue.value(seed + '-' + extraSeed + '-4c-' + i);
|
||||||
|
values.push(value.value);
|
||||||
|
normalized.push(value.normalized);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
value: values,
|
||||||
|
normalized: normalized
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// bytes
|
||||||
|
case 5:
|
||||||
|
return {
|
||||||
|
type: 'bytes',
|
||||||
|
value: function(extraSeed) {
|
||||||
|
var size = utils.randomNumber(seed + '-type-5b', 0, 100);
|
||||||
|
var value = new Buffer(utils.randomBytes(seed + '-' + extraSeed + '-type-5a', size));
|
||||||
|
return {
|
||||||
|
value: value,
|
||||||
|
normalized: value
|
||||||
|
};
|
||||||
|
},
|
||||||
|
skip: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// string
|
||||||
|
case 6:
|
||||||
|
var text = 'abcdefghijklmnopqrstuvwxyz\u2014ABCDEFGHIJKLMNOPQRSTUVWXYZFOOBARfoobar'
|
||||||
|
return {
|
||||||
|
type: 'string',
|
||||||
|
value: function(extraSeed) {
|
||||||
|
var size = utils.randomNumber(seed + '-' + extraSeed + '-type-6', 0, 60);
|
||||||
|
var value = text.substring(0, size);
|
||||||
|
return {
|
||||||
|
value: value,
|
||||||
|
normalized: value
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// variable-sized array of subtype
|
||||||
|
case 7:
|
||||||
|
// @TODO: bug in solidity or VM prevents this from being 0
|
||||||
|
var size = utils.randomNumber(seed + '-type-7a', 1, 6);
|
||||||
|
var subTypeValue = randomTypeValue(seed + '-type-7b', true);
|
||||||
|
return {
|
||||||
|
type: subTypeValue.type + '[]',
|
||||||
|
value: function(extraSeed) {
|
||||||
|
var values = [];
|
||||||
|
var normalized = [];
|
||||||
|
for (var i = 0; i < size; i++) {
|
||||||
|
var value = subTypeValue.value(seed + '-' + extraSeed + '-7c-' + i);
|
||||||
|
values.push(value.value);
|
||||||
|
normalized.push(value.normalized);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
value: values,
|
||||||
|
normalized: normalized
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// @TODO: Test 0 arguments
|
||||||
|
|
||||||
|
// Create a bunch of random test cases
|
||||||
|
for (var i = 0; i < 2000; i++) {
|
||||||
|
var count = utils.randomNumber('count-' + i, 1, 4);
|
||||||
|
var types = [], values = [], normalized = [];;
|
||||||
|
for (var j = 0; j < count; j++) {
|
||||||
|
var type = randomTypeValue('type-' + i + '-' + j);
|
||||||
|
types.push(type.type);
|
||||||
|
var value = type.value();
|
||||||
|
values.push(value.value);
|
||||||
|
normalized.push(value.normalized);
|
||||||
|
}
|
||||||
|
check('random-' + i, types, values, normalized);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bug in solidity or in the VM, not sure, but this fails
|
||||||
|
// check('', ['uint8[4][]'], [ [] ]);
|
||||||
|
|
||||||
|
promiseRationing.all(promiseFuncs, 100).then(function(result) {
|
||||||
|
utils.saveTestcase('contract-interface', result);
|
||||||
|
}, function(error) {
|
||||||
|
console.log('ERROR', error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
makeTests();
|
111
tests/make-tests/make-hdnode.js
Normal file
111
tests/make-tests/make-hdnode.js
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var bip39 = require('bip39');
|
||||||
|
var HDNode = require('bitcoinjs-lib').HDNode;
|
||||||
|
var ethereumUtil = require('ethereumjs-util');
|
||||||
|
|
||||||
|
var utils = require('./utils.js');
|
||||||
|
|
||||||
|
function getPath(seed) {
|
||||||
|
var path = 'm';
|
||||||
|
|
||||||
|
var count = utils.randomNumber(seed + '-getPath-1', 1, 7);
|
||||||
|
var hardened = utils.randomNumber(seed + '-getPath-2', 0, count + 2);
|
||||||
|
for (var i = 0; i < count; i++) {
|
||||||
|
path += '/' + utils.randomNumber(seed + '-getPath-3' + i, 0, 12);
|
||||||
|
if (i < hardened) {
|
||||||
|
path += "'";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPrivateKey() {
|
||||||
|
}
|
||||||
|
|
||||||
|
function getHD(seed) {
|
||||||
|
var rootNode = HDNode.fromSeedHex(seed);
|
||||||
|
|
||||||
|
var privateKey = rootNode.keyPair.d.toBuffer(32);
|
||||||
|
var hdnodes = [{
|
||||||
|
path: 'm',
|
||||||
|
privateKey: '0x' + privateKey.toString('hex'),
|
||||||
|
address: '0x' + ethereumUtil.privateToAddress(privateKey).toString('hex'),
|
||||||
|
}];
|
||||||
|
|
||||||
|
for (var j = 0; j < 5; j++) {
|
||||||
|
var path = getPath(seed + '-hdnode-' + i + '-' + j);
|
||||||
|
var node = rootNode.derivePath(path);
|
||||||
|
var privateKey = node.keyPair.d.toBuffer(32);
|
||||||
|
hdnodes.push({
|
||||||
|
path: path,
|
||||||
|
privateKey: '0x' + privateKey.toString('hex'),
|
||||||
|
address: '0x' + ethereumUtil.privateToAddress(privateKey).toString('hex'),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return hdnodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
var Testcases = {};
|
||||||
|
|
||||||
|
var trezor = require('./test-mnemonics/tests-trezor-bip39.json');
|
||||||
|
trezor.english.forEach(function(testcase, i) {
|
||||||
|
Testcases['trezor-' + i] = {
|
||||||
|
entropy: '0x' + testcase[0],
|
||||||
|
mnemonic: testcase[1],
|
||||||
|
seed: '0x' + testcase[2],
|
||||||
|
hdnodes: getHD(testcase[2]),
|
||||||
|
password: 'TREZOR',
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
for (var i = 0; i < 1000; i++) {
|
||||||
|
var strength = 16 + 4 * utils.randomNumber('random-1-' + i, 0, 5);
|
||||||
|
var entropy = utils.randomHexString('random-2-' + i, strength);
|
||||||
|
|
||||||
|
var mnemonic = bip39.entropyToMnemonic(entropy.substring(2));
|
||||||
|
var seed = bip39.mnemonicToSeedHex(mnemonic);
|
||||||
|
|
||||||
|
Testcases['random-' + i] = {
|
||||||
|
entropy: entropy,
|
||||||
|
mnemonic: mnemonic,
|
||||||
|
seed: '0x' + seed,
|
||||||
|
hdnodes: getHD(seed),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
var seed = bip39.mnemonicToSeedHex('radar blur cabbage chef fix engine embark joy scheme fiction master release');
|
||||||
|
console.log('Seed', seed);
|
||||||
|
var entropy = bip39.mnemonicToEntropy('radar blur cabbage chef fix engine embark joy scheme fiction master release');
|
||||||
|
console.log('Entropy', entropy);
|
||||||
|
var rootNode = HDNode.fromSeedHex(seed);
|
||||||
|
var node = rootNode.derivePath("m/44'/60'/0'/0/0");
|
||||||
|
console.log('PrivateKey', node.keyPair.d.toBuffer(32).toString('hex')),
|
||||||
|
*/
|
||||||
|
|
||||||
|
// https://medium.com/@alexberegszaszi/why-do-my-bip32-wallets-disagree-6f3254cc5846#.6tqszlvf4
|
||||||
|
Testcases['axic'] = {
|
||||||
|
entropy: '0xb0a30c7e93a58094d213c4c0aaba22da',
|
||||||
|
mnemonic: 'radar blur cabbage chef fix engine embark joy scheme fiction master release',
|
||||||
|
seed: '0xed37b3442b3d550d0fbb6f01f20aac041c245d4911e13452cac7b1676a070eda66771b71c0083b34cc57ca9c327c459a0ec3600dbaf7f238ff27626c8430a806',
|
||||||
|
hdnodes: [
|
||||||
|
{
|
||||||
|
path: "m/44'/60'/0'/0/0",
|
||||||
|
address: '0xac39b311dceb2a4b2f5d8461c1cdaf756f4f7ae9',
|
||||||
|
privateKey: '0xb96e9ccb774cc33213cbcb2c69d3cdae17b0fe4888a1ccd343cbd1a17fd98b18',
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
var Output = [];
|
||||||
|
var TestcaseNames = Object.keys(Testcases);
|
||||||
|
TestcaseNames.sort();
|
||||||
|
TestcaseNames.forEach(function(testcase) {
|
||||||
|
Testcases[testcase].name = testcase;
|
||||||
|
Output.push(Testcases[testcase]);
|
||||||
|
});
|
||||||
|
|
||||||
|
utils.saveTestcase('hdnode', Output);
|
78
tests/make-tests/make-rlpcoder.js
Normal file
78
tests/make-tests/make-rlpcoder.js
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var rlp = require('rlp');
|
||||||
|
|
||||||
|
var utils = require('./utils.js');
|
||||||
|
|
||||||
|
var nullBuffer = new Buffer('');
|
||||||
|
var shortBuffer = new Buffer('Hello World');
|
||||||
|
var longBuffer = new Buffer('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas aliquet dolor nulla, nec tincidunt massa mollis at. In mollis blandit dui, id elementum eros iaculis ut. Phasellus lobortis, ipsum quis fermentum mollis, eros nisl rutrum dui, ut luctus leo turpis ut augue. Fusce odio turpis, pharetra at venenatis in, convallis quis nibh. Duis auctor, augue sit amet venenatis vulputate, nisl nibh feugiat mauris, id molestie augue dui sed justo. Suspendisse ipsum mauris, sagittis nec laoreet non, egestas vel nibh. Pellentesque aliquet accumsan velit in dapibus. Aenean eget augue arcu. Ut mollis leo mi, eu luctus eros facilisis eu. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse efficitur, justo a volutpat tempor, nibh ligula finibus turpis, eu facilisis tortor velit nec velit. Duis nec tempor lectus, non convallis sem.');
|
||||||
|
|
||||||
|
var singleLow = new Buffer([0x02]);
|
||||||
|
var singleLessMed = new Buffer([0x7e]);
|
||||||
|
var singleMed = new Buffer([0x7f]);
|
||||||
|
var singleMoreMed = new Buffer([0x80]);
|
||||||
|
var singleHigh = new Buffer([0xff]);
|
||||||
|
|
||||||
|
var Tests = {
|
||||||
|
nullString: nullBuffer,
|
||||||
|
emptyArray: [],
|
||||||
|
arrayWithNullString: [nullBuffer],
|
||||||
|
arrayWithNullString3: [nullBuffer, nullBuffer, nullBuffer],
|
||||||
|
threeSet: [ [], [[]], [[[]]] ],
|
||||||
|
arrayShort2: [shortBuffer, shortBuffer],
|
||||||
|
arrayLong2: [shortBuffer, shortBuffer],
|
||||||
|
arrayShortLong: [shortBuffer, longBuffer],
|
||||||
|
arrayInside: [shortBuffer, [shortBuffer, longBuffer, [shortBuffer, [shortBuffer]]], shortBuffer],
|
||||||
|
singleLow: singleLow,
|
||||||
|
singleLessMed: singleLessMed,
|
||||||
|
singleMed: singleMed,
|
||||||
|
singleMoreMed: singleMoreMed,
|
||||||
|
singleHigh: singleHigh,
|
||||||
|
assortedSingle1: [singleLow, singleMed, singleMoreMed, singleHigh, [singleLessMed, singleLow]],
|
||||||
|
assortedSingle2: [[singleLow, singleLow], [singleHigh, singleHigh, singleHigh]],
|
||||||
|
assorted: [[longBuffer], [singleMoreMed], singleLow, [singleLessMed], [[shortBuffer], [singleHigh]]],
|
||||||
|
}
|
||||||
|
|
||||||
|
function repeated(text, count) {
|
||||||
|
var result = '';
|
||||||
|
for (var i = 0; i < count; i++) {
|
||||||
|
result += text;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
[1, 2, 3, 4, 7, 8, 9, 15, 16, 17, 31, 32, 33, 53, 54, 55, 56, 57, 58, 100, 1000, 2049].forEach(function(i) {
|
||||||
|
Tests['zeros_' + i] = new Buffer(repeated('00', i), 'hex');
|
||||||
|
Tests['ones_' + i] = new Buffer(repeated('01', i), 'hex');
|
||||||
|
})
|
||||||
|
|
||||||
|
function toNestedHex(value) {
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
var result = [];
|
||||||
|
value.forEach(function(value) {
|
||||||
|
result.push(toNestedHex(value));
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
} else if (Buffer.isBuffer(value)) {
|
||||||
|
return utils.hexlify(value);
|
||||||
|
}
|
||||||
|
throw new Error('invalid object - ' + value);
|
||||||
|
}
|
||||||
|
|
||||||
|
var Output = [];
|
||||||
|
|
||||||
|
var testNames = Object.keys(Tests);
|
||||||
|
testNames.sort();
|
||||||
|
testNames.forEach(function(testName) {
|
||||||
|
var test = Tests[testName];
|
||||||
|
var encoded = rlp.encode(test);
|
||||||
|
Output.push({
|
||||||
|
name: testName,
|
||||||
|
decoded: toNestedHex(test),
|
||||||
|
encoded: '0x' + encoded.toString('hex')
|
||||||
|
});
|
||||||
|
});
|
||||||
|
console.log(Output);
|
||||||
|
|
||||||
|
utils.saveTestcase('rlp-coder', Output);
|
125
tests/make-tests/make-transactions.js
Normal file
125
tests/make-tests/make-transactions.js
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var ethereumUtil = require('ethereumjs-util');
|
||||||
|
var ethereumTx = require('ethereumjs-tx');
|
||||||
|
|
||||||
|
var utils = require('./utils.js');
|
||||||
|
|
||||||
|
|
||||||
|
var Output = [];
|
||||||
|
|
||||||
|
|
||||||
|
function addTransaction(privateKey, name, transaction, signature) {
|
||||||
|
var rawTransaction = new ethereumTx(transaction);
|
||||||
|
|
||||||
|
var rawTransactionEip155 = new ethereumTx(transaction);
|
||||||
|
rawTransactionEip155._chainId = 5;
|
||||||
|
|
||||||
|
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'),
|
||||||
|
}
|
||||||
|
|
||||||
|
rawTransaction.sign(privateKey);
|
||||||
|
test.signedTransaction = '0x' + rawTransaction.serialize().toString('hex');
|
||||||
|
|
||||||
|
rawTransactionEip155.sign(privateKey);
|
||||||
|
test.signedTransactionChainId5 = '0x' + rawTransactionEip155.serialize().toString('hex');
|
||||||
|
|
||||||
|
for (var k in transaction) {
|
||||||
|
test[k] = transaction[k];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var k in (signature || {})) {
|
||||||
|
test[k] = signature[k];
|
||||||
|
}
|
||||||
|
|
||||||
|
Output.push(test);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < 1000; i++) {
|
||||||
|
var transaction = {
|
||||||
|
to: utils.randomHexString('to-' + i, 20),
|
||||||
|
data: utils.randomHexString('data-' + i, 0, 10),
|
||||||
|
gasLimit: utils.randomHexString('gasLimit-' + i, 0, 10),
|
||||||
|
gasPrice: utils.randomHexString('gasPrice-' + i, 0, 10),
|
||||||
|
value: utils.randomHexString('value-' + i, 0, 10),
|
||||||
|
nonce: utils.randomHexString('nonce-' + i, 0, 4),
|
||||||
|
};
|
||||||
|
|
||||||
|
var privateKey = new Buffer(utils.randomBytes('privateKey-' + i, 32));
|
||||||
|
|
||||||
|
addTransaction(privateKey, 'random-' + i, transaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
// See: https://github.com/ethereumjs/ethereumjs-tx/blob/master/test/txs.json
|
||||||
|
addTransaction(new Buffer('164122e5d39e9814ca723a749253663bafb07f6af91704d9754c361eb315f0c1', 'hex'),
|
||||||
|
"ethereumjs1", {
|
||||||
|
nonce: "0x",
|
||||||
|
gasPrice: "0x09184e72a000",
|
||||||
|
gasLimit: "0x2710",
|
||||||
|
to: "0x0000000000000000000000000000000000000000",
|
||||||
|
value: "0x",
|
||||||
|
data: "0x7f7465737432000000000000000000000000000000000000000000000000000000600057",
|
||||||
|
}, {
|
||||||
|
v: "0x1c",
|
||||||
|
r: "0x5e1d3a76fbf824220eafc8c79ad578ad2b67d01b0c2425eb1f1347e8f50882ab",
|
||||||
|
s: "0x5bd428537f05f9830e93792f90ea6a3e2d1ee84952dd96edbae9f658f831ab13"
|
||||||
|
});
|
||||||
|
|
||||||
|
addTransaction(new Buffer('e0a462586887362a18a318b128dbc1e3a0cae6d4b0739f5d0419ec25114bc722', 'hex'),
|
||||||
|
"ethereumjs2", {
|
||||||
|
nonce: "0x06",
|
||||||
|
gasPrice: "0x09184e72a000",
|
||||||
|
gasLimit: "0x01f4",
|
||||||
|
to: "0xbe862ad9abfe6f22bcb087716c7d89a26051f74c",
|
||||||
|
value: "0x016345785d8a0000",
|
||||||
|
data: "0x",
|
||||||
|
}, {
|
||||||
|
v: "0x1c",
|
||||||
|
r: "0x24a484bfa7380860e9fa0a9f5e4b64b985e860ca31abd36e66583f9030c2e29d",
|
||||||
|
s: "0x4d5ef07d9e73fa2fbfdad059591b4f13d0aa79e7634a2bb00174c9200cabb04d"
|
||||||
|
});
|
||||||
|
|
||||||
|
addTransaction(new Buffer('164122e5d39e9814ca723a749253663bafb07f6af91704d9754c361eb315f0c1', 'hex'),
|
||||||
|
"ethereumjs3", {
|
||||||
|
nonce: "0x06",
|
||||||
|
gasPrice: "0x09184e72a000",
|
||||||
|
gasLimit: "0x0974",
|
||||||
|
to: "0xbe862ad9abfe6f22bcb087716c7d89a26051f74c",
|
||||||
|
value: "0x016345785d8a0000",
|
||||||
|
data: "0x00000000000000000000000000000000000000000000000000000000000000ad000000000000000000000000000000000000000000000000000000000000fafa0000000000000000000000000000000000000000000000000000000000000dfa0000000000000000000000000000000000000000000000000000000000000dfa00000000000000000000000000000000000000000000000000000000000000ad000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000df000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000df000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000d",
|
||||||
|
}, {
|
||||||
|
v: "0x1c",
|
||||||
|
r: "0x5e9361ca27e14f3af0e6b28466406ad8be026d3b0f2ae56e3c064043fb73ec77",
|
||||||
|
s: "0x29ae9893dac4f9afb1af743e25fbb6a63f7879a61437203cb48c997b0fcefc3a"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Test all possible blank fields
|
||||||
|
var privateKey = new Buffer('0123456789012345678901234567890123456789012345678901234567890123', 'hex');
|
||||||
|
for (var i = 0; i < 64; i++) {
|
||||||
|
var transaction = {};
|
||||||
|
if (i & (1 << 0)) { transaction.nonce = '0x02'; }
|
||||||
|
if (i & (1 << 1)) { transaction.gasPrice = '0x03'; }
|
||||||
|
if (i & (1 << 2)) { transaction.gasLimit = '0x04'; }
|
||||||
|
if (i & (1 << 3)) { transaction.to = '0x0123456789012345678901234567890123456789'; }
|
||||||
|
if (i & (1 << 4)) { transaction.value = '0x05'; }
|
||||||
|
if (i & (1 << 5)) { transaction.data = '0x06'; }
|
||||||
|
var bits = '';
|
||||||
|
for (var j = 0; j < 6; j++) { bits += ((i & (1 << j)) ? '1': '0'); }
|
||||||
|
addTransaction(privateKey, 'blank_' + bits, transaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
Output.sort(function(a, b) {
|
||||||
|
if (a.name < b.name) {
|
||||||
|
return -1;
|
||||||
|
} else if (a.name > b.name) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
utils.saveTestcase('transactions', Output);
|
58
tests/make-tests/make-wallets.js
Normal file
58
tests/make-tests/make-wallets.js
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var fs = require('fs');
|
||||||
|
var path = require('path');
|
||||||
|
|
||||||
|
var utils = require('./utils.js');
|
||||||
|
|
||||||
|
function prefixAddress(address) {
|
||||||
|
if (address.substring(0, 2) !== '0x') {
|
||||||
|
address = '0x' + address;
|
||||||
|
}
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var Output = [];
|
||||||
|
|
||||||
|
/*
|
||||||
|
Output.push({
|
||||||
|
type: 'brainwallet',
|
||||||
|
address: '0xbed9d2E41BdD066f702C4bDB86eB3A3740101acC',
|
||||||
|
password: 'password',
|
||||||
|
privateKey: '',
|
||||||
|
username: 'ricmoo',
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
|
var walletPath = path.join(__dirname, 'test-wallets');
|
||||||
|
fs.readdirSync(walletPath).forEach(function(filename) {
|
||||||
|
var data = require(path.join(walletPath, filename));
|
||||||
|
|
||||||
|
// The password is the last segment of the filename
|
||||||
|
var password = filename.substring(0, filename.length - 5).split('-');
|
||||||
|
password = password[password.length - 1];
|
||||||
|
|
||||||
|
if (password === 'life') { password = 'foobar42'; }
|
||||||
|
|
||||||
|
if (data.ethaddr) {
|
||||||
|
Output.push({
|
||||||
|
type: 'crowdsale',
|
||||||
|
address: prefixAddress(data.ethaddr),
|
||||||
|
json: JSON.stringify(data),
|
||||||
|
password: password,
|
||||||
|
privateKey: '',
|
||||||
|
});
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Output.push({
|
||||||
|
type: 'secret-storage',
|
||||||
|
address: prefixAddress(data.address),
|
||||||
|
json: JSON.stringify(data),
|
||||||
|
password: password,
|
||||||
|
privateKey: '',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
utils.saveTestcase('wallets', Output);
|
52
tests/make-tests/utils.js
Normal file
52
tests/make-tests/utils.js
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
|
||||||
|
var fs = require('fs');
|
||||||
|
var path = require('path');
|
||||||
|
|
||||||
|
var utils = require('ethers-utils');
|
||||||
|
|
||||||
|
function randomBytes(seed, lower, upper) {
|
||||||
|
if (!upper) { upper = lower; }
|
||||||
|
|
||||||
|
if (upper === 0 && upper === lower) { return new Uint8Array(0); }
|
||||||
|
|
||||||
|
seed = utils.toUtf8Bytes(seed);
|
||||||
|
|
||||||
|
var result = utils.arrayify(utils.keccak256(seed));
|
||||||
|
while (result.length < upper) {
|
||||||
|
result = utils.concat([result, utils.keccak256(utils.concat([seed, result]))]);
|
||||||
|
}
|
||||||
|
|
||||||
|
var top = utils.arrayify(utils.keccak256(result));
|
||||||
|
var percent = ((top[0] << 16) | (top[1] << 8) | top[2]) / 0x00ffffff;
|
||||||
|
|
||||||
|
return result.slice(0, lower + parseInt((upper - lower) * percent));
|
||||||
|
}
|
||||||
|
|
||||||
|
function randomHexString(seed, lower, upper) {
|
||||||
|
return utils.hexlify(randomBytes(seed, lower, upper));
|
||||||
|
}
|
||||||
|
|
||||||
|
function randomNumber(seed, lower, upper) {
|
||||||
|
var top = randomBytes(seed, 3);
|
||||||
|
var percent = ((top[0] << 16) | (top[1] << 8) | top[2]) / 0x00ffffff;
|
||||||
|
return lower + parseInt((upper - lower) * percent);
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveTestcase(testcaseName, json) {
|
||||||
|
var data = JSON.stringify(json, undefined, ' ');
|
||||||
|
var filename = path.join(__dirname, '../tests/', testcaseName + '.json');
|
||||||
|
fs.writeFileSync(filename, data);
|
||||||
|
|
||||||
|
console.log('Save testcase: ' + filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
randomBytes: randomBytes,
|
||||||
|
randomHexString: randomHexString,
|
||||||
|
randomNumber: randomNumber,
|
||||||
|
|
||||||
|
arrayify: utils.arrayify,
|
||||||
|
hexlify: utils.hexlify,
|
||||||
|
|
||||||
|
saveTestcase: saveTestcase,
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user