2.1.1 • Published 2 years ago

@0xbigboss/pocketjs-signer v2.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

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

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

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

Instanciates a new KeyManager from a valid ED25519 private key.

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

ParamTypeDescription
privateKeystringThe private key to use for instanciating the account.

static fromPPK({ password, ppk }): Promise

Instanciates a new KeyManager from a valid Portable-Private-Key file.

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

ParamTypeDescription
passwordstringThe password of the account to instanciate from the PPK.
ppkstringPPK, stringified. (PPKs are valid JSON files)

exportPPK({ privateKey, password, hint }): Promise

Exports a provided private key as a Portable-Private-Key, unlockable with the provided password.

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

ParamTypeDescription
passwordstringThe password to use in the PPK.
privateKeystringThe private key to create the PPK from.
hintstringThe password hint.

exportPPK({ password, hint }): Promise

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.

ParamTypeDescription
passwordstringThe password to use in the PPK.
hintstringThe password hint.

sign(payload): Promise

Signs a valid hex-string payload with the imported private key.

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

ParamTypeDescription
payloadstringThe 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.