PublicKey class
Default implementation of the Public interface, with encryption support. In theory, RSA, ed25519, and secp256k1 key types (and more) should be supported, although currently only ed25519 has full verify and encrypt capabilities.
Signature:
export declare class PublicKey implements Public
Implements: Public
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)(pubKey, type) | Constructs a new instance of the PublicKey class |
Properties
Property | Modifiers | Type | Description |
---|---|---|---|
bytes | Uint8Array | Return the protobuf-encoded bytes of this public key. | |
pubKey | Uint8Array | ||
type | string |
Methods
Method | Modifiers | Description |
---|---|---|
encrypt(data) | Encrypt the given data using this public key. | |
fromString(str) | static | Create a PublicKey from the result of calling toString() . |
toString() | Return the base32-encoded multibase string of the bytes of this public key. Useful for encoding the key for sharing etc. | |
verify(data, signature) | Verifies the signature for the data and returns true if verification succeeded or false if it failed. |