This commit is contained in:
Hanh 2022-06-10 18:09:37 +08:00
parent 0a458421e6
commit ef97829bf7
8 changed files with 60 additions and 44 deletions

View File

@ -102,7 +102,8 @@ git = "https://github.com/hhanh00/librustzcash.git"
rev = "466806932d21597eb4f89a449347fa1983dffb22"
[dependencies.zcash_params]
path = "../zcash-params"
git = "https://github.com/hhanh00/zcash-params.git"
rev = "2781e8514533ea9f31e00465ee984036e3fa493c"
[dependencies.zcash_address]
git = "https://github.com/hhanh00/librustzcash.git"

View File

@ -7,7 +7,6 @@ use log::Level;
use std::ffi::{CStr, CString};
use std::io::Read;
use std::os::raw::c_char;
use std::path::Path;
use std::sync::Mutex;
use zcash_primitives::transaction::builder::Progress;

View File

@ -1,9 +1,9 @@
use crate::{connect_lightwalletd, CompactTxStreamerClient, DbAdapter, MemPool};
use anyhow::anyhow;
use lazy_static::lazy_static;
use lazycell::AtomicLazyCell;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
use anyhow::anyhow;
use tonic::transport::Channel;
use zcash_params::coin::{get_coin_chain, CoinChain, CoinType};
use zcash_params::{OUTPUT_PARAMS, SPEND_PARAMS};
@ -111,8 +111,7 @@ impl CoinConfig {
pub async fn connect_lwd(&self) -> anyhow::Result<CompactTxStreamerClient<Channel>> {
if let Some(lwd_url) = &self.lwd_url {
connect_lightwalletd(lwd_url).await
}
else {
} else {
Err(anyhow!("LWD URL Not set"))
}
}

View File

@ -950,7 +950,12 @@ impl DbAdapter {
let address: String = row.get(4)?;
let memo: String = row.get(5)?;
Ok(TxRec {
txid, height, timestamp, value, address, memo
txid,
height,
timestamp,
value,
address,
memo,
})
})?;
let mut txs = vec![];

View File

@ -406,9 +406,9 @@ pub mod compact_tx_streamer_client {
&mut self,
request: impl tonic::IntoRequest<super::BlockRange>,
) -> Result<
tonic::Response<tonic::codec::Streaming<super::CompactBlock>>,
tonic::Status,
> {
tonic::Response<tonic::codec::Streaming<super::CompactBlock>>,
tonic::Status,
> {
self.inner
.ready()
.await
@ -508,9 +508,9 @@ pub mod compact_tx_streamer_client {
&mut self,
request: impl tonic::IntoRequest<super::TransparentAddressBlockFilter>,
) -> Result<
tonic::Response<tonic::codec::Streaming<super::RawTransaction>>,
tonic::Status,
> {
tonic::Response<tonic::codec::Streaming<super::RawTransaction>>,
tonic::Status,
> {
self.inner
.ready()
.await
@ -531,9 +531,9 @@ pub mod compact_tx_streamer_client {
&mut self,
request: impl tonic::IntoRequest<super::TransparentAddressBlockFilter>,
) -> Result<
tonic::Response<tonic::codec::Streaming<super::RawTransaction>>,
tonic::Status,
> {
tonic::Response<tonic::codec::Streaming<super::RawTransaction>>,
tonic::Status,
> {
self.inner
.ready()
.await
@ -602,9 +602,9 @@ pub mod compact_tx_streamer_client {
&mut self,
request: impl tonic::IntoRequest<super::Exclude>,
) -> Result<
tonic::Response<tonic::codec::Streaming<super::CompactTx>>,
tonic::Status,
> {
tonic::Response<tonic::codec::Streaming<super::CompactTx>>,
tonic::Status,
> {
self.inner
.ready()
.await
@ -666,9 +666,9 @@ pub mod compact_tx_streamer_client {
&mut self,
request: impl tonic::IntoRequest<super::GetAddressUtxosArg>,
) -> Result<
tonic::Response<tonic::codec::Streaming<super::GetAddressUtxosReply>>,
tonic::Status,
> {
tonic::Response<tonic::codec::Streaming<super::GetAddressUtxosReply>>,
tonic::Status,
> {
self.inner
.ready()
.await

View File

@ -59,12 +59,14 @@ pub fn hex_to_hash(hex: &str) -> anyhow::Result<[u8; 32]> {
Ok(hash)
}
pub use crate::coinconfig::{CoinConfig, init_coin, set_active, set_active_account, set_coin_lwd_url};
pub use crate::builder::advance_tree;
pub use crate::chain::{
calculate_tree_state_v2, connect_lightwalletd, download_chain, get_latest_height, sync,
ChainError, DecryptNode,
};
pub use crate::coinconfig::{
init_coin, set_active, set_active_account, set_coin_lwd_url, CoinConfig,
};
pub use crate::commitment::{CTree, Witness};
pub use crate::db::{DbAdapter, TxRec};
pub use crate::hash::pedersen_hash;

View File

@ -2,16 +2,22 @@
extern crate rocket;
use rocket::fairing::AdHoc;
use rocket::serde::{Serialize, Deserialize, json::Json};
use rocket::serde::{json::Json, Deserialize, Serialize};
use rocket::State;
use warp_api_ffi::{CoinConfig, TxRec};
use warp_api_ffi::api::payment::{Recipient, RecipientMemo};
use warp_api_ffi::{CoinConfig, TxRec};
#[rocket::main]
async fn main() -> anyhow::Result<()> {
dotenv::dotenv()?;
warp_api_ffi::init_coin(0, &dotenv::var("ZEC_DB_PATH").unwrap_or("/tmp/zec.db".to_string()))?;
warp_api_ffi::set_coin_lwd_url(0, &dotenv::var("ZEC_LWD_URL").unwrap_or("https://mainnet.lightwalletd.com:9067".to_string()));
warp_api_ffi::init_coin(
0,
&dotenv::var("ZEC_DB_PATH").unwrap_or("/tmp/zec.db".to_string()),
)?;
warp_api_ffi::set_coin_lwd_url(
0,
&dotenv::var("ZEC_LWD_URL").unwrap_or("https://mainnet.lightwalletd.com:9067".to_string()),
);
let _ = rocket::build()
.mount(
@ -47,9 +53,15 @@ pub fn set_active(coin: u8, id_account: u32) {
warp_api_ffi::set_active_account(coin, id_account);
}
#[post("/new_account", format = "application/json", data="<seed>")]
#[post("/new_account", format = "application/json", data = "<seed>")]
pub fn new_account(seed: Json<AccountSeed>) -> String {
let id_account = warp_api_ffi::api::account::new_account(seed.coin, &seed.name, seed.key.clone(), seed.index).unwrap();
let id_account = warp_api_ffi::api::account::new_account(
seed.coin,
&seed.name,
seed.key.clone(),
seed.index,
)
.unwrap();
warp_api_ffi::set_active_account(seed.coin, id_account);
id_account.to_string()
}
@ -83,16 +95,11 @@ pub fn get_address() -> String {
pub fn get_backup(config: &State<Config>) -> Result<Json<Backup>, String> {
if !config.allow_backup {
Err("Backup API not enabled".to_string())
}
else {
} else {
let c = CoinConfig::get_active();
let db = c.db().unwrap();
let (seed, sk, fvk) = db.get_backup(c.id_account).unwrap();
Ok(Json(Backup {
seed,
sk,
fvk
}))
Ok(Json(Backup { seed, sk, fvk }))
}
}
@ -112,26 +119,31 @@ pub fn get_balance() -> String {
balance.to_string()
}
#[post("/pay", data="<payment>")]
#[post("/pay", data = "<payment>")]
pub async fn pay(payment: Json<Payment>, config: &State<Config>) -> Result<String, String> {
if !config.allow_send {
Err("Backup API not enabled".to_string())
}
else {
} else {
let c = CoinConfig::get_active();
let latest = warp_api_ffi::api::sync::get_latest_height().await.unwrap();
let from = {
let db = c.db().unwrap();
db.get_address(c.id_account).unwrap()
};
let recipients: Vec<_> = payment.recipients.iter().map(|p| RecipientMemo::from_recipient(&from, p)).collect();
let recipients: Vec<_> = payment
.recipients
.iter()
.map(|p| RecipientMemo::from_recipient(&from, p))
.collect();
let txid = warp_api_ffi::api::payment::build_sign_send_multi_payment(
latest,
&recipients,
false,
payment.confirmations,
Box::new(|_| {})
).await.unwrap();
Box::new(|_| {}),
)
.await
.unwrap();
Ok(txid)
}
}

View File

@ -1,7 +1,5 @@
use crate::coinconfig::CoinConfig;
use crate::{
AddressList, CompactTxStreamerClient, GetAddressUtxosArg, GetAddressUtxosReply,
};
use crate::{AddressList, CompactTxStreamerClient, GetAddressUtxosArg, GetAddressUtxosReply};
use bip39::{Language, Mnemonic, Seed};
use ripemd::{Digest, Ripemd160};
use secp256k1::{All, PublicKey, Secp256k1, SecretKey};