|
| 1 | +import 'dart:typed_data'; |
| 2 | +import 'package:coinlib/src/common/hex.dart'; |
| 3 | +import 'package:coinlib/src/common/serial.dart'; |
| 4 | +import 'package:coinlib/src/crypto/ec_public_key.dart'; |
| 5 | +import 'package:coinlib/src/network.dart'; |
| 6 | +import 'package:coinlib/src/tx/locktime.dart'; |
| 7 | +import 'package:coinlib/src/tx/output.dart'; |
| 8 | +import 'package:coinlib/src/tx/transaction.dart'; |
| 9 | + |
| 10 | +class InvalidDLCTerms implements Exception { |
| 11 | + |
| 12 | + final String message; |
| 13 | + |
| 14 | + InvalidDLCTerms(this.message); |
| 15 | + InvalidDLCTerms.badOutcomeMatch() |
| 16 | + : this("Contains outcome output amounts not matching the funded amount"); |
| 17 | + InvalidDLCTerms.badVersion(int v) |
| 18 | + : this("Version $v isn't allowed. Only v1 is supported."); |
| 19 | + InvalidDLCTerms.noOutputs() : this("CETOutputs have no outputs"); |
| 20 | + InvalidDLCTerms.smallOutput(BigInt min) |
| 21 | + : this("Contains output value less than min of $min"); |
| 22 | + InvalidDLCTerms.smallFunding(BigInt min) |
| 23 | + : this("Contains funding value less than min of $min"); |
| 24 | + |
| 25 | +} |
| 26 | + |
| 27 | +BigInt addBigInts(Iterable<BigInt> ints) => ints.fold( |
| 28 | + BigInt.zero, (a, b) => a+b, |
| 29 | +); |
| 30 | + |
| 31 | +/// A CET will pay to the [outputs] with the value of each output evenly reduced |
| 32 | +/// to cover the transaction fee. |
| 33 | +class CETOutputs { |
| 34 | + |
| 35 | + /// The outputs to be included in the CET of this outcome. The values must |
| 36 | + /// add up to the amounts being funded by the participants in |
| 37 | + /// [DLCTerms.fundAmounts]. |
| 38 | + /// |
| 39 | + /// The [Output.value] for each output will have an equal share of the |
| 40 | + /// transaction fee removed when the transaction is constructed. When doing |
| 41 | + /// this, if any of the outputs fall below the dust amount, they will be |
| 42 | + /// removed first. |
| 43 | + final List<Output> outputs; |
| 44 | + |
| 45 | + /// Requires that the output values are at least [Network.minOutput] or |
| 46 | + /// [InvalidDLCTerms] may be thrown. |
| 47 | + CETOutputs(this.outputs, Network network) { |
| 48 | + if (outputs.isEmpty) { |
| 49 | + throw InvalidDLCTerms.noOutputs(); |
| 50 | + } |
| 51 | + if (outputs.any((out) => out.value.compareTo(network.minOutput) < 0)) { |
| 52 | + throw InvalidDLCTerms.smallOutput(network.minOutput); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + BigInt get totalValue => addBigInts(outputs.map((out) => out.value)); |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +/// Specifies the terms of a DLC contract to be agreed upon by all |
| 61 | +/// [participants]. |
| 62 | +/// |
| 63 | +/// DLCs use CETs signed with adaptor signatures to be completed using an |
| 64 | +/// oracle. CETs do not have a refund mechanism as they can only be broadcast |
| 65 | +/// when the oracle reveals the associated scalar. |
| 66 | +/// |
| 67 | +/// Funding transactions are only created after all CETs and a Refund |
| 68 | +/// Transaction have been created. APO is used to allow creation of the CETs and |
| 69 | +/// the Refund Transaction before the Funding Transaction. |
| 70 | +class DLCTerms with Writable { |
| 71 | + |
| 72 | + /// The version of the protocol is currently 1 |
| 73 | + static final int version = 1; |
| 74 | + |
| 75 | + /// A list of participants that must sign all CETs and RTs for the DLCs. |
| 76 | + final List<ECPublicKey> participants; |
| 77 | + |
| 78 | + /// How much each participant is expected to fund the DLC. A public key may |
| 79 | + /// refer to a funder outside of [participants] if they are not expected to |
| 80 | + /// sign. |
| 81 | + /// |
| 82 | + /// Note that these participants will also be expected to pay an equal |
| 83 | + /// contribution to the Funding Transaction fee in excess of these amounts. |
| 84 | + final Map<ECPublicKey, BigInt> fundAmounts; |
| 85 | + |
| 86 | + /// Maps oracle adaptor points to [CETOutputs] that contain the output |
| 87 | + /// information to include in Contract Execution Transactions. |
| 88 | + /// |
| 89 | + /// The points can be arbitrarily announced by the oracle in association with |
| 90 | + /// each outcome. |
| 91 | + /// |
| 92 | + /// Alternatively, as proposed in the original DLC designs, a point can be |
| 93 | + /// the S point of an oracle signature where `S = R + eP`, and where `R` is a |
| 94 | + /// pre-disclosed nonce point, `P` is the oracle public key and `e` is the |
| 95 | + /// commitment of the nonce and message representing the outcome. This allows |
| 96 | + /// computation of multiple adaptor points for multiple outcome messages given |
| 97 | + /// the R and P points. coinlib doesn't provide an abstraction for |
| 98 | + /// constructing adaptor points via signatures this way. |
| 99 | + final Map<ECPublicKey, CETOutputs> outcomes; |
| 100 | + |
| 101 | + /// The [Transaction.locktime] to be used in the Refund Transaction where |
| 102 | + /// participants may regain access to funds. |
| 103 | + /// |
| 104 | + /// Ought to be in the future to give enough time for the oracle event and |
| 105 | + /// broadcast of a CET, but this is not checked. |
| 106 | + final Locktime refundLocktime; |
| 107 | + |
| 108 | + /// May throw [InvalidDLCTerms]. |
| 109 | + DLCTerms({ |
| 110 | + required List<ECPublicKey> participants, |
| 111 | + required Map<ECPublicKey, BigInt> fundAmounts, |
| 112 | + required Map<ECPublicKey, CETOutputs> outcomes, |
| 113 | + required this.refundLocktime, |
| 114 | + required Network network, |
| 115 | + }) : |
| 116 | + participants = List.unmodifiable(participants), |
| 117 | + fundAmounts = Map.unmodifiable(fundAmounts), |
| 118 | + outcomes = Map.unmodifiable(outcomes) { |
| 119 | + |
| 120 | + // There should not be any funding amount for a participant which is under |
| 121 | + // the minimum output |
| 122 | + if (fundAmounts.values.any((val) => val.compareTo(network.minOutput) < 0)) { |
| 123 | + throw InvalidDLCTerms.smallFunding(network.minOutput); |
| 124 | + } |
| 125 | + |
| 126 | + // The outcome output amounts must add up to the total funded amount |
| 127 | + final totalToFund = addBigInts(fundAmounts.values); |
| 128 | + if ( |
| 129 | + outcomes.values.any( |
| 130 | + (outcome) => outcome.totalValue.compareTo(totalToFund) != 0, |
| 131 | + ) |
| 132 | + ) { |
| 133 | + throw InvalidDLCTerms.badOutcomeMatch(); |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + /// There are no size limits, so the caller may wish to enforce a reasonable |
| 139 | + /// size for the serialised data. Public keys will always be serialised and |
| 140 | + /// read as compressed keys. |
| 141 | + /// |
| 142 | + /// May throw [InvalidDLCTerms]. |
| 143 | + factory DLCTerms.fromReader(BytesReader reader, Network network) { |
| 144 | + |
| 145 | + if (reader.readUInt16() != version) { |
| 146 | + throw InvalidDLCTerms.badVersion(version); |
| 147 | + } |
| 148 | + |
| 149 | + return DLCTerms( |
| 150 | + participants: reader.readPubKeyVector(), |
| 151 | + fundAmounts: reader.readPubKeyMap(() => reader.readVarInt()), |
| 152 | + outcomes: reader.readPubKeyMap( |
| 153 | + () => CETOutputs( |
| 154 | + reader.readListWithFunc(() => Output.fromReader(reader)), |
| 155 | + network, |
| 156 | + ), |
| 157 | + ), |
| 158 | + refundLocktime: reader.readLocktime(), |
| 159 | + network: network, |
| 160 | + ); |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + factory DLCTerms.fromBytes(Uint8List bytes, Network network) |
| 165 | + => DLCTerms.fromReader(BytesReader(bytes), network); |
| 166 | + |
| 167 | + factory DLCTerms.fromHex(String hex, Network network) |
| 168 | + => DLCTerms.fromBytes(hexToBytes(hex), network); |
| 169 | + |
| 170 | + @override |
| 171 | + /// The public keys will be written as compressed public keys |
| 172 | + void write(Writer writer) { |
| 173 | + writer.writeUInt16(version); |
| 174 | + writer.writePubKeyVector(participants); |
| 175 | + writer.writePubKeyMap( |
| 176 | + fundAmounts, |
| 177 | + (amount) => writer.writeVarInt(amount), |
| 178 | + ); |
| 179 | + writer.writePubKeyMap( |
| 180 | + outcomes, |
| 181 | + (outputs) => writer.writeWritableVector(outputs.outputs), |
| 182 | + ); |
| 183 | + writer.writeLocktime(refundLocktime); |
| 184 | + } |
| 185 | + |
| 186 | +} |
0 commit comments