lnsocket

A minimal C library for connecting to the lightning network
git clone git://jb55.com/lnsocket
Log | Files | Refs | Submodules | README | LICENSE

lib.rs (2535B)


      1 #![allow(non_upper_case_globals)]
      2 #![allow(non_camel_case_types)]
      3 #![allow(non_snake_case)]
      4 
      5 include!("bindings.rs");
      6 
      7 #[cfg(test)]
      8 mod tests {
      9     use super::*;
     10     //use libc::*;
     11     use std::ffi::{CStr, CString};
     12 
     13     #[test]
     14     fn testing() {
     15         unsafe {
     16             let node_id = "03f3c108ccd536b8526841f0a5c58212bb9e6584a1eb493080e7c1cc34f82dad71";
     17             let host = "monad.endpoint.jb55.com";
     18             let rune = "uQux-hID66AX5rFUpkt1p9CU_7DsTMyUC4G5yq7-dcw9MTMmbWV0aG9kPWdldGluZm8=";
     19 
     20             let mut socket: lnsocket = {
     21                 let sock = lnsocket_create();
     22                 ffi_helpers::null_pointer_check!(sock);
     23                 *sock
     24             };
     25 
     26             lnsocket_genkey(&mut socket);
     27 
     28             let res_connect = {
     29                 let c_node_id = CString::new(node_id).unwrap();
     30                 let c_host = CString::new(host).unwrap();
     31                 lnsocket_connect(&mut socket, c_node_id.as_ptr(), c_host.as_ptr())
     32             };
     33             assert_eq!(res_connect, 1);
     34 
     35             let res_perform_init = lnsocket_perform_init(&mut socket);
     36             assert_eq!(res_perform_init, 1);
     37 
     38             let method = "getinfo";
     39             let params = "[]";
     40             let msg = format!(
     41                 "{{\"method\":\"{}\",\"params\":{},\"rune\":\"{}\"}}",
     42                 method, params, rune
     43             );
     44             let res_write = {
     45                 let mut cmd = vec![];
     46                 cmd.append(&mut 0x4c4fu16.to_be_bytes().to_vec());
     47                 cmd.append(&mut 1u64.to_be_bytes().to_vec());
     48                 cmd.append(&mut msg.as_bytes().to_vec());
     49                 assert_eq!(cmd.len(), msg.len() + 10);
     50                 lnsocket_write(&mut socket, cmd.as_ptr(), cmd.len() as u16)
     51             };
     52             assert_eq!(res_write, 1);
     53 
     54             let response = {
     55                 let mut len = 0u16;
     56                 let mut t: u8 = 0;
     57                 let mut typ: u16 = 0;
     58                 let addr = &mut t as *mut u8 as usize;
     59                 let mut uptr = addr as *mut u8;
     60                 let res_recv = lnsocket_recv(&mut socket, &mut typ, &mut uptr, &mut len);
     61                 assert_eq!(res_recv, 1);
     62                 let iptr = uptr as *mut i8;
     63                 if typ == 0x594d {
     64                     CStr::from_ptr(iptr.offset(8)).to_str().unwrap().to_string()
     65                 } else {
     66                     "other".to_string()
     67                 }
     68             };
     69             assert!(response.len() > 0);
     70 
     71             lnsocket_destroy(&mut socket);
     72         }
     73     }
     74 }