Skip to content

Commit 2e056ce

Browse files
committed
initial commit
0 parents  commit 2e056ce

File tree

7 files changed

+819
-0
lines changed

7 files changed

+819
-0
lines changed

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
debug/
4+
target/
5+
6+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
8+
# Cargo.lock
9+
10+
# These are backup files generated by rustfmt
11+
**/*.rs.bk
12+
13+
# MSVC Windows builds of rustc generate these, which store debugging information
14+
*.pdb
15+
16+
# LSP configs
17+
.taplo.toml
18+
19+
dist/

Cargo.lock

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

Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "keplr"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["cdylib", "rlib"]
8+
9+
[dependencies]
10+
base64 = "0.22"
11+
wasm-bindgen = "0.2.92"
12+
wasm-bindgen-futures = "0.4.42"
13+
js-sys = "0.3.69"
14+
web-sys = { version = "0.3.69", features = [
15+
"console",
16+
"Window",
17+
"TextDecoder",
18+
] }
19+
async-trait = "0.1.80"
20+
serde = { version = "1.0.203", features = ["derive"] }
21+
serde_json = "1.0.117"
22+
serde-wasm-bindgen = "0.6.5"

src/demo.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
use crate::keplr::{suggest_chain, suggest_chain_types::*, Keplr};
2+
use serde::{Deserialize, Serialize};
3+
use wasm_bindgen::prelude::*;
4+
use web_sys::console;
5+
6+
#[wasm_bindgen]
7+
extern "C" {
8+
#[wasm_bindgen(js_namespace = window, js_name = alert)]
9+
fn alert(s: &str);
10+
}
11+
12+
#[wasm_bindgen(start)]
13+
pub async fn start() -> Result<(), JsValue> {
14+
const CHAIN_ID: &str = "secret-4";
15+
let window = web_sys::window().expect("no global `window` exists");
16+
17+
if js_sys::Reflect::has(&window, &JsValue::from_str("keplr")).unwrap() {
18+
// suggest().await;
19+
// disable(CHAIN_ID);
20+
let keplr = Keplr::new().enable(CHAIN_ID).await?;
21+
keplr.debug();
22+
23+
let key_info = keplr.get_key(CHAIN_ID).await;
24+
console::log_1(&serde_wasm_bindgen::to_value(&key_info)?);
25+
26+
let offline_signer = keplr.get_offline_signer(CHAIN_ID);
27+
28+
let accounts = offline_signer.get_accounts().await;
29+
let accounts = js_sys::Array::from(&accounts);
30+
let account = accounts.get(0);
31+
console::log_1(&account);
32+
33+
// hey it works
34+
35+
// let signer_address = JsString::from_str(&key_info.bech32_address).unwrap();
36+
// let sign_doc = StdSignDoc {
37+
// chain_id: CHAIN_ID.into(),
38+
// account_number: "0".into(),
39+
// sequence: "0".into(),
40+
// fee: StdFee::default(),
41+
// msgs: vec![],
42+
// memo: "".into(),
43+
// };
44+
// let result = offline_signer
45+
// .sign_amino(signer_address, serde_wasm_bindgen::to_value(&sign_doc)?)
46+
// .await;
47+
48+
let enigma_utils = keplr.get_enigma_utils(CHAIN_ID);
49+
50+
let contract_code_hash = "9a00ca4ad505e9be7e6e6dddf8d939b7ec7e9ac8e109c8681f10db9cacb36d42";
51+
52+
#[derive(Serialize)]
53+
pub struct SomeMsg {
54+
pub value: String,
55+
}
56+
57+
let msg = SomeMsg {
58+
value: "hello".to_string(),
59+
};
60+
61+
let message = enigma_utils
62+
.encrypt(contract_code_hash.into(), &msg)
63+
.await?;
64+
65+
let message: Vec<u8> = serde_wasm_bindgen::from_value(message.into())?;
66+
let nonce = &message[0..32];
67+
let ciphertext = &message[64..];
68+
let plaintext = enigma_utils.decrypt(ciphertext, nonce).await?;
69+
let plaintext = plaintext.to_vec();
70+
let plaintext = String::from_utf8_lossy(&plaintext);
71+
console::log_1(&plaintext.to_string().into());
72+
} else {
73+
alert("Please install keplr extension");
74+
}
75+
Ok(())
76+
}
77+
78+
pub async fn suggest() {
79+
let chain_info = SuggestingChainInfo {
80+
chain_id: "mychain-1".to_string(),
81+
chain_name: "my new chain".to_string(),
82+
rpc: "http://123.456.789.012:26657".to_string(),
83+
rest: "http://123.456.789.012:1317".to_string(),
84+
bip44: Bip44 { coin_type: 118 },
85+
bech32_config: Bech32Config {
86+
bech32_prefix_acc_addr: "cosmos".to_string(),
87+
bech32_prefix_acc_pub: "cosmospub".to_string(),
88+
bech32_prefix_val_addr: "cosmosvaloper".to_string(),
89+
bech32_prefix_val_pub: "cosmosvaloperpub".to_string(),
90+
bech32_prefix_cons_addr: "cosmosvalcons".to_string(),
91+
bech32_prefix_cons_pub: "cosmosvalconspub".to_string(),
92+
},
93+
currencies: vec![Currency {
94+
coin_denom: "ATOM".to_string(),
95+
coin_minimal_denom: "uatom".to_string(),
96+
coin_decimals: 6,
97+
coin_gecko_id: "cosmos".to_string(),
98+
}],
99+
fee_currencies: vec![FeeCurrency {
100+
coin_denom: "ATOM".to_string(),
101+
coin_minimal_denom: "uatom".to_string(),
102+
coin_decimals: 6,
103+
coin_gecko_id: "cosmos".to_string(),
104+
gas_price_step: GasPriceStep {
105+
low: 0.01,
106+
average: 0.025,
107+
high: 0.04,
108+
},
109+
}],
110+
stake_currency: Currency {
111+
coin_denom: "ATOM".to_string(),
112+
coin_minimal_denom: "uatom".to_string(),
113+
coin_decimals: 6,
114+
coin_gecko_id: "cosmos".to_string(),
115+
},
116+
};
117+
118+
let chain_info_js = serde_wasm_bindgen::to_value(&chain_info).unwrap();
119+
let _ = suggest_chain(chain_info_js).await;
120+
}

0 commit comments

Comments
 (0)