Skip to content

Commit

Permalink
Implement Dislay and Decodable to the ColoreIdentifier
Browse files Browse the repository at this point in the history
ported from PR #23
  • Loading branch information
rantan committed Jun 13, 2024
1 parent 234460e commit 503bdec
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
43 changes: 40 additions & 3 deletions tapyrus/src/blockdata/script/color_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
//! [Colored Coin]: <https://github.com/chaintope/tapyrus-core/blob/master/doc/tapyrus/colored_coin.md>
use core::convert::TryFrom;
use core::fmt;

use hashes::{Hash, sha256};
use crate::consensus::{Encodable, serialize};
use crate::consensus::{Decodable, Encodable, encode, serialize};
use crate::blockdata::script::Script;
use crate::blockdata::transaction::OutPoint;
use crate::consensus::encode::serialize_hex;
use crate::script::PushBytesBuf;
use crate::io;

Expand Down Expand Up @@ -63,12 +65,29 @@ impl ColorIdentifier {

impl Encodable for ColorIdentifier {
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
let mut len = (self.token_type as u8).consensus_encode(w)?;
let mut len = self.token_type.consensus_encode(w)?;
len += self.payload.consensus_encode(w)?;
Ok(len)
}
}

impl Decodable for ColorIdentifier {
fn consensus_decode<R: io::Read + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
let token_type = TokenTypes::consensus_decode(r)?;
let payload = ColorIdentifierPayload::consensus_decode(r)?;
Ok(ColorIdentifier {
token_type,
payload,
})
}
}

impl fmt::Display for ColorIdentifier {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", serialize_hex(self))
}
}

impl From<ColorIdentifier> for PushBytesBuf {
fn from(color_id: ColorIdentifier) -> Self {
PushBytesBuf::try_from(serialize(&color_id)).unwrap()
Expand Down Expand Up @@ -102,9 +121,27 @@ impl TokenTypes {
}
}

impl Encodable for TokenTypes {
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
(*self as u8).consensus_encode(w)
}
}

impl Decodable for TokenTypes {
fn consensus_decode<R: io::Read + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
let token_type = u8::consensus_decode(r)?;
match token_type {
0xc1 => Ok(TokenTypes::Reissuable),
0xc2 => Ok(TokenTypes::NonReissuable),
0xc3 => Ok(TokenTypes::Nft),
_ => Err(encode::Error::ParseFailed("Unknown token type")),
}
}
}

/// Error occured with colored coin
#[derive(Debug)]
pub enum ColoredCoinError {
/// original script is not based p2pkh and p2sh
UnsuppotedScriptType,
UnsupportedScriptType,
}
2 changes: 1 addition & 1 deletion tapyrus/src/blockdata/script/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl ScriptBuf {
/// Create new script with color identifier
pub fn add_color(&self, color_id: ColorIdentifier) -> Result<Self, ColoredCoinError> {
if !self.is_p2pkh() && !self.is_p2sh() {
return Err(ColoredCoinError::UnsuppotedScriptType);
return Err(ColoredCoinError::UnsupportedScriptType);
}

let script = Builder::new()
Expand Down
15 changes: 15 additions & 0 deletions tapyrus/src/blockdata/script/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,4 +951,19 @@ fn add_color_test() {
// op_return -> err
let op_return = op_return.add_color(color_id);
assert!(op_return.is_err());
}

#[test]
fn serialize_color_id() {
let out_point = OutPoint { txid: "0101010101010101010101010101010101010101010101010101010101010101".parse().expect("txid"), vout: 1 };
let color_id = ColorIdentifier::nft(out_point);

assert_eq!(format!("{}", color_id), "c3ec2fd806701a3f55808cbec3922c38dafaa3070c48c803e9043ee3642c660b46");

let hex_script = hex!("c3ec2fd806701a3f55808cbec3922c38dafaa3070c48c803e9043ee3642c660b46");
let script: Result<ColorIdentifier, _> = deserialize(&hex_script);
assert!(script.is_ok());
let color_id = script.unwrap();
assert_eq!(color_id.token_type, TokenTypes::Nft);
assert_eq!(serialize(&color_id), hex_script);
}

0 comments on commit 503bdec

Please sign in to comment.