Exemplo n.º 1
0
        /// <summary>
        /// Sign the provided message using a Verifiable Random Function (VRF)
        /// and if the result is less than param threshold provide the proof.
        /// </summary>
        /// <param name="message">The raw bytes of the message to sign.</param>
        /// <param name="keypair">The keypair for signing.</param>
        /// <param name="threshold">Threshold (byte array, 16 bytes).</param>
        /// <param name="result">VRF signature output & proof.</param>
        /// <returns>True if VRF signature was successful (and result below threshold)</returns>
        public static bool VrfSignIfLess(byte[] message,
                                         SR25519Keypair keypair, byte[] threshold, out VrfSignResult result)
        {
            result = null;

            if (threshold?.Length != Constants.SR25519_VRF_THRESHOLD_SIZE)
            {
                throw new SR25519VrfException(StringConstants.BadVrfTresholdSizeMessage);
            }

            var vrfOutputAndProof = new byte[
                Constants.SR25519_VRF_OUTPUT_SIZE +
                Constants.SR25519_VRF_PROOF_SIZE
                                    ];

            var rc = Bindings.VrfSignIfLess(
                vrfOutputAndProof,
                keypair.GetBytes(),
                message,
                Convert.ToUInt64(message.Length),
                threshold);

            result = new VrfSignResult(rc, vrfOutputAndProof);
            return(rc.Result == Sr25519SignatureResult.Ok && rc.IsLess);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Soft derive a new keypair from an existing keypair.
        /// </summary>
        /// <param name="keypair">Input keypair.</param>
        /// <param name="chainCodeHex">Chain code as hex string.</param>
        /// <returns>SR25519Keypair</returns>
        public static SR25519Keypair SoftDeriveKeypair(SR25519Keypair keypair, string chainCodeHex)
        {
            byte[] chainCodeBytes = Utils.HexStringToByteArray(chainCodeHex);

            if (chainCodeBytes.Length != Constants.SR25519_CHAINCODE_SIZE)
            {
                throw new SR25519KeypairException(StringConstants.BadChaincodeSizeMessage);
            }

            var bytes   = keypair.GetBytes();
            var derived = new byte[Constants.SR25519_KEYPAIR_SIZE];

            Bindings.DeriveKeypairSoft(derived, bytes, chainCodeBytes);

            return(new SR25519Keypair(derived));
        }