rgb20
v0.0.4
Published
RGB20 contract creation library with StrictEncode and BAID64 integration
Maintainers
Readme
RGB20 Contract Creator
A comprehensive RGB20 token contract creation library that combines StrictEncode deterministic binary serialization with BAID64 encoding to generate contract hashes and human-readable identifiers.
Features
- 🎨 Complete RGB20 contract creation with validation
- 🔧 StrictEncode integration for deterministic binary serialization
- 🔐 SHA256 hashing of encoded contract data
- 🆔 BAID64 encoding with
contract:HRI prefix and checksums - 🖥️ Rich CLI interface with multiple output formats
- ✅ Comprehensive validation of all contract parameters
- 📦 Dual module support (ES modules + CommonJS)
- 🧪 48 comprehensive tests ensuring reliability
Installation
npm install rgb20Quick Start
Library Usage
import { RGB20Contract, createRGB20Contract } from 'rgb20';
// Create a contract
const contract = createRGB20Contract({
ticker: 'MYTOKEN',
name: 'My Token',
precision: 8,
contractTerms: 'Token contract terms',
totalSupply: 1000000,
genesisUtxo: 'abcd1234...ef56:0'
});
console.log('Contract ID:', contract.contractId);
console.log('Contract Hash:', contract.contractHash);CLI Usage
# Create a contract
rgb20 create \
--ticker "MYTOKEN" \
--name "My Token" \
--precision 8 \
--terms "Token contract terms" \
--supply 1000000 \
--utxo "abcd1234...ef56:0"
# JSON output
rgb20 create --ticker "MYTOKEN" --name "My Token" \
--precision 8 --terms "Terms" --supply 1000000 \
--utxo "abcd...ef:0" --json
# Interactive mode
rgb20 interactive
# Show examples
rgb20 exampleAPI Reference
RGB20Contract Class
Constructor
const contract = new RGB20Contract({
ticker: string, // Asset ticker symbol
name: string, // Asset name
precision: number, // Decimal precision (0-18)
contractTerms: string, // Contract terms text
totalSupply: number, // Total token supply
genesisUtxo: string // Genesis UTXO (txid:vout format)
});Methods
createAssetSpec()- Generate RGB20 AssetSpec structurecreateContractTerms()- Generate RGB20 ContractTerms structureencodeContract()- StrictEncode the complete contractcreateContractHash()- Generate SHA256 hash of encoded datacreateContractId()- Generate BAID64 contract ID with HRI prefixgenerateContract()- Generate complete contract information
Helper Functions
import { createRGB20Contract, validateRGB20Params } from 'rgb20';
// Quick contract creation
const contract = createRGB20Contract(params);
// Validate parameters
const isValid = validateRGB20Params(params); // throws on invalidCLI Commands
Contract Creation
rgb20 create [options]Required Options:
--ticker <symbol>- Asset ticker symbol--name <name>- Asset name--precision <0-18>- Decimal precision--terms <text>- Contract terms--supply <amount>- Total supply--utxo <txid:vout>- Genesis UTXO
Output Options:
--json- JSON format output--hash-only- Show only contract hash--id-only- Show only contract ID--encoded-only- Show only encoded data--compact- Compact JSON output
Other Commands
rgb20 validate [options]- Validate contract parametersrgb20 example- Show example with test vectorsrgb20 interactive- Interactive contract creationrgb20 help- Show help information
Example Output
{
"ticker": "NIATCKR",
"name": "NIA asset name",
"precision": 0,
"contractTerms": "NIA terms",
"totalSupply": "666",
"genesisUtxo": "22f0538e...2ac0:1",
"contractHash": "74e6a36a7ba3948c43ed4653c1723f8426bbc8c038016b9439e59e804fbf5c95",
"contractId": "contract:dOajanujlIxD7UZTwXI~hCa7yMA4AWuUOeWegE_~XJXV9~e0",
"encodedData": "074e494154434b52...",
"encodedLength": 80
}Technical Details
Contract Structure
RGB20 contracts consist of:
- AssetSpec - Ticker, name, precision, optional details
- ContractTerms - Legal terms, optional media attachments
- Amount - Total token supply (u64)
- Genesis UTXO - Transaction output reference
Encoding Process
- StrictEncode each component using deterministic binary serialization
- Concatenate all encoded components
- SHA256 hash the complete encoded data
- BAID64 encode the hash with
contract:HRI prefix and embedded checksum
Dependencies
- strictencode - Deterministic binary encoding
- baid64 - Base64 variant with HRI and checksums
Test Vectors
The package includes comprehensive test vectors from the RGB20 specification:
# Run the specification test vector
rgb20 create \
--ticker "NIATCKR" \
--name "NIA asset name" \
--precision 0 \
--terms "NIA terms" \
--supply 666 \
--utxo "22f0538e189f32922e55daf6fa0b7120bc01de8520a9a4c80655fdaf70272ac0:1"Expected Output:
- Contract Hash:
74e6a36a7ba3948c43ed4653c1723f8426bbc8c038016b9439e59e804fbf5c95 - Contract ID:
contract:dOajanujlIxD7UZTwXI~hCa7yMA4AWuUOeWegE_~XJXV9~e0
Development
Running Tests
npm test # Run all tests
npm run test:watch # Watch mode
npm run test:coverage # With coverageBuilding
npm run build:cjs # Build CommonJS version
npm run lint # Run linterProject Structure
rgb20/
├── index.js # Main ES module
├── index.cjs # CommonJS build
├── cli.js # Command-line interface
├── package.json # Package configuration
├── test/ # Test files
│ ├── rgb20-contract.test.js
│ └── cli.test.js
└── README.md # This fileStandards Compliance
- RGB20 - RGB token standard specification
- StrictEncode - Deterministic binary serialization
- BAID64 - Base64 variant with HRI prefixes
- Node.js ESM - Modern ES module support
- Jest - Comprehensive test coverage
Contributing
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
Apache-2.0 License - see LICENSE file for details.
Links
RGB Community | Building the future of Bitcoin smart contracts
