test.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. function expect(actual, matcher) {
  2. if (actual !== matcher) {
  3. throw new Error(`expect ${matcher}, got ${actual}`);
  4. }
  5. }
  6. async function runSettingsTest() {
  7. console.log("test connect");
  8. const endpoint = "wss://kusama.api.onfinality.io/public-ws";
  9. const connected = await settings.connect([endpoint]);
  10. expect(connected, endpoint);
  11. expect(!!api, true);
  12. console.log("test get consts");
  13. const constants = await settings.getNetworkConst(api);
  14. expect(constants.babe.epochDuration.toHuman(), api.consts.babe.epochDuration.toHuman());
  15. console.log("settings tests passed.");
  16. }
  17. const testKeystore =
  18. '{"pubKey":"0xcc597bd2e7eda5094d6aa462523b629a502db6cc71a6ae0e9b158d9e42c6c462","mnemonic":"welcome clinic duck mom connect heart poet admit vendor robot group vacuum","rawSeed":"","address":"15cwMLiH57HvrqBfMYpt5AgGrb5SAUKx7XQUcHnBSs2DAsGt","encoded":"taoH2SolrO8UhraK1JxuNW9AcMMPY5UXMTJjlcpuyEEAgAAAAQAAAAgAAADdvrSwzB9yIFQ7ZCHQoQQV93zLhlAiZlits1CX2hFNm3/zPjYW63U7NzoF76UU4hUvyUTmrvT/K37v0zQ1eFrXwXvc2fmKFJ17qSR2oDvHfuCb+ruCsSrx/UsGtNLbzyCiomVYGMvRh/EzHEfBQO4jGaDi4Sq5++8QE2vuDUTePF8WsVSb5L9N30SFuNQ1YiTH7XBRG9zQhQTofLl0","encoding":{"content":["pkcs8","sr25519"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},"meta":{}}';
  19. async function runKeyringTest() {
  20. console.log("init keys from json");
  21. const initialAcc = await keyring.initKeys([JSON.parse(testKeystore)], [0, 2]);
  22. expect(
  23. initialAcc[0]["0xcc597bd2e7eda5094d6aa462523b629a502db6cc71a6ae0e9b158d9e42c6c462"],
  24. "15cwMLiH57HvrqBfMYpt5AgGrb5SAUKx7XQUcHnBSs2DAsGt"
  25. );
  26. console.log("generate mnemonic");
  27. const mnemonic = await keyring.gen(null, 0, "sr25519", "");
  28. expect(mnemonic.mnemonic.split(" ").length, 12);
  29. expect(!!mnemonic.svg.match("<svg"), true);
  30. const mnemonic1 = await keyring.gen(mnemonic.mnemonic, 0, "sr25519", "//superman");
  31. expect(mnemonic1.mnemonic, mnemonic.mnemonic);
  32. expect(mnemonic1.address == mnemonic.mnemonic, false);
  33. expect(mnemonic1.svg == mnemonic.svg, false);
  34. const mnemonic2 = await keyring.gen("null", 0, "sr25519", "");
  35. expect(mnemonic2, null);
  36. console.log("check mnemonic valid");
  37. const mnemonicValid = await keyring.checkMnemonicValid(mnemonic.mnemonic);
  38. expect(mnemonicValid, true);
  39. const mnemonicValid1 = await keyring.checkMnemonicValid("null");
  40. expect(mnemonicValid1, false);
  41. const mnemonicValid2 = await keyring.checkMnemonicValid(null);
  42. expect(mnemonicValid2, false);
  43. console.log("get address from mnemonic/seed");
  44. const addrWithIcon = await keyring.addressFromMnemonic(mnemonic.mnemonic, 0, "sr25519", "");
  45. expect(!!addrWithIcon.svg.match("<svg"), true);
  46. const addrWithIcon2 = await keyring.addressFromRawSeed("Alice", 0, "sr25519", "");
  47. expect(!!addrWithIcon2.svg.match("<svg"), true);
  48. console.log("import account from mnemonic");
  49. const sr25519 = "sr25519";
  50. const password = "a111111";
  51. const acc = await keyring.recover("mnemonic", sr25519, mnemonic.mnemonic, password);
  52. expect(acc.pubKey.length, 66);
  53. expect(acc.mnemonic, mnemonic.mnemonic);
  54. expect(acc.encoding.content[1], sr25519);
  55. const acc1 = await keyring.recover("mnemonic", sr25519, mnemonic.mnemonic + "//superman", password);
  56. expect(acc1.address == acc.address, false);
  57. expect(acc1.pubKey.length, 66);
  58. expect(acc1.mnemonic, mnemonic.mnemonic + "//superman");
  59. expect(acc1.encoding.content[1], sr25519);
  60. const accBad = await keyring.recover("mnemonic", sr25519, mnemonic.mnemonic + " badword", password);
  61. expect(accBad.mnemonic, undefined);
  62. expect(!!accBad.error, true);
  63. expect(!!accBad.error.match("invalid mnemonic"), true);
  64. console.log("import account from raw seed");
  65. const acc2 = await keyring.recover("rawSeed", sr25519, "Alice", password);
  66. expect(acc2.pubKey.length, 66);
  67. expect(acc2.address, "13iz1UvC8XMnHTW2wgoG7SxUhNgbp7trCgjxcuqTne9bGMQX");
  68. expect(acc2.encoding.content[1], sr25519);
  69. console.log("import account from json");
  70. const acc3 = await keyring.recover("keystore", sr25519, testKeystore, password);
  71. expect(acc3.pubKey.length, 66);
  72. expect(acc3.address, "15cwMLiH57HvrqBfMYpt5AgGrb5SAUKx7XQUcHnBSs2DAsGt");
  73. expect(acc3.encoding.content[1], sr25519);
  74. console.log("check derive path");
  75. const deriveError = await keyring.checkDerivePath("Alice", "", sr25519);
  76. expect(deriveError, null);
  77. const deriveError1 = await keyring.checkDerivePath("Alice", "//test", sr25519);
  78. expect(deriveError1, null);
  79. const deriveError2 = await keyring.checkDerivePath("Alice", "//test/wallet", sr25519);
  80. expect(deriveError2, null);
  81. const deriveError3 = await keyring.checkDerivePath("Alice", "test//", sr25519);
  82. expect(true, !!deriveError3);
  83. const deriveError4 = await keyring.checkDerivePath("Alice", "//test", "ed25519");
  84. expect(deriveError4, null);
  85. const deriveError5 = await keyring.checkDerivePath("Alice", "/test", "ed25519");
  86. expect(true, !!deriveError5);
  87. console.log("generate icons from address");
  88. const icon1 = await account.genIcons([acc.address]);
  89. expect(icon1[0][0], acc.address);
  90. expect(!!icon1[0][1].match("svg"), true);
  91. console.log("generate icons from pubKey");
  92. const icon2 = await account.genPubKeyIcons([acc.pubKey]);
  93. expect(icon2[0][0], acc.pubKey);
  94. expect(!!icon2[0][1].match("svg"), true);
  95. console.log("encode address");
  96. const encoded = await account.encodeAddress([acc.pubKey], [0, 2]);
  97. expect(encoded[0][acc.pubKey], acc.address);
  98. console.log("decode address");
  99. const decoded = await account.decodeAddress([acc.address]);
  100. expect(decoded[acc.pubKey], acc.address);
  101. console.log("check password");
  102. const passCheck = await keyring.checkPassword(acc.pubKey, "b111111");
  103. expect(passCheck, null);
  104. const passCheck2 = await keyring.checkPassword(acc.pubKey, password);
  105. expect(passCheck2.success, true);
  106. console.log("change password");
  107. const passNew = "c111111";
  108. const passChangeRes = await keyring.changePassword(acc.pubKey, password, passNew);
  109. expect(passChangeRes.pubKey, acc.pubKey);
  110. const passCheck3 = await keyring.checkPassword(acc.pubKey, password);
  111. expect(passCheck3, null);
  112. const passCheck4 = await keyring.checkPassword(acc.pubKey, passNew);
  113. expect(passCheck4.success, true);
  114. console.log("keyring tests passed.");
  115. }
  116. async function runAccountTest() {
  117. console.log("query account bonded");
  118. const testKey = "0xe611c2eced1b561183f88faed0dd7d88d5fafdf16f5840c63ec36d8c31136f61";
  119. const testAddr = "FfBhEQgE9785gKaCtt9P2Nbpwrb3VFryKDrRM1QhYZkVYNS";
  120. const bonded = await account.queryAccountsBonded(api, [testKey]);
  121. expect(bonded[0][0], testKey);
  122. expect(bonded[0].length, 3);
  123. console.log("query balance");
  124. const balance = await account.getBalance(api, testAddr);
  125. expect(balance.accountId.toHuman(), testAddr);
  126. expect(parseFloat(balance.accountNonce.toHuman()) > 0, true);
  127. expect(parseFloat(balance.availableBalance.toHuman()) > 0, true);
  128. expect(parseFloat(balance.freeBalance.toHuman()) > 0, true);
  129. console.log("query info of address");
  130. const addr2 = "HSNBs8VHxcZiqz9NfSQq2YaznTa8BzSvuEWVe4uTihcGiQN";
  131. const info = await account.getAccountIndex(api, [addr2]);
  132. expect(info[0].accountId.toString(), addr2);
  133. expect(info[0].identity.display, "Acala Foundation");
  134. expect(info[0].identity.web, "https://acala.network");
  135. expect(info[0].identity.judgements.length > 0, true);
  136. console.log("account tests passed.");
  137. }
  138. async function runTests() {
  139. // keyring api run without network
  140. await runKeyringTest();
  141. // run settings api to connect to node
  142. await runSettingsTest();
  143. // run other tests
  144. await runAccountTest();
  145. console.log("all tests passed.");
  146. }
  147. window.runTests = runTests;