From 8a7d59e09b2509b2b66db654cbd3af6d6873b1c3 Mon Sep 17 00:00:00 2001 From: muraca Date: Mon, 16 Oct 2023 18:12:51 +0200 Subject: [PATCH] beautify GenesisConfig Signed-off-by: muraca --- tuxedo-core/src/types.rs | 9 +++++ tuxedo-core/src/verifier.rs | 15 ++++++++ tuxedo-template-runtime/src/lib.rs | 56 +++++++----------------------- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/tuxedo-core/src/types.rs b/tuxedo-core/src/types.rs index c41860433..c9d1fbdc4 100644 --- a/tuxedo-core/src/types.rs +++ b/tuxedo-core/src/types.rs @@ -190,6 +190,15 @@ impl From for Output { } } +impl, P: Into> From<(P, V1)> for Output { + fn from(values: (P, V1)) -> Self { + Self { + payload: values.0.into(), + verifier: values.1.into(), + } + } +} + #[cfg(test)] pub mod tests { diff --git a/tuxedo-core/src/verifier.rs b/tuxedo-core/src/verifier.rs index 9a6bfb2d7..a58e12228 100644 --- a/tuxedo-core/src/verifier.rs +++ b/tuxedo-core/src/verifier.rs @@ -31,6 +31,14 @@ pub struct SigCheck { pub owner_pubkey: H256, } +impl SigCheck { + pub fn new>(value: T) -> Self { + SigCheck { + owner_pubkey: value.into(), + } + } +} + impl Verifier for SigCheck { fn verify(&self, simplified_tx: &[u8], redeemer: &[u8]) -> bool { let sig = match Signature::try_from(redeemer) { @@ -69,6 +77,13 @@ pub struct ThresholdMultiSignature { } impl ThresholdMultiSignature { + pub fn new(threshold: u8, signatories: Vec) -> Self { + ThresholdMultiSignature { + threshold, + signatories, + } + } + pub fn has_duplicate_signatories(&self) -> bool { let set: BTreeSet<_> = self.signatories.iter().collect(); set.len() < self.signatories.len() diff --git a/tuxedo-template-runtime/src/lib.rs b/tuxedo-template-runtime/src/lib.rs index a76f88443..7b36d04bd 100644 --- a/tuxedo-template-runtime/src/lib.rs +++ b/tuxedo-template-runtime/src/lib.rs @@ -36,7 +36,6 @@ use sp_version::RuntimeVersion; use serde::{Deserialize, Serialize}; use tuxedo_core::{ - dynamic_typing::{DynamicallyTypedData, UtxoData}, tuxedo_constraint_checker, tuxedo_verifier, types::Transaction as TuxedoTransaction, verifier::{SigCheck, ThresholdMultiSignature, UpForGrabs}, @@ -113,40 +112,25 @@ pub struct GenesisConfig { impl Default for GenesisConfig { fn default() -> Self { use hex_literal::hex; + use money::Coin; const SHAWN_PUB_KEY_BYTES: [u8; 32] = hex!("d2bf4b844dfefd6772a8843e669f943408966a977e3ae2af1dd78e0f55f4df67"); const ANDREW_PUB_KEY_BYTES: [u8; 32] = hex!("baa81e58b1b4d053c2e86d93045765036f9d265c7dfe8b9693bbc2c0f048d93a"); + let signers = vec![SHAWN_PUB_KEY_BYTES.into(), ANDREW_PUB_KEY_BYTES.into()]; + // Initial Config just for a Money UTXO - GenesisConfig { - genesis_utxos: vec![ - Output { - verifier: OuterVerifier::SigCheck(SigCheck { - owner_pubkey: SHAWN_PUB_KEY_BYTES.into(), - }), - payload: DynamicallyTypedData { - data: 100u128.encode(), - type_id: as UtxoData>::TYPE_ID, - }, - }, - Output { - verifier: OuterVerifier::ThresholdMultiSignature(ThresholdMultiSignature { - threshold: 1, - signatories: vec![SHAWN_PUB_KEY_BYTES.into(), ANDREW_PUB_KEY_BYTES.into()], - }), - payload: DynamicallyTypedData { - data: 100u128.encode(), - type_id: as UtxoData>::TYPE_ID, - }, - }, - ], - } + let genesis_utxos = vec![ + (Coin::<0>(100), SigCheck::new(SHAWN_PUB_KEY_BYTES)).into(), + (Coin::<0>(100), ThresholdMultiSignature::new(1, signers)).into(), + ]; // TODO: Initial UTXO for Kitties // TODO: Initial UTXO for Existence + GenesisConfig { genesis_utxos } } } @@ -427,6 +411,7 @@ impl_runtime_apis! { #[cfg(test)] mod tests { use super::*; + use money::Coin; use parity_scale_codec::Encode; use sp_core::testing::SR25519; use sp_keystore::testing::MemoryKeystore; @@ -461,15 +446,7 @@ mod tests { .unwrap(); // Grab genesis value from storage and assert it is correct - let genesis_utxo = Output { - verifier: OuterVerifier::SigCheck(SigCheck { - owner_pubkey: shawn_pub_key.into(), - }), - payload: DynamicallyTypedData { - data: 100u128.encode(), - type_id: as UtxoData>::TYPE_ID, - }, - }; + let genesis_utxo = (Coin::<0>(100), SigCheck::new(shawn_pub_key)).into(); let output_ref = OutputRef { // Genesis UTXOs don't come from any real transaction, so just uze the zero hash @@ -494,17 +471,10 @@ mod tests { let andrew_pub_key = keystore .sr25519_generate_new(SR25519, Some(ANDREW_PHRASE)) .unwrap(); + let signatories = vec![shawn_pub_key.into(), andrew_pub_key.into()]; - let genesis_multi_sig_utxo = Output { - verifier: OuterVerifier::ThresholdMultiSignature(ThresholdMultiSignature { - threshold: 1, - signatories: vec![shawn_pub_key.into(), andrew_pub_key.into()], - }), - payload: DynamicallyTypedData { - data: 100u128.encode(), - type_id: as UtxoData>::TYPE_ID, - }, - }; + let genesis_multi_sig_utxo = + (Coin::<0>(100), ThresholdMultiSignature::new(1, signatories)).into(); let output_ref = OutputRef { // Genesis UTXOs don't come from any real transaction, so just uze the zero hash