-
Notifications
You must be signed in to change notification settings - Fork 9
Colored Coin
tapyrusrb provides the following features for using Tapyrus' Colored Coin feature.
The color identifier is the identifier of the colored coin and generated at the time of issue with the UTXO information referenced by the input. See here for the rules for issuing identifiers.
In tapyrusrb, you can compute these identifiers with Tapyrus::Color::ColorIdentifier
.
# color id for reissuable token(script_pubkey is the scriptPubkey(Tapyrus::Script) referenced by the input of the issuing Tx.)
color_id = Tapyrus::Color::ColorIdentifier.reissuable(script_pubkey)
color_id.to_hex
=> c1d32110f8a0bb4b95ced8ef2fbad3973421538976e1aa2d8994f5c97c903e18e5
# color id for non-reissuable token(out_point is Tapyrus::OutPoint to which the input of the issuing Tx refers.)
color_id = Tapyrus::Color::ColorIdentifier.non_reissuable(out_point)
color_id.to_hex
=> c2ec2fd806701a3f55808cbec3922c38dafaa3070c48c803e9043ee3642c660b46
# color id for nft(out_point is Tapyrus::OutPoint to which the input of the issuing Tx refers.)
color_id = Tapyrus::Color::ColorIdentifier.non_reissuable(out_point)
color_id.to_hex
=> c33f6c2724a21a3b29ef886a52aa414bec96c46f7af137c636065209ff892cee6c
You can color the scriptPubkey by using the color identifier and OP_COLOR
opcode. See here for details.
Tapyrus::Script#add_color
can be used to color the scriptPubkey.
p2pkh = Tapyrus::Script.parse_from_payload('76a9141654c4fcb23c1b50fa0270249eb6120a133cd32e88ac'.htb)
=> OP_DUP OP_HASH160 1654c4fcb23c1b50fa0270249eb6120a133cd32e OP_EQUALVERIFY OP_CHECKSIG
cp2pkh = p2pkh.add_color(color_id)
=> c33f6c2724a21a3b29ef886a52aa414bec96c46f7af137c636065209ff892cee6c OP_COLOR OP_DUP OP_HASH160 1654c4fcb23c1b50fa0270249eb6120a133cd32e OP_EQUALVERIFY OP_CHECKSIG
For P2PKH and P2SH, same can be done with Tapyrus::Script#to_cp2pkh
and Tapyrus::Script#to_cp2sh
.
# CP2PKH
pubkey_hash = '46c2fbfbecc99a63148fa076de58cf29b0bcf0b0'
cp2pkh = Tapyrus::Script.to_cp2pkh(color_id, pubkey_hash)
=> c33f6c2724a21a3b29ef886a52aa414bec96c46f7af137c636065209ff892cee6c OP_COLOR OP_DUP OP_HASH160 46c2fbfbecc99a63148fa076de58cf29b0bcf0b0 OP_EQUALVERIFY OP_CHECKSIG
# CP2SH
script_hash = '7620a79e8657d066cff10e21228bf983cf546ac6'
cp2sh = Tapyrus::Script.to_cp2sh(color_id, script_hash)
=> c33f6c2724a21a3b29ef886a52aa414bec96c46f7af137c636065209ff892cee6c OP_COLOR OP_HASH160 7620a79e8657d066cff10e21228bf983cf546ac6 OP_EQUAL
A colored UTXO is represented by an address containing a color id. See here for detail.
cp2pkh.addresses
=> ["vwRatPMueGUA97tsUvWvMh51Far81QwG17XNVKMDKK4oYK9C3hbN9WSjKVmPLoN9QZ2UGMFdgxqCan"]
cp2sh.addresses
=> ["4ZwSswiRuQ9rePuiTKfseyF2xBuMGhq6B2furrDjjPouaaTsEHN3pJH1PuxqQhPoNY5bUFwDMouP3Zj"]
The following methods have been added to Tapyrus::TxOut
to support Colored Coin.
-
colored?
: check whether scriptPubkey has color id or not. -
color_id
: return color id. If scriptPubkey does not have color id, return nil. -
reissuable?
: check whether scriptPubkey is reissuable token or not. -
non_reissuable?
: check whether scriptPubkey is non-reissuable token or not. -
nft?
: check whether scriptPubkey is nft or not.
Tapyrus::TxBuilder
also supports the construction of colored coin transactions with following methods:
-
reissuable
add reissuable token output to tx. -
non_reissuable
add non-reissuable token output to tx. -
nft
add nft output to tx.