PrivateKey class
Default implementation of the Private interface, with decryption support. In theory, RSA, ed25519, and secp256k1 key types (and more) should be supported, although currently only ed25519 has full sign and decrypt capabilities.
Signature:
export declare class PrivateKey implements Private
Implements: Private
Example 1
Create a private key, extract the public key string
import { PrivateKey } from '@textile/hub'
function example() {
const identity = PrivateKey.fromRandom()
const publicKey = identity.public.toString()
return publicKey
}
Example 2
Encrypt a message using another person's public key (string)
import { PublicKey } from '@textile/hub'
async function example(publicKey: string) {
const recipient = PublicKey.fromString(publicKey)
const msg = new TextEncoder().encode('howdy!')
const ciphertext = await recipient.encrypt(msg)
return ciphertext
}
Example 3
Decrypt a message sent to the local user with their private key
import { PrivateKey } from '@textile/hub'
async function example(identity: PrivateKey, ciphertext: Uint8Array) {
const plaintext = await identity.decrypt(ciphertext)
return plaintext
}
Example 4
Encrypt the local user's own message for later reading.
import { PrivateKey } from '@textile/hub'
async function example(identity: PrivateKey) {
const msg = new TextEncoder().encode('private message!')
const ciphertext = await identity.public.encrypt(msg)
return ciphertext // can only be decrypted by the local user
}
Constructors
Constructor | Modifiers | Description |
---|---|---|
(constructor)(secretKey, type) | Constructor |
Properties
Property | Modifiers | Type | Description |
---|---|---|---|
bytes | Uint8Array | Return the protobuf-encoded bytes of this private key. | |
privKey | Uint8Array | The raw private key bytes. | |
pubKey | Uint8Array | The raw public key bytes. | |
public | PublicKey | Get the public key associated with this identity. | |
seed | Uint8Array | The raw 32-byte secret seed | |
type | string |
Methods
Method | Modifiers | Description |
---|---|---|
canSign() | Returns true if this key contains a secret key and can sign. | |
decrypt(ciphertext) | Decrypt the given ciphertext using this private key. | |
fromRandom() | static | Create a random PrivateKey. |
fromRawEd25519Seed(rawSeed) | static | Creates a new PrivateKey from ed25519 secret key seed raw bytes. |
fromString(str) | static | Create a PrivateKey from the result of calling toString() . |
sign(data) | Sign the message using this private key and return the signature. | |
toString() | Return the base32-encoded multibase string of the bytes of this private key. Useful for encoding the key for sharing etc. |