示例#1
0
        /// <summary>
        /// Parse a variable-length public key into the pubkey object.
        /// This function supports parsing compressed (33 bytes, header byte 0x02 or
        /// 0x03), uncompressed(65 bytes, header byte 0x04), or hybrid(65 bytes, header
        /// byte 0x06 or 0x07) format public keys.
        /// </summary>
        /// <param name="publicKeyOutput">(Output) pointer to a pubkey object. If 1 is returned, it is set to a parsed version of input. If not, its value is undefined.</param>
        /// <param name="serializedPublicKey">Serialized public key.</param>
        /// <returns>True if the public key was fully valid, false if the public key could not be parsed or is invalid.</returns>
        public static unsafe bool PublicKeyParse(Span <byte> publicKeyOutput, Span <byte> serializedPublicKey)
        {
            var inputLen = serializedPublicKey.Length;

            if (inputLen != 33 && inputLen != 65)
            {
                throw new ArgumentException($"{nameof(serializedPublicKey)} must be 33 or 65 bytes");
            }

            if (publicKeyOutput.Length < 64)
            {
                throw new ArgumentException($"{nameof(publicKeyOutput)} must be {64} bytes");
            }

            fixed(byte *pubKeyPtr = &MemoryMarshal.GetReference(publicKeyOutput),
                  serializedPtr   = &MemoryMarshal.GetReference(serializedPublicKey))
            {
                return((Platform == OsPlatform.Windows
                    ? Win64Lib.secp256k1_ec_pubkey_parse(Context, pubKeyPtr, serializedPtr, (uint)inputLen)
                    : Platform == OsPlatform.Linux
                        ? PosixLib.secp256k1_ec_pubkey_parse(Context, pubKeyPtr, serializedPtr, (uint)inputLen)
                        : MacLib.secp256k1_ec_pubkey_parse(Context, pubKeyPtr, serializedPtr, (uint)inputLen)) == 1);
            }
        }