Skip to content

Commit

Permalink
Merge ac03b18 into 5a53ae9
Browse files Browse the repository at this point in the history
  • Loading branch information
nivida committed Jan 22, 2020
2 parents 5a53ae9 + ac03b18 commit 952c052
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 54 deletions.
14 changes: 12 additions & 2 deletions packages/web3-eth-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ Accounts.prototype.create = function create(entropy) {

Accounts.prototype.privateKeyToAccount = function privateKeyToAccount(privateKey) {
if (!privateKey.startsWith('0x')) {
throw new Error('Required prefix "0x" is missing.');
privateKey = '0x' + privateKey;
}

// 64 hex characters + hex-prefix
if (privateKey.length !== 66) {
throw new Error("Private key must be 32 bytes long");
}

return this._addAccountFunctions(Account.fromPrivate(privateKey));
Expand Down Expand Up @@ -299,7 +304,12 @@ Accounts.prototype.hashMessage = function hashMessage(data) {

Accounts.prototype.sign = function sign(data, privateKey) {
if (!privateKey.startsWith('0x')) {
throw new Error('Required prefix "0x" is missing for the given private key.');
privateKey = '0x' + privateKey;
}

// 64 hex characters + hex-prefix
if (privateKey.length !== 66) {
throw new Error("Private key must be 32 bytes long");
}

var hash = this.hashMessage(data);
Expand Down
46 changes: 0 additions & 46 deletions test/eth.accounts.encrypt-decrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,52 +63,6 @@ var staticTests = [{
},
"password": "testpassword",
"priv": "7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d"
}, {
"json": {
"crypto" : {
"cipher" : "aes-128-ctr",
"cipherparams" : {
"iv" : "e0c41130a323adc1446fc82f724bca2f"
},
"ciphertext" : "9517cd5bdbe69076f9bf5057248c6c050141e970efa36ce53692d5d59a3984",
"kdf" : "scrypt",
"kdfparams" : {
"dklen" : 32,
"n" : 2,
"r" : 8,
"p" : 1,
"salt" : "711f816911c92d649fb4c84b047915679933555030b3552c1212609b38208c63"
},
"mac" : "d5e116151c6aa71470e67a7d42c9620c75c4d23229847dcc127794f0732b0db5"
},
"id" : "fecfc4ce-e956-48fd-953b-30f8b52ed66c",
"version" : 3
},
"password": "foo",
"priv": "fa7b3db73dc7dfdf8c5fbdb796d741e4488628c41fc4febd9160a866ba0f35"
},{
"json": {
"crypto" : {
"cipher" : "aes-128-ctr",
"cipherparams" : {
"iv" : "3ca92af36ad7c2cd92454c59cea5ef00"
},
"ciphertext" : "108b7d34f3442fc26ab1ab90ca91476ba6bfa8c00975a49ef9051dc675aa",
"kdf" : "scrypt",
"kdfparams" : {
"dklen" : 32,
"n" : 2,
"r" : 8,
"p" : 1,
"salt" : "d0769e608fb86cda848065642a9c6fa046845c928175662b8e356c77f914cd3b"
},
"mac" : "75d0e6759f7b3cefa319c3be41680ab6beea7d8328653474bd06706d4cc67420"
},
"id" : "a37e1559-5955-450d-8075-7b8931b392b2",
"version" : 3
},
"password": "foo",
"priv": "81c29e8142bb6a81bef5a92bda7a8328a5c85bb2f9542e76f9b0f94fc018"
}];

describe("eth", function () {
Expand Down
26 changes: 20 additions & 6 deletions test/eth.accounts.sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,35 @@ describe("eth", function () {
});
});

it('should throw an error if a PK got passed to Accounts.sign without a "0x" prefix', function () {
it('should add the "0x" prefix and sign the given message correctly', function() {
assert.equal(
'0xa8037a6116c176a25e6fc224947fde9e79a2deaa0dd8b67b366fbdfdbffc01f953e41351267b20d4a89ebfe9c8f03c04de9b345add4a52f15bd026b63c8fb1501b',
new Accounts().sign('Some data', 'be6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728').signature
);
});

it('should add the "0x" prefix to the privateKey', function() {
assert.equal(
'0xbe6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728',
new Accounts().privateKeyToAccount('be6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728').privateKey
);
});

it('should throw if a privateKey is given with a invalid length', function() {
try {
new Accounts().sign('DATA', 'be6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728');
new Accounts().privateKeyToAccount('0000be6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728');
assert.fail();
} catch(err) {
assert(err.message.includes('Required prefix "0x" is missing for the given private key.'));
assert(err.message.includes('Private key must be 32 bytes long'));
}
});

it('should throw an error if a PK got passed to Accounts.privateKeyToAccount without a "0x" prefix', function () {
it('should throw if a privateKey is given with a invalid length', function() {
try {
new Accounts().privateKeyToAccount('be6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728');
new Accounts().sign('data', '00be6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728');
assert.fail();
} catch(err) {
assert(err.message.includes('Required prefix "0x" is missing.'));
assert(err.message.includes('Private key must be 32 bytes long'));
}
});
});

0 comments on commit 952c052

Please sign in to comment.