Skip to content

Commit

Permalink
Change Buffer constructor to safer operations (#265).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Sep 4, 2018
1 parent 32a070d commit 7aebe53
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src.ts/utils/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ declare class Buffer implements ArrayLike<number> {
}
*/
export function decode(textData: string): Uint8Array {
return arrayify(new Uint8Array(new Buffer(textData, 'base64')));
return arrayify(new Uint8Array(Buffer.from(textData, 'base64')));
};

export function encode(data: Arrayish): string {
return new Buffer(arrayify(data)).toString('base64');
return Buffer.from(arrayify(data)).toString('base64');
}
2 changes: 1 addition & 1 deletion src.ts/utils/hmac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export function computeHmac(algorithm: SupportedAlgorithms, key: Arrayish, data:
if (!SupportedAlgorithms[algorithm]) {
errors.throwError('unsupported algorithm ' + algorithm, errors.UNSUPPORTED_OPERATION, { operation: 'hmac', algorithm: algorithm });
}
return arrayify(createHmac(algorithm, new Buffer(arrayify(key))).update(new Buffer(arrayify(data))).digest());
return arrayify(createHmac(algorithm, Buffer.from(arrayify(key))).update(Buffer.from(arrayify(data))).digest());
}

2 changes: 1 addition & 1 deletion src.ts/utils/pbkdf2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { arrayify } from './bytes';
import { Arrayish } from './bytes';

function bufferify(value: Arrayish): Buffer {
return new Buffer(arrayify(value));
return Buffer.from(arrayify(value));
}

export function pbkdf2(password: Arrayish, salt: Arrayish, iterations: number, keylen: number, hashAlgorithm: string): Uint8Array {
Expand Down
6 changes: 3 additions & 3 deletions tests/make-tests/make-accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var Output = [];
]

Tests.forEach(function(privateKey) {
var privateKeyBuffer = new Buffer(privateKey.substring(2), 'hex');
var privateKeyBuffer = Buffer.from(privateKey.substring(2), 'hex');
var address = '0x' + ethereumUtil.privateToAddress(privateKeyBuffer).toString('hex');
Output.push({
address: address,
Expand Down Expand Up @@ -86,8 +86,8 @@ Output.push({

// Add 1024 random private keys (checks for nibble and byte padding bugs)
for (var i = 0; i < 1024; i++) {
var privateKey = new Buffer(utils.randomBytes('accounts-' + i, 32));
var address = '0x' + ethereumUtil.privateToAddress(new Buffer(privateKey)).toString('hex');
var privateKey = Buffer.from(utils.randomBytes('accounts-' + i, 32));
var address = '0x' + ethereumUtil.privateToAddress(Buffer.from(privateKey)).toString('hex');

Output.push({
address: address,
Expand Down
10 changes: 5 additions & 5 deletions tests/make-tests/make-contract-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function waitForTransaction(hash) {

// Create the indent given a tabstop
function indent(tabs) {
var indent = new Buffer(tabs * 4);
var indent = Buffer.alloc(tabs * 4);
indent.fill(32);
return indent.toString('utf8')
}
Expand Down Expand Up @@ -108,7 +108,7 @@ function createContractSource(test, comments) {
source += (indent(2) + 's' + index + ' = ' + (param.value ? 'true': 'false') + ';\n');

} else if (param.type === 'bytes') {
var value = new Buffer(param.value.substring(2), 'hex');
var value = Buffer.from(param.value.substring(2), 'hex');
source += indent(2) + 's' + index + ' = new bytes(' + value.length + ');\n';
source += indent(2) + 'assembly {\n';
source += indent(3) + 'mstore(s' + index + ', ' + value.length + ')\n';
Expand Down Expand Up @@ -506,10 +506,10 @@ function makeTests() {
if (param.indexed && isHashed(param.type)) {
hashed.push(true);
if (param.type === 'string') {
normalizedValues.push(keccak256(new Buffer(param.value, 'utf8')));
normalizedValues.push(keccak256(Buffer.from(param.value, 'utf8')));

} else if (param.type === 'bytes') {
normalizedValues.push(keccak256(new Buffer(param.value.substring(2), 'hex')));
normalizedValues.push(keccak256(Buffer.from(param.value.substring(2), 'hex')));

} else if (param.type.indexOf('[') >= 0) {
var compute = param.type;
Expand Down Expand Up @@ -549,7 +549,7 @@ function makeTests() {
// The web3 coder has lots of bugs, but it does fine as long as there
// is only one type and nothing is dynamic
var encoded = web3Coder.encodeParams([ compute ], [ web3Value ]);
normalizedValues.push(keccak256(new Buffer(encoded, 'hex')));
normalizedValues.push(keccak256(Buffer.from(encoded, 'hex')));

} else {
throw new Error('unknown hashed type');
Expand Down
32 changes: 16 additions & 16 deletions tests/make-tests/make-contract-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var compile = (function() {

// Create the indent given a tabstop
function indent(tabs) {
var indent = new Buffer(tabs * 4);
var indent = Buffer.alloc(tabs * 4);
indent.fill(32);
return indent.toString('utf8')
}
Expand Down Expand Up @@ -484,11 +484,11 @@ function makeTests() {

check('sol-25',
['bytes32'],
[new Buffer('6761766f66796f726b0000000000000000000000000000000000000000000000', 'hex')]
[Buffer.from('6761766f66796f726b0000000000000000000000000000000000000000000000', 'hex')]
);
check('sol-26',
['bytes'],
[new Buffer('6761766f66796f726b', 'hex')]
[Buffer.from('6761766f66796f726b', 'hex')]
);

check('sol-27',
Expand All @@ -502,35 +502,35 @@ function makeTests() {
getAddress("0x97916ef549947a3e0d321485a31dd2715a97d455"),
"foobar2",
[
new Buffer("a165ab0173c6", 'hex'),
new Buffer("f0f37bee9244", 'hex'),
new Buffer("c8dc0bf08d2b", 'hex'),
new Buffer("c8dc0bf08d2b", 'hex')
Buffer.from("a165ab0173c6", 'hex'),
Buffer.from("f0f37bee9244", 'hex'),
Buffer.from("c8dc0bf08d2b", 'hex'),
Buffer.from("c8dc0bf08d2b", 'hex')
],
34
]
);

check('sol-29',
['bytes32'],
[new Buffer('731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 'hex')]
[Buffer.from('731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 'hex')]
);
check('sol-30',
['bytes'],
[new Buffer('731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 'hex')]
[Buffer.from('731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 'hex')]
);

check('sol-31',
['bytes32[2]'],
[[
new Buffer('731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 'hex'),
new Buffer('731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 'hex')
Buffer.from('731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 'hex'),
Buffer.from('731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 'hex')
]]
);

check('sol-32',
['bytes'],
[new Buffer('131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
[Buffer.from('131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', 'hex')]
);
Expand Down Expand Up @@ -562,7 +562,7 @@ function makeTests() {
return {
type: 'bytes' + size,
value: function(extraSeed) {
var value = new Buffer(utils.randomBytes(seed + '-' + extraSeed + '-type-0-value', size));
var value = Buffer.from(utils.randomBytes(seed + '-' + extraSeed + '-type-0-value', size));
return {
value: value,
normalized: value
Expand Down Expand Up @@ -653,7 +653,7 @@ function makeTests() {
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));
var value = Buffer.from(utils.randomBytes(seed + '-' + extraSeed + '-type-5a', size));
return {
value: value,
normalized: value
Expand Down Expand Up @@ -920,7 +920,7 @@ function makeTestsAbi2() {
return {
type: 'bytes',
name: 'bytes',
value: new Buffer(utils.randomBytes(seed + '-bytes-' + extra, 0, 64))
value: Buffer.from(utils.randomBytes(seed + '-bytes-' + extra, 0, 64))
}
}
};
Expand All @@ -945,7 +945,7 @@ function makeTestsAbi2() {
return {
type: 'bytes' + String(count),
name: 'bytes' + String(count),
value: new Buffer(utils.randomBytes(seed + '-bytes-value-' + extra, count, count))
value: Buffer.from(utils.randomBytes(seed + '-bytes-value-' + extra, count, count))
};
}
};
Expand Down
10 changes: 5 additions & 5 deletions tests/make-tests/make-hashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ function add(data) {
});
}

add(new Buffer([ ]));
add(new Buffer([ 0 ]));
add(new Buffer([ 1 ]));
add(new Buffer([ 0, 1 ]));
add(Buffer.from([ ]));
add(Buffer.from([ 0 ]));
add(Buffer.from([ 1 ]));
add(Buffer.from([ 0, 1 ]));

for (var i = 0; i < 512; i++) {
var data = new Buffer(utils.randomBytes('data-' + i, 1, 128));
var data = Buffer.from(utils.randomBytes('data-' + i, 1, 128));
add(data);
}

Expand Down
20 changes: 10 additions & 10 deletions tests/make-tests/make-rlpcoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ 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 nullBuffer = Buffer.from('');
var shortBuffer = Buffer.from('Hello World');
var longBuffer = Buffer.from('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 singleLow = Buffer.from([0x02]);
var singleLessMed = Buffer.from([0x7e]);
var singleMed = Buffer.from([0x7f]);
var singleMoreMed = Buffer.from([0x80]);
var singleHigh = Buffer.from([0xff]);

var Tests = {
nullString: nullBuffer,
Expand Down Expand Up @@ -43,8 +43,8 @@ function repeated(text, count) {
}

[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');
Tests['zeros_' + i] = Buffer.from(repeated('00', i), 'hex');
Tests['ones_' + i] = Buffer.from(repeated('01', i), 'hex');
})

function toNestedHex(value) {
Expand Down
10 changes: 5 additions & 5 deletions tests/make-tests/make-transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ for (var i = 0; i < 1000; i++) {
nonce: trimHex(utils.randomHexString('nonce-' + i, 0, 4)),
};

var privateKey = new Buffer(utils.randomBytes('privateKey-' + i, 32));
var privateKey = Buffer.from(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'),
addTransaction(Buffer.from('164122e5d39e9814ca723a749253663bafb07f6af91704d9754c361eb315f0c1', 'hex'),
"ethereumjs1", {
nonce: "0x",
gasPrice: "0x09184e72a000",
Expand All @@ -100,7 +100,7 @@ addTransaction(new Buffer('164122e5d39e9814ca723a749253663bafb07f6af91704d9754c3
s: "0x5bd428537f05f9830e93792f90ea6a3e2d1ee84952dd96edbae9f658f831ab13"
});

addTransaction(new Buffer('e0a462586887362a18a318b128dbc1e3a0cae6d4b0739f5d0419ec25114bc722', 'hex'),
addTransaction(Buffer.from('e0a462586887362a18a318b128dbc1e3a0cae6d4b0739f5d0419ec25114bc722', 'hex'),
"ethereumjs2", {
nonce: "0x06",
gasPrice: "0x09184e72a000",
Expand All @@ -114,7 +114,7 @@ addTransaction(new Buffer('e0a462586887362a18a318b128dbc1e3a0cae6d4b0739f5d0419e
s: "0x4d5ef07d9e73fa2fbfdad059591b4f13d0aa79e7634a2bb00174c9200cabb04d"
});

addTransaction(new Buffer('164122e5d39e9814ca723a749253663bafb07f6af91704d9754c361eb315f0c1', 'hex'),
addTransaction(Buffer.from('164122e5d39e9814ca723a749253663bafb07f6af91704d9754c361eb315f0c1', 'hex'),
"ethereumjs3", {
nonce: "0x06",
gasPrice: "0x09184e72a000",
Expand All @@ -129,7 +129,7 @@ addTransaction(new Buffer('164122e5d39e9814ca723a749253663bafb07f6af91704d9754c3
});

// Test all possible blank fields
var privateKey = new Buffer('0123456789012345678901234567890123456789012345678901234567890123', 'hex');
var privateKey = Buffer.from('0123456789012345678901234567890123456789012345678901234567890123', 'hex');
for (var i = 0; i < 64; i++) {
var transaction = {};
if (i & (1 << 0)) { transaction.nonce = '0x02'; }
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if (!readFileSync) {
var data = require('./dist/tests.json');

readFileSync = function(filename) {
return new Buffer(data[filename], 'base64');
return Buffer.from(data[filename], 'base64');
}
}

Expand Down

0 comments on commit 7aebe53

Please sign in to comment.