Skip to content

Commit

Permalink
Add tx hash to core ret
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Aug 29, 2023
1 parent ddd8fa3 commit 6820525
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 12 deletions.
9 changes: 7 additions & 2 deletions radix-engine-toolkit-core/src/functions/intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ use transaction::errors::*;
use transaction::model::*;
use transaction::validation::*;

pub fn hash(intent: &IntentV1) -> Result<Hash, PrepareError> {
intent.prepare().map(|prepared| prepared.intent_hash().0)
use crate::models::transaction_hash::TransactionHash;

pub fn hash(intent: &IntentV1) -> Result<TransactionHash, PrepareError> {
intent
.prepare()
.map(|prepared| prepared.intent_hash())
.map(|hash| TransactionHash::new(hash, intent.header.network_id))
}

pub fn compile(intent: &IntentV1) -> Result<Vec<u8>, EncodeError> {
Expand Down
14 changes: 12 additions & 2 deletions radix-engine-toolkit-core/src/functions/notarized_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,20 @@ use transaction::errors::*;
use transaction::model::*;
use transaction::validation::*;

pub fn hash(notarized_transaction: &NotarizedTransactionV1) -> Result<Hash, PrepareError> {
use crate::models::transaction_hash::TransactionHash;

pub fn hash(
notarized_transaction: &NotarizedTransactionV1,
) -> Result<TransactionHash, PrepareError> {
notarized_transaction
.prepare()
.map(|prepared| prepared.notarized_transaction_hash().0)
.map(|prepared| prepared.notarized_transaction_hash())
.map(|hash| {
TransactionHash::new(
hash,
notarized_transaction.signed_intent.intent.header.network_id,
)
})
}

pub fn compile(notarized_transaction: &NotarizedTransactionV1) -> Result<Vec<u8>, EncodeError> {
Expand Down
7 changes: 5 additions & 2 deletions radix-engine-toolkit-core/src/functions/signed_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ use transaction::errors::*;
use transaction::model::*;
use transaction::validation::*;

pub fn hash(signed_intent: &SignedIntentV1) -> Result<Hash, PrepareError> {
use crate::models::transaction_hash::TransactionHash;

pub fn hash(signed_intent: &SignedIntentV1) -> Result<TransactionHash, PrepareError> {
signed_intent
.prepare()
.map(|prepared| prepared.signed_intent_hash().0)
.map(|prepared| prepared.signed_intent_hash())
.map(|hash| TransactionHash::new(hash, signed_intent.intent.header.network_id))
}

pub fn compile(signed_intent: &SignedIntentV1) -> Result<Vec<u8>, EncodeError> {
Expand Down
1 change: 1 addition & 0 deletions radix-engine-toolkit-core/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
// under the License.

pub mod node_id;
pub mod transaction_hash;
37 changes: 37 additions & 0 deletions radix-engine-toolkit-core/src/models/transaction_hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 scrypto::prelude::*;
use transaction::prelude::{HashHasHrp, TransactionHashBech32Encoder};

pub struct TransactionHash {
pub hash: Hash,
pub id: String,
}

impl TransactionHash {
pub fn new<H>(transaction_hash: H, network_id: u8) -> Self
where
H: HashHasHrp + IsHash,
{
let network_definition = crate::utils::network_definition_from_network_id(network_id);
let encoder = TransactionHashBech32Encoder::new(&network_definition);
let hash = *transaction_hash.as_hash();
let id = encoder.encode(&transaction_hash).unwrap();
Self { hash, id }
}
}
2 changes: 1 addition & 1 deletion radix-engine-toolkit-uniffi/src/transaction/intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Intent {
pub fn hash(&self) -> Result<Arc<TransactionHash>> {
NativeIntent::try_from(self.clone()).and_then(|intent| {
core_intent_hash(&intent).map_err(Into::into).map(|hash| {
let intent_hash = NativeIntentHash(hash);
let intent_hash = NativeIntentHash(hash.hash);
Arc::new(TransactionHash::new(&intent_hash, self.header.network_id))
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl NotarizedTransaction {
core_notarized_transaction_hash(&notarized_transaction)
.map_err(Into::into)
.map(|hash| {
let notarized_transaction_hash = NativeNotarizedTransactionHash(hash);
let notarized_transaction_hash = NativeNotarizedTransactionHash(hash.hash);
Arc::new(TransactionHash::new(
&notarized_transaction_hash,
self.signed_intent.intent.header.network_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl SignedIntent {
core_signed_intent_hash(&signed_intent)
.map_err(Into::into)
.map(|hash| {
let signed_intent_hash = NativeSignedIntentHash(hash);
let signed_intent_hash = NativeSignedIntentHash(hash.hash);
Arc::new(TransactionHash::new(
&signed_intent_hash,
self.intent.header.network_id,
Expand Down
2 changes: 1 addition & 1 deletion radix-engine-toolkit/src/functions/intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::prelude::*;
#[typeshare::typeshare]
pub type IntentHashInput = SerializableIntent;
#[typeshare::typeshare]
pub type IntentHashOutput = SerializableHash;
pub type IntentHashOutput = SerializableTransactionHash;

pub struct IntentHash;
impl<'f> Function<'f> for IntentHash {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::prelude::*;
#[typeshare::typeshare]
pub type NotarizedTransactionHashInput = SerializableNotarizedTransaction;
#[typeshare::typeshare]
pub type NotarizedTransactionHashOutput = SerializableHash;
pub type NotarizedTransactionHashOutput = SerializableTransactionHash;

pub struct NotarizedTransactionHash;
impl<'f> Function<'f> for NotarizedTransactionHash {
Expand Down
2 changes: 1 addition & 1 deletion radix-engine-toolkit/src/functions/signed_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::prelude::*;
#[typeshare::typeshare]
pub type SignedIntentHashInput = SerializableSignedIntent;
#[typeshare::typeshare]
pub type SignedIntentHashOutput = SerializableHash;
pub type SignedIntentHashOutput = SerializableTransactionHash;

pub struct SignedIntentHash;
impl<'f> Function<'f> for SignedIntentHash {
Expand Down
38 changes: 38 additions & 0 deletions radix-engine-toolkit/src/models/transaction/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 radix_engine_toolkit_core::models::transaction_hash::TransactionHash;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::prelude::*;

#[typeshare::typeshare]
#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug, PartialEq, Eq)]
pub struct SerializableTransactionHash {
pub hash: SerializableHash,
pub id: String,
}

impl From<TransactionHash> for SerializableTransactionHash {
fn from(TransactionHash { hash, id }: TransactionHash) -> Self {
Self {
hash: hash.into(),
id,
}
}
}
1 change: 1 addition & 0 deletions radix-engine-toolkit/src/models/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

pub mod hash;
pub mod header;
pub mod instruction;
pub mod instructions;
Expand Down
1 change: 1 addition & 0 deletions radix-engine-toolkit/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub use crate::models::sbor::serialization_mode::*;
pub use crate::models::scrypto::node_id::*;
pub use crate::models::scrypto::non_fungible_global_id::*;
pub use crate::models::traits::*;
pub use crate::models::transaction::hash::*;
pub use crate::models::transaction::header::*;
pub use crate::models::transaction::instruction::*;
pub use crate::models::transaction::instructions::*;
Expand Down

0 comments on commit 6820525

Please sign in to comment.