solana/src/mint.rs

86 lines
2.3 KiB
Rust
Raw Normal View History

2018-03-30 10:43:38 -07:00
//! The `mint` module is a library for generating the chain's genesis block.
2018-05-11 11:38:52 -07:00
use entry::Entry;
use hash::{hash, Hash};
use ring::rand::SystemRandom;
use signature::{KeyPair, KeyPairUtil, PublicKey};
use transaction::Transaction;
use untrusted::Input;
#[derive(Serialize, Deserialize, Debug)]
pub struct Mint {
pub pkcs8: Vec<u8>,
pubkey: PublicKey,
pub tokens: i64,
}
impl Mint {
pub fn new(tokens: i64) -> Self {
let rnd = SystemRandom::new();
2018-05-11 11:38:52 -07:00
let pkcs8 = KeyPair::generate_pkcs8(&rnd)
.expect("generate_pkcs8 in mint pub fn new")
.to_vec();
let keypair =
KeyPair::from_pkcs8(Input::from(&pkcs8)).expect("from_pkcs8 in mint pub fn new");
let pubkey = keypair.pubkey();
Mint {
pkcs8,
pubkey,
tokens,
}
}
pub fn seed(&self) -> Hash {
hash(&self.pkcs8)
}
pub fn last_id(&self) -> Hash {
self.create_entries()[1].id
}
pub fn keypair(&self) -> KeyPair {
2018-05-10 17:06:43 -07:00
KeyPair::from_pkcs8(Input::from(&self.pkcs8)).expect("from_pkcs8 in mint pub fn keypair")
2018-03-03 23:13:40 -08:00
}
pub fn pubkey(&self) -> PublicKey {
self.pubkey
2018-03-03 23:13:40 -08:00
}
2018-05-25 14:51:41 -07:00
pub fn create_transactions(&self) -> Vec<Transaction> {
2018-03-08 09:05:00 -08:00
let keypair = self.keypair();
2018-05-25 15:05:37 -07:00
let tx = Transaction::new(&keypair, self.pubkey(), self.tokens, self.seed());
vec![tx]
}
2018-03-05 12:03:56 -08:00
pub fn create_entries(&self) -> Vec<Entry> {
let e0 = Entry::new(&self.seed(), 0, vec![], false);
let e1 = Entry::new(&e0.id, 0, self.create_transactions(), false);
vec![e0, e1]
2018-03-05 12:03:56 -08:00
}
}
#[cfg(test)]
mod tests {
use super::*;
2018-05-29 12:28:07 -07:00
use budget::Budget;
2018-04-02 10:36:51 -07:00
use ledger::Block;
use transaction::{Instruction, Plan};
#[test]
2018-05-25 14:51:41 -07:00
fn test_create_transactions() {
let mut transactions = Mint::new(100).create_transactions().into_iter();
2018-05-25 15:05:37 -07:00
let tx = transactions.next().unwrap();
if let Instruction::NewContract(contract) = tx.instruction {
if let Plan::Budget(Budget::Pay(payment)) = contract.plan {
2018-05-25 15:05:37 -07:00
assert_eq!(tx.from, payment.to);
}
2018-03-03 23:13:40 -08:00
}
2018-05-25 14:51:41 -07:00
assert_eq!(transactions.next(), None);
}
#[test]
fn test_verify_entries() {
let entries = Mint::new(100).create_entries();
2018-04-02 10:36:51 -07:00
assert!(entries[..].verify(&entries[0].id));
}
}