#bitcoin #tapyrus #openassets

openassets-tapyrus

Open Assets 协议的 Rust 实现

3 个版本

使用旧的 Rust 2015

0.2.4 2021 年 3 月 23 日
0.2.3 2021 年 2 月 1 日
0.2.2 2021 年 1 月 16 日

#433#bitcoin


用于 esplora-tapyrus

MITLGPL-3.0+

29KB
635

openassets-tapyrus openassets-tapyrus at crates.io 构建状态 MIT 许可证

Rust 的 Open Assets 协议实现。

示例

tapyrus::TxOut 支持标记输出。

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);

资产 ID 计算。

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 地址

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"

依赖项

~10MB
~146K SLoC