public void PublicAndPrivateKeysMatch()
        {
            var privateKey         = Ed25519Util.PrivateKeyParamsFromBytes(_networkCredentials.PrivateKey);
            var generatedPublicKey = privateKey.GeneratePublicKey();
            var publicKey          = Ed25519Util.PublicKeyParamsFromBytes(_networkCredentials.PublicKey);

            Assert.Equal(generatedPublicKey.GetEncoded(), publicKey.GetEncoded());
        }
示例#2
0
        /// <summary>
        /// Create a signatory having a private key of the specified type.
        /// </summary>
        /// <param name="type">
        /// The type of private key this <code>Signatory</code> should use to
        /// sign transactions.
        /// </param>
        /// <param name="privateKey">
        /// The bytes of a private key corresponding to the specified type.
        /// </param>
        /// <remarks>
        /// At this time, the library only supports Ed25519 keys, any other
        /// key type will result in an exception.  This is why this method
        /// is marked as <code>internal</code> at the moment.  When the library
        /// can support other key types, it will make sense to make this public.
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If any key type other than Ed25519 is used.
        /// </exception>
        internal Signatory(KeyType type, ReadOnlyMemory <byte> privateKey)
        {
            switch (type)
            {
            case KeyType.Ed25519:
                _type = Type.Ed25519;
                _data = Ed25519Util.PrivateKeyParamsFromBytes(privateKey);
                break;

            case KeyType.List:
                throw new ArgumentOutOfRangeException(nameof(type), "Only signatories representing a single key are supported with this constructor, please use the list constructor instead.");

            default:
                throw new ArgumentOutOfRangeException(nameof(type), "Not a presently supported Signatory key type, please consider the callback signatory as an alternative.");
            }
        }
        public void CanGenerateKeyPairFromBouncyCastle()
        {
            var privateKeyPrefix = Hex.ToBytes("302e020100300506032b657004220420").ToArray();
            var publicKeyPrefix  = Hex.ToBytes("302a300506032b6570032100").ToArray();

            var keyPairGenerator = new Ed25519KeyPairGenerator();

            keyPairGenerator.Init(new Ed25519KeyGenerationParameters(new SecureRandom()));
            var keyPair = keyPairGenerator.GenerateKeyPair();

            var privateKey = keyPair.Private as Ed25519PrivateKeyParameters;
            var publicKey  = keyPair.Public as Ed25519PublicKeyParameters;

            var pubKey = Hex.FromBytes(publicKeyPrefix.Concat(publicKey.GetEncoded()).ToArray());
            var priKey = Hex.FromBytes(privateKeyPrefix.Concat(privateKey.GetEncoded()).ToArray());

            var checkPrivateKey = Ed25519Util.PrivateKeyParamsFromBytes(Hex.ToBytes(priKey));
            var checkPublicKey  = Ed25519Util.ToBytes(checkPrivateKey.GeneratePublicKey());
            var checkPublicHex  = Hex.FromBytes(checkPublicKey);

            Assert.Equal(pubKey, checkPublicHex);
        }