Skip to content

Commit

Permalink
Release version 0.1.5
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Aug 21, 2020
1 parent 278523f commit 6223141
Show file tree
Hide file tree
Showing 33 changed files with 892 additions and 309 deletions.
541 changes: 339 additions & 202 deletions LICENSE.md

Large diffs are not rendered by default.

56 changes: 54 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<img src="https://i.ibb.co/tKG2Kkq/final-Icon.png" alt="final-Icon" border="0">
</p>

This is a simple wrapper for c-lightning rpc, this project is inspired by this
This is a wrapper for c-lightning rpc, this project is inspired by this
[pull request](https://github.com/ElementsProject/lightning/pull/2223) by @renepickhardt.

The wrapper aims to be versatile, the final version should allow the user to execute commands even of custom plugins, allowing them to write the code for the command.
Expand Down Expand Up @@ -82,6 +82,21 @@ The library support now the plugins and you can subscribe the plugin to notify w

### Example

### Plugin example

```java
@RPCMethod(
name = "annotation_hello",
description = "Annotation plugin"
)
public void hello(CLightningPlugin plugin, CLightningJsonObject request, CLightningJsonObject response) {
log(CLightningLevelLog.WARNING, request.toString());
response.add("type", "random");
}
```

### Notification example

```java
@Subscription(notification = "invoice_creation")
public void doInvoiceCreation(CLightningJsonObject data){
Expand All @@ -90,5 +105,42 @@ The library support now the plugins and you can subscribe the plugin to notify w
}
```

### Hook example

```java
@Hook(hook = "rpc_command")
public void logAllRPCCommand(CLightningPlugin plugin, CLightningJsonObject request, CLightningJsonObject response) {
log(CLightningLevelLog.WARNING, request.toString());
response.add("result", "continue");
}
```

## Support
For any additional support you can use this bitcoin address: `3BQ8qbn8hLdmBKEjt1Hj1Z6SiDsnjJurfU`
For any additional support you can use this bitcoin address: ``
If you like the library and want support it, please considerer to donate with the following system
- [liberapay.com/vincenzopalazzo](https://liberapay.com/vincenzopalazzo)
- [3BQ8qbn8hLdmBKEjt1Hj1Z6SiDsnjJurfU](bitcoin:3BQ8qbn8hLdmBKEjt1Hj1Z6SiDsnjJurfU)
- [Github support](https://github.com/sponsors/vincenzopalazzo)

## License

<div align="center">
<img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" width="150" height="150"/>
</div>

This is a wrapper for c-lightning RPC interface.
Copyright (C) 2020 Vincenzo Palazzo vincenzopalazzodev@gmail.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
*51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
6 changes: 3 additions & 3 deletions docs/index-all.html
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ <h2 class="title">P</h2>
<dl>
<dt><span class="memberNameLink"><a href="jrpc/clightning/CLightningRPC.html#parsingArrayElement-java.lang.String-java.lang.String:A-">parsingArrayElement(String, String[])</a></span> - Method in class jrpc.clightning.<a href="jrpc/clightning/CLightningRPC.html" title="class in jrpc.clightning">CLightningRPC</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="jrpc/clightning/CLightningRPC.html#parsingBitcoinOutputs-jrpc.clightning.model.types.BitcoinOutput...-">parsingBitcoinOutputs(BitcoinOutput...)</a></span> - Method in class jrpc.clightning.<a href="jrpc/clightning/CLightningRPC.html" title="class in jrpc.clightning">CLightningRPC</a></dt>
<dt><span class="memberNameLink"><a href="jrpc/clightning/CLightningRPC.html#parsingBitcoinOutputs-jrpc.clightning.model.types.bitcoin.BitcoinOutput...-">parsingBitcoinOutputs(BitcoinOutput...)</a></span> - Method in class jrpc.clightning.<a href="jrpc/clightning/CLightningRPC.html" title="class in jrpc.clightning">CLightningRPC</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="jrpc/service/socket/UnixDomainSocketRpc.html#pathSocket">pathSocket</a></span> - Variable in class jrpc.service.socket.<a href="jrpc/service/socket/UnixDomainSocketRpc.html" title="class in jrpc.service.socket">UnixDomainSocketRpc</a></dt>
<dd>&nbsp;</dd>
Expand Down Expand Up @@ -719,9 +719,9 @@ <h2 class="title">T</h2>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="jrpc/clightning/CLightningRPC.html#txDiscard-java.lang.String-">txDiscard(String)</a></span> - Method in class jrpc.clightning.<a href="jrpc/clightning/CLightningRPC.html" title="class in jrpc.clightning">CLightningRPC</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="jrpc/clightning/CLightningRPC.html#txPrepare-java.lang.String-java.lang.String-jrpc.clightning.model.types.BitcoinOutput...-">txPrepare(String, String, BitcoinOutput...)</a></span> - Method in class jrpc.clightning.<a href="jrpc/clightning/CLightningRPC.html" title="class in jrpc.clightning">CLightningRPC</a></dt>
<dt><span class="memberNameLink"><a href="jrpc/clightning/CLightningRPC.html#txPrepare-java.lang.String-java.lang.String-jrpc.clightning.model.types.bitcoin.BitcoinOutput...-">txPrepare(String, String, BitcoinOutput...)</a></span> - Method in class jrpc.clightning.<a href="jrpc/clightning/CLightningRPC.html" title="class in jrpc.clightning">CLightningRPC</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="jrpc/clightning/CLightningRPC.html#txPrepare-jrpc.clightning.model.types.BitcoinOutput...-">txPrepare(BitcoinOutput...)</a></span> - Method in class jrpc.clightning.<a href="jrpc/clightning/CLightningRPC.html" title="class in jrpc.clightning">CLightningRPC</a></dt>
<dt><span class="memberNameLink"><a href="jrpc/clightning/CLightningRPC.html#txPrepare-jrpc.clightning.model.types.bitcoin.BitcoinOutput...-">txPrepare(BitcoinOutput...)</a></span> - Method in class jrpc.clightning.<a href="jrpc/clightning/CLightningRPC.html" title="class in jrpc.clightning">CLightningRPC</a></dt>
<dd>&nbsp;</dd>
</dl>
<a name="I:U">
Expand Down
12 changes: 6 additions & 6 deletions docs/jrpc/clightning/CLightningRPC.html
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ <h3>Method Summary</h3>
</tr>
<tr id="i27" class="rowColor">
<td class="colFirst"><code>protected java.lang.String</code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../jrpc/clightning/CLightningRPC.html#parsingBitcoinOutputs-jrpc.clightning.model.types.BitcoinOutput...-">parsingBitcoinOutputs</a></span>(<a href="../../jrpc/clightning/model/types/BitcoinOutput.html" title="class in jrpc.clightning.model.types">BitcoinOutput</a>...&nbsp;bitcoinOutputs)</code>&nbsp;</td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../jrpc/clightning/CLightningRPC.html#parsingBitcoinOutputs-jrpc.clightning.model.types.bitcoin.BitcoinOutput...-">parsingBitcoinOutputs</a></span>(<a href="../../jrpc/clightning/model/types/BitcoinOutput.html" title="class in jrpc.clightning.model.types">BitcoinOutput</a>...&nbsp;bitcoinOutputs)</code>&nbsp;</td>
</tr>
<tr id="i28" class="altColor">
<td class="colFirst"><code><a href="../../jrpc/clightning/model/CLightningPay.html" title="class in jrpc.clightning.model">CLightningPay</a></code></td>
Expand All @@ -310,11 +310,11 @@ <h3>Method Summary</h3>
</tr>
<tr id="i31" class="rowColor">
<td class="colFirst"><code><a href="../../jrpc/clightning/model/CLightningBitcoinTx.html" title="class in jrpc.clightning.model">CLightningBitcoinTx</a></code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../jrpc/clightning/CLightningRPC.html#txPrepare-jrpc.clightning.model.types.BitcoinOutput...-">txPrepare</a></span>(<a href="../../jrpc/clightning/model/types/BitcoinOutput.html" title="class in jrpc.clightning.model.types">BitcoinOutput</a>...&nbsp;bitcoinOutputs)</code>&nbsp;</td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../jrpc/clightning/CLightningRPC.html#txPrepare-jrpc.clightning.model.types.bitcoin.BitcoinOutput...-">txPrepare</a></span>(<a href="../../jrpc/clightning/model/types/BitcoinOutput.html" title="class in jrpc.clightning.model.types">BitcoinOutput</a>...&nbsp;bitcoinOutputs)</code>&nbsp;</td>
</tr>
<tr id="i32" class="altColor">
<td class="colFirst"><code><a href="../../jrpc/clightning/model/CLightningBitcoinTx.html" title="class in jrpc.clightning.model">CLightningBitcoinTx</a></code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../jrpc/clightning/CLightningRPC.html#txPrepare-java.lang.String-java.lang.String-jrpc.clightning.model.types.BitcoinOutput...-">txPrepare</a></span>(java.lang.String&nbsp;feerate,
<td class="colLast"><code><span class="memberNameLink"><a href="../../jrpc/clightning/CLightningRPC.html#txPrepare-java.lang.String-java.lang.String-jrpc.clightning.model.types.bitcoin.BitcoinOutput...-">txPrepare</a></span>(java.lang.String&nbsp;feerate,
java.lang.String&nbsp;minconf,
<a href="../../jrpc/clightning/model/types/BitcoinOutput.html" title="class in jrpc.clightning.model.types">BitcoinOutput</a>...&nbsp;bitcoinOutputs)</code>&nbsp;</td>
</tr>
Expand Down Expand Up @@ -494,7 +494,7 @@ <h4>autoCleanInvoice</h4>
java.lang.String&nbsp;expiredBy)</pre>
</li>
</ul>
<a name="txPrepare-java.lang.String-java.lang.String-jrpc.clightning.model.types.BitcoinOutput...-">
<a name="txPrepare-java.lang.String-java.lang.String-jrpc.clightning.model.types.bitcoin.BitcoinOutput...-">
<!-- -->
</a>
<ul class="blockList">
Expand All @@ -505,7 +505,7 @@ <h4>txPrepare</h4>
<a href="../../jrpc/clightning/model/types/BitcoinOutput.html" title="class in jrpc.clightning.model.types">BitcoinOutput</a>...&nbsp;bitcoinOutputs)</pre>
</li>
</ul>
<a name="txPrepare-jrpc.clightning.model.types.BitcoinOutput...-">
<a name="txPrepare-jrpc.clightning.model.types.bitcoin.BitcoinOutput...-">
<!-- -->
</a>
<ul class="blockList">
Expand Down Expand Up @@ -717,7 +717,7 @@ <h4>decodePay</h4>
java.lang.String&nbsp;description)</pre>
</li>
</ul>
<a name="parsingBitcoinOutputs-jrpc.clightning.model.types.BitcoinOutput...-">
<a name="parsingBitcoinOutputs-jrpc.clightning.model.types.bitcoin.BitcoinOutput...-">
<!-- -->
</a>
<ul class="blockList">
Expand Down
2 changes: 1 addition & 1 deletion docs/jrpc/clightning/model/types/BitcoinOutput.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ <h2 title="Class BitcoinOutput" class="title">Class BitcoinOutput</h2>
<li>java.lang.Object</li>
<li>
<ul class="inheritance">
<li>jrpc.clightning.model.types.BitcoinOutput</li>
<li>jrpc.clightning.model.types.bitcoin.BitcoinOutput</li>
</ul>
</li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion docs/jrpc/clightning/model/types/CLightningChannelId.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ <h2 title="Class CLightningChannelId" class="title">Class CLightningChannelId</h
<li>java.lang.Object</li>
<li>
<ul class="inheritance">
<li>jrpc.clightning.model.types.CLightningChannelId</li>
<li>jrpc.clightning.model.types.CLightningConnect</li>
</ul>
</li>
</ul>
Expand Down
96 changes: 67 additions & 29 deletions src/main/java/jrpc/clightning/CLightningRPC.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
/**
* Copyright 2019-2020 Vincenzo Palazzo [email protected]
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
* This is a wrapper for c-lightning RPC interface.
* Copyright (C) 2020 Vincenzo Palazzo [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package jrpc.clightning;

Expand All @@ -22,6 +25,7 @@
import jrpc.clightning.exceptions.CLightningException;
import jrpc.clightning.model.*;
import jrpc.clightning.model.types.*;
import jrpc.clightning.model.types.bitcoin.BitcoinOutput;
import jrpc.clightning.service.socket.CLightningSocket;
import jrpc.exceptions.ServiceException;
import jrpc.service.CLightningLogger;
Expand Down Expand Up @@ -383,7 +387,7 @@ public CLightningListFounds listFunds() {
return (CLightningListFounds) mediatorCommand.runCommand(Command.LISTFOUNDS, payloadString);
}

public String connect(String id, String host, String port) {
public CLightningConnect connect(String id, String host, String port) {
if (id == null || id.trim().isEmpty()) {
throw new CLightningException("The method connect have the parameter id is null or empty");
}
Expand All @@ -410,11 +414,11 @@ public String connect(String id, String host, String port) {

String payloadString = payload.toString();
CLightningLogger.getInstance().debug(TAG, "Payload for command connect is: " + payloadString);
CLightningChannelId channelId = (CLightningChannelId) mediatorCommand.runCommand(Command.CONNECT, payloadString);
return channelId.getId();
CLightningConnect channelId = (CLightningConnect) mediatorCommand.runCommand(Command.CONNECT, payloadString);
return channelId;
}

public String connect(String id) {
public CLightningConnect connect(String id) {
if (id == null || id.trim().isEmpty()) {
throw new CLightningException("The method connect have the parameter id is null or empty");
}
Expand Down Expand Up @@ -621,38 +625,74 @@ public CLightningListPays listPays() {
return this.listPays("");
}

public CLightningInvoice waitInvoice(String label){
if(label == null){
public CLightningInvoice waitInvoice(String label) {
if (label == null) {
throw new CLightningException("The argument inside the method listPays is null");
}
Map<String, Object> payload = new HashMap<>();
payload.put("label", label.trim());
return (CLightningInvoice) mediatorCommand.runCommand(Command.WAITINVOICE, payload);
}

public CLightningSendPay waitSendPay(String paymentHash){
public CLightningSendPay waitSendPay(String paymentHash) {
return this.waitSendPays(paymentHash, 0, 0);
}

public CLightningSendPay waitSendPays(String paymentHash, int timeout, int partid){
if(paymentHash == null){
public CLightningSendPay waitSendPays(String paymentHash, int timeout, int partid) {
if (paymentHash == null) {
throw new CLightningException("The argument {paymentHash} inside the method waitSendPays is null");
}
doCheckPositiveNumber("waitSendPays", "timeout", timeout);
doCheckPositiveNumber("waitSendPays", "partid", timeout);

Map<String, Object> payload = new HashMap<>();
payload.put("payment_hash", paymentHash.trim());
if(timeout > 0){
if (timeout > 0) {
payload.put("timeout", timeout);
}
if (partid > 0){
if (partid > 0) {
payload.put("partid", partid);
}
return (CLightningSendPay) mediatorCommand.runCommand(Command.WAITSENDPAY, payload);
}

public CLightningListNodes listNodes(String nodeId) {
doCheckString("listNodes", "nodeId", nodeId, true);
Map<String, Object> payload = new HashMap<>();
if (!nodeId.trim().isEmpty()) {
payload.put("id", nodeId);
}
return (CLightningListNodes) mediatorCommand.runCommand(Command.LISTNODES, payload);
}

public CLightningListNodes listNodes() {
return this.listNodes("");
}

public CLightningPing ping(String nodeId, int len, int pongBytes) {
doCheckString("ping", "ping", nodeId, true);
doCheckPositiveNumber("ping", "len", len);
doCheckPositiveNumber("ping", "pongBytes", pongBytes);
Map<String, Object> payload = new HashMap<>();

if (!nodeId.isEmpty()) {
payload.put("id", nodeId);
}

payload.put("len", len);
payload.put("pongbytes", pongBytes);
return (CLightningPing) mediatorCommand.runCommand(Command.PING, payload);
}

public CLightningPing ping(String nodeId) {
return this.ping(nodeId, 128, 128);
}

public CLightningListTransactions listTransactions(){
return (CLightningListTransactions) mediatorCommand.runCommand(Command.LISTTRANSACTIONS, new HashMap<>());
}

//Register commands
public void registerCommand(ICommandKey key, IRPCCommand command) {
if (key == null || command == null) {
throw new IllegalArgumentException("Key and/or command null");
Expand All @@ -666,13 +706,11 @@ public <T> T runRegisterCommand(ICommandKey key, HashMap<String, Object> payload

//Utility methods
private void doCheckString(String command, String name, String value, boolean onlyNull) {
if (value == null || value.isEmpty()) {
if (value == null || (value.isEmpty() && !onlyNull)) {
String message = "Propriety " + name + " in the command " + command;
if (name == null) {
if (value == null) {
message += " null";
} else if (onlyNull) {
return; //Good for the library
} else {
} else if (value.isEmpty() && !onlyNull) {
message += " empty";
}
throw new CLightningException(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@
package jrpc.clightning.commands;

import com.google.gson.reflect.TypeToken;
import jrpc.clightning.model.types.CLightningChannelId;
import jrpc.clightning.model.types.CLightningConnect;
import jrpc.wrapper.response.RPCResponseWrapper;

import java.lang.reflect.Type;

/**
* @author https://github.com/vincenzopalazzo
*/
public class CLightningCommandConnect extends AbstractRPCCommand<CLightningChannelId>{

private static final String COMMAND_NAME = "connect";
public class CLightningCommandConnect extends AbstractRPCCommand<CLightningConnect>{

public CLightningCommandConnect() {
super(COMMAND_NAME);
super(Command.CONNECT.getCommandKey());
}

@Override
protected Type toTypeFromClass() {
return new TypeToken<RPCResponseWrapper<CLightningChannelId>>(){}.getType();
return new TypeToken<RPCResponseWrapper<CLightningConnect>>(){}.getType();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package jrpc.clightning.commands;

import com.google.gson.reflect.TypeToken;
import jrpc.clightning.model.CLightningListTransactions;
import jrpc.clightning.model.types.CLightningPing;
import jrpc.wrapper.response.RPCResponseWrapper;

import java.lang.reflect.Type;

public class CLightningCommandListTransactions extends AbstractRPCCommand<CLightningListTransactions> {

public CLightningCommandListTransactions() {
super(Command.LISTTRANSACTIONS.getCommandKey());
}

@Override
protected Type toTypeFromClass() {
return new TypeToken<RPCResponseWrapper<CLightningListTransactions>>(){}.getType();
}
}
19 changes: 19 additions & 0 deletions src/main/java/jrpc/clightning/commands/CLightningCommandPing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package jrpc.clightning.commands;

import com.google.gson.reflect.TypeToken;
import jrpc.clightning.model.types.CLightningPing;
import jrpc.wrapper.response.RPCResponseWrapper;

import java.lang.reflect.Type;

public class CLightningCommandPing extends AbstractRPCCommand<CLightningPing>{

public CLightningCommandPing() {
super(Command.PING.getCommandKey());
}

@Override
protected Type toTypeFromClass() {
return new TypeToken<RPCResponseWrapper<CLightningPing>>(){}.getType();
}
}
Loading

0 comments on commit 6223141

Please sign in to comment.