Skip to content

Commit f3fb9b7

Browse files
authored
Merge pull request #245 from Psychedelic/feat/import-from-secretKey
feat: implement importAccounFromSecretKey
2 parents 71bb270 + f3e9e3f commit f3fb9b7

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"typescript": "^4.5"
5959
},
6060
"name": "@psychedelic/plug-controller",
61-
"version": "0.24.9",
61+
"version": "0.24.10",
6262
"description": "Internet Computer Plug wallet's controller",
6363
"main": "dist/index.js",
6464
"scripts": {

src/PlugKeyRing/index.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ import {
3636
ImportMnemonicOptions,
3737
ImportFromPemOptions,
3838
GetPrincipalFromPem,
39-
ValidatePemResponse
39+
ValidatePemResponse,
40+
ImportFromSecretKey
4041
} from './interfaces';
4142
import { WALLET_METHODS, MAIN_WALLET_METHODS } from './constants';
4243
import { getIdentityFromPem } from './../utils/identity/parsePem'
44+
import Secp256k1KeyIdentity from '../utils/identity/secpk256k1/identity';
4345

4446
class PlugKeyRing {
4547
// state
@@ -276,6 +278,41 @@ class PlugKeyRing {
276278
return wallet;
277279
};
278280

281+
public importAccountFromPrivateKey = async ({
282+
icon,
283+
name,
284+
secretKey,
285+
}: ImportFromSecretKey
286+
): Promise<PlugWallet> => {
287+
await this.checkInitialized();
288+
this.checkUnlocked();
289+
const walletId = uuid();
290+
const orderNumber = Object.keys(this.state.wallets).length;
291+
const buffSecretKey = Buffer.from(secretKey, 'hex');
292+
const identity = Secp256k1KeyIdentity.fromSecretKey(buffSecretKey);
293+
const wallet = new PlugWallet({
294+
icon,
295+
name,
296+
walletId,
297+
orderNumber,
298+
fetch: this.fetch,
299+
network: this.networkModule.network,
300+
type: Types.secretKey256k1,
301+
identity,
302+
});
303+
304+
if (this.checkRepeatedAccount(wallet.principal)) {
305+
throw new Error(ERRORS.INVALID_ACCOUNT);
306+
}
307+
308+
const wallets = { ...this.state.wallets, [walletId]: wallet };
309+
this.state.wallets = wallets;
310+
await this.saveEncryptedState({ wallets }, this.state.password);
311+
return wallet;
312+
};
313+
314+
315+
279316
public getPrincipalFromPem = async ({
280317
pem,
281318
}: GetPrincipalFromPem

src/PlugKeyRing/interfaces.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export interface ImportFromPemOptions extends CreatePrincipalOptions {
2626
pem: string;
2727
}
2828

29+
export interface ImportFromSecretKey extends CreatePrincipalOptions {
30+
secretKey: string;
31+
}
32+
2933
export interface GetPrincipalFromPem {
3034
pem: string;
3135
}

src/constants/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const PLUG_CONTROLLER_VERSION = "0.24.9";
1+
export const PLUG_CONTROLLER_VERSION = "0.24.10";

src/utils/account/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ export enum Types {
1313
mnemonic = "MNEMONIC",
1414
pem256k1 = "PEM_256k1",
1515
pem25519 = "PEM_25519",
16+
secretKey256k1 = "SECRET_KEY_256k1",
1617
}

src/utils/identity/identityFactory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ERRORS } from '../../errors';
77
export class IdentityFactory {
88
public static createIdentity(type: string, secretKey: string): GenericSignIdentity {
99
switch (type) {
10+
case Types.secretKey256k1:
1011
case Types.pem256k1:
1112
case Types.mnemonic:
1213
return Secp256k1KeyIdentity.fromJSON(secretKey);

0 commit comments

Comments
 (0)