@chain1/stok-utils
v1.0.1
Published
Utility functions for StoChain blockchain applications - date formatting, number formatting, crypto operations, storage, and blockchain utilities
Maintainers
Readme
@chain1/stok-utils
Comprehensive utility library for StoChain blockchain applications providing utilities for date formatting, number formatting, crypto operations, storage management, and blockchain interactions.
Installation
npm install @chain1/stok-utilsor
yarn add @chain1/stok-utilsFeatures
📅 Date Utilities
- Date formatting with multiple format options
- Korean date formatting support
- Today/date range helpers
- Zero dependencies
🔢 Number Utilities
- Thousands separator formatting
- Decimal precision control
- Compact number notation (K, M, B)
- Zero dependencies
💰 Currency Formatting
- Multi-currency support (KRW, USD, CNY, etc.)
- Automatic precision handling
- Locale-aware formatting
- Zero dependencies
🔐 Crypto & Security
- PIN-based encryption/decryption
- Private key management
- Secure storage integration
- Recovery phrase generation
⛓️ Blockchain Utilities
- Multi-chain support (ETH, BSC, STO, KAIA)
- Wallet management
- Token balance queries
- Transaction sending
- Gas price estimation
- ERC-20 token operations
Usage
Core Utilities (No Dependencies)
import {
formatDate,
numberWithCommas,
formatDisplayAmount
} from '@chain1/stok-utils';
// Date formatting
const date = formatDate(new Date().toISOString(), 'korean');
// "2025년 11월 08일 10:00"
// Number formatting
const amount = numberWithCommas(1000000);
// "1,000,000"
// Currency formatting
const price = formatDisplayAmount(1234.56, 'usd');
// "1,234.56"Blockchain Operations
Note: Requires ethers peer dependency
import {
getWalletInfoFromPrivateKey,
getChainBalance,
sendToken
} from '@chain1/stok-utils';
// Get wallet info from private key
const walletInfo = await getWalletInfoFromPrivateKey(
privateKey,
'ETH'
);
// { address, balance, chain, chainSymbol, privateKey }
// Get balance for specific chain
const balance = await getChainBalance(address, 'BSC');
// Send tokens
const result = await sendToken(
privateKey,
recipientAddress,
amount,
'STO'
);Crypto Operations
Note: Requires React Native dependencies
import {
encryptPrivateKey,
decryptPrivateKey,
generateRecoveryPhrase
} from '@chain1/stok-utils';
// Encrypt private key with user ID and PIN
const encrypted = await encryptPrivateKey(privateKey, userId);
// Decrypt private key
const decrypted = await decryptPrivateKey(encrypted, userId);
// Generate recovery phrase
const phrase = generateRecoveryPhrase();
// "word1 word2 word3 ... word12"Storage Operations
Note: Requires React Native dependencies
import {
saveUserId,
getUserId,
saveMbId,
clearUserData
} from '@chain1/stok-utils';
// Save user ID securely
await saveUserId('user-uuid');
// Retrieve user ID
const userId = await getUserId();
// Clear all user data
await clearUserData();Peer Dependencies
Core Utilities
No dependencies required for date, number, and format utilities.
Blockchain Utilities
{
"ethers": "^5.7.0"
}Crypto & Storage (React Native)
{
"react-native": ">=0.60.0",
"@react-native-async-storage/async-storage": "^1.15.0",
"react-native-crypto-js": "^1.0.0",
"expo-crypto": "^14.0.0",
"expo-secure-store": "^14.0.0",
"react-native-aes-crypto": "^3.2.0",
"@supabase/supabase-js": "^2.38.0"
}All React Native dependencies are optional. Install only what you need based on the features you use.
API Reference
Date Utilities
formatDate(isoDateString, format)
Formats an ISO date string to a readable format.
Parameters:
isoDateString(string): ISO format date stringformat(string): Format type - 'default', 'korean', 'short', 'time', 'date', 'korean-date'
Returns: Formatted date string
getTodayTimeRange()
Returns today's start and end time in ISO format.
Returns: { startTime, endTime }
isToday(date)
Checks if a date is today.
Parameters:
date(string|Date): Date to check
Returns: boolean
Number Utilities
numberWithCommas(number)
Adds thousands separator to a number.
Parameters:
number(number|string): Number to format
Returns: Formatted string with commas
formatDecimal(number, decimals)
Limits decimal places of a number.
Parameters:
number(number|string): Number to formatdecimals(number): Decimal places (default: 2)
Returns: Formatted decimal string
formatCompactNumber(number)
Converts numbers to compact notation (K, M, B).
Parameters:
number(number|string): Number to format
Returns: Compact notation string
Currency Utilities
formatDisplayAmount(amount, currency)
Formats amount according to currency standards.
Parameters:
amount(number|string): Amount to formatcurrency(string): Currency code ('krw', 'usd', 'cny', etc.)
Returns: Formatted amount string
Blockchain Utilities
getWalletInfoFromPrivateKey(privateKey, chain)
Gets wallet information from a private key.
Parameters:
privateKey(string): Ethereum format private keychain(string): Chain type ('STO', 'ETH', 'BSC', 'KAIA')
Returns: Promise<{ address, balance, chain, chainSymbol, privateKey }>
getChainBalance(address, chain)
Queries balance for a specific chain.
Parameters:
address(string): Wallet addresschain(string): Chain type (default: 'STO')
Returns: Promise - Balance in wei
sendToken(privateKey, recipientAddress, amount, chain)
Sends native tokens.
Parameters:
privateKey(string): Sender's private keyrecipientAddress(string): Recipient addressamount(number|string): Amount to sendchain(string): Chain type (default: 'STO')
Returns: Promise<{ success, txHash, receipt }>
transferERC20Token(tokenAddress, recipientAddress, amount, privateKey, chain)
Transfers ERC-20 tokens.
Parameters:
tokenAddress(string): Token contract addressrecipientAddress(string): Recipient addressamount(string): Amount to transferprivateKey(string): Sender's private keychain(string): Chain type (default: 'STO')
Returns: Promise<{ success, txHash, receipt, amount, token }>
Crypto Utilities
encryptPrivateKey(privateKey, userId)
Encrypts a private key with user ID and optional PIN.
Parameters:
privateKey(string): Private key to encryptuserId(string): User identifier
Returns: Promise - Encrypted data
decryptPrivateKey(encryptedData, userId)
Decrypts an encrypted private key.
Parameters:
encryptedData(string): Encrypted private keyuserId(string): User identifier
Returns: Promise - Decrypted private key
generateRecoveryPhrase()
Generates a 12-word recovery phrase.
Returns: string - 12-word mnemonic phrase
Storage Utilities
saveUserId(userId)
Saves user ID to secure storage.
Parameters:
userId(string): User UUID
getUserId()
Retrieves saved user ID.
Returns: Promise<string|null>
clearUserData()
Clears all stored user data.
Returns: Promise
Supported Chains
- ETH: Ethereum Mainnet
- BSC: Binance Smart Chain (Testnet & Mainnet)
- STO: StoChain
- KAIA: KAIA Network (Baobab Testnet)
License
MIT License
Author
chain1
Repository
https://github.com/chain1/stok-utils
Issues
https://github.com/chain1/stok-utils/issues
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
