feat: use rust_util
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use std::{collections::HashMap, fs, fs::File};
|
||||
use rust_util::{SimpleError, XResult, simple_error};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use crate::{tx::Transaction, util::{SimpleError, XResult}};
|
||||
use crate::{tx::Transaction};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct CreditContract {
|
||||
@@ -24,10 +25,10 @@ impl CreditContract {
|
||||
|
||||
pub fn issue(&mut self, tx: &Transaction, receiver: &str, credit: u32) -> XResult<()> {
|
||||
if &self.admin != tx.sender.as_ref().ok_or_else(|| SimpleError::new("Sender is not provided".into()))? {
|
||||
return Err(SimpleError::new(format!("Current user is not admin, {} vs {:?}", self.admin, tx.sender)).into())
|
||||
return simple_error!("Current user is not admin, {} vs {:?}", self.admin, tx.sender);
|
||||
}
|
||||
if self.issue_amount + credit > self.credit_limit {
|
||||
return Err(SimpleError::new(format!("Issue too much credit, current: {}, issue: {}, limit: {}", self.issue_amount, credit, self.credit_limit)).into());
|
||||
return simple_error!("Issue too much credit, current: {}, issue: {}, limit: {}", self.issue_amount, credit, self.credit_limit);
|
||||
}
|
||||
match self.credit.get_mut(receiver) {
|
||||
None => { self.credit.insert(receiver.to_owned(), credit); },
|
||||
@@ -39,7 +40,7 @@ impl CreditContract {
|
||||
|
||||
pub fn transfer(&mut self, tx: &Transaction, receiver: &str, credit: u32) -> XResult<()> {
|
||||
match self.credit.get_mut(tx.sender.as_ref().ok_or_else(|| SimpleError::new("Sender is not provided".into()))?) {
|
||||
None => return Err(SimpleError::new(format!("Have not enough credit: {:?}", tx.sender)).into()),
|
||||
None => return simple_error!("Have not enough credit: {:?}", tx.sender),
|
||||
Some(cr) => {
|
||||
if *cr >= credit {
|
||||
*cr -= credit;
|
||||
@@ -48,7 +49,7 @@ impl CreditContract {
|
||||
Some(receiver_credit) => *receiver_credit += credit,
|
||||
}
|
||||
} else {
|
||||
return Err(SimpleError::new(format!("Have not enough credit: {:?}", tx.sender)).into());
|
||||
return simple_error!("Have not enough credit: {:?}", tx.sender);
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -69,7 +70,7 @@ impl CreditContract {
|
||||
|
||||
pub fn save(name: &str, c: &CreditContract) -> XResult<()> {
|
||||
if let Ok(_) = File::open(name) {
|
||||
return Err(SimpleError::new(format!("File exists: {}", name)).into());
|
||||
return simple_error!("File exists: {}", name);
|
||||
}
|
||||
fs::write(name, serde_json::to_string(c)?.as_bytes())?;
|
||||
Ok(())
|
||||
|
||||
@@ -3,6 +3,7 @@ pub mod tx;
|
||||
pub mod credit;
|
||||
|
||||
use std::str::FromStr;
|
||||
use rust_util::XResult;
|
||||
use secp256k1::{Message, Secp256k1, Signature};
|
||||
use util::*;
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use rust_util::{XResult, simple_error};
|
||||
use secp256k1::{Message, PublicKey, Secp256k1, SecretKey, Signature};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use std::str::FromStr;
|
||||
use crate::util::{SimpleError, XResult, calc_sha256, make_btc_address};
|
||||
use crate::util::{calc_sha256, make_btc_address};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Transaction {
|
||||
@@ -42,7 +43,7 @@ impl Transaction {
|
||||
|
||||
pub fn sign(&mut self, sender: &str, priv_key: &SecretKey) -> XResult<()> {
|
||||
if self.signature.is_some() {
|
||||
Err(SimpleError::new("Transaction is signed!".into()).into())
|
||||
simple_error!("Transaction is signed!")
|
||||
} else {
|
||||
let message = self.get_body_message()?;
|
||||
let sign = Secp256k1::new().sign(&message, priv_key);
|
||||
@@ -55,12 +56,12 @@ impl Transaction {
|
||||
pub fn verify(&self, pub_key: &PublicKey) -> XResult<()> {
|
||||
match (&self.sender, &self.signature) {
|
||||
(None, None) | (None, Some(_)) | (Some(_), None) => {
|
||||
Err(SimpleError::new("Transaction has no sender or not signed!".into()).into())
|
||||
simple_error!("Transaction has no sender or not signed!")
|
||||
},
|
||||
(Some(sender), Some(sign_hex)) => {
|
||||
let address = make_btc_address(pub_key);
|
||||
if &address != sender {
|
||||
return Err(SimpleError::new("".into()).into());
|
||||
return simple_error!("Assress and sender not match!");
|
||||
}
|
||||
let message = self.get_body_message()?;
|
||||
let sig = Signature::from_str(sign_hex)?;
|
||||
|
||||
@@ -1,32 +1,11 @@
|
||||
use rand::rngs::OsRng;
|
||||
use rust_util::XResult;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use secp256k1::{Secp256k1, SecretKey, key::PublicKey};
|
||||
use sha2::Sha256;
|
||||
use ripemd160::Ripemd160;
|
||||
use digest::{ Input, FixedOutput };
|
||||
use std::{fmt::Display, str::FromStr};
|
||||
use std::error::Error;
|
||||
|
||||
pub type XResult<T> = Result<T, Box<dyn Error>>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SimpleError {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl SimpleError {
|
||||
pub fn new(message: String) -> Self {
|
||||
Self { message }
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for SimpleError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "SimpleErorr, message: {}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for SimpleError {}
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
|
||||
Reference in New Issue
Block a user