Use
Keys and signatures
PrivateKey, PublicKey, and Signature create Hive keys and verify signatures.
PrivateKey
A private key is a secp256k1 secret. The WIF string is the storage format.
import { PrivateKey } from 'hive-tx'
const fromWif = PrivateKey.from('5JdeC9P7Pbd1uGdFVEsJ41EkEnADbbHGq6p1BwFxm6txNBsQnsw')
const random = PrivateKey.randomKey()
const fromSeed = PrivateKey.fromSeed('my-secret-seed')
const posting = PrivateKey.fromLogin('alice', 'master-password', 'posting')
| Method | Input | Output |
|---|---|---|
from | WIF string or 32-byte Uint8Array | PrivateKey |
fromString | WIF string | PrivateKey |
fromSeed | UTF-8 string, hex string, or bytes | SHA-256 of the seed as a key |
fromLogin | username, password, role | Hive login key for that role |
randomKey | none | New random key |
createPublic | optional prefix | PublicKey |
sign | 32-byte hash | Signature |
toString | none | WIF string |
inspect | none | Masked WIF string |
getSharedSecret | PublicKey | 64-byte secret for memo encryption |
Roles for fromLogin are owner, active, posting, and memo. The default role is active.
The seed for fromLogin is username + role + password.
PublicKey
A Hive public key string starts with the address prefix. The default prefix is STM.
import { PublicKey } from 'hive-tx'
const pub = privateKey.createPublic()
const fromStr = PublicKey.from('STM8m5UgaFAAYQRuaNejYdS8FVLVp9Ss3K1qAVk5de6F8s3HnVbvA')
const ok = pub.verify(messageHash, signature)
| Method | Function |
|---|---|
from / fromString | Read a public key string. |
verify | Return true if the signature matches the 32-byte hash. |
toString / toJSON | Return the public key string with prefix. |
Signature
A compact recoverable signature is a hex string with 130 characters.
import { Signature } from 'hive-tx'
const sig = Signature.from(hex)
const recovered = sig.getPublicKey(messageHash)
const hexOut = sig.customToString()
getPublicKey needs a 32-byte hash as Uint8Array or a 64-character hex string.
To recover a Hive Keychain requestSignBuffer proof:
- Create a SHA-256 hash of the UTF-8 message.
- Remove a leading
0xfrom the signature if it is present. - Call
Signature.from(hex).getPublicKey(hash).toString().