Skip to content

Commit

Permalink
WalletEvent structure, wallet api example update, (kaspanet#40)
Browse files Browse the repository at this point in the history
* transaction.addresses getter

* SignableTransaction struct removed

* docs

* Address.isValid(string)  : a static method for address validation

* typescript docs: Optional NetworkId

* Address.isValid -> Address.validate

* tx.addresses getter to tx.addresses() method

* transaction.addresses() updated for address creation from SPK

* using NetworkTypeT instead of NetworkType

* batch mode issue

* Delete mod copy.rs

* AccountKind constructor, WalletApiObjectExtension helper method

* TS IAccountsCreateRequest type updated using union
* IWalletEvent "event" property renamed to "type"
* examples/wallet.js WIP

* TransactionRecord  notification structure

* TransactionRecord notification structure

* wallet example update

* ecdsa address creation issue

* create_address : ecdsa param use case

* Update CHANGELOG.md

* changelogs, deps version update

* Update wallet.js

* Update package.json
  • Loading branch information
surinder83singh authored Apr 27, 2024
1 parent 216a0a4 commit 944e212
Show file tree
Hide file tree
Showing 18 changed files with 459 additions and 2,062 deletions.
80 changes: 40 additions & 40 deletions Cargo.lock

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

22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,17 @@ workflow-perf-monitor = "0.0.2"
nw-sys = "0.1.6"

# workflow dependencies
workflow-core = { version = "0.12.0" }
workflow-d3 = { version = "0.12.0" }
workflow-dom = { version = "0.12.0" }
workflow-http = { version = "0.12.0" }
workflow-log = { version = "0.12.0" }
workflow-node = { version = "0.12.0" }
workflow-nw = { version = "0.12.0" }
workflow-rpc = { version = "0.12.0" }
workflow-store = { version = "0.12.0" }
workflow-terminal = { version = "0.12.0" }
workflow-wasm = { version = "0.12.0" }
workflow-core = { version = "0.12.1" }
workflow-d3 = { version = "0.12.1" }
workflow-dom = { version = "0.12.1" }
workflow-http = { version = "0.12.1" }
workflow-log = { version = "0.12.1" }
workflow-node = { version = "0.12.1" }
workflow-nw = { version = "0.12.1" }
workflow-rpc = { version = "0.12.1" }
workflow-store = { version = "0.12.1" }
workflow-terminal = { version = "0.12.1" }
workflow-wasm = { version = "0.12.1" }

# if below is enabled, this means that there is an ongoing work
# on the workflow-rs crate. This requires that you clone workflow-rs
Expand Down
19 changes: 17 additions & 2 deletions wallet/core/src/account/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,25 @@ use crate::imports::*;
use fixedstr::*;
use std::hash::Hash;
use std::str::FromStr;
use workflow_wasm::convert::CastFromJs;

/// @category Wallet SDK
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Hash)]
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Hash, CastFromJs)]
#[wasm_bindgen]
pub struct AccountKind(str64);

#[wasm_bindgen]
impl AccountKind {
#[wasm_bindgen(constructor)]
pub fn ctor(kind: &str) -> Result<AccountKind> {
Self::from_str(kind)
}
#[wasm_bindgen(js_name=toString)]
pub fn js_to_string(&self) -> String {
self.0.as_str().to_string()
}
}

impl AccountKind {
pub fn as_str(&self) -> &str {
self.0.as_str()
Expand Down Expand Up @@ -62,7 +75,9 @@ impl FromStr for AccountKind {
impl TryFrom<JsValue> for AccountKind {
type Error = Error;
fn try_from(kind: JsValue) -> Result<Self> {
if let Some(kind) = kind.as_string() {
if let Ok(kind_ref) = Self::try_ref_from_js_value(&kind) {
Ok(*kind_ref)
} else if let Some(kind) = kind.as_string() {
Ok(AccountKind::from_str(kind.as_str())?)
} else {
Err(Error::InvalidAccountKind)
Expand Down
58 changes: 58 additions & 0 deletions wallet/core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use crate::imports::*;
use crate::storage::{Hint, PrvKeyDataInfo, StorageDescriptor, TransactionRecord, WalletDescriptor};
use crate::utxo::context::UtxoContextId;
use transaction::TransactionRecordNotification;

/// Sync state of the kaspad node
#[derive(Clone, Debug, Serialize, BorshSerialize, BorshDeserialize)]
Expand Down Expand Up @@ -227,6 +228,23 @@ pub enum Events {
},
}

impl Events {
pub fn kind(&self) -> String {
EventKind::from(self).to_string()
}

pub fn to_js_value(&self) -> wasm_bindgen::JsValue {
match self {
Events::Pending { record }
| Events::Reorg { record }
| Events::Stasis { record }
| Events::Maturity { record }
| Events::Discovery { record } => TransactionRecordNotification::new(self.kind(), record.clone()).into(),
_ => serde_wasm_bindgen::to_value(self).unwrap(),
}
}
}

#[derive(Clone, Copy, Debug, Serialize, Eq, PartialEq, Hash)]
#[serde(rename_all = "kebab-case")]
pub enum EventKind {
Expand Down Expand Up @@ -348,3 +366,43 @@ impl TryFrom<JsValue> for EventKind {
EventKind::from_str(&s)
}
}

impl std::fmt::Display for EventKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
let str = match self {
EventKind::All => "all",
EventKind::WalletStart => "wallet-start",
EventKind::Connect => "connect",
EventKind::Disconnect => "disconnect",
EventKind::UtxoIndexNotEnabled => "utxo-index-not-enabled",
EventKind::SyncState => "sync-state",
EventKind::WalletHint => "wallet-hint",
EventKind::WalletOpen => "wallet-open",
EventKind::WalletCreate => "wallet-create",
EventKind::WalletReload => "wallet-reload",
EventKind::WalletError => "wallet-error",
EventKind::WalletClose => "wallet-close",
EventKind::PrvKeyDataCreate => "prv-key-data-create",
EventKind::AccountActivation => "account-activation",
EventKind::AccountDeactivation => "account-deactivation",
EventKind::AccountSelection => "account-selection",
EventKind::AccountCreate => "account-create",
EventKind::AccountUpdate => "account-update",
EventKind::ServerStatus => "server-status",
EventKind::UtxoProcStart => "utxo-proc-start",
EventKind::UtxoProcStop => "utxo-proc-stop",
EventKind::UtxoProcError => "utxo-proc-error",
EventKind::DaaScoreChange => "daa-score-change",
EventKind::Pending => "pending",
EventKind::Reorg => "reorg",
EventKind::Stasis => "stasis",
EventKind::Maturity => "maturity",
EventKind::Discovery => "discovery",
EventKind::Balance => "balance",
EventKind::Metrics => "metrics",
EventKind::Error => "error",
};

write!(f, "{str}")
}
}
Loading

0 comments on commit 944e212

Please sign in to comment.