Skip to content

Commit

Permalink
wip - wallet api cleanup & testing
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Sep 25, 2023
1 parent b168913 commit 4d21727
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 184 deletions.
38 changes: 0 additions & 38 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 20 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -205,31 +205,31 @@ pbkdf2 = { version = "0.12.1" }
# pbkdf2 = { version = "0.11", default-features = false}

# workflow dependencies
workflow-d3 = { version = "0.7.0" }
workflow-nw = { version = "0.7.0" }
workflow-log = { version = "0.7.0" }
workflow-core = { version = "0.7.0" }
workflow-wasm = { version = "0.7.0" }
workflow-dom = { version = "0.7.0" }
workflow-rpc = { version = "0.7.0" }
workflow-node = { version = "0.7.0" }
workflow-store = { version = "0.7.0" }
workflow-terminal = { version = "0.7.0" }
# workflow-d3 = { version = "0.7.0" }
# workflow-nw = { version = "0.7.0" }
# workflow-log = { version = "0.7.0" }
# workflow-core = { version = "0.7.0" }
# workflow-wasm = { version = "0.7.0" }
# workflow-dom = { version = "0.7.0" }
# workflow-rpc = { version = "0.7.0" }
# workflow-node = { version = "0.7.0" }
# workflow-store = { version = "0.7.0" }
# workflow-terminal = { version = "0.7.0" }
nw-sys = "0.1.5"

# if below is enabled, this means that there is an ongoing work
# on the workflow-rs crate. This requires that you clone workflow-rs
# into a sibling folder from https://github.com/workflow-rs/workflow-rs
# workflow-d3 = { path = "../workflow-rs/d3" }
# workflow-nw = { path = "../workflow-rs/nw" }
# workflow-log = { path = "../workflow-rs/log" }
# workflow-core = { path = "../workflow-rs/core" }
# workflow-wasm = { path = "../workflow-rs/wasm" }
# workflow-dom = { path = "../workflow-rs/dom" }
# workflow-rpc = { path = "../workflow-rs/rpc" }
# workflow-node = { path = "../workflow-rs/node" }
# workflow-store = { path = "../workflow-rs/store" }
# workflow-terminal = { path = "../workflow-rs/terminal" }
workflow-d3 = { path = "../workflow-rs/d3" }
workflow-nw = { path = "../workflow-rs/nw" }
workflow-log = { path = "../workflow-rs/log" }
workflow-core = { path = "../workflow-rs/core" }
workflow-wasm = { path = "../workflow-rs/wasm" }
workflow-dom = { path = "../workflow-rs/dom" }
workflow-rpc = { path = "../workflow-rs/rpc" }
workflow-node = { path = "../workflow-rs/node" }
workflow-store = { path = "../workflow-rs/store" }
workflow-terminal = { path = "../workflow-rs/terminal" }
# https://github.com/aspectron/nw-sys
# nw-sys = { path = "../nw-sys" }

Expand Down
8 changes: 6 additions & 2 deletions wallet/core/src/runtime/api/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ use crate::{

#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[serde(rename_all = "camelCase")]
pub struct PingRequest {}
pub struct PingRequest {
pub v: u32,
}

#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[serde(rename_all = "camelCase")]
pub struct PingResponse {}
pub struct PingResponse {
pub v: u32,
}

#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[serde(rename_all = "camelCase")]
Expand Down
18 changes: 6 additions & 12 deletions wallet/core/src/runtime/api/traits.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
use crate::imports::*;
// use crate::runtime::Wallet;
use crate::result::Result;
use crate::runtime::api::message::*;
use crate::runtime::{AccountCreateArgs, PrvKeyDataCreateArgs, WalletCreateArgs};
use crate::storage::WalletDescriptor;
use workflow_core::channel::Receiver;
// use ;

// - TODO
//

// Id

// fn generate_task_id() -> TaskId {
// TaskId::generate()
// }

#[async_trait]
pub trait WalletApi: Send + Sync + 'static {
pub trait WalletApi: Send + Sync + AnySync {
async fn register_notifications(self: Arc<Self>, channel: Receiver<WalletNotification>) -> Result<u64>;
async fn unregister_notifications(self: Arc<Self>, channel_id: u64) -> Result<()>;

Expand Down Expand Up @@ -45,6 +34,9 @@ pub trait WalletApi: Send + Sync + 'static {
self.wallet_create_call(WalletCreateRequest { wallet_args, prv_key_data_args, account_args }).await
}

async fn ping(self: Arc<Self>, v: u32) -> Result<u32> {
Ok(self.ping_call(PingRequest { v }).await?.v)
}
async fn ping_call(self: Arc<Self>, request: PingRequest) -> Result<PingResponse>;

async fn wallet_create_call(self: Arc<Self>, request: WalletCreateRequest) -> Result<WalletCreateResponse>;
Expand Down Expand Up @@ -72,3 +64,5 @@ pub trait WalletApi: Send + Sync + 'static {
}

pub type DynWalletApi = Arc<dyn WalletApi + Send + Sync + 'static>;

downcast_sync!(dyn WalletApi);
75 changes: 46 additions & 29 deletions wallet/core/src/runtime/api/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ use crate::result::Result;
use async_trait::async_trait;
use borsh::{BorshDeserialize, BorshSerialize};
use kaspa_wallet_macros::{build_wallet_client_transport_interface, build_wallet_server_transport_interface};
// use serde::de::DeserializeOwned;
// use serde::{Deserialize, Serialize};

#[async_trait]
pub trait Transport {
async fn call(&self, op: u64, request: &[u8]) -> Result<Vec<u8>>;
pub trait BorshTransport: Send + Sync {
async fn call(&self, op: u64, request: Vec<u8>) -> Result<Vec<u8>>;
}

#[async_trait]
pub trait SerdeTransport: Send + Sync {
async fn call(&self, op: &str, request: &str) -> Result<String>;
}

#[derive(Clone)]
pub enum Transport {
Borsh(Arc<dyn BorshTransport>),
Serde(Arc<dyn SerdeTransport>),
// SerdeWasm(Arc<dyn SerdeWasmTransport>),
}

// - TODO - WALLET SERVER
Expand All @@ -21,38 +31,45 @@ pub struct WalletServer {
pub wallet_api: Arc<dyn WalletApi>,
}

#[async_trait]
impl Transport for WalletServer {
async fn call(&self, op: u64, request: &[u8]) -> Result<Vec<u8>> {
build_wallet_server_transport_interface! {[
Ping,
WalletEnumerate,
WalletCreate,
WalletOpen,
WalletClose,
PrvKeyDataCreate,
PrvKeyDataRemove,
PrvKeyDataGet,
AccountEnumerate,
AccountCreate,
AccountImport,
AccountGet,
AccountCreateNewAddress,
AccountSend,
AccountEstimate,
TransactionDataGet,
AddressBookEnumerate,
]}
impl WalletServer {
pub fn new(wallet_api: Arc<dyn WalletApi>) -> Self {
Self { wallet_api }
}

pub fn wallet_api(&self) -> &Arc<dyn WalletApi> {
&self.wallet_api
}
}

impl WalletServer {
build_wallet_server_transport_interface! {[
Ping,
WalletEnumerate,
WalletCreate,
WalletOpen,
WalletClose,
PrvKeyDataCreate,
PrvKeyDataRemove,
PrvKeyDataGet,
AccountEnumerate,
AccountCreate,
AccountImport,
AccountGet,
AccountCreateNewAddress,
AccountSend,
AccountEstimate,
TransactionDataGet,
AddressBookEnumerate,
]}
}

pub struct WalletClient {
pub client_sender: Arc<dyn Transport + Send + Sync>,
pub transport: Transport,
}

impl WalletClient {
pub fn new(client_sender: Arc<dyn Transport + Send + Sync>) -> Self {
Self { client_sender }
pub fn new(transport: Transport) -> Self {
Self { transport }
}
}

Expand Down
5 changes: 3 additions & 2 deletions wallet/core/src/runtime/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,9 @@ impl WalletApi for Wallet {

// -------------------------------------------------------------------------------------

async fn ping_call(self: Arc<Self>, _request: PingRequest) -> Result<PingResponse> {
Ok(PingResponse { })
async fn ping_call(self: Arc<Self>, request: PingRequest) -> Result<PingResponse> {
log_info!("Wallet received ping request '{}' (should be 1)...", request.v);
Ok(PingResponse { v: request.v + 1 })
}

async fn wallet_enumerate_call(self: Arc<Self>, _request: WalletEnumerateRequest) -> Result<WalletEnumerateResponse> {
Expand Down
2 changes: 1 addition & 1 deletion wallet/macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ quote = "1.0.21"
syn = {version="1.0.99",features=["full","fold","extra-traits","parsing","proc-macro"]}
convert_case = "0.5.0"
regex.workspace = true
xxhash-rust.workspace = true
xxhash-rust = { workspace = true, features = ["xxh32"] }
Loading

0 comments on commit 4d21727

Please sign in to comment.