Skip to content

Commit

Permalink
test: fix load keystore test case
Browse files Browse the repository at this point in the history
  • Loading branch information
twhy committed Jun 13, 2024
1 parent 9da663d commit 2f94343
Showing 1 changed file with 56 additions and 42 deletions.
98 changes: 56 additions & 42 deletions packages/hd/tests/keystore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,69 +55,83 @@ test("load and check password, loads private key", (t) => {
* MAC: 2103ac29920d71da29f15d75b4a16dbe95cfd7ff8faea1056c33131d846e3097
* Cipher key: fac192ceb5fd772906bea3e118a69e8b
*/
// This case does NOT work with @crpyto/hashes/scrypt because N is not less than 2^(128 * r / 8)
// const json = {
// crypto: {
// cipher: "aes-128-ctr",
// cipherparams: {
// iv: "83dbcc02d8ccb40e466191a123791e0e",
// This case does NOT work with @crpyto/hashes/scrypt because N (kdfparams.n) is greater than 2^(128 * r / 8)
// Error message: 'Scrypt: N must be larger than 1, a power of 2, less than 2^(128 * r / 8) and less than 2^32'
// test("load test vector keystore", (t) => {
// const json = {
// crypto: {
// cipher: "aes-128-ctr",
// cipherparams: {
// iv: "83dbcc02d8ccb40e466191a123791e0e",
// },
// ciphertext:
// "d172bf743a674da9cdad04534d56926ef8358534d458fffccd4e6ad2fbde479c",
// kdf: "scrypt",
// kdfparams: {
// dklen: 32,
// n: 262144, // 2^18, but 2^(128 * r / 8) is 2^16
// p: 8,
// r: 1,
// salt: "ab0c7876052600dd703518d6fc3fe8984592145b591fc8fb5c6d43190334ba19",
// },
// mac: "2103ac29920d71da29f15d75b4a16dbe95cfd7ff8faea1056c33131d846e3097",
// },
// ciphertext:
// "d172bf743a674da9cdad04534d56926ef8358534d458fffccd4e6ad2fbde479c",
// kdf: "scrypt",
// kdfparams: {
// dklen: 32,
// n: 262144, // 2^18 Scrypt: N must be larger than 1, a power of 2, less than 2^(128 * r / 8) and less than 2^32
// r: 1,
// p: 8,
// salt: "ab0c7876052600dd703518d6fc3fe8984592145b591fc8fb5c6d43190334ba19",
// },
// mac: "2103ac29920d71da29f15d75b4a16dbe95cfd7ff8faea1056c33131d846e3097",
// },
// id: "3198bc9c-6672-5ab3-d995-4942343ae5b6",
// version: 3,
// };
// id: "3198bc9c-6672-5ab3-d995-4942343ae5b6",
// version: 3,
// };
// const keystore = Keystore.fromJson(JSON.stringify(json));
// t.true(keystore.checkPassword("testpassword"));
// t.deepEqual(
// keystore.decrypt("testpassword"),
// "0x7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d"
// );
// t.deepEqual(
// hexify(keystore.derivedKey("testpassword")),
// "0xfac192ceb5fd772906bea3e118a69e8bbb5cc24229e20d8766fd298291bba6bd"
// );
// });

/**
* test vector:
* https://github.com/web3/web3.js/blob/4.x/packages/web3-eth-accounts/src/account.ts#L489-L516
* Address: cda9a91875fc35c8ac1320e098e584495d66e47c
* UUID: c0cb0a94-4702-4492-b6e6-eb2ac404344a
* Password: 123
* Secret: 67f476289210e3bef3c1c75e4de993ff0a00663df00def84e73aa7411eac18a6
* Derived key: d3d187f93f0d3c5e45d35881f7740713e3dd9488f9a4e25344799e58e1cfc76d
* MAC: efbf6d3409f37c0084a79d5fdf9a6f5d97d11447517ef1ea8374f51e581b7efd
*/
test("load test vector keystore", (t) => {
// const privateKey = "0xe8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35";
// const chainCode = "0x873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508";
// const keystore = KeyStore.create(
// new ExtendedPrivateKey(privateKey, chainCode),
// 'testpassword',
// {
// iv: bytify("0x8f7097ee22add66106055bf3e241fece"),
// salt: bytify("0x481ead5b0311079d2adcd74f32e3db3bdc5f447bc2217ad8164d2d7ead19079f")
// }
// );
const json = {
crypto: {
cipher: "aes-128-ctr",
cipherparams: {
iv: "8f7097ee22add66106055bf3e241fece",
iv: "bfb43120ae00e9de110f8325143a2709",
},
ciphertext:
"28b4e6295dca3079d2e0d661517dd18ee7fe0b6a816ef7398d1828e10dfff57480107d30e1e4a73d30a7e0c46584f8f7f66ca38c13fb6fddc22abfe07f3ab0fb",
"cb3e13e3281ff3861a3f0257fad4c9a51b0eb046f9c7821825c46b210f040b8f",
kdf: "scrypt",
kdfparams: {
dklen: 32,
n: 262144, // 2^18
n: 8192, // 2^13
r: 8,
p: 1,
salt: "481ead5b0311079d2adcd74f32e3db3bdc5f447bc2217ad8164d2d7ead19079f",
salt: "210d0ec956787d865358ac45716e6dd42e68d48e346d795746509523aeb477dd",
},
mac: "87e41a279a32c206579738a051801b3532749682a34fe914e3f487622b0c7b2a",
mac: "efbf6d3409f37c0084a79d5fdf9a6f5d97d11447517ef1ea8374f51e581b7efd",
},
id: "3198bc9c-6672-5ab3-d995-4942343ae5b6",
id: "c0cb0a94-4702-4492-b6e6-eb2ac404344a",
version: 3,
};
const keystore = Keystore.fromJson(JSON.stringify(json));
t.true(keystore.checkPassword("testpassword"));
t.true(keystore.checkPassword("123"));
t.deepEqual(
keystore.decrypt("testpassword"),
"0xe8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508"
keystore.decrypt("123"),
"0x67f476289210e3bef3c1c75e4de993ff0a00663df00def84e73aa7411eac18a6"
);
t.deepEqual(
hexify(keystore.derivedKey("testpassword")),
"0xdc0b555db92835c5a417a6c7229a0c3a228c91ff1b22580ee2c368a8aed33140"
hexify(keystore.derivedKey("123")),
"0xd3d187f93f0d3c5e45d35881f7740713e3dd9488f9a4e25344799e58e1cfc76d"
);
});

Expand Down

0 comments on commit 2f94343

Please sign in to comment.