/// <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> /// convert mnemonic to seed. /// </summary> /// <param name="mnemonic">mnemonic words</param> /// <param name="passphrase">passphrase</param> /// <param name="language">language</param> /// <param name="useIdeographicSpace">use ideographics space</param> /// <returns>seed bytes</returns> public static HDWallet ConvertMnemonicToSeed( string[] mnemonic, string passphrase, string language, bool useIdeographicSpace) { if (mnemonic is null) { throw new ArgumentNullException(nameof(mnemonic)); } if (passphrase is null) { throw new ArgumentNullException(nameof(passphrase)); } if (language is null) { throw new ArgumentNullException(nameof(language)); } string mnemonicJoinWord = string.Join(' ', mnemonic); using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdConvertMnemonicToSeed( handle.GetHandle(), Encoding.UTF8.GetBytes(mnemonicJoinWord.ToCharArray()), passphrase, true, language, useIdeographicSpace, out IntPtr tempSeed, out IntPtr tempEntropy); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } CCommon.ConvertToString(tempEntropy); string seedString = CCommon.ConvertToString(tempSeed); return(new HDWallet(new ByteData(seedString))); } }
/// <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))); } }
/// <summary> /// convert mnemonic to entropy /// </summary> /// <param name="mnemonic">mnemonic words</param> /// <param name="language">language</param> /// <returns>entropy</returns> public static ByteData ConvertMnemonicToEntropy( string[] mnemonic, string language) { if (mnemonic is null) { throw new ArgumentNullException(nameof(mnemonic)); } if (language is null) { throw new ArgumentNullException(nameof(language)); } string mnemonicJoinWord = string.Join(" ", mnemonic); using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdConvertMnemonicToSeed( handle.GetHandle(), Encoding.UTF8.GetBytes(mnemonicJoinWord.ToCharArray()), "", true, language, false, out IntPtr tempSeed, out IntPtr tempEntropy); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } CCommon.ConvertToString(tempSeed); string entropy = CCommon.ConvertToString(tempEntropy); return(new ByteData(entropy)); } }
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 _); } }
/// <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> /// Get pegin address. /// </summary> /// <param name="fedpegScript">fedpeg script</param> /// <param name="redeemScript">redeem script</param> /// <param name="hashType">hash type</param> /// <param name="network">network type</param> /// <returns>pegin address data</returns> public static PeginData GetPeginAddress(Script fedpegScript, Script redeemScript, CfdHashType hashType, CfdNetworkType network) { if (fedpegScript is null) { throw new ArgumentNullException(nameof(fedpegScript)); } if (redeemScript is null) { throw new ArgumentNullException(nameof(redeemScript)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdGetPeginAddress( handle.GetHandle(), (int)network, fedpegScript.ToHexString(), (int)hashType, "", redeemScript.ToHexString(), out IntPtr outputPeginAddress, out IntPtr outputClaimScript, out IntPtr outputFedpegScript); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } string peginAddress = CCommon.ConvertToString(outputPeginAddress); string claimScript = CCommon.ConvertToString(outputClaimScript); string tweakedFedpegScript = CCommon.ConvertToString(outputFedpegScript); return(new PeginData(new Address(peginAddress), new Script(claimScript), new Script(tweakedFedpegScript))); } }
/// <summary> /// Get taproot data. /// </summary> /// <param name="internalPubkey">internal pubkey</param> /// <returns>taproot data</returns> public TaprootScriptData GetTaprootData(SchnorrPubkey internalPubkey) { if (internalPubkey is null) { throw new ArgumentNullException(nameof(internalPubkey)); } using (var handle = new ErrorHandle()) using (var treeHandle = new TreeHandle(handle)) { Load(handle, treeHandle); var ret = NativeMethods.CfdGetTaprootScriptTreeHash( handle.GetHandle(), treeHandle.GetHandle(), internalPubkey.ToHexString(), out IntPtr witnessProgram, out IntPtr tapLeafHashPtr, out IntPtr controlBlockStr); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } var witnessProgramStr = CCommon.ConvertToString(witnessProgram); var tapLeafHash = CCommon.ConvertToString(tapLeafHashPtr); var controlBlock = CCommon.ConvertToString(controlBlockStr); return(new TaprootScriptData( new SchnorrPubkey(witnessProgramStr), new ByteData(controlBlock), new ByteData256(tapLeafHash), GetTapScript())); } }
/// <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); } }
public ExtPubkey DerivePubkey(uint[] path) { if (path is null) { throw new ArgumentNullException(nameof(path)); } using (var handle = new ErrorHandle()) { string childExtkey = extkey; foreach (uint childNum in path) { IntPtr tempExtkey = IntPtr.Zero; var ret = NativeMethods.CfdCreateExtkeyFromParent( handle.GetHandle(), childExtkey, childNum, false, (int)networkType, (int)CfdExtKeyType.Pubkey, out tempExtkey); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } childExtkey = CCommon.ConvertToString(tempExtkey); } return(new ExtPubkey(childExtkey)); } }
/// <summary> /// Get sign parameter object. /// </summary> /// <param name="signaturehashType">sighash type</param> /// <returns>sign parameter object.</returns> public SignParameter GetSignData(SignatureHashType signaturehashType) { var sig = data; var sigType = sighashType; if ((data.Length == Size * 2) && (signaturehashType.SighashType != CfdSighashType.Default)) { using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdAddSighashTypeInSchnorrSignature( handle.GetHandle(), data, signaturehashType.GetValue(), signaturehashType.IsSighashAnyoneCanPay, out IntPtr addedSignature); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } sig = CCommon.ConvertToString(addedSignature); } sigType = signaturehashType; } var signData = new SignParameter(sig); signData.SetSignatureHashType(sigType); return(signData); }
/// <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)); } }
/// <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)); } }
private static string[] ParseScript(ErrorHandle handle, string scriptHex) { var ret = NativeMethods.CfdParseScript( handle.GetHandle(), scriptHex, out IntPtr scriptItemHandle, out uint scriptItemNum); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } try { var items = new string[scriptItemNum]; for (uint index = 0; index < scriptItemNum; ++index) { IntPtr scriptItem = IntPtr.Zero; ret = NativeMethods.CfdGetScriptItem( handle.GetHandle(), scriptItemHandle, index, out scriptItem); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } items[index] = CCommon.ConvertToString(scriptItem); } return(items); } finally { NativeMethods.CfdFreeScriptItemHandle( handle.GetHandle(), scriptItemHandle); } }
/// <summary> /// Get txid list. /// </summary> /// <returns>txid list</returns> public Txid[] GetTxidList() { using (var handle = new ErrorHandle()) using (var txHandle = new BlockHandle(handle, defaultNetType, hex)) { var ret = NativeMethods.CfdGetTxCountInBlock( handle.GetHandle(), txHandle.GetHandle(), out uint txCount); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } var result = new Txid[txCount]; for (uint index = 0; index < txCount; ++index) { ret = NativeMethods.CfdGetTxidFromBlock( handle.GetHandle(), txHandle.GetHandle(), index, out IntPtr txidHex); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } var txid = CCommon.ConvertToString(txidHex); result[index] = new Txid(txid); } return(result); } }
static TapBranch GetBranchData(ErrorHandle handle, TreeHandle treeHandle) { TapBranch branch = new TapBranch(); var ret = NativeMethods.CfdGetTapBranchCount( handle.GetHandle(), treeHandle.GetHandle(), out uint count); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } TapBranch[] branchList = new TapBranch[count]; ret = NativeMethods.CfdGetBaseTapLeaf(handle.GetHandle(), treeHandle.GetHandle(), out byte leafVersion, out IntPtr tapscript, out IntPtr tapLeafHash); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } string tapscriptStr = CCommon.ConvertToString(tapscript); string tapLeafHashStr = CCommon.ConvertToString(tapLeafHash); if (count == 0) { branch.topHash = new ByteData256(tapLeafHashStr); } branch.branches = branchList; if (tapscriptStr.Length != 0) { branch.tapscript = new Script(tapscriptStr); branch.leafVersion = leafVersion; branch.targetNodes = new ByteData256[count]; } branch.UpdateBranchData(handle, treeHandle); return(branch); }
/// <summary> /// constructor for redeem script. /// </summary> /// <param name="script">redeem script</param> /// <param name="type">address type</param> /// <param name="networkType">network type</param> public Address(Script script, CfdAddressType type, CfdNetworkType networkType) { if (script is null) { throw new ArgumentNullException(nameof(script)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdCreateAddress( handle.GetHandle(), (int)type, "", script.ToHexString(), (int)networkType, out IntPtr outputAddress, out IntPtr outputLockingScript, out IntPtr outputP2shSegwitLockingScript); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } address = CCommon.ConvertToString(outputAddress); lockingScript = CCommon.ConvertToString(outputLockingScript); p2shSegwitLockingScript = CCommon.ConvertToString(outputP2shSegwitLockingScript); Initialize(handle, address, out _, out CfdWitnessVersion outputWitnessVersion, out _, out string outputHash); witnessVersion = outputWitnessVersion; hash = outputHash; network = networkType; addressType = type; } }
/// <summary> /// constructor for schnorr pubkey. /// </summary> /// <param name="pubkey">schnorr public key</param> /// <param name="type">address type</param> /// <param name="network">network type</param> public Address(SchnorrPubkey pubkey, CfdAddressType type, CfdNetworkType network) { if (pubkey is null) { throw new ArgumentNullException(nameof(pubkey)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdCreateAddress( handle.GetHandle(), (int)type, pubkey.ToHexString(), "", (int)network, out IntPtr outputAddress, out IntPtr outputLockingScript, out IntPtr outputP2shSegwitLockingScript); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } address = CCommon.ConvertToString(outputAddress); lockingScript = CCommon.ConvertToString(outputLockingScript); p2shSegwitLockingScript = CCommon.ConvertToString(outputP2shSegwitLockingScript); Initialize(handle, address, out network, out witnessVersion, out string tempLockingScript, out hash); this.network = network; addressType = type; } }
protected virtual void Dispose(bool disposing) { if (!disposed) { CCommon.CfdFreeHandle(handle); disposed = true; } }
private static string GetDescriptorChecksum(ErrorHandle handle, string descriptorString, CfdNetworkType network) { var ret = NativeMethods.CfdGetDescriptorChecksum(handle.GetHandle(), (int)network, descriptorString, out IntPtr descriptorAddedChecksum); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } return(CCommon.ConvertToString(descriptorAddedChecksum)); }
/// <summary> /// Serialize byte data. /// </summary> /// <returns>serialized byte data.</returns> public ByteData Serialize() { using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdSerializeByteData( handle.GetHandle(), data, out IntPtr output); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } return(new ByteData(CCommon.ConvertToString(output))); } }
public ExtPubkey GetExtPubkey() { using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdCreateExtPubkey( handle.GetHandle(), extkey, (int)networkType, out IntPtr tempExtkey); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } string childExtkey = CCommon.ConvertToString(tempExtkey); return(new ExtPubkey(childExtkey)); } }
/// <summary> /// get pubkey. /// </summary> /// <param name="isCompress">compressed pubkey flag</param> /// <returns>pubkey</returns> public Pubkey GetPubkey(bool isCompress) { using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdGetPubkeyFromPrivkey( handle.GetHandle(), privkey, "", isCompress, out IntPtr pubkeyHex); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } return(new Pubkey(CCommon.ConvertToString(pubkeyHex))); } }
/// <summary> /// negate pubkey. /// </summary> /// <returns>negated pubkey</returns> public Pubkey Negate() { using (var handle = new ErrorHandle()) { CfdErrorCode ret; ret = NativeMethods.CfdNegatePubkey( handle.GetHandle(), pubkey, out IntPtr negatedKey); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } return(new Pubkey(CCommon.ConvertToString(negatedKey))); } }
private static void Validate(string pubkeyHex) { using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdCompressPubkey( handle.GetHandle(), pubkeyHex, out IntPtr compressedPubkey); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } CCommon.ConvertToString(compressedPubkey); } }
/// <summary> /// Get block hash (internal). /// </summary> /// <param name="hex">block hex</param> /// <returns>block hash</returns> private static BlockHash GetHash(string hex) { using (var handle = new ErrorHandle()) using (var txHandle = new BlockHandle(handle, defaultNetType, hex)) { var ret = NativeMethods.CfdGetBlockHash( handle.GetHandle(), txHandle.GetHandle(), out IntPtr hash); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } var blockHash = CCommon.ConvertToString(hash); return(new BlockHash(blockHash)); } }
private static Pubkey GetPubkeyFromExtKey(ErrorHandle handle, string extPubkey, CfdNetworkType networkType) { var ret = NativeMethods.CfdGetPubkeyFromExtkey( handle.GetHandle(), extPubkey, (int)networkType, out IntPtr tempPubkey); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } string pubkeyHex = CCommon.ConvertToString(tempPubkey); return(new Pubkey(pubkeyHex)); }
/// <summary> /// create script from asm string. /// </summary> /// <param name="asm">asm string</param> /// <returns>script object</returns> public static Script CreateFromAsm(string asm) { if (asm is null) { throw new ArgumentNullException(nameof(asm)); } using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdConvertScriptAsmToHex(handle.GetHandle(), asm, out IntPtr hexString); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } return(new Script(CCommon.ConvertToString(hexString))); } }
public ExtPubkey DerivePubkey(uint childNumber) { using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdCreateExtkeyFromParent( handle.GetHandle(), extkey, childNumber, false, (int)networkType, (int)CfdExtKeyType.Pubkey, out IntPtr tempExtkey); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } string childExtkey = CCommon.ConvertToString(tempExtkey); return(new ExtPubkey(childExtkey)); } }
/// <summary> /// generate random privkey. /// </summary> /// <returns>privkey</returns> public static Privkey Generate() { using (var handle = new ErrorHandle()) { var ret = NativeMethods.CfdCreateKeyPair( handle.GetHandle(), true, (int)CfdNetworkType.Mainnet, out IntPtr pubkey, out IntPtr privkey, out IntPtr wif); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } CCommon.ConvertToString(pubkey); CCommon.ConvertToString(wif); return(new Privkey(CCommon.ConvertToString(privkey))); } }