nvc-sdk-wallet-sol
v1.0.0
Published
Solana Wallet SDK for React Native - TrustWallet/Phantom style
Maintainers
Readme
Solana Wallet SDK
SDK Solana Wallet cho React Native với style tương tự TrustWallet/Phantom. Hỗ trợ đầy đủ các chức năng quản lý ví, giao dịch, swap và tích hợp Solscan.
Tính năng
- ✅ Tạo ví Solana mới với mnemonic 12 từ
- ✅ Tạo nhiều ví con từ mnemonic gốc
- ✅ Import ví từ private key hoặc mnemonic
- ✅ Gửi và nhận SOL và SPL tokens
- ✅ Swap onchain các token trên Solana (tích hợp Jupiter)
- ✅ Xem lịch sử giao dịch từ Solscan
- ✅ Config mainnet/devnet
- ✅ Encrypt private key với AES + password
- ✅ Token balance cache
- ✅ Adapter riêng cho React Native với AsyncStorage
- ✅ TypeScript support đầy đủ
Cài đặt
npm install nvc-sdk-wallet-sol
# hoặc
yarn add nvc-sdk-wallet-solPeer Dependencies
SDK này yêu cầu các peer dependencies sau:
npm install react-native @react-native-async-storage/async-storageSử dụng
Khởi tạo SDK
import { ReactNativeAdapter, Network } from 'nvc-sdk-wallet-sol';
// Khởi tạo với React Native Adapter
const adapter = new ReactNativeAdapter({
network: Network.MAINNET, // hoặc Network.DEVNET
rpcUrl: 'https://api.mainnet-beta.solana.com', // optional, custom RPC
solscanApiKey: 'your-api-key', // optional, để sử dụng Solscan API
});
// Initialize adapter (load data từ storage)
await adapter.initialize();Tạo ví mới
// Tạo ví mới
const { account, mnemonic } = await adapter.createWallet({
name: 'My Wallet',
password: 'your-secure-password',
});
// Lưu mnemonic an toàn! Bạn sẽ cần nó để khôi phục ví
console.log('Mnemonic:', mnemonic);
console.log('Public Key:', account.publicKey);Tạo ví con
// Tạo ví con từ mnemonic gốc
const childWallet = await adapter.createChildWallet(
mnemonic,
'your-password',
0, // index
'Child Wallet 1'
);Import ví
// Import từ private key
const importedWallet = await adapter.importFromPrivateKey({
name: 'Imported Wallet',
password: 'your-password',
privateKey: 'your-private-key-base58',
});
// Import từ mnemonic
const importedWallet2 = await adapter.importFromMnemonic({
name: 'Imported Wallet 2',
password: 'your-password',
mnemonic: 'word1 word2 ... word12',
derivationPath: "m/44'/501'/0'/0'", // optional
});Gửi SOL
const sdk = adapter.getSDK();
const currentAccount = sdk.getCurrentAccount();
if (currentAccount) {
const signature = await sdk.sendSOL(
currentAccount.id,
'your-password',
'recipient-address',
0.1 // amount in SOL
);
console.log('Transaction signature:', signature);
}Gửi Token
const signature = await sdk.sendToken(
currentAccount.id,
'your-password',
'recipient-address',
'token-mint-address',
100, // amount
9 // decimals (optional)
);Lấy số dư
// SOL balance (có cache)
const solBalance = await sdk.getSOLBalance(currentAccount.id);
console.log('SOL Balance:', solBalance);
// Token balance (có cache)
const tokenBalance = await sdk.getTokenBalance(
currentAccount.id,
'token-mint-address'
);
if (tokenBalance) {
console.log('Token Balance:', tokenBalance.uiAmount);
}Swap Token
// Get swap quote
const quote = await sdk.getSwapQuote({
fromToken: 'So11111111111111111111111111111111111111112', // SOL
toToken: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', // USDC
amount: '1000000000', // amount in smallest unit
slippage: 0.5, // 0.5% slippage
});
console.log('Output amount:', quote.outputAmount);
console.log('Price impact:', quote.priceImpact);
// Execute swap
const swapSignature = await sdk.swap(
currentAccount.id,
'your-password',
{
fromToken: 'So11111111111111111111111111111111111111112',
toToken: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
amount: '1000000000',
slippage: 0.5,
}
);Lịch sử giao dịch
// Lấy lịch sử giao dịch từ Solscan
const transactions = await sdk.getTransactionHistory(
currentAccount.id,
50 // limit
);
transactions.forEach(tx => {
console.log('Type:', tx.type);
console.log('Amount:', tx.amount);
console.log('Status:', tx.status);
console.log('Signature:', tx.signature);
});Quản lý nhiều ví
// Lấy tất cả ví
const allAccounts = sdk.getAllAccounts();
// Chuyển đổi ví hiện tại
await adapter.setCurrentAccount(accountId);
// Xóa ví
await adapter.removeAccount(accountId);
// Đổi tên ví
await adapter.updateAccountName(accountId, 'New Name');Thay đổi network
// Chuyển sang devnet
sdk.setNetwork(Network.DEVNET);
// Hoặc custom RPC
sdk.setNetwork(Network.MAINNET, 'https://custom-rpc-url.com');Sử dụng trực tiếp SDK (không dùng adapter)
import { SolanaWalletSDK, Network } from 'nvc-sdk-wallet-sol';
const sdk = new SolanaWalletSDK({
network: Network.MAINNET,
rpcUrl: 'https://api.mainnet-beta.solana.com',
solscanApiKey: 'your-api-key',
});
// Sử dụng tương tự như trên
// Lưu ý: Bạn cần tự quản lý storage cho accountsCấu trúc
src/
├── core/ # Core classes
│ ├── Wallet.ts # Quản lý ví
│ ├── TransactionManager.ts # Gửi/nhận token
│ ├── SwapManager.ts # Swap tokens
│ ├── SolscanManager.ts # Solscan integration
│ └── CacheManager.ts # Cache manager
├── config/ # Configuration
│ └── network.ts # Network config
├── utils/ # Utilities
│ ├── encryption.ts # AES encryption
│ └── keyDerivation.ts # Key derivation
├── adapters/ # Platform adapters
│ └── ReactNativeAdapter.ts # React Native adapter
├── types/ # TypeScript types
│ └── index.ts
└── index.ts # Main exportBảo mật
- Private keys được encrypt bằng AES với password
- Mnemonic phrases được generate theo BIP39 standard
- Key derivation theo BIP44 với path
m/44'/501'/index'/0' - Không lưu plaintext private keys
Lưu ý
- Password: Luôn sử dụng password mạnh và an toàn
- Mnemonic: Lưu mnemonic ở nơi an toàn, mất mnemonic = mất ví
- Network: Kiểm tra network (mainnet/devnet) trước khi giao dịch
- RPC: Có thể cần custom RPC để tránh rate limit
- Solscan API: Cần API key để sử dụng đầy đủ tính năng Solscan
Publish lên NPM
# Build
npm run build
# Publish
npm publish --access publicLicense
MIT
