Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use Buffer.from instead of new Buffer
  • Loading branch information
noelyoo committed Oct 10, 2018
commit 65dc0c3c4bde1ae0c5d18e6e19e4dc1a70216241
2 changes: 1 addition & 1 deletion docs/_build/html/_sources/web3-eth.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ Example
.. code-block:: javascript

var Tx = require('ethereumjs-tx');
var privateKey = new Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')
var privateKey = Buffer.from('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')

var rawTx = {
nonce: '0x00',
Expand Down
2 changes: 1 addition & 1 deletion docs/web3-eth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ Example
.. code-block:: javascript

var Tx = require('ethereumjs-tx');
var privateKey = new Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')
var privateKey = Buffer.from('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')

var rawTx = {
nonce: '0x00',
Expand Down
16 changes: 8 additions & 8 deletions packages/web3-eth-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,27 +307,27 @@ Accounts.prototype.decrypt = function (v3Keystore, password, nonStrict) {
kdfparams = json.crypto.kdfparams;

// FIXME: support progress reporting callback
derivedKey = scryptsy(new Buffer(password), new Buffer(kdfparams.salt, 'hex'), kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
derivedKey = scryptsy(Buffer.from(password), Buffer.from(kdfparams.salt, 'hex'), kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
} else if (json.crypto.kdf === 'pbkdf2') {
kdfparams = json.crypto.kdfparams;

if (kdfparams.prf !== 'hmac-sha256') {
throw new Error('Unsupported parameters to PBKDF2');
}

derivedKey = cryp.pbkdf2Sync(new Buffer(password), new Buffer(kdfparams.salt, 'hex'), kdfparams.c, kdfparams.dklen, 'sha256');
derivedKey = cryp.pbkdf2Sync(Buffer.from(password), Buffer.from(kdfparams.salt, 'hex'), kdfparams.c, kdfparams.dklen, 'sha256');
} else {
throw new Error('Unsupported key derivation scheme');
}

var ciphertext = new Buffer(json.crypto.ciphertext, 'hex');
var ciphertext = Buffer.from(json.crypto.ciphertext, 'hex');

var mac = utils.sha3(Buffer.concat([ derivedKey.slice(16, 32), ciphertext ])).replace('0x','');
if (mac !== json.crypto.mac) {
throw new Error('Key derivation failed - possibly wrong password');
}

var decipher = cryp.createDecipheriv(json.crypto.cipher, derivedKey.slice(0, 16), new Buffer(json.crypto.cipherparams.iv, 'hex'));
var decipher = cryp.createDecipheriv(json.crypto.cipher, derivedKey.slice(0, 16), Buffer.from(json.crypto.cipherparams.iv, 'hex'));
var seed = '0x'+ Buffer.concat([ decipher.update(ciphertext), decipher.final() ]).toString('hex');

return this.privateKeyToAccount(seed);
Expand All @@ -351,13 +351,13 @@ Accounts.prototype.encrypt = function (privateKey, password, options) {
if (kdf === 'pbkdf2') {
kdfparams.c = options.c || 262144;
kdfparams.prf = 'hmac-sha256';
derivedKey = cryp.pbkdf2Sync(new Buffer(password), salt, kdfparams.c, kdfparams.dklen, 'sha256');
derivedKey = cryp.pbkdf2Sync(Buffer.from(password), salt, kdfparams.c, kdfparams.dklen, 'sha256');
} else if (kdf === 'scrypt') {
// FIXME: support progress reporting callback
kdfparams.n = options.n || 8192; // 2048 4096 8192 16384
kdfparams.r = options.r || 8;
kdfparams.p = options.p || 1;
derivedKey = scryptsy(new Buffer(password), salt, kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
derivedKey = scryptsy(Buffer.from(password), salt, kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
} else {
throw new Error('Unsupported kdf');
}
Expand All @@ -367,9 +367,9 @@ Accounts.prototype.encrypt = function (privateKey, password, options) {
throw new Error('Unsupported cipher');
}

var ciphertext = Buffer.concat([ cipher.update(new Buffer(account.privateKey.replace('0x',''), 'hex')), cipher.final() ]);
var ciphertext = Buffer.concat([ cipher.update(Buffer.from(account.privateKey.replace('0x',''), 'hex')), cipher.final() ]);

var mac = utils.sha3(Buffer.concat([ derivedKey.slice(16, 32), new Buffer(ciphertext, 'hex') ])).replace('0x','');
var mac = utils.sha3(Buffer.concat([ derivedKey.slice(16, 32), Buffer.from(ciphertext, 'hex') ])).replace('0x','');

return {
version: 3,
Expand Down
2 changes: 1 addition & 1 deletion test/eth.accounts.create.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("eth", function () {
var acc = ethAccounts.create();

// create ethereumjs-wallet account
var ethWall = ethereumWallet.fromPrivateKey(new Buffer(acc.privateKey.replace('0x',''),'hex'));
var ethWall = ethereumWallet.fromPrivateKey(Buffer.from(acc.privateKey.replace('0x',''),'hex'));

// compare addresses
assert.equal(acc.address, ethWall.getChecksumAddressString());
Expand Down
6 changes: 3 additions & 3 deletions test/eth.accounts.encrypt-decrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ for (var i = 0; i < 50; i++) {
}
var n = 256;
var salt = '3a1012583f8be138537bc7cf8a50c925b6fcc01a9f7744c85a18fbdc07999f10';
var iv = new Buffer('653195c3e2791ac53f3f19b125c18f8c', 'hex');
var uuid = new Buffer('ff31ddc3e2791ac53f3f19b125c18fff', 'hex');
var iv = Buffer.from('653195c3e2791ac53f3f19b125c18f8c', 'hex');
var uuid = Buffer.from('ff31ddc3e2791ac53f3f19b125c18fff', 'hex');
var pw = 'test';

// tests from https://github.com/Gustav-Simonsson/go-ethereum/blob/7cc6b801e0967e5ebfa26b9f670675acea6e3a20/accounts/testdata/v3_test_vector.json
Expand Down Expand Up @@ -120,7 +120,7 @@ describe("eth", function () {
var acc = ethAccounts.create();

// create ethereumjs-wallet account
var ethWall = ethereumWallet.fromPrivateKey(new Buffer(acc.privateKey.replace('0x',''),'hex'));
var ethWall = ethereumWallet.fromPrivateKey(Buffer.from(acc.privateKey.replace('0x',''),'hex'));

// compare addresses
assert.equal(acc.address, ethWall.getChecksumAddressString());
Expand Down