Esempio n. 1
0
        public void Format()
        {
            byte[] bytes =
            {
                0x04, 0xb5, 0xa2, 0x4a, 0xa2, 0x11, 0x27, 0x20, 0x42, 0x3b,
                0xad, 0x39, 0xa0, 0x20, 0x51, 0x82, 0x37, 0x9d, 0x6f, 0x2b,
                0x33, 0xe3, 0x48, 0x7c, 0x9a, 0xb6, 0xcc, 0x8f, 0xc4, 0x96,
                0xf8, 0xa5, 0x48, 0x34, 0x40, 0xef, 0xbb, 0xef, 0x06, 0x57,
                0xac, 0x2e, 0xf6, 0xc6, 0xee, 0x05, 0xdb, 0x06, 0xa9, 0x45,
                0x32, 0xfd, 0xa7, 0xdd, 0xc4, 0x4a, 0x16, 0x95, 0xe5, 0xce,
                0x1a, 0x3d, 0x3c, 0x76, 0xdb,
            };
            byte[] compressed =
            {
                0x03, 0xb5, 0xa2, 0x4a, 0xa2, 0x11, 0x27, 0x20, 0x42, 0x3b, 0xad,
                0x39, 0xa0, 0x20, 0x51, 0x82, 0x37, 0x9d, 0x6f, 0x2b, 0x33, 0xe3,
                0x48, 0x7c, 0x9a, 0xb6, 0xcc, 0x8f, 0xc4, 0x96, 0xf8, 0xa5, 0x48,
            };

            var key = new PublicKey(bytes);

            TestUtils.AssertBytesEqual(bytes, key.Format(compress: false));
            TestUtils.AssertBytesEqual(compressed, key.Format(compress: true));
            TestUtils.AssertBytesEqual(
                bytes.ToImmutableArray(),
                key.ToImmutableArray(compress: false)
                );
            TestUtils.AssertBytesEqual(
                compressed.ToImmutableArray(),
                key.ToImmutableArray(compress: true)
                );
        }
Esempio n. 2
0
        private static byte[] DeriveAddress(PublicKey key)
        {
            byte[] hashPayload = key.Format(false).Skip(1).ToArray();
            var    output      = CalculateHash(hashPayload);

            return(output.Skip(output.Length - Size).ToArray());
        }
Esempio n. 3
0
 public void GetObjectData(
     SerializationInfo info,
     StreamingContext context
     )
 {
     info.AddValue("public_key", PublicKey.Format(true));
     info.AddValue("urls", Urls.Select(u => u.ToString()).ToList());
 }
Esempio n. 4
0
 public void GetObjectData(
     SerializationInfo info,
     StreamingContext context
     )
 {
     info.AddValue("public_key", PublicKey.Format(true));
     info.AddValue("end_point_host", EndPoint.Host);
     info.AddValue("end_point_port", EndPoint.Port);
 }
 public override void GetObjectData(
     SerializationInfo info, StreamingContext context)
 {
     base.GetObjectData(info, context);
     info.AddValue(nameof(Peer), Peer);
     info.AddValue(nameof(PublicKey), PublicKey.Format(true));
     info.AddValue(nameof(MessageToVerify), MessageToVerify);
     info.AddValue(nameof(Signature), Signature);
 }
Esempio n. 6
0
        private static byte[] DeriveAddress(PublicKey key)
        {
            byte[] hashPayload = key.Format(false).Skip(1).ToArray();
            var    digest      = new KeccakDigest(256);
            var    output      = new byte[digest.GetDigestSize()];

            digest.BlockUpdate(hashPayload, 0, hashPayload.Length);
            digest.DoFinal(output, 0);

            return(output.Skip(output.Length - 20).ToArray());
        }
Esempio n. 7
0
 /// <inheritdoc/>
 public void GetObjectData(
     SerializationInfo info,
     StreamingContext context
     )
 {
     info.AddValue("public_key", PublicKey.Format(true));
     info.AddValue("end_point_host", EndPoint.Host);
     info.AddValue("end_point_port", EndPoint.Port);
     info.AddValue("app_protocol_version", AppProtocolVersion);
     info.AddValue("public_ip_address", PublicIPAddress?.ToString());
 }
Esempio n. 8
0
 /// <inheritdoc/>
 public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
 {
     info.AddValue("public_key", PublicKey.Format(true));
     info.AddValue("app_protocol_version", AppProtocolVersion.Version);
     info.AddValue(
         "app_protocol_version_extra",
         AppProtocolVersion.Extra is IValue e ? _codec.Encode(e) : null);
     info.AddValue(
         "app_protocol_version_sig",
         AppProtocolVersion.Signature.ToBuilder().ToArray());
     info.AddValue("app_protocol_version_signer", AppProtocolVersion.Signer.ToByteArray());
     info.AddValue("public_ip_address", PublicIPAddress?.ToString());
 }
Esempio n. 9
0
        public bool Verify(
            HashDigest <T> messageHash,
            byte[] signature,
            PublicKey publicKey)
        {
            lock (_instanceLock)
            {
                var secp256K1Signature = new byte[64];
                _instance.SignatureParseDer(secp256K1Signature, signature);

                byte[] secp256K1PublicKey  = new byte[64];
                byte[] serializedPublicKey = publicKey.Format(false);
                _instance.PublicKeyParse(secp256K1PublicKey, serializedPublicKey);

                return(_instance.Verify(secp256K1Signature, messageHash.ToByteArray(), secp256K1PublicKey));
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Validates this <see cref="Transaction{T}"/>.  If there is something
        /// invalid it throws an exception.  If valid it does nothing.
        /// </summary>
        /// <exception cref="InvalidTxSignatureException">Thrown when its
        /// <see cref="Transaction{T}.Signature"/> is invalid or not signed by
        /// the account who corresponds to its <see cref="PublicKey"/>.
        /// </exception>
        /// <exception cref="InvalidTxPublicKeyException">Thrown when its
        /// <see cref="Signer"/> is not derived from its
        /// <see cref="Transaction{T}.PublicKey"/>.</exception>
        public void Validate()
        {
            if (!PublicKey.Verify(Serialize(false), Signature))
            {
                string message =
                    $"The signature ({ByteUtil.Hex(Signature)}) is failed " +
                    "to verify.";
                throw new InvalidTxSignatureException(Id, message);
            }

            if (!new Address(PublicKey).Equals(Signer))
            {
                string message =
                    $"The public key ({ByteUtil.Hex(PublicKey.Format(true))} " +
                    $"is not matched to the address ({Signer}).";
                throw new InvalidTxPublicKeyException(Id, message);
            }
        }
Esempio n. 11
0
#pragma warning restore LAA1002

        public int Redeem(string code, Address userAddress)
        {
            var       privateKey = new PrivateKey(ByteUtil.ParseHex(code));
            PublicKey publicKey  = privateKey.PublicKey;

            if (!Map.ContainsKey(publicKey))
            {
                throw new InvalidRedeemCodeException();
            }

            var result = Map[publicKey];

            if (result.UserAddress.HasValue)
            {
                throw new DuplicateRedeemException($"Code already used by {result.UserAddress}");
            }

            result.UserAddress           = userAddress;
            _map[publicKey.Format(true)] = result;
            return(result.RewardId);
        }
Esempio n. 12
0
        /// <summary>
        /// Gets decoded textual representation (dump) of the current object.
        /// </summary>
        /// <returns>Textual representation of the current object.</returns>
        public virtual String Format()
        {
            var SB   = new StringBuilder();
            var blob = new SignedContentBlob(RawData, ContentBlobType.SignedBlob);

            SB.Append(
                $@"PKCS10 Certificate Request:
Version: {Version}
Subject:
    {Subject ?? "EMPTY"}

{PublicKey.Format().TrimEnd()}
Request attributes (Count={Attributes.Count}):{formatAttributes().TrimEnd()}
Request extensions (Count={Extensions.Count}):{formatExtensions().TrimEnd()}
{blob.SignatureAlgorithm.ToString().TrimEnd()}    
Signature: Unused bits={blob.Signature.UnusedBits}
    {AsnFormatter.BinaryToString(blob.Signature.Value.ToArray(), EncodingType.HexAddress).Replace("\r\n", "\r\n    ")}
Signature matches Public Key: {SignatureIsValid}
");

            return(SB.ToString());
        }
Esempio n. 13
0
        internal RawTransaction ToRawTransaction(bool includeSign)
        {
            ImmutableArray <byte> genesisHash =
                GenesisHash?.ToByteArray().ToImmutableArray() ?? ImmutableArray <byte> .Empty;
            var rawTx = new RawTransaction(
                nonce: Nonce,
                signer: Signer.ByteArray,
                genesisHash: genesisHash,
                updatedAddresses: UpdatedAddresses.Select(a =>
                                                          a.ByteArray).ToImmutableArray(),
                publicKey: PublicKey.Format(false).ToImmutableArray(),
                timestamp: Timestamp.ToString(TimestampFormat, CultureInfo.InvariantCulture),
                actions: Actions.Select(a => a.PlainValue).ToImmutableArray()
                );

            if (includeSign)
            {
                rawTx = rawTx.AddSignature(Signature);
            }

            return(rawTx);
        }
Esempio n. 14
0
        public void Constructor()
        {
            byte[] bytes =
            {
                0x04, 0xb5, 0xa2, 0x4a, 0xa2, 0x11, 0x27, 0x20, 0x42, 0x3b,
                0xad, 0x39, 0xa0, 0x20, 0x51, 0x82, 0x37, 0x9d, 0x6f, 0x2b,
                0x33, 0xe3, 0x48, 0x7c, 0x9a, 0xb6, 0xcc, 0x8f, 0xc4, 0x96,
                0xf8, 0xa5, 0x48, 0x34, 0x40, 0xef, 0xbb, 0xef, 0x06, 0x57,
                0xac, 0x2e, 0xf6, 0xc6, 0xee, 0x05, 0xdb, 0x06, 0xa9, 0x45,
                0x32, 0xfd, 0xa7, 0xdd, 0xc4, 0x4a, 0x16, 0x95, 0xe5, 0xce,
                0x1a, 0x3d, 0x3c, 0x76, 0xdb,
            };

            var mutable   = new PublicKey(bytes);
            var immutable = new PublicKey(bytes.ToImmutableArray());

            Assert.Equal(mutable, immutable);
            var compressedMutable = new PublicKey(mutable.Format(compress: true));

            Assert.Equal(mutable, compressedMutable);
            var compressedImmutable = new PublicKey(immutable.ToImmutableArray(compress: true));

            Assert.Equal(mutable, compressedImmutable);
        }
Esempio n. 15
0
        public async Task CreateUnsignedTx()
        {
            var       privateKey = new PrivateKey();
            PublicKey publicKey  = privateKey.PublicKey;
            Address   signer     = publicKey.ToAddress();
            long      nonce      = _blockChain.GetNextTxNonce(signer);
            NCAction  action     = new CreateAvatar2
            {
                index = 0,
                hair  = 1,
                lens  = 2,
                ear   = 3,
                tail  = 4,
                name  = "action",
            };

            var codec       = new Codec();
            var queryFormat = @"query {{
                createUnsignedTx(publicKey: ""{0}"", plainValue: ""{1}"")
            }}";
            var queryResult = await ExecuteAsync(string.Format(
                                                     queryFormat,
                                                     Convert.ToBase64String(publicKey.Format(false)),
                                                     Convert.ToBase64String(codec.Encode(action.PlainValue))));

            var base64EncodedUnsignedTx = (string)queryResult.Data
                                          .As <Dictionary <string, object> >()["createUnsignedTx"];
            Transaction <NCAction> unsignedTx =
                Transaction <NCAction> .Deserialize(Convert.FromBase64String(base64EncodedUnsignedTx), validate : false);

            Assert.Empty(unsignedTx.Signature);
            Assert.Equal(publicKey, unsignedTx.PublicKey);
            Assert.Equal(signer, unsignedTx.Signer);
            Assert.Equal(nonce, unsignedTx.Nonce);
            Assert.Contains(signer, unsignedTx.UpdatedAddresses);
        }
Esempio n. 16
0
 private static Address DeriveAddress(byte[] nonce, PublicKey publicKey)
 {
     return(BaseAddress.Derive(nonce.Concat(publicKey.Format(true)).ToArray()));
 }
Esempio n. 17
0
 /// <inheritdoc/>
 public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
 {
     info.AddValue("public_key", PublicKey.Format(true));
     info.AddValue("app_protocol_version", AppProtocolVersion);
     info.AddValue("public_ip_address", PublicIPAddress?.ToString());
 }
Esempio n. 18
0
 public static IValue Serialize(this PublicKey key) => new Binary(key.Format(true));