Ejemplo n.º 1
0
        private const uint hpSMT = 0x534D5400u; // SMT

        public ReadOnlyMemory <byte> Sign(KeyPair keyPair, out Hash256 hash)
        {
            Signers       = null;
            TxnSignature  = null;
            SigningPubKey = keyPair.PublicKey.GetCanoncialBytes();
            var bufferWriter = new ArrayBufferWriter <byte>();

            System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(bufferWriter.GetSpan(4), hpSTX);
            bufferWriter.Advance(4);
            Serialize(bufferWriter, true);

            // Calculate signature and serialize again
            TxnSignature = keyPair.Sign(bufferWriter.WrittenSpan);
            bufferWriter.Clear();

            System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(bufferWriter.GetSpan(4), hpTXN);
            bufferWriter.Advance(4);

            Serialize(bufferWriter, false);

            using (var sha512 = System.Security.Cryptography.SHA512.Create())
            {
                Span <byte> hashSpan = stackalloc byte[64];
                sha512.TryComputeHash(bufferWriter.WrittenSpan, hashSpan, out var bytesWritten);
                hash = new Hash256(hashSpan.Slice(0, 32));
            }

            return(bufferWriter.WrittenMemory.Slice(4));
        }
Ejemplo n.º 2
0
        public Majority(JsonElement json)
        {
            var entry = json.GetProperty("Majority");

            CloseTime = Epoch.ToDateTimeOffset(entry.GetProperty("CloseTime").GetUInt32());
            Amendment = new Hash256(entry.GetProperty("Amendment").GetString());
        }
Ejemplo n.º 3
0
        public static Hash256 CalculatePage(Hash256 root, uint page)
        {
            Span <byte> buffer = stackalloc byte[38];

            System.Buffers.Binary.BinaryPrimitives.WriteUInt16BigEndian(buffer, 0x0064);
            root.CopyTo(buffer.Slice(2));
            System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(34), page);
            return(CalculateId(buffer));
        }
Ejemplo n.º 4
0
        private const uint hpCLM = 0x434C4D00u; // CLM

        public static byte[] Authorize(KeyPair keyPair, Hash256 channelId, XrpAmount amount)
        {
            Span <byte> buffer = stackalloc byte[44];

            System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(buffer, hpCLM);
            channelId.CopyTo(buffer.Slice(4));
            System.Buffers.Binary.BinaryPrimitives.WriteUInt64BigEndian(buffer.Slice(36), amount.Drops);
            return(keyPair.Sign(buffer));
        }
Ejemplo n.º 5
0
        private protected Transaction(JsonElement json)
        {
            JsonElement element;

            Account  = new AccountId(json.GetProperty("Account").GetString());
            Fee      = XrpAmount.ReadJson(json.GetProperty("Fee"));
            Sequence = json.GetProperty("Sequence").GetUInt32();
            if (json.TryGetProperty("AccountTxnID", out element))
            {
                AccountTxnID = new Hash256(element.GetString());
            }
            if (json.TryGetProperty("Flags", out element))
            {
                Flags = element.GetUInt32();
            }
            if (json.TryGetProperty("LastLedgerSequence", out element))
            {
                LastLedgerSequence = element.GetUInt32();
            }
            if (json.TryGetProperty("Memos", out element))
            {
                var MemosArray = new Memo[element.GetArrayLength()];
                for (int i = 0; i < MemosArray.Length; ++i)
                {
                    MemosArray[i] = new Memo(element[i]);
                }
                Memos = Array.AsReadOnly(MemosArray);
            }
            if (json.TryGetProperty("Signers", out element))
            {
                var SignersArray = new Signer[element.GetArrayLength()];
                for (int i = 0; i < SignersArray.Length; ++i)
                {
                    SignersArray[i] = new Signer(element[i]);
                }
                Signers = Array.AsReadOnly(SignersArray);
            }
            if (json.TryGetProperty("SourceTag", out element))
            {
                SourceTag = element.GetUInt32();
            }
            if (json.TryGetProperty("SigningPubKey", out element))
            {
                SigningPubKey = element.GetBytesFromBase16();
            }
            if (json.TryGetProperty("TicketSequence", out element))
            {
                TicketSequence = element.GetUInt32();
            }
            if (json.TryGetProperty("TxnSignature", out element))
            {
                TxnSignature = element.GetBytesFromBase16();
            }
        }
Ejemplo n.º 6
0
        public ReadOnlyMemory <byte> Sign(ReadOnlySpan <ValueTuple <AccountId, KeyPair> > signers, out Hash256 hash)
        {
            var signerArray = new Signer[signers.Length];

            for (int i = 0; i < signers.Length; ++i)
            {
                var signer = new Signer();
                signer.Account       = signers[i].Item1;
                signer.SigningPubKey = signers[i].Item2.PublicKey.GetCanoncialBytes();
                signer.TxnSignature  = null;
                signerArray[i]       = signer;
            }
            Signers       = Array.AsReadOnly(signerArray);
            TxnSignature  = null;
            SigningPubKey = null;
            var bufferWriter = new ArrayBufferWriter <byte>();

            // Calculate signatures and then serialize again
            for (int i = 0; i < signers.Length; ++i)
            {
                // For each signer we need to write the account being signed for the the buffer, sign that then rewind
                System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(bufferWriter.GetSpan(4), hpSMT);
                bufferWriter.Advance(4);
                Serialize(bufferWriter, true);
                Signers[i].Account.CopyTo(bufferWriter.GetSpan(20));
                bufferWriter.Advance(20);

                signerArray[i].TxnSignature = signers[i].Item2.Sign(bufferWriter.WrittenSpan);
                bufferWriter.Clear();
            }

            Array.Sort <Signer>(signerArray, (x, y) => x.Account.CompareTo(y.Account));
            Signers = Array.AsReadOnly(signerArray);

            System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(bufferWriter.GetSpan(4), hpTXN);
            bufferWriter.Advance(4);

            Serialize(bufferWriter, false);

            using (var sha512 = System.Security.Cryptography.SHA512.Create())
            {
                Span <byte> hashSpan = stackalloc byte[64];
                sha512.TryComputeHash(bufferWriter.WrittenSpan, hashSpan, out var bytesWritten);
                hash = new Hash256(hashSpan.Slice(0, 32));
            }

            return(bufferWriter.WrittenMemory.Slice(4));
        }
Ejemplo n.º 7
0
        public static Hash256 CalculateOfferId(CurrencyCode takerPaysCurrency, CurrencyCode takerGetsCurrency, AccountId takerPaysIssuer, AccountId takerGetsIssuer, ulong offers)
        {
            // The first page of an Offer Directory has a special ID: the higher 192 bits define the order book, and the remaining 64 bits define the exchange rate of the offers in that directory.
            // (The ID is big-endian, so the book is in the more significant bits, which come first, and the quality is in the less significant bits which come last.)
            // This provides a way to iterate through an order book from best offers to worst.Specifically: the first 192 bits are the first 192 bits of the SHA-512Half of the following values, concatenated in order:
            //
            //The Book Directory space key(0x0042)
            //The 160-bit currency code from the TakerPaysCurrency
            //The 160-bit currency code from the TakerGetsCurrency
            //The AccountID from the TakerPaysIssuer
            //The AccountID from the TakerGetsIssuer
            //The lower 64 bits of an Offer Directory's ID represent the TakerPays amount divided by TakerGets amount from the offer(s) in that directory as a 64-bit number in the XRP Ledger's internal amount format.
            //
            Span <byte> buffer = stackalloc byte[38];

            System.Buffers.Binary.BinaryPrimitives.WriteUInt16BigEndian(buffer, 0x0042);
            takerPaysCurrency.CopyTo(buffer.Slice(2));
            takerGetsCurrency.CopyTo(buffer.Slice(22));
            takerPaysIssuer.CopyTo(buffer.Slice(42));
            takerGetsIssuer.CopyTo(buffer.Slice(62));
            Hash256 hash = CalculateId(buffer);

            return(hash);
        }
Ejemplo n.º 8
0
        public static bool Verify(PublicKey publicKey, ReadOnlySpan <byte> signature, Hash256 channelId, XrpAmount amount)
        {
            Span <byte> buffer = stackalloc byte[44];

            System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(buffer, hpCLM);
            channelId.CopyTo(buffer.Slice(4));
            System.Buffers.Binary.BinaryPrimitives.WriteUInt64BigEndian(buffer.Slice(36), amount.Drops);
            return(publicKey.Verify(buffer, signature));
        }
Ejemplo n.º 9
0
 public LedgerSpecification(Hash256 hash)
 {
     this.index    = 0;
     this.shortcut = null;
     this.hash     = new Hash256?(hash);
 }