diff --git a/Makefile b/Makefile index a02b212..7d28ab5 100644 --- a/Makefile +++ b/Makefile @@ -42,5 +42,9 @@ cpptests: @LD_LIBRARY_PATH=./target/release/ ./cpp_test @rm cpp_test +gotests: + cargo +nightly build --release + go test go/libbolt.go go/libbolt_test.go + clean: cargo +nightly clean diff --git a/go/libbolt.go b/go/libbolt.go index cc86c97..bd113fc 100644 --- a/go/libbolt.go +++ b/go/libbolt.go @@ -18,6 +18,7 @@ type setupResp struct { ComProof string `json:"com_proof"` IsTokenValid bool `json:"is_token_valid,string"` IsEstablished bool `json:"is_established,string"` + IsPayValid bool `json:"is_pay_valid,string"` Payment string `json:"payment"` CloseToken string `json:"close_token"` RevokeToken string `json:"revoke_token"` @@ -104,7 +105,7 @@ func BidirectionalPayVerifyRevokeToken(serRevokeToken string, serMerchState stri func BidirectionalPayVerifyPaymentToken(serChannelState string, serCustState string, serPayToken string) (string, bool) { resp := C.GoString(C.ffishim_bidirectional_pay_verify_payment_token(C.CString(serChannelState), C.CString(serCustState), C.CString(serPayToken))) r := processCResponse(resp) - return r.CustState, r.IsTokenValid + return r.CustState, r.IsPayValid } func BidirectionalCustomerClose(serChannelState string, serCustState string) string { diff --git a/go/libbolt_test.go b/go/libbolt_test.go index 0a740c9..2b70723 100644 --- a/go/libbolt_test.go +++ b/go/libbolt_test.go @@ -41,3 +41,42 @@ func Test_Establish(t *testing.T) { assert.True(t, isChannelEstablished) } +func Test_Pay(t *testing.T) { + b0Cust := 1000 + b0Merch := 100 + channelState, channelToken, merchState, custState := setup(b0Cust, b0Merch) + channelToken, custState, com, comProof := BidirectionalEstablishCustomerGenerateProof(channelToken, custState) + closeToken := BidirectionalEstablishMerchantIssueCloseToken(channelState, com, comProof, b0Cust, b0Merch, merchState) + _, channelState, custState = BidirectionalVerifyCloseToken(channelState, custState, closeToken) + payToken := BidirectionalEstablishMerchantIssuePayToken(channelState, com, merchState) + _, channelState, custState = BidirectionalEstablishCustomerFinal(channelState, custState, payToken) + + payment, newCustState := BidirectionalPayGeneratePaymentProof(channelState, custState, 10) + closeToken, merchState = BidirectionalPayVerifyPaymentProof(channelState, payment, merchState) + revokeToken, custState := BidirectionalPayGenerateRevokeToken(channelState, custState, newCustState, closeToken) + payToken, merchState = BidirectionalPayVerifyRevokeToken(revokeToken, merchState) + custState, isTokenValid := BidirectionalPayVerifyPaymentToken(channelState, custState, payToken) + assert.True(t, isTokenValid) +} + +func Test_Close(t *testing.T) { + b0Cust := 1000 + b0Merch := 100 + channelState, channelToken, merchState, custState := setup(b0Cust, b0Merch) + channelToken, custState, com, comProof := BidirectionalEstablishCustomerGenerateProof(channelToken, custState) + closeToken := BidirectionalEstablishMerchantIssueCloseToken(channelState, com, comProof, b0Cust, b0Merch, merchState) + _, channelState, custState = BidirectionalVerifyCloseToken(channelState, custState, closeToken) + payToken := BidirectionalEstablishMerchantIssuePayToken(channelState, com, merchState) + _, channelState, custState = BidirectionalEstablishCustomerFinal(channelState, custState, payToken) + + payment, newCustState := BidirectionalPayGeneratePaymentProof(channelState, custState, 10) + closeToken, merchState = BidirectionalPayVerifyPaymentProof(channelState, payment, merchState) + revokeToken, custState := BidirectionalPayGenerateRevokeToken(channelState, custState, newCustState, closeToken) + payToken, merchState = BidirectionalPayVerifyRevokeToken(revokeToken, merchState) + custState, _ = BidirectionalPayVerifyPaymentToken(channelState, custState, payToken) + + custClose := BidirectionalCustomerClose(channelState, custState) + _, _, err := BidirectionalMerchantClose(channelState, channelToken, "onChainAddress", custClose, merchState) + assert.Equal(t, "merchant_close - Could not find entry for wpk & revoke token pair. Valid close!", err) +} + diff --git a/src/ffishim.rs b/src/ffishim.rs index d7adb09..3cf9125 100644 --- a/src/ffishim.rs +++ b/src/ffishim.rs @@ -15,7 +15,7 @@ pub mod ffishim { use std::alloc::handle_alloc_error; fn error_message(s: String) -> *mut c_char { - let ser = ["{\'error\':", serde_json::to_string(&s).unwrap().as_str(), "}"].concat(); + let ser = ["{\'error\':\'", &s, "\'}"].concat(); let cser = CString::new(ser).unwrap(); cser.into_raw() }