// 11111111... private keyconst privKey = new Uint8Array(32).fill(0x01);// Get address of private keyconst address = sol.getAddress(privKey);// AKnL4NNf3DGWZJS6cPknBuEGnVsV4A4m5tgebLHaRSZ9// Format private keyconst privFormatted = sol.formatPrivate(privKey);// 2AXDGYSE4f2sz7tvMMzyHvUfcoJmxudvdhBcmiUSo6iuCXagjUCKEQF21awZnUGxmwD4m9vGXuC3qieHXJQHAcT// Simple tx (transfer some sol's from one account to another)const toAddress = 'FDwkzWGxx6LfCfzcmVVLEk3QUMxNhuFuKEMRwzR4Dtys';const blockhash = 'J2BjKU6L83eehHVgoze6uTXGCBu6nbxsqEro9QvWpU52';const amount = '10.1';const fee = '1.2'
Decode transaction
import { base64 } from '@scure/base';import * as sol from 'micro-sol-signer';// Random USDT tx from explorer:// https://explorer.solana.com/tx/5Nnhjv1GVB8T1k8MguUGHQw5zQQQsWET1f1zzj8azRhnVoYQPoZPtkscPCKy6FisP2eVWehjU1EYV8zywqKm5if4const tx = 'Atrba9P4rJ4tA3fMXioF+LBR5Y397TCaCC7o/JsViIFxDQ+FOpW2/I+DGMtapWPmrRJ3KDEaYa21YbpUcXaygQPKXDfudpRNZKsMsjhhH018U2YKTAJoqu6Jr1jASfnV98/65boYyPzPujo4YMKnIaCjrt1EsvnPNCuoBMXUEzYAAgEECc20MANIMI92j1eVfOiH5WQ691HznE9ZeQfjeXpDNm0eH5z5eohWokD+6H+jjnZ/KFqkCmlEdPrk6HCx+mOgjTAJUM/3r5vR1DjJnZhT6PQK3Z32pIe8MzDmPxe8Ttzy2CTxiTfFaNQeAkRJCefcB5JJGeb/Qxrj4dpxv8Kv9gClJ544V5wdVgmhBbCFO1kSIv6OaEUizyYdqhTUiO8w8XsGp9UXGSxWjuCKhF9z0peIzwNcMUWyGrNE2AYuqUAAAM4BDmCv7bInF71jGS9UFFo/llozu4LSxwKess4eIIJkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAG3fbh12Whk9nL4UbO63msHLSF7V9bN5E6jPWFfv8AqYUpqsC9KfFD7lsris1C7YZkNRdSH5qix9nMo2igoP0yAgcDAgUBBAQAAAAIBAMGBAAKDJSDxgMPAAAABg==';const decodedTx = sol.Transaction.decode(base64.decode(tx));console.log(decodedTx);/*{ msg: { feePayer: 'EqywLUZcm73PSWri93X3M5TN62iFMsUPMjvWYUq89dKB', blockhash: '9xp5Jz2v7ZsE3Xn5SVGkRisjo7h16vzF1ducwhWnc5n9',
Create complex transactions and send tokens
Solana is very flexible and has awesome architecture, but it also means there is no
'right' way to send tokens:
Basic account (ex. EqywLUZcm73PSWri93X3M5TN62iFMsUPMjvWYUq89dKB) cannot have any tokens
For every token contract you need to create separate token account which will be controlled by token contract
If a user gives some address to send tokens, it can be:
Solana account: which means we need to derive token account address (sol.tokenAddress)
Token account: it is possible the token account has not yet been created,
in this case we can create it for user; but it is not free, and will cost some fee.
token account which was derived in a different way from main solana account
The basic token sending example is:
// Current blockhashconst blockhash = '9xp5Jz2v7ZsE3Xn5SVGkRisjo7h16vzF1ducwhWnc5n9';// Sol account which is owner of tokenAccountconst fromAccount = 'EqywLUZcm73PSWri93X3M5TN62iFMsUPMjvWYUq89dKB';const USDT = sol.tokenFromSymbol('USDT', { ...sol.COMMON_TOKENS, // You can add custom tokens here});// Deriving token account address from solana account addressconst fromTokenAccount = sol.tokenAddress(USDT.contract, fromAccount);// Should be valid token account, not solana accountconst toTokenAddress = 'FDwkzWGxx6LfCfzcmVVLEk3QUMxNhuFuKEMRwzR4Dtys';const amount = '64487.8509';const tokenSimple = sol.createTxComplex
However, in real world you may need more complex logic, like:
// Check if account is valid token accountfunction isValidTokenAccount(mint, info, owner) { if (!info) return false; if (info.owner !== sol.TOKEN_PROGRAM) return false; try { const data = sol.TokenAccount.decode(info.data); if (data.mint !== mint) return false; if (data.state !== 'initialized') return false; if (owner && data.owner !== owner) return false; return true; } catch (e) { return
Network
import * as net from 'micro-sol-signer/net.js';// Docs available in src/net.ts
The section should be written; for now check out source code for network-related methods, like fetching balances, or sending txs.
No real network calls are done in the library for simplified auditing. When network needs to be used, JSON-RPC compatible provider is required. We have one in micro-ftch and it's recommended for usage.
ABI/API
There is no official ABI for Solana (in comparison with ethereum), but it is actually not a big deal,
since you will need to write API on top of raw ABI anyway.
We have small DSL, based on micro-packed,
which allows to define ABI easier: look at sol.token definition in index.ts.
hint: 'Transfer 64487.8509 USDT from token account=3VDHywae15vgbG2euNPpwoHTEr2eyGLuS6EoF74kDkp4 of owner=EqywLUZcm73PSWri93X3M5TN62iFMsUPMjvWYUq89dKB to 3feqC1fmo5YHMh2iw7X9kGE9F8P147hiiDQqC5xtSbpN'
}
]
*/
(
fromAccount, // owner of source token account (solana account)