From 0b695e6d68bc10328be4b313ba8694e29853ace9 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Wed, 4 Sep 2019 21:09:03 +0200 Subject: [PATCH] init version 0.0.1 of the wrapper node - Added License Apache 2.0 - Added license and reference to code of the pull request https://github.com/ElementsProject/lightning/pull/2223 by https://github.com/renepickhardt --- LICENSE.md | 53 +++++++++ README.md | 15 ++- .../java/jrpc/clightning/CLightningRPC.java | 103 ++++++++++++++++++ .../commands/AbstractRPCCommand.java | 18 ++- .../commands/CLightningCommandGetInfo.java | 24 +++- .../commands/CLightningCommandInvoice.java | 58 ++++++++++ .../commands/CLightningCommandNewAddress.java | 20 +++- .../jrpc/clightning/commands/Command.java | 16 +++ .../commands/CommandRPCMediator.java | 27 ++++- .../jrpc/clightning/commands/IRPCCommand.java | 18 ++- .../exceptions/CLightningException.java | 15 +++ .../exceptions/CommandException.java | 40 +++++++ .../clightning/model/CLightningGetInfo.java | 15 +++ .../clightning/model/CLightningInvoice.java | 48 ++++++++ .../model/CLightningNewAddress.java | 15 +++ .../clightning/model/types/AddressType.java | 24 ++++ .../clightning/model/types/BindingType.java | 15 +++ .../service/socket/CLightningSocket.java | 15 +++ .../jrpc/exceptions/ServiceException.java | 15 +++ src/main/java/jrpc/service/IOUtil.java | 33 ------ .../jrpc/service/converters/IConverter.java | 15 +++ .../service/converters/JsonConverter.java | 17 ++- .../java/jrpc/service/socket/ISocket.java | 15 +++ .../service/socket/UnixDomainSocketRpc.java | 40 ++++++- .../jrpc/wrapper/response/ErrorResponse.java | 18 +++ .../wrapper/response/RPCResponseWrapper.java | 20 ++++ .../wrapper/socket/IWrapperSocketCall.java | 15 +++ .../wrapper/socket/RPCUnixRequestMethod.java | 28 ++++- src/main/resources/logback.xml | 17 +++ .../jrpc/clightning/TestCLightningRPC.java | 59 ++++++++++ .../clightning/TestCommandRPCMediator.java | 75 +++++++++++++ .../java/jrpc/mock/RequestLightningRPC.java | 15 +++ .../jrpc/service/TestJsonConverterDAO.java | 15 +++ 33 files changed, 883 insertions(+), 53 deletions(-) create mode 100644 LICENSE.md create mode 100644 src/main/java/jrpc/clightning/CLightningRPC.java create mode 100644 src/main/java/jrpc/clightning/commands/CLightningCommandInvoice.java create mode 100644 src/main/java/jrpc/clightning/exceptions/CommandException.java create mode 100644 src/main/java/jrpc/clightning/model/CLightningInvoice.java create mode 100644 src/main/java/jrpc/clightning/model/types/AddressType.java delete mode 100644 src/main/java/jrpc/service/IOUtil.java create mode 100644 src/main/java/jrpc/wrapper/response/ErrorResponse.java create mode 100644 src/test/java/jrpc/clightning/TestCLightningRPC.java diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..490b5c7 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,53 @@ +Apache License + +Version 2.0, January 2004 + +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. + +"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: + +You must give any other recipients of the Work or Derivative Works a copy of this License; and +You must cause any modified files to carry prominent notices stating that You changed the files; and +You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and +If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. + +You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. +5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/README.md b/README.md index 9fd8aa3..22cc0b0 100644 --- a/README.md +++ b/README.md @@ -1 +1,14 @@ -JRPClightning +# JRPClightning + +![work in progress](http://s3.amazonaws.com/snd-store/a/26553114/02_02_18_508408464_aab_560x292.jpg) + +This is an simple wrapper for c-lightning rpc, this project is inspiration to this +[pull request](https://github.com/ElementsProject/lightning/pull/2223) by @renepickhardt + +This wrapper aims to be versatile, that is the final version should allow to execute commands even of custom plugins, allowing to write the code for the command. + +## License +The license is available [here](https://www.apache.org/licenses/LICENSE-2.0) + +## Support +Bitcoin Address Vincent: `3BQ8qbn8hLdmBKEjt1Hj1Z6SiDsnjJurfU` diff --git a/src/main/java/jrpc/clightning/CLightningRPC.java b/src/main/java/jrpc/clightning/CLightningRPC.java new file mode 100644 index 0000000..231ccc7 --- /dev/null +++ b/src/main/java/jrpc/clightning/CLightningRPC.java @@ -0,0 +1,103 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package jrpc.clightning; + +import jrpc.clightning.commands.Command; +import jrpc.clightning.commands.CommandRPCMediator; +import jrpc.clightning.exceptions.CLightningException; +import jrpc.clightning.model.CLightningGetInfo; +import jrpc.clightning.model.CLightningInvoice; +import jrpc.clightning.model.CLightningNewAddress; +import jrpc.clightning.model.types.AddressType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import sun.plugin.ClassLoaderInfo; + +/** + * @author https://github.com/vincenzopalazzo + */ +public class CLightningRPC { + + private static final Logger LOGGER = LoggerFactory.getLogger(CLightningRPC.class); + private static final CLightningRPC SINGLETON = new CLightningRPC(); + + public static CLightningRPC getInstance(){ + return SINGLETON; + } + + protected CommandRPCMediator mediatorCommand; + + private CLightningRPC() { + this.mediatorCommand = new CommandRPCMediator(); + } + + + public CLightningGetInfo getInfo(){ + String payload = ""; + CLightningGetInfo resultCommand = (CLightningGetInfo) mediatorCommand.runCommand(Command.GETINFO, payload); + return resultCommand; + } + + public String getNewAddress(AddressType type){ + if(type == null){ + throw new IllegalArgumentException("Type address is null"); + } + String typeString = null; + if(type.equals(AddressType.BECH32)){ + typeString = "bech32"; + }else { + typeString = "p2sh-segwit"; + } + String payload = "addresstype=" + typeString; + LOGGER.debug("Payload: " + payload); + CLightningNewAddress resultCommand = (CLightningNewAddress) mediatorCommand.runCommand(Command.NEWADDR, payload); + if(type.equals(AddressType.BECH32)){ + return resultCommand.getBech32(); + } + return resultCommand.getP2shSegwit(); + } + + //TODO added the invoice for proprieties fallbacks, preimage, exposeprivatechannels + public CLightningInvoice getInvoice(int msatoshi, String label, String description, String expiry){ + if(label == null || label.isEmpty()){ + throw new CLightningException("The method getInvoice have the parameter label null"); + } + if(description == null || description.isEmpty()){ + throw new CLightningException("The method getInvoice have the parameter description null"); + } + if(expiry == null){ + throw new CLightningException("The method getInvoice have the parameter expiry null"); + } + String payload = "msatoshi=" + msatoshi + "_label=" + label + + "_description=" + description; + if(!expiry.isEmpty()){ + payload += "_expiry=" + expiry; + } + CLightningInvoice response = (CLightningInvoice) mediatorCommand.runCommand(Command.INVOICE, payload); + return response; + } + + public CLightningInvoice getInvoice(int msatoshi, String label, String description){ + if(label == null || label.isEmpty()){ + throw new CLightningException("The method getInvoice have the parameter label null"); + } + if(description == null || description.isEmpty()){ + throw new CLightningException("The method getInvoice have the parameter description null"); + } + return this.getInvoice(msatoshi, label, description, ""); + } + +} diff --git a/src/main/java/jrpc/clightning/commands/AbstractRPCCommand.java b/src/main/java/jrpc/clightning/commands/AbstractRPCCommand.java index adeeb65..540e815 100644 --- a/src/main/java/jrpc/clightning/commands/AbstractRPCCommand.java +++ b/src/main/java/jrpc/clightning/commands/AbstractRPCCommand.java @@ -1,5 +1,21 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.clightning.commands; +import jrpc.clightning.exceptions.CommandException; import jrpc.clightning.service.socket.CLightningSocket; import jrpc.exceptions.ServiceException; @@ -17,7 +33,7 @@ public AbstractRPCCommand(String commandName) { } @Override - public T doRPCCommand(CLightningSocket socket, HashMap payload) throws ServiceException { + public T doRPCCommand(CLightningSocket socket, HashMap payload) throws ServiceException, CommandException { if(socket == null || payload == null){ throw new IllegalArgumentException("Methods is/are null"); } diff --git a/src/main/java/jrpc/clightning/commands/CLightningCommandGetInfo.java b/src/main/java/jrpc/clightning/commands/CLightningCommandGetInfo.java index 2bd5c96..a646151 100644 --- a/src/main/java/jrpc/clightning/commands/CLightningCommandGetInfo.java +++ b/src/main/java/jrpc/clightning/commands/CLightningCommandGetInfo.java @@ -1,6 +1,22 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.clightning.commands; import com.google.gson.reflect.TypeToken; +import jrpc.clightning.exceptions.CommandException; import jrpc.clightning.model.CLightningGetInfo; import jrpc.clightning.service.socket.CLightningSocket; import jrpc.exceptions.ServiceException; @@ -25,12 +41,16 @@ public CLightningCommandGetInfo() { } @Override - public CLightningGetInfo doRPCCommand(CLightningSocket socket, HashMap payload) throws ServiceException { + public CLightningGetInfo doRPCCommand(CLightningSocket socket, HashMap payload) throws ServiceException, CommandException { super.doRPCCommand(socket, payload); - RPCUnixRequestMethod wrapper = new RPCUnixRequestMethod(1546, COMMAND_NAME, payload); + RPCUnixRequestMethod wrapper = new RPCUnixRequestMethod(COMMAND_NAME, payload); Type collectionType = new TypeToken>(){}.getType(); RPCResponseWrapper response = (RPCResponseWrapper) socket.doCall(wrapper, collectionType); + if(response.getError() != null){ + throw new CommandException("Error inside command with error code: " + + response.getError().getCode() + "\nMessage: " + response.getError().getMessage()); + } return response.getResult(); } } diff --git a/src/main/java/jrpc/clightning/commands/CLightningCommandInvoice.java b/src/main/java/jrpc/clightning/commands/CLightningCommandInvoice.java new file mode 100644 index 0000000..744fe15 --- /dev/null +++ b/src/main/java/jrpc/clightning/commands/CLightningCommandInvoice.java @@ -0,0 +1,58 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package jrpc.clightning.commands; + +import com.google.gson.reflect.TypeToken; +import jrpc.clightning.exceptions.CommandException; +import jrpc.clightning.model.CLightningInvoice; +import jrpc.clightning.service.socket.CLightningSocket; +import jrpc.exceptions.ServiceException; +import jrpc.wrapper.response.RPCResponseWrapper; +import jrpc.wrapper.socket.RPCUnixRequestMethod; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Type; +import java.util.HashMap; + +/** + * @author https://github.com/vincenzopalazzo + */ +public class CLightningCommandInvoice extends AbstractRPCCommand{ + + private static final Logger LOGGER = LoggerFactory.getLogger(CLightningCommandInvoice.class); + private static final String COMMAND_NAME = "invoice"; + + public CLightningCommandInvoice() { + super(COMMAND_NAME); + } + + @Override + public CLightningInvoice doRPCCommand(CLightningSocket socket, HashMap payload) throws ServiceException, CommandException { + super.doRPCCommand(socket, payload); + + RPCUnixRequestMethod wrapper = new RPCUnixRequestMethod(COMMAND_NAME, payload); + + Type collectionType = new TypeToken>(){}.getType(); + RPCResponseWrapper response = (RPCResponseWrapper) socket.doCall(wrapper, collectionType); + + if(response.getError() != null){ + throw new CommandException("Error inside command with error code: " + + response.getError().getCode() + "\nMessage: " + response.getError().getMessage()); + } + return response.getResult(); + } +} diff --git a/src/main/java/jrpc/clightning/commands/CLightningCommandNewAddress.java b/src/main/java/jrpc/clightning/commands/CLightningCommandNewAddress.java index 66f71e9..30535ef 100644 --- a/src/main/java/jrpc/clightning/commands/CLightningCommandNewAddress.java +++ b/src/main/java/jrpc/clightning/commands/CLightningCommandNewAddress.java @@ -1,6 +1,22 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.clightning.commands; import com.google.gson.reflect.TypeToken; +import jrpc.clightning.exceptions.CommandException; import jrpc.clightning.model.CLightningNewAddress; import jrpc.clightning.service.socket.CLightningSocket; import jrpc.exceptions.ServiceException; @@ -26,10 +42,10 @@ public CLightningCommandNewAddress() { } @Override - public CLightningNewAddress doRPCCommand(CLightningSocket socket, HashMap payload) throws ServiceException { + public CLightningNewAddress doRPCCommand(CLightningSocket socket, HashMap payload) throws ServiceException, CommandException { super.doRPCCommand(socket, payload); - RPCUnixRequestMethod wrapper = new RPCUnixRequestMethod(1546, commandName, payload); + RPCUnixRequestMethod wrapper = new RPCUnixRequestMethod(commandName, payload); String addrType = payload.get(IDENTIFIER_KEY); LOGGER.debug("addr type: " + addrType); Type collectionType = new TypeToken>(){}.getType(); diff --git a/src/main/java/jrpc/clightning/commands/Command.java b/src/main/java/jrpc/clightning/commands/Command.java index e378221..6c45d97 100644 --- a/src/main/java/jrpc/clightning/commands/Command.java +++ b/src/main/java/jrpc/clightning/commands/Command.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.clightning.commands; /** @@ -9,4 +24,5 @@ public enum Command { GETINFO, //Supported NEWADDR, //Supported + INVOICE, } diff --git a/src/main/java/jrpc/clightning/commands/CommandRPCMediator.java b/src/main/java/jrpc/clightning/commands/CommandRPCMediator.java index 80377ce..fa17037 100644 --- a/src/main/java/jrpc/clightning/commands/CommandRPCMediator.java +++ b/src/main/java/jrpc/clightning/commands/CommandRPCMediator.java @@ -1,9 +1,26 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.clightning.commands; import jrpc.clightning.exceptions.CLightningException; +import jrpc.clightning.exceptions.CommandException; import jrpc.clightning.service.socket.CLightningSocket; import jrpc.exceptions.ServiceException; import jrpc.service.socket.ISocket; +import jrpc.wrapper.response.ErrorResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,18 +37,20 @@ public class CommandRPCMediator { private static final String GETINFO = "GETINFO"; private static final String NEWADDR = "NEWADDR"; + private static final String INVOICE = "INVOICE"; private CLightningSocket socket; private Map commands = new HashMap<>(); public CommandRPCMediator() { try { - socket = new CLightningSocket("/media/vincenzo/Maxtor/C-lightning/node/testnet3/lightning-rpc"); + socket = new CLightningSocket("/media/vincenzo/Maxtor/C-lightning/node/testnet/lightning-rpc"); } catch (ServiceException e) { throw new RuntimeException("Configuration socket error, Message error is:" + e.getLocalizedMessage()); } commands.put(GETINFO, new CLightningCommandGetInfo()); commands.put(NEWADDR, new CLightningCommandNewAddress()); + commands.put(INVOICE, new CLightningCommandInvoice()); } public Object runCommand(Command command, String payload) { @@ -40,6 +59,8 @@ public Object runCommand(Command command, String payload) { runCommand = GETINFO; }else if(command.equals(Command.NEWADDR)){ runCommand = NEWADDR; + }else if(command.equals(Command.INVOICE)){ + runCommand = INVOICE; }else{ throw new IllegalArgumentException("Command not found"); } @@ -51,6 +72,8 @@ public Object runCommand(Command command, String payload) { return commandSelected.doRPCCommand(socket, setting); } catch (ServiceException e) { throw new RuntimeException("Service exception with message error\n" + e.getLocalizedMessage()); + } catch (CommandException e) { + throw new RuntimeException("Error whent running the command " + runCommand + ".\n" + e.getLocalizedMessage()); } } @@ -59,7 +82,7 @@ private HashMap decodePayload(String payload) { return new HashMap<>(); } - StringTokenizer tokenizer = new StringTokenizer(payload, ""); + StringTokenizer tokenizer = new StringTokenizer(payload, "_"); LOGGER.debug("Number toke of the payload " + payload + "\nis: " + tokenizer.countTokens()); HashMap configResult = new HashMap<>(); while (tokenizer.hasMoreTokens()) { diff --git a/src/main/java/jrpc/clightning/commands/IRPCCommand.java b/src/main/java/jrpc/clightning/commands/IRPCCommand.java index a3639d0..9582a2c 100644 --- a/src/main/java/jrpc/clightning/commands/IRPCCommand.java +++ b/src/main/java/jrpc/clightning/commands/IRPCCommand.java @@ -1,5 +1,21 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.clightning.commands; +import jrpc.clightning.exceptions.CommandException; import jrpc.clightning.service.socket.CLightningSocket; import jrpc.exceptions.ServiceException; @@ -10,5 +26,5 @@ */ interface IRPCCommand { - T doRPCCommand(CLightningSocket socket, HashMap payload) throws ServiceException; + T doRPCCommand(CLightningSocket socket, HashMap payload) throws ServiceException, CommandException; } diff --git a/src/main/java/jrpc/clightning/exceptions/CLightningException.java b/src/main/java/jrpc/clightning/exceptions/CLightningException.java index 7001038..dfc6ab3 100644 --- a/src/main/java/jrpc/clightning/exceptions/CLightningException.java +++ b/src/main/java/jrpc/clightning/exceptions/CLightningException.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.clightning.exceptions; /** diff --git a/src/main/java/jrpc/clightning/exceptions/CommandException.java b/src/main/java/jrpc/clightning/exceptions/CommandException.java new file mode 100644 index 0000000..c909391 --- /dev/null +++ b/src/main/java/jrpc/clightning/exceptions/CommandException.java @@ -0,0 +1,40 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package jrpc.clightning.exceptions; + +/** + * @author https://github.com/vincenzopalazzo + */ +public class CommandException extends Exception{ + public CommandException() { + } + + public CommandException(String message) { + super(message); + } + + public CommandException(String message, Throwable cause) { + super(message, cause); + } + + public CommandException(Throwable cause) { + super(cause); + } + + public CommandException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/jrpc/clightning/model/CLightningGetInfo.java b/src/main/java/jrpc/clightning/model/CLightningGetInfo.java index 841c3ef..285e1cf 100644 --- a/src/main/java/jrpc/clightning/model/CLightningGetInfo.java +++ b/src/main/java/jrpc/clightning/model/CLightningGetInfo.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.clightning.model; import jrpc.clightning.model.types.BindingType; diff --git a/src/main/java/jrpc/clightning/model/CLightningInvoice.java b/src/main/java/jrpc/clightning/model/CLightningInvoice.java new file mode 100644 index 0000000..ca3e273 --- /dev/null +++ b/src/main/java/jrpc/clightning/model/CLightningInvoice.java @@ -0,0 +1,48 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package jrpc.clightning.model; + +import com.google.gson.annotations.SerializedName; + +/** + * @author https://github.com/vincenzopalazzo + */ +public class CLightningInvoice { + + @SerializedName("payment_hash") + private String paymentHash; + @SerializedName("expires_at") + private String expiresAt; + private String bolt11; + @SerializedName("warning_capacity") + private String warningCapacity; + + public String getPaymentHash() { + return paymentHash; + } + + public String getExpiresAt() { + return expiresAt; + } + + public String getBolt11() { + return bolt11; + } + + public String getWarningCapacity() { + return warningCapacity; + } +} diff --git a/src/main/java/jrpc/clightning/model/CLightningNewAddress.java b/src/main/java/jrpc/clightning/model/CLightningNewAddress.java index 1e659c0..6e6bd0a 100644 --- a/src/main/java/jrpc/clightning/model/CLightningNewAddress.java +++ b/src/main/java/jrpc/clightning/model/CLightningNewAddress.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.clightning.model; import com.google.gson.annotations.SerializedName; diff --git a/src/main/java/jrpc/clightning/model/types/AddressType.java b/src/main/java/jrpc/clightning/model/types/AddressType.java new file mode 100644 index 0000000..6e59a77 --- /dev/null +++ b/src/main/java/jrpc/clightning/model/types/AddressType.java @@ -0,0 +1,24 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package jrpc.clightning.model.types; + +/** + * @author https://github.com/vincenzopalazzo + */ +public enum AddressType { + P2SH_SEGWIT, + BECH32, +} diff --git a/src/main/java/jrpc/clightning/model/types/BindingType.java b/src/main/java/jrpc/clightning/model/types/BindingType.java index 9cdc05b..a31c6ee 100644 --- a/src/main/java/jrpc/clightning/model/types/BindingType.java +++ b/src/main/java/jrpc/clightning/model/types/BindingType.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.clightning.model.types; /** diff --git a/src/main/java/jrpc/clightning/service/socket/CLightningSocket.java b/src/main/java/jrpc/clightning/service/socket/CLightningSocket.java index 01a0c52..e0bf24e 100644 --- a/src/main/java/jrpc/clightning/service/socket/CLightningSocket.java +++ b/src/main/java/jrpc/clightning/service/socket/CLightningSocket.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.clightning.service.socket; import jrpc.exceptions.ServiceException; diff --git a/src/main/java/jrpc/exceptions/ServiceException.java b/src/main/java/jrpc/exceptions/ServiceException.java index cd9af18..ca3e8ef 100644 --- a/src/main/java/jrpc/exceptions/ServiceException.java +++ b/src/main/java/jrpc/exceptions/ServiceException.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.exceptions; /** diff --git a/src/main/java/jrpc/service/IOUtil.java b/src/main/java/jrpc/service/IOUtil.java deleted file mode 100644 index 06d839d..0000000 --- a/src/main/java/jrpc/service/IOUtil.java +++ /dev/null @@ -1,33 +0,0 @@ -package jrpc.service; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; - -/** - * @author https://github.com/vincenzopalazzo - */ -//TODO can be removed -public class IOUtil { - - public static String readFullyAsString(InputStream inputStream, String encoding) - throws IOException { - return readFully(inputStream).toString(encoding); - } - - public static byte[] readFullyAsBytes(InputStream inputStream) - throws IOException { - return readFully(inputStream).toByteArray(); - } - - private static ByteArrayOutputStream readFully(InputStream inputStream) - throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - int length = 0; - while ((length = inputStream.read(buffer)) != -1) { - baos.write(buffer, 0, length); - } - return baos; - } -} diff --git a/src/main/java/jrpc/service/converters/IConverter.java b/src/main/java/jrpc/service/converters/IConverter.java index 399b836..8426643 100644 --- a/src/main/java/jrpc/service/converters/IConverter.java +++ b/src/main/java/jrpc/service/converters/IConverter.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.service.converters; import jrpc.exceptions.ServiceException; diff --git a/src/main/java/jrpc/service/converters/JsonConverter.java b/src/main/java/jrpc/service/converters/JsonConverter.java index 0326353..32fc54c 100644 --- a/src/main/java/jrpc/service/converters/JsonConverter.java +++ b/src/main/java/jrpc/service/converters/JsonConverter.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.service.converters; import com.google.gson.*; @@ -49,7 +64,7 @@ public Object deserialization(InputStream inputStream, Type type) throws Service reader = new JsonReader( new InputStreamReader(inputStream, ENCODING_DEFAULT)); Gson gson = gsonBuilder.create(); response = gson.fromJson(reader, type); - reader.close(); + //reader.close(); //TODO can I release the connection open with socket? }catch (Exception ex){ throw new ServiceException("Exception inside the method deserialization to " + this.getClass().getSimpleName() + "\nMessage: " + ex.getLocalizedMessage()); diff --git a/src/main/java/jrpc/service/socket/ISocket.java b/src/main/java/jrpc/service/socket/ISocket.java index 2225da6..a84dec1 100644 --- a/src/main/java/jrpc/service/socket/ISocket.java +++ b/src/main/java/jrpc/service/socket/ISocket.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.service.socket; import jrpc.exceptions.ServiceException; diff --git a/src/main/java/jrpc/service/socket/UnixDomainSocketRpc.java b/src/main/java/jrpc/service/socket/UnixDomainSocketRpc.java index 573edd3..b6cce77 100644 --- a/src/main/java/jrpc/service/socket/UnixDomainSocketRpc.java +++ b/src/main/java/jrpc/service/socket/UnixDomainSocketRpc.java @@ -1,5 +1,23 @@ package jrpc.service.socket; - +/** + * UnixDomainSocketRpc the base class to handle communication between + * JLightning and the c-lightning node over the UnixDomainSocket + * + * This file is basically a port of the pylightning python client library + * that comes with c-lightning. + * + * The Author of this Java Client library is Rene Pickhardt and Vincent Palazzo + * He also holds the copyright of this file. The library is licensed with + * a BSD-style license. Have a look at the LICENSE file. + * + * @author Rene Pickhardt + * If you like this library consider a donation via bitcoin or the lightning + * network at http://ln.rene-pickhardt.de + * + * @author https://github.com/vincenzopalazzo + * vincenzopalazzo apported the modific from this pull request https://github.com/ElementsProject/lightning/pull/2223 + * + */ import jrpc.exceptions.ServiceException; import jrpc.wrapper.socket.IWrapperSocketCall; import jrpc.service.converters.IConverter; @@ -12,9 +30,6 @@ import java.io.*; import java.lang.reflect.Type; -/** - * @author https://github.com/vincenzopalazzo - */ public abstract class UnixDomainSocketRpc implements ISocket { private static final Logger LOGGER = LoggerFactory.getLogger(UnixDomainSocketRpc.class); @@ -24,10 +39,11 @@ public abstract class UnixDomainSocketRpc implements ISocket { protected InputStream inputStream; protected OutputStream outputStream; protected IConverter converterJson; + protected String pathSocket; public UnixDomainSocketRpc(String pathSocket) throws ServiceException { File file = new File(pathSocket); - + this.pathSocket = pathSocket; try { this.socket = AFUNIXSocket.newInstance(); this.socket.connect(new AFUNIXSocketAddress(file)); @@ -45,6 +61,14 @@ public Object doCall(IWrapperSocketCall wrapperSocket, Type typeResult) throws S if (wrapperSocket == null) { throw new IllegalArgumentException("The argument is null"); } + if(socket.isClosed()){ + try { + socket.connect(new AFUNIXSocketAddress(new File(pathSocket))); + } catch (IOException e) { + throw new ServiceException("Exception generated to doCall method of the class " + this.getClass().getSimpleName() + + " with message\n" + e.getLocalizedMessage()); + } + } String serializationForm = converterJson.serialization(wrapperSocket); LOGGER.debug("Request: \n" + serializationForm); try { @@ -56,6 +80,12 @@ public Object doCall(IWrapperSocketCall wrapperSocket, Type typeResult) throws S + " with message\n" + e.getLocalizedMessage()); } Object o = converterJson.deserialization(inputStream, typeResult); + try { + socket.close(); + } catch (IOException e) { + throw new ServiceException("Exception generated to doCall method of the class " + this.getClass().getSimpleName() + + " with message\n" + e.getLocalizedMessage()); + } LOGGER.debug("Response\n" + converterJson.serialization(o)); return o; } diff --git a/src/main/java/jrpc/wrapper/response/ErrorResponse.java b/src/main/java/jrpc/wrapper/response/ErrorResponse.java new file mode 100644 index 0000000..60124b8 --- /dev/null +++ b/src/main/java/jrpc/wrapper/response/ErrorResponse.java @@ -0,0 +1,18 @@ +package jrpc.wrapper.response; + +/** + * @author https://github.com/vincenzopalazzo + */ +public class ErrorResponse { + + private int code; + private String message; + + public int getCode() { + return code; + } + + public String getMessage() { + return message; + } +} diff --git a/src/main/java/jrpc/wrapper/response/RPCResponseWrapper.java b/src/main/java/jrpc/wrapper/response/RPCResponseWrapper.java index 08957bc..7805c01 100644 --- a/src/main/java/jrpc/wrapper/response/RPCResponseWrapper.java +++ b/src/main/java/jrpc/wrapper/response/RPCResponseWrapper.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.wrapper.response; /** @@ -8,6 +23,7 @@ public class RPCResponseWrapper { private float jsonrpc; private int id; private T result; + private ErrorResponse error; public float getJsonrpc() { return jsonrpc; @@ -20,4 +36,8 @@ public int getId() { public T getResult() { return result; } + + public ErrorResponse getError() { + return error; + } } diff --git a/src/main/java/jrpc/wrapper/socket/IWrapperSocketCall.java b/src/main/java/jrpc/wrapper/socket/IWrapperSocketCall.java index 71cb3c5..c06471a 100644 --- a/src/main/java/jrpc/wrapper/socket/IWrapperSocketCall.java +++ b/src/main/java/jrpc/wrapper/socket/IWrapperSocketCall.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.wrapper.socket; import java.util.Map; diff --git a/src/main/java/jrpc/wrapper/socket/RPCUnixRequestMethod.java b/src/main/java/jrpc/wrapper/socket/RPCUnixRequestMethod.java index 248061c..3b8885a 100644 --- a/src/main/java/jrpc/wrapper/socket/RPCUnixRequestMethod.java +++ b/src/main/java/jrpc/wrapper/socket/RPCUnixRequestMethod.java @@ -1,18 +1,31 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.wrapper.socket; + import java.util.Map; -/** - * @author https://github.com/vincenzopalazzo - */ public class RPCUnixRequestMethod implements IWrapperSocketCall{ private int id = 1; private String method; private Map params; - public RPCUnixRequestMethod(int id, String method, Map params) { - this.id = id; + public RPCUnixRequestMethod(String method, Map params) { + this.id = getRandomNumber(); this.method = method; this.params = params; } @@ -28,4 +41,9 @@ public String getMethod() { public Map getParams() { return params; } + + + public int getRandomNumber(){ + return (int)(Math.random()*100); + } } diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 20f6f88..4a263b9 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -1,3 +1,20 @@ + diff --git a/src/test/java/jrpc/clightning/TestCLightningRPC.java b/src/test/java/jrpc/clightning/TestCLightningRPC.java new file mode 100644 index 0000000..3a859b9 --- /dev/null +++ b/src/test/java/jrpc/clightning/TestCLightningRPC.java @@ -0,0 +1,59 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package jrpc.clightning; + +import jrpc.clightning.model.CLightningGetInfo; +import jrpc.clightning.model.CLightningInvoice; +import jrpc.clightning.model.types.AddressType; +import junit.framework.TestCase; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author https://github.com/vincenzopalazzo + */ +public class TestCLightningRPC { + + private static final Logger LOGGER = LoggerFactory.getLogger(TestCLightningRPC.class); + + @Test + public void testCommandGetInfo(){ + CLightningGetInfo infoNode = CLightningRPC.getInstance().getInfo(); + TestCase.assertEquals(infoNode.getNetwork(), "testnet"); + } + + @Test + public void testCommandNewAddress(){ + String newAddress = CLightningRPC.getInstance().getNewAddress(AddressType.BECH32); + LOGGER.debug("Address: " + newAddress); + TestCase.assertTrue(newAddress.contains("tb")); + } + + @Test + public void testCommandGetInvoiceOne(){ + CLightningInvoice invoice = CLightningRPC.getInstance().getInvoice(1000, "Hello this is an test " + Math.random(), "description"); + LOGGER.debug("invoice: " + invoice.getBolt11()); + TestCase.assertNotNull(invoice.getBolt11()); + } + + @Test + public void testCommandGetInvoiceTwo(){ + CLightningInvoice invoice = CLightningRPC.getInstance().getInvoice(1000, "This is an test " + Math.random(), "description", "1w"); + LOGGER.debug("invoice: " + invoice.getBolt11()); + TestCase.assertNotNull(invoice.getBolt11()); + } +} diff --git a/src/test/java/jrpc/clightning/TestCommandRPCMediator.java b/src/test/java/jrpc/clightning/TestCommandRPCMediator.java index 7c83faf..276ac52 100644 --- a/src/test/java/jrpc/clightning/TestCommandRPCMediator.java +++ b/src/test/java/jrpc/clightning/TestCommandRPCMediator.java @@ -1,7 +1,23 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.clightning; import jrpc.clightning.commands.Command; import jrpc.clightning.commands.CommandRPCMediator; +import jrpc.clightning.model.CLightningGetInfo; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; @@ -25,6 +41,20 @@ public void testParsingPayloadOne(){ TestCase.assertNotNull(response); } + @Test(expected = RuntimeException.class) + public void testParsingPayloadTwo(){ + String payload = "pippo"; + CLightningGetInfo response = (CLightningGetInfo) mediator.runCommand(Command.GETINFO, payload); + TestCase.assertNotNull(response); + } + + @Test(expected = RuntimeException.class) + public void testParsingPayloadThree(){ + String payload = "pippo=pippo"; + CLightningGetInfo response = (CLightningGetInfo) mediator.runCommand(Command.GETINFO, payload); + TestCase.assertNotNull(response); + } + @Test public void testParsingPayloadAddressTypeBech32(){ String payload = "addresstype=bech32"; @@ -39,4 +69,49 @@ public void testParsingPayloadAddressTypeP2SH(){ Object response = mediator.runCommand(Command.NEWADDR, payload); TestCase.assertNotNull(response); } + + @Test + public void testParsingPayloadInvoiceOne(){ + String label = "I am vincent and this is my personal test" + (int)(Math.random()*100);; + String description = "This is a minimal expression for command invoice?"; + String payload = "msatoshi=1000_label=" + label + "_description=" + description; + Object response = mediator.runCommand(Command.INVOICE, payload); + TestCase.assertNotNull(response); + } + + @Test(expected = RuntimeException.class) + public void testParsingPayloadInvoiceTwo(){ + String description = "This is a minimal expression for command invoice?"; + String payload = "msatoshi=1000_description=" + description; + Object response = mediator.runCommand(Command.INVOICE, payload); + TestCase.assertNotNull(response); + } + + @Test(expected = RuntimeException.class) + public void testParsingPayloadInvoiceThree(){ + String description = "This is a minimal expression for command invoice?"; + String payload = "msatoshi=1000_label" + "_description=" + description; + Object response = mediator.runCommand(Command.INVOICE, payload); + TestCase.assertNotNull(response); + } + + @Test + public void testParsingPayloadInvoiceFour(){ + String label = "I am vincent and this is my personal test" + (int)(Math.random()*100);; + String description = "This is a minimal expression for command invoice?"; + String payload = "msatoshi=1000_label=" + label + "_description=" + description + "_expiry=1w"; + Object response = mediator.runCommand(Command.INVOICE, payload); + TestCase.assertNotNull(response); + } + + @Test(expected = RuntimeException.class) + public void testParsingPayloadInvoiceFive(){ + String label = "I am vincent and this is my personal test" + (int)(Math.random()*100);; + String description = "This is a minimal expression for command invoice?"; + String payload = "msatoshi=1000_label=" + label + "_description=" + description + "_expiry=1p"; + Object response = mediator.runCommand(Command.INVOICE, payload); + TestCase.assertNotNull(response); + } + + //TODO test invoice on the propriety fallbacks, preimage, exposeprivatechannels } diff --git a/src/test/java/jrpc/mock/RequestLightningRPC.java b/src/test/java/jrpc/mock/RequestLightningRPC.java index 2f47bf3..d3145b0 100644 --- a/src/test/java/jrpc/mock/RequestLightningRPC.java +++ b/src/test/java/jrpc/mock/RequestLightningRPC.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.mock; /** diff --git a/src/test/java/jrpc/service/TestJsonConverterDAO.java b/src/test/java/jrpc/service/TestJsonConverterDAO.java index e2bf19a..1a40593 100644 --- a/src/test/java/jrpc/service/TestJsonConverterDAO.java +++ b/src/test/java/jrpc/service/TestJsonConverterDAO.java @@ -1,3 +1,18 @@ +/** + * Copyright 2019 https://github.com/vincenzopalazzo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package jrpc.service; import jrpc.exceptions.ServiceException;