Skip to content

Commit

Permalink
beautify GenesisConfig
Browse files Browse the repository at this point in the history
Signed-off-by: muraca <mmuraca247@gmail.com>
  • Loading branch information
muraca committed Oct 16, 2023
1 parent bad03b6 commit 8a7d59e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 43 deletions.
9 changes: 9 additions & 0 deletions tuxedo-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ impl<V: Default> From<DynamicallyTypedData> for Output<V> {
}
}

impl<V, V1: Into<V>, P: Into<DynamicallyTypedData>> From<(P, V1)> for Output<V> {
fn from(values: (P, V1)) -> Self {
Self {
payload: values.0.into(),
verifier: values.1.into(),
}
}
}

#[cfg(test)]
pub mod tests {

Expand Down
15 changes: 15 additions & 0 deletions tuxedo-core/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ pub struct SigCheck {
pub owner_pubkey: H256,
}

impl SigCheck {
pub fn new<T: Into<H256>>(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) {
Expand Down Expand Up @@ -69,6 +77,13 @@ pub struct ThresholdMultiSignature {
}

impl ThresholdMultiSignature {
pub fn new(threshold: u8, signatories: Vec<H256>) -> Self {
ThresholdMultiSignature {
threshold,
signatories,
}
}

pub fn has_duplicate_signatories(&self) -> bool {
let set: BTreeSet<_> = self.signatories.iter().collect();
set.len() < self.signatories.len()
Expand Down
56 changes: 13 additions & 43 deletions tuxedo-template-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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: <money::Coin<0> 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: <money::Coin<0> 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 }
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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: <money::Coin<0> 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
Expand All @@ -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: <money::Coin<0> 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
Expand Down

0 comments on commit 8a7d59e

Please sign in to comment.