Skip to content

Commit

Permalink
Merge pull request #3474 from threefoldtech/development_validate_seed
Browse files Browse the repository at this point in the history
Validate hex seed in case activate account
  • Loading branch information
samaradel authored Oct 10, 2024
2 parents 45dea6f + d4c6d5a commit 038404b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 85 deletions.
19 changes: 15 additions & 4 deletions packages/playground/src/weblets/profile_manager.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
<p>
Balance:
<strong :class="theme.name.value === AppThemeSelection.light ? 'text-primary' : 'text-info'">
{{ normalizeBalance(balance.free + balance.reserved, true) }} TFT
{{ normalizeBalance(balance.free + balance.reserved, true) }}
TFT
</strong>
</p>
<p>
Expand Down Expand Up @@ -353,7 +354,11 @@
<v-col cols="12" md="6" lg="6" xl="6">
<PasswordInputWrapper #="{ props }">
<VTextField
:label="profileManager.profile.mnemonic.startsWith('0x') ? 'Your Hex Seed' : 'Your Mnemonic'"
:label="
profileManager.profile.mnemonic.startsWith('0x') || profileManager.profile.mnemonic.length === 64
? 'Your Hex Seed'
: 'Your Mnemonic'
"
readonly
v-model="profileManager.profile.mnemonic"
v-bind="props"
Expand Down Expand Up @@ -714,6 +719,7 @@ async function createNewAccount() {
}
const activatingAccount = ref(false);
async function activateAccount() {
openAcceptTerms.value = false;
termsLoading.value = false;
Expand All @@ -722,7 +728,12 @@ async function activateAccount() {
activatingAccount.value = true;
activating.value = true;
try {
await activateAccountAndCreateTwin(mnemonic.value);
const mnemonicOrSeedValue = validateMnemonic(mnemonic.value)
? mnemonic.value
: mnemonic.value.length === 66
? mnemonic.value
: `0x${mnemonic.value}`;
await activateAccountAndCreateTwin(mnemonicOrSeedValue);
await storeAndLogin();
} catch (e) {
enableReload.value = true;
Expand Down Expand Up @@ -794,9 +805,9 @@ async function storeAndLogin() {
await activate(mnemonic.value, keypairType.value);
} catch (e) {
if (e instanceof TwinNotExistError) {
isNonActiveMnemonic.value = true;
openAcceptTerms.value = true;
termsLoading.value = true;
isNonActiveMnemonic.value = true;
}
enableReload.value = false;
return {
Expand Down
47 changes: 28 additions & 19 deletions packages/tfchain_client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,29 +276,38 @@ class Client extends QueryClient {
}

checkInputs(): void {
if (!this.url) throw new ValidationError("url should be provided");
if (!this.url) {
throw new ValidationError("TFChain URL is required.");
}

if (!SUPPORTED_KEYPAIR_TYPES.includes(this.keypairType)) {
throw new ValidationError(
`Keypair type ${this.keypairType} is not a valid type. Should be either of: ${SUPPORTED_KEYPAIR_TYPES}`,
);
const validTypes = SUPPORTED_KEYPAIR_TYPES.join(", ");
throw new ValidationError(`Invalid keypair type: "${this.keypairType}". Valid options are: ${validTypes}.`);
}

if (!this.mnemonicOrSecret || this.mnemonicOrSecret.startsWith("//")) {
return;
}

if ((this.mnemonicOrSecret && this.extSigner) || !(this.mnemonicOrSecret || this.extSigner)) {
throw new ValidationError("mnemonicOrSecret or extension signer should be provided");
if (!this.mnemonicOrSecret && !this.extSigner) {
throw new ValidationError("Either 'mnemonicOrSecret' or 'extSigner' must be provided.");
}
if (this.mnemonicOrSecret) {
if (this.mnemonicOrSecret.startsWith("//")) {
return;
} else if (!validateMnemonic(this.mnemonicOrSecret)) {
if (this.mnemonicOrSecret.includes(" "))
// seed shouldn't have spaces
throw new ValidationError("Invalid mnemonic! Must be bip39 compliant");

if (!this.mnemonicOrSecret.startsWith("0x"))
throw new ValidationError("Invalid secret seed. secret seed should starts with 0x");
const secret = this.mnemonicOrSecret.substring(2);
if (secret.length !== 64) throw new ValidationError("Invalid secret length. Secret length should be 64");
if (!isValidSeed(secret)) throw new ValidationError("Invalid secret seed");

if (
!validateMnemonic(this.mnemonicOrSecret) &&
this.mnemonicOrSecret.length !== 64 &&
this.mnemonicOrSecret.length !== 66
) {
throw new ValidationError("Invalid mnemonic provided.");
}

if (this.mnemonicOrSecret.startsWith("0x")) {
const secret = this.mnemonicOrSecret.substring(2);
if (secret.length !== 64) {
throw new ValidationError("Secret must be exactly 64 characters long.");
}
if (!isValidSeed(secret)) {
throw new ValidationError("Invalid seed derived from the secret.");
}
}
}
Expand Down
124 changes: 62 additions & 62 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3920,13 +3920,13 @@
estree-walker "^2.0.2"
source-map-js "^1.0.2"

"@vue/[email protected].9":
version "3.5.9"
resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.5.9.tgz#d51fbfe6c18479b27fe6b1723344ba0832e4aacb"
integrity sha512-KE1sCdwqSKq0CQ/ltg3XnlMTKeinjegIkuFsuq9DKvNPmqLGdmI51ChZdGBBRXIvEYTLm8X/JxOuBQ1HqF/+PA==
"@vue/[email protected].10":
version "3.5.10"
resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.5.10.tgz#dc382e4173c5ad6d309887f5cb02983dfd88cfee"
integrity sha512-iXWlk+Cg/ag7gLvY0SfVucU8Kh2CjysYZjhhP70w9qI4MvSox4frrP+vDGvtQuzIcgD8+sxM6lZvCtdxGunTAA==
dependencies:
"@babel/parser" "^7.25.3"
"@vue/shared" "3.5.9"
"@vue/shared" "3.5.10"
entities "^4.5.0"
estree-walker "^2.0.2"
source-map-js "^1.2.0"
Expand All @@ -3947,13 +3947,13 @@
"@vue/compiler-core" "3.3.4"
"@vue/shared" "3.3.4"

"@vue/[email protected].9":
version "3.5.9"
resolved "https://registry.yarnpkg.com/@vue/compiler-dom/-/compiler-dom-3.5.9.tgz#6fa2b7e536ae4c416fc2d60b7e9e33b3410eac7a"
integrity sha512-gEAURwPo902AsJF50vl59VaWR+Cx6cX9SoqLYHu1jq9hDbmQlXvpZyYNIIbxa2JTJ+FD/oBQweVUwuTQv79KTg==
"@vue/[email protected].10":
version "3.5.10"
resolved "https://registry.yarnpkg.com/@vue/compiler-dom/-/compiler-dom-3.5.10.tgz#233c660289ce289a48e8fe759b07b95f607cd98e"
integrity sha512-DyxHC6qPcktwYGKOIy3XqnHRrrXyWR2u91AjP+nLkADko380srsC2DC3s7Y1Rk6YfOlxOlvEQKa9XXmLI+W4ZA==
dependencies:
"@vue/compiler-core" "3.5.9"
"@vue/shared" "3.5.9"
"@vue/compiler-core" "3.5.10"
"@vue/shared" "3.5.10"

"@vue/[email protected]", "@vue/compiler-sfc@^3.3.0":
version "3.3.2"
Expand Down Expand Up @@ -3987,16 +3987,16 @@
postcss "^8.1.10"
source-map-js "^1.0.2"

"@vue/[email protected].9":
version "3.5.9"
resolved "https://registry.yarnpkg.com/@vue/compiler-sfc/-/compiler-sfc-3.5.9.tgz#020b7654f1fde7c606a49ec4e4d2838e8e1a43c5"
integrity sha512-kp9qawcTXakYm0TN6YAwH24IurSywoXh4fWhRbLu0at4UVyo994bhEzJlQn82eiyqtut4GjkQodSfn8drFbpZQ==
"@vue/[email protected].10":
version "3.5.10"
resolved "https://registry.yarnpkg.com/@vue/compiler-sfc/-/compiler-sfc-3.5.10.tgz#95e262a5ed836521a5aeee9492cc265ad3f1c787"
integrity sha512-to8E1BgpakV7224ZCm8gz1ZRSyjNCAWEplwFMWKlzCdP9DkMKhRRwt0WkCjY7jkzi/Vz3xgbpeig5Pnbly4Tow==
dependencies:
"@babel/parser" "^7.25.3"
"@vue/compiler-core" "3.5.9"
"@vue/compiler-dom" "3.5.9"
"@vue/compiler-ssr" "3.5.9"
"@vue/shared" "3.5.9"
"@vue/compiler-core" "3.5.10"
"@vue/compiler-dom" "3.5.10"
"@vue/compiler-ssr" "3.5.10"
"@vue/shared" "3.5.10"
estree-walker "^2.0.2"
magic-string "^0.30.11"
postcss "^8.4.47"
Expand All @@ -4018,13 +4018,13 @@
"@vue/compiler-dom" "3.3.4"
"@vue/shared" "3.3.4"

"@vue/[email protected].9":
version "3.5.9"
resolved "https://registry.yarnpkg.com/@vue/compiler-ssr/-/compiler-ssr-3.5.9.tgz#e30f8e866589392421abcbfc0e0241470f3ca9a6"
integrity sha512-fb1g2mQv32QzIei76rlXRTz08Grw+ZzBXSQfHo4StGFutm/flyebw3dGJkexKwcU3GjX9s5fIGjEv/cjO8j8Yw==
"@vue/[email protected].10":
version "3.5.10"
resolved "https://registry.yarnpkg.com/@vue/compiler-ssr/-/compiler-ssr-3.5.10.tgz#195f83ae7c52174be37fd7a4a0217132c1c0ed11"
integrity sha512-hxP4Y3KImqdtyUKXDRSxKSRkSm1H9fCvhojEYrnaoWhE4w/y8vwWhnosJoPPe2AXm5sU7CSbYYAgkt2ZPhDz+A==
dependencies:
"@vue/compiler-dom" "3.5.9"
"@vue/shared" "3.5.9"
"@vue/compiler-dom" "3.5.10"
"@vue/shared" "3.5.10"

"@vue/devtools-api@^6.5.0":
version "6.5.0"
Expand Down Expand Up @@ -4112,12 +4112,12 @@
dependencies:
"@vue/shared" "3.3.4"

"@vue/[email protected].9":
version "3.5.9"
resolved "https://registry.yarnpkg.com/@vue/reactivity/-/reactivity-3.5.9.tgz#8864a55e4c495666f3c679beb8f734489eeb042e"
integrity sha512-88ApgNZ6yPYpyYkTfXzcbWk6O8+LrPRIpa/U4AdeTzpfRUO+EUt5jemnTBVSlAUNmlYY96xa5feUNEq+BouLog==
"@vue/[email protected].10":
version "3.5.10"
resolved "https://registry.yarnpkg.com/@vue/reactivity/-/reactivity-3.5.10.tgz#81140ef0b05096973356d3c8fc32f48c79940b9c"
integrity sha512-kW08v06F6xPSHhid9DJ9YjOGmwNDOsJJQk0ax21wKaUYzzuJGEuoKNU2Ujux8FLMrP7CFJJKsHhXN9l2WOVi2g==
dependencies:
"@vue/shared" "3.5.9"
"@vue/shared" "3.5.10"

"@vue/[email protected]":
version "3.3.2"
Expand All @@ -4135,13 +4135,13 @@
"@vue/reactivity" "3.3.4"
"@vue/shared" "3.3.4"

"@vue/[email protected].9":
version "3.5.9"
resolved "https://registry.yarnpkg.com/@vue/runtime-core/-/runtime-core-3.5.9.tgz#e47f890734039f77dac86328cc059cf8188c5729"
integrity sha512-YAeP0zNkjSl5mEc1NxOg9qoAhLNbREElHAhfYbMXT57oF0ixehEEJWBhg2uvVxslCGh23JhpEAyMvJrJHW9WGg==
"@vue/[email protected].10":
version "3.5.10"
resolved "https://registry.yarnpkg.com/@vue/runtime-core/-/runtime-core-3.5.10.tgz#e902eb2640fa6ab4cc4589af263818a898812668"
integrity sha512-9Q86I5Qq3swSkFfzrZ+iqEy7Vla325M7S7xc1NwKnRm/qoi1Dauz0rT6mTMmscqx4qz0EDJ1wjB+A36k7rl8mA==
dependencies:
"@vue/reactivity" "3.5.9"
"@vue/shared" "3.5.9"
"@vue/reactivity" "3.5.10"
"@vue/shared" "3.5.10"

"@vue/[email protected]":
version "3.3.2"
Expand All @@ -4161,14 +4161,14 @@
"@vue/shared" "3.3.4"
csstype "^3.1.1"

"@vue/[email protected].9":
version "3.5.9"
resolved "https://registry.yarnpkg.com/@vue/runtime-dom/-/runtime-dom-3.5.9.tgz#088746207f74963d09b31ce7b79add0bf96aa337"
integrity sha512-5Oq/5oenpB9lw94moKvOHqBDEaMSyDmcu2HS8AtAT6/pwdo/t9fR9aVtLh6FzYGGqZR9yRfoHAN6P7goblq1aA==
"@vue/[email protected].10":
version "3.5.10"
resolved "https://registry.yarnpkg.com/@vue/runtime-dom/-/runtime-dom-3.5.10.tgz#dca26d7761147373c6929f1370cf2733aa19f3de"
integrity sha512-t3x7ht5qF8ZRi1H4fZqFzyY2j+GTMTDxRheT+i8M9Ph0oepUxoadmbwlFwMoW7RYCpNQLpP2Yx3feKs+fyBdpA==
dependencies:
"@vue/reactivity" "3.5.9"
"@vue/runtime-core" "3.5.9"
"@vue/shared" "3.5.9"
"@vue/reactivity" "3.5.10"
"@vue/runtime-core" "3.5.10"
"@vue/shared" "3.5.10"
csstype "^3.1.3"

"@vue/[email protected]", "@vue/server-renderer@^3.0.1":
Expand All @@ -4187,13 +4187,13 @@
"@vue/compiler-ssr" "3.3.4"
"@vue/shared" "3.3.4"

"@vue/[email protected].9":
version "3.5.9"
resolved "https://registry.yarnpkg.com/@vue/server-renderer/-/server-renderer-3.5.9.tgz#3bf0736001623960d120ef01dee5045fad6efadb"
integrity sha512-tbuUsZfMWGazR9LXLNiiDSTwkO8K9sLyR70diY+FbQmKmh7236PPz4jkTxymelV8D89IJUGtbfe4VdmpHkmuxg==
"@vue/[email protected].10":
version "3.5.10"
resolved "https://registry.yarnpkg.com/@vue/server-renderer/-/server-renderer-3.5.10.tgz#90462492c30c8cae499b9149d1b90af2ebfe7599"
integrity sha512-IVE97tt2kGKwHNq9yVO0xdh1IvYfZCShvDSy46JIh5OQxP1/EXSpoDqetVmyIzL7CYOWnnmMkVqd7YK2QSWkdw==
dependencies:
"@vue/compiler-ssr" "3.5.9"
"@vue/shared" "3.5.9"
"@vue/compiler-ssr" "3.5.10"
"@vue/shared" "3.5.10"

"@vue/[email protected]", "@vue/shared@^3.3.0":
version "3.3.2"
Expand All @@ -4205,10 +4205,10 @@
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.3.4.tgz#06e83c5027f464eef861c329be81454bc8b70780"
integrity sha512-7OjdcV8vQ74eiz1TZLzZP4JwqM5fA94K6yntPS5Z25r9HDuGNzaGdgvwKYq6S+MxwF0TFRwe50fIR/MYnakdkQ==

"@vue/[email protected].9":
version "3.5.9"
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.5.9.tgz#713257216ea2cbf4e200cb9ae395c34ae2349385"
integrity sha512-8wiT/m0mnsLhTME0mPgc57jv+4TipRBSAAmheUdYgiOaO6AobZPNOmm87ub4np65VVDgLcWxc+Edc++5Wyz1uA==
"@vue/[email protected].10":
version "3.5.10"
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.5.10.tgz#066f7dde31e09d700123e92e63eaa126cda21a17"
integrity sha512-VkkBhU97Ki+XJ0xvl4C9YJsIZ2uIlQ7HqPpZOS3m9VCvmROPaChZU6DexdMJqvz9tbgG+4EtFVrSuailUq5KGQ==

"@vue/test-utils@^2.3.0":
version "2.3.2"
Expand Down Expand Up @@ -14869,15 +14869,15 @@ vue@^3.2.47:
"@vue/shared" "3.3.2"

vue@^3.3.5:
version "3.5.9"
resolved "https://registry.yarnpkg.com/vue/-/vue-3.5.9.tgz#a065952d7a7c0e2cbfec8e016582b055ab984357"
integrity sha512-nHzQhZ5cjFKynAY2beAm7XtJ5C13VKAFTLTgRYXy+Id1KEKBeiK6hO2RcW1hUjdbHMadz1YzxyHgQigOC54wug==
dependencies:
"@vue/compiler-dom" "3.5.9"
"@vue/compiler-sfc" "3.5.9"
"@vue/runtime-dom" "3.5.9"
"@vue/server-renderer" "3.5.9"
"@vue/shared" "3.5.9"
version "3.5.10"
resolved "https://registry.yarnpkg.com/vue/-/vue-3.5.10.tgz#14be9d4655e07be8d5e8295d017815ed14337f96"
integrity sha512-Vy2kmJwHPlouC/tSnIgXVg03SG+9wSqT1xu1Vehc+ChsXsRd7jLkKgMltVEFOzUdBr3uFwBCG+41LJtfAcBRng==
dependencies:
"@vue/compiler-dom" "3.5.10"
"@vue/compiler-sfc" "3.5.10"
"@vue/runtime-dom" "3.5.10"
"@vue/server-renderer" "3.5.10"
"@vue/shared" "3.5.10"

vuetify@^3.3.21:
version "3.3.21"
Expand Down

0 comments on commit 038404b

Please sign in to comment.