Skip to content

Commit

Permalink
fix(txbuilder): sign transactions using conway era
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega committed Oct 24, 2024
1 parent 698d7a4 commit 7541d9c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 21 deletions.
30 changes: 18 additions & 12 deletions pallas-txbuilder/src/babbage.rs → pallas-txbuilder/src/conway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use pallas_primitives::{
conway::{
DatumOption, ExUnits as PallasExUnits, NativeScript, NetworkId, NonZeroInt, PlutusData,
PlutusScript, PostAlonzoTransactionOutput, PseudoScript as PallasScript,
PseudoTransactionOutput, Redeemer, RedeemerTag, TransactionBody, TransactionInput,
Tx as BabbageTx, Value, WitnessSet,
PseudoTransactionOutput, Redeemer, RedeemerTag, TransactionBody, TransactionInput, Tx,
Value, WitnessSet,
},
Fragment, NonEmptyKeyValuePairs, NonEmptySet, PositiveCoin,
};
Expand All @@ -25,15 +25,15 @@ use crate::{
TxBuilderError,
};

pub trait BuildBabbage {
fn build_babbage_raw(self) -> Result<BuiltTransaction, TxBuilderError>;
pub trait BuildConway {
fn build_conway_raw(self) -> Result<BuiltTransaction, TxBuilderError>;

// fn build_babbage(staging_tx: StagingTransaction, resolver: (), params: ()) ->
// Result<BuiltTransaction, TxBuilderError>;
}

impl BuildBabbage for StagingTransaction {
fn build_babbage_raw(self) -> Result<BuiltTransaction, TxBuilderError> {
impl BuildConway for StagingTransaction {
fn build_conway_raw(self) -> Result<BuiltTransaction, TxBuilderError> {
let mut inputs = self
.inputs
.unwrap_or_default()
Expand Down Expand Up @@ -220,15 +220,21 @@ impl BuildBabbage for StagingTransaction {
);

let script_data_hash = self.language_view.map(|language_view| {
scriptdata::ScriptData {
let dta = scriptdata::ScriptData {
redeemers: witness_set_redeemers.clone(),
datums: Some(plutus_data.clone()),
datums: if !plutus_data.is_empty() {
Some(plutus_data.clone())
} else {
None
},
language_view,
}
.hash()
};

dbg!(&dta);
dta.hash()
});

let mut pallas_tx = BabbageTx {
let mut pallas_tx = Tx {
transaction_body: TransactionBody {
inputs: pallas_primitives::Set::from(inputs),
outputs,
Expand Down Expand Up @@ -274,7 +280,7 @@ impl BuildBabbage for StagingTransaction {

Ok(BuiltTransaction {
version: self.version,
era: BuilderEra::Babbage,
era: BuilderEra::Conway,
status: TransactionStatus::Built,
tx_hash: Bytes32(*pallas_tx.transaction_body.compute_hash()),
tx_bytes: Bytes(pallas_tx.encode_fragment().unwrap()),
Expand Down
7 changes: 5 additions & 2 deletions pallas-txbuilder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod babbage;
mod conway;
mod scriptdata;
mod transaction;

pub use babbage::BuildBabbage;
pub use conway::BuildConway;
pub use transaction::model::{
BuiltTransaction, ExUnits, Input, Output, ScriptKind, StagingTransaction,
};
Expand Down Expand Up @@ -34,4 +34,7 @@ pub enum TxBuilderError {
/// Asset name is too long, it must be 32 bytes or less
#[error("Asset name must be 32 bytes or less")]
AssetNameTooLong,
/// Unsupported era
#[error("Unsupported era")]
UnsupportedEra,
}
22 changes: 22 additions & 0 deletions pallas-txbuilder/src/scriptdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl<C> Encode<C> for LanguageView {
}
}

#[derive(Debug, Clone)]
pub struct ScriptData {
pub redeemers: Redeemers,
pub datums: Option<Vec<PlutusData>>,
Expand Down Expand Up @@ -102,6 +103,23 @@ mod tests {
]
});

pub static COST_MODEL_PLUTUS_V2_PREPROD: LazyLock<Vec<i64>> = LazyLock::new(|| {
vec![
205665, 812, 1, 1, 1000, 571, 0, 1, 1000, 24177, 4, 1, 1000, 32, 117366, 10475, 4,
23000, 100, 23000, 100, 23000, 100, 23000, 100, 23000, 100, 23000, 100, 100, 100,
23000, 100, 19537, 32, 175354, 32, 46417, 4, 221973, 511, 0, 1, 89141, 32, 497525,
14068, 4, 2, 196500, 453240, 220, 0, 1, 1, 1000, 28662, 4, 2, 245000, 216773, 62, 1,
1060367, 12586, 1, 208512, 421, 1, 187000, 1000, 52998, 1, 80436, 32, 43249, 32, 1000,
32, 80556, 1, 57667, 4, 1000, 10, 197145, 156, 1, 197145, 156, 1, 204924, 473, 1,
208896, 511, 1, 52467, 32, 64832, 32, 65493, 32, 22558, 32, 16563, 32, 76511, 32,
196500, 453240, 220, 0, 1, 1, 69522, 11687, 0, 1, 60091, 32, 196500, 453240, 220, 0, 1,
1, 196500, 453240, 220, 0, 1, 1, 1159724, 392670, 0, 2, 806990, 30482, 4, 1927926,
82523, 4, 265318, 0, 4, 0, 85931, 32, 205665, 812, 1, 1, 41182, 32, 212342, 32, 31220,
32, 32696, 32, 43357, 32, 32247, 32, 38314, 32, 35892428, 10, 57996947, 18975, 10,
38887044, 32947, 10,
]
});

const TEST_VECTORS: LazyLock<Vec<(Vec<u8>, LanguageView)>> = LazyLock::new(|| {
vec![
(
Expand All @@ -112,6 +130,10 @@ mod tests {
hex::decode(include_str!("../../test_data/conway2.tx")).unwrap(),
LanguageView(0, COST_MODEL_PLUTUS_V1.clone()),
),
(
hex::decode(include_str!("../../test_data/hydra-init.tx")).unwrap(),
LanguageView(1, COST_MODEL_PLUTUS_V2_PREPROD.clone()),
),
]
});

Expand Down
23 changes: 16 additions & 7 deletions pallas-txbuilder/src/transaction/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pallas_crypto::{
hash::{Hash, Hasher},
key::ed25519,
};
use pallas_primitives::{babbage, Fragment};
use pallas_primitives::{babbage, conway, Fragment, NonEmptySet};
use pallas_wallet::PrivateKey;

use std::{collections::HashMap, ops::Deref};
Expand Down Expand Up @@ -599,6 +599,7 @@ impl From<PallasAddress> for Address {
#[serde(rename_all = "snake_case")]
pub enum BuilderEra {
Babbage,
Conway,
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -626,28 +627,34 @@ impl BuiltTransaction {
.unwrap();

match self.era {
BuilderEra::Babbage => {
BuilderEra::Conway => {
let mut new_sigs = self.signatures.unwrap_or_default();

new_sigs.insert(Bytes32(pubkey), Bytes64(signature));

self.signatures = Some(new_sigs);

// TODO: chance for serialisation round trip issues?
let mut tx = babbage::Tx::decode_fragment(&self.tx_bytes.0)
let mut tx = conway::Tx::decode_fragment(&self.tx_bytes.0)
.map_err(|_| TxBuilderError::CorruptedTxBytes)?;

let mut vkey_witnesses = tx.transaction_witness_set.vkeywitness.unwrap_or_default();
let mut vkey_witnesses = tx
.transaction_witness_set
.vkeywitness
.map(|x| x.to_vec())
.unwrap_or_default();

vkey_witnesses.push(babbage::VKeyWitness {
vkey: Vec::from(pubkey.as_ref()).into(),
signature: Vec::from(signature.as_ref()).into(),
});

tx.transaction_witness_set.vkeywitness = Some(vkey_witnesses);
tx.transaction_witness_set.vkeywitness =
Some(NonEmptySet::from_vec(vkey_witnesses).unwrap());

self.tx_bytes = tx.encode_fragment().unwrap().into();
}
_ => return Err(TxBuilderError::UnsupportedEra),
}

Ok(self)
Expand All @@ -659,7 +666,7 @@ impl BuiltTransaction {
signature: [u8; 64],
) -> Result<Self, TxBuilderError> {
match self.era {
BuilderEra::Babbage => {
BuilderEra::Conway => {
let mut new_sigs = self.signatures.unwrap_or_default();

new_sigs.insert(
Expand Down Expand Up @@ -689,14 +696,15 @@ impl BuiltTransaction {

self.tx_bytes = tx.encode_fragment().unwrap().into();
}
_ => return Err(TxBuilderError::UnsupportedEra),
}

Ok(self)
}

pub fn remove_signature(mut self, pub_key: ed25519::PublicKey) -> Result<Self, TxBuilderError> {
match self.era {
BuilderEra::Babbage => {
BuilderEra::Conway => {
let mut new_sigs = self.signatures.unwrap_or_default();

let pk = Bytes32(
Expand All @@ -722,6 +730,7 @@ impl BuiltTransaction {

self.tx_bytes = tx.encode_fragment().unwrap().into();
}
_ => return Err(TxBuilderError::UnsupportedEra),
}

Ok(self)
Expand Down
1 change: 1 addition & 0 deletions test_data/hydra-init.tx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
84a700d9010281825820e9a81012c52f175287ca2f0b73912915f6a75aa0d21b339e9af0af707674d0ad020183a300581d702ee477c60839936be49a50030690865b5bed4db8cd2f05bf255ac68001821a001e8480a1581cb88de01f11692b4f660799b5fd3537be275f728aace9597e325ec059a14b487964726148656164563101028201d8185878d8799fd8799f19ea60ff9f58207bbfc8ffc6da9e6f6f070f0f28a4c0de8e099c34485e192660475059d8bb9557ff581cb88de01f11692b4f660799b5fd3537be275f728aace9597e325ec059d8799fd8799f5820e9a81012c52f175287ca2f0b73912915f6a75aa0d21b339e9af0af707674d0adff02ffffa300581d702fac819a1f4f14e29639d1414220d2a18b6abd6b8e444d88d0dda8ff01821a001e8480a1581cb88de01f11692b4f660799b5fd3537be275f728aace9597e325ec059a1581c9b29dd55b38a5e824775d303723e83e08d83c4ba72ceab284154b8a201028201d818581e581cb88de01f11692b4f660799b5fd3537be275f728aace9597e325ec059a200581d609b29dd55b38a5e824775d303723e83e08d83c4ba72ceab284154b8a2011b0000000250248439021a004c4b4009a1581cb88de01f11692b4f660799b5fd3537be275f728aace9597e325ec059a24b487964726148656164563101581c9b29dd55b38a5e824775d303723e83e08d83c4ba72ceab284154b8a2010b5820a35efb257287f7f146a97c8c5eaa798bab0da34d90fcc779e0292075186afa620dd9010281825820e9a81012c52f175287ca2f0b73912915f6a75aa0d21b339e9af0af707674d0ad020f00a20581840100d8799fff821a00d59f801b00000002540be40006d901028159119959119601000033332323233223322323233223232323232323232323232222225335325335323232325335333573400203a0382244002264a66a666ae6800407807444880084c07d240103505431003370e900100199b8748000008d55cf0011aab9d0013754006266014921034d30360053353330082232333573400203803666e3c008004c024004c02cd40048800884cc074894cd40044070884d4008894cd4c8ccd5cd00081081019b88001480004c01800c407c004406054cc8d4cc02d241034d303100323301e225335001101c22153353500a2235004223253353335734002048046264666ae68004094090cdc3802001081199b8f004002101f130040010013301d225335001101a22133501b350022200230040013500122222222222200c15332323232323235330114901034d30320032333573400204204066e1cc94cd4ccc04088c8ccd5cd00081201199b8f00200150073013008213002001148000c8cc88c88c008004c098894cd4004400c884d400888cc020004c01800cc00488008c004880048cc00488cdc0001000a400066e0000520021325335330124901034d3033003302422533500110222213500222325335333573400205004e2602c0042600e00866e3c00801cd54014880044c94cd54cc8d4cc0512401034d30340032333573400204804666e1c010cc0184488c88c008004c0a0894cd4004400c884cc018008c0100040044cc06494cd4ccc04c88c8ccd5cd00081381319b8f002001004001215335001130264901034d303800221350022253350031533532333573400205405266e1c005200210291302a491034d303800221302c491034d30380013025491034d303700001330252253350011022221335023350022200130040010011021153353301825333500115016215335301b00121330164901034d31320032333573400204c04a66e3c0040144c0952401034d31320021501733025225335001102222133502335002220023004001001133013491034d303500533532333573400204604466e3cc01088800c0084d4c01088800888d403c88c94cd4ccd5cd000813813099199ab9a0010280273370e008004204c66e3c010008408440844084cc01803802040814018c00488800554d5400888ccd400854054854cd4cccccc06400488c94c8c8c8c8c8cd4ccd5cd0030148140a99a9999aab9f0072502323232302633027002001357440126ae840200ac84d4004894cd4c014008854cd4cccd55cf80112813919191815198158010009aba20043574200605e426a00244a66a66a01601800442a66a6666aae7c008940ac8c8c8c0b8cc0bc008004d5d10021aba10030332135001225335302b00221533533335573e0044a05e4646460640046ae88010d5d080181b90a99a9999998170009119299a999ab9a0010390381533533335573e0044a06646464606c6606e0040026ae88010d5d080181d909a80091299a99999981a0011119299a999ab9a00103f03e1533533335573e0044a0724646460780046ae88010d5d080182090a99a981b80090981d8008a81c8a81c0a81c19b8748000008940d8940d8940d8940d8854cd4cccd55cf8011281b91919181d0011aba20043574200607e42a66a6030002426072660760060022a06e2a06c2a06a2a0642a06466e1d20000022503025030250302503021303112222333300200f00b0070051300e4984c035261300c4984c02526130084984c01526130044984c00526130014988c94c8cd4ccd5cd0010158150a99a9999aab9f00925025232323028002357440166ae840280b4854cd4cccccc09000488c94cd4ccd5cd0008178170a99a9999aab9f0022502923232302c3302d002001357440086ae8400c0c484d4004894cd4c0a4008854cd4cccd55cf80112816919191818198188010009aba20043574200606a426a00244a66a66a02202400442a66a6666aae7c008940c48c8c8c0d0cc0d4008004d5d10021aba10030392135001225335301300221533533335573e0044a06a464646070660720040026ae88010d5d080181e909a80091299a980c00110a99a9999aab9f0022503923232303c002357440086ae8400c104854cd4c0dc00484c0ec48ccccc004040030020010008540e4540e0540dc540d0540cc540c0540bc540b0540ac540a0540a0cdc3a40000044a04c4a04c4a04c4a04c42604e24444600200a260049309800a4c260029311929919a999ab9a00202d02c1533533335573e0164a04e4646460540046ae88034d5d080601790a99a9999998130009119299a999ab9a0010310301533533335573e0044a05646464605c6605e0040026ae88010d5d0801819909a80091299a981580110a99a9999aab9f0022502f23232303233033002001357440086ae8400c0dc84d400488c94cd4cc00405400c854cd4cccd55cf8019281a11919181b9981c0010009aba200535742008078426a00244a66a602c00442a66a6666aae7c008940e08c8c8c0eccc0f0008004d5d10021aba10030402135001225335301b00221533533335573e0044a07846464607e660800040026ae88010d5d0801822109a80091299a980f80110a99a9999aab9f0022504023232304333044002001357440086ae8400c12084d4004894cd4c100008854cd4cccd55cf80112822119191823998240010009aba200435742006098426a00244a66a608800442a66a6666aae7c008941208c8c8c12ccc130008004d5d10021aba100305021350012253353301904800221533533335573e0044a09846464609e0046ae88010d5d080182a10a99a98168009098270919999999980081080e00c00a0080060040020010a8260a8258a8250a8238a8230a8218a8210a81f8a81f0a81d8a81d0a81b8a81b0a8198a81928098a8170a8168a8150a81519b8748000008940a0940a0940a0940a084c0a448888c0100144c00926130014984c005262325335333573400205c05a26050244440062a04e66e1d200600c3370e900200519b87480080208cccccc08000488c94cd4ccd5cd0008158150a99a9999aab9f00225025232323028002357440086ae8400c0b4854cd4c01800484c09c004540945409054090cdc3a40000044a0444a0444a0444a044466666603e00244a0444a0424a042460440024a042264464466666604200244a0484a0460044a0464a0466054446666aae7c004801494cd4c010d5d080110a99a98021aba2003213026335029002001150241325024300335744004056603e0464a66a60380024260400022a03c66e1d20000022501b2501b2501b2501b21533335001215017150162222132123330010040030023300911223223002001302b2253350011003221330060023004001003215017130224901034d3039002150162335500122337000029001240002a66a660020100062603a9201034d313100221533500110022213021491034d3131002233020225335001101d22132533535003222235004223350022502223253353335734002054052260486604a00c00e2a04666e3c00403884cd40800040084004c010004d40048888888888880284c0280084064d4004880084060c8c8c8c94cd4ccd5cd00080e00d89919191991091980080180119191919299a999ab9a001023022132323232323232323232332333233333233323332222222222221233333333333300100d00c00b00a009008007006005004003002330032323232325335333573400206c06a26644246600200600460306ae84008c018d5d09aba200213037490103505431003370e90000011aab9e00235573a0026ea8004d5d08081aba100e330030013574201a464646464a66a666ae680040d40d04c8c8c8cccc888848cccc00401401000c008c8c8c8c94cd4ccd5cd00081e01d89991091980080180118101aba10023300501f357426ae880084c0f5240103505431003370e90000011aab9e00235573a0026ea8d5d080298049aba1003323232325335333573400207807622444006264a66a666ae680040f40f04c84888c004010dd71aba10031325335333573400207c07a264244460040086ae840104c0fd24103505431003370e900200219b874800800ccdc3a40000046aae78008d55ce8009baa3574200466002eb8d5d09aba200222323232325335333573400207a0782a06c264a66a666ae680040f80f44c0e0c01cd5d08018981fa4903505431003370e900000199b8748008008d55cf0011aab9d00137540026ae88004d5d10010981b24903505431003370e90000011aab9e00235573a0026ea8004c004d5d080598009aba100a3002300275a6ae84020d5d08039aba10063300175c6ae840148c88c008dd60009819111999aab9f0012030233502f30043574200460066ae880080ccd5d08019980090009aba10022322300237560026062446666aae7c00480bc8c8cd40bccc0b4dd71aab9d001300535573c00260086ae8800cd5d08010191aba1357440026ae88004d5d10009aba2001357440026ae88004d5d10009aba2001357440026ae88004d5d10010981224903505431003370e90000011aab9e00235573a0026ea8d5d080299191919299a999ab9a001023022132122223003005375c6ae840084c94cd4ccd5cd00081201189909111180080298031aba10031325335333573400204a04826424444600400a60106ae840104c94cd4ccd5cd00081301289909111180200299191919299a999ab9a00102a029132122222223005008300d35742004264a66a666ae680040ac0a84c848888888c01c020c038d5d0801899299a999ab9a00102c02b133221222222233006009008300f357420086eb8d5d09aba20041325335333573400205a0582664424444444660040120106eb8d5d08029bae357426ae880144c94cd4ccd5cd0008170168999109111111198008048041bae3574200c6eb4d5d09aba20061325335333573400205e05c224444444008264a66a666ae680040c00bc44888888800c4c0c524103505431003370e900600419b874802801ccdc3a401000c66e1d20060053370e900200219b874800800ccdc3a40000046aae78008d55ce8009baa3574200a2604e920103505431003370e900300299b8748010010cdc3a400400666e1d200000235573c0046aae74004dd51aba13574400a464646464a66a666ae6800408c0884cc07cc8c8c8c94cd4ccd5cd00081381309bae357420042605092103505431003370e90000011aab9e00235573a0026ea8d5d08011bad357426ae880084c09124103505431003370e90000011aab9e00235573a0026ea80048c8c8c8c94cd4ccd5cd000811010899091180100198031aba10021325335333573400204604426466644424466600200a0080066eb4d5d08021bad357420026eb4d5d09aba20013574400626048920103505431003370e900100199b8748000008d55cf0011aab9d0013754002464646464a66a666ae680040840804c8488c00800cdd71aba10021325335333573400204404226424460020066eb8d5d080189811a48103505431003370e900100199b8748000008d55cf0011aab9d00137540022603a920103505431003370e90000011aab9e00235573a0026ea80045261301949103505435003015225335001101322135002223253353335734002032030264666ae68004068064cdc3801240042600e00866e3c00922010b48796472614865616456310022322300200130162253350011500e22135002225335330080020071301300113006003235001223233335002250022001250022500213263357389201024c68000152253350011011133573800402046a0024444444444440102601c921034d3132001300d491034d3039001300c491034d31300023223002001300e225335001100d2215335300500213004001100e2333333002001225005250042500425004230050012222223333333574800c4646600e6aae74004d55cf0009baa00723005375600e460086eb001c8c00cdd6803918011bae007010212230020031122001221233001003002221233001003002122002122122330010040031220021220012326335738002004240024646002002460044660040040029111c2fac819a1f4f14e29639d1414220d2a18b6abd6b8e444d88d0dda8ff0048811c2ee477c60839936be49a50030690865b5bed4db8cd2f05bf255ac68000333351220022212330010030022001488120e9a81012c52f175287ca2f0b73912915f6a75aa0d21b339e9af0af707674d0ad00480101f5f6

0 comments on commit 7541d9c

Please sign in to comment.