From 4cd081c11c4c08e6f25d48a3487d6bcd9b2475ef Mon Sep 17 00:00:00 2001 From: Anton Kumaigorodski Date: Fri, 25 Aug 2017 14:38:14 +0300 Subject: [PATCH] Add API method to accept requests with custom amount (closes #134) (#135) * Add API method to accept requests with custom amount - can be used to send up to 2x higher amount than requested according to BOLT11 - should be used for payment requests without amounts * Refactor 'send' method in API * Add comments and description for 'send' API method --- README.md | 1 + .../src/main/scala/fr/acinq/eclair/api/Service.scala | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aa5d4b1b6..ce29de20b 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ option | description | default value receive | amountMsat, description | generate a payment request for a given amount send | amountMsat, paymentHash, nodeId | send a payment to a lightning node send | paymentRequest | send a payment to a lightning node using a BOLT11 payment request + send | paymentRequest, amountMsat | send a payment to a lightning node using a BOLT11 payment request and a custom amount close | channelId | close a channel close | channelId, scriptPubKey (optional) | close a channel and send the funds to the given scriptPubKey help | | display available methods diff --git a/eclair-core/src/main/scala/fr/acinq/eclair/api/Service.scala b/eclair-core/src/main/scala/fr/acinq/eclair/api/Service.scala index c32d071b8..80acf918b 100644 --- a/eclair-core/src/main/scala/fr/acinq/eclair/api/Service.scala +++ b/eclair-core/src/main/scala/fr/acinq/eclair/api/Service.scala @@ -99,10 +99,16 @@ trait Service extends Logging { (paymentHandler ? ReceivePayment(MilliSatoshi(amountMsat.toLong), description)).mapTo[PaymentRequest].map(PaymentRequest.write) case JsonRPCBody(_, _, "send", JInt(amountMsat) :: JString(paymentHash) :: JString(nodeId) :: Nil) => (paymentInitiator ? SendPayment(amountMsat.toLong, paymentHash, PublicKey(nodeId))).mapTo[PaymentResult] - case JsonRPCBody(_, _, "send", JString(paymentRequest) :: Nil) => + case JsonRPCBody(_, _, "send", JString(paymentRequest) :: rest) => for { req <- Future(PaymentRequest.read(paymentRequest)) - res <- (paymentInitiator ? SendPayment(req.amount.getOrElse(throw new RuntimeException("request without amounts are not supported")).amount, req.paymentHash, req.nodeId)).mapTo[PaymentResult] + amount = (req.amount, rest) match { + case (Some(amt), Nil) => amt.amount + case (Some(_), JInt(amt) :: Nil) => amt.toLong // overriding payment request amount with the one provided + case (None, JInt(amt) :: Nil) => amt.toLong // amount wasn't specified in request, using custom one + case (None, Nil) => throw new RuntimeException("you need to manually specify an amount for this payment request") + } + res <- (paymentInitiator ? SendPayment(amount, req.paymentHash, req.nodeId)).mapTo[PaymentResult] } yield res case JsonRPCBody(_, _, "close", JString(channelId) :: JString(scriptPubKey) :: Nil) => getChannel(channelId).flatMap(_ ? CMD_CLOSE(scriptPubKey = Some(scriptPubKey))).mapTo[String] @@ -120,6 +126,7 @@ trait Service extends Logging { "receive (amountMsat, description): generate a payment request for a given amount", "send (amountMsat, paymentHash, nodeId): send a payment to a lightning node", "send (paymentRequest): send a payment to a lightning node using a BOLT11 payment request", + "send (paymentRequest, amountMsat): send a payment to a lightning node using a BOLT11 payment request and a custom amount", "close (channelId): close a channel", "close (channelId, scriptPubKey): close a channel and send the funds to the given scriptPubKey", "help: display this message"))