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 static Script ReadScript(this TLVReader reader) { var size = reader.ReadU16(); var buf = new byte[size]; reader.ReadBytes(buf); return(Script.FromBytesUnsafe(buf)); }
private ushort GetTLVType(string hexOrBase64) { try { var data = HexEncoder.IsWellFormed(hexOrBase64) ? Encoders.Hex.DecodeData(hexOrBase64) : Encoders.Base64.DecodeData(hexOrBase64); var r = new TLVReader(new MemoryStream(data)); return(r.ReadU16()); } catch { } return(0); }
public void ReadTLV(TLVReader reader, Network network) { if (reader.ReadU16() != TLVType) { throw new FormatException("Invalid TLV type for accept"); } TemporaryContractId = reader.ReadUInt256(); TotalCollateral = Money.Satoshis(reader.ReadU64()); PubKeys = new PubKeyObject(); var pk = new byte[33]; reader.ReadBytes(pk); PubKeys.FundingKey = new PubKey(pk, true); PubKeys.PayoutAddress = reader.ReadScript().GetDestinationAddress(network); var inputLen = reader.ReadU16(); FundingInputs = new FundingInput[inputLen]; for (int i = 0; i < inputLen; i++) { FundingInputs[i] = FundingInput.ParseFromTLV(reader, network); } ChangeAddress = reader.ReadScript().GetDestinationAddress(network); CetSigs = CetSigs.ParseFromTLV(reader); }
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() }; }