Пример #1
0
    /// <summary>
    /// Implement the signing algorithm.  In the case of an Ed25519
    /// it will use the private key to sign the transaction and
    /// return immediately.  In the case of the callback method, it
    /// will pass the invoice to the async method and async await
    /// for the method to return.
    /// </summary>
    /// <param name="invoice">
    /// The information for the transaction, including the Transaction
    /// ID, Memo and serialized bytes of the crypto transfers and other
    /// embedded information making up the transaction.
    /// </param>
    /// <returns></returns>
    async Task ISignatory.SignAsync(IInvoice invoice)
    {
        switch (_type)
        {
        case Type.Ed25519:
            Ed25519Util.Sign(invoice, (Ed25519PrivateKeyParameters)_data);
            break;

        case Type.ECDSASecp256K1:
            EcdsaSecp256k1Util.Sign(invoice, (ECPrivateKeyParameters)_data);
            break;

        case Type.List:
            foreach (ISignatory signer in (Signatory[])_data)
            {
                await signer.SignAsync(invoice).ConfigureAwait(false);
            }
            break;

        case Type.Callback:
            await((Func <IInvoice, Task>)_data)(invoice).ConfigureAwait(false);
            break;

        case Type.Pending:
            // This will be called to sign the to-be-scheduled
            // transaction. In this context, we do nothing.
            break;

        default:
            throw new InvalidOperationException("Not a presently supported Signatory key type, please consider the callback signatory as an alternative.");
        }
    }
        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());
        }
Пример #3
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);
        }