3 releases
Uses old Rust 2015
0.2.4 | Mar 23, 2021 |
---|---|
0.2.3 | Feb 1, 2021 |
0.2.2 | Jan 16, 2021 |
#490 in #bitcoin
34 downloads per month
Used in esplora-tapyrus
29KB
635 lines
openassets-tapyrus
The implementation of the Open Assets Protocol for Rust.
Examples
tapyrus::TxOut supports marker output.
use tapyrus::{Script, TxOut};
use tapyrus::blockdata::script::Builder;
use tapyrus::consensus::serialize;
use tapyrus::util::misc::hex_bytes;
use hex::decode as hex_decode;
use openassets::marker_output::{Metadata, TxOutExt, Payload};
let marker_output = TxOut {value: 0, script_pubkey: Builder::from(hex_decode("6a244f4101000364007b1b753d68747470733a2f2f6370722e736d2f35596753553150672d71").unwrap()).into_script()};
// judge marker output
marker_output.is_openassets_marker();
// get open assets payload
let payload: Payload = marker_output.get_oa_payload().unwrap();
// asset quantities
payload.quantities;
=> [100, 0, 123]
// metadata
payload.metadata.to_string()
=> "u=https://cpr.sm/5YgSU1Pg-q"
// encode payload
let metadata = Metadata("u=https://cpr.sm/5YgSU1Pg-q".as_bytes().to_vec());
let payload = Payload { quantities: vec![100, 0, 123], metadata };
let serialized_marker: Vec<u8> = serialize(&payload);
Asset ID calculation.
use std::str::FromStr;
use tapyrus::blockdata::script::Builder;
use openassets::asset_id::AssetId;
use hex::decode as hex_decode;
let p2pkh = Builder::from(hex_decode("76a914010966776006953d5567439e5e39f86a0d273bee88ac").unwrap()).into_script();
let asset_id = AssetId::new(&p2pkh, tapyrus::network::constants::Network::Bitcoin);
asset_id.to_string();
=> "ALn3aK1fSuG27N96UGYB1kUYUpGKRhBuBC"
// load from string
AssetId::from_str("ALn3aK1fSuG27N96UGYB1kUYUpGKRhBuBC");
Open Assets Address
use std::str::FromStr;
use std::string::ToString;
use openassets::address::OAAddressConverter;
// convert btc address to open assets address
let addr = tapyrus::Address::from_str("1F2AQr6oqNtcJQ6p9SiCLQTrHuM9en44H8").unwrap();
addr.to_oa_address().unwrap().to_string();
=> "akQz3f1v9JrnJAeGBC4pNzGNRdWXKan4U6E"
// convert open assets address to btc address
let open_asset_addr = addr.to_oa_address().unwrap();
open_asset_addr.to_btc_addr().unwrap().to_string();
=> "1F2AQr6oqNtcJQ6p9SiCLQTrHuM9en44H8"
Dependencies
~11MB
~152K SLoC