# Use Crypto Utilities
You can work with the Private Key, Public Key, and Address methods.
# Private Key
You can create a private key from:
- Seed phrase.
- Randomly generated seed phrase.
- Seed bytes.
- Randomly generated seed bytes.
- Bytes.
- Encoded string.
# Create Private Key from Seed Phrase
// Necessary import.
import { keyPair } from '@waves/ts-lib-crypto';
// Specify your account's seed phrase.
const seedPhrase: string = 'PASTE A SEED PHRASE';
// Create the key pair, including the private and public keys.
const accountKeyPair = keyPair(seedPhrase);
// Print the private key.
console.log("Sender's Private Key:", accountKeyPair.privateKey);
# Generate Random Seed Phrase and Private Key
// Necessary import.
import { keyPair, randomSeed } from '@waves/ts-lib-crypto';
// Generate a random seed phrase.
const seedPhrase: string = randomSeed();
// Create the key pair, including the private and public keys.
const accountKeyPair = keyPair(seedPhrase);
// Print the private key.
console.log("Seed Phrase:", seedPhrase);
console.log("Sender's Private Key:", accountKeyPair.privateKey);
# Create Private Key from Seed Bytes
// Necessary import.
import { keyPair } from '@waves/ts-lib-crypto';
// Create a byte array.
const seedBytes: Uint8Array = Uint8Array.from(
[1, 2, 3, 4, 5, 6, 7] // Array example.
);
// Convert the byte array to a string seed.
const seedString: string = new TextDecoder().decode(seedBytes);
// Create the key pair, including the private and public keys.
const accountKeyPair = keyPair(seedString);
// Print the private key.
console.log("Sender's Private Key:", accountKeyPair.privateKey);
# Create Private Key from Randomly Generated Seed Bytes
// Necessary import.
import { randomBytes, blake2b, keyPair } from '@waves/ts-lib-crypto';
// Generate random seed bytes. 32 bytes by default.
const randomSeedBytes: Uint8Array = randomBytes(32);
// Derive the private key from the random seed bytes.
const hashedSeed: Uint8Array = blake2b(randomSeedBytes);
// Create the key pair from the hashed seed.
const accountKeyPair = keyPair(hashedSeed);
// Print the private key.
console.log("Sender's Private Key:", accountKeyPair.privateKey);
# Create Private Key from Bytes
// Necessary import.
import { keyPair } from '@waves/ts-lib-crypto';
// Define the private key bytes.
const privateKeyBytes: Uint8Array = Uint8Array.from([
56, 253, 37, 64, 2, 38, 78, 37, 158, 211, 233, 117, 14, 88, 20, 42,
247, 21, 55, 87, 117, 248, 175, 77, 77, 157, 169, 233, 7, 116, 157, 236
]); // Array example.
// Create the key pair, including the private and public keys.
const accountKeyPair = keyPair(privateKeyBytes);
// Print the private key.
console.log("Sender's Private Key:", accountKeyPair.privateKey);
# Create Private Key from an Encoded String
// Necessary import.
import { base58Decode, keyPair } from '@waves/ts-lib-crypto';
// Define the Base58-encoded private key string.
const base58String: string = 'A5u9Ugt2nG1rWfLkL7pzZrtkP8LgF2rD3g1XBjsF8ZfV'; // Value example.
// Decode the Base58 string to raw bytes.
const privateKeyBytes: Uint8Array = base58Decode(base58String);
// Create the key pair, including the private and public keys.
const accountKeyPair = keyPair(privateKeyBytes);
// Print the private key.
console.log("Sender's Private Key:", accountKeyPair.privateKey);
# Public Key
You can create a public key from the seed's key pair:
- Generate the private key.
- Place the following code after the private key generation:
// Generate the public key from the existing key pair. const publicKey: string = accountKeyPair.publicKey; // Print the public key. console.log("Sender's Public Key:", publicKey);
# Address
You can create an address from a public key:
Place the following code after the public key generation:
NOTE: Ensure importing the following dependencies:
import { address } from '@waves/ts-lib-crypto';
// Necessary import. import { address } from '@waves/ts-lib-crypto'; /* * Specify the network chain ID: * - 'W' for Mainnet. * - 'T' for Testnet. */ const chainId: string = 'T' // Generate the address from the given public key and chain ID. const senderAddress = address(publicKey, chainId); // Print the address. console.log("Sender's Address:", senderAddress);