-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a preview partial transaction & builder
- Loading branch information
Showing
6 changed files
with
252 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
crates/radix-engine-toolkit-uniffi/src/builder/preview_partial_transaction_v2_builder.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use crate::prelude::*; | ||
use std::ops::Deref; | ||
|
||
#[derive(Clone, Debug, Object)] | ||
pub struct PreviewPartialTransactionV2Builder { | ||
children_with_signers: Vec<PreviewPartialTransactionV2>, | ||
root_subintent_header: Option<IntentHeaderV2>, | ||
root_subintent_message: MessageV2, | ||
root_subintent_manifest: Option<TransactionManifestV2>, | ||
} | ||
|
||
#[uniffi::export] | ||
impl PreviewPartialTransactionV2Builder { | ||
#[uniffi::constructor] | ||
pub fn new() -> Arc<Self> { | ||
Arc::new(Self { | ||
children_with_signers: Default::default(), | ||
root_subintent_header: Default::default(), | ||
root_subintent_message: Default::default(), | ||
root_subintent_manifest: Default::default(), | ||
}) | ||
} | ||
|
||
pub fn add_child( | ||
self: Arc<Self>, | ||
child: Arc<PreviewPartialTransactionV2>, | ||
) -> Arc<Self> { | ||
self.with_builder(|builder| { | ||
builder.children_with_signers.push(child.as_ref().clone()) | ||
}) | ||
} | ||
|
||
pub fn message(self: Arc<Self>, message: MessageV2) -> Arc<Self> { | ||
self.with_builder(|builder| builder.root_subintent_message = message) | ||
} | ||
|
||
pub fn intent_header( | ||
self: Arc<Self>, | ||
intent_header: IntentHeaderV2, | ||
) -> Arc<Self> { | ||
self.with_builder(|builder| { | ||
builder.root_subintent_header = Some(intent_header) | ||
}) | ||
} | ||
|
||
pub fn manifest( | ||
self: Arc<Self>, | ||
manifest: Arc<TransactionManifestV2>, | ||
) -> Arc<Self> { | ||
self.with_builder(|builder| { | ||
builder.root_subintent_manifest = Some(manifest.as_ref().clone()) | ||
}) | ||
} | ||
|
||
pub fn prepare_for_signing( | ||
self: Arc<Self>, | ||
) -> Result<Arc<PreviewPartialTransactionV2>> { | ||
// Deconstructing the builder. | ||
let PreviewPartialTransactionV2Builder { | ||
children_with_signers, | ||
root_subintent_header: Some(header), | ||
root_subintent_message: message, | ||
root_subintent_manifest: | ||
Some(TransactionManifestV2 { | ||
instructions, | ||
blobs, | ||
children, | ||
}), | ||
} = Arc::try_unwrap(self).unwrap_or_else(|x| (*x).clone()) | ||
else { | ||
return Err( | ||
RadixEngineToolkitError::NotAllBuilderItemsWereSpecified, | ||
); | ||
}; | ||
|
||
// Constructing the partial transaction | ||
let partial_transaction = PartialTransactionV2 { | ||
root_subintent: SubintentV2::new(IntentCoreV2::new( | ||
header, | ||
blobs, | ||
message, | ||
children, | ||
instructions, | ||
)), | ||
non_root_subintents: children_with_signers | ||
.iter() | ||
.flat_map(|child| { | ||
let mut subintents = Vec::new(); | ||
subintents | ||
.push(child.partial_transaction.root_subintent.clone()); | ||
subintents.extend( | ||
child | ||
.partial_transaction | ||
.non_root_subintents | ||
.iter() | ||
.cloned(), | ||
); | ||
subintents | ||
}) | ||
.collect(), | ||
}; | ||
|
||
// Constructing the signed partial transaction | ||
let preview_partial_transaction = PreviewPartialTransactionV2 { | ||
partial_transaction: Arc::new(partial_transaction), | ||
root_subintent_signers: Default::default(), | ||
non_root_subintent_signers: children_with_signers | ||
.iter() | ||
.flat_map(|child| { | ||
let mut signers = Vec::new(); | ||
signers.push(child.root_subintent_signers.clone()); | ||
signers.extend(child.non_root_subintent_signers.clone()); | ||
signers | ||
}) | ||
.collect(), | ||
}; | ||
|
||
Ok(Arc::new(preview_partial_transaction)) | ||
} | ||
} | ||
|
||
impl PreviewPartialTransactionV2Builder { | ||
fn with_builder( | ||
self: Arc<Self>, | ||
callback: impl FnOnce(&mut Self), | ||
) -> Arc<Self> { | ||
let mut this = Arc::try_unwrap(self).unwrap_or_else(|x| (*x).clone()); | ||
callback(&mut this); | ||
Arc::new(this) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
crates/radix-engine-toolkit-uniffi/src/transaction_v2/preview_partial_transaction.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use crate::prelude::*; | ||
|
||
#[derive(Clone, Debug, Object)] | ||
pub struct PreviewPartialTransactionV2 { | ||
pub partial_transaction: Arc<PartialTransactionV2>, | ||
pub root_subintent_signers: Vec<PublicKey>, | ||
pub non_root_subintent_signers: Vec<Vec<PublicKey>>, | ||
} | ||
|
||
#[uniffi::export] | ||
impl PreviewPartialTransactionV2 { | ||
#[uniffi::constructor] | ||
pub fn new( | ||
partial_transaction: Arc<PartialTransactionV2>, | ||
root_subintent_signers: Vec<PublicKey>, | ||
non_root_subintent_signers: Vec<Vec<PublicKey>>, | ||
) -> Arc<Self> { | ||
Arc::new(Self { | ||
partial_transaction, | ||
root_subintent_signers, | ||
non_root_subintent_signers, | ||
}) | ||
} | ||
|
||
pub fn partial_transaction(&self) -> Arc<PartialTransactionV2> { | ||
// TODO: We're creating another pointer to the partial transaction | ||
// object which means that this doesn't quite follow value semantics. | ||
// The caller will have a reference to the partial transaction which | ||
// means that any changes made to it will reflect in the object that | ||
// they get back. This isn't the first instance of this but it's my | ||
// first time noticing it. This might be something that we want to fix | ||
// or look into when rearchitecting the toolkit such that it follows the | ||
// value semantics perfectly. Perhaps even taking a step back, am I | ||
// right about the assumption that this is now a reference and that it | ||
// won't follow value semantics? | ||
self.partial_transaction.clone() | ||
} | ||
|
||
pub fn root_subintent_signers(&self) -> Vec<PublicKey> { | ||
self.root_subintent_signers.clone() | ||
} | ||
|
||
pub fn non_root_subintent_signers(&self) -> Vec<Vec<PublicKey>> { | ||
self.non_root_subintent_signers.clone() | ||
} | ||
|
||
pub fn root_subintent_hash(&self) -> Result<Arc<TransactionHash>> { | ||
self.partial_transaction.root_subintent_hash() | ||
} | ||
} |