public void CanUseSegwitAddress() { var address = (BitcoinWitPubKeyAddress)BitcoinAddress.Create("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", Network.Main); var pubkey = new PubKey("0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"); Assert.Equal(pubkey.WitHash.ScriptPubKey.ToHex(), address.ScriptPubKey.ToHex()); Assert.Equal("0014751e76e8199196d454941c45d1b3a323f1433bd6", address.Hash.ScriptPubKey.ToHex()); Assert.Equal("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", address.ScriptPubKey.GetDestinationAddress(address.Network).ToString()); Assert.Equal("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", address.Hash.GetAddress(address.Network).ToString()); Assert.Equal(Network.Main, address.Network); Assert.Equal("tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx", address.ScriptPubKey.GetDestinationAddress(address.Network).ToNetwork(Network.TestNet).ToString()); address = (BitcoinWitPubKeyAddress)BitcoinAddress.Create("tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx", Network.TestNet); pubkey = new PubKey("0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"); Assert.Equal(pubkey.WitHash.ScriptPubKey.ToHex(), address.ScriptPubKey.ToHex()); Assert.Equal("tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx", address.ScriptPubKey.GetDestinationAddress(address.Network).ToString()); Assert.Equal("0014751e76e8199196d454941c45d1b3a323f1433bd6", address.Hash.ScriptPubKey.ToHex()); Assert.Equal("tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx", address.Hash.GetAddress(address.Network).ToString()); Assert.Equal(Network.TestNet, address.Network); Assert.Throws <FormatException>(() => BitcoinAddress.Create("tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx", Network.Main)); var addressScript = (BitcoinWitScriptAddress)BitcoinAddress.Create("bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", Network.Main); Assert.Equal(pubkey.ScriptPubKey.WitHash.ScriptPubKey.ToHex(), addressScript.ScriptPubKey.ToHex()); Assert.Equal("bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", addressScript.Hash.GetAddress(addressScript.Network).ToString()); Assert.Equal("bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", addressScript.ScriptPubKey.GetDestinationAddress(addressScript.Network).ToString()); //Example of the BIP pubkey = new PubKey("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6"); Assert.Equal(new Script("OP_0 010966776006953D5567439E5E39F86A0D273BEE"), pubkey.GetSegwitAddress(Network.Main).ScriptPubKey); //Test .ToNetwork() var addr = pubkey.GetAddress(Network.Main); Assert.Equal("16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM", addr.ToString()); Assert.Equal("mfcSEPR8EkJrpX91YkTJ9iscdAzppJrG9j", addr.ToNetwork(Network.TestNet).ToString()); Assert.Throws <FormatException>(() => Network.Parse <IBase58Data>("bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", Network.Main)); Network.Parse <IBitcoinString>("bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", Network.Main); Assert.Throws <Bech32FormatException>(() => Network.Parse <IBitcoinString>("bc1qrp33g0q5c3txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", Network.Main)); Assert.Throws <Bech32FormatException>(() => new BitcoinWitScriptAddress("bc1qrp33g0q5c3txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", Network.Main)); Assert.Throws <Bech32FormatException>(() => new BitcoinWitPubKeyAddress("bc1qw507d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", Network.Main)); var addr1 = new BitcoinWitPubKeyAddress("tb1qr5d68t6qm8t2n7ch4nph3ha4prztteuw98ewda", Network.TestNet); var addr2 = new BitcoinWitPubKeyAddress("tb1qr5d68t6qm8t2n7ch4nph3ha4prztteuw98ewda"); Assert.Equal(addr1, addr2); }
// COLORED COINS public static void DemoKeys() { Key key = new Key(); PubKey pubKey = key.PubKey; var secret = key.GetBitcoinSecret(Network.Main); Console.WriteLine(secret); // L2He2yhKupreS8RoLkW6mXS5uZvSBsr3F3NBYAhbZ8jsvrWLiDbL Console.WriteLine(pubKey.GetAddress(Network.Main)); // 1D8P2cBtv74vXdTJLE6hbba88q4QdQVyox Console.WriteLine(pubKey.Hash.ScriptPubKey); // OP_DUP OP_HASH160 8506cf167fe11cc4fc9165590263a4ac830ac3ea OP_EQUALVERIFY OP_CHECKSIG }
// This uses my WIF private key and shows the address public void SecretToAddress() { BitcoinSecret frank = new BitcoinSecret("cSRGkyQ8Ny3QEMxHYp3BnDPAibQYReg4g1Nso7jfrAUgH79sjzsL"); Key pk = frank.PrivateKey; PubKey publicKey = pk.PubKey; //var publicKeyHash = publicKey.Hash; var testNetAddress = publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.TestNet); Console.WriteLine("testNetAddress: " + testNetAddress); // n1T57TZPCLqFJy1JVHBvrs25druomRxwVS }
static void Main() { //Bitcoin Address is what you share to the world to get paid //-> All i need is address to send $$$ //Use this key to spend $$$, and no need to access to the internet Key privateKey = new Key(); // generate a random private key Console.WriteLine($"Here is the privateKey looks like **{privateKey.ToString()}"); //From here use one way crypto to generate public key PubKey publicKey = privateKey.PubKey; Console.WriteLine($"One way gen public key from privateKey **{publicKey}"); // 0251036303164f6c458e9f7abecb4e55e5ce9ec2b2f1d06d633c9653a07976560c //There are 2 Mian Bitcoin Networks // - TestNet -> Get free coins to test, no big deal // - MainNet //This is how I get the address from TestNet and MainNet & get paid!! yahooo... Console.WriteLine(publicKey.GetAddress(Network.Main)); // 1PUYsjwfNmX64wS368ZR5FMouTtUmvtmTY Console.WriteLine(publicKey.GetAddress(Network.TestNet)); // n3zWAo2eBnxLr3ueohXnuAa8mTVBhxmPhq //**Proper way to get the bitcoin address //Fact: The hash of the public key is generated by performing a //SHA256 hash on the public key, and then performing a RIPEMD160 hash //on the result, with Big Endian notation. //The function could look like this: RIPEMD160(SHA256(pubkey)) var publicKeyHash = publicKey.Hash; Console.WriteLine("Generate to get bitcoin address " + publicKeyHash); // f6889b21b5540353a29ed18c45ea0031280c42cf var mainNetAddress = publicKeyHash.GetAddress(Network.Main); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); Console.WriteLine($"The MainNet Address {mainNetAddress}"); // 1PUYsjwfNmX64wS368ZR5FMouTtUmvtmTY Console.WriteLine($"The TestNet Address {testNetAddress}"); // n3zWAo2eBnxLr3ueohXnuAa8mTVBhxmPhq Console.ReadLine(); }
public void createaddress(out string publickey, out BitcoinSecret privatekey) { Key privateKey = new Key(); PubKey publicKey = privateKey.PubKey; publickey = publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main).ToString(); privatekey = privateKey.GetWif(Network.Main); HttpContext.Current.Session["publickey"] = publickey.ToString(); HttpContext.Current.Session["privkey"] = privatekey.ToString(); }
static void Main(string[] args) { Key privateKey = new Key(); PubKey publicKey = privateKey.PubKey; BitcoinSecret mainNetPrivateKey = privateKey.GetBitcoinSecret(Network.Main); Console.WriteLine("Public key: {0} ", publicKey); Console.WriteLine("address: {0} ", publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.TestNet)); Console.WriteLine("Private key: {0} ", mainNetPrivateKey); }
public async Task <bool> Verify(string trHex, string pubKey, SigHash hashType = SigHash.All) { PubKey checkPubKey = new PubKey(pubKey); var tr = new Transaction(trHex); for (var i = 0; i < tr.Inputs.Count; i++) { var input = tr.Inputs[i]; var redeemScript = PayToScriptHashTemplate.Instance.ExtractScriptSigParameters(input.ScriptSig)?.RedeemScript ?? PayToScriptHashTemplate.Instance.ExtractScriptSigParameters(input.WitScript)?.RedeemScript; if (redeemScript != null) { if (PayToMultiSigTemplate.Instance.CheckScriptPubKey(redeemScript)) { var pubkeys = PayToMultiSigTemplate.Instance.ExtractScriptPubKeyParameters(redeemScript).PubKeys; for (int j = 0; j < pubkeys.Length; j++) { if (pubkeys[j] == checkPubKey) { var scriptParams = PayToScriptHashTemplate.Instance.ExtractScriptSigParameters(input.ScriptSig); var hash = Script.SignatureHash(scriptParams.RedeemScript, tr, i, hashType); if (!checkPubKey.Verify(hash, scriptParams.Pushes[j + 1])) { return(false); } } } continue; } } var prevTransaction = await _bitcoinTransactionService.GetTransaction(input.PrevOut.Hash.ToString()); var output = prevTransaction.Outputs[input.PrevOut.N]; if (PayToPubkeyHashTemplate.Instance.CheckScriptPubKey(output.ScriptPubKey)) { if (output.ScriptPubKey.GetDestinationAddress(_rpcParams.Network) == checkPubKey.GetAddress(_rpcParams.Network)) { var hash = Script.SignatureHash(output.ScriptPubKey, tr, i, hashType); var sign = PayToPubkeyHashTemplate.Instance.ExtractScriptSigParameters(input.ScriptSig)?.TransactionSignature?.Signature; if (sign == null) { return(false); } if (!checkPubKey.Verify(hash, sign)) { return(false); } } } } return(true); }
void Start() { Key privateKey = new Key(); // generate a random private key PubKey publicKey = privateKey.PubKey; UnityEngine.Debug.Log(publicKey); // 0251036303164f6c458e9f7abecb4e55e5ce9ec2b2f1d06d633c9653a07976560c UnityEngine.Debug.Log(publicKey.GetAddress(Network.Main)); // 1PUYsjwfNmX64wS368ZR5FMouTtUmvtmTY UnityEngine.Debug.Log(publicKey.GetAddress(Network.TestNet)); // n3zWAo2eBnxLr3ueohXnuAa8mTVBhxmPhq var publicKeyHash = publicKey.Hash; UnityEngine.Debug.Log(publicKeyHash); // f6889b21b5540353a29ed18c45ea0031280c42cf var mainNetAddress = publicKeyHash.GetAddress(Network.Main); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); UnityEngine.Debug.Log(mainNetAddress); // 1PUYsjwfNmX64wS368ZR5FMouTtUmvtmTY UnityEngine.Debug.Log(testNetAddress); // n3zWAo2eBnxLr3ueohXnuAa8mTVBhxmPhq }
public static string[] generateKeys() { Key privKey = new Key(); BitcoinSecret mainNetPrivateKey = privKey.GetBitcoinSecret(Network.Main); PubKey pubKey = privKey.PubKey; string[] Keys = new string[2]; Keys[0] = mainNetPrivateKey.ToString(); Keys[1] = pubKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main).ToString(); return(Keys); }
public WhatIsPublicKey(PubKey pubkey, Network network) { Hex = pubkey.ToHex(); Address = new WhatIsAddress(pubkey.GetAddress(network)); P2SHAddress = new WhatIsAddress(pubkey.ScriptPubKey.GetScriptAddress(network)) { RedeemScript = new WhatIsScript(pubkey.ScriptPubKey, network) }; ScriptPubKey = new WhatIsScript(pubkey.ScriptPubKey, network); IsCompressed = pubkey.IsCompressed; }
//-------------------------------------------------------------------------------- void Sample_01_BitcoinAddress() { Key privateKey = new Key(); // generate random private key PubKey publicKey = privateKey.PubKey; // generate public key from private key KeyId publicKeyHash = publicKey.Hash; // generate public key hash Debug.Log(publicKey + " <-------------- publicKey"); Debug.Log(publicKeyHash + " <-------------- publicKeyHash"); Debug.Log(publicKey.GetAddress(NBitcoin.Network.TestNet) + " <-------------- testNetAdress from public Key"); Debug.Log(publicKeyHash.GetAddress(NBitcoin.Network.TestNet) + " <-------------- testNetAdress from public Key Hash"); }
static void Main() { Key privateKey = new Key(); // generate a random private key PubKey publicKey = privateKey.PubKey; Console.WriteLine(publicKey); // 0251036303164f6c458e9f7abecb4e55e5ce9ec2b2f1d06d633c9653a07976560c Console.WriteLine(publicKey.GetAddress(Network.Main)); // 1PUYsjwfNmX64wS368ZR5FMouTtUmvtmTY Console.WriteLine(publicKey.GetAddress(Network.TestNet)); // n3zWAo2eBnxLr3ueohXnuAa8mTVBhxmPhq var publicKeyHash = publicKey.Hash; Console.WriteLine(publicKeyHash); // f6889b21b5540353a29ed18c45ea0031280c42cf var mainNetAddress = publicKeyHash.GetAddress(Network.Main); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); Console.WriteLine(mainNetAddress); // 1PUYsjwfNmX64wS368ZR5FMouTtUmvtmTY Console.WriteLine(testNetAddress); // n3zWAo2eBnxLr3ueohXnuAa8mTVBhxmPhq Console.ReadLine(); }
public static (BitcoinAddress, BitcoinSecret) GetKeyAndAddress() { BitcoinAddress addr = null; Key privateKey = new Key(); // generate a random private key PubKey publicKey = privateKey.PubKey; var secret = privateKey.GetBitcoinSecret(NeblioTransactionHelpers.Network); var address = publicKey.GetAddress(ScriptPubKeyType.Legacy, NeblioTransactionHelpers.Network).ToString(); addr = BitcoinAddress.Create(address, NeblioTransactionHelpers.Network); return(addr, secret); }
static void Main() { Key privateKey = new Key(); // From the private key, we use a one-way cryptographic function, to generate a public key. PubKey publicKey = privateKey.PubKey; Console.WriteLine(publicKey); // Get your addresses from your public key Console.WriteLine(publicKey.GetAddress(Network.Main)); Console.WriteLine(publicKey.GetAddress(Network.TestNet)); var publicKeyHash = publicKey.Hash; Console.WriteLine(publicKeyHash); var mainNetAddress = publicKeyHash.GetAddress(Network.Main); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); Console.WriteLine(mainNetAddress); Console.WriteLine(testNetAddress); Console.WriteLine(mainNetAddress.ScriptPubKey); Console.WriteLine(testNetAddress.ScriptPubKey); var paymentScript = publicKeyHash.ScriptPubKey; var sameMainNetAddress = paymentScript.GetDestinationAddress(Network.Main); Console.WriteLine(mainNetAddress == mainNetAddress); var samePublicKeyHash = (KeyId)paymentScript.GetDestination(); Console.WriteLine(publicKeyHash == samePublicKeyHash); var sameMainNetAddress2 = new BitcoinPubKeyAddress(samePublicKeyHash, Network.Main); Console.WriteLine(mainNetAddress == sameMainNetAddress2); }
static void Main(string[] args) { Key privateKey = new Key(); //generate a random private key //from the private key, we use a one-way cryptography function, to generate a public key. PubKey publicKey = privateKey.PubKey; Console.WriteLine(publicKey); //03d15421b7301726d1576b3b75c3d986c77b75d7126bbc3cca176f869777070e85 //you can easily get your bitcoin address from your public key and the network on which this address should be used Console.WriteLine(publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main)); //1ANN5FJ1DXuqSJJwHypomjyGwdLbAAHb5i Console.WriteLine(publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.TestNet)); //mptKNJNz2ZM6DQnZ1YoBbfBbocwJ4uhSqQ var publicKeyHash = publicKey.Hash; Console.WriteLine(publicKey); //0319330d98c1ec6e7683640a9957d453cbb7088d01bd7005e074e8ccf8e5709f83 var mainNetAddress = publicKeyHash.GetAddress(Network.Main); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); Console.WriteLine(mainNetAddress); // 1MYHsmESjyjdxMFtvuLB77mqbiqzr3ghoZ Console.WriteLine(testNetAddress); // n24FApKRZ1AtjTjWeUJYw2zATiShhnMWU9 }
public async Task <IActionResult> IndexAsync() { var user = await _userManager.GetUserAsync(HttpContext.User); var adress = _context.UserDetails.FirstOrDefault(item => item.user_id == Guid.Parse(user.Id)); var wallet_pk = adress.user_adress; PubKey temp_pk = new PubKey(wallet_pk); ViewBag.adress = temp_pk.GetAddress(ScriptPubKeyType.Legacy, Network.TestNet); return(View()); }
public bool Check(string passphrase, BitcoinAddress expectedAddress) { //Derive passfactor using scrypt with ownerentropy and the user's passphrase and use it to recompute passpoint var passfactor = BitcoinEncryptedSecretEC.CalculatePassFactor(passphrase, this.LotSequence, this.OwnerEntropy); //Derive decryption key for pointb using scrypt with passpoint, addresshash, and ownerentropy var passpoint = BitcoinEncryptedSecretEC.CalculatePassPoint(passfactor); var derived = BitcoinEncryptedSecretEC.CalculateDecryptionKey(passpoint, this.AddressHash, this.OwnerEntropy); //Decrypt encryptedpointb to yield pointb var pointbprefix = this.EncryptedPointB[0]; pointbprefix = (byte)(pointbprefix ^ (byte)(derived[63] & 0x01)); //Optional since ArithmeticException will catch it, but it saves some times if (pointbprefix != 0x02 && pointbprefix != 0x03) { return(false); } var pointb = BitcoinEncryptedSecret.DecryptKey(this.EncryptedPointB.Skip(1).ToArray(), derived); pointb = new[] { pointbprefix }.Concat(pointb).ToArray(); //4.ECMultiply pointb by passfactor. Use the resulting EC point as a public key var curve = ECKey.Secp256k1; ECPoint pointbec; try { pointbec = curve.Curve.DecodePoint(pointb); } catch (ArgumentException) { return(false); } catch (ArithmeticException) { return(false); } var pubkey = new PubKey(pointbec.Multiply(new BigInteger(1, passfactor)).GetEncoded()); //and hash it into address using either compressed or uncompressed public key methodology as specifid in flagbyte. pubkey = this.IsCompressed ? pubkey.Compress() : pubkey.Decompress(); var actualhash = BitcoinEncryptedSecretEC.HashAddress(pubkey.GetAddress(this.Network)); var expectedhash = BitcoinEncryptedSecretEC.HashAddress(expectedAddress); return(Utils.ArrayEqual(actualhash, expectedhash)); }
public void ListSecrets(NetworkCredential credentials, string host, Network net) { RPCClient client = new RPCClient("usuaraw:SupEr3421aw", new Uri("127.0.0.1"), net); IEnumerable <BitcoinSecret> secrets = client.ListSecrets(); foreach (BitcoinSecret secret in secrets) { PubKey pub = secret.PubKey; Console.WriteLine("key: " + secret.PrivateKey.GetWif(net)); Console.WriteLine("secret: " + secret); Console.WriteLine("pub: " + pub.ToHex()); Console.WriteLine("addr: " + pub.GetAddress(net)); } }
public static void Execute(Key privateKey) { // use a one-way cryptographic function, to generate a public key. PubKey publicKey = privateKey.PubKey; Console.WriteLine("My publicKey: " + publicKey); // get your bitcoin address from your public key and the network Console.WriteLine(); Console.WriteLine("MainNet bitcoin address: " + publicKey.GetAddress(Network.Main)); Console.WriteLine("TestNet bitcoin address: " + publicKey.GetAddress(Network.TestNet)); var publicKeyHash = publicKey.Hash; var mainNetAddress = publicKeyHash.GetAddress(Network.Main); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); Console.WriteLine(); Console.WriteLine("publicKeyHash: " + publicKeyHash); Console.WriteLine("mainNetAddress: " + mainNetAddress); Console.WriteLine("testNetAddress: " + testNetAddress); Console.ReadLine(); }
/// <summary> /// Dictionary<string1, string2> /// /// string1: private key /// string2: bitcoin address /// </summary> /// <returns></returns> public Dictionary <string, string> CreateAddress() { var dic = new Dictionary <string, string>(); Key privateKey = new Key(); // generate a random private key //BitcoinSecret mainNetPrivateKey = privateKey.GetBitcoinSecret(Network.Main); // bitcoin secret PubKey publicKey = privateKey.PubKey; var address = publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main); dic.Add(privateKey.ToString(Network.Main), address.ToString()); return(dic); }
/// <summary> /// Creates a number of additional addresses in the current account. /// </summary> /// <remarks> /// The name given to the account is of the form "account (i)" by default, where (i) is an incremental index starting at 0. /// According to BIP44, an account at index (i) can only be created when the account at index (i - 1) contains at least one transaction. /// </remarks> /// <param name="network">The network these addresses will be for.</param> /// <param name="addressesQuantity">The number of addresses to create.</param> /// <param name="isChange">Whether the addresses added are change (internal) addresses or receiving (external) addresses.</param> /// <returns>The created addresses.</returns> public IEnumerable <HdAddress> CreateAddresses(Network network, int addressesQuantity, bool isChange = false) { ICollection <HdAddress> addresses = isChange ? this.InternalAddresses : this.ExternalAddresses; // Get the index of the last address. int firstNewAddressIndex = 0; if (addresses.Any()) { firstNewAddressIndex = addresses.Max(add => add.Index) + 1; } var addressesCreated = new List <HdAddress>(); for (int i = firstNewAddressIndex; i < firstNewAddressIndex + addressesQuantity; i++) { // Retrieve the pubkey associated with the private key of this address index. PubKey pubkey = HdOperations.GeneratePublicKey(this.ExtendedPubKey, i, isChange); // Generate the P2PKH address corresponding to the pubkey. BitcoinPubKeyAddress address = pubkey.GetAddress(network); BitcoinWitPubKeyAddress witAddress = pubkey.GetSegwitAddress(network); // Add the new address details to the list of addresses. var newAddress = new HdAddress { Index = i, HdPath = HdOperations.CreateHdPath((int)this.GetCoinType(), this.Index, isChange, i), ScriptPubKey = address.ScriptPubKey, Pubkey = pubkey.ScriptPubKey, Bech32Address = witAddress.ToString(), Address = address.ToString(), }; addresses.Add(newAddress); addressesCreated.Add(newAddress); } if (isChange) { this.InternalAddresses = addresses; } else { this.ExternalAddresses = addresses; } return(addressesCreated); }
private static void Main(string[] args) { // signing a message var bitcoinPrivateKey = new BitcoinSecret("KzgjNRhcJ3HRjxVdFhv14BrYUKrYBzdoxQyR2iJBHG9SNGGgbmtC"); const string message = "Craig Wright is a fraud"; string signature = bitcoinPrivateKey.PrivateKey.SignMessage(message); Console.WriteLine(signature); var genesisBlockAddress = new BitcoinPubKeyAddress("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"); var isCraigWrightSatoshi = genesisBlockAddress.VerifyMessage(message, signature); Console.WriteLine($"Is Craig Wright Satoshi? {isCraigWrightSatoshi}!"); var bookAddress = new BitcoinPubKeyAddress("1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB"); const string DoriersMessage = "Nicolas Dorier Book Funding Address"; const string DoriersSignature = "H1jiXPzun3rXi0N9v9R5fAWrfEae9WPmlL5DJBj1eTStSvpKdRR8Io6/uT9tGH/3OnzG6ym5yytuWoA9ahkC3dQ="; var isDorierTheBookAddressOwner = bookAddress.VerifyMessage(DoriersMessage, DoriersSignature); Console.WriteLine(isDorierTheBookAddressOwner); // get first genisis block Block genesisBlock = Network.Main.GetGenesis(); // get first tx Transaction firstEverTransaction = genesisBlock.Transactions.FirstOrDefault(); // get first output TxOut firstEverOutput = firstEverTransaction?.Outputs.FirstOrDefault(); // get first script pub key Script firstEverScriptPubKey = firstEverOutput?.ScriptPubKey; // get first public key PubKey firstEverPubKey = firstEverScriptPubKey?.GetDestinationPublicKeys().FirstOrDefault(); // get bitcoin address from public key with network identifier BitcoinPubKeyAddress firstEverBitcoinAddress = firstEverPubKey?.GetAddress(Network.Main); Console.WriteLine($"{genesisBlock}\n{firstEverTransaction}\n" + $"{firstEverOutput}\n{firstEverScriptPubKey}\n" + $"{firstEverPubKey}\n{firstEverBitcoinAddress}"); Console.ReadLine(); }
// 演習1: 秘密鍵およびBitcoinアドレス鍵の生成 public static void createKeys() { Key privateKey = new Key(); //秘密鍵の生成 // 秘密鍵は通常、WIF(Wallet Import Format) = Bitcoin Secret形式で扱う BitcoinSecret testNetSecret = privateKey.GetBitcoinSecret(Network.TestNet); // NetWork.Main / Network.TestNet Console.WriteLine("Bitcoin Secret(WIF): {0}", testNetSecret); PubKey publicKey = privateKey.PubKey; //公開鍵の生成 // Bitcoin Addressへ変換 BitcoinPubKeyAddress testNetAddress = publicKey.GetAddress(Network.TestNet); Console.WriteLine("Address: {0}", testNetAddress); }
private void ExecuteReceiveBitcoin() { if (WalletNotNull && MainWallet.GetWalletState().Equals(WalletModelState.Ready)) { PubKey newScript = MainWallet.GetNewPubKey(); string address = newScript.GetAddress(MainWallet.NetworkChoice).ToString(); Messenger.Default.Send <string>(address, "OpenReceiveView"); } else if (WalletNotNull && MainWallet.GetWalletState().Equals(WalletModelState.Preparing)) { this.ShowWalletLoadingDialog(); } else { this.ShowNoActiveWalletDialog(); } }
/// <summary> /// Creates a number of additional addresses in the current account. /// </summary> /// <remarks> /// The name given to the account is of the form "account (i)" by default, where (i) is an incremental index starting at 0. /// According to BIP44, an account at index (i) can only be created when the account at index (i - 1) contains at least one transaction. /// </remarks> /// <param name="network">The network these addresses will be for.</param> /// <param name="addressesQuantity">The number of addresses to create.</param> /// <param name="isChange">Whether the addresses added are change (internal) addresses or receiving (external) addresses.</param> /// <returns>The created addresses.</returns> public IEnumerable <HdAddress> CreateAddresses(Network network, int addressesQuantity, bool isChange = false) { var addresses = isChange ? this.InternalAddresses : this.ExternalAddresses; // Get the index of the last address. int firstNewAddressIndex = 0; if (addresses.Any()) { firstNewAddressIndex = addresses.Max(add => add.Index) + 1; } List <HdAddress> addressesCreated = new List <HdAddress>(); for (int i = firstNewAddressIndex; i < firstNewAddressIndex + addressesQuantity; i++) { // Generate a new address. PubKey pubkey = HdOperations.GeneratePublicKey(this.ExtendedPubKey, i, isChange); BitcoinPubKeyAddress address = pubkey.GetAddress(network); // Add the new address details to the list of addresses. HdAddress newAddress = new HdAddress { Index = i, HdPath = HdOperations.CreateHdPath((int)this.GetCoinType(), this.Index, i, isChange), ScriptPubKey = address.ScriptPubKey, Pubkey = pubkey.ScriptPubKey, Address = address.ToString(), Transactions = new List <TransactionData>() }; addresses.Add(newAddress); addressesCreated.Add(newAddress); } if (isChange) { this.InternalAddresses = addresses; } else { this.ExternalAddresses = addresses; } return(addressesCreated); }
public static IBitcoinBasedTransaction CreateSwapRefundTx( BitcoinBasedCurrency currency, IEnumerable <ITxOutput> outputs, PubKey from, PubKey to, int amount, int fee, DateTimeOffset lockTime) { return(currency.CreateSwapRefundTx( unspentOutputs: outputs, destinationAddress: to.GetAddress(ScriptPubKeyType.Legacy, currency.Network).ToString(), changeAddress: from.GetAddress(ScriptPubKeyType.Legacy, currency.Network).ToString(), amount: amount, fee: fee, lockTime: lockTime)); }
private static void Code1() { Key key = new Key(); //Create the private key PubKey pubkey = key.PubKey; //Get the public key Console.WriteLine("Public key : " + pubkey); KeyId hash = pubkey.Hash; Console.WriteLine("Hash public key : " + hash); BitcoinAddress address = pubkey.GetAddress(Network.Main); //Get the address on the main network //var address = hash.ScriptPubKey; is also possible Console.WriteLine("Address : " + address); Script scriptPubKey = address.ScriptPubKey; //Get the scriptPubKey Console.WriteLine("Script : " + scriptPubKey); }
// This one just generates a random private key along with its info public void Demo() { Key privateKey = new Key(); // a 256-bit number BitcoinSecret wifKey = privateKey.GetWif(Network.TestNet); // base58check of privateKey (on TestNet where coins are worth nothing) PubKey publicKey = privateKey.PubKey; // privateKey multiplied by base point (base point=point on Bitcoins elliptical curve) var publicKeyHash = publicKey.Hash; // another point on Bitcoins elliptical curve var testNetAddress = publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.TestNet); // used to receive/check funds var paymentScript = testNetAddress.ScriptPubKey; // has conditions on how to receive coins Console.WriteLine("Private Key: " + privateKey); Console.WriteLine("BitcoinSecret: " + wifKey); Console.WriteLine("Public Key: " + publicKey); Console.WriteLine("Public Key Hash: " + publicKeyHash); Console.WriteLine("Address for TestNet: " + testNetAddress); Console.WriteLine("ScriptPubKey: " + paymentScript); //var paymentScript = publicKey.ScriptPubKey; }
private static void MostrarInformacaoDeEndereco() { Console.WriteLine("cTQA9XcNUcS7CVtvAvz6BipKn5xzWTn3ppFTGCkEwe8QS9dVZPDw"); string chavePrivada = "cTQA9XcNUcS7CVtvAvz6BipKn5xzWTn3ppFTGCkEwe8QS9dVZPDw"; var bitcoinSecret = new BitcoinSecret(chavePrivada); PubKey chavePublica = bitcoinSecret.PubKey; Console.WriteLine(chavePublica); BitcoinPubKeyAddress endereco = chavePublica.GetAddress(Network.TestNet); var client = new QBitNinjaClient(bitcoinSecret.Network); var listaDeOperacoes = client.GetBalance(bitcoinSecret.GetAddress()).Result.Operations; foreach (var operacao in listaDeOperacoes) { Console.WriteLine(operacao.TransactionId); } }
/// <summary> /// Verify the Dogecoin message signed by some dogecoin private key /// </summary> /// <param name="message">input original message</param> /// <param name="signature">signature made by some dogecoin address</param> /// <param name="address">Dogecoin address</param> /// <returns></returns> public static async Task <(bool, string)> VerifyDogeMessage(string message, string signature, string address, bool messageIsAlreadyHash = false) { if (string.IsNullOrEmpty(message) || string.IsNullOrEmpty(signature) || string.IsNullOrEmpty(address)) { return(false, "Input parameters cannot be empty or null."); } try { var add = BitcoinAddress.Create(address, DogeTransactionHelpers.Network); //var vadd = (add as IPubkeyHashUsable); var split = signature.Split('@'); if (split.Length > 1) { var sg = Convert.FromBase64String(split[1]); var recoveryId = Convert.ToInt32(split[0]); var sgs = new CompactSignature(recoveryId, sg); Console.WriteLine("Signature loaded."); PubKey recoveredPubKey = null; if (!messageIsAlreadyHash) { uint256 hash = NBitcoin.Crypto.Hashes.DoubleSHA256(Encoding.UTF8.GetBytes(message)); recoveredPubKey = PubKey.RecoverCompact(hash, sgs); } else { recoveredPubKey = PubKey.RecoverCompact(uint256.Parse(message), sgs); } var pk = recoveredPubKey.GetAddress(ScriptPubKeyType.Legacy, DogeTransactionHelpers.Network); if (pk.ToString() == add.ToString()) { return(true, "Verified."); } } } catch { return(false, "Wrong input. Cannot verify the message signature."); } return(false, "Not verified."); }