testEth.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ((window) => {
  2. function expect(actual, matcher) {
  3. if (actual !== matcher) {
  4. throw new Error(`expect ${matcher}, got ${actual}`);
  5. }
  6. }
  7. async function runSettingsTestETH() {
  8. console.log("test connect");
  9. const endpoint = "wss://kusama.api.onfinality.io/public-ws";
  10. const connected = await settings.connect([endpoint]);
  11. expect(connected, endpoint);
  12. expect(!!api, true);
  13. console.log("test get consts");
  14. const constants = await settings.getNetworkConst(api);
  15. expect(constants.babe.epochDuration.toHuman(), api.consts.babe.epochDuration.toHuman());
  16. console.log("settings tests passed.");
  17. }
  18. const testKeystoreETH = {
  19. address: "634dbe93a30148af3eb54e92a8ebfb852d1be50b",
  20. id: "72d15c27-ee38-415f-9e19-dc84ffb3a22d",
  21. version: 3,
  22. Crypto: {
  23. cipher: "aes-128-ctr",
  24. cipherparams: { iv: "096257c41b1df3a7112a91c08b1493e6" },
  25. ciphertext: "0206a8ee03e9b0344aefffea1761703312015714df503976727a152b02cce629",
  26. kdf: "scrypt",
  27. kdfparams: { salt: "4e7f6e1fc953833472128462092eec42ac58040b1673b9112512acbcbf6e6d4b", n: 131072, dklen: 32, p: 1, r: 8 },
  28. mac: "633c5b6fef097822a454743fd36f9fc674c6d38355b4bedbfdcafdb1518ab1fd",
  29. },
  30. "x-ethers": {
  31. client: "ethers.js",
  32. gethFilename: "UTC--2021-09-26T07-27-09.0Z--634dbe93a30148af3eb54e92a8ebfb852d1be50b",
  33. mnemonicCounter: "2db575fa106234a5e2ff7b6b78940046",
  34. mnemonicCiphertext: "a147c6e8cfd9d01eb3412708191996d0",
  35. path: "m/44'/60'/0'/0/0",
  36. locale: "en",
  37. version: "0.1",
  38. },
  39. };
  40. const testMnemonicETH = "clown trash dish duty expire select announce nothing winner pepper scorpion until";
  41. const testAddressETH = "0x634DbE93A30148aF3eB54E92a8Ebfb852D1Be50B";
  42. const testPKeyETH = "0xac2920c6d04d70f70aaaa541f61e2efaaa89a791e95e62505daa7b67593d87b1";
  43. const derivePath1 = "m/44'/60'/0'/0/1";
  44. const testAddress1ETH = "0x015d775B11761d78637801E1f166019Ca147B5BE";
  45. const testPKey1ETH = "0x5caf995633bac90804739bd0410ca151e397a8c03af364446d58af7a55d11040";
  46. async function runKeyringTestETH() {
  47. console.log("generate mnemonic");
  48. const mnemonicGen = await eth.keyring.gen();
  49. expect(mnemonicGen.mnemonic.split(" ").length, 12);
  50. expect(!!mnemonicGen.address.match("0x"), true);
  51. expect(!!mnemonicGen.svg.match("<svg"), true);
  52. const mnemonic = await eth.keyring.gen(testMnemonicETH);
  53. expect(mnemonic.address, testAddressETH);
  54. const mnemonic1 = await eth.keyring.gen(testMnemonicETH, 1);
  55. expect(mnemonic1.address, testAddress1ETH);
  56. console.log("get address from mnemonic/privateKey");
  57. const mnemonic2 = await eth.keyring.addressFromMnemonic(testMnemonicETH);
  58. expect(mnemonic2.address, testAddressETH);
  59. const mnemonic3 = await eth.keyring.addressFromMnemonic(testMnemonicETH, derivePath1);
  60. expect(mnemonic3.address, testAddress1ETH);
  61. const mnemonic4 = await eth.keyring.addressFromPrivateKey(testPKeyETH);
  62. expect(mnemonic4.address, testAddressETH);
  63. const mnemonic5 = await eth.keyring.addressFromPrivateKey(testPKey1ETH);
  64. expect(mnemonic5.address, testAddress1ETH);
  65. console.log("import account from mnemonic");
  66. const password = "a111111";
  67. console.log(new Date().toLocaleString());
  68. const acc = await eth.keyring.recover("mnemonic", testMnemonicETH, null, password);
  69. console.log(new Date().toLocaleString());
  70. expect(acc.mnemonic, testMnemonicETH);
  71. expect(acc.address, testAddressETH);
  72. expect("0x" + JSON.parse(acc.keystore).address, testAddressETH.toLowerCase());
  73. const acc1 = await eth.keyring.recover("mnemonic", testMnemonicETH, derivePath1, password);
  74. expect(acc1.mnemonic, testMnemonicETH);
  75. expect(acc1.address, testAddress1ETH);
  76. expect("0x" + JSON.parse(acc1.keystore).address, testAddress1ETH.toLowerCase());
  77. console.log("import account from privateKey");
  78. console.log(new Date().toLocaleString());
  79. const acc2 = await eth.keyring.recover("privateKey", testPKeyETH, null, password);
  80. console.log(new Date().toLocaleString());
  81. expect(acc2.privateKey, testPKeyETH);
  82. expect(acc2.address, testAddressETH);
  83. expect("0x" + JSON.parse(acc2.keystore).address, testAddressETH.toLowerCase());
  84. const acc3 = await eth.keyring.recover("privateKey", testPKey1ETH, null, password);
  85. expect(acc3.privateKey, testPKey1ETH);
  86. expect(acc3.address, testAddress1ETH);
  87. expect("0x" + JSON.parse(acc3.keystore).address, testAddress1ETH.toLowerCase());
  88. console.log("import account from json");
  89. const acc4 = await eth.keyring.recover("keystore", JSON.stringify(testKeystoreETH), null, password);
  90. expect(acc4.address, testAddressETH);
  91. expect("0x" + JSON.parse(acc4.keystore).address, testAddressETH.toLowerCase());
  92. const acc5 = await eth.keyring.recover("keystore", JSON.stringify(testKeystoreETH), null, password + "xx");
  93. expect(acc5.address === testAddressETH, false);
  94. expect(acc5.keystore, undefined);
  95. console.log("sign message / verify signature");
  96. const message = "Hello world, my tests.";
  97. const signed = await eth.keyring.signMessage(message, acc.address, password);
  98. expect(signed.address, testAddressETH);
  99. expect(signed.pubKey, acc.pubKey);
  100. const signatureCheck = await eth.keyring.verifySignature(message, signed.signature);
  101. expect(signatureCheck.signer, testAddressETH);
  102. const signatureCheck1 = await eth.keyring.verifySignature(message + "msg changed", signed.signature);
  103. expect(signatureCheck1.signer === testAddressETH, false);
  104. console.log("generate icons from address");
  105. const icon1 = await eth.account.genIcons([testAddressETH]);
  106. expect(icon1[0][0], testAddressETH);
  107. expect(icon1[0][1], mnemonic2.svg);
  108. const icon2 = await eth.account.genIcons([testAddress1ETH]);
  109. expect(icon2[0][0], testAddress1ETH);
  110. expect(icon2[0][1], mnemonic3.svg);
  111. console.log("check password");
  112. const passCheck1 = await eth.keyring.checkPassword(acc.address, password);
  113. expect(passCheck1.success, true);
  114. const passCheck2 = await eth.keyring.checkPassword(acc.address, password + "xx");
  115. expect(passCheck2.success, false);
  116. expect(passCheck2.error, "invalid password");
  117. console.log("change password");
  118. const passNew = "c111111";
  119. const changed = await eth.keyring.changePassword(acc.address, password, passNew);
  120. expect(changed.pubKey, acc.pubKey);
  121. expect(changed.address, acc.address);
  122. const passCheck3 = await eth.keyring.checkPassword(changed.address, password);
  123. expect(passCheck3.success, false);
  124. expect(passCheck3.error, "invalid password");
  125. const passCheck4 = await eth.keyring.checkPassword(changed.address, passNew);
  126. expect(passCheck4.success, true);
  127. console.log("keyring tests passed.");
  128. }
  129. async function runTestsETH() {
  130. // keyring api run without network
  131. await runKeyringTestETH();
  132. console.log("all tests passed.");
  133. }
  134. window.runTestsETH = runTestsETH;
  135. })(window);