public void Ok()
        {
            const long payloadSize = 20;

            // Version 0.1.1
            var signaturePayloadBytes = new byte[payloadSize];
            var rnd = new Random();

            rnd.NextBytes(signaturePayloadBytes);
            long payloadLength = payloadSize;

            byte[] signerPublicKey = Converters.StringToByteArray("0xd678b3e00c4238888bbf08dbbe1d7de77c3f1ca1fc71a5a283770f06f7cd1205");
            byte[] secretKeyVec    = Converters.StringToByteArray("0xa81056d713af1ff17b599e60d287952e89301b5208324a0529b62dc7369c745defc9c8dd67b7c59b201bc164163a8978d40010c22743db142a47f2e064480d4b");

            var message = signaturePayloadBytes.AsMemory().Slice(0, (int)payloadLength).ToArray();
            var sig2    = Sr25519v011.SignSimple(signerPublicKey, secretKeyVec, message);

            Console.Write("Message: 0x");
            for (var i = 0; i < payloadSize; i++)
            {
                Console.Write($"{signaturePayloadBytes[i]:X2}");
            }
            Console.WriteLine("");

            Console.Write("Signature: 0x");
            for (var i = 0; i < sig2.Length; i++)
            {
                Console.Write($"{sig2[i]:X2}");
            }
            Console.WriteLine("");


            Assert.True(Sr25519v011.Verify(sig2, signerPublicKey, message));
        }
Beispiel #2
0
        public void SignatureVerify11Test()
        {
            string pubKey0x = "0xd678b3e00c4238888bbf08dbbe1d7de77c3f1ca1fc71a5a283770f06f7cd1205";
            string secKey0x = "0xa81056d713af1ff17b599e60d287952e89301b5208324a0529b62dc7369c745defc9c8dd67b7c59b201bc164163a8978d40010c22743db142a47f2e064480d4b";

            byte[] pubKey = Utils.HexToByteArray(pubKey0x);
            byte[] secKey = Utils.HexToByteArray(secKey0x);

            int messageLength = _random.Next(10, 200);
            var message       = new byte[messageLength];

            _random.NextBytes(message);

            //var message = signaturePayloadBytes.AsMemory().Slice(0, (int)payloadLength).ToArray();
            var simpleSign = Sr25519v011.SignSimple(pubKey, secKey, message);

            Assert.True(Sr25519v011.Verify(simpleSign, pubKey, message));
        }
Beispiel #3
0
        public void Ok()
        {
            const long payloadSize = 20;

            // Version 0.1.1
            var signaturePayloadBytes = new byte[payloadSize];
            var rnd = new Random();

            rnd.NextBytes(signaturePayloadBytes);

            byte[] signerPublicKey = new byte[] { 214, 120, 179, 224, 12, 66, 56, 136, 139, 191, 8, 219, 190, 29, 125, 231, 124, 63, 28, 161, 252, 113, 165, 162, 131, 119, 15, 6, 247, 205, 18, 5 };
            byte[] secretKeyVec    = new byte[] { 168, 16, 86, 215, 19, 175, 31, 241, 123, 89, 158, 96, 210, 135, 149, 46, 137, 48, 27, 82, 8, 50, 74, 5, 41, 182, 45, 199, 54, 156, 116, 93, 239, 201, 200, 221, 103, 183, 197, 155, 32, 27, 193, 100, 22, 58, 137, 120, 212, 0, 16, 194, 39, 67, 219, 20, 42, 71, 242, 224, 100, 72, 13, 75 };

            var message = signaturePayloadBytes.AsMemory().Slice(0, (int)payloadSize).ToArray();
            var sig2    = Sr25519v011.SignSimple(signerPublicKey, secretKeyVec, message);

            Console.Write("Message: 0x");
            for (var i = 0; i < payloadSize; i++)
            {
                Console.Write($"{signaturePayloadBytes[i]:X2}");
            }
            Console.WriteLine("");

            Console.Write("Signature: 0x");
            for (var i = 0; i < sig2.Length; i++)
            {
                Console.Write($"{sig2[i]:X2}");
            }
            Console.WriteLine("");

            Assert.True(Sr25519v011.Verify(sig2, signerPublicKey, message));

            // modify message
            message[0] = 0;
            Assert.False(Sr25519v011.Verify(sig2, signerPublicKey, message));
        }
        private string ExtrinsicQueryString(byte[] encodedMethodBytes, string module, string method, Address sender, string privateKey)
        {
            _logger.Info("=== Started Invoking Extrinsic ===");

            // Get account Nonce
            var nonce        = GetAccountNonce(sender);
            var compactNonce = Scale.EncodeCompactInteger(nonce);

            _logger.Info($"sender nonce: {nonce}");

            byte[] mmBuf = new byte[3];
            // Module + Method
            var absoluteIndex = _protocolParams.Metadata.GetModuleIndex(module, false);

            mmBuf[0] = (byte)_protocolParams.Metadata.GetModuleIndex(module, true);
            mmBuf[1] = (byte)_protocolParams.Metadata.GetCallMethodIndex(absoluteIndex, method);

            // Address separator
            mmBuf[2] = Consts.ADDRESS_SEPARATOR;

            Extrinsic ce = new Extrinsic();

            var completeMessage = new byte[encodedMethodBytes.Length + 3];

            mmBuf.CopyTo(completeMessage.AsMemory());
            encodedMethodBytes.CopyTo(completeMessage.AsMemory(3));

            // memcpy(completeMessage + 3, encodedMethodBytes, encodedMethodBytesSize);

            ce.Signature.Version = Consts.SIGNATURE_VERSION;
            var senderPK = _protocolParams.Metadata.GetPublicKeyFromAddr(sender);

            ce.Signature.SignerPublicKey = senderPK.Bytes;
            ce.Signature.Nonce           = nonce;
            ce.Signature.Era             = ExtrinsicEra.IMMORTAL_ERA;

            // Format signature payload
            SignaturePayload sp = new SignaturePayload();

            sp.Nonce = nonce;

            sp.MethodBytesLength = encodedMethodBytes.Length + 3;
            sp.MethodBytes       = completeMessage;
            sp.Era = ExtrinsicEra.IMMORTAL_ERA;
            sp.AuthoringBlockHash = _protocolParams.GenesisBlockHash;

            // Serialize and Sign payload
            var  signaturePayloadBytes = new byte[Consts.MAX_METHOD_BYTES_SZ];
            long payloadLength         = sp.SerializeBinary(ref signaturePayloadBytes);

            var secretKeyVec = Converters.StringToByteArray(privateKey);

            var message = signaturePayloadBytes.AsMemory().Slice(0, (int)payloadLength).ToArray();
            var sig     = Sr25519v011.SignSimple(ce.Signature.SignerPublicKey, secretKeyVec, message);

            ce.Signature.Sr25519Signature = sig;

            // Copy signature bytes to transaction
            ce.Signature.Sr25519Signature = sig;
            var length        = Consts.DEFAULT_FIXED_EXSTRINSIC_SIZE + encodedMethodBytes.Length + compactNonce.Length - 1;
            var compactLength = Scale.EncodeCompactInteger(length);

            /////////////////////////////////////////
            // Serialize message signature and write to buffer

            long writtenLength = 0;
            var  buf           = new byte[2048];
            var  buf2          = new List <byte>();

            // Length
            writtenLength += Scale.WriteCompactToBuf(compactLength, ref buf, writtenLength);

            // Signature version
            buf[writtenLength++] = ce.Signature.Version;

            // Address separator
            buf[writtenLength++] = Consts.ADDRESS_SEPARATOR;

            // Signer public key
            ce.Signature.SignerPublicKey.CopyTo(buf.AsMemory((int)writtenLength));

            writtenLength += Consts.SR25519_PUBLIC_SIZE;

            // SR25519 Signature
            ce.Signature.Sr25519Signature.CopyTo(buf.AsMemory((int)writtenLength));
            writtenLength += Consts.SR25519_SIGNATURE_SIZE;

            // Nonce
            writtenLength += Scale.WriteCompactToBuf(compactNonce, ref buf, writtenLength);

            // Extrinsic Era
            buf[writtenLength++] = (byte)ce.Signature.Era;

            // Serialize and send transaction
            var teBytes = new byte[Consts.MAX_METHOD_BYTES_SZ];

            teBytes = buf;
            completeMessage.AsMemory().CopyTo(teBytes.AsMemory((int)writtenLength));

            long teByteLength = writtenLength + encodedMethodBytes.Length + 3;

            return($"0x{Converters.ByteArrayToString(teBytes, (int)teByteLength)}");
        }
        public string SignAndSendTransfer(string sender, string privateKey, string recipient, BigInteger amount, Action <string> callback)
        {
            _logger.Info("=== Starting a Transfer Extrinsic ===");

            // Get account Nonce
            var address = new Address {
                Symbols = sender
            };
            var nonce = GetAccountNonce(address);

            _logger.Info($"sender nonce: {nonce} ");

            // Format transaction
            TransferExtrinsic te = new TransferExtrinsic();

            te.Method.ModuleIndex = _protocolParams.BalanceModuleIndex;
            te.Method.MethodIndex = _protocolParams.TransferMethodIndex;

            var recipientPK = _protocolParams.Metadata.GetPublicKeyFromAddr(new Address(recipient));

            te.Method.ReceiverPublicKey = recipientPK.Bytes;
            te.Method.Amount            = amount;
            te.Signature.Version        = Consts.SIGNATURE_VERSION;
            var senderPK = _protocolParams.Metadata.GetPublicKeyFromAddr(new Address(sender));

            te.Signature.SignerPublicKey = senderPK.Bytes;
            te.Signature.Nonce           = nonce;
            te.Signature.Era             = ExtrinsicEra.IMMORTAL_ERA;

            // Format signature payload
            SignaturePayload sp = new SignaturePayload();

            sp.Nonce = nonce;
            var methodBytes = new byte[Consts.MAX_METHOD_BYTES_SZ];

            sp.MethodBytesLength = (int)te.SerializeMethodBinary(ref methodBytes);
            sp.MethodBytes       = methodBytes;
            sp.Era = ExtrinsicEra.IMMORTAL_ERA;
            sp.AuthoringBlockHash = _protocolParams.GenesisBlockHash;

            // Serialize and Sign payload
            var  signaturePayloadBytes = new byte[Consts.MAX_METHOD_BYTES_SZ];
            long payloadLength         = sp.SerializeBinary(ref signaturePayloadBytes);

            byte[] secretKeyVec = Converters.StringToByteArray(privateKey);

            var message = signaturePayloadBytes.AsMemory().Slice(0, (int)payloadLength).ToArray();
            var sig     = Sr25519v011.SignSimple(te.Signature.SignerPublicKey, secretKeyVec, message);

            te.Signature.Sr25519Signature = sig;

            // Serialize and send transaction
            var    teBytes      = new byte[Consts.MAX_METHOD_BYTES_SZ];
            long   teByteLength = te.SerializeBinary(ref teBytes);
            string teStr        = $"0x{Converters.ByteArrayToString(teBytes, (int)teByteLength)}";

            var query = new JObject {
                { "method", "author_submitAndWatchExtrinsic" }, { "params", new JArray {
                                                                      teStr
                                                                  } }
            };

            // Send == Subscribe callback to completion
            return(Subscribe(query, (json) =>
            {
                callback(json.ToString());
            }));
        }