From 67d321e30ce4686c488ae10605b85732e06c39a3 Mon Sep 17 00:00:00 2001 From: "J. Ayo Akinyele" Date: Thu, 24 Oct 2019 01:33:44 -0400 Subject: [PATCH] added func call to generate channel ID from channel token --- go/libbolt.go | 19 +++++++++++++++++++ include/libbolt.h | 1 + py/libbolt.py | 12 ++++++++++++ src/ffishim.rs | 14 +++++++++++++- 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/go/libbolt.go b/go/libbolt.go index f7d2125..95e5065 100644 --- a/go/libbolt.go +++ b/go/libbolt.go @@ -7,6 +7,7 @@ import "C" import ( "encoding/json" "strings" + "fmt" ) type setupResp struct { @@ -317,6 +318,24 @@ func BidirectionalEstablishCustomerGenerateProof(channelToken ChannelToken, cust return channelToken, custState, com, comProof, err } + + +func BidirectionalGenerateChannelID(channelToken ChannelToken) (error) { + serChannelToken, err := json.Marshal(channelToken) + if err != nil { + return err + } + resp := C.GoString(C.ffishim_bidirectional_generate_channel_id(C.CString(string(serChannelToken)))) + r, err := processCResponse(resp) + if err != nil { + return err + } + fmt.Println("channel id: ", r) + return err +} + + + func BidirectionalEstablishMerchantIssueCloseToken(channelState ChannelState, com Commitment, comProof CommitmentProof, channelId []string, initCustBal int, initMerchBal int, merchState MerchState) (Signature, error) { serChannelState, err := json.Marshal(channelState) if err != nil { diff --git a/include/libbolt.h b/include/libbolt.h index df4e45d..f36d6bb 100644 --- a/include/libbolt.h +++ b/include/libbolt.h @@ -22,6 +22,7 @@ char* ffishim_bidirectional_init_customer(const char *ser_channel_token, long lo // channel establish protocol routines char* ffishim_bidirectional_establish_customer_generate_proof(const char *ser_channel_token, const char *ser_customer_wallet); +char* ffishim_bidirectional_generate_channel_id(const char *ser_channel_token); char* ffishim_bidirectional_establish_merchant_issue_close_token(const char *ser_channel_state, const char *ser_com, const char *ser_com_proof, const char *ser_pk_c, long long int init_cust_bal, long long int init_merch_bal, const char *ser_merch_state); char* ffishim_bidirectional_establish_merchant_issue_pay_token(const char *ser_channel_state, const char *ser_com, const char *ser_merch_state); char* ffishim_bidirectional_verify_close_token(const char *ser_channel_state, const char *ser_customer_wallet, const char *ser_close_token); diff --git a/py/libbolt.py b/py/libbolt.py index 710f26f..137e5c6 100644 --- a/py/libbolt.py +++ b/py/libbolt.py @@ -29,6 +29,9 @@ class Libbolt(object): self.lib.ffishim_bidirectional_establish_customer_generate_proof.argtypes = (c_void_p, c_void_p) self.lib.ffishim_bidirectional_establish_customer_generate_proof.restype = c_void_p + self.lib.ffishim_bidirectional_generate_channel_id.argtypes = (c_void_p, ) + self.lib.ffishim_bidirectional_generate_channel_id.restype = c_void_p + self.lib.ffishim_bidirectional_establish_merchant_issue_close_token.argtypes = (c_void_p, c_void_p, c_void_p, c_void_p, c_void_p, c_void_p, c_void_p) self.lib.ffishim_bidirectional_establish_merchant_issue_close_token.restype = c_void_p @@ -101,6 +104,11 @@ class Libbolt(object): # ESTABLISH PROTOCOL + def bidirectional_generate_channel_id(self, channel_token): + output_string = self.lib.ffishim_bidirectional_generate_channel_id(channel_token.encode()) + output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8')) + return output_dictionary.get('channel_id') + def bidirectional_establish_customer_generate_proof(self, channel_token, cust_state): output_string = self.lib.ffishim_bidirectional_establish_customer_generate_proof(channel_token.encode(), cust_state.encode()) output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8')) @@ -259,6 +267,10 @@ def run_unit_test(): print("com: ", com) cust_state_dict = json.loads(cust_state) + channel_id = libbolt.bidirectional_generate_channel_id(channel_token) + print("channel ID: ", channel_id) + #print("wallet chan ID: ", cust_state_dict["wallet"]["channelId"]) + close_token = libbolt.bidirectional_establish_merchant_issue_close_token(channel_state, com, com_proof, cust_state_dict["wallet"]["channelId"], b0_cust, b0_merch, merch_state) print("close token: ", close_token) diff --git a/src/ffishim.rs b/src/ffishim.rs index 9be004c..e89d990 100644 --- a/src/ffishim.rs +++ b/src/ffishim.rs @@ -136,7 +136,7 @@ pub mod ffishim { // ESTABLISH - #[no_mangle] // bidirectional::establish_customer_generate_proof(rng, &mut channel_token, &mut cust_state); + #[no_mangle] pub extern fn ffishim_bidirectional_establish_customer_generate_proof(ser_channel_token: *mut c_char, ser_customer_state: *mut c_char) -> *mut c_char { let rng = &mut rand::thread_rng(); // Deserialize the channel token @@ -158,6 +158,18 @@ pub mod ffishim { cser.into_raw() } + #[no_mangle] + pub extern fn ffishim_bidirectional_generate_channel_id(ser_channel_token: *mut c_char) -> *mut c_char { + // Deserialize the channel token + let channel_token_result: ResultSerdeType> = deserialize_result_object(ser_channel_token); + let channel_token = handle_errors!(channel_token_result); + + let id = channel_token.compute_channel_id(); + let ser = ["{\'channel_id\':\'", serde_json::to_string(&id).unwrap().as_str(), "\'}"].concat(); + let cser = CString::new(ser).unwrap(); + cser.into_raw() + } + #[no_mangle] pub extern fn ffishim_bidirectional_establish_merchant_issue_close_token(ser_channel_state: *mut c_char, ser_com: *mut c_char, ser_com_proof: *mut c_char, ser_channel_id: *mut c_char, init_cust_bal: i64, init_merch_bal: i64, ser_merch_state: *mut c_char) -> *mut c_char { let rng = &mut rand::thread_rng();