Skip to content

Commit

Permalink
Merge pull request #78 from UnUniFi/feature/wrong-privkey
Browse files Browse the repository at this point in the history
fix: private key validation
  • Loading branch information
YasunoriMATSUOKA authored Apr 19, 2022
2 parents f3097bb + 318cd1d commit 8a100bf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,17 @@ export class BankApplicationService {
return;
}

if (!validatePrivateStoredWallet(privateWallet)) {
this.snackBar.open('Invalid Wallet info!', 'Close');
return;
}

const privateKey = convertHexStringToUint8Array(privateWallet.privateKey);

if (!privateKey) {
this.snackBar.open('Invalid PrivateKey!', 'Close');
return;
}

if (!validatePrivateStoredWallet(privateWallet)) {
this.snackBar.open('Invalid Wallet info!', 'Close');
return;
}

// simulate
let simulatedResultData: SimulatedTxResultResponse;
let gas: proto.cosmos.base.v1beta1.ICoin;
Expand Down
3 changes: 3 additions & 0 deletions projects/portal/src/app/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export const convertHexStringToUint8Array = (hexString: string): Uint8Array | un
const hexStringWithNoWhitespace = hexString.replace(/\s+/g, '');
const buffer = Buffer.from(hexStringWithNoWhitespace, 'hex');
const uint8Array = Uint8Array.from(buffer);
if (uint8Array.length == 0) {
return undefined;
}
return uint8Array;
} catch (error) {
console.error(error);
Expand Down
53 changes: 29 additions & 24 deletions projects/portal/src/app/utils/validater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,35 @@ import { cosmosclient } from '@cosmos-client/core';
export const validatePrivateStoredWallet = (
privateStoredWallet: StoredWallet & { privateKey: string },
): boolean => {
const cosmosPrivateKey = createCosmosPrivateKeyFromString(
privateStoredWallet.key_type,
privateStoredWallet.privateKey,
);
if (!cosmosPrivateKey) {
try {
const cosmosPrivateKey = createCosmosPrivateKeyFromString(
privateStoredWallet.key_type,
privateStoredWallet.privateKey,
);
if (!cosmosPrivateKey) {
return false;
}
const cosmosPublicKey = createCosmosPublicKeyFromString(
privateStoredWallet.key_type,
privateStoredWallet.public_key,
);
if (!cosmosPublicKey) {
return false;
}
const matchPrivateKeyAndPublicKey =
cosmosPrivateKey.pubKey().bytes().toString() !== cosmosPublicKey.bytes().toString();
if (matchPrivateKeyAndPublicKey) {
return false;
}
const matchPublicKeyAndAddress =
cosmosclient.AccAddress.fromPublicKey(cosmosPublicKey).toAccAddress().toString() !==
privateStoredWallet.address;
if (matchPublicKeyAndAddress) {
return false;
}
return true;
} catch (error) {
console.error(error);
return false;
}
const cosmosPublicKey = createCosmosPublicKeyFromString(
privateStoredWallet.key_type,
privateStoredWallet.public_key,
);
if (!cosmosPublicKey) {
return false;
}
const matchPrivateKeyAndPublicKey =
cosmosPrivateKey.pubKey().bytes().toString() !== cosmosPublicKey.bytes().toString();
if (matchPrivateKeyAndPublicKey) {
return false;
}
const matchPublicKeyAndAddress =
cosmosclient.AccAddress.fromPublicKey(cosmosPublicKey).toAccAddress().toString() !==
privateStoredWallet.address;
if (matchPublicKeyAndAddress) {
return false;
}
return true;
};

0 comments on commit 8a100bf

Please sign in to comment.