Ejemplo n.º 1
0
 /// <summary>
 /// Verify schnorr signature.
 /// </summary>
 /// <param name="signature">schnorr signature</param>
 /// <param name="msg">32-byte msg</param>
 /// <param name="schnorrPubkey">pubkey</param>
 /// <returns>verify result</returns>
 public static bool Verify(SchnorrSignature signature, ByteData msg, SchnorrPubkey schnorrPubkey)
 {
     if (signature is null)
     {
         throw new ArgumentNullException(nameof(signature));
     }
     if (msg is null)
     {
         throw new ArgumentNullException(nameof(msg));
     }
     if (schnorrPubkey is null)
     {
         throw new ArgumentNullException(nameof(schnorrPubkey));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdVerifySchnorr(
             handle.GetHandle(), signature.ToHexString(),
             msg.ToHexString(),
             schnorrPubkey.ToHexString());
         if (ret == CfdErrorCode.Success)
         {
             return(true);
         }
         else if (ret != CfdErrorCode.SignVerificationError)
         {
             handle.ThrowError(ret);
         }
     }
     return(false);
 }
Ejemplo n.º 2
0
 public ExtPubkey(CfdNetworkType networkType, Pubkey parentPubkey,
                  Pubkey pubkey, ByteData chainCode, uint depth, uint childNumber)
 {
     if (parentPubkey is null)
     {
         throw new ArgumentNullException(nameof(parentPubkey));
     }
     if (pubkey is null)
     {
         throw new ArgumentNullException(nameof(pubkey));
     }
     if (chainCode is null)
     {
         throw new ArgumentNullException(nameof(chainCode));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdCreateExtkey(
             handle.GetHandle(), (int)networkType, (int)CfdExtKeyType.Pubkey,
             parentPubkey.ToHexString(), "", pubkey.ToHexString(),
             chainCode.ToHexString(), (byte)depth, childNumber, out IntPtr tempExtkey);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         extkey           = CCommon.ConvertToString(tempExtkey);
         this.networkType = networkType;
         this.pubkey      = pubkey;
         this.chainCode   = chainCode;
         this.depth       = depth;
         this.childNumber = childNumber;
         GetExtkeyInformation(handle, extkey, out version, out fingerprint,
                              out _, out _, out _, out _);
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Get tweaked Schnorr public key and private key.
 /// </summary>
 /// <param name="privkey">private key</param>
 /// <param name="tweak">tweak</param>
 /// <param name="tweakedPubkey">tweaked public key</param>
 /// <param name="tweakedParity">tweaked parity flag</param>
 /// <param name="tweakedPrivkey">tweaked private key</param>
 public static void GetTweakAddKeyPair(Privkey privkey, ByteData tweak,
                                       out SchnorrPubkey tweakedPubkey, out bool tweakedParity, out Privkey tweakedPrivkey)
 {
     if (privkey is null)
     {
         throw new ArgumentNullException(nameof(privkey));
     }
     if (tweak is null)
     {
         throw new ArgumentNullException(nameof(tweak));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdSchnorrKeyPairTweakAdd(
             handle.GetHandle(), privkey.ToHexString(), tweak.ToHexString(),
             out IntPtr tempPubkey, out tweakedParity, out IntPtr tempPrivkey);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         var pk = CCommon.ConvertToString(tempPubkey);
         var sk = CCommon.ConvertToString(tempPrivkey);
         tweakedPubkey  = new SchnorrPubkey(pk);
         tweakedPrivkey = new Privkey(sk);
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Sign ECDSA-adaptor.
 /// </summary>
 /// <param name="msg">32-byte msg</param>
 /// <param name="secretKey">secret key</param>
 /// <param name="adaptor">adaptor pubkey</param>
 /// <returns>ECDSA-adaptor pair</returns>
 public static AdaptorPair Sign(ByteData msg, Privkey secretKey, Pubkey adaptor)
 {
     if (msg is null)
     {
         throw new ArgumentNullException(nameof(msg));
     }
     if (secretKey is null)
     {
         throw new ArgumentNullException(nameof(secretKey));
     }
     if (adaptor is null)
     {
         throw new ArgumentNullException(nameof(adaptor));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdSignEcdsaAdaptor(
             handle.GetHandle(), msg.ToHexString(),
             secretKey.ToHexString(),
             adaptor.ToHexString(),
             out IntPtr signature,
             out IntPtr proof);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         string tempSig   = CCommon.ConvertToString(signature);
         string tempProof = CCommon.ConvertToString(proof);
         return(new AdaptorPair(new ByteData(tempSig), new ByteData(tempProof)));
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Sign schnorr with nonce.
 /// </summary>
 /// <param name="msg">32-byte msg</param>
 /// <param name="secretKey">secret key</param>
 /// <param name="nonce">32-byte nonce</param>
 /// <returns>schnorr signature</returns>
 public static SchnorrSignature SignWithNonce(ByteData msg, Privkey secretKey, ByteData nonce)
 {
     if (msg is null)
     {
         throw new ArgumentNullException(nameof(msg));
     }
     if (secretKey is null)
     {
         throw new ArgumentNullException(nameof(secretKey));
     }
     if (nonce is null)
     {
         throw new ArgumentNullException(nameof(nonce));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdSignSchnorrWithNonce(
             handle.GetHandle(), msg.ToHexString(),
             secretKey.ToHexString(),
             nonce.ToHexString(),
             out IntPtr signature);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         string tempSig = CCommon.ConvertToString(signature);
         return(new SchnorrSignature(tempSig));
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Compute signature point.
 /// </summary>
 /// <param name="msg">32-byte msg</param>
 /// <param name="nonce">schnorr nonce</param>
 /// <param name="schnorrPubkey">pubkey</param>
 /// <returns>signature point</returns>
 public static Pubkey ComputeSigPoint(ByteData msg, SchnorrPubkey nonce, SchnorrPubkey schnorrPubkey)
 {
     if (msg is null)
     {
         throw new ArgumentNullException(nameof(msg));
     }
     if (nonce is null)
     {
         throw new ArgumentNullException(nameof(nonce));
     }
     if (schnorrPubkey is null)
     {
         throw new ArgumentNullException(nameof(schnorrPubkey));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdComputeSchnorrSigPoint(
             handle.GetHandle(), msg.ToHexString(),
             nonce.ToHexString(), schnorrPubkey.ToHexString(),
             out IntPtr sigPoint);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         string point = CCommon.ConvertToString(sigPoint);
         return(new Pubkey(point));
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Extract ECDSA-adaptor secret key.
 /// </summary>
 /// <param name="adaptorSignature">adaptor signature</param>
 /// <param name="signature">signature</param>
 /// <param name="adaptor">adaptor pubkey</param>
 /// <returns>secret key</returns>
 public static Privkey ExtractSecret(ByteData adaptorSignature, ByteData signature, Pubkey adaptor)
 {
     if (adaptorSignature is null)
     {
         throw new ArgumentNullException(nameof(adaptorSignature));
     }
     if (signature is null)
     {
         throw new ArgumentNullException(nameof(signature));
     }
     if (adaptor is null)
     {
         throw new ArgumentNullException(nameof(adaptor));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdExtractEcdsaAdaptorSecret(
             handle.GetHandle(), adaptorSignature.ToHexString(),
             signature.ToHexString(),
             adaptor.ToHexString(),
             out IntPtr secret);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         string sk = CCommon.ConvertToString(secret);
         return(new Privkey(sk));
     }
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Check tweaked Schnorr public key from base public key.
 /// </summary>
 /// <param name="parity">parity flag</param>
 /// <param name="basePubkey">base public key</param>
 /// <param name="tweak">tweak</param>
 /// <returns>true or false</returns>
 public bool IsTweaked(bool parity, SchnorrPubkey basePubkey, ByteData tweak)
 {
     if (basePubkey is null)
     {
         throw new ArgumentNullException(nameof(basePubkey));
     }
     if (tweak is null)
     {
         throw new ArgumentNullException(nameof(tweak));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdCheckTweakAddFromSchnorrPubkey(
             handle.GetHandle(), data, parity, basePubkey.ToHexString(),
             tweak.ToHexString());
         if (ret == CfdErrorCode.Success)
         {
             return(true);
         }
         else if (ret != CfdErrorCode.SignVerificationError)
         {
             handle.ThrowError(ret);
         }
     }
     return(false);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Add branch.
 /// </summary>
 /// <param name="hash">branch hash.</param>
 public void AddBranch(ByteData hash)
 {
     if (hash is null)
     {
         throw new ArgumentNullException(nameof(hash));
     }
     if (hash.GetSize() != 32)
     {
         CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError, "Invalid branch hash size.");
     }
     AddBranch(hash.ToHexString());
 }
Ejemplo n.º 10
0
        private static void GetExtkeyInformation(ErrorHandle handle, string extkey,
                                                 out ByteData version, out ByteData fingerprint, out ByteData chainCode,
                                                 out uint depth, out uint childNumber, out CfdNetworkType networkType)
        {
            var ret = NativeMethods.CfdGetExtkeyInformation(
                handle.GetHandle(), extkey,
                out IntPtr tempVersion,
                out IntPtr tempFingerprint,
                out IntPtr tempChainCode,
                out depth,
                out childNumber);

            if (ret != CfdErrorCode.Success)
            {
                handle.ThrowError(ret);
            }
            string workVersion     = CCommon.ConvertToString(tempVersion);
            string workFingerprint = CCommon.ConvertToString(tempFingerprint);
            string workChainCode   = CCommon.ConvertToString(tempChainCode);

            version     = new ByteData(workVersion);
            fingerprint = new ByteData(workFingerprint);
            chainCode   = new ByteData(workChainCode);
            if (VersionMainnet == version.ToHexString())
            {
                networkType = CfdNetworkType.Mainnet;
            }
            else
            {
                networkType = CfdNetworkType.Testnet;
                if (VersionTestnet != version.ToHexString())
                {
                    CfdCommon.ThrowError(CfdErrorCode.IllegalArgumentError,
                                         "Failed to version format.");
                }
            }
        }
Ejemplo n.º 11
0
        protected static string ConvertTapscriptToString(Script tapscript, byte leafVersion)
        {
            if (tapscript is null)
            {
                throw new ArgumentNullException(nameof(tapscript));
            }
            string treeStr = "tl(" + tapscript.ToHexString();

            if (leafVersion != tapscriptLeafVersion)
            {
                ByteData data = new ByteData(new byte[] { leafVersion });
                treeStr += "," + data.ToHexString();
            }
            treeStr += ")";
            return(treeStr);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// normalize signature.
 /// </summary>
 /// <param name="signature">signature</param>
 /// <returns>normalized signature</returns>
 public static ByteData NormalizeSignature(ByteData signature)
 {
     if (signature is null)
     {
         throw new ArgumentNullException(nameof(signature));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdNormalizeSignature(
             handle.GetHandle(), signature.ToHexString(),
             out IntPtr normalizedSignature);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         return(new ByteData(CCommon.ConvertToString(normalizedSignature)));
     }
 }
Ejemplo n.º 13
0
 /// <summary>
 /// pubkey tweak mul.
 /// </summary>
 /// <param name="tweak">tweak bytes</param>
 /// <returns>tweaked pubkey</returns>
 public Pubkey TweakMul(ByteData tweak)
 {
     if (tweak is null)
     {
         throw new ArgumentNullException(nameof(tweak));
     }
     using (var handle = new ErrorHandle())
     {
         CfdErrorCode ret;
         ret = NativeMethods.CfdPubkeyTweakMul(
             handle.GetHandle(), pubkey, tweak.ToHexString(),
             out IntPtr tweakedKey);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         return(new Pubkey(CCommon.ConvertToString(tweakedKey)));
     }
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Get tweaked Schnorr public key.
 /// </summary>
 /// <param name="tweak">tweak</param>
 /// <param name="parity">parity flag</param>
 /// <returns>schnorr pubkey</returns>
 public SchnorrPubkey TweakAdd(ByteData tweak, out bool parity)
 {
     if (tweak is null)
     {
         throw new ArgumentNullException(nameof(tweak));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdSchnorrPubkeyTweakAdd(
             handle.GetHandle(), data, tweak.ToHexString(),
             out IntPtr tempPubkey, out parity);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         var pk = CCommon.ConvertToString(tempPubkey);
         return(new SchnorrPubkey(pk));
     }
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Verify ECDSA-adaptor signature.
 /// </summary>
 /// <param name="adaptorSignature">adaptor signature</param>
 /// <param name="adaptorProof">adaptor proof</param>
 /// <param name="adaptor">adaptor pubkey</param>
 /// <param name="msg">32-byte msg</param>
 /// <param name="pubkey">pubkey</param>
 /// <returns>verify result</returns>
 public static bool Verify(ByteData adaptorSignature, ByteData adaptorProof, Pubkey adaptor,
                           ByteData msg, Pubkey pubkey)
 {
     if (adaptorSignature is null)
     {
         throw new ArgumentNullException(nameof(adaptorSignature));
     }
     if (adaptorProof is null)
     {
         throw new ArgumentNullException(nameof(adaptorProof));
     }
     if (adaptor is null)
     {
         throw new ArgumentNullException(nameof(adaptor));
     }
     if (msg is null)
     {
         throw new ArgumentNullException(nameof(msg));
     }
     if (pubkey is null)
     {
         throw new ArgumentNullException(nameof(pubkey));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdVerifyEcdsaAdaptor(
             handle.GetHandle(), adaptorSignature.ToHexString(),
             adaptorProof.ToHexString(),
             adaptor.ToHexString(),
             msg.ToHexString(),
             pubkey.ToHexString());
         if (ret == CfdErrorCode.Success)
         {
             return(true);
         }
         else if (ret != CfdErrorCode.SignVerificationError)
         {
             handle.ThrowError(ret);
         }
     }
     return(false);
 }
Ejemplo n.º 16
0
 /// <summary>
 /// encode by DER.
 /// </summary>
 /// <param name="signature">signature</param>
 /// <param name="sighashType">sighash type</param>
 /// <returns>DER encoded data</returns>
 public static ByteData EncodeToDer(ByteData signature, SignatureHashType sighashType)
 {
     if (signature is null)
     {
         throw new ArgumentNullException(nameof(signature));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdEncodeSignatureByDer(
             handle.GetHandle(), signature.ToHexString(),
             sighashType.GetValue(),
             sighashType.IsSighashAnyoneCanPay,
             out IntPtr derSignature);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         return(new ByteData(CCommon.ConvertToString(derSignature)));
     }
 }
Ejemplo n.º 17
0
 /// <summary>
 /// constructor.
 /// </summary>
 /// <param name="seed">seed bytes</param>
 /// <param name="networkType">network type</param>
 public ExtPrivkey(ByteData seed, CfdNetworkType networkType)
 {
     if (seed is null)
     {
         throw new ArgumentNullException(nameof(seed));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdCreateExtkeyFromSeed(
             handle.GetHandle(), seed.ToHexString(), (int)networkType,
             (int)CfdExtKeyType.Privkey, out IntPtr tempExtkey);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         extkey = CCommon.ConvertToString(tempExtkey);
         GetExtkeyInformation(handle, extkey,
                              out version, out fingerprint,
                              out chainCode, out depth, out childNumber, out _);
         privkey          = GetPrivkeyFromExtKey(handle, extkey, networkType);
         this.networkType = networkType;
     }
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Calculate ec-signature.
 /// </summary>
 /// <param name="sighash">signature hash.</param>
 /// <param name="hasGrindR">use grind-R.</param>
 /// <returns></returns>
 public SignParameter CalculateEcSignature(ByteData sighash, bool hasGrindR)
 {
     if (sighash is null)
     {
         throw new ArgumentNullException(nameof(sighash));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdCalculateEcSignature(
             handle.GetHandle(), sighash.ToHexString(),
             privkey, privkeyWif, (int)networkType, hasGrindR,
             out IntPtr signatureHex);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         SignParameter     signature   = new SignParameter(CCommon.ConvertToString(signatureHex));
         SignatureHashType sighashType = new SignatureHashType(CfdSighashType.All, false);
         signature.SetDerEncode(sighashType);
         signature.SetRelatedPubkey(GetPubkey());
         return(signature);
     }
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Adapt ECDSA-adaptor signature.
 /// </summary>
 /// <param name="adaptorSignature">adaptor signature</param>
 /// <param name="adaptorSecret">adaptor secret key</param>
 /// <returns>ecdsa signature in compact format</returns>
 public static ByteData Adapt(ByteData adaptorSignature, Privkey adaptorSecret)
 {
     if (adaptorSignature is null)
     {
         throw new ArgumentNullException(nameof(adaptorSignature));
     }
     if (adaptorSecret is null)
     {
         throw new ArgumentNullException(nameof(adaptorSecret));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdAdaptEcdsaAdaptor(
             handle.GetHandle(), adaptorSignature.ToHexString(),
             adaptorSecret.ToHexString(),
             out IntPtr signature);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         string sig = CCommon.ConvertToString(signature);
         return(new ByteData(sig));
     }
 }
Ejemplo n.º 20
0
 /// <summary>
 /// convert entropy to mnemonic.
 /// </summary>
 /// <param name="entropy">entropy</param>
 /// <param name="language">language</param>
 /// <returns>mnemonic words</returns>
 public static string[] ConvertEntropyToMnemonic(
     ByteData entropy, string language)
 {
     if (entropy is null)
     {
         throw new ArgumentNullException(nameof(entropy));
     }
     if (language is null)
     {
         throw new ArgumentNullException(nameof(language));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdConvertEntropyToMnemonic(handle.GetHandle(),
                                                             entropy.ToHexString(),
                                                             language, out IntPtr tempMnemonic);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         string mnemonic = CCommon.ConvertToString(tempMnemonic);
         return(mnemonic.Split(' '));
     }
 }
Ejemplo n.º 21
0
 /// <summary>
 /// decode from DER.
 /// </summary>
 /// <param name="derSignature">DER encoded data</param>
 /// <returns>signature (SignParameter object)</returns>
 public static SignParameter DecodeFromDer(ByteData derSignature)
 {
     if (derSignature is null)
     {
         throw new ArgumentNullException(nameof(derSignature));
     }
     using (var handle = new ErrorHandle())
     {
         var ret = NativeMethods.CfdDecodeSignatureFromDer(
             handle.GetHandle(), derSignature.ToHexString(),
             out IntPtr signature,
             out int signatureHashType,
             out bool _);
         if (ret != CfdErrorCode.Success)
         {
             handle.ThrowError(ret);
         }
         string            signatureStr = CCommon.ConvertToString(signature);
         SignatureHashType sighashType  = new SignatureHashType(signatureHashType);
         SignParameter     signParam    = new SignParameter(signatureStr);
         signParam.SetDerEncode(sighashType);
         return(signParam);
     }
 }
Ejemplo n.º 22
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="controlBlock">control block</param>
        /// <param name="tapscript">tapscript</param>
        public TaprootScriptTree(ByteData controlBlock, Script tapscript)
        {
            if (controlBlock is null)
            {
                throw new ArgumentNullException(nameof(controlBlock));
            }
            if (tapscript is null)
            {
                throw new ArgumentNullException(nameof(tapscript));
            }

            using (var handle = new ErrorHandle())
                using (var treeHandle = new TreeHandle(handle))
                {
                    var ret = NativeMethods.CfdSetTapScriptByWitnessStack(handle.GetHandle(),
                                                                          treeHandle.GetHandle(), controlBlock.ToHexString(), tapscript.ToHexString(),
                                                                          out IntPtr internalPubkeyPtr);
                    if (ret != CfdErrorCode.Success)
                    {
                        handle.ThrowError(ret);
                    }
                    internalPubkey = new SchnorrPubkey(CCommon.ConvertToString(internalPubkeyPtr));
                    GetAllData(handle, treeHandle);
                }
        }