This guide will help you get up and running with the Guardian TypeScript SDK quickly.
- Node.js 16+
- TypeScript 4.5+
- npm or yarn
- Basic knowledge of Ethereum and smart contracts
# Install the SDK
npm install @guardian/sdk
# Or with yarn
yarn add @guardian/sdkimport { SecureOwnable, DynamicRBAC } from '@guardian/sdk/typescript'
import { createPublicClient, createWalletClient, http } from 'viem'
import { mainnet } from 'viem/chains'
import { privateKeyToAccount } from 'viem/accounts'// Public client for read operations
const publicClient = createPublicClient({
chain: mainnet,
transport: http('https://eth-mainnet.alchemyapi.io/v2/your-api-key')
})
// Wallet client for write operations (optional)
const account = privateKeyToAccount('0x...') // Your private key
const walletClient = createWalletClient({
account,
chain: mainnet,
transport: http('https://eth-mainnet.alchemyapi.io/v2/your-api-key')
})// SecureOwnable contract
const secureOwnable = new SecureOwnable(
publicClient,
walletClient, // optional
'0x...', // contract address
mainnet
)
// DynamicRBAC contract
const dynamicRBAC = new DynamicRBAC(
publicClient,
walletClient, // optional
'0x...', // contract address
mainnet
)// Get contract owner
const owner = await secureOwnable.owner()
console.log('Owner:', owner)
// Get time lock period
const timeLockPeriod = await secureOwnable.getTimeLockPeriodSec()
console.log('Time lock period:', timeLockPeriod)
// Check if role editing is enabled
const roleEditingEnabled = await dynamicRBAC.roleEditingEnabled()
console.log('Role editing enabled:', roleEditingEnabled)// Request ownership transfer (requires wallet client)
const txHash = await secureOwnable.transferOwnershipRequest(
'0x...', // new owner address
{ from: account.address }
)
console.log('Transaction hash:', txHash)
// Wait for transaction confirmation
const receipt = await publicClient.waitForTransactionReceipt({ hash: txHash })
console.log('Transaction confirmed:', receipt.status)// Listen for ownership transfer events
const unwatch = publicClient.watchContractEvent({
address: '0x...', // contract address
abi: secureOwnable.abi,
eventName: 'OwnershipTransferRequested',
onLogs: (logs) => {
console.log('Ownership transfer requested:', logs)
}
})
// Stop watching
unwatch()# Clone the repository
git clone https://github.com/PracticalParticle/Guardian.git
cd guardian
# Install dependencies
npm install
# Compile contracts
npm run compile:truffle
# Run tests
npm run test:truffle// Test with local network
const localClient = createPublicClient({
chain: {
id: 1337,
name: 'local',
nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
rpcUrls: {
default: { http: ['http://127.0.0.1:8545'] }
}
},
transport: http('http://127.0.0.1:8545')
})
// Deploy and test locally
const localSecureOwnable = new SecureOwnable(
localClient,
undefined,
'0x...', // deployed contract address
{ id: 1337, name: 'local' }
)// Never hardcode private keys
// Use environment variables
const privateKey = process.env.PRIVATE_KEY
if (!privateKey) {
throw new Error('PRIVATE_KEY environment variable is required')
}
const account = privateKeyToAccount(privateKey)// Always validate addresses
function isValidAddress(address: string): boolean {
return /^0x[a-fA-F0-9]{40}$/.test(address)
}
// Validate before making transactions
if (!isValidAddress(newOwner)) {
throw new Error('Invalid address provided')
}try {
const txHash = await secureOwnable.transferOwnershipRequest(newOwner)
console.log('Transaction successful:', txHash)
} catch (error) {
console.error('Transaction failed:', error.message)
// Handle specific error types
if (error.message.includes('insufficient funds')) {
console.log('Please add more ETH to your account')
}
}- Read the API Reference: API Reference
- Explore SecureOwnable: SecureOwnable Guide
- Learn about DynamicRBAC: DynamicRBAC Guide
- Check Examples: Basic Examples
Solution: Ensure the contract address is correct and the contract is deployed on the network you're using.
Solution: Add more ETH to your account or use a testnet with faucet.
Solution: Check the contract's requirements (e.g., only owner can call certain functions).
Solution: Ensure your client is configured for the correct network.
- Documentation: Check the API Reference
- Examples: See Basic Examples
- Issues: GitHub Issues
- Discord: Join our Discord
Ready to dive deeper? Check out the API Reference for detailed documentation of all SDK methods.