예제 #1
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)));
     }
 }
예제 #2
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));
     }
 }
예제 #3
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);
 }
예제 #4
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));
     }
 }
예제 #5
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));
     }
 }
        /// <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);
                }
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="pubkey">schnorr pubkey</param>
 /// <param name="controlBlock">control block</param>
 /// <param name="tapLeafHash">tapleaf hash</param>
 /// <param name="tapScript">tapscript</param>
 public TaprootScriptData(SchnorrPubkey pubkey, ByteData controlBlock,
                          ByteData256 tapLeafHash, Script tapScript)
 {
     Pubkey       = pubkey;
     ControlBlock = controlBlock;
     TapLeafHash  = tapLeafHash;
     TapScript    = tapScript;
 }
예제 #8
0
 /// <summary>
 /// convert hex from ByteData object.
 /// </summary>
 /// <param name="bytes">ByteData object</param>
 /// <returns>hex string</returns>
 public static string FromBytes(ByteData bytes)
 {
     if (bytes is null)
     {
         throw new ArgumentNullException(nameof(bytes));
     }
     return(FromBytes(bytes.ToBytes()));
 }
예제 #9
0
 /// <summary>
 /// constructor from seed.
 /// </summary>
 /// <param name="seed">seed bytes</param>
 public HDWallet(ByteData seed)
 {
     if (seed is null)
     {
         throw new ArgumentNullException(nameof(seed));
     }
     this.seed = seed;
 }
예제 #10
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());
 }
예제 #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);
        }
예제 #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)));
     }
 }
예제 #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)));
     }
 }
예제 #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));
     }
 }
예제 #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);
 }
예제 #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)));
     }
 }
예제 #17
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);
     }
 }
예제 #18
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;
     }
 }
예제 #19
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.");
                }
            }
        }
예제 #20
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));
     }
 }
예제 #21
0
 public ExtPubkey(CfdNetworkType networkType, ByteData fingerprint,
                  Pubkey pubkey, ByteData chainCode, uint depth, uint childNumber)
 {
     if (fingerprint is null)
     {
         throw new ArgumentNullException(nameof(fingerprint));
     }
     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, "",
             fingerprint.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.fingerprint = fingerprint;
         this.pubkey      = pubkey;
         this.chainCode   = chainCode;
         this.depth       = depth;
         this.childNumber = childNumber;
         GetExtkeyInformation(handle, extkey, out version, out _,
                              out _, out _, out _, out _);
     }
 }
예제 #22
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(' '));
     }
 }
예제 #23
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);
     }
 }
예제 #24
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="signature">adaptor signature</param>
 /// <param name="proof">adaptor proof</param>
 public AdaptorPair(ByteData signature, ByteData proof)
 {
     Signature = signature;
     Proof     = proof;
 }
예제 #25
0
 /// <summary>
 /// Get tree from control block.
 /// </summary>
 /// <param name="controlBlock">control block</param>
 /// <param name="tapscript">tapscript</param>
 /// <returns>Taproot script tree</returns>
 public static TaprootScriptTree GetFromControlBlock(
     ByteData controlBlock, Script tapscript)
 {
     return(new TaprootScriptTree(controlBlock, tapscript));
 }
예제 #26
0
 /// <summary>
 /// Verify schnorr signature.
 /// </summary>
 /// <param name="signature">schnorr signature</param>
 /// <param name="msg">32-byte msg</param>
 /// <returns>verify result</returns>
 public bool Verify(SchnorrSignature signature, ByteData msg)
 {
     return(SchnorrUtil.Verify(signature, msg, this));
 }
예제 #27
0
 /// <summary>
 /// Calculate ec-signature.
 /// </summary>
 /// <param name="sighash">signature hash.</param>
 /// <returns></returns>
 public SignParameter CalculateEcSignature(ByteData sighash)
 {
     return(CalculateEcSignature(sighash, true));
 }
예제 #28
0
 /// <summary>
 /// Sign schnorr.
 /// </summary>
 /// <param name="msg">32-byte msg</param>
 /// <param name="secretKey">secret key</param>
 /// <returns>schnorr signature</returns>
 public static SchnorrSignature Sign(ByteData msg, Privkey secretKey)
 {
     return(Sign(msg, secretKey, new ByteData()));
 }