Skip to content
Permalink
Browse files

add input enumerator

  • Loading branch information...
vincenthz committed Mar 12, 2019
1 parent 05c9ce7 commit b8cdd8e0d8918d6e5c4137ab540762c631fb5607
@@ -3,7 +3,7 @@ use chain_crypto::{Ed25519Extended, PublicKey, SecretKey};
use imhamt::{Hamt, InsertError, UpdateError};
use std::collections::hash_map::DefaultHasher;

type AccountAlg = Ed25519Extended;
pub type AccountAlg = Ed25519Extended;

/// Possible errors during an account operation
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -43,6 +43,18 @@ impl From<InsertError> for LedgerError {
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Identifier(PublicKey<AccountAlg>);

impl From<PublicKey<AccountAlg>> for Identifier {
fn from(pk: PublicKey<AccountAlg>) -> Self {
Identifier(pk)
}
}

impl From<Identifier> for PublicKey<AccountAlg> {
fn from(i: Identifier) -> Self {
i.0
}
}

/// Account Secret Key
pub struct Secret(SecretKey<AccountAlg>);

@@ -174,6 +174,9 @@ impl Hash {
pub fn hash_bytes(bytes: &[u8]) -> Self {
Hash(crypto::Blake2b256::new(bytes))
}
pub fn from_bytes(bytes: [u8; 32]) -> Self {
Hash(crypto::Blake2b256::from(bytes))
}
}

impl property::Serialize for Hash {
@@ -1,14 +1,10 @@
mod transfer;
mod utxo;

use crate::key::{
deserialize_signature, serialize_signature, Hash, SpendingPublicKey, SpendingSecretKey,
SpendingSignature,
};
use crate::key::Hash;
use crate::value::*;
use chain_addr::Address;
use chain_core::property;
use chain_crypto::Verification;

// to remove..
pub use transfer::*;
@@ -1,13 +1,66 @@
use super::utxo::{TransactionId, UtxoPointer};
use crate::account;
use crate::value::*;
use chain_crypto::PublicKey;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// Generalized input which have a specific input value, and
/// either contains an account reference or a TransactionId+index
///
/// This uniquely refer to a specific source of value.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Input {
index_or_account: u8,
value: Value,
input_ptr: [u8; 32],
}

pub enum InputEnum {
AccountInput(account::Identifier, Value),
UtxoInput(UtxoPointer),
}

impl Input {
pub fn to_enum(&self) -> InputEnum {
if self.index_or_account == 0xff {
let pk =
PublicKey::from_bytes(&self.input_ptr).expect("internal error in publickey type");
InputEnum::AccountInput(pk.into(), self.value)
} else {
InputEnum::UtxoInput(UtxoPointer::new(
TransactionId::from_bytes(self.input_ptr.clone()),
self.index_or_account,
self.value,
))
}
}

pub fn from_enum(ie: InputEnum) -> Input {
match ie {
InputEnum::AccountInput(id, value) => {
let pk: PublicKey<account::AccountAlg> = id.into();
let mut input_ptr = [0u8; 32];
input_ptr.clone_from_slice(pk.as_ref());
Input {
index_or_account: 0xff,
value: value,
input_ptr: input_ptr,
}
}

InputEnum::UtxoInput(utxo_pointer) => {
let mut input_ptr = [0u8; 32];
input_ptr.clone_from_slice(utxo_pointer.transaction_id.as_ref());
Input {
index_or_account: utxo_pointer.output_index,
value: utxo_pointer.value,
input_ptr: input_ptr,
}
}
}
}
}

/// Information how tokens are spent.
/// A value of tokens is sent to the address.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Output<Address>(pub Address, pub Value);
pub struct Output<Address>(pub Address, pub Value);

0 comments on commit b8cdd8e

Please sign in to comment.
You can’t perform that action at this time.