public static CetSigs ParseFromTLV(TLVReader reader) { CetSigs cet = new CetSigs(); using (var r = reader.StartReadRecord()) { if (r.Type != AdaptorSigsTLVType) { throw new FormatException("Invalid TLV type, expected adaptor sigs"); } List <AdaptorSignature> sigs = new List <AdaptorSignature>(); while (!r.IsEnd) { sigs.Add(AdaptorSignature.ParseFromTLV(reader)); } cet.OutcomeSigs = sigs.ToArray(); } Span <byte> buf = stackalloc byte[64]; reader.ReadBytes(buf); if (!ECDSASignature.TryParseFromCompact(buf, out var sig)) { throw new FormatException("Invalid DER signature"); } cet.RefundSig = sig; return(cet); }
public void ReadTLV(TLVReader reader, Network network) { if (reader.ReadU16() != TLVType) { throw new FormatException("Invalid TLV type for sign"); } ContractId = reader.ReadUInt256(); CetSigs = CetSigs.ParseFromTLV(reader); using (var r = reader.StartReadRecord()) { if (r.Type != FundingSignaturesTLVType) { throw new FormatException("Invalid TLV type for funding signatures"); } FundingSigs = new List <WitScript>(); var witnesses = r.ReadU16(); for (int i = 0; i < witnesses; i++) { var elementsCount = reader.ReadU16(); var witnessBytes = new byte[elementsCount][]; for (int y = 0; y < elementsCount; y++) { var elementSize = reader.ReadU16(); witnessBytes[y] = new byte[elementSize]; r.ReadBytes(witnessBytes[y]); } FundingSigs.Add(new WitScript(witnessBytes)); } } }
public void ReadTLV(TLVReader reader, Network network) { if (reader.ReadU16() != TLVType) { throw new FormatException("Invalid TLV type for offer"); } reader.ReadByte(); // contract_flags ChainHash = reader.ReadUInt256(); if (network.GenesisHash != network.GenesisHash) { throw new FormatException("Invalid ChainHash"); } Span <byte> buf = stackalloc byte[64]; using (var ciRecord = reader.StartReadRecord()) { if (ciRecord.Type != TLVContractInfoType) { throw new FormatException("Invalid TLV type for contract info"); } List <ContractInfo> cis = new List <ContractInfo>(); while (!ciRecord.IsEnd) { ciRecord.ReadBytes(buf.Slice(0, 32)); var sats = ciRecord.ReadU64(); cis.Add(new Messages.ContractInfo(new DiscreteOutcome(buf.Slice(0, 32).ToArray()), Money.Satoshis(sats))); } ContractInfo = cis.ToArray(); } using (var oracleRecord = reader.StartReadRecord()) { if (oracleRecord.Type != TLVOracleInfoType) { throw new FormatException("Invalid TLV type for oracle info"); } oracleRecord.ReadBytes(buf.Slice(0, 64)); OracleInfo = OracleInfo.Create(buf); } PubKeys = new PubKeyObject(); reader.ReadBytes(buf.Slice(0, 33)); PubKeys.FundingKey = new PubKey(buf.Slice(0, 33).ToArray()); PubKeys.PayoutAddress = reader.ReadScript().GetDestinationAddress(network); if (PubKeys.PayoutAddress is null) { throw new FormatException("Invalid script"); } TotalCollateral = Money.Satoshis(reader.ReadU64()); var fiCount = reader.ReadU16(); List <FundingInput> fis = new List <FundingInput>(); while (fiCount > 0) { fis.Add(FundingInput.ParseFromTLV(reader, network)); fiCount--; } FundingInputs = fis.ToArray(); ChangeAddress = reader.ReadScript().GetDestinationAddress(network); if (ChangeAddress is null) { throw new FormatException("Invalid script"); } FeeRate = new FeeRate(Money.Satoshis(reader.ReadU64()), 1); Timeouts = new Timeouts() { ContractMaturity = reader.ReadU32(), ContractTimeout = reader.ReadU32() }; }