Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Static Method to Account Class for Key Validation #931

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/neon-core/src/wallet/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ import { NeonObject } from "../model";

const log = logger("wallet");

export enum KeyType {
PrivateKey = "PrivateKey",
PublicKeyUnencoded = "PublicKeyUnencoded",
PublicKeyEncoded = "PublicKeyEncoded",
ScriptHash = "ScriptHash",
Address = "Address",
WIF = "WIF",
NEP2 = "NEP2",
Unknown = "",
}

export interface AccountJSON {
/** Base58 encoded string */
address: string;
Expand Down Expand Up @@ -88,6 +99,23 @@ export class Account implements NeonObject<AccountJSON> {
});
}



public static validateKey(str: string): KeyType {
switch (true) {
case isPrivateKey(str): return KeyType.PrivateKey;
case isPublicKey(str, false): return KeyType.PublicKeyUnencoded;
case isPublicKey(str, true): return KeyType.PublicKeyEncoded;
case isScriptHash(str): return KeyType.ScriptHash;
case isAddress(str): return KeyType.Address;
case isWIF(str): return KeyType.WIF;
case isNEP2(str): return KeyType.NEP2;
default:
// returning empty string enables falsly checks
return KeyType.Unknown;
}
}

public isDefault: boolean;
public lock: boolean;
public contract: {
Expand Down