Skip to content

Commit

Permalink
Use deterministic key channel path for LOCAL -> REMOTE channel opening
Browse files Browse the repository at this point in the history
  • Loading branch information
araspitzu committed Jul 22, 2019
1 parent 00df431 commit 5e3b8f4
Show file tree
Hide file tree
Showing 27 changed files with 364 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ trait EclairWallet {

def getFinalAddress: Future[String]

def makeFundingTx(pubkeyScript: ByteVector, amount: Satoshi, feeRatePerKw: Long): Future[MakeFundingTxResponse]
def makeFundingTx(pubkeyScript: ByteVector, amount: Satoshi, feeRatePerKw: Long, lockUnspent: Boolean = true): Future[MakeFundingTxResponse]

def signTransactionComplete(tx: Transaction): Future[Transaction]

/**
* Committing *must* include publishing the transaction on the network.
Expand Down Expand Up @@ -67,3 +69,4 @@ trait EclairWallet {
}

final case class MakeFundingTxResponse(fundingTx: Transaction, fundingTxOutputIndex: Int, fee: Satoshi)
final case class SignFundingTxResponse(fundingTx: Transaction, fundingTxOutputIndex: Int, fee: Satoshi)
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class BitcoinCoreWallet(rpcClient: BitcoinJsonRPCClient)(implicit ec: ExecutionC

def signTransaction(tx: Transaction): Future[SignTransactionResponse] = signTransaction(Transaction.write(tx).toHex)

override def signTransactionComplete(tx: Transaction): Future[Transaction] = signTransaction(tx).map {
case SignTransactionResponse(signedTx, true) => signedTx
case _ => throw new IllegalStateException("Signed transaction is not complete")
}

def getTransaction(txid: ByteVector32): Future[Transaction] = rpcClient.invoke("getrawtransaction", txid.toString()) collect { case JString(hex) => Transaction.read(hex) }

def publishTransaction(tx: Transaction)(implicit ec: ExecutionContext): Future[String] = publishTransaction(Transaction.write(tx).toHex)
Expand Down Expand Up @@ -89,7 +94,7 @@ class BitcoinCoreWallet(rpcClient: BitcoinJsonRPCClient)(implicit ec: ExecutionC
}
}

override def makeFundingTx(pubkeyScript: ByteVector, amount: Satoshi, feeRatePerKw: Long): Future[MakeFundingTxResponse] = {
override def makeFundingTx(pubkeyScript: ByteVector, amount: Satoshi, feeRatePerKw: Long, lockUnspent: Boolean = true): Future[MakeFundingTxResponse] = {
// partial funding tx
val partialFundingTx = Transaction(
version = 2,
Expand All @@ -98,7 +103,7 @@ class BitcoinCoreWallet(rpcClient: BitcoinJsonRPCClient)(implicit ec: ExecutionC
lockTime = 0)
for {
// we ask bitcoin core to add inputs to the funding tx, and use the specified change address
FundTransactionResponse(unsignedFundingTx, _, fee) <- fundTransaction(partialFundingTx, lockUnspents = true, feeRatePerKw)
FundTransactionResponse(unsignedFundingTx, _, fee) <- fundTransaction(partialFundingTx, lockUnspents = lockUnspent, feeRatePerKw)
// now let's sign the funding tx
SignTransactionResponse(fundingTx, true) <- signTransactionOrUnlock(unsignedFundingTx)
// there will probably be a change output, so we need to find which output is ours
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ class ElectrumEclairWallet(val wallet: ActorRef, chainHash: ByteVector32)(implic

def getXpub: Future[GetXpubResponse] = (wallet ? GetXpub).mapTo[GetXpubResponse]

override def makeFundingTx(pubkeyScript: ByteVector, amount: Satoshi, feeRatePerKw: Long): Future[MakeFundingTxResponse] = {
override def signTransactionComplete(tx: Transaction): Future[Transaction] = {
(wallet ? SignTransaction(tx)).mapTo[SignTransactionResponse].map(_.tx)
}

override def makeFundingTx(pubkeyScript: ByteVector, amount: Satoshi, feeRatePerKw: Long, lockUnspent: Boolean = true): Future[MakeFundingTxResponse] = {
val tx = Transaction(version = 2, txIn = Nil, txOut = TxOut(amount, pubkeyScript) :: Nil, lockTime = 0)
(wallet ? CompleteTransaction(tx, feeRatePerKw)).mapTo[CompleteTransactionResponse].map(response => response match {
case CompleteTransactionResponse(tx1, fee1, None) => MakeFundingTxResponse(tx1, 0, fee1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,11 @@ class ElectrumWallet(seed: ByteVector, client: ActorRef, params: ElectrumWallet.
log.info(s"cancelling txid=${tx.txid}")
stay using persistAndNotify(data.cancelTransaction(tx)) replying CancelTransactionResponse(tx)

case Event(SignTransaction(tx), data) =>
log.info(s"signing txid=${tx.txid}")
val signed = data.signTransaction(tx)
stay replying SignTransactionResponse(signed)

case Event(bc@ElectrumClient.BroadcastTransaction(tx), _) =>
log.info(s"broadcasting txid=${tx.txid}")
client forward bc
Expand Down Expand Up @@ -543,6 +548,9 @@ object ElectrumWallet {
case class IsDoubleSpent(tx: Transaction) extends Request
case class IsDoubleSpentResponse(tx: Transaction, isDoubleSpent: Boolean) extends Response

case class SignTransaction(tx: Transaction) extends Request
case class SignTransactionResponse(tx: Transaction) extends Response

sealed trait WalletEvent
/**
*
Expand Down
Loading

0 comments on commit 5e3b8f4

Please sign in to comment.