Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
Moved 'receivePayment()' into ThunderContext
Browse files Browse the repository at this point in the history
  • Loading branch information
matsjj committed May 17, 2016
1 parent b511fc2 commit 9a0c60a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
import javafx.scene.layout.HBox;
import net.glxn.qrgen.QRCode;
import net.glxn.qrgen.image.ImageType;
import network.thunder.core.communication.layer.high.payments.PaymentSecret;
import network.thunder.core.etc.Tools;
import network.thunder.core.helper.PaymentRequest;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer;
import java.util.ResourceBundle;

public class ReceiveMoneyRequestController {
Expand Down Expand Up @@ -58,8 +57,6 @@ void cancel (ActionEvent event) {
overlayUI.done();
}

PaymentSecret secret = null;

@FXML
void initialize () {
assert topHBox != null : "fx:id=\"topHBox\" was not injected: check your FXML file 'receive_money_request.fxml'.";
Expand All @@ -78,20 +75,14 @@ void initialize () {

public void update () {

System.out.println(Tools.bytesToHex(Main.node.pubKeyServer.getPubKey()));

if (secret == null) {
secret = new PaymentSecret(Tools.getRandomByte(20));
Main.dbHandler.addPaymentSecret(secret);
System.out.println("HASH: " + Tools.bytesToHex(secret.hash));
}
PaymentRequest paymentRequest = Main.thunderContext.receivePayment(getAmount());

try {

byte[] payload = getPayload();
byte[] payload = paymentRequest.getPayload();

FieldAddress.setText(Tools.bytesToHex(payload));
FieldHash.setText(Tools.bytesToHex(secret.hash));
FieldHash.setText(Tools.bytesToHex(paymentRequest.paymentSecret.hash));

System.out.println(Tools.bytesToHex(payload));

Expand All @@ -116,17 +107,6 @@ public void update () {

}

public byte[] getPayload () {

ByteBuffer buffer = ByteBuffer.allocate(33 + 8 + 20);

buffer.putLong(getAmount());
buffer.put(secret.hash);
buffer.put(Main.node.pubKeyServer.getPubKey());

return buffer.array();
}

private long getAmount () {
return Long.parseLong(amount.getText());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import network.thunder.core.communication.processor.exceptions.LNPaymentException;
import network.thunder.core.database.DBHandler;
import network.thunder.core.etc.Tools;
import network.thunder.core.helper.PaymentRequest;
import network.thunder.core.helper.callback.ChannelOpenListener;
import network.thunder.core.helper.callback.ConnectionListener;
import network.thunder.core.helper.callback.ResultCommand;
Expand Down Expand Up @@ -117,6 +118,16 @@ public void makePayment (byte[] receiver, long amount, PaymentSecret secret, Res
}
}

public PaymentRequest receivePayment (long amount) {
PaymentSecret secret = new PaymentSecret(Tools.getRandomByte(20));
dbHandler.addPaymentSecret(secret);
PaymentRequest paymentRequest = new PaymentRequest();
paymentRequest.amount = amount;
paymentRequest.paymentSecret = secret;
paymentRequest.pubkey = node.pubKeyServer.getPubKey();
return paymentRequest;
}

public void closeChannel (Channel channel, ResultCommand resultCommand) {
contextFactory.getChannelManager().closeChannel(channel, resultCommand);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package network.thunder.core.helper;

import network.thunder.core.communication.layer.high.payments.PaymentSecret;

import java.nio.ByteBuffer;

public class PaymentRequest {
public long amount;
public PaymentSecret paymentSecret;
public byte[] pubkey;

public byte[] getPayload () {

ByteBuffer buffer = ByteBuffer.allocate(33 + 8 + 20);

buffer.putLong(amount);
buffer.put(paymentSecret.hash);
buffer.put(pubkey);

return buffer.array();
}
}

0 comments on commit 9a0c60a

Please sign in to comment.