Skip to content

Commit 5695931

Browse files
Merge pull request #248 from klever-io/refactor/kos-web
Refactor/kos-web
2 parents ace9c82 + d2f11f0 commit 5695931

9 files changed

Lines changed: 1813 additions & 1390 deletions

File tree

Cargo.lock

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ homepage = "https://klever.org/"
4747
license = "Apache-2.0"
4848
repository = "https://github.com/klever-io/kos-rs"
4949
rust-version = "1.74.0"
50-
version = "0.2.41"
50+
version = "0.3.0"
5151

5252
[workspace.dependencies]
5353
bech32 = "0.9.1"
@@ -86,9 +86,9 @@ enum_delegate = "0.2"
8686
serde = { version = "1.0", default-features = false }
8787
serde_json = "1.0"
8888
thiserror = "1.0"
89-
kos-mobile = { version = "0.2.41", path = "./packages/kos-mobile", default-features = false }
89+
kos-mobile = { version = "0.3.0", path = "./packages/kos-mobile", default-features = false }
9090
ecies = { version = "0.2.7", default-features = false, features = ["pure"] }
91-
kos = { version = "0.2.41", path = "./packages/kos", default-features = false, features = ["not-ksafe"] }
91+
kos = { version = "0.3.0", path = "./packages/kos", default-features = false, features = ["not-ksafe"] }
9292

9393
# lightning
9494
lwk_common = "0.9.0"

packages/kos-web/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ kos = { workspace = true, features = ["not-ksafe"] }
3030
wasm-bindgen = { workspace = true }
3131
num-bigint = "0.4"
3232
num-traits = "0.2"
33+
bigdecimal = "0.4"
3334
kos-codec = { path = "../kos-codec" }
34-
35+
thiserror = { workspace = true }

packages/kos-web/src/error.rs

Lines changed: 87 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,110 @@
11
use kos::chains::ChainError;
2-
use std::{error, fmt, str};
3-
use wasm_bindgen::JsValue;
4-
5-
/// Crypto error variants
6-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8-
pub enum Error {
9-
// Invalid string
10-
InvalidString(String),
11-
// JSON serialization error
12-
JSONSerde(String),
13-
// UnsupportedChain,
14-
UnsupportedChain(String),
15-
// InvalidMnemonic,
16-
InvalidMnemonic(&'static str),
17-
// InvalidPath,
18-
InvalidPath(&'static str),
19-
// InvalidPrivateKey,
20-
InvalidPrivateKey(&'static str),
21-
// InvalidPublicKey,
22-
InvalidPublicKey(String),
23-
// InvalidAddress,
24-
InvalidAddress(String),
25-
// InvalidChecksum,
26-
InvalidChecksum(&'static str),
27-
/// Invalid secp256k1 signature
28-
InvalidSignature(&'static str),
29-
/// Invalid secp256k1 signature message
30-
InvalidMessage(String),
31-
/// Out of preallocated memory
32-
NotEnoughMemory(String),
33-
/// Invalid Enum Variant
34-
InvalidEnumVariant(String),
35-
/// Invalid Len
36-
InvalidLen(String),
37-
/// InvalidNumberParse
38-
InvalidNumberParse(String),
39-
/// InvalidTransaction
40-
InvalidTransaction(String),
41-
/// WalletManagerError
42-
WalletManager(String),
43-
/// CipherError
44-
Cipher(String),
45-
/// TransportError
46-
Transport(String),
47-
/// DelegateError
48-
Delegate(String),
2+
use std::{error, fmt};
3+
use wasm_bindgen::prelude::*;
4+
5+
// Rust-only error type with associated data
6+
#[derive(Debug, Clone, PartialEq, Eq)]
7+
#[wasm_bindgen]
8+
pub struct KOSError {
9+
#[wasm_bindgen(skip)]
10+
pub message: String,
4911
}
5012

51-
impl fmt::Display for Error {
52-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53-
match self {
54-
Error::InvalidString(e) => write!(f, "Invalid string: {e}"),
55-
Error::JSONSerde(e) => write!(f, "JSON serialization: {e}"),
56-
Error::UnsupportedChain(e) => write!(f, "Unsupported chain: {e}"),
57-
Error::InvalidMnemonic(e) => write!(f, "Invalid mnemonic: {e}"),
58-
Error::InvalidPath(e) => write!(f, "Invalid path: {e}"),
59-
Error::InvalidPrivateKey(e) => write!(f, "Invalid private key: {e}"),
60-
Error::InvalidPublicKey(e) => write!(f, "Invalid public key: {e}"),
61-
Error::InvalidAddress(e) => write!(f, "Invalid address: {e}"),
62-
Error::InvalidChecksum(e) => write!(f, "Invalid checksum: {e}"),
63-
Error::InvalidSignature(e) => write!(f, "Invalid signature: {e}"),
64-
Error::InvalidMessage(e) => write!(f, "Invalid message: {e}"),
65-
Error::NotEnoughMemory(e) => write!(f, "Not enough memory: {e}"),
66-
Error::InvalidEnumVariant(e) => write!(f, "Invalid Enum Variant error: {e}"),
67-
Error::InvalidLen(e) => write!(f, "Invalid Len: {e}"),
68-
Error::InvalidNumberParse(e) => write!(f, "Invalid number parse: {e}"),
69-
Error::InvalidTransaction(e) => write!(f, "Invalid transaction: {e}"),
70-
Error::WalletManager(e) => write!(f, "WalletManager error: {e}"),
71-
Error::Cipher(e) => write!(f, "Cipher error: {e}"),
72-
Error::Transport(e) => write!(f, "Transport error: {e}"),
73-
Error::Delegate(e) => write!(f, "Delegate error: {e}"),
13+
#[wasm_bindgen]
14+
impl KOSError {
15+
#[wasm_bindgen(constructor)]
16+
pub fn new(message: String) -> KOSError {
17+
KOSError { message }
18+
}
19+
20+
#[wasm_bindgen(js_name = getMessage)]
21+
pub fn get_message(&self) -> String {
22+
self.message.clone()
23+
}
24+
}
25+
26+
impl KOSError {
27+
pub fn unsupported_chain(id: String) -> Self {
28+
KOSError {
29+
message: format!("UnsupportedChainError: Unsupported chain {id}"),
30+
}
31+
}
32+
33+
pub fn kos_delegate(msg: String) -> Self {
34+
KOSError {
35+
message: format!("KOSDelegateError: {msg}"),
36+
}
37+
}
38+
39+
pub fn hex_decode(msg: String) -> Self {
40+
KOSError {
41+
message: format!("HexDecodeError: {msg}"),
42+
}
43+
}
44+
45+
pub fn kos_number(msg: String) -> Self {
46+
KOSError {
47+
message: format!("KOSNumberError: {msg}"),
48+
}
49+
}
50+
51+
pub fn invalid_string(msg: String) -> Self {
52+
KOSError {
53+
message: format!("Invalid string: {msg}"),
54+
}
55+
}
56+
57+
pub fn json_serde(msg: String) -> Self {
58+
KOSError {
59+
message: format!("JSON serialization: {msg}"),
60+
}
61+
}
62+
63+
pub fn invalid_mnemonic(msg: String) -> Self {
64+
KOSError {
65+
message: format!("Invalid mnemonic: {msg}"),
66+
}
67+
}
68+
69+
pub fn wallet_manager(msg: String) -> Self {
70+
KOSError {
71+
message: format!("WalletManager error: {msg}"),
72+
}
73+
}
74+
75+
pub fn cipher(msg: String) -> Self {
76+
KOSError {
77+
message: format!("Cipher error: {msg}"),
7478
}
7579
}
7680
}
7781

78-
impl From<serde_json::Error> for Error {
79-
fn from(e: serde_json::Error) -> Self {
80-
Self::JSONSerde(e.to_string())
82+
impl fmt::Display for KOSError {
83+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84+
write!(f, "{}", self.message)
8185
}
8286
}
8387

84-
impl error::Error for Error {
85-
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
86-
None
88+
impl From<serde_json::Error> for KOSError {
89+
fn from(e: serde_json::Error) -> Self {
90+
KOSError::json_serde(e.to_string())
8791
}
8892
}
8993

90-
impl From<Error> for JsValue {
91-
fn from(e: Error) -> Self {
92-
JsValue::from_str(&format!("{e}"))
94+
impl error::Error for KOSError {
95+
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
96+
None
9397
}
9498
}
9599

96-
impl From<hex::FromHexError> for Error {
100+
impl From<hex::FromHexError> for KOSError {
97101
fn from(e: hex::FromHexError) -> Self {
98-
Self::InvalidString(e.to_string())
102+
KOSError::hex_decode(e.to_string())
99103
}
100104
}
101105

102-
impl From<ChainError> for Error {
106+
impl From<ChainError> for KOSError {
103107
fn from(err: ChainError) -> Self {
104-
Error::Delegate(err.to_string())
108+
KOSError::kos_delegate(err.to_string())
105109
}
106110
}

0 commit comments

Comments
 (0)