solana/src/mint.rs

91 lines
2.5 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, Pubkey};
use transaction::Transaction;
use untrusted::Input;
#[derive(Serialize, Deserialize, Debug)]
pub struct Mint {
pub pkcs8: Vec<u8>,
pubkey: Pubkey,
pub tokens: i64,
}
impl Mint {
2018-07-12 14:42:01 -07:00
pub fn new_with_pkcs8(tokens: i64, pkcs8: Vec<u8>) -> Self {
2018-05-11 11:38:52 -07:00
let keypair =
2018-08-09 07:56:04 -07:00
Keypair::from_pkcs8(Input::from(&pkcs8)).expect("from_pkcs8 in mint pub fn new");
let pubkey = keypair.pubkey();
Mint {
pkcs8,
pubkey,
tokens,
}
}
2018-07-12 14:42:01 -07:00
pub fn new(tokens: i64) -> Self {
let rnd = SystemRandom::new();
2018-08-09 07:56:04 -07:00
let pkcs8 = Keypair::generate_pkcs8(&rnd)
2018-07-12 14:42:01 -07:00
.expect("generate_pkcs8 in mint pub fn new")
.to_vec();
Self::new_with_pkcs8(tokens, pkcs8)
}
pub fn seed(&self) -> Hash {
hash(&self.pkcs8)
}
pub fn last_id(&self) -> Hash {
self.create_entries()[1].id
}
2018-08-09 07:56:04 -07:00
pub fn keypair(&self) -> Keypair {
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) -> Pubkey {
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();
let tx = Transaction::system_move(&keypair, self.pubkey(), self.tokens, self.seed(), 0);
2018-05-25 15:05:37 -07:00
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::*;
use bincode::deserialize;
2018-04-02 10:36:51 -07:00
use ledger::Block;
use system_contract::SystemContract;
#[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();
assert!(SystemContract::check_id(&tx.contract_id));
let instruction: SystemContract = deserialize(&tx.userdata).unwrap();
if let SystemContract::Move { tokens } = instruction {
assert_eq!(tokens, 100);
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));
}
}