Exemplo n.º 1
0
        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));
                }
            }
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 public void WriteTLV(TLVWriter writer)
 {
     if (ContractId is null)
     {
         throw new InvalidOperationException("ContractId is not set");
     }
     if (CetSigs is null)
     {
         throw new InvalidOperationException("CetSigs is not set");
     }
     if (FundingSigs is null)
     {
         throw new InvalidOperationException("FundingSigs is not set");
     }
     writer.WriteU16(TLVType);
     writer.WriteUInt256(ContractId);
     CetSigs.WriteTLV(writer);
     using (var w = writer.StartWriteRecord(FundingSignaturesTLVType))
     {
         w.WriteU16(FundingSigs.Count);
         foreach (var s in FundingSigs)
         {
             w.WriteU16(s.PushCount);
             for (int i = 0; i < s.PushCount; i++)
             {
                 var p = s.GetUnsafePush(i);
                 w.WriteU16(p.Length);
                 w.WriteBytes(p);
             }
         }
     }
 }
Exemplo n.º 4
0
        public void WriteTLV(TLVWriter writer)
        {
            if (TemporaryContractId is null)
            {
                throw new InvalidOperationException($"{nameof(TemporaryContractId)} is not set");
            }
            if (TotalCollateral is null)
            {
                throw new InvalidOperationException($"{nameof(TotalCollateral)} is not set");
            }
            if (PubKeys?.FundingKey is null)
            {
                throw new InvalidOperationException($"{nameof(PubKeys.FundingKey)} is not set");
            }
            if (PubKeys?.PayoutAddress is null)
            {
                throw new InvalidOperationException($"{nameof(PubKeys.PayoutAddress)} is not set");
            }
            if (FundingInputs is null)
            {
                throw new InvalidOperationException($"{nameof(FundingInputs)} is not set");
            }
            if (ChangeAddress is null)
            {
                throw new InvalidOperationException($"{nameof(ChangeAddress)} is not set");
            }
            if (CetSigs is null)
            {
                throw new InvalidOperationException($"{nameof(CetSigs)} is not set");
            }
            writer.WriteU16(TLVType);
            writer.WriteUInt256(TemporaryContractId);
            writer.WriteU64((ulong)TotalCollateral.Satoshi);
            Span <byte> buf = stackalloc byte[64];

            PubKeys.FundingKey.Compress().ToBytes(buf, out _);
            writer.WriteBytes(buf.Slice(0, 33));
            writer.WriteScript(PubKeys.PayoutAddress.ScriptPubKey);
            writer.WriteU16((ushort)FundingInputs.Length);
            foreach (var input in FundingInputs)
            {
                input.WriteTLV(writer);
            }
            writer.WriteScript(ChangeAddress.ScriptPubKey);
            CetSigs.WriteTLV(writer);
        }
Exemplo n.º 5
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);
        }