Skip to content

Commit

Permalink
Merge pull request #53 from tonlabs/update-engine-tag
Browse files Browse the repository at this point in the history
Update dengine tag
  • Loading branch information
joydark authored Oct 27, 2020
2 parents 9170975 + 9b53c3e commit 7b80c34
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 77 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ documentation = "https://docs.ton.dev"
build = "build.rs"
readme = "README.md"
license = "Apache-2.0"
keywords = ["TON", "SDK", "smart contract", "tonlabs"]
keywords = ["TON", "SDK", "smart contract", "tonlabs", "solidity"]
edition = "2018"
version = "0.1.21"
version = "0.1.22"

[dependencies]
base64 = "0.10.1"
Expand All @@ -34,9 +34,8 @@ ton_sdk = { git = 'https://github.com/tonlabs/TON-SDK.git', tag = "0" }
ton_types = { git = "https://github.com/tonlabs/ton-labs-types.git" }
ton_block = { git = "https://github.com/tonlabs/ton-labs-block.git" }

debot-engine = { git = 'https://github.com/tonlabs/debot-engine.git', tag = "v0.1.0" }
debot-engine = { git = 'https://github.com/tonlabs/debot-engine.git', tag = "v0.1.1" }

[dev-dependencies]
assert_cmd = "0.11"
predicates = "1"
stdio-override = "0.1.3"
132 changes: 61 additions & 71 deletions src/debot/term_browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::crypto::load_keypair;
use crate::helpers::load_ton_address;
use debot_engine::{BrowserCallbacks, DAction, DEngine, STATE_EXIT};
use std::cell::RefCell;
use std::io::{self, Write};
use std::io::{self, BufRead, Write};
use std::rc::Rc;
use ton_client_rs::{Ed25519KeyPair, TonAddress};

Expand Down Expand Up @@ -97,36 +97,18 @@ impl BrowserCallbacks for Callbacks {

// Debot engine asks user to enter argument for an action.
fn input(&self, prefix: &str, value: &mut String) {
let mut input_str = "".to_owned();
let mut argc = 0;
while argc == 0 {
print!("{} > ", prefix);
let _ = io::stdout().flush();
if let Err(e) = io::stdin().read_line(&mut input_str) {
println!("failed to read line: {}", e)
}
argc = input_str
.split_whitespace()
.map(|x| x.parse::<String>().unwrap())
.collect::<Vec<String>>()
.len();
}
*value = input_str.trim().to_owned();
let stdio = io::stdin();
let mut reader = stdio.lock();
let mut writer = io::stdout();
*value = input(prefix, &mut reader, &mut writer);
}

/// Debot engine requests keys to sign something
fn load_key(&self, keys: &mut Ed25519KeyPair) {
let mut value = String::new();
let enter_str = "enter seed phrase or path to keypair file";
self.input(enter_str, &mut value);

let mut pair = load_keypair(&value);
while let Err(e) = pair {
println!("Invalid keys: {}. Try again.", e);
self.input(enter_str, &mut value);
pair = load_keypair(&value);
}
*keys = pair.unwrap();
let stdio = io::stdin();
let mut reader = stdio.lock();
let mut writer = io::stdout();
*keys = input_keys(&mut reader, &mut writer);
}
/// Debot asks to run action of another debot
fn invoke_debot(&self, debot: TonAddress, action: DAction) -> Result<(), String> {
Expand All @@ -147,6 +129,33 @@ impl BrowserCallbacks for Callbacks {
}
}

fn input<R, W>(prefix: &str, reader: &mut R, writer: &mut W) -> String
where
R: BufRead,
W: Write,
{
let mut input_str = "".to_owned();
let mut argc = 0;
while argc == 0 {
print!("{} > ", prefix);
if let Err(e) = writer.flush() {
println!("failed to flush: {}", e);
return input_str;
}
if let Err(e) = reader.read_line(&mut input_str) {
println!("failed to read line: {}", e);
return input_str;
}
argc = input_str
.split_whitespace()
.map(|x| x.parse::<String>().unwrap())
.collect::<Vec<String>>()
.len();
}
input_str.trim().to_owned()
}


fn action_input(max: usize) -> Result<(usize, usize, Vec<String>), String> {
let mut a_str = String::new();
let mut argc = 0;
Expand All @@ -173,6 +182,23 @@ fn action_input(max: usize) -> Result<(usize, usize, Vec<String>), String> {
Ok((n, argc, argv))
}

fn input_keys<R, W>(reader: &mut R, writer: &mut W) -> Ed25519KeyPair
where
R: BufRead,
W: Write,
{
let enter_str = "enter seed phrase or path to keypair file";
let mut value = input(enter_str, reader, writer);

let mut pair = load_keypair(&value);
while let Err(e) = pair {
println!("Invalid keys: {}. Try again.", e);
value = input(enter_str, reader, writer);
pair = load_keypair(&value);
}
pair.unwrap()
}

pub fn run_debot_browser(addr: &str, abi: Option<String>, config: Config) -> Result<(), String> {
let url = config.url.clone();
let browser = Rc::new(RefCell::new(TerminalBrowser::new(config.url)));
Expand All @@ -194,8 +220,6 @@ pub fn run_debot_browser(addr: &str, abi: Option<String>, config: Config) -> Res
#[cfg(test)]
mod browser_tests {
use super::*;

use stdio_override::StdinOverride;
use std::fs::File;

const PUBLIC: &'static str = "9711a04f0b19474272bc7bae5472a8fbbb6ef71ce9c193f5ec3f5af808069a41";
Expand All @@ -211,60 +235,26 @@ mod browser_tests {
}}"#, PUBLIC, PRIVATE).as_bytes()).unwrap();
}

fn prepare_stdin_file_with_path_to_file(stdin_name: &str) {
create_keypair_file(KEYS_FILE);
let mut file = File::create(&stdin_name).unwrap();
file.write_all(format!("{}\n", KEYS_FILE).as_bytes()).unwrap();
}

fn prepare_stdin_file_with_seedphrase(stdin_name: &str) {
let mut file = File::create(&stdin_name).unwrap();
file.write_all(format!("{}", SEED).as_bytes()).unwrap();
}

fn create_callbacks() -> Callbacks {
let browser = Rc::new(RefCell::new(TerminalBrowser::new("localhost".to_owned())));
Callbacks::new(Rc::clone(&browser))
}

#[test]
fn load_key_from_file() {
let stdin_file = "./keys.txt";
let mut in_data = KEYS_FILE.as_bytes();
let mut out_data = vec![];

prepare_stdin_file_with_path_to_file(stdin_file);
let guard = StdinOverride::override_file(stdin_file).unwrap();

let callbacks = create_callbacks();

let mut keys = Ed25519KeyPair::zero();
callbacks.load_key(&mut keys);

drop(guard);
std::fs::remove_file(KEYS_FILE).unwrap();
create_keypair_file(KEYS_FILE);
let keys = input_keys(&mut in_data, &mut out_data);

assert_eq!(format!("{}", keys.public), PUBLIC);
assert_eq!(format!("{}", keys.secret), PRIVATE);

std::fs::remove_file(stdin_file).unwrap();
assert_eq!(format!("{}", keys.secret), PRIVATE);
}

#[test]
fn load_key_from_seed() {
let stdin_file = "./seed.txt";
let mut in_data = SEED.as_bytes();
let mut out_data = vec![];

prepare_stdin_file_with_seedphrase(stdin_file);
let guard = StdinOverride::override_file(stdin_file).unwrap();

let callbacks = create_callbacks();

let mut keys = Ed25519KeyPair::zero();
callbacks.load_key(&mut keys);

drop(guard);
let keys = input_keys(&mut in_data, &mut out_data);

assert_eq!(format!("{}", keys.public), PUBLIC);
assert_eq!(format!("{}", keys.secret), PRIVATE);

std::fs::remove_file(stdin_file).unwrap();
}
}
4 changes: 2 additions & 2 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl log::Log for SimpleLogger {
pub fn read_keys(filename: &str) -> Result<Ed25519KeyPair, String> {
let keys_str = std::fs::read_to_string(filename)
.map_err(|e| format!("failed to read keypair file: {}", e.to_string()))?;
let keys: Ed25519KeyPair = serde_json::from_str(&keys_str).unwrap();
Ok(keys)
serde_json::from_str(&keys_str)
.map_err(|e| format!("failed to parse keypair file: {}", e))
}

pub fn load_ton_address(addr: &str) -> Result<TonAddress, String> {
Expand Down

0 comments on commit 7b80c34

Please sign in to comment.