示例#1
0
    public void PublicAndPrivateKeysMatch()
    {
        var privateKey         = Ed25519Util.PrivateParamsFromDerOrRaw(_networkCredentials.PrivateKey);
        var generatedPublicKey = privateKey.GeneratePublicKey();
        var publicKey          = Ed25519Util.PublicParamsFromDerOrRaw(_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.PrivateParamsFromDerOrRaw(privateKey);
            break;

        case KeyType.ECDSASecp256K1:
            _type = Type.ECDSASecp256K1;
            _data = EcdsaSecp256k1Util.PrivateParamsFromDerOrRaw(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.");
        }
    }
示例#3
0
    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.PrivateParamsFromDerOrRaw(Hex.ToBytes(priKey));
        var checkPublicKey  = Ed25519Util.ToDerBytes(checkPrivateKey.GeneratePublicKey());
        var checkPublicHex  = Hex.FromBytes(checkPublicKey);

        Assert.Equal(pubKey, checkPublicHex);
    }