1
0
Fork 0
mirror of https://github.com/ACINQ/eclair.git synced 2025-02-23 14:40:34 +01:00

first commit

This commit is contained in:
pm47 2016-10-17 17:39:42 +02:00
parent cc066369f1
commit fae0663c45
5 changed files with 122 additions and 0 deletions

45
eclair-wire/pom.xml Normal file
View file

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>fr.acinq.eclair</groupId>
<artifactId>eclair_2.11</artifactId>
<version>0.2-SNAPSHOT</version>
</parent>
<artifactId>eclair-wire_2.11</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/scala</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.scodec</groupId>
<artifactId>scodec-core_${scala.version.short}</artifactId>
</dependency>
</dependencies>
</project>

View file

@ -0,0 +1,21 @@
package fr.acinq.eclair.wire
import fr.acinq.eclair.wire.PacketType._
import scodec.Codec
import scodec.codecs._
/**
* Created by PM on 17/10/2016.
*/
object Packet {
implicit val codec: Codec[Packet] = {
("packet_type" | Codec[PacketType]) ::
(("data_length" | uint16) >>:~ { length =>
("data" | vectorOfN(provide(length), uint8)).hlist
})
}.as[Packet]
}
case class Packet(packetType: PacketType, dataLength: Int, data: Vector[Int]) {
require(dataLength == data.length)
}

View file

@ -0,0 +1,36 @@
package fr.acinq.eclair.wire
import scodec.bits.BitVector
import scodec.{Attempt, Codec, DecodeResult, Err, SizeBound}
import scodec.codecs._
/**
* Created by PM on 17/10/2016.
*/
object PacketType extends Enumeration {
type PacketType = Value
val ADD = Value(0)
val FAIL = Value(1)
val FULFILL = Value(2)
implicit val commandTypeCodec: Codec[PacketType] =
mappedEnum(uint(2), PacketType.values.map(v => (v, v.id)).toMap)
}
import fr.acinq.eclair.wire.PacketType._
class PacketTypeCodec() extends Codec[PacketType] {
override def sizeBound = SizeBound.exact(1)
override def encode(a: PacketType) = Attempt.successful(BitVector.empty)
override def decode(buffer: BitVector) =
buffer.acquire(1) match {
case Left(e) => Attempt.failure(Err.insufficientBits(1, buffer.size))
case Right(b) if b(0) == 0 => Attempt.successful(DecodeResult(ADD, buffer))
case Right(b) if b(0) == 1 => Attempt.successful(DecodeResult(FAIL, buffer))
case Right(b) if b(0) == 2 => Attempt.successful(DecodeResult(FULFILL, buffer))
}
override def toString = s"PacketTypeCodec"
}

View file

@ -0,0 +1,14 @@
package fr.acinq.eclair.wire
import scodec.Codec
import scodec.bits._
/**
* Created by PM on 17/10/2016.
*/
object Test extends App {
val bin = hex"0x00010000000000".bits
println(Codec[Packet].decode(bin))
}

View file

@ -8,6 +8,7 @@
<packaging>pom</packaging>
<modules>
<module>eclair-wire</module>
<module>lightning-types</module>
<module>eclair-node</module>
</modules>
@ -160,6 +161,11 @@
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.scodec</groupId>
<artifactId>scodec-core_${scala.version.short}</artifactId>
<version>1.10.2</version>
</dependency>
<dependency>
<groupId>fr.acinq</groupId>
<artifactId>bitcoin-lib_${scala.version.short}</artifactId>