saito-wasm 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +30 -0
- package/dist/browser/index.d.ts +428 -0
- package/dist/browser/index.js +220 -0
- package/dist/server/113b3ee1c6c31ca8f96e.module.wasm +0 -0
- package/dist/server/index.d.ts +300 -0
- package/dist/server/index.js +283 -0
- package/dist/server/pkg_node_index_js.index.js +55 -0
- package/dist/server/vendors-node_modules_text-encoding_index_js.index.js +44 -0
- package/dist/types/dist/browser/index.d.ts +3 -0
- package/dist/types/dist/browser/index.d.ts.map +1 -0
- package/dist/types/dist/server/index.d.ts +3 -0
- package/dist/types/dist/server/index.d.ts.map +1 -0
- package/dist/types/dist/server/pkg_node_index_js.index.d.ts +9 -0
- package/dist/types/dist/server/pkg_node_index_js.index.d.ts.map +1 -0
- package/dist/types/dist/server/vendors-node_modules_text-encoding_index_js.index.d.ts +8 -0
- package/dist/types/dist/server/vendors-node_modules_text-encoding_index_js.index.d.ts.map +1 -0
- package/dist/types/index.node.d.ts +34 -0
- package/dist/types/index.node.d.ts.map +1 -0
- package/dist/types/index.web.d.ts +3 -0
- package/dist/types/index.web.d.ts.map +1 -0
- package/dist/types/js/msg_handler.d.ts +17 -0
- package/dist/types/js/msg_handler.d.ts.map +1 -0
- package/dist/types/pkg/node/index.d.ts +2 -0
- package/dist/types/pkg/node/index.d.ts.map +1 -0
- package/dist/types/pkg/node/index_bg.d.ts +476 -0
- package/dist/types/pkg/node/index_bg.d.ts.map +1 -0
- package/dist/types/pkg/node/snippets/saito-wasm-10de7e358f6ef4db/js/msg_handler.d.ts +17 -0
- package/dist/types/pkg/node/snippets/saito-wasm-10de7e358f6ef4db/js/msg_handler.d.ts.map +1 -0
- package/dist/types/pkg/web/index.d.ts +406 -0
- package/dist/types/pkg/web/index.d.ts.map +1 -0
- package/dist/types/pkg/web/snippets/saito-wasm-10de7e358f6ef4db/js/msg_handler.d.ts +17 -0
- package/dist/types/pkg/web/snippets/saito-wasm-10de7e358f6ef4db/js/msg_handler.d.ts.map +1 -0
- package/dist/types/webpack.config.d.ts +14 -0
- package/dist/types/webpack.config.d.ts.map +1 -0
- package/index.node.ts +5 -0
- package/index.web.ts +8 -0
- package/js/msg_handler.js +60 -0
- package/package.json +39 -0
- package/src/lib.rs +18 -0
- package/src/saitowasm.rs +718 -0
- package/src/wasm_block.rs +97 -0
- package/src/wasm_configuration.rs +94 -0
- package/src/wasm_io_handler.rs +258 -0
- package/src/wasm_peer.rs +80 -0
- package/src/wasm_slip.rs +101 -0
- package/src/wasm_task_runner.rs +12 -0
- package/src/wasm_time_keeper.rs +12 -0
- package/src/wasm_transaction.rs +177 -0
- package/tsconfig.json +116 -0
- package/webpack.config.js +171 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
use js_sys::{Array, JsString, Uint8Array};
|
|
2
|
+
use num_traits::FromPrimitive;
|
|
3
|
+
use saito_core::common::defs::Timestamp;
|
|
4
|
+
use wasm_bindgen::prelude::wasm_bindgen;
|
|
5
|
+
use wasm_bindgen::JsValue;
|
|
6
|
+
|
|
7
|
+
use saito_core::core::data::block::{Block, BlockType};
|
|
8
|
+
|
|
9
|
+
use crate::saitowasm::string_to_key;
|
|
10
|
+
use crate::wasm_transaction::WasmTransaction;
|
|
11
|
+
|
|
12
|
+
#[wasm_bindgen]
|
|
13
|
+
pub struct WasmBlock {
|
|
14
|
+
block: Block,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
#[wasm_bindgen]
|
|
18
|
+
impl WasmBlock {
|
|
19
|
+
#[wasm_bindgen(constructor)]
|
|
20
|
+
pub fn new() -> WasmBlock {
|
|
21
|
+
WasmBlock {
|
|
22
|
+
block: Block::new(),
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
#[wasm_bindgen(getter = transactions)]
|
|
26
|
+
pub fn get_transactions(&self) -> Array {
|
|
27
|
+
let mut txs: Vec<WasmTransaction> = self
|
|
28
|
+
.block
|
|
29
|
+
.transactions
|
|
30
|
+
.iter()
|
|
31
|
+
.map(|tx| WasmTransaction::from_transaction(tx.clone()))
|
|
32
|
+
.collect();
|
|
33
|
+
let array = js_sys::Array::new_with_length(txs.len() as u32);
|
|
34
|
+
for (i, tx) in txs.drain(..).enumerate() {
|
|
35
|
+
array.set(i as u32, JsValue::from(tx));
|
|
36
|
+
}
|
|
37
|
+
array
|
|
38
|
+
}
|
|
39
|
+
#[wasm_bindgen(getter = id)]
|
|
40
|
+
pub fn get_id(&self) -> u64 {
|
|
41
|
+
self.block.id
|
|
42
|
+
}
|
|
43
|
+
#[wasm_bindgen(setter = id)]
|
|
44
|
+
pub fn set_id(&mut self, id: u64) {
|
|
45
|
+
self.block.id = id;
|
|
46
|
+
}
|
|
47
|
+
#[wasm_bindgen(getter = timestamp)]
|
|
48
|
+
pub fn get_timestamp(&self) -> Timestamp {
|
|
49
|
+
self.block.timestamp
|
|
50
|
+
}
|
|
51
|
+
#[wasm_bindgen(setter = timestamp)]
|
|
52
|
+
pub fn set_timestamp(&mut self, timestamp: Timestamp) {
|
|
53
|
+
self.block.timestamp = timestamp;
|
|
54
|
+
}
|
|
55
|
+
#[wasm_bindgen(getter = previous_block_hash)]
|
|
56
|
+
pub fn get_previous_block_hash(&self) -> JsString {
|
|
57
|
+
hex::encode(self.block.previous_block_hash).into()
|
|
58
|
+
}
|
|
59
|
+
#[wasm_bindgen(setter = previous_block_hash)]
|
|
60
|
+
pub fn set_previous_block_hash(&mut self, hash: JsString) {
|
|
61
|
+
self.block.previous_block_hash = string_to_key(hash).unwrap();
|
|
62
|
+
}
|
|
63
|
+
#[wasm_bindgen(setter = creator)]
|
|
64
|
+
pub fn set_creator(&mut self, key: JsString) {
|
|
65
|
+
self.block.creator = string_to_key(key).unwrap();
|
|
66
|
+
}
|
|
67
|
+
#[wasm_bindgen(getter = creator)]
|
|
68
|
+
pub fn get_creator(&self) -> JsString {
|
|
69
|
+
hex::encode(self.block.creator).into()
|
|
70
|
+
}
|
|
71
|
+
#[wasm_bindgen(getter = type)]
|
|
72
|
+
pub fn get_type(&self) -> u8 {
|
|
73
|
+
self.block.block_type as u8
|
|
74
|
+
}
|
|
75
|
+
#[wasm_bindgen(setter = type)]
|
|
76
|
+
pub fn set_type(&mut self, t: u8) {
|
|
77
|
+
self.block.block_type = BlockType::from_u8(t).unwrap();
|
|
78
|
+
}
|
|
79
|
+
#[wasm_bindgen(getter = hash)]
|
|
80
|
+
pub fn get_hash(&self) -> JsString {
|
|
81
|
+
hex::encode(self.block.hash).into()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#[wasm_bindgen]
|
|
85
|
+
pub fn serialize(&self) -> Uint8Array {
|
|
86
|
+
let buffer = self.block.serialize_for_net(BlockType::Full);
|
|
87
|
+
let buf = Uint8Array::new_with_length(buffer.len() as u32);
|
|
88
|
+
buf.copy_from(buffer.as_slice());
|
|
89
|
+
buf
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
impl WasmBlock {
|
|
94
|
+
pub fn from_block(block: Block) -> WasmBlock {
|
|
95
|
+
WasmBlock { block }
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
use std::io::{Error, ErrorKind};
|
|
2
|
+
|
|
3
|
+
use figment::providers::{Format, Json};
|
|
4
|
+
use figment::Figment;
|
|
5
|
+
use log::{error, info};
|
|
6
|
+
use serde::Deserialize;
|
|
7
|
+
|
|
8
|
+
use saito_core::core::data::configuration::{Configuration, Endpoint, PeerConfig, Server};
|
|
9
|
+
|
|
10
|
+
// #[wasm_bindgen]
|
|
11
|
+
#[derive(Deserialize, Debug)]
|
|
12
|
+
pub struct WasmConfiguration {
|
|
13
|
+
server: Option<Server>,
|
|
14
|
+
peers: Vec<PeerConfig>,
|
|
15
|
+
#[serde(skip)]
|
|
16
|
+
spv_mode: bool,
|
|
17
|
+
#[serde(skip)]
|
|
18
|
+
browser_mode: bool,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// #[wasm_bindgen]
|
|
22
|
+
impl WasmConfiguration {
|
|
23
|
+
pub fn new() -> WasmConfiguration {
|
|
24
|
+
WasmConfiguration {
|
|
25
|
+
server: Option::Some(Server {
|
|
26
|
+
host: "127.0.0.1".to_string(),
|
|
27
|
+
port: 12100,
|
|
28
|
+
protocol: "http".to_string(),
|
|
29
|
+
endpoint: Endpoint {
|
|
30
|
+
host: "127.0.0.1".to_string(),
|
|
31
|
+
port: 12101,
|
|
32
|
+
protocol: "http".to_string(),
|
|
33
|
+
},
|
|
34
|
+
verification_threads: 2,
|
|
35
|
+
channel_size: 1000,
|
|
36
|
+
stat_timer_in_ms: 10000,
|
|
37
|
+
reconnection_wait_time: 10000,
|
|
38
|
+
thread_sleep_time_in_ms: 10,
|
|
39
|
+
block_fetch_batch_size: 0,
|
|
40
|
+
}),
|
|
41
|
+
peers: vec![],
|
|
42
|
+
spv_mode: true,
|
|
43
|
+
browser_mode: true,
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
pub fn new_from_json(json: &str) -> Result<WasmConfiguration, std::io::Error> {
|
|
47
|
+
// info!("new from json : {:?}", json);
|
|
48
|
+
let configs = Figment::new()
|
|
49
|
+
.merge(Json::string(json))
|
|
50
|
+
.extract::<WasmConfiguration>();
|
|
51
|
+
if configs.is_err() {
|
|
52
|
+
error!(
|
|
53
|
+
"failed parsing json string to configs. {:?}",
|
|
54
|
+
configs.err().unwrap()
|
|
55
|
+
);
|
|
56
|
+
return Err(Error::from(ErrorKind::InvalidInput));
|
|
57
|
+
}
|
|
58
|
+
let configs = configs.unwrap();
|
|
59
|
+
Ok(configs)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
impl Configuration for WasmConfiguration {
|
|
64
|
+
fn get_server_configs(&self) -> Option<&Server> {
|
|
65
|
+
return self.server.as_ref();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
fn get_peer_configs(&self) -> &Vec<PeerConfig> {
|
|
69
|
+
return &self.peers;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
fn get_block_fetch_url(&self) -> String {
|
|
73
|
+
let endpoint = &self.get_server_configs().unwrap().endpoint;
|
|
74
|
+
endpoint.protocol.to_string()
|
|
75
|
+
+ "://"
|
|
76
|
+
+ endpoint.host.as_str()
|
|
77
|
+
+ ":"
|
|
78
|
+
+ endpoint.port.to_string().as_str()
|
|
79
|
+
+ "/block/"
|
|
80
|
+
}
|
|
81
|
+
fn is_spv_mode(&self) -> bool {
|
|
82
|
+
self.spv_mode
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
fn is_browser(&self) -> bool {
|
|
86
|
+
self.browser_mode
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
fn replace(&mut self, config: &dyn Configuration) {
|
|
90
|
+
self.server = config.get_server_configs().cloned();
|
|
91
|
+
self.peers = config.get_peer_configs().clone();
|
|
92
|
+
self.spv_mode = config.is_spv_mode();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
use std::fmt::{Debug, Formatter};
|
|
2
|
+
use std::io::{Error, ErrorKind};
|
|
3
|
+
|
|
4
|
+
use async_trait::async_trait;
|
|
5
|
+
use js_sys::{Array, BigInt, Boolean, Uint8Array};
|
|
6
|
+
use log::{info, trace};
|
|
7
|
+
use wasm_bindgen::prelude::wasm_bindgen;
|
|
8
|
+
use wasm_bindgen::JsValue;
|
|
9
|
+
|
|
10
|
+
use saito_core::common::defs::{PeerIndex, SaitoHash};
|
|
11
|
+
use saito_core::common::interface_io::{InterfaceEvent, InterfaceIO};
|
|
12
|
+
use saito_core::core::data::configuration::PeerConfig;
|
|
13
|
+
use saito_core::core::data::transaction::Transaction;
|
|
14
|
+
|
|
15
|
+
pub struct WasmIoHandler {}
|
|
16
|
+
|
|
17
|
+
#[async_trait]
|
|
18
|
+
impl InterfaceIO for WasmIoHandler {
|
|
19
|
+
async fn send_message(&self, peer_index: u64, buffer: Vec<u8>) -> Result<(), Error> {
|
|
20
|
+
info!("WasmIoHandler::send_message : {:?}", peer_index);
|
|
21
|
+
|
|
22
|
+
let array = js_sys::Uint8Array::new_with_length(buffer.len() as u32);
|
|
23
|
+
array.copy_from(buffer.as_slice());
|
|
24
|
+
|
|
25
|
+
// let async_fn =
|
|
26
|
+
MsgHandler::send_message(js_sys::BigInt::from(peer_index), &array);
|
|
27
|
+
// let promise = js_sys::Promise::resolve(async_fn);
|
|
28
|
+
// let result = wasm_bindgen_futures::JsFuture::from(async_fn).await;
|
|
29
|
+
drop(array);
|
|
30
|
+
|
|
31
|
+
Ok(())
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async fn send_message_to_all(
|
|
35
|
+
&self,
|
|
36
|
+
buffer: Vec<u8>,
|
|
37
|
+
peer_exceptions: Vec<u64>,
|
|
38
|
+
) -> Result<(), Error> {
|
|
39
|
+
let array = js_sys::Uint8Array::new_with_length(buffer.len() as u32);
|
|
40
|
+
array.copy_from(buffer.as_slice());
|
|
41
|
+
|
|
42
|
+
let arr2 = js_sys::Array::new_with_length(peer_exceptions.len() as u32);
|
|
43
|
+
|
|
44
|
+
for (i, ex) in peer_exceptions.iter().enumerate() {
|
|
45
|
+
let int = js_sys::BigInt::from(*ex);
|
|
46
|
+
let int = JsValue::from(int);
|
|
47
|
+
|
|
48
|
+
arr2.set(i as u32, int);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
MsgHandler::send_message_to_all(&array, &arr2);
|
|
52
|
+
|
|
53
|
+
drop(array);
|
|
54
|
+
drop(arr2);
|
|
55
|
+
|
|
56
|
+
Ok(())
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async fn connect_to_peer(&mut self, peer: PeerConfig) -> Result<(), Error> {
|
|
60
|
+
trace!("connect_to_peer : {:?}", peer.host);
|
|
61
|
+
|
|
62
|
+
let json_string = serde_json::to_string(&peer).unwrap();
|
|
63
|
+
let json = js_sys::JSON::parse(&json_string).unwrap();
|
|
64
|
+
MsgHandler::connect_to_peer(json);
|
|
65
|
+
|
|
66
|
+
Ok(())
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// async fn process_interface_event(&mut self, event: InterfaceEvent) -> Result<(), Error> {
|
|
70
|
+
// todo!()
|
|
71
|
+
// }
|
|
72
|
+
|
|
73
|
+
async fn disconnect_from_peer(&mut self, peer_index: u64) -> Result<(), Error> {
|
|
74
|
+
MsgHandler::disconnect_from_peer(js_sys::BigInt::from(peer_index));
|
|
75
|
+
Ok(())
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// fn set_write_result(
|
|
79
|
+
// &mut self,
|
|
80
|
+
// result_key: String,
|
|
81
|
+
// result: Result<String, Error>,
|
|
82
|
+
// ) -> Result<(), Error> {
|
|
83
|
+
// todo!()
|
|
84
|
+
// }
|
|
85
|
+
|
|
86
|
+
async fn fetch_block_from_peer(
|
|
87
|
+
&self,
|
|
88
|
+
block_hash: SaitoHash,
|
|
89
|
+
peer_index: u64,
|
|
90
|
+
url: String,
|
|
91
|
+
) -> Result<(), Error> {
|
|
92
|
+
let hash = js_sys::Uint8Array::new_with_length(32);
|
|
93
|
+
hash.copy_from(block_hash.as_slice());
|
|
94
|
+
MsgHandler::fetch_block_from_peer(&hash, BigInt::from(peer_index), url);
|
|
95
|
+
|
|
96
|
+
drop(hash);
|
|
97
|
+
|
|
98
|
+
Ok(())
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async fn write_value(&mut self, key: String, value: Vec<u8>) -> Result<(), Error> {
|
|
102
|
+
let array = js_sys::Uint8Array::new_with_length(value.len() as u32);
|
|
103
|
+
array.copy_from(value.as_slice());
|
|
104
|
+
|
|
105
|
+
MsgHandler::write_value(key, &array);
|
|
106
|
+
drop(array);
|
|
107
|
+
|
|
108
|
+
Ok(())
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async fn read_value(&self, key: String) -> Result<Vec<u8>, Error> {
|
|
112
|
+
let result = MsgHandler::read_value(key);
|
|
113
|
+
if result.is_err() {
|
|
114
|
+
return Err(Error::from(ErrorKind::Other));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let result = result.unwrap();
|
|
118
|
+
let v = result.to_vec();
|
|
119
|
+
drop(result);
|
|
120
|
+
Ok(v)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async fn load_block_file_list(&self) -> Result<Vec<String>, Error> {
|
|
124
|
+
let result = MsgHandler::load_block_file_list();
|
|
125
|
+
if result.is_err() {
|
|
126
|
+
return Err(Error::from(ErrorKind::Other));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let result = result.unwrap();
|
|
130
|
+
let result = Array::try_from(result);
|
|
131
|
+
if result.is_err() {
|
|
132
|
+
return Err(Error::from(ErrorKind::Other));
|
|
133
|
+
}
|
|
134
|
+
let result = result.unwrap();
|
|
135
|
+
|
|
136
|
+
let mut v = vec![];
|
|
137
|
+
for i in 0..result.length() {
|
|
138
|
+
let res = result.get(i);
|
|
139
|
+
let res = js_sys::JsString::from(res).as_string().unwrap();
|
|
140
|
+
v.push(res);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
Ok(v)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async fn is_existing_file(&self, key: String) -> bool {
|
|
147
|
+
let result = MsgHandler::is_existing_file(key);
|
|
148
|
+
if result.is_err() {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
let result = result.unwrap();
|
|
153
|
+
result.into()
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async fn remove_value(&self, key: String) -> Result<(), Error> {
|
|
157
|
+
MsgHandler::remove_value(key);
|
|
158
|
+
|
|
159
|
+
Ok(())
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
fn get_block_dir(&self) -> String {
|
|
163
|
+
"data/blocks/".to_string()
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async fn process_api_call(&self, buffer: Vec<u8>, msg_index: u32, peer_index: PeerIndex) {
|
|
167
|
+
let buf = Uint8Array::new_with_length(buffer.len() as u32);
|
|
168
|
+
buf.copy_from(buffer.as_slice());
|
|
169
|
+
MsgHandler::process_api_call(buf, msg_index, peer_index);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async fn process_api_success(&self, buffer: Vec<u8>, msg_index: u32, peer_index: PeerIndex) {
|
|
173
|
+
// let tx = Transaction::deserialize_from_net(&buffer);
|
|
174
|
+
// let buffer = tx.data;
|
|
175
|
+
let buf = Uint8Array::new_with_length(buffer.len() as u32);
|
|
176
|
+
buf.copy_from(buffer.as_slice());
|
|
177
|
+
MsgHandler::process_api_success(buf, msg_index, peer_index);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async fn process_api_error(&self, buffer: Vec<u8>, msg_index: u32, peer_index: PeerIndex) {
|
|
181
|
+
// let tx = Transaction::deserialize_from_net(&buffer);
|
|
182
|
+
// let buffer = tx.data;
|
|
183
|
+
|
|
184
|
+
let buf = Uint8Array::new_with_length(buffer.len() as u32);
|
|
185
|
+
buf.copy_from(buffer.as_slice());
|
|
186
|
+
MsgHandler::process_api_error(buf, msg_index, peer_index);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
fn send_interface_event(&self, event: InterfaceEvent) {
|
|
190
|
+
match event {
|
|
191
|
+
InterfaceEvent::PeerHandshakeComplete(index) => {
|
|
192
|
+
MsgHandler::send_interface_event("handshake_complete".to_string(), index);
|
|
193
|
+
}
|
|
194
|
+
InterfaceEvent::PeerConnectionDropped(index) => {
|
|
195
|
+
MsgHandler::send_interface_event("peer_disconnect".to_string(), index);
|
|
196
|
+
}
|
|
197
|
+
InterfaceEvent::PeerConnected(index) => {
|
|
198
|
+
MsgHandler::send_interface_event("peer_connect".to_string(), index);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
impl Debug for WasmIoHandler {
|
|
205
|
+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
206
|
+
f.debug_struct("RustIoHandler")
|
|
207
|
+
// .field("handler_id", &self.handler_id)
|
|
208
|
+
.finish()
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
#[wasm_bindgen(module = "/js/msg_handler.js")]
|
|
213
|
+
extern "C" {
|
|
214
|
+
type MsgHandler;
|
|
215
|
+
|
|
216
|
+
#[wasm_bindgen(static_method_of = MsgHandler)]
|
|
217
|
+
pub fn send_message(peer_index: BigInt, buffer: &Uint8Array);
|
|
218
|
+
|
|
219
|
+
#[wasm_bindgen(static_method_of = MsgHandler)]
|
|
220
|
+
pub fn send_message_to_all(buffer: &Uint8Array, exceptions: &Array);
|
|
221
|
+
|
|
222
|
+
#[wasm_bindgen(static_method_of = MsgHandler, catch)]
|
|
223
|
+
pub fn connect_to_peer(peer_data: JsValue) -> Result<JsValue, js_sys::Error>;
|
|
224
|
+
#[wasm_bindgen(static_method_of = MsgHandler)]
|
|
225
|
+
pub fn write_value(key: String, value: &Uint8Array);
|
|
226
|
+
|
|
227
|
+
#[wasm_bindgen(static_method_of = MsgHandler, catch)]
|
|
228
|
+
pub fn read_value(key: String) -> Result<Uint8Array, js_sys::Error>;
|
|
229
|
+
|
|
230
|
+
#[wasm_bindgen(static_method_of = MsgHandler, catch)]
|
|
231
|
+
pub fn load_block_file_list() -> Result<Array, js_sys::Error>;
|
|
232
|
+
#[wasm_bindgen(static_method_of = MsgHandler, catch)]
|
|
233
|
+
pub fn is_existing_file(key: String) -> Result<Boolean, js_sys::Error>;
|
|
234
|
+
#[wasm_bindgen(static_method_of = MsgHandler, catch)]
|
|
235
|
+
pub fn remove_value(key: String) -> Result<JsValue, JsValue>;
|
|
236
|
+
|
|
237
|
+
#[wasm_bindgen(static_method_of = MsgHandler, catch)]
|
|
238
|
+
pub fn disconnect_from_peer(peer_index: BigInt) -> Result<JsValue, js_sys::Error>;
|
|
239
|
+
|
|
240
|
+
#[wasm_bindgen(static_method_of = MsgHandler, catch)]
|
|
241
|
+
pub fn fetch_block_from_peer(
|
|
242
|
+
hash: &Uint8Array,
|
|
243
|
+
peer_index: BigInt,
|
|
244
|
+
url: String,
|
|
245
|
+
) -> Result<JsValue, js_sys::Error>;
|
|
246
|
+
|
|
247
|
+
#[wasm_bindgen(static_method_of = MsgHandler)]
|
|
248
|
+
pub fn process_api_call(buffer: Uint8Array, msg_index: u32, peer_index: u64);
|
|
249
|
+
|
|
250
|
+
#[wasm_bindgen(static_method_of = MsgHandler)]
|
|
251
|
+
pub fn process_api_success(buffer: Uint8Array, msg_index: u32, peer_index: u64);
|
|
252
|
+
|
|
253
|
+
#[wasm_bindgen(static_method_of = MsgHandler)]
|
|
254
|
+
pub fn process_api_error(buffer: Uint8Array, msg_index: u32, peer_index: u64);
|
|
255
|
+
|
|
256
|
+
#[wasm_bindgen(static_method_of = MsgHandler)]
|
|
257
|
+
pub fn send_interface_event(event: String, peer_index: u64);
|
|
258
|
+
}
|
package/src/wasm_peer.rs
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
use js_sys::{Array, JsString};
|
|
2
|
+
use log::warn;
|
|
3
|
+
use wasm_bindgen::prelude::wasm_bindgen;
|
|
4
|
+
use wasm_bindgen::JsValue;
|
|
5
|
+
|
|
6
|
+
use saito_core::common::defs::PeerIndex;
|
|
7
|
+
use saito_core::core::data::peer::Peer;
|
|
8
|
+
|
|
9
|
+
#[wasm_bindgen]
|
|
10
|
+
#[derive(Clone)]
|
|
11
|
+
pub struct WasmPeer {
|
|
12
|
+
peer: Peer,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
#[wasm_bindgen]
|
|
16
|
+
impl WasmPeer {
|
|
17
|
+
#[wasm_bindgen(getter = public_key)]
|
|
18
|
+
pub fn get_public_key(&self) -> JsString {
|
|
19
|
+
if self.peer.public_key.is_none() {
|
|
20
|
+
warn!("peer : {:?} public key is not set", self.peer.index);
|
|
21
|
+
}
|
|
22
|
+
hex::encode(self.peer.public_key.unwrap()).into()
|
|
23
|
+
}
|
|
24
|
+
#[wasm_bindgen(getter = key_list)]
|
|
25
|
+
pub fn get_key_list(&self) -> Array {
|
|
26
|
+
let array = Array::new_with_length(self.peer.key_list.len() as u32);
|
|
27
|
+
for (i, key) in self.peer.key_list.iter().enumerate() {
|
|
28
|
+
array.set(i as u32, JsValue::from(hex::encode(key)));
|
|
29
|
+
}
|
|
30
|
+
array
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#[wasm_bindgen(getter = peer_index)]
|
|
34
|
+
pub fn get_peer_index(&self) -> u64 {
|
|
35
|
+
self.peer.index
|
|
36
|
+
}
|
|
37
|
+
#[wasm_bindgen(constructor)]
|
|
38
|
+
pub fn new(peer_index: PeerIndex) -> WasmPeer {
|
|
39
|
+
WasmPeer {
|
|
40
|
+
peer: Peer::new(peer_index),
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
#[wasm_bindgen(getter = sync_type)]
|
|
44
|
+
pub fn get_sync_type(&self) -> JsString {
|
|
45
|
+
if self.peer.block_fetch_url.is_empty() {
|
|
46
|
+
return "lite".into();
|
|
47
|
+
}
|
|
48
|
+
return "full".into();
|
|
49
|
+
}
|
|
50
|
+
#[wasm_bindgen(getter = services)]
|
|
51
|
+
pub fn get_services(&self) -> JsValue {
|
|
52
|
+
let arr = js_sys::Array::new_with_length(self.peer.services.len() as u32);
|
|
53
|
+
for (i, service) in self.peer.services.iter().enumerate() {
|
|
54
|
+
arr.set(i as u32, JsValue::from(JsString::from(service.as_str())));
|
|
55
|
+
}
|
|
56
|
+
JsValue::from(arr)
|
|
57
|
+
}
|
|
58
|
+
#[wasm_bindgen(setter = services)]
|
|
59
|
+
pub fn set_services(&mut self, services: JsValue) {
|
|
60
|
+
let services = js_sys::Array::from(&services);
|
|
61
|
+
let mut ser = vec![];
|
|
62
|
+
for i in 0..services.length() {
|
|
63
|
+
let str = JsString::from(services.at(i as i32));
|
|
64
|
+
ser.push(str.into());
|
|
65
|
+
}
|
|
66
|
+
self.peer.services = ser;
|
|
67
|
+
}
|
|
68
|
+
pub fn has_service(&self, service: JsString) -> bool {
|
|
69
|
+
return self.peer.has_service(service.into());
|
|
70
|
+
}
|
|
71
|
+
pub fn is_main_peer(&self) -> bool {
|
|
72
|
+
self.peer.is_main_peer()
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
impl WasmPeer {
|
|
77
|
+
pub fn new_from_peer(peer: Peer) -> WasmPeer {
|
|
78
|
+
WasmPeer { peer }
|
|
79
|
+
}
|
|
80
|
+
}
|
package/src/wasm_slip.rs
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
use js_sys::JsString;
|
|
2
|
+
use wasm_bindgen::prelude::wasm_bindgen;
|
|
3
|
+
|
|
4
|
+
use num_traits::FromPrimitive;
|
|
5
|
+
use saito_core::common::defs::{Currency, SaitoUTXOSetKey};
|
|
6
|
+
use saito_core::core::data::slip::{Slip, SlipType};
|
|
7
|
+
|
|
8
|
+
use crate::saitowasm::string_to_key;
|
|
9
|
+
|
|
10
|
+
// #[derive(Serialize, Deserialize)]
|
|
11
|
+
#[wasm_bindgen]
|
|
12
|
+
pub struct WasmSlip {
|
|
13
|
+
pub(crate) slip: Slip,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
#[wasm_bindgen]
|
|
17
|
+
impl WasmSlip {
|
|
18
|
+
#[wasm_bindgen(getter=amount)]
|
|
19
|
+
pub fn amount(&self) -> u64 {
|
|
20
|
+
self.slip.amount
|
|
21
|
+
}
|
|
22
|
+
#[wasm_bindgen(setter=amount)]
|
|
23
|
+
pub fn set_amount(&mut self, amount: Currency) {
|
|
24
|
+
self.slip.amount = amount;
|
|
25
|
+
}
|
|
26
|
+
#[wasm_bindgen(getter=slip_type)]
|
|
27
|
+
pub fn slip_type(&self) -> u8 {
|
|
28
|
+
self.slip.slip_type as u8
|
|
29
|
+
}
|
|
30
|
+
#[wasm_bindgen(setter=slip_type)]
|
|
31
|
+
pub fn set_slip_type(&mut self, slip_type: u8) {
|
|
32
|
+
self.slip.slip_type = SlipType::from_u8(slip_type).expect("value is not in slip types");
|
|
33
|
+
}
|
|
34
|
+
#[wasm_bindgen(getter=public_key)]
|
|
35
|
+
pub fn public_key(&self) -> JsString {
|
|
36
|
+
let key = hex::encode(self.slip.public_key);
|
|
37
|
+
key.into()
|
|
38
|
+
}
|
|
39
|
+
#[wasm_bindgen(setter=public_key)]
|
|
40
|
+
pub fn set_public_key(&mut self, key: JsString) {
|
|
41
|
+
let key = string_to_key(key).unwrap();
|
|
42
|
+
|
|
43
|
+
self.slip.public_key = key;
|
|
44
|
+
}
|
|
45
|
+
#[wasm_bindgen(getter=slip_index)]
|
|
46
|
+
pub fn slip_index(&self) -> u8 {
|
|
47
|
+
self.slip.slip_index
|
|
48
|
+
}
|
|
49
|
+
#[wasm_bindgen(setter=slip_index)]
|
|
50
|
+
pub fn set_slip_index(&mut self, index: u8) {
|
|
51
|
+
self.slip.slip_index = index;
|
|
52
|
+
}
|
|
53
|
+
#[wasm_bindgen(getter=block_id)]
|
|
54
|
+
pub fn block_id(&self) -> u64 {
|
|
55
|
+
self.slip.block_id
|
|
56
|
+
}
|
|
57
|
+
#[wasm_bindgen(setter=block_id)]
|
|
58
|
+
pub fn set_block_id(&mut self, id: u64) {
|
|
59
|
+
self.slip.block_id = id;
|
|
60
|
+
}
|
|
61
|
+
#[wasm_bindgen(getter=tx_ordinal)]
|
|
62
|
+
pub fn tx_ordinal(&self) -> u64 {
|
|
63
|
+
self.slip.tx_ordinal
|
|
64
|
+
}
|
|
65
|
+
#[wasm_bindgen(setter=tx_ordinal)]
|
|
66
|
+
pub fn set_tx_ordinal(&mut self, ordinal: u64) {
|
|
67
|
+
self.slip.tx_ordinal = ordinal;
|
|
68
|
+
}
|
|
69
|
+
#[wasm_bindgen(setter=utxo_key)]
|
|
70
|
+
pub fn set_utxo_key(&mut self, key: JsString) {
|
|
71
|
+
let key: SaitoUTXOSetKey = string_to_key(key).unwrap();
|
|
72
|
+
self.slip.utxoset_key = key;
|
|
73
|
+
}
|
|
74
|
+
#[wasm_bindgen(getter=utxo_key)]
|
|
75
|
+
pub fn utxo_key(&self) -> JsString {
|
|
76
|
+
let key = hex::encode(self.slip.utxoset_key);
|
|
77
|
+
key.into()
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
#[wasm_bindgen(constructor)]
|
|
81
|
+
pub fn new() -> WasmSlip {
|
|
82
|
+
WasmSlip {
|
|
83
|
+
slip: Slip {
|
|
84
|
+
public_key: [0; 33],
|
|
85
|
+
amount: 0,
|
|
86
|
+
slip_index: 0,
|
|
87
|
+
block_id: 0,
|
|
88
|
+
tx_ordinal: 0,
|
|
89
|
+
slip_type: SlipType::Normal,
|
|
90
|
+
utxoset_key: [0; 58],
|
|
91
|
+
is_utxoset_key_set: false,
|
|
92
|
+
},
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
impl WasmSlip {
|
|
98
|
+
pub fn new_from_slip(slip: Slip) -> WasmSlip {
|
|
99
|
+
WasmSlip { slip }
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
use std::pin::Pin;
|
|
2
|
+
|
|
3
|
+
use saito_core::common::run_task::RunTask;
|
|
4
|
+
|
|
5
|
+
pub struct WasmTaskRunner {}
|
|
6
|
+
|
|
7
|
+
impl RunTask for WasmTaskRunner {
|
|
8
|
+
fn run(&self, task: Pin<Box<dyn Fn() -> () + Send + 'static>>) {
|
|
9
|
+
println!("WasmTaskRunner.run");
|
|
10
|
+
task();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
use saito_core::common::defs::Timestamp;
|
|
2
|
+
use saito_core::common::keep_time::KeepTime;
|
|
3
|
+
|
|
4
|
+
pub struct WasmTimeKeeper {}
|
|
5
|
+
|
|
6
|
+
impl KeepTime for WasmTimeKeeper {
|
|
7
|
+
fn get_timestamp_in_ms(&self) -> Timestamp {
|
|
8
|
+
let date = js_sys::Date::new_0();
|
|
9
|
+
|
|
10
|
+
date.get_time() as Timestamp
|
|
11
|
+
}
|
|
12
|
+
}
|