2019-07-31 19:31:10 -07:00
|
|
|
from ctypes import cdll
|
|
|
|
from sys import platform
|
|
|
|
|
|
|
|
import sys, ctypes
|
|
|
|
from ctypes import c_void_p, c_uint8
|
|
|
|
|
|
|
|
import ast
|
|
|
|
import json
|
|
|
|
|
|
|
|
class Libbolt(object):
|
2019-09-16 08:58:10 -07:00
|
|
|
"""Libbolt Py/C low-level API"""
|
2019-07-31 19:31:10 -07:00
|
|
|
|
|
|
|
def __init__(self, path):
|
|
|
|
self.lib = cdll.LoadLibrary(path)
|
|
|
|
self.load_library_params()
|
|
|
|
|
|
|
|
def load_library_params(self):
|
|
|
|
self.lib.ffishim_bidirectional_channel_setup.argtypes = (c_void_p, c_uint8)
|
|
|
|
self.lib.ffishim_bidirectional_channel_setup.restype = c_void_p
|
|
|
|
|
|
|
|
# ESTABLISH PROTOCOL
|
|
|
|
|
2019-09-16 08:58:10 -07:00
|
|
|
self.lib.ffishim_bidirectional_init_merchant.argtypes = (c_void_p, c_void_p)
|
2019-07-31 19:31:10 -07:00
|
|
|
self.lib.ffishim_bidirectional_init_merchant.restype = c_void_p
|
|
|
|
|
2019-09-16 08:58:10 -07:00
|
|
|
self.lib.ffishim_bidirectional_init_customer.argtypes = (c_void_p, ctypes.c_int32, ctypes.c_int32, c_void_p)
|
2019-07-31 19:31:10 -07:00
|
|
|
self.lib.ffishim_bidirectional_init_customer.restype = c_void_p
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-10-23 22:33:44 -07:00
|
|
|
self.lib.ffishim_bidirectional_generate_channel_id.argtypes = (c_void_p, )
|
|
|
|
self.lib.ffishim_bidirectional_generate_channel_id.restype = c_void_p
|
|
|
|
|
2019-09-13 13:16:35 -07:00
|
|
|
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)
|
2019-07-31 19:31:10 -07:00
|
|
|
self.lib.ffishim_bidirectional_establish_merchant_issue_close_token.restype = c_void_p
|
|
|
|
|
|
|
|
self.lib.ffishim_bidirectional_establish_merchant_issue_pay_token.argtypes = (c_void_p, c_void_p, c_void_p)
|
|
|
|
self.lib.ffishim_bidirectional_establish_merchant_issue_pay_token.restype = c_void_p
|
|
|
|
|
|
|
|
self.lib.ffishim_bidirectional_verify_close_token.argtypes = (c_void_p, c_void_p, c_void_p)
|
|
|
|
self.lib.ffishim_bidirectional_verify_close_token.restype = c_void_p
|
|
|
|
|
|
|
|
self.lib.ffishim_bidirectional_establish_customer_final.argtypes = (c_void_p, c_void_p, c_void_p)
|
|
|
|
self.lib.ffishim_bidirectional_establish_customer_final.restype = c_void_p
|
|
|
|
|
|
|
|
# PAY PROTOCOL
|
|
|
|
|
|
|
|
self.lib.ffishim_bidirectional_pay_generate_payment_proof.argtypes = (c_void_p, c_void_p, ctypes.c_int32)
|
|
|
|
self.lib.ffishim_bidirectional_pay_generate_payment_proof.restype = c_void_p
|
|
|
|
|
|
|
|
self.lib.ffishim_bidirectional_pay_verify_payment_proof.argtypes = (c_void_p, c_void_p, c_void_p)
|
|
|
|
self.lib.ffishim_bidirectional_pay_verify_payment_proof.restype = c_void_p
|
|
|
|
|
2019-10-11 09:55:30 -07:00
|
|
|
self.lib.ffishim_bidirectional_pay_verify_multiple_payment_proofs.argtypes = (c_void_p, c_void_p, c_void_p, c_void_p)
|
|
|
|
self.lib.ffishim_bidirectional_pay_verify_multiple_payment_proofs.restype = c_void_p
|
|
|
|
|
2019-07-31 19:31:10 -07:00
|
|
|
self.lib.ffishim_bidirectional_pay_generate_revoke_token.argtypes = (c_void_p, c_void_p, c_void_p, c_void_p)
|
|
|
|
self.lib.ffishim_bidirectional_pay_generate_revoke_token.restype = c_void_p
|
|
|
|
|
|
|
|
self.lib.ffishim_bidirectional_pay_verify_revoke_token.argtypes = (c_void_p, c_void_p)
|
|
|
|
self.lib.ffishim_bidirectional_pay_verify_revoke_token.restype = c_void_p
|
|
|
|
|
2019-10-11 09:55:30 -07:00
|
|
|
self.lib.ffishim_bidirectional_pay_verify_multiple_revoke_tokens.argtypes = (c_void_p, c_void_p, c_void_p)
|
|
|
|
self.lib.ffishim_bidirectional_pay_verify_multiple_revoke_tokens.restype = c_void_p
|
|
|
|
|
2019-07-31 19:31:10 -07:00
|
|
|
self.lib.ffishim_bidirectional_pay_verify_payment_token.argtypes = (c_void_p, c_void_p)
|
|
|
|
self.lib.ffishim_bidirectional_pay_verify_payment_token.restype = c_void_p
|
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
# CLOSE
|
|
|
|
|
2019-08-12 19:47:21 -07:00
|
|
|
self.lib.ffishim_bidirectional_customer_close.argtypes = (c_void_p, c_void_p)
|
|
|
|
self.lib.ffishim_bidirectional_customer_close.restype = c_void_p
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-26 08:08:49 -07:00
|
|
|
self.lib.ffishim_bidirectional_merchant_close.argtypes = (c_void_p, c_void_p, c_void_p, c_void_p, c_void_p)
|
2019-08-20 16:09:01 -07:00
|
|
|
self.lib.ffishim_bidirectional_merchant_close.restype = c_void_p
|
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
# ON-CHAIN BOLT LOGIC / WTPs
|
|
|
|
|
2019-08-26 08:08:49 -07:00
|
|
|
self.lib.ffishim_bidirectional_wtp_verify_cust_close_message.argtypes = (c_void_p, c_void_p, c_void_p, c_void_p)
|
|
|
|
self.lib.ffishim_bidirectional_wtp_verify_cust_close_message.restype = c_void_p
|
2019-08-22 17:52:18 -07:00
|
|
|
|
2019-08-26 08:08:49 -07:00
|
|
|
self.lib.ffishim_bidirectional_wtp_verify_merch_close_message.argtypes = (c_void_p, c_void_p, c_void_p)
|
|
|
|
self.lib.ffishim_bidirectional_wtp_verify_merch_close_message.restype = c_void_p
|
2019-07-31 19:31:10 -07:00
|
|
|
|
|
|
|
self.lib.ffishim_free_string.argtypes = (c_void_p, )
|
|
|
|
|
|
|
|
def channel_setup(self, name, third_party_support=0):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_channel_setup(name.encode(), third_party_support)
|
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-09-13 20:52:47 -07:00
|
|
|
return output_dictionary.get('channel_state')
|
2019-07-31 19:31:10 -07:00
|
|
|
|
|
|
|
# INIT PROTOCOL
|
|
|
|
|
2019-09-16 08:58:10 -07:00
|
|
|
def bidirectional_init_merchant(self, channel_state, name):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_init_merchant(channel_state.encode(), name.encode())
|
2019-07-31 19:31:10 -07:00
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-09-11 07:54:16 -07:00
|
|
|
return output_dictionary.get('channel_token'), output_dictionary.get('merch_state'), output_dictionary.get('channel_state')
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-09-16 08:58:10 -07:00
|
|
|
def bidirectional_init_customer(self, channel_token, b0_cust, b0_merch, name):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_init_customer(channel_token.encode(), b0_cust, b0_merch, name.encode())
|
2019-07-31 19:31:10 -07:00
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-09-13 20:52:47 -07:00
|
|
|
return (output_dictionary.get('channel_token'), output_dictionary.get('cust_state'))
|
2019-07-31 19:31:10 -07:00
|
|
|
|
|
|
|
# ESTABLISH PROTOCOL
|
|
|
|
|
2019-10-23 22:33:44 -07:00
|
|
|
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')
|
|
|
|
|
2019-08-20 16:09:01 -07:00
|
|
|
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())
|
2019-07-31 19:31:10 -07:00
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-09-13 20:52:47 -07:00
|
|
|
return output_dictionary.get('channel_token'), output_dictionary.get('cust_state'), output_dictionary.get('com'), output_dictionary.get('com_proof')
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-10-17 15:01:41 -07:00
|
|
|
def bidirectional_establish_merchant_issue_close_token(self, channel_state, com, com_proof, channel_id, init_cust, init_merch, merch_state):
|
2019-10-17 15:44:05 -07:00
|
|
|
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())
|
2019-07-31 19:31:10 -07:00
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-08-27 23:17:12 -07:00
|
|
|
return output_dictionary.get('close_token')
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-20 16:09:01 -07:00
|
|
|
def bidirectional_establish_merchant_issue_pay_token(self, channel_state, com, merch_state):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_establish_merchant_issue_pay_token(channel_state.encode(), com.encode(), merch_state.encode())
|
2019-07-31 19:31:10 -07:00
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-08-27 23:17:12 -07:00
|
|
|
return output_dictionary.get('pay_token')
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-20 16:09:01 -07:00
|
|
|
def bidirectional_establish_customer_verify_close_token(self, channel_state, cust_state, close_token):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_verify_close_token(channel_state.encode(), cust_state.encode(), close_token.encode())
|
2019-07-31 19:31:10 -07:00
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-08-27 23:17:12 -07:00
|
|
|
is_token_valid = self._convert_boolean(output_dictionary.get('is_token_valid'))
|
|
|
|
return is_token_valid, output_dictionary.get('channel_state'), output_dictionary.get('cust_state')
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-20 16:09:01 -07:00
|
|
|
def bidirectional_establish_customer_final(self, channel_state, cust_state, pay_token):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_establish_customer_final(channel_state.encode(), cust_state.encode(), pay_token.encode())
|
2019-07-31 19:31:10 -07:00
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-08-27 23:17:12 -07:00
|
|
|
is_established = self._convert_boolean(output_dictionary.get('is_established'))
|
|
|
|
return is_established, output_dictionary.get('channel_state'), output_dictionary.get('cust_state')
|
2019-07-31 19:31:10 -07:00
|
|
|
|
|
|
|
# PAY PROTOCOL
|
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
# generate payment proof and new cust state
|
2019-08-20 16:09:01 -07:00
|
|
|
def bidirectional_pay_generate_payment_proof(self, channel_state, cust_state, amount):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_pay_generate_payment_proof(channel_state.encode(), cust_state.encode(), amount)
|
2019-07-31 19:31:10 -07:00
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-09-05 19:45:47 -07:00
|
|
|
return output_dictionary.get('payment'), output_dictionary.get('cust_state')
|
2019-07-31 19:31:10 -07:00
|
|
|
|
|
|
|
# verify payment proof
|
2019-08-20 16:09:01 -07:00
|
|
|
def bidirectional_pay_verify_payment_proof(self, channel_state, pay_proof, merch_state):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_pay_verify_payment_proof(channel_state.encode(), pay_proof.encode(), merch_state.encode())
|
2019-07-31 19:31:10 -07:00
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-09-05 08:48:08 -07:00
|
|
|
return (output_dictionary.get('close_token'), output_dictionary.get('merch_state'))
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-10-11 09:55:30 -07:00
|
|
|
# verify multiple payment proof
|
|
|
|
def bidirectional_pay_verify_multiple_payment_proofs(self, channel_state, sender_pay_proof, receiver_pay_proof, merch_state):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_pay_verify_multiple_payment_proofs(channel_state.encode(), sender_pay_proof.encode(), receiver_pay_proof.encode(), merch_state.encode())
|
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
|
|
|
return (output_dictionary.get('sender_close_token'), output_dictionary.get('receiver_cond_close_token'), output_dictionary.get('merch_state'))
|
|
|
|
|
2019-07-31 19:31:10 -07:00
|
|
|
# generate revoke token
|
2019-08-20 16:09:01 -07:00
|
|
|
def bidirectional_pay_generate_revoke_token(self, channel_state, cust_state, new_cust_state, close_token):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_pay_generate_revoke_token(channel_state.encode(), cust_state.encode(),
|
|
|
|
new_cust_state.encode(), close_token.encode())
|
2019-07-31 19:31:10 -07:00
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-09-05 19:45:47 -07:00
|
|
|
return output_dictionary.get('revoke_token'), output_dictionary.get('cust_state')
|
2019-07-31 19:31:10 -07:00
|
|
|
|
|
|
|
# verify revoke token
|
2019-08-20 16:09:01 -07:00
|
|
|
def bidirectional_pay_verify_revoke_token(self, revoke_token, merch_state):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_pay_verify_revoke_token(revoke_token.encode(), merch_state.encode())
|
2019-07-31 19:31:10 -07:00
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-09-05 19:45:47 -07:00
|
|
|
return (output_dictionary.get('pay_token'), output_dictionary.get('merch_state'))
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-10-11 09:55:30 -07:00
|
|
|
# verify multiple revoke tokens
|
|
|
|
def bidirectional_pay_verify_multiple_revoke_tokens(self, sender_revoke_token, receiver_revoke_token, merch_state):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_pay_verify_multiple_revoke_tokens(sender_revoke_token.encode(), receiver_revoke_token.encode(), merch_state.encode())
|
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
|
|
|
return (output_dictionary.get('sender_pay_token'), output_dictionary.get('receiver_pay_token'), output_dictionary.get('merch_state'))
|
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
# verify payment token
|
|
|
|
def bidirectional_pay_verify_payment_token(self, channel_state, cust_state, pay_token):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_pay_verify_payment_token(channel_state.encode(), cust_state.encode(), pay_token.encode())
|
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-09-05 08:48:08 -07:00
|
|
|
is_pay_valid = self._convert_boolean(output_dictionary.get('is_pay_valid'))
|
2019-09-05 19:45:47 -07:00
|
|
|
return (output_dictionary.get('cust_state'), is_pay_valid)
|
2019-08-22 17:52:18 -07:00
|
|
|
|
2019-07-31 19:31:10 -07:00
|
|
|
# CLOSE
|
|
|
|
|
2019-08-20 16:09:01 -07:00
|
|
|
def bidirectional_customer_close(self, channel_state, cust_state):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_customer_close(channel_state.encode(), cust_state.encode())
|
2019-07-31 19:31:10 -07:00
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-08-20 16:09:01 -07:00
|
|
|
return output_dictionary.get('cust_close')
|
|
|
|
|
2019-08-26 08:08:49 -07:00
|
|
|
def bidirectional_merchant_close(self, channel_state, channel_token, address, cust_close, merch_state):
|
2019-08-20 16:09:01 -07:00
|
|
|
output_string = self.lib.ffishim_bidirectional_merchant_close(channel_state.encode(), channel_token.encode(),
|
2019-08-26 08:08:49 -07:00
|
|
|
address.encode(), cust_close.encode(), merch_state.encode())
|
2019-08-20 16:09:01 -07:00
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
2019-08-26 08:08:49 -07:00
|
|
|
return (output_dictionary.get('wpk'), output_dictionary.get('merch_close'), output_dictionary.get('error'))
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
# WTP logic
|
|
|
|
|
|
|
|
def wtp_get_wallet(self, cust_state):
|
2019-08-26 08:08:49 -07:00
|
|
|
cust_state_dict = self._interperate_json_string_as_dictionary(cust_state)
|
2019-08-22 17:52:18 -07:00
|
|
|
return json.dumps(cust_state_dict.get("wpk")), json.dumps(cust_state_dict.get("wallet"))
|
|
|
|
|
|
|
|
def wtp_get_close_token(self, cust_close):
|
2019-08-26 08:08:49 -07:00
|
|
|
cust_close_dict = self._interperate_json_string_as_dictionary(cust_close)
|
2019-08-22 17:52:18 -07:00
|
|
|
return json.dumps(cust_close_dict.get("signature"))
|
|
|
|
|
2019-08-26 08:08:49 -07:00
|
|
|
def wtp_verify_cust_close_message(self, channel_token, wpk, cust_close_wallet, close_token):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_wtp_verify_cust_close_message(channel_token.encode(),
|
|
|
|
wpk.encode(),
|
|
|
|
cust_close_wallet.encode(),
|
|
|
|
close_token.encode())
|
2019-08-22 17:52:18 -07:00
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
|
|
|
return output_dictionary.get('result')
|
|
|
|
|
2019-08-26 08:08:49 -07:00
|
|
|
def wtp_verify_merch_close_message(self, channel_token, wpk, merch_close):
|
|
|
|
output_string = self.lib.ffishim_bidirectional_wtp_verify_merch_close_message(channel_token.encode(),
|
|
|
|
wpk.encode(),
|
|
|
|
merch_close.encode())
|
|
|
|
output_dictionary = ast.literal_eval(ctypes.cast(output_string, ctypes.c_char_p).value.decode('utf-8'))
|
|
|
|
return output_dictionary.get('result')
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-26 08:08:49 -07:00
|
|
|
def _interperate_json_string_as_dictionary(self, json_string):
|
|
|
|
return ast.literal_eval(json_string)
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-27 23:17:12 -07:00
|
|
|
def _convert_boolean(self, bool_str):
|
|
|
|
if bool_str == "true":
|
|
|
|
return True
|
|
|
|
if bool_str == "false":
|
|
|
|
return False
|
|
|
|
return bool_str
|
|
|
|
|
2019-07-31 19:31:10 -07:00
|
|
|
if platform == 'darwin':
|
|
|
|
prefix = 'lib'
|
|
|
|
ext = 'dylib'
|
|
|
|
elif platform == 'win32':
|
|
|
|
prefix = ''
|
|
|
|
ext = 'dll'
|
|
|
|
else:
|
|
|
|
prefix = 'lib'
|
|
|
|
ext = 'so'
|
|
|
|
|
|
|
|
DEBUG = 'debug'
|
|
|
|
RELEASE = 'release'
|
|
|
|
mode = RELEASE # debug or release
|
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
def run_unit_test():
|
|
|
|
libbolt = Libbolt('target/{}/{}bolt.{}'.format(mode, prefix, ext))
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
b0_cust = 100
|
|
|
|
b0_merch = 10
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
channel_state = libbolt.channel_setup("My New Channel A")
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
print("channel state new: ", len(channel_state))
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-09-16 08:58:10 -07:00
|
|
|
(channel_token, merch_state, channel_state) = libbolt.bidirectional_init_merchant(channel_state, "Bob")
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
print("merch_state: ", len(merch_state))
|
|
|
|
#print("channel_token: ", type(_channel_token))
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-09-16 08:58:10 -07:00
|
|
|
(channel_token, cust_state) = libbolt.bidirectional_init_customer(channel_token, b0_cust, b0_merch, "Alice")
|
2019-08-22 17:52:18 -07:00
|
|
|
print("cust_state: ", len(cust_state))
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
(channel_token, cust_state, com, com_proof) = libbolt.bidirectional_establish_customer_generate_proof(channel_token, cust_state)
|
2019-08-27 23:17:12 -07:00
|
|
|
print("channel token: => ", channel_token)
|
2019-08-22 17:52:18 -07:00
|
|
|
print("com: ", com)
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-09-13 13:16:35 -07:00
|
|
|
cust_state_dict = json.loads(cust_state)
|
2019-10-23 22:33:44 -07:00
|
|
|
channel_id = libbolt.bidirectional_generate_channel_id(channel_token)
|
|
|
|
print("channel ID: ", channel_id)
|
|
|
|
#print("wallet chan ID: ", cust_state_dict["wallet"]["channelId"])
|
|
|
|
|
2019-10-17 15:01:41 -07:00
|
|
|
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)
|
2019-08-22 17:52:18 -07:00
|
|
|
print("close token: ", close_token)
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
(is_token_valid, channel_state, cust_state) = libbolt.bidirectional_establish_customer_verify_close_token(channel_state, cust_state, close_token)
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
pay_token = libbolt.bidirectional_establish_merchant_issue_pay_token(channel_state, com, merch_state)
|
|
|
|
print("pay token: ", pay_token)
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
(is_channel_established, channel_state, cust_state) = libbolt.bidirectional_establish_customer_final(channel_state, cust_state, pay_token)
|
|
|
|
if is_channel_established:
|
|
|
|
print("updated cust_state: ", cust_state)
|
|
|
|
else:
|
|
|
|
print("channel still not established. did you verify close token?")
|
|
|
|
|
|
|
|
# Pay protocol
|
|
|
|
print("Pay protocol...")
|
|
|
|
|
|
|
|
# make a payment
|
|
|
|
amount = 5
|
|
|
|
(payment_proof, new_cust_state) = libbolt.bidirectional_pay_generate_payment_proof(channel_state, cust_state, amount)
|
|
|
|
print("Pay proof: ", len(payment_proof))
|
|
|
|
print("new cust wallet: ", new_cust_state)
|
|
|
|
print("<========================================>")
|
|
|
|
revoked_wpk, _ = libbolt.wtp_get_wallet(new_cust_state)
|
|
|
|
|
|
|
|
(new_close_token, merch_state) = libbolt.bidirectional_pay_verify_payment_proof(channel_state, payment_proof, merch_state)
|
|
|
|
print("Close token: ", new_close_token)
|
|
|
|
print("<========================================>")
|
|
|
|
|
|
|
|
(revoke_token, cust_state) = libbolt.bidirectional_pay_generate_revoke_token(channel_state, cust_state, new_cust_state, new_close_token)
|
|
|
|
print("Revoke token: ", revoke_token)
|
|
|
|
|
|
|
|
(pay_token, merch_state) = libbolt.bidirectional_pay_verify_revoke_token(revoke_token, merch_state)
|
|
|
|
print("Pay token: ", pay_token)
|
|
|
|
|
|
|
|
(cust_state, is_pay_valid) = libbolt.bidirectional_pay_verify_payment_token(channel_state, cust_state, pay_token)
|
|
|
|
print("Pay token is valid: ", is_pay_valid)
|
|
|
|
|
2019-08-26 08:08:49 -07:00
|
|
|
old_cust_close = libbolt.bidirectional_customer_close(channel_state, cust_state)
|
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
# make a payment
|
|
|
|
amount = 10
|
|
|
|
(payment_proof2, new_cust_state2) = libbolt.bidirectional_pay_generate_payment_proof(channel_state, cust_state, amount)
|
|
|
|
print("Pay proof 2: ", len(payment_proof2))
|
|
|
|
print("new cust wallet 2: ", new_cust_state2)
|
|
|
|
print("<========================================>")
|
|
|
|
|
|
|
|
(new_close_token2, merch_state) = libbolt.bidirectional_pay_verify_payment_proof(channel_state, payment_proof2, merch_state)
|
|
|
|
print("Close token 2: ", new_close_token2)
|
|
|
|
print("<========================================>")
|
|
|
|
|
|
|
|
(revoke_token2, cust_state) = libbolt.bidirectional_pay_generate_revoke_token(channel_state, cust_state, new_cust_state2, new_close_token2)
|
|
|
|
print("Revoke token 2: ", revoke_token)
|
|
|
|
|
|
|
|
(pay_token2, merch_state) = libbolt.bidirectional_pay_verify_revoke_token(revoke_token2, merch_state)
|
|
|
|
print("Pay token 2: ", pay_token2)
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
(cust_state, is_pay_valid) = libbolt.bidirectional_pay_verify_payment_token(channel_state, cust_state, pay_token2)
|
|
|
|
print("Pay token is valid: ", is_pay_valid)
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
print("<========================================>")
|
|
|
|
print("<========================================>")
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
cust_close = libbolt.bidirectional_customer_close(channel_state, cust_state)
|
|
|
|
print("Cust close msg: ", cust_close)
|
|
|
|
print("<========================================>")
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-26 08:08:49 -07:00
|
|
|
# normal case: no action b/c cust close is valid
|
|
|
|
address = "11" * 32
|
|
|
|
merch_close = libbolt.bidirectional_merchant_close(channel_state, channel_token, address, cust_close, merch_state)
|
|
|
|
print("Customer initiated - Merch close msg: ", merch_close)
|
|
|
|
print("<========================================>")
|
|
|
|
|
|
|
|
# common case: merchant catches customer double spending
|
|
|
|
address = "11" * 32
|
|
|
|
merch_wpk, merch_close_msg, _ = libbolt.bidirectional_merchant_close(channel_state, channel_token, address, old_cust_close, merch_state)
|
|
|
|
print("Double spend - Merch close msg: ", merch_close_msg)
|
|
|
|
merch_close_valid = libbolt.wtp_verify_merch_close_message(channel_token, merch_wpk, merch_close_msg)
|
|
|
|
print("Merchant close msg valid: ", merch_close_valid)
|
|
|
|
print("<========================================>")
|
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
print("<========================================>")
|
|
|
|
wpk, cust_close_wallet = libbolt.wtp_get_wallet(cust_state)
|
|
|
|
print("wpk = ", wpk)
|
|
|
|
print("close-msg wallet = ", cust_close_wallet)
|
|
|
|
cust_close_token = libbolt.wtp_get_close_token(cust_close)
|
|
|
|
print("close token: ", cust_close_token)
|
2019-08-26 08:08:49 -07:00
|
|
|
print("Valid channel opening: ", libbolt.wtp_verify_cust_close_message(channel_token, wpk, cust_close_wallet, cust_close_token))
|
|
|
|
# TODO: merch close when cust_close represents correct channel state
|
2019-07-31 19:31:10 -07:00
|
|
|
|
2019-08-26 08:08:49 -07:00
|
|
|
print("Invalid channel opening: ", libbolt.wtp_verify_cust_close_message(channel_token, revoked_wpk, cust_close_wallet, cust_close_token))
|
|
|
|
print("<========================================>")
|
2019-08-20 16:09:01 -07:00
|
|
|
|
2019-08-22 17:52:18 -07:00
|
|
|
if __name__ == "__main__":
|
|
|
|
run_unit_test()
|