channelid: fix serialization from go and python

This commit is contained in:
Gijs Van Laer 2019-10-17 18:44:05 -04:00
parent 8b6a520c99
commit 25445b974e
7 changed files with 37 additions and 30 deletions

View File

@ -311,7 +311,7 @@ func BidirectionalEstablishCustomerGenerateProof(channelToken ChannelToken, cust
return channelToken, custState, com, comProof, err
}
func BidirectionalEstablishMerchantIssueCloseToken(channelState ChannelState, com Commitment, comProof CommitmentProof, channelId string, initCustBal int, initMerchBal int, merchState MerchState) (Signature, error) {
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 {
return Signature{}, err
@ -328,7 +328,11 @@ func BidirectionalEstablishMerchantIssueCloseToken(channelState ChannelState, co
if err != nil {
return Signature{}, err
}
resp := C.GoString(C.ffishim_bidirectional_establish_merchant_issue_close_token(C.CString(string(serChannelState)), C.CString(string(serCom)), C.CString(string(serComProof)), C.CString(channelId), C.longlong(initCustBal), C.longlong(initMerchBal), C.CString(string(serMerchState))))
serChannelId, err := json.Marshal(channelId)
if err != nil {
return Signature{}, err
}
resp := C.GoString(C.ffishim_bidirectional_establish_merchant_issue_close_token(C.CString(string(serChannelState)), C.CString(string(serCom)), C.CString(string(serComProof)), C.CString(string(serChannelId)), C.longlong(initCustBal), C.longlong(initMerchBal), C.CString(string(serMerchState))))
r, err := processCResponse(resp)
if err != nil {
return Signature{}, err

View File

@ -36,7 +36,7 @@ func Test_Establish(t *testing.T) {
channelToken, custState, com, comProof, err := BidirectionalEstablishCustomerGenerateProof(channelToken, custState)
assert.Nil(t, err)
closeToken, err := BidirectionalEstablishMerchantIssueCloseToken(channelState, com, comProof, custState.Wallet.ChannelId[0], b0Cust, b0Merch, merchState)
closeToken, err := BidirectionalEstablishMerchantIssueCloseToken(channelState, com, comProof, custState.Wallet.ChannelId, b0Cust, b0Merch, merchState)
assert.Nil(t, err)
assert.NotNil(t, closeToken)
@ -61,7 +61,7 @@ func Test_Pay(t *testing.T) {
assert.Nil(t, err)
channelToken, custState, com, comProof, err := BidirectionalEstablishCustomerGenerateProof(channelToken, custState)
assert.Nil(t, err)
closeToken, err := BidirectionalEstablishMerchantIssueCloseToken(channelState, com, comProof, custState.Wallet.ChannelId[0], b0Cust, b0Merch, merchState)
closeToken, err := BidirectionalEstablishMerchantIssueCloseToken(channelState, com, comProof, custState.Wallet.ChannelId, b0Cust, b0Merch, merchState)
assert.Nil(t, err)
_, channelState, custState, err = BidirectionalVerifyCloseToken(channelState, custState, closeToken)
assert.Nil(t, err)
@ -95,7 +95,7 @@ func Test_IntermediaryPay(t *testing.T) {
assert.Nil(t, err)
channelToken, custStateAlice, com, comProof, err := BidirectionalEstablishCustomerGenerateProof(channelToken, custStateAlice)
assert.Nil(t, err)
closeToken, err := BidirectionalEstablishMerchantIssueCloseToken(channelState, com, comProof, custStateAlice.Wallet.ChannelId[0], b0Alice, b0Intermediary, merchState)
closeToken, err := BidirectionalEstablishMerchantIssueCloseToken(channelState, com, comProof, custStateAlice.Wallet.ChannelId, b0Alice, b0Intermediary, merchState)
assert.Nil(t, err)
_, channelState, custStateAlice, err = BidirectionalVerifyCloseToken(channelState, custStateAlice, closeToken)
assert.Nil(t, err)
@ -107,7 +107,7 @@ func Test_IntermediaryPay(t *testing.T) {
assert.Nil(t, err)
channelToken, custStateBob, com, comProof, err = BidirectionalEstablishCustomerGenerateProof(channelToken, custStateBob)
assert.Nil(t, err)
closeToken, err = BidirectionalEstablishMerchantIssueCloseToken(channelState, com, comProof, custStateBob.Wallet.ChannelId[0], b0Bob, b0Intermediary, merchState)
closeToken, err = BidirectionalEstablishMerchantIssueCloseToken(channelState, com, comProof, custStateBob.Wallet.ChannelId, b0Bob, b0Intermediary, merchState)
assert.Nil(t, err)
_, channelState, custStateBob, err = BidirectionalVerifyCloseToken(channelState, custStateBob, closeToken)
assert.Nil(t, err)
@ -143,7 +143,7 @@ func Test_Close(t *testing.T) {
assert.Nil(t, err)
channelToken, custState, com, comProof, err := BidirectionalEstablishCustomerGenerateProof(channelToken, custState)
assert.Nil(t, err)
closeToken, err := BidirectionalEstablishMerchantIssueCloseToken(channelState, com, comProof, custState.Wallet.ChannelId[0], b0Cust, b0Merch, merchState)
closeToken, err := BidirectionalEstablishMerchantIssueCloseToken(channelState, com, comProof, custState.Wallet.ChannelId, b0Cust, b0Merch, merchState)
assert.Nil(t, err)
_, channelState, custState, err = BidirectionalVerifyCloseToken(channelState, custState, closeToken)
assert.Nil(t, err)

View File

@ -107,7 +107,7 @@ class Libbolt(object):
return output_dictionary.get('channel_token'), output_dictionary.get('cust_state'), output_dictionary.get('com'), output_dictionary.get('com_proof')
def bidirectional_establish_merchant_issue_close_token(self, channel_state, com, com_proof, channel_id, init_cust, init_merch, merch_state):
output_string = self.lib.ffishim_bidirectional_establish_merchant_issue_close_token(channel_state.encode(), com.encode(), com_proof.encode(), channel_id.encode(), init_cust, init_merch, merch_state.encode())
output_string = self.lib.ffishim_bidirectional_establish_merchant_issue_close_token(channel_state.encode(), com.encode(), com_proof.encode(), json.dumps(channel_id).encode(), init_cust, init_merch, merch_state.encode())
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
return output_dictionary.get('close_token')

View File

@ -47,7 +47,7 @@ class BoltEstablishTests(unittest.TestCase):
(channel_token, cust_state, com, com_proof) = self.bolt.bidirectional_establish_customer_generate_proof(self.channel_token, self.cust_state)
cust_state_dict = json.loads(cust_state)
close_token = self.bolt.bidirectional_establish_merchant_issue_close_token(self.channel_state, com, com_proof, cust_state_dict["pk_c"], self.b0_cust, self.b0_merch, self.merch_state)
close_token = self.bolt.bidirectional_establish_merchant_issue_close_token(self.channel_state, com, com_proof, cust_state_dict["wallet"]["channelId"], self.b0_cust, self.b0_merch, self.merch_state)
self.assertTrue(close_token is not None)
(is_token_valid, channel_state, cust_state) = self.bolt.bidirectional_establish_customer_verify_close_token(self.channel_state, cust_state, close_token)
@ -68,7 +68,7 @@ class BoltEstablishTests(unittest.TestCase):
(channel_token, cust_state, com, com_proof) = self.bolt.bidirectional_establish_customer_generate_proof(self.channel_token_bad, self.cust_state_bad)
cust_state_dict = json.loads(cust_state)
close_token = self.bolt.bidirectional_establish_merchant_issue_close_token(self.channel_state, com, com_proof, cust_state_dict["pk_c"], self.b0_cust, self.b0_merch, self.merch_state)
close_token = self.bolt.bidirectional_establish_merchant_issue_close_token(self.channel_state, com, com_proof, cust_state_dict["wallet"]["channelId"], self.b0_cust, self.b0_merch, self.merch_state)
self.assertTrue(close_token is None)
#print("Establish protocol fail works as expected.")
@ -91,7 +91,7 @@ class BoltEstablishTests(unittest.TestCase):
(channel_token, cust_state, com, com_proof) = self.bolt.bidirectional_establish_customer_generate_proof(self.channel_token, self.cust_state)
cust_state_dict = json.loads(cust_state)
close_token = self.bolt.bidirectional_establish_merchant_issue_close_token(self.channel_state, com, com_proof, cust_state_dict["pk_c"], self.b0_cust, self.b0_merch, self.merch_state)
close_token = self.bolt.bidirectional_establish_merchant_issue_close_token(self.channel_state, com, com_proof, cust_state_dict["wallet"]["channelId"], self.b0_cust, self.b0_merch, self.merch_state)
self.assertTrue(close_token is not None)
(is_token_valid, channel_state, cust_state) = self.bolt.bidirectional_establish_customer_verify_close_token(self.channel_state, cust_state, close_token)
@ -125,7 +125,7 @@ class BoltEstablishTests(unittest.TestCase):
(channel_token, cust_state, com, com_proof) = self.bolt.bidirectional_establish_customer_generate_proof(self.channel_token, self.cust_state)
cust_state_dict = json.loads(cust_state)
close_token = self.bolt.bidirectional_establish_merchant_issue_close_token(self.channel_state, com, com_proof, cust_state_dict["pk_c"], self.b0_cust, self.b0_merch, self.merch_state)
close_token = self.bolt.bidirectional_establish_merchant_issue_close_token(self.channel_state, com, com_proof, cust_state_dict["wallet"]["channelId"], self.b0_cust, self.b0_merch, self.merch_state)
self.assertTrue(close_token is not None)
malformed_close_token = malformed_token(close_token)
@ -156,7 +156,7 @@ class BoltPayTests(unittest.TestCase):
(self.channel_token, self.cust_state, com, com_proof) = self.bolt.bidirectional_establish_customer_generate_proof(self.channel_token, self.cust_state)
cust_state_dict = json.loads(self.cust_state)
close_token = self.bolt.bidirectional_establish_merchant_issue_close_token(self.channel_state, com, com_proof, cust_state_dict["pk_c"], self.b0_cust, self.b0_merch, self.merch_state)
close_token = self.bolt.bidirectional_establish_merchant_issue_close_token(self.channel_state, com, com_proof, cust_state_dict["wallet"]["channelId"], self.b0_cust, self.b0_merch, self.merch_state)
self.assertTrue(close_token is not None)
(is_token_valid, self.channel_state, self.cust_state) = self.bolt.bidirectional_establish_customer_verify_close_token(self.channel_state, self.cust_state, close_token)
@ -299,12 +299,12 @@ class BoltMultiChannelTests(unittest.TestCase):
"""
alice_cust_state_dict = json.loads(self.alice_state)
self.channel_token_a, self.channel_state_a, alice_cust_state = self._establish_channel(self.channel_token_a, self.channel_state,
self.alice_state, alice_cust_state_dict["pk_c"],
self.alice_state, alice_cust_state_dict["wallet"]["channelId"],
self.b0_alice, self.b0_merch)
charlie_cust_state_dict = json.loads(self.charlie_state)
self.channel_token_c, self.channel_state_c, charlie_cust_state = self._establish_channel(self.channel_token_c, self.channel_state,
self.charlie_state, charlie_cust_state_dict["pk_c"],
self.charlie_state, charlie_cust_state_dict["wallet"]["channelId"],
self.b0_charlie, self.b0_merch)
self.channel_state_a, alice_cust_state = self._pay_on_channel(self.channel_state_a, alice_cust_state, 15)

View File

@ -124,16 +124,14 @@ impl<E: Engine> ChannelToken<E> {
}
pub fn compute_channel_id(&self) -> E::Fr
// where <E as pairing::Engine>::G1: serde::Serialize,
// <E as pairing::Engine>::G2: serde::Serialize,
// <E as ff::ScalarEngine>::Fr: serde::Serialize
where <E as pairing::Engine>::G1: serde::Serialize,
<E as pairing::Engine>::G2: serde::Serialize,
<E as ff::ScalarEngine>::Fr: serde::Serialize
{
if self.pk_c.is_none() {
panic!("pk_c is not initialized yet");
}
//let input = serde_json::to_vec(&self).unwrap();
let mut input = Vec::new();
input.extend_from_slice(&self.pk_m.serialize_uncompressed());
let input = serde_json::to_vec(&self).unwrap();
return hash_to_fr::<E>(input);
}
@ -224,7 +222,11 @@ pub struct CustomerState<E: Engine> {
}
impl<E: Engine> CustomerState<E> {
pub fn new<R: Rng>(csprng: &mut R, channel_token: &mut ChannelToken<E>, cust_bal: i64, merch_bal: i64, name: String) -> Self {
pub fn new<R: Rng>(csprng: &mut R, channel_token: &mut ChannelToken<E>, cust_bal: i64, merch_bal: i64, name: String) -> Self
where <E as pairing::Engine>::G1: serde::Serialize,
<E as pairing::Engine>::G2: serde::Serialize,
<E as ff::ScalarEngine>::Fr: serde::Serialize
{
let mut kp = secp256k1::Secp256k1::new();
kp.randomize(csprng);

View File

@ -3,7 +3,7 @@ pub mod ffishim {
extern crate libc;
use bidirectional;
use ff::Rand;
use ff::{Rand, ScalarEngine};
use pairing::bls12_381::Bls12;
use serde::Deserialize;
@ -180,13 +180,10 @@ pub mod ffishim {
let merch_state = handle_errors!(merch_state_result);
// Deserialize the pk_c
let bytes = unsafe { CStr::from_ptr(ser_channel_id).to_bytes() };
let string: &str = str::from_utf8(bytes).unwrap();
let pk_c_result = secp256k1::PublicKey::from_str(string);
let pk_c = handle_errors!(pk_c_result);
let pk_fr = hash_pubkey_to_fr::<Bls12>(&pk_c);
let channel_id_result: ResultSerdeType<<Bls12 as ScalarEngine>::Fr> = deserialize_result_object(ser_channel_id);
let channel_id_fr = handle_errors!(channel_id_result);
let close_token = bolt_try!(bidirectional::establish_merchant_issue_close_token(rng, &channel_state, &com, &com_proof, &pk_fr, init_cust_bal, init_merch_bal, &merch_state));
let close_token = bolt_try!(bidirectional::establish_merchant_issue_close_token(rng, &channel_state, &com, &com_proof, &channel_id_fr, init_cust_bal, init_merch_bal, &merch_state));
let ser = ["{\'close_token\':\'", serde_json::to_string(&close_token).unwrap().as_str(), "\'}"].concat();
let cser = CString::new(ser).unwrap();

View File

@ -193,7 +193,11 @@ pub mod bidirectional {
/// and wallet commitment.
///
pub fn init_customer<'a, R: Rng, E: Engine>(csprng: &mut R, channel_token: &mut ChannelToken<E>,
b0_cust: i64, b0_merch: i64, name: &'a str) -> CustomerState<E> {
b0_cust: i64, b0_merch: i64, name: &'a str) -> CustomerState<E>
where <E as pairing::Engine>::G1: serde::Serialize,
<E as pairing::Engine>::G2: serde::Serialize,
<E as ff::ScalarEngine>::Fr: serde::Serialize
{
assert!(b0_cust >= 0);
assert!(b0_merch >= 0);