# Key Manager

This package houses both the abstract Key Manager interface we expose to people so that they build their own KeyManager, and an actual KeyManager ready to use for handling account-related operations.
## Installation
Install through your package manager of choice:
```
pnpm install @pokt-foundation/pocketjs-signer
```

## Usage

```js 
import { AbstractSigner, KeyManager } from '@pokt-foundation/pocketjs-signer'

// For the AbstractSigner, just implement it!
class MySigner extends AbstractSigner {
 // Now override the required methods...
}

// For the KeyManager, there's a few ways you can initialize it:
// 1. Create a random (new) account with the KeyManager
const randomAccount = await KeyManager.createRandom()

// 2. Use a private key you own
const account = await KeyManager.fromPrivateKey(process.env.PRIVATE_KEY);

// 3. Use a Portable-Private-Key file, stringified.
// A PPK is generated by exporting an account through the `exportPPK` method.
const ppkAccount = await KeyManager.fromPPK({ password: process.env.PASS, ppk: process.env.PPK })

// Now, you can use it!
// Get your address
const address = account.getAddress()

// Get your public key 
const publicKey = account.getPublicKey()

// Sign a message
const signedCoffee = account.sign("0xcafe")

```

## KeyManager API

### Constructor
Instanciating through the constructor is only possible if all params are known beforehand. If not, use one of the initialization methods provided.

#### address
- type: 'String'
The address of the account to instanciate manually.

#### privateKey
- type: 'String'
The private key of the account to instanciate manually.

#### publicKey
- type: 'String'
The public key of the account to instanciate manually.

### Methods
#### static createRandom(): Promise<KeyManager>
Creates a new, random pocket account. Can be called from the base class as it's static.

Returns a `Promise<KeyManager>`: The new key manager instance with the account attached.

#### static fromPrivateKey(privateKey): Promise<KeyManager>
Instanciates a new KeyManager from a valid ED25519 private key.

Returns a `Promise<KeyManager>`: The new key manager instance with the account attached.

| Param      | Type     | Description                                           |
|------------|----------|-------------------------------------------------------|
| privateKey | `string` | The private key to use for instanciating the account. |

#### static fromPPK({ password, ppk }): Promise<KeyManager>
Instanciates a new KeyManager from a valid Portable-Private-Key file.

Returns a `Promise<KeyManager>`: The new key manager instance with the account attached.

| Param    | Type     | Description                                              |
|----------|----------|----------------------------------------------------------|
| password | `string` | The password of the account to instanciate from the PPK. |
| ppk      | `string` | PPK, stringified. (PPKs are valid JSON files)            |

#### exportPPK({ privateKey, password, hint }): Promise<string>
Exports a provided private key as a Portable-Private-Key, unlockable with the provided password.

Returns a `Promise<string>`: A PPK, in stringified manner.

| Param      | Type     | Description                             |
|------------|----------|-----------------------------------------|
| password   | `string` | The password to use in the PPK.         |
| privateKey | `string` | The private key to create the PPK from. |
| hint       | `string` | The password hint.                      |

#### exportPPK({ password, hint }): Promise<string>
Exports the current account's private key as a Portable-Private-Key, unlockable with the provided password.

Returns a `Promise<string>`: A PPK, in stringified manner.

| Param      | Type     | Description                             |
|------------|----------|-----------------------------------------|
| password   | `string` | The password to use in the PPK.         |
| hint       | `string` | The password hint.                      |

#### sign(payload): Promise<string>
Signs a valid hex-string payload with the imported private key.

Returns a `Promise<string>`: The signed payload as a string.

| Param   | Type     | Description              |
|---------|----------|--------------------------|
| payload | `string` | The hex payload to sign. |

#### getAddress(): string
Gets the account address.

Returns `string`: The attached account's address.

#### getPublicKey(): string.
Gets teh account public key.

Returns `string`: The attached account's public key.

#### getPrivateKey(): string
Gets the account private key.

Returns `string`: The attached account's private key.

#### getAccount(): Account
Gets the attached account.

Returns `Account`: The attached account in its entirety in an object.
