more shuffling content between crates

This commit is contained in:
waalge 2025-10-02 00:57:41 +00:00
parent 7163525fb1
commit 7d96313386
15 changed files with 282 additions and 193 deletions

24
Cargo.lock generated
View File

@ -246,8 +246,9 @@ dependencies = [
"blockfrost-openapi",
"cardano-connect",
"cardano-tx-builder",
"futures",
"hex",
"minicbor",
"minicbor 0.25.1",
"reqwest",
"serde",
"serde_json",
@ -262,7 +263,7 @@ dependencies = [
"bech32 0.11.0",
"cryptoxide 0.5.1",
"hex",
"minicbor",
"minicbor 0.25.1",
"pallas-addresses",
"pallas-codec",
"pallas-crypto",
@ -1229,22 +1230,17 @@ version = "0.0.0"
dependencies = [
"anyhow",
"bech32 0.11.0",
"blockfrost",
"blockfrost-openapi",
"cardano-tx-builder",
"clap",
"cryptoxide 0.5.1",
"hex",
"minicbor",
"pallas-addresses",
"pallas-codec",
"minicbor 2.1.1",
"pallas-crypto",
"pallas-primitives",
"rand",
"reqwest",
"serde",
"serde_json",
"tokio",
"uplc",
]
[[package]]
@ -1254,7 +1250,7 @@ dependencies = [
"anyhow",
"cardano-tx-builder",
"hex",
"minicbor",
"minicbor 0.25.1",
"rand",
]
@ -1347,6 +1343,12 @@ dependencies = [
"minicbor-derive",
]
[[package]]
name = "minicbor"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f182275033b808ede9427884caa8e05fa7db930801759524ca7925bd8aa7a82"
[[package]]
name = "minicbor-derive"
version = "0.15.3"
@ -1521,7 +1523,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b2737b05f0dbb6d197feeb26ef15d2567e54833184bd469f5655a0537da89fa"
dependencies = [
"hex",
"minicbor",
"minicbor 0.25.1",
"num-bigint",
"serde",
"thiserror 1.0.69",

View File

@ -25,3 +25,4 @@ serde_json = "1.0.138"
tokio = { version = "1.47.1", features = ["full"] }
cardano-tx-builder = { path = "../cardano-tx-builder" }
cardano-connect = { path = "../cardano-connect" }
futures = "0.3.31"

View File

@ -4,15 +4,16 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use std::collections::{BTreeMap, HashMap};
use std::collections::HashMap;
use anyhow::{Result, anyhow};
use futures::stream::{self, StreamExt};
use blockfrost::{BlockfrostAPI, Pagination};
use blockfrost_openapi::models::tx_content_output_amount_inner::TxContentOutputAmountInner;
use blockfrost_openapi::models::{address_utxo_content_inner::AddressUtxoContentInner, tx_content_output_amount_inner::TxContentOutputAmountInner};
use cardano_connect::CardanoConnect;
use cardano_tx_builder::{
Address, BuildParameters, Credential, DatumOrHash, Input, Network, Output, PlutusData, Utxo,
Address, BuildParameters, Credential, Datum, Input, Network, Output, PlutusData, Script, Utxo,
Value,
};
@ -109,22 +110,54 @@ impl Blockfrost {
&self,
datum_hash: &Option<String>,
inline_datum: &Option<String>,
) -> Result<Option<DatumOrHash>> {
if let Some(inline_datum) = inline_datum {
Ok(Some(DatumOrHash::Data(plutus_data_from_inline(
inline_datum,
)?)))
} else {
if let Some(datum_hash) = datum_hash {
Ok(Some(DatumOrHash::Data(
self.plutus_data_from_hash(&datum_hash).await?,
)))
} else {
Ok(None)
) -> Result<Datum> {
match (inline_datum, datum_hash) {
(None, None) => Ok(Datum::None),
(Some(inline_datum), _) => Ok(Datum::Data(plutus_data_from_inline(inline_datum)?)),
(_, Some(datum_hash)) => {
Ok(Datum::Data(self.plutus_data_from_hash(&datum_hash).await?))
}
}
}
pub async fn resolve_utxo(
&self,
bf_utxo: AddressUtxoContentInner,
) -> Result<Utxo> {
let datum = self
.resolve_datum(&bf_utxo.data_hash, &bf_utxo.inline_datum)
.await?;
let script_ref = match &bf_utxo.reference_script_hash {
None => None,
Some(hash) => Some(self.resolve_script(&hash).await?),
};
Ok(Utxo {
input: Input {
transaction_id: v2a(hex::decode(&bf_utxo.tx_hash)?)?.into(),
index: bf_utxo.tx_index as u64,
},
output: Output {
address: Address::from_bech32(&bf_utxo.address)?,
value: from_tx_content_output_amounts(&bf_utxo.amount[..])?,
datum,
script_ref,
},
})
}
/// Blockfrost client has the wrong type.
pub async fn resolve_script(&self, script_hash: &str) -> Result<Script> {
let plutus_version = self.plutus_version(script_hash);
let bytes = self.scripts_hash_cbor(script_hash);
match plutus_version.await? {
1 => Ok(Script::V1(bytes.await?)),
2 => Ok(Script::V2(bytes.await?)),
3 => Ok(Script::V3(bytes.await?)),
_ => Err(anyhow!("Unknown script")),
}
}
/// Blockfrost client has the wrong type.
pub async fn scripts_hash_cbor(&self, script_hash: &str) -> Result<Vec<u8>> {
let response = self
@ -146,27 +179,43 @@ impl Blockfrost {
}
}
// /// Blockfrost client has incomplete type
// pub async fn script_type(&self, script_hash : &str) -> Result<PlutusVersion>{
// let response = self
// .client
// .get(&format!( "{}/scripts/{}/cbor", self.base_url, script_hash))
// .header("Accept", "application/json")
// .header("project_id", self.project_id.as_str())
// .send()
// .await
// .unwrap();
/// Blockfrost client has incomplete type
pub async fn plutus_version(&self, script_hash: &str) -> Result<u8> {
let response = self
.client
.get(&format!("{}/scripts/{}/cbor", self.base_url, script_hash))
.header("Accept", "application/json")
.header("project_id", self.project_id.as_str())
.send()
.await
.unwrap();
// match response.status() {
// reqwest::StatusCode::OK => {
// let ResponseScript { plutus_type,.. } = response.json::<ResponseScript>().await.unwrap();
// }
// _ => Err(anyhow!("No script found")),
// }
// }
match response.status() {
reqwest::StatusCode::OK => {
let ResponseScript { plutus_type, .. } =
response.json::<ResponseScript>().await.unwrap();
match plutus_type.as_str() {
"plutusV1" => Ok(1),
"plutusV2" => Ok(2),
"plutusV3" => Ok(3),
"plutusV4" => Ok(4),
_ => Err(anyhow!("Unknown plutus version")),
}
}
_ => Err(anyhow!("No script found")),
}
}
}
impl CardanoConnect for Blockfrost {
fn network(&self) -> Network {
self.network
}
async fn health(&self) -> Result<String> {
Ok(format!("{:?}", self.api.health().await?))
}
async fn build_parameters(&self) -> BuildParameters {
let params = self
.api
@ -228,44 +277,21 @@ impl CardanoConnect for Blockfrost {
payment: &Credential,
delegation: &Option<Credential>,
) -> Result<Vec<Utxo>> {
let addr = Address::new(self.network_id(), payment.clone(), delegation.clone());
let addr = Address::new(self.network(), payment.clone(), delegation.clone());
let response = self
.api
.addresses_utxos(&addr.to_bech32()?, Pagination::all())
.addresses_utxos(&addr.to_bech32(), Pagination::all())
.await?;
response
.iter()
.map(|o| {
// FIXME: This should pull the datum and script reference
let datum = None;
let script_ref = None;
Ok(Utxo {
input: Input {
transaction_id: v2a(hex::decode(&o.tx_hash)?)?.into(),
index: o.tx_index as u64,
},
output: Output {
address: Address::from_bech32(&o.address).into(),
value: from_tx_content_output_amounts(&o.amount[..]),
datum,
script_ref,
},
})
})
.collect()
let s = stream::iter(response)
.map(move |bf_utxo| self.resolve_utxo(bf_utxo))
.buffer_unordered(10)
.collect::<Vec<Result<Utxo>>>()
.await;
s.into_iter().collect::<Result<Vec<Utxo>>>()
}
async fn submit(&self, tx: Vec<u8>) -> Result<String> {
let tx_hash = self.api.transactions_submit(tx).await?;
Ok(tx_hash)
}
fn network(&self) -> Network {
self.network
}
fn health(&self) -> impl std::future::Future<Output = Result<String, String>> + Send {
todo!()
Ok(self.api.transactions_submit(tx).await?)
}
}
@ -282,56 +308,27 @@ struct ResponseScript {
serialised_size: u64,
}
fn from_tx_content_output_amounts(xs: &[TxContentOutputAmountInner]) -> Value {
/// FIXME : Using the sane version of value
let mut lovelaces = 0;
let mut assets = BTreeMap::new();
fn from_tx_content_output_amounts(xs: &[TxContentOutputAmountInner]) -> Result<Value> {
let mut v = Value::zero();
for asset in xs {
let quantity: u64 = asset.quantity.parse().unwrap();
let amount: i128 = asset.quantity.parse()?;
if asset.unit == UNIT_LOVELACE {
lovelaces += quantity;
v.add_lovelace(amount);
} else {
let policy_id: PolicyId = asset.unit[0..56].parse().unwrap();
let asset_name: AssetName = hex::decode(&asset.unit[56..]).unwrap().into();
assets
.entry(policy_id)
.and_modify(|m: &mut BTreeMap<AssetName, u64>| {
m.entry(asset_name.clone())
.and_modify(|q| *q += quantity)
.or_insert(quantity);
})
.or_insert_with(|| BTreeMap::from([(asset_name, quantity)]));
let hash: [u8; 28] = v2a(hex::decode(&asset.unit[0..56])?)?;
let name: Vec<u8> = hex::decode(&asset.unit[56..])?;
v.add_asset(hash, name, amount);
}
}
if assets.is_empty() {
Value::Coin(lovelaces)
} else {
Value::Multiasset(
lovelaces,
NonEmptyKeyValuePairs::Def(
assets
.into_iter()
.map(|(policy_id, policies)| {
(
policy_id,
NonEmptyKeyValuePairs::Def(
policies
.into_iter()
.map(|(asset_name, quantity)| {
(asset_name, quantity.try_into().unwrap())
})
.collect::<Vec<_>>(),
),
)
})
.collect::<Vec<_>>(),
),
)
}
Ok(v)
}
pub fn plutus_data_from_inline(inline_datum: &str) -> Result<PlutusData> {
Ok(minicbor::decode(&hex::decode(inline_datum)?)?)
}
/// Handles the map error
fn v2a<T, const N: usize>(v: Vec<T>) -> Result<[T; N]> {
<[T; N]>::try_from(v)
.map_err(|v: Vec<T>| anyhow!("Expected a Vec of length {}, but got {}", N, v.len()))
}

View File

@ -9,12 +9,12 @@ use cardano_tx_builder::{
pub trait CardanoConnect {
fn network(&self) -> Network;
fn health(&self) -> impl std::future::Future<Output = Result<String>> + Send;
fn build_parameters(&self) -> impl std::future::Future<Output = BuildParameters> + Send;
fn utxos_at(
&self,
payment: &Credential,
delegation: Option<&Credential>,
delegation: &Option<Credential>,
) -> impl std::future::Future<Output = Result<Vec<Utxo>>> + Send;
fn health(&self) -> impl std::future::Future<Output = Result<String, String>> + Send;
fn submit(&self, tx: Vec<u8>) -> impl std::future::Future<Output = Result<String>> + Send;
}

View File

@ -3,7 +3,6 @@
use anyhow::{Result, anyhow};
use pallas_addresses;
pub use pallas_addresses::Network;
use pallas_crypto::hash::Hash;
use pallas_primitives::NetworkId;
use crate::utils::v2a;
@ -29,10 +28,10 @@ fn parse_network(header: u8) -> Network {
/// Credential
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub enum Credential {
Key(Hash<28>),
Script(Hash<28>),
Key([u8; 28]),
Script([u8; 28]),
}
impl Into<pallas_addresses::ShelleyPaymentPart> for Credential {
@ -78,13 +77,19 @@ impl Address {
let x = Address::try_from(bytes)?;
Ok(x)
}
pub fn to_bech32(self) -> String {
Into::<pallas_addresses::ShelleyAddress>::into(self)
.to_bech32()
.unwrap()
}
}
impl TryFrom<Vec<u8>> for Address {
fn try_from(bytes: Vec<u8>) -> std::result::Result<Self, Self::Error> {
let header = *bytes.first().expect("Missing bytes");
let network = parse_network(header);
let payment: Hash<28> = v2a(bytes[1..=28].to_vec())?.into();
let payment: [u8; 28] = v2a(bytes[1..=28].to_vec())?.into();
let delegation = v2a(bytes[28..].to_vec());
match header & 0b1111_0000 {
0b0000_0000 => Ok(Address::new(

View File

@ -0,0 +1,28 @@
use cryptoxide::digest::Digest;
use cryptoxide::sha2::Sha256;
use cryptoxide::blake2b::Blake2b;
pub fn sha2_256(msg: &[u8]) -> [u8;32] {
let mut output = [0;32];
let mut hasher = Sha256::new();
hasher.input(msg);
hasher.result(&mut output);
output
}
pub fn blake2b_224(msg: &[u8]) -> [u8;28] {
let mut output = [0;28];
let mut hasher = Blake2b::new(28);
hasher.input(msg);
hasher.result(&mut output);
output
}
pub fn blake2b_256(msg: &[u8]) -> [u8;32] {
let mut output = [0;32];
let mut hasher = Blake2b::new(32);
hasher.input(msg);
hasher.result(&mut output);
output
}

View File

@ -4,21 +4,19 @@ use super::pallas::era;
use super::plutus_data::PlutusData;
/// Datum
/// We are not being too clever with this.
/// We make use of rust's `Optional` for the no datum case.
/// We are yet to distinguish cases where we need the data,
/// in place of the hash.
/// It should be documented.
pub enum DatumOrHash {
/// As it may appear on a utxo
pub enum Datum {
None,
Hash([u8; 32]), // Check this
Data(PlutusData),
}
impl Into<era::DatumOption> for DatumOrHash {
fn into(self) -> era::DatumOption {
impl Into<Option<era::DatumOption>> for Datum {
fn into(self) -> Option<era::DatumOption> {
match self {
DatumOrHash::Hash(hash) => era::DatumOption::Hash(hash.into()),
DatumOrHash::Data(plutus_data) => era::DatumOption::Data(CborWrap(plutus_data)),
Datum::None => None,
Datum::Hash(hash) => Some(era::DatumOption::Hash(hash.into())),
Datum::Data(plutus_data) => Some(era::DatumOption::Data(CborWrap(plutus_data))),
}
}
}

View File

@ -0,0 +1,32 @@
use cryptoxide::ed25519;
use super::crypto::blake2b_224;
use super::address::{Credential};
pub struct Skey(pub [u8; 32]);
pub struct Vkey(pub [u8; 32]);
impl Skey {
pub fn vkey(&self) -> Vkey {
Vkey(ed25519::keypair(&self.0).1)
}
pub fn sign(&self, msg: &[u8]) -> [u8;64] {
ed25519::signature(msg.as_ref(), &ed25519::keypair(&self.0).0)
}
}
impl Vkey {
pub fn hash(&self) -> [u8;28] {
blake2b_224(&self.0)
}
pub fn verify(&self, msg: &[u8], sig: &[u8;64]) -> bool {
ed25519::verify(msg, &self.0, sig)
}
pub fn credential(&self) -> Credential {
Credential::Key(self.hash())
}
}

View File

@ -1,6 +1,12 @@
mod pallas;
mod utils;
pub mod crypto;
pub use crypto::{blake2b_224, blake2b_256, sha2_256};
pub mod key;
pub use key::{Skey, Vkey};
pub mod plutus_data;
pub use plutus_data::PlutusData;
@ -14,7 +20,7 @@ pub mod value;
pub use value::{MultiAsset, Value};
pub mod datum;
pub use datum::DatumOrHash;
pub use datum::Datum;
pub mod script;
pub use script::Script;

View File

@ -1,7 +1,7 @@
use super::pallas::era;
use super::address::Address;
use super::datum::DatumOrHash;
use super::datum::Datum;
use super::script::Script;
use super::value::Value;
@ -9,7 +9,7 @@ use super::value::Value;
pub struct Output {
pub address: Address,
pub value: Value,
pub datum: Option<DatumOrHash>,
pub datum: Datum,
pub script_ref: Option<Script>,
}
@ -18,8 +18,8 @@ impl Into<era::PostAlonzoTransactionOutput> for Output {
let address: pallas_addresses::ShelleyAddress = self.address.into();
era::PostAlonzoTransactionOutput {
address: address.to_vec().into(),
datum_option: self.datum.map(|x| x.into()),
value: self.value.into(),
datum_option: self.datum.into(),
value: self.value.try_into().expect("Failed to coerce value"),
script_ref: self.script_ref.map(|x| x.into()),
}
}

View File

@ -1,31 +1,71 @@
// TODO: Write some sane value handling fuctions:
// - add
// - sub
// - prune
// - split (negative, positive)
// - is_positive
use super::pallas::era;
use std::collections::HashMap;
use anyhow::{Error, Result};
use std::collections::BTreeMap;
/// Naive version of value
pub type MultiAsset = HashMap<[u8; 28], HashMap<Vec<u8>, u64>>;
pub type MultiAsset = BTreeMap<[u8; 28], BTreeMap<Vec<u8>, i128>>;
#[derive(Debug)]
pub struct Value(pub u64, pub MultiAsset);
pub struct Value(pub i128, pub MultiAsset);
impl Value {
pub fn add(self, _other: Value) -> Self {
todo!()
pub fn zero() -> Self {
Value(0, BTreeMap::new())
}
pub fn sub(self, _other: Value) -> Self {
todo!()
pub fn add_lovelace(&mut self, amount: i128) {
self.0 = self.0 + amount;
}
pub fn add_asset(&mut self, hash: [u8; 28], name: Vec<u8>, amount: i128) {
self.1
.entry(hash)
.and_modify(|tree| {
tree.entry(name.clone())
.and_modify(|curr| *curr += amount)
.or_insert(amount);
})
.or_insert(vec![(name, amount)].into_iter().collect());
}
pub fn add(&mut self, other: Value) {
self.0 += other.0;
for (k0, v0) in other.1.iter() {
self.1
.entry(k0.clone())
.and_modify(|curr| {
for (k1, v1) in v0.iter() {
curr.entry(k1.clone())
.and_modify(|quantity| *quantity += v1)
.or_insert(v1.clone());
}
})
.or_insert(v0.clone());
}
}
impl Into<era::Value> for Value {
fn into(self) -> era::Value {
todo!()
pub fn neg(&mut self) {
self.0 = -self.0;
for class in self.1.values_mut() {
for quantity in class.values_mut() {
*quantity = 0 - *quantity;
}
}
}
}
impl TryInto<era::Value> for Value {
fn try_into(self) -> Result<era::Value> {
if self.1.is_empty() {
Ok(era::Value::Coin(self.0.try_into()?))
} else {
panic!("Not yet implemented")
}
}
type Error = Error;
}

View File

@ -19,9 +19,12 @@ bech32 = "0.11.0"
clap = { version = "4.5.18", features = ["cargo", "derive"] }
cryptoxide = "0.5.1"
hex = "0.4.3"
minicbor = { version = "0.25.1", features = ["alloc", "derive"] }
pallas-crypto = "0.33.0"
# minicbor = { version = "0.25.1", features = ["alloc", "derive"] }
rand = "0.9.2"
reqwest = { version = "0.12.23", features = ["json"] }
serde = { version = "1.0.213", features = ["derive"] }
serde_json = "1.0.138"
tokio = { version = "1.47.1", features = ["full"] }
cardano-tx-builder = { path = "../cardano-tx-builder" }
minicbor = "2.1.1"

View File

@ -2,14 +2,14 @@ use clap::Subcommand;
mod cardano;
mod data;
mod tx;
// mod tx;
mod wallet;
#[derive(Subcommand)]
pub enum Cmd {
/// Txs
#[command(subcommand)]
Tx(tx::Cmd),
// #[command(subcommand)]
// Tx(tx::Cmd),
#[command(subcommand)]
Data(data::Cmd),
#[command(subcommand)]
@ -20,7 +20,7 @@ pub enum Cmd {
pub fn handle(cmd: Cmd) {
match cmd {
Cmd::Tx(inner) => tx::handle(inner),
// Cmd::Tx(inner) => tx::handle(inner),
Cmd::Data(inner) => data::handle(inner),
Cmd::Cardano(inner) => cardano::handle(inner),
Cmd::Wallet(inner) => wallet::handle(inner),

View File

@ -14,3 +14,4 @@ pub fn get_env() -> HashMap<String, String> {
}
env
}

View File

@ -1,51 +1,27 @@
use std::collections::HashMap;
use cryptoxide::hashing::blake2b_256;
use minicbor::encode;
use pallas_addresses::ShelleyPaymentPart;
use pallas_crypto::hash::{Hash, Hasher};
use pallas_crypto::key::ed25519::Signature;
use pallas_crypto::{self, key::ed25519::SecretKey};
use pallas_primitives::conway::{Tx, VKeyWitness};
use crate::tx::plutus::non_empty_set;
use crate::utils::v2a;
use cardano_tx_builder::Skey;
use rand::{rngs::OsRng, TryRngCore};
const PREFIX: &str = "wallet_";
pub struct Wallet {
pub skey: [u8; 32],
pub skey: Skey
}
impl Wallet {
pub fn vkey(&self) -> [u8; 32] {
SecretKey::from(self.skey).public_key().into()
}
pub fn key_hash(&self) -> Hash<28> {
Hasher::<224>::hash(SecretKey::from(self.skey).public_key().as_ref())
}
pub fn payment_credential(&self) -> ShelleyPaymentPart {
ShelleyPaymentPart::Key(self.key_hash())
}
pub fn sign_hash(&self, h: &[u8; 32]) -> Signature {
SecretKey::from(self.skey).sign(h)
}
pub fn sign(&self, tx: &mut Tx) {
let mut msg = Vec::new();
encode(&tx.transaction_body, &mut msg).unwrap();
let tx_hash = blake2b_256(&msg);
let sig = self.sign_hash(&tx_hash);
tx.transaction_witness_set.vkeywitness = non_empty_set(vec![VKeyWitness {
vkey: self.vkey().to_vec().into(),
signature: sig.as_ref().to_vec().into(),
}])
}
// pub fn sign(&self, tx: &mut Tx) {
// let mut msg = Vec::new();
// encode(&tx.transaction_body, &mut msg).unwrap();
// let tx_hash = blake2b_256(&msg);
// let sig = self.sign_hash(&tx_hash);
// tx.transaction_witness_set.vkeywitness = non_empty_set(vec![VKeyWitness {
// vkey: self.vkey().to_vec().into(),
// signature: sig.as_ref().to_vec().into(),
// }])
// }
}
pub fn from_env(env: &HashMap<String, String>) -> Wallet {