/// <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)); } }
/// <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; } }
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); }
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> /// 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> /// 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> /// 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> /// 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); }
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> /// 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); } }
/// <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; } }
/// <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 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); } }
/// <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))); } }
internal void Load(ErrorHandle handle, TreeHandle treeHandle) { if (handle is null) { throw new ArgumentNullException(nameof(handle)); } if (treeHandle is null) { throw new ArgumentNullException(nameof(treeHandle)); } string nodes = ""; foreach (var node in targetNodes) { nodes += node.ToHexString(); } var ret = NativeMethods.CfdSetScriptTreeFromString(handle.GetHandle(), treeHandle.GetHandle(), treeString, tapscript.ToHexString(), leafVersion, nodes); if (ret != CfdErrorCode.Success) { handle.ThrowError(ret); } }
/// <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); }
/// <summary> /// create multisig script. /// </summary> /// <param name="requireNum">require num</param> /// <param name="pubkeys">pubkey list</param> /// <returns>multisig script</returns> public static Script CreateMultisigScript(uint requireNum, Pubkey[] pubkeys) { if (pubkeys is null) { throw new ArgumentNullException(nameof(pubkeys)); } using (var handle = new ErrorHandle()) { return(new Script(CreateMultisig(handle, requireNum, pubkeys))); } }
/// <summary> /// constructor. /// </summary> /// <param name="errorhandle">error handle</param> public TreeHandle(ErrorHandle errorhandle) { var ret = NativeMethods.CfdInitializeTaprootScriptTree( errorhandle.GetHandle(), out IntPtr newHandle); if ((ret != CfdErrorCode.Success) || (newHandle == IntPtr.Zero)) { throw new InvalidOperationException(); } handle = newHandle; errHandle = errorhandle; }
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> /// parse output descriptor. /// </summary> /// <param name="descriptorString">output descriptor</param> /// <param name="network">network type for address</param> public Descriptor(string descriptorString, CfdNetworkType network) { if (descriptorString is null) { throw new ArgumentNullException(nameof(descriptorString)); } using (var handle = new ErrorHandle()) { descriptor = GetDescriptorChecksum(handle, descriptorString, network); scriptList = ParseDescriptor(handle, descriptorString, "", network, out rootData); } }
/// <summary> /// constructor. /// </summary> public BlockHandle(ErrorHandle errorhandle, int networkType, string blockHex) { var ret = NativeMethods.CfdInitializeBlockHandle( errorhandle.GetHandle(), networkType, blockHex, out IntPtr newHandle); if ((ret != CfdErrorCode.Success) || (newHandle == IntPtr.Zero)) { throw new InvalidOperationException(); } handle = newHandle; errHandle = errorhandle; }
/// <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(string base58String) { if (base58String is null) { throw new ArgumentNullException(nameof(base58String)); } extkey = base58String; using (var handle = new ErrorHandle()) { GetExtkeyInformation(handle, extkey, out version, out fingerprint, out chainCode, out depth, out childNumber, out networkType); pubkey = GetPubkeyFromExtKey(handle, extkey, networkType); } }
/// <summary> /// constructor. /// </summary> /// <param name="addressString">address string.</param> public Address(string addressString) { using (var handle = new ErrorHandle()) { Initialize(handle, addressString, out CfdNetworkType outputNetwork, out CfdWitnessVersion outputWitnessVersion, out string outputLockingScript, out string outputHash); witnessVersion = outputWitnessVersion; hash = outputHash; network = outputNetwork; lockingScript = outputLockingScript; address = addressString; } }
/// <summary> /// Get transaction count in block. /// </summary> /// <returns>transaction count</returns> public uint GetTxCount() { 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); } return(txCount); } }