Acquire the skills needed to understand and use RGB
rgb created by the LNP/BP association. The aim is to show how to install and manipulate the CLI, compile a Schema, import the Interface and Interface Implementation, and then issue an RGB asset. We'll also look at the underlying logic, including compilation and state validation. At the end of this tutorial, you should be able to reproduce the process and create your own RGB contracts.RGB protocol reminder
rgb CLI is the official reference, even if it remains relatively unpolished.Installation and presentation of the rgb CLI tool
rgb. It is designed to be reminiscent of git, with a set of sub-commands for manipulating contracts, invoking them, issuing assets and so on. Bitcoin Wallet is not currently integrated, but will be in an imminent version (0.11). This next version will enable users to create and manage their wallets (via descriptors) directly from rgb, including PSBT generation, compatibility with external hardware (e.g. a hardware wallet) for signing, and interoperability with software such as Sparrow. This will simplify the entire asset issuance and transfer scenario.Installation via Cargo
cargo install rgb-contracts --all-features
rgb-contracts, and the installed command will be named rgb. If a crate named rgb already existed, there could have been a collision, hence the name)rgb
rgb (without arguments) displays a list of available sub-commands, such as interfaces, schema, import, export, issue, invoice, transfer, etc. You can change the local storage directory (a stash that holds all logs, schematics and implementations), choose the network (testnet, mainnet) or configure your Electrum server.First overview of controls
RGB20 interface is already integrated by default:rgb interfaces
git clone https://github.com/RGB-WG/rgb-interfaces
cargo run
rgb import interfaces/RGB20.rgb
rgb schemata
git clone https://github.com/RGB-WG/rgb-schemata
src/ directory, several Rust files (for example nia.rs) which define schemas (NIA for "Non Inflatable Asset", UDA for "Unique Digital Asset", etc.). To compile, you can then run :cd rgb-schemata cargo run
.rgb and .rgba files corresponding to the compiled schematics. For example, you'll find NonInflatableAsset.rgb.Importing Schema and Interface Implementation
rgb :rgb import schemata/NonInflatableAssets.rgb
rgb schemata
Contract creation (issuing)
- Either we use a script or code in Rust that builds a Contract by populating schema fields (global state, Owned States, etc.) and produces a
.rgbor.rgbafile; - Or use the
issuesub-command directly, with a YAML (or TOML) file describing the token's properties.
examples folder, which illustrate how you build a ContractBuilder, fill in the global state (asset name, ticker, supply, date, etc.), define the Owned State (to which UTXO it is assigned), then compile all this into a contract consignment that you can export, validate and import into a stash.ticker, the name, the supply, and so on. Suppose the file is called RGB20-demo.yaml. You can specify :spec: ticker, name, precision ;terms: a field for legal notices ;issuedSupply: the amount of token issued ;assignments: indicates the Single-use Seal (seal definition) and the quantity unlocked.
interface: RGB20Fixed globals: spec: ticker: Plan ₿ Academy name: Plan ₿ Academy details: "Pay attention: the asset has no value" precision: 2 terms: text: > SUBJECT TO, AND WITHOUT IN ANY WAY LIMITING, THE REPRESENTATIONS AND WARRANTIES OF ANY SELLER. PROPERTY IS BEING SOLD “AS IS”... media: ~ issuedSupply: 100000000 assignments: assetOwner: seal: tapret1st:b449f7eaa3f98c145b27ad0eeb7b5679ceb567faef7a52479bc995792b65f804:1 amount: 100000000 # this is 1 million (we have two digits for cents)
rgb issue '<SchemaID>' ssi:<Issuer> rgb20-demo.yaml
RDYhMTR!9gv8Y2GLv9UNBEK1hcrCmdLDFk9Qd5fnO8k and I haven't put any issuer. So my order is :rgb issue 'RDYhMTR!9gv8Y2GLv9UNBEK1hcrCmdLDFk9Qd5fnO8k' ssi:anonymous rgb20-demo.yaml
rgb schemata
rgb contracts
Plan ₿ Academy tokens defined in UTXO b449f7eaa3f98c145b27ad0eeb7b5679ceb567faef7a52479bc995792b65f804:1).rgb state '<ContractId>'
Export, import and validation
rgb export '<ContractId>' myContractPBN.rgb
myContractPBN.rgb file can be passed on to another user, who can add it to his stash with the command :rgb import myContractPBN.rgb
Importing consignment rgb" message. If it's a larger state transition consignment, the command will be different (rgb accept).rgb validate myContract.rgb
Stash usage, verification and display
rgb dump
Transfer and PSBT
Tapret or Opret commitments.Generate an invoice
- Alice (the issuer of the invoice) ;
- Bob (who receives and executes the invoice).
$ rgb invoice $CONTRACT -i $INTERFACE $ACTION $STATE $SEAL
$CONTRACT: Contract identifier (ContractId) ;$INTERFACE: the interface to be used (e.g.RGB20) ;$ACTION: the name of the operation specified in the interface (for a simple fungible token transfer, this could be "Transfer"). If the interface already provides a default action, you don't need to enter it again here;$STATE: the status data to be transferred (for example, an amount of tokens if a fungible token is transferred);$SEAL: the beneficiary's (Alice's) Single-use Seal, i.e. an explicit reference to an UTXO. Bob will use this info to build the witness transaction, and the corresponding output will then belong to Alice (in blinded UTXO or unencrypted form).
alice$ CONTRACT='iZgIN9EL-2H21UgQ-x!A3uJc-WwXhCSm-$9Lwcc1-v!mUkKY' alice$ MY_UTXO=4960acc21c175c551af84114541eace09c14d3a1bb184809f7b80916f57f9ef8:1 alice$ rgb invoice $CONTRACT -i RGB20 --amount 100 $MY_UTXO
rgb:iZgIN9EL-2H21UgQ-x!A3uJc-WwXhCSm-$9Lwcc1-v!mUkKY/RGB20/100+utxob:zlVS28Rb-...
Making a transfer
- Bob (who holds the tokens in his stash) has a Bitcoin wallet. He needs to prepare a Bitcoin transaction (in the form of a PSBT, e.g.
tx.psbt) which spends the UTXOs where the required RGB tokens are located, plus one UTXO for currency (exchange) ; - Bob executes the following command:
bob$ rgb transfer tx.psbt $INVOICE consignment.rgb
- This generates a
consignment.rgbfile which contains : - The transition history proving to Alice that the tokens are genuine;
- The new transition that transfers tokens to Alice's Single-use Seal ;
- A witness transaction (unsigned).
- Bob sends this
consignment.rgbfile to Alice (by e-mail, a sharing server or an RGB-RPC protocol, Storm, etc.); - Alice receives
consignment.rgband accepts it in its own stash :
alice$ rgb accept consignment.rgb
- The CLI checks the validity of the transition and adds it to Alice's stash. If invalid, the command fails with detailed error messages. Otherwise, it succeeds, and reports that the sample transaction has not yet been broadcast on the Bitcoin network (Bob is waiting for Alice's green light);
- By way of confirmation, the
acceptcommand returns a signature (payslip) which Alice can send to Bob to show him that she has validated the consignment ; - Bob can then sign and publish (
--publish) his Bitcoin transaction:
bob$ rgb check <sig> && wallet sign --publish tx.psbt
- As soon as this transaction is confirmed on-chain, ownership of the asset is considered transferred to Alice. Alice's wallet, monitoring the transaction's mining, sees the new Owned State appear in its stash.
Author
This tutorial has been written by Loïc Morel
You can say thanks by tipping the professor.
I write educational content about Bitcoin.
Credits
This tutorial has not been proofread yet
The original content has been translated by AI, but human review is necessary to ensure its accuracy.
2 905 sats1 453 sats727 satsEvery content on the platform is the result of a collaborative effort: each lesson, translation, and revision is made possible by the work of contributors. For this reason, we are always looking for proofreaders who can review our content in many languages. If you want to participate in the proofreading process, please reach out in our Telegram group and read our tutorial. We remind you that this content is open-source - licensed under CC BY-SA - so it can be freely shared and used, as long as the original source is credited.


