This is a high-level guide to implementing a library for a new chain.
An input consumes some TXO referenced by its ID.
- Select or create a struct to be your
TxoIdentifier
- This is the unique in-protocol identifier for a TXO.
- In Bitcoin this is
bitcoins::types::txin::BitcoinOutpoint
. - Add the marker trait:
impl coins_core::types::tx::TxoIdentifier
- Implement a type to be your
Input
- This represents the input TXOs consumed by a transaction.
- This could be the same as
TxoIdentifier
, depending on your protocol. - In Bitcoin this is
bitcoins::types::txin::BitcoinInput
. - Add the marker trait:
impl coins_core::types::tx::Input
- Associate your
TxoIdentifier
type
An output creates a new TXO with some value owned by some payee.
- Select or create a struct to be your
RecipientIdentifier
- This is used to identify payees in-protocol.
- In Bitcoin this is
bitcoins::types::script::ScriptPubkey
impl coins_core::types::tx::RecipientIdentifier
on your struct- Select or create a type to be your
Value
- This type represents how the in-protocol value of a TXO.
- In Bitcoin this is
u64
. - In Ethereum-based TXO chains this is often a
U256
struct. impl coins_core::types::tx::Value
on your struct.- Implement a type to be your
Output
- This represents the output TXOs created by a transaction.
- In Bitcoin this is
bitcoins::types::txout::BitcoinInput
. - Add the trait:
impl coins_core::types::tx::Output
- Associate your
Value
andRecipientIdentifier
types.
A transaction is a list of inputs and outputs. It provides methods to access its properties, as well as calculate its signature hash.
- Define the hash function used by your TX:
- We do this so that your TX can use arbitray hashes, while keeping type safety.
- Define a
Digest
type that represents the output of your hash functions. 1. Bitcoin uses[u8; 32]
- Define a
MarkedDigest
type (seecoins_core::hashes::marked
) for your TXIDs. - Define a
MarkedDigestWriter
type that can outputDigest
andMarkedDigest
types. 1. This must implementstd::io::Write
. 1. Bitcoin usescoins_core::hashes::hash256::Hash256Writer
- Define an
Error
type to handle your errors. - This should contain any errors that can occur while interacting with your transaction.
- See
bitcoins::types::transactions::TxError
for an example. - Define a
SighashArgs
type for your transaction. - This struct should carry all the information needed to calculate the hash of your transaction that is signed.
- Define a
Transaction
struct. impl coins_core::ser::Ser
on yourTransaction
.- If necessary, create a new
Error
type (see an example inbitcoin/types/transaction.rs
) - This ensures that your tx can be serialized easily.
- It is used in the default txid implementation.
impl coins_core::types::tx::Transaction
on yourTransaction
- Associate your
Error
,Digest
,HashWriter
,TXID
,SighashArgs
,Input
, andOutput
types. - This is a simple interface for accessing inputs, outputs, and other tx info.
- If your transaction type does not have a version or a locktime, you may implement these as NOPs.
An AddressEncoder
tranlates between protocol-facing, and human-facing
datastructures. Typically this means managing the relationship between
addresses and their in-protocol counterparts.
- Define an
Error
type. It's fine to reuse a previouseError
here. - Create a type to be your
Address
- This represents the human-facing payee, and can be translated to a
RecipientIdentifier
- In Bitcoin this is an enum wrapper around a
String
. - Create a type to be your
Encoder
impl coins_core::enc::AddressEncoder
on yourEncoder
encode_address
transforms aRecipientIdentifier
to anAddress
.decode_address
transforms anAddress
to aRecipientIdentifier
.string_to_address
transforms a&str
to anAddress
- Associate your
Address
,RecipientIdentifier
, andEncoderError
types.
- Create a struct to be your
Builder
. impl coins_core::builder::TxBuilder
for yourBuilder
type- Associate your
Transaction
andAddressEncoder
from previous steps. - Feel free to leave
version
andlocktime
as NOPs if unsupported by your protocol.
The Network
ensures type consistency across all previous types. For example,
it guarantees that your Encoder
, Builder
, and TxOut
use the same
RecipientIdentifier
.
- Define a new
Error
type. It's fine to reuse a previousError
here. - Define a
Network
type. impl coins_core::nets::Network
on yourNetwork
.- Associate your
Error
type. - Associate your
TxIn
,TxOut
, andTransaction
types from previous steps. - Associate your
Address
,RecipientIdentifier
, andAddressEncoder
types. - Associate your
Builder
type.