2021-02-04 19:15:23 +01:00
|
|
|
import assert from 'assert';
|
2021-04-16 17:05:13 +02:00
|
|
|
import { stripHexPrefix } from 'ethereumjs-util';
|
2021-03-16 22:00:08 +01:00
|
|
|
import accountImporter from '.';
|
2018-05-09 10:51:55 +02:00
|
|
|
|
|
|
|
describe('Account Import Strategies', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const privkey =
|
2021-02-04 19:15:23 +01:00
|
|
|
'0x4cfd3e90fc78b0f86bf7524722150bb8da9c60cd532564d7ff43f5716514f553';
|
2020-11-03 00:41:28 +01:00
|
|
|
const json =
|
2021-02-04 19:15:23 +01:00
|
|
|
'{"version":3,"id":"dbb54385-0a99-437f-83c0-647de9f244c3","address":"a7f92ce3fba24196cf6f4bd2e1eb3db282ba998c","Crypto":{"ciphertext":"bde13d9ade5c82df80281ca363320ce254a8a3a06535bbf6ffdeaf0726b1312c","cipherparams":{"iv":"fbf93718a57f26051b292f072f2e5b41"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"7ffe00488319dec48e4c49a120ca49c6afbde9272854c64d9541c83fc6acdffe","n":8192,"r":8,"p":1},"mac":"2adfd9c4bc1cdac4c85bddfb31d9e21a684e0e050247a70c5698facf6b7d4681"}}';
|
2018-05-09 10:51:55 +02:00
|
|
|
|
2018-06-11 23:58:05 +02:00
|
|
|
describe('private key import', function () {
|
2018-06-07 19:54:19 +02:00
|
|
|
it('imports a private key and strips 0x prefix', async function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const importPrivKey = await accountImporter.importAccount('Private Key', [
|
|
|
|
privkey,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2021-04-16 17:05:13 +02:00
|
|
|
assert.equal(importPrivKey, stripHexPrefix(privkey));
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-06-07 22:34:10 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('throws an error for empty string private key', async function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
await assert.rejects(
|
|
|
|
async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
await accountImporter.importAccount('Private Key', ['']);
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
Error,
|
|
|
|
'no empty strings',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-06-07 22:34:10 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('throws an error for undefined string private key', async function () {
|
|
|
|
await assert.rejects(async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
await accountImporter.importAccount('Private Key', [undefined]);
|
|
|
|
});
|
2018-06-07 22:34:10 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
await assert.rejects(async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
await accountImporter.importAccount('Private Key', []);
|
|
|
|
});
|
|
|
|
});
|
2018-06-07 22:34:10 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('throws an error for invalid private key', async function () {
|
|
|
|
await assert.rejects(async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
await accountImporter.importAccount('Private Key', ['popcorn']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-05-09 10:51:55 +02:00
|
|
|
|
2018-06-07 19:54:19 +02:00
|
|
|
describe('JSON keystore import', function () {
|
|
|
|
it('fails when password is incorrect for keystore', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const wrongPassword = 'password2';
|
2018-05-09 10:51:55 +02:00
|
|
|
|
2018-06-07 19:54:19 +02:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
await accountImporter.importAccount('JSON File', [json, wrongPassword]);
|
2018-06-07 19:54:19 +02:00
|
|
|
} catch (error) {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
error.message,
|
|
|
|
'Key derivation failed - possibly wrong passphrase',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-06-07 19:54:19 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-05-09 10:51:55 +02:00
|
|
|
|
2018-06-07 19:54:19 +02:00
|
|
|
it('imports json string and password to return a private key', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const fileContentsPassword = 'password1';
|
2020-11-03 00:41:28 +01:00
|
|
|
const importJson = await accountImporter.importAccount('JSON File', [
|
|
|
|
json,
|
|
|
|
fileContentsPassword,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
importJson,
|
|
|
|
'0x5733876abe94146069ce8bcbabbde2677f2e35fa33e875e92041ed2ac87e5bc7',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|