Skip to content

Commit 4062d6e

Browse files
committed
Merge branch 'develop'
2 parents 746f63d + 8c8acb2 commit 4062d6e

File tree

11 files changed

+218
-86
lines changed

11 files changed

+218
-86
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [0.3.5] - 20211018
2+
3+
* bump polkadot-js/api v6.4.1
4+
* add apiKeyring.checkMnemonicValid()
5+
16
## [0.3.4] - 20211012
27

38
* bump polkadot-js/api v6.3.1

js_api/dist/main.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js_api/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@polkawallet/js-api",
3-
"version": "0.3.3",
3+
"version": "0.3.5",
44
"main": "index.js",
55
"license": "Apache-2.0",
66
"keywords": [
@@ -24,10 +24,10 @@
2424
"build-dev": "webpack --mode development"
2525
},
2626
"resolutions": {
27-
"@polkadot/api": "^6.3.1"
27+
"@polkadot/api": "^6.4.1"
2828
},
2929
"dependencies": {
30-
"@polkadot/api": "^6.3.1",
30+
"@polkadot/api": "^6.4.1",
3131
"@polkadot/extension-dapp": "^0.40.3",
3232
"@polkadot/ui-shared": "0.85.5",
3333
"@walletconnect/client": "2.0.0-alpha.26",

js_api/src/constants/networkMetadata.ts

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

js_api/src/service/keyring.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { keyExtractSuri, mnemonicGenerate, cryptoWaitReady, signatureVerify, encodeAddress } from "@polkadot/util-crypto";
1+
import { keyExtractSuri, mnemonicGenerate, mnemonicValidate, cryptoWaitReady, signatureVerify, encodeAddress } from "@polkadot/util-crypto";
22
import { hexToU8a, u8aToHex } from "@polkadot/util";
33
import BN from "bn.js";
44
import { parseQrCode, getSigner, makeTx, getSubmittable } from "../utils/QrSigner";
@@ -22,6 +22,8 @@ let keyring = new Keyring({ ss58Format: 0, type: "sr25519" });
2222
*/
2323
async function gen(mnemonic: string, ss58Format: number, cryptoType: KeypairType, derivePath: string) {
2424
const key = mnemonic || mnemonicGenerate();
25+
if (!mnemonicValidate(key)) return null;
26+
2527
const keyPair = keyring.addFromMnemonic(key + (derivePath || ""), {}, cryptoType || "sr25519");
2628
const address = encodeAddress(keyPair.publicKey, ss58Format || 0);
2729
const icons = await account.genIcons([address]);
@@ -32,6 +34,13 @@ async function gen(mnemonic: string, ss58Format: number, cryptoType: KeypairType
3234
};
3335
}
3436

37+
/**
38+
* mnemonic validate.
39+
*/
40+
async function checkMnemonicValid(mnemonic: string) {
41+
return mnemonicValidate(mnemonic);
42+
}
43+
3544
/**
3645
* get address and avatar from mnemonic.
3746
*/
@@ -79,6 +88,9 @@ function recover(keyType: string, cryptoType: KeypairType, key: string, password
7988
try {
8089
switch (keyType) {
8190
case "mnemonic":
91+
if (!mnemonicValidate(key.split("/")[0])) {
92+
throw new Error(`invalid mnemonic ${key}`);
93+
}
8294
keyPair = keyring.addFromMnemonic(key, {}, cryptoType);
8395
mnemonic = key;
8496
break;
@@ -466,6 +478,7 @@ async function verifySignature(message: string, signature: string, address: stri
466478
export default {
467479
initKeys,
468480
gen,
481+
checkMnemonicValid,
469482
addressFromMnemonic,
470483
addressFromRawSeed,
471484
recover,

js_api/test/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ async function runKeyringTest() {
3333
const mnemonic = await keyring.gen(null, 0, "sr25519", "");
3434
expect(mnemonic.mnemonic.split(" ").length, 12);
3535
expect(!!mnemonic.svg.match("<svg"), true);
36+
const mnemonic1 = await keyring.gen(mnemonic.mnemonic, 0, "sr25519", "//superman");
37+
expect(mnemonic1.mnemonic, mnemonic.mnemonic);
38+
expect(mnemonic1.address == mnemonic.mnemonic, false);
39+
expect(mnemonic1.svg == mnemonic.svg, false);
40+
const mnemonic2 = await keyring.gen("null", 0, "sr25519", "");
41+
expect(mnemonic2, null);
42+
43+
console.log("check mnemonic valid");
44+
const mnemonicValid = await keyring.checkMnemonicValid(mnemonic.mnemonic);
45+
expect(mnemonicValid, true);
46+
const mnemonicValid1 = await keyring.checkMnemonicValid("null");
47+
expect(mnemonicValid1, false);
48+
const mnemonicValid2 = await keyring.checkMnemonicValid(null);
49+
expect(mnemonicValid2, false);
3650

3751
console.log("get address from mnemonic/seed");
3852
const addrWithIcon = await keyring.addressFromMnemonic(mnemonic.mnemonic, 0, "sr25519", "");
@@ -47,6 +61,15 @@ async function runKeyringTest() {
4761
expect(acc.pubKey.length, 66);
4862
expect(acc.mnemonic, mnemonic.mnemonic);
4963
expect(acc.encoding.content[1], sr25519);
64+
const acc1 = await keyring.recover("mnemonic", sr25519, mnemonic.mnemonic + "//superman", password);
65+
expect(acc1.address == acc.address, false);
66+
expect(acc1.pubKey.length, 66);
67+
expect(acc1.mnemonic, mnemonic.mnemonic + "//superman");
68+
expect(acc1.encoding.content[1], sr25519);
69+
const accBad = await keyring.recover("mnemonic", sr25519, mnemonic.mnemonic + " badword", password);
70+
expect(accBad.mnemonic, undefined);
71+
expect(!!accBad.error, true);
72+
expect(!!accBad.error.match("invalid mnemonic"), true);
5073

5174
console.log("import account from raw seed");
5275
const acc2 = await keyring.recover("rawSeed", sr25519, "Alice", password);

0 commit comments

Comments
 (0)