Skip to content

Commit

Permalink
Use new derivation scheme for fundee scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
araspitzu committed Jun 5, 2019
1 parent 2a329d6 commit 3b25ea5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
26 changes: 22 additions & 4 deletions eclair-core/src/main/scala/fr/acinq/eclair/channel/Channel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,37 @@ object Channel {
case class RemoteError(e: Error) extends ChannelError
// @formatter:on

def makeChannelKeyPathFromInput(fundingInput: TxIn): KeyPath = {
def fourByteGroupsFromSha(input: ByteVector): List[Long] = {
// split the SHA into 8 groups of 4 bytes and convert to uint32
val List(h0, h1, h2, h3, h4, h5, h6, h7) = Crypto.sha256(fundingInput.outPoint.hash).toArray.grouped(4).map(ByteVector(_).toLong(signed = false)).toList
KeyPath(Seq(47, 2, h0, h1, h2, h3, h4, h5, h6, h7, 0))
Crypto.sha256(input).toArray.grouped(4).map(ByteVector(_).toLong(signed = false)).toList
}

def makeFunderChannelParams(nodeParams: NodeParams, defaultFinalScriptPubKey: ByteVector, isFunder: Boolean, fundingSatoshis: Long, fundingInput: TxIn): LocalParams = {
require(isFunder, s"Wrong params for isFunder=$isFunder")

val channelKeyPath = makeChannelKeyPathFromInput(fundingInput)
val List(h0, h1, h2, h3, h4, h5, h6, h7) = fourByteGroupsFromSha(fundingInput.outPoint.hash)
val channelKeyPath = KeyPath(Seq(47, 2, h0, h1, h2, h3, h4, h5, h6, h7, 0))

makeChannelParams(nodeParams, defaultFinalScriptPubKey, isFunder, fundingSatoshis, Left(channelKeyPath))
}

def makeFundeeChannelParams(nodeParams: NodeParams, open: OpenChannel, defaultFinalScriptPubKey: ByteVector, fundingSatoshis: Long): LocalParams = {

val List(h0, h1, h2, h3, h4, h5, h6, h7) = fourByteGroupsFromSha(ByteVector.view(s"${Globals.blockCount} || 0".getBytes))
val publicKeyPath = KeyPath(Seq(47, 2, h0, h1, h2, h3, h4, h5, h6, h7, 2))

val localFundingPubkey = nodeParams.keyManager.fundingPublicKey(publicKeyPath).publicKey
val fundingPubkeyScript = Script.write(Script.pay2wsh(Scripts.multiSig2of2(localFundingPubkey, open.fundingPubkey)))

val List(s0, s1, s2, s3, s4, s5, s6, s7) = fourByteGroupsFromSha(fundingPubkeyScript)
val pointsKeyPath = KeyPath(Seq(47, 2, s0, s1, s2, s3, s4, s5, s6, s7, 1))

val channelKeyPaths = KeyPathFundee(publicKeyPath, pointsKeyPath)

makeChannelParams(nodeParams, defaultFinalScriptPubKey, isFunder = false, fundingSatoshis, Right(channelKeyPaths))
}


def makeChannelParams(nodeParams: NodeParams, defaultFinalScriptPubKey: ByteVector, isFunder: Boolean, fundingSatoshis: Long, channelKeyPath: Either[DeterministicWallet.KeyPath, KeyPathFundee]): LocalParams = {
LocalParams(
nodeParams.nodeId,
Expand Down
12 changes: 4 additions & 8 deletions eclair-core/src/main/scala/fr/acinq/eclair/io/Peer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ class Peer(nodeParams: NodeParams, remoteNodeId: PublicKey, authenticator: Actor
d.transport ! TransportHandler.ReadAck(msg)
d.channels.get(TemporaryChannelId(msg.temporaryChannelId)) match {
case None =>
val (channel, localParams) = createNewChannel(nodeParams, funder = false, fundingSatoshis = msg.fundingSatoshis, origin_opt = None)

val channel = spawnChannel(nodeParams, origin_opt = None)
val defaultFinalScriptPubkey = Helpers.getFinalScriptPubKey(wallet, nodeParams.chainHash)
val localParams = Channel.makeFundeeChannelParams(nodeParams, msg, defaultFinalScriptPubkey, msg.fundingSatoshis)
val temporaryChannelId = msg.temporaryChannelId
log.info(s"accepting a new channel to $remoteNodeId temporaryChannelId=$temporaryChannelId")

Expand Down Expand Up @@ -486,13 +489,6 @@ class Peer(nodeParams: NodeParams, remoteNodeId: PublicKey, authenticator: Actor
case DISCONNECTED -> _ if nodeParams.autoReconnect && stateData.address_opt.isDefined => cancelTimer(RECONNECT_TIMER)
}

def createNewChannel(nodeParams: NodeParams, funder: Boolean, fundingSatoshis: Long, origin_opt: Option[ActorRef]): (ActorRef, LocalParams) = {
val defaultFinalScriptPubKey = Helpers.getFinalScriptPubKey(wallet, nodeParams.chainHash)
val localParams = makeChannelParams(nodeParams, defaultFinalScriptPubKey, funder, fundingSatoshis)
val channel = spawnChannel(nodeParams, origin_opt)
(channel, localParams)
}

def spawnChannel(nodeParams: NodeParams, origin_opt: Option[ActorRef]): ActorRef = {
val channel = context.actorOf(Channel.props(nodeParams, wallet, remoteNodeId, watcher, router, relayer, origin_opt))
context watch channel
Expand Down

0 comments on commit 3b25ea5

Please sign in to comment.