From 9b77042b11a428a3a93e14ae1af0d6558a7fa9a0 Mon Sep 17 00:00:00 2001 From: aleacevedo Date: Mon, 7 Nov 2022 17:42:12 -0300 Subject: [PATCH 1/2] feat: implement importAccounFromSecretKey --- src/PlugKeyRing/index.ts | 39 ++++++++++++++++++++++++++- src/PlugKeyRing/interfaces.ts | 4 +++ src/utils/account/constants.ts | 1 + src/utils/identity/identityFactory.ts | 1 + 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/PlugKeyRing/index.ts b/src/PlugKeyRing/index.ts index b008d163..fe5f40ff 100644 --- a/src/PlugKeyRing/index.ts +++ b/src/PlugKeyRing/index.ts @@ -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 @@ -276,6 +278,41 @@ class PlugKeyRing { return wallet; }; + public importAccountFromPrivateKey = async ({ + icon, + name, + secretKey, + }: ImportFromSecretKey + ): Promise => { + 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 diff --git a/src/PlugKeyRing/interfaces.ts b/src/PlugKeyRing/interfaces.ts index 678b2195..611afd8f 100644 --- a/src/PlugKeyRing/interfaces.ts +++ b/src/PlugKeyRing/interfaces.ts @@ -26,6 +26,10 @@ export interface ImportFromPemOptions extends CreatePrincipalOptions { pem: string; } +export interface ImportFromSecretKey extends CreatePrincipalOptions { + secretKey: string; +} + export interface GetPrincipalFromPem { pem: string; } diff --git a/src/utils/account/constants.ts b/src/utils/account/constants.ts index a8238155..278f2eef 100644 --- a/src/utils/account/constants.ts +++ b/src/utils/account/constants.ts @@ -13,4 +13,5 @@ export enum Types { mnemonic = "MNEMONIC", pem256k1 = "PEM_256k1", pem25519 = "PEM_25519", + secretKey256k1 = "SECRET_KEY_256k1", } diff --git a/src/utils/identity/identityFactory.ts b/src/utils/identity/identityFactory.ts index 15147936..cb4c05aa 100644 --- a/src/utils/identity/identityFactory.ts +++ b/src/utils/identity/identityFactory.ts @@ -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); From f3e9e3f65c07494eed8e8ee6768711d0531b64a9 Mon Sep 17 00:00:00 2001 From: aleacevedo Date: Mon, 7 Nov 2022 17:45:13 -0300 Subject: [PATCH 2/2] chore: bump version --- package.json | 2 +- src/constants/version.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index e559a2ef..1f3f7fbc 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/constants/version.ts b/src/constants/version.ts index 613835e2..8b948c80 100644 --- a/src/constants/version.ts +++ b/src/constants/version.ts @@ -1 +1 @@ -export const PLUG_CONTROLLER_VERSION = "0.24.9"; +export const PLUG_CONTROLLER_VERSION = "0.24.10";