public virtual void Sign(EthereumEcdsa localPrivateKey, EthereumEcdsa ephemeralPrivateKey, EthereumEcdsa receiverPublicKey, uint?chainID = null) { // Generate the shared secret using ECDH between our local private key and this remote public key byte[] ecdhKey = localPrivateKey.ComputeECDHKey(receiverPublicKey); // If our nonce is null, generate a new one Nonce = Nonce ?? RLPxSession.GenerateNonce(); // Verify the nonce is the correct length. if (Nonce.Length != RLPxSession.NONCE_SIZE) { // Throw an exception if an invalid nonce was provided. throw new ArgumentException($"Invalid size nonce provided for RLPx session when signing auth message. Should be {RLPxSession.NONCE_SIZE} bytes but was {Nonce?.Length}."); } // Obtain our transformed nonce data. byte[] transformedNonceData = GetTransformedNonce(ecdhKey); // Sign the transformed data. var signature = ephemeralPrivateKey.SignData(transformedNonceData); // We want our signature in r,s,v format. R = BigIntegerConverter.GetBytes(signature.r, 32); S = BigIntegerConverter.GetBytes(signature.s, 32); V = EthereumEcdsa.GetVFromRecoveryID(chainID, signature.RecoveryID); // Set our local public key and the public key hash. PublicKey = localPrivateKey.ToPublicKeyArray(false, true); }
protected void VerifyProperties() { // If our nonce is null, generate a new one Nonce = Nonce ?? RLPxSession.GenerateNonce(); // Verify the ephemeral public key is not null and is the correct size. if (EphemeralPublicKey?.Length != EthereumEcdsa.PUBLIC_KEY_SIZE) { throw new ArgumentException("Could not construct RLPx auth-ack because the provided ephemeral public key is not the correct size."); } // Verify the nonce is not null and is the correct size. if (Nonce.Length != RLPxSession.NONCE_SIZE) { throw new ArgumentException("Could not construct RLPx auth-ack because the provided nonce is not the correct size."); } }
protected void VerifyProperties() { // If our nonce is null, generate a new one Nonce = Nonce ?? RLPxSession.GenerateNonce(); // Verify the components are the correct size if (R?.Length != 32) { throw new ArgumentException("RLPx EIP8 auth serialization failed because the signature R component must be 32 bytes."); } else if (S?.Length != 32) { throw new ArgumentException("RLPx EIP8 auth serialization failed because the signature R component must be 32 bytes."); } else if (PublicKey?.Length != EthereumEcdsa.PUBLIC_KEY_SIZE) { throw new ArgumentException($"RLPx EIP8 auth serialization failed because the public key must be {EthereumEcdsa.PUBLIC_KEY_SIZE} bytes in size."); } else if (Nonce.Length != RLPxSession.NONCE_SIZE) { throw new ArgumentException($"RLPx EIP8 auth serialization failed because the nonce must be {RLPxSession.NONCE_SIZE} bytes in size."); } }