Skip to content

Commit

Permalink
feat: implement importAccounFromSecretKey
Browse files Browse the repository at this point in the history
  • Loading branch information
alejo-acevedo-deel committed Nov 7, 2022
1 parent 71bb270 commit 9b77042
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/PlugKeyRing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ import {
ImportMnemonicOptions,
ImportFromPemOptions,
GetPrincipalFromPem,
ValidatePemResponse
ValidatePemResponse,
ImportFromSecretKey
} from './interfaces';
import { WALLET_METHODS, MAIN_WALLET_METHODS } from './constants';
import { getIdentityFromPem } from './../utils/identity/parsePem'
import Secp256k1KeyIdentity from '../utils/identity/secpk256k1/identity';

class PlugKeyRing {
// state
Expand Down Expand Up @@ -276,6 +278,41 @@ class PlugKeyRing {
return wallet;
};

public importAccountFromPrivateKey = async ({
icon,
name,
secretKey,
}: ImportFromSecretKey
): Promise<PlugWallet> => {
await this.checkInitialized();
this.checkUnlocked();
const walletId = uuid();
const orderNumber = Object.keys(this.state.wallets).length;
const buffSecretKey = Buffer.from(secretKey, 'hex');
const identity = Secp256k1KeyIdentity.fromSecretKey(buffSecretKey);
const wallet = new PlugWallet({
icon,
name,
walletId,
orderNumber,
fetch: this.fetch,
network: this.networkModule.network,
type: Types.secretKey256k1,
identity,
});

if (this.checkRepeatedAccount(wallet.principal)) {
throw new Error(ERRORS.INVALID_ACCOUNT);
}

const wallets = { ...this.state.wallets, [walletId]: wallet };
this.state.wallets = wallets;
await this.saveEncryptedState({ wallets }, this.state.password);
return wallet;
};



public getPrincipalFromPem = async ({
pem,
}: GetPrincipalFromPem
Expand Down
4 changes: 4 additions & 0 deletions src/PlugKeyRing/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export interface ImportFromPemOptions extends CreatePrincipalOptions {
pem: string;
}

export interface ImportFromSecretKey extends CreatePrincipalOptions {
secretKey: string;
}

export interface GetPrincipalFromPem {
pem: string;
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/account/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export enum Types {
mnemonic = "MNEMONIC",
pem256k1 = "PEM_256k1",
pem25519 = "PEM_25519",
secretKey256k1 = "SECRET_KEY_256k1",
}
1 change: 1 addition & 0 deletions src/utils/identity/identityFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ERRORS } from '../../errors';
export class IdentityFactory {
public static createIdentity(type: string, secretKey: string): GenericSignIdentity {
switch (type) {
case Types.secretKey256k1:
case Types.pem256k1:
case Types.mnemonic:
return Secp256k1KeyIdentity.fromJSON(secretKey);
Expand Down

0 comments on commit 9b77042

Please sign in to comment.