Skip to content

Commit

Permalink
Update dlc before release (#2543)
Browse files Browse the repository at this point in the history
* Pulled down DLC data structure work

* Responded to Ben's review

* Gave Vector types names

* Pulled down Chris' work
  • Loading branch information
nkohen committed Feb 2, 2021
1 parent 940fce4 commit c4edcb2
Show file tree
Hide file tree
Showing 37 changed files with 2,891 additions and 1,066 deletions.
Expand Up @@ -11,9 +11,8 @@ import org.bitcoins.core.crypto.{
import org.bitcoins.core.currency.{Bitcoins, Satoshis}
import org.bitcoins.core.hd.AddressType
import org.bitcoins.core.number.UInt32
import org.bitcoins.core.protocol.dlc.DLCMessage._
import org.bitcoins.core.protocol.dlc.DLCStatus._
import org.bitcoins.core.protocol.dlc.{DLCState, DLCStatus, DLCTimeouts}
import org.bitcoins.core.protocol.dlc._
import org.bitcoins.core.protocol.tlv._
import org.bitcoins.core.protocol.transaction.{Transaction, TransactionOutPoint}
import org.bitcoins.core.protocol.{BitcoinAddress, BlockStamp}
Expand Down Expand Up @@ -102,8 +101,8 @@ object Picklers {
implicit val contractInfoPickler: ReadWriter[ContractInfo] =
readwriter[String].bimap(_.hex, ContractInfo.fromHex)

implicit val contractInfoTLVPickler: ReadWriter[ContractInfoTLV] =
readwriter[String].bimap(_.hex, ContractInfoTLV.fromHex)
implicit val contractInfoTLVPickler: ReadWriter[ContractInfoV0TLV] =
readwriter[String].bimap(_.hex, ContractInfoV0TLV.fromHex)

implicit val schnorrDigitalSignaturePickler: ReadWriter[
SchnorrDigitalSignature] =
Expand Down Expand Up @@ -268,11 +267,13 @@ object Picklers {

implicit val claimedW: Writer[Claimed] = writer[Obj].comap { claimed =>
import claimed._
val outcomeJs = outcome match {
case EnumOutcome(outcome) =>
Str(outcome)
case UnsignedNumericOutcome(digits) =>
Arr.from(digits.map(num => Num(num)))
val (oraclesJs, outcomesJs) = oracleOutcome match {
case EnumOracleOutcome(oracles, outcome) =>
(Arr.from(oracles.map(o => Str(o.announcement.hex))),
Str(outcome.outcome))
case numeric: NumericOracleOutcome =>
(Arr.from(numeric.oracles.map(_.hex)),
Arr.from(numeric.outcomes.map(o => Arr.from(o.digits))))
}

Obj(
Expand All @@ -294,18 +295,21 @@ object Picklers {
"fundingTxId" -> Str(fundingTxId.hex),
"closingTxId" -> Str(closingTxId.hex),
"oracleSigs" -> oracleSigs.map(sig => Str(sig.hex)),
"outcome" -> outcomeJs
"outcomes" -> outcomesJs,
"oracles" -> oraclesJs
)
}

implicit val remoteClaimedW: Writer[RemoteClaimed] =
writer[Obj].comap { remoteClaimed =>
import remoteClaimed._
val outcomeJs = outcome match {
case EnumOutcome(outcome) =>
Str(outcome)
case UnsignedNumericOutcome(digits) =>
Arr.from(digits.map(num => Num(num)))
val (oraclesJs, outcomesJs) = oracleOutcome match {
case EnumOracleOutcome(oracles, outcome) =>
(Arr.from(oracles.map(o => Str(o.announcement.hex))),
Str(outcome.outcome))
case numeric: NumericOracleOutcome =>
(Arr.from(numeric.oracles.map(_.hex)),
Arr.from(numeric.outcomes.map(o => Arr.from(o.digits))))
}

Obj(
Expand All @@ -327,7 +331,8 @@ object Picklers {
"fundingTxId" -> Str(fundingTxId.hex),
"closingTxId" -> Str(closingTxId.hex),
"oracleSigs" -> oracleSigs.map(sig => Str(sig.hex)),
"outcome" -> outcomeJs
"outcomes" -> outcomesJs,
"oracles" -> oraclesJs
)
}

Expand Down Expand Up @@ -368,8 +373,7 @@ object Picklers {
val state = DLCState.fromString(obj("state").str)
val isInitiator = obj("isInitiator").bool
val tempContractId = Sha256Digest(obj("tempContractId").str)
val oracleInfo = OracleInfo(obj("oracleInfo").str)
val contractInfoTLV = ContractInfoTLV(obj("contractInfo").str)
val contractInfoTLV = ContractInfoV0TLV(obj("contractInfo").str)
val contractMaturity =
BlockStamp(UInt32(obj("contractMaturity").num.toLong))
val contractTimeout = BlockStamp(UInt32(obj("contractTimeout").num.toLong))
Expand All @@ -385,12 +389,32 @@ object Picklers {
.map(value => SchnorrDigitalSignature(value.str))
.toVector

lazy val outcomeJs = obj("outcome")
lazy val outcome = outcomeJs.strOpt match {
case Some(value) => EnumOutcome(value)
lazy val outcomesJs = obj("outcomes")
lazy val outcomes = outcomesJs.strOpt match {
case Some(value) => Vector(EnumOutcome(value))
case None =>
val digits = outcomeJs.arr.map(value => value.num.toInt)
UnsignedNumericOutcome(digits.toVector)
outcomesJs.arr.map { outcomeJs =>
val digits = outcomeJs.arr.map(value => value.num.toInt)
UnsignedNumericOutcome(digits.toVector)
}.toVector
}

lazy val oraclesJs = obj("oracles")
lazy val oracles = oraclesJs.arr.map { value =>
val announcementTLV = OracleAnnouncementTLV(value.str)
SingleOracleInfo(announcementTLV)
}.toVector

lazy val oracleOutcome = outcomes.head match {
case outcome: EnumOutcome =>
EnumOracleOutcome(oracles.asInstanceOf[Vector[EnumSingleOracleInfo]],
outcome)
case UnsignedNumericOutcome(_) =>
val numericOutcomes =
outcomes.map(_.asInstanceOf[UnsignedNumericOutcome])
val numericOracles =
oracles.map(_.asInstanceOf[NumericSingleOracleInfo])
NumericOracleOutcome(numericOracles.zip(numericOutcomes))
}

state match {
Expand All @@ -399,7 +423,6 @@ object Picklers {
paramHash,
isInitiator,
tempContractId,
oracleInfo,
ContractInfo.fromTLV(contractInfoTLV),
DLCTimeouts(contractMaturity, contractTimeout),
feeRate,
Expand All @@ -412,7 +435,6 @@ object Picklers {
isInitiator,
tempContractId,
contractId,
oracleInfo,
ContractInfo.fromTLV(contractInfoTLV),
DLCTimeouts(contractMaturity, contractTimeout),
feeRate,
Expand All @@ -425,7 +447,6 @@ object Picklers {
isInitiator,
tempContractId,
contractId,
oracleInfo,
ContractInfo.fromTLV(contractInfoTLV),
DLCTimeouts(contractMaturity, contractTimeout),
feeRate,
Expand All @@ -438,7 +459,6 @@ object Picklers {
isInitiator,
tempContractId,
contractId,
oracleInfo,
ContractInfo.fromTLV(contractInfoTLV),
DLCTimeouts(contractMaturity, contractTimeout),
feeRate,
Expand All @@ -452,7 +472,6 @@ object Picklers {
isInitiator,
tempContractId,
contractId,
oracleInfo,
ContractInfo.fromTLV(contractInfoTLV),
DLCTimeouts(contractMaturity, contractTimeout),
feeRate,
Expand All @@ -466,7 +485,6 @@ object Picklers {
isInitiator,
tempContractId,
contractId,
oracleInfo,
ContractInfo.fromTLV(contractInfoTLV),
DLCTimeouts(contractMaturity, contractTimeout),
feeRate,
Expand All @@ -475,7 +493,7 @@ object Picklers {
fundingTxId,
closingTxId,
oracleSigs,
outcome
oracleOutcome
)
case DLCState.RemoteClaimed =>
require(oracleSigs.size == 1,
Expand All @@ -485,7 +503,6 @@ object Picklers {
isInitiator,
tempContractId,
contractId,
oracleInfo,
ContractInfo.fromTLV(contractInfoTLV),
DLCTimeouts(contractMaturity, contractTimeout),
feeRate,
Expand All @@ -494,15 +511,14 @@ object Picklers {
fundingTxId,
closingTxId,
oracleSigs.head,
outcome
oracleOutcome
)
case DLCState.Refunded =>
Refunded(
paramHash,
isInitiator,
tempContractId,
contractId,
oracleInfo,
ContractInfo.fromTLV(contractInfoTLV),
DLCTimeouts(contractMaturity, contractTimeout),
feeRate,
Expand All @@ -522,6 +538,9 @@ object Picklers {
implicit val extPrivateKeyPickler: ReadWriter[ExtPrivateKey] =
readwriter[String].bimap(ExtKey.toString, ExtPrivateKey.fromString)

implicit val oracleAttestmentTLV: ReadWriter[OracleAttestmentTLV] =
readwriter[String].bimap(_.hex, OracleAttestmentTLV.fromHex)

implicit val ecPublicKeyPickler: ReadWriter[ECPublicKey] =
readwriter[String].bimap(_.hex, ECPublicKey.fromHex)

Expand Down
16 changes: 12 additions & 4 deletions app/cli/src/main/scala/org/bitcoins/cli/CliReaders.scala
Expand Up @@ -4,7 +4,6 @@ import java.io.File
import java.nio.file.Path
import java.time.{Instant, ZoneId, ZonedDateTime}
import org.bitcoins.commons.jsonmodels.bitcoind.RpcOpts.LockUnspentOutputParameter
import org.bitcoins.core.protocol.dlc.DLCMessage._
import org.bitcoins.core.api.wallet.CoinSelectionAlgo
import org.bitcoins.core.config.{NetworkParameters, Networks}
import org.bitcoins.core.crypto.{ExtPrivateKey, MnemonicCode}
Expand All @@ -13,6 +12,7 @@ import org.bitcoins.core.hd.AddressType
import org.bitcoins.core.number.UInt32
import org.bitcoins.core.protocol.BlockStamp.BlockTime
import org.bitcoins.core.protocol._
import org.bitcoins.core.protocol.dlc.{ContractInfo, OracleInfo}
import org.bitcoins.core.protocol.tlv._
import org.bitcoins.core.protocol.transaction.{Transaction, TransactionOutPoint}
import org.bitcoins.core.psbt.InputPSBTRecord.PartialSignature
Expand Down Expand Up @@ -177,10 +177,10 @@ object CliReaders {
val reads: String => ContractInfo = ContractInfo.fromHex
}

implicit val contractInfoTLVReads: Read[ContractInfoTLV] =
new Read[ContractInfoTLV] {
implicit val contractInfoTLVReads: Read[ContractInfoV0TLV] =
new Read[ContractInfoV0TLV] {
val arity: Int = 1
val reads: String => ContractInfoTLV = ContractInfoTLV.fromHex
val reads: String => ContractInfoV0TLV = ContractInfoV0TLV.fromHex
}

implicit val blockStampReads: Read[BlockStamp] =
Expand Down Expand Up @@ -336,6 +336,14 @@ object CliReaders {
}
}

implicit val oracleAttestmentTLVReads: Read[OracleAttestmentTLV] =
new Read[OracleAttestmentTLV] {
override def arity: Int = 1

override def reads: String => OracleAttestmentTLV =
OracleAttestmentTLV.fromHex
}

implicit val ecPublicKeyReads: Read[ECPublicKey] = new Read[ECPublicKey] {
override def arity: Int = 1

Expand Down
28 changes: 7 additions & 21 deletions app/cli/src/main/scala/org/bitcoins/cli/ConsoleCli.scala
Expand Up @@ -6,11 +6,10 @@ import java.time.Instant
import org.bitcoins.cli.CliCommand._
import org.bitcoins.cli.CliReaders._
import org.bitcoins.commons.jsonmodels.bitcoind.RpcOpts.LockUnspentOutputParameter
import org.bitcoins.core.protocol.dlc.DLCMessage._
import org.bitcoins.commons.serializers.Picklers._
import org.bitcoins.core.api.wallet.CoinSelectionAlgo
import org.bitcoins.core.config.NetworkParameters
import org.bitcoins.core.crypto.{ExtPrivateKey, MnemonicCode}
import org.bitcoins.core.crypto._
import org.bitcoins.core.currency._
import org.bitcoins.core.hd.AddressType
import org.bitcoins.core.hd.AddressType.SegWit
Expand All @@ -30,7 +29,6 @@ import org.bitcoins.crypto.{
AesPassword,
DoubleSha256DigestBE,
ECPublicKey,
SchnorrDigitalSignature,
Sha256DigestBE
}
import scodec.bits.ByteVector
Expand Down Expand Up @@ -174,23 +172,14 @@ object ConsoleCli {
cmd("createdlcoffer")
.action((_, conf) =>
conf.copy(
command = CreateDLCOffer(OracleAnnouncementV0TLV.dummy,
ContractInfo.empty.toTLV,
command = CreateDLCOffer(ContractInfoV0TLV.dummy,
Satoshis.zero,
None,
UInt32.zero,
UInt32.zero)))
.text("Creates a DLC offer that another party can accept")
.children(
arg[OracleAnnouncementTLV]("oracle")
.required()
.action((oracle, conf) =>
conf.copy(command = conf.command match {
case offer: CreateDLCOffer =>
offer.copy(oracle = oracle)
case other => other
})),
arg[ContractInfoTLV]("contractInfo")
arg[ContractInfoV0TLV]("contractInfo")
.required()
.action((info, conf) =>
conf.copy(command = conf.command match {
Expand Down Expand Up @@ -352,7 +341,7 @@ object ConsoleCli {
executeDLC.copy(contractId = contractId)
case other => other
})),
arg[Seq[SchnorrDigitalSignature]]("oraclesigs")
arg[Seq[OracleAttestmentTLV]]("oraclesigs")
.required()
.action((sigs, conf) =>
conf.copy(command = conf.command match {
Expand Down Expand Up @@ -1339,16 +1328,14 @@ object ConsoleCli {
case GetDLCs => RequestParam("getdlcs")
case GetDLC(paramHash) =>
RequestParam("getdlc", Seq(up.writeJs(paramHash)))
case CreateDLCOffer(oracle,
contractInfo,
case CreateDLCOffer(contractInfo,
collateral,
feeRateOpt,
locktime,
refundLT) =>
RequestParam(
"createdlcoffer",
Seq(
up.writeJs(oracle),
up.writeJs(contractInfo),
up.writeJs(collateral),
up.writeJs(feeRateOpt),
Expand Down Expand Up @@ -1664,8 +1651,7 @@ object CliCommand {

// DLC
case class CreateDLCOffer(
oracle: OracleAnnouncementTLV,
contractInfo: ContractInfoTLV,
contractInfo: ContractInfoV0TLV,
collateral: Satoshis,
feeRateOpt: Option[SatoshisPerVirtualByte],
locktime: UInt32,
Expand Down Expand Up @@ -1698,7 +1684,7 @@ object CliCommand {

case class ExecuteDLC(
contractId: ByteVector,
oracleSigs: Vector[SchnorrDigitalSignature],
oracleSigs: Vector[OracleAttestmentTLV],
noBroadcast: Boolean)
extends CliCommand
with Broadcastable
Expand Down

0 comments on commit c4edcb2

Please sign in to comment.