Skip to content

Commit

Permalink
Merge pull request #245 from Psychedelic/feat/import-from-secretKey
Browse files Browse the repository at this point in the history
feat: implement importAccounFromSecretKey
  • Loading branch information
tomiir authored Nov 7, 2022
2 parents 71bb270 + f3e9e3f commit f3fb9b7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"typescript": "^4.5"
},
"name": "@psychedelic/plug-controller",
"version": "0.24.9",
"version": "0.24.10",
"description": "Internet Computer Plug wallet's controller",
"main": "dist/index.js",
"scripts": {
Expand Down
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
2 changes: 1 addition & 1 deletion src/constants/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const PLUG_CONTROLLER_VERSION = "0.24.9";
export const PLUG_CONTROLLER_VERSION = "0.24.10";
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 f3fb9b7

Please sign in to comment.