public static void Execute(Key privateKey) { // use a one-way cryptographic function, to generate a public key. PubKey publicKey = privateKey.PubKey; KeyId publicKeyHash = publicKey.Hash; var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); var mainNetAddress = publicKeyHash.GetAddress(Network.Main); Console.WriteLine(); Console.WriteLine("mainNetAddress.ScriptPubKey: " + mainNetAddress.ScriptPubKey); Console.WriteLine("testNetAddress.ScriptPubKey: " + testNetAddress.ScriptPubKey); // generate a bitcoin address from the ScriptPubKey and the network identifier. var paymentScript = publicKeyHash.ScriptPubKey; var sameMainNetAddress = paymentScript.GetDestinationAddress(Network.Main); // retrieve the hash from the ScriptPubKey and generate a Bitcoin Address from it var samePublicKeyHash = (KeyId)paymentScript.GetDestination(); var sameMainNetAddress2 = new BitcoinPubKeyAddress(samePublicKeyHash, Network.Main); Console.WriteLine(); Console.WriteLine(mainNetAddress == sameMainNetAddress); // True Console.WriteLine(publicKeyHash == samePublicKeyHash); // True Console.WriteLine(mainNetAddress == sameMainNetAddress2); // True Console.ReadLine(); }
static void Main() { //There is no know "bitcoin Address",all it goes to //it actually all they care is ScriptPubKey var publicKeyHash = new KeyId("14836dbe7f38c5ac3d49e8d790af808a4ee9edcf"); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); var mainNetAddress = publicKeyHash.GetAddress(Network.Main); //** Here is what ScriptPubKey look like Console.WriteLine("The MainNetAddress's ScriptPubKey " + mainNetAddress.ScriptPubKey); // OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG Console.WriteLine("The TestNetAddress's ScriptPubKey " + testNetAddress.ScriptPubKey); // OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG //They look the same, Are they really the same? var paymentScript = publicKeyHash.ScriptPubKey; var sameMainNetAddress = paymentScript.GetDestinationAddress(Network.Main); Console.WriteLine("Generate bitcoin address from ScriptPubKey " mainNetAddress == sameMainNetAddress); // True //generate bitcoin address from ScriptPubKey //Retrieve the harsh from the ScriptPubKey and generate Bitcoin Address //The ScriptPubKey does not contains the hashed public key permitted to spend the bitcoin var samePublicKeyHash = (KeyId)paymentScript.GetDestination(); Console.WriteLine(publicKeyHash == samePublicKeyHash); // True var sameMainNetAddress2 = new BitcoinPubKeyAddress(samePublicKeyHash, Network.Main); Console.WriteLine("Retrieve the harsh from the ScriptPubKey, Are they the same " mainNetAddress == sameMainNetAddress2); // True //From now on, we only exclusively user ScriptPubKey, Address is only a user interface concept Console.ReadLine(); }
static void Main() { var publicKeyHash = new KeyId("14836dbe7f38c5ac3d49e8d790af808a4ee9edcf"); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); var mainNetAddress = publicKeyHash.GetAddress(Network.Main); Console.WriteLine(mainNetAddress.ScriptPubKey); // OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG Console.WriteLine(testNetAddress.ScriptPubKey); // OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG var paymentScript = publicKeyHash.ScriptPubKey; var sameMainNetAddress = paymentScript.GetDestinationAddress(Network.Main); Console.WriteLine(mainNetAddress == sameMainNetAddress); // True var samePublicKeyHash = (KeyId)paymentScript.GetDestination(); Console.WriteLine(publicKeyHash == samePublicKeyHash); // True var sameMainNetAddress2 = new BitcoinPubKeyAddress(samePublicKeyHash, Network.Main); Console.WriteLine(mainNetAddress == sameMainNetAddress2); // True Console.ReadLine(); }
private static void ScriptPubKey() { Key privateKey = new Key(); KeyId publicKeyHash = privateKey.PubKey.Hash; var mainNetAddress = publicKeyHash.GetAddress(Network.Main); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); publicKeyHash = new KeyId(publicKeyHash.ToString()); testNetAddress = publicKeyHash.GetAddress(Network.TestNet); mainNetAddress = publicKeyHash.GetAddress(Network.Main); Console.WriteLine(mainNetAddress.ScriptPubKey); // OP_DUP OP_HASH160 18Av7Pm5knLGDu2CCAwVs6fPoy7UhyaC3Z OP_EQUALVERIFY OP_CHECKSIG Console.WriteLine(testNetAddress.ScriptPubKey); // OP_DUP OP_HASH160 18Av7Pm5knLGDu2CCAwVs6fPoy7UhyaC3Z OP_EQUALVERIFY OP_CHECKSIG var paymentScript = publicKeyHash.ScriptPubKey; Console.WriteLine(paymentScript); var sameMainNetAddress = paymentScript.GetDestinationAddress(Network.Main); Console.WriteLine(sameMainNetAddress); Console.WriteLine(mainNetAddress == sameMainNetAddress); // True var samePublicKeyHash = (KeyId)paymentScript.GetDestination(); Console.WriteLine(publicKeyHash == samePublicKeyHash); // True var sameMainNetAddress2 = new BitcoinPubKeyAddress(samePublicKeyHash, Network.Main); Console.WriteLine(mainNetAddress == sameMainNetAddress2); // True }
static void Main(string[] args) { Console.WriteLine("Hello World! " + new Key().GetWif(Network.Main)); // generate a random private key //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); //we can easily get your bitcoin address from your public key // and the network on which this address should be used. 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); */ var publicKeyHash = new KeyId("14836dbe7f38c5ac3d49e8d790af808a4ee9edcf"); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); var mainNetAddress = publicKeyHash.GetAddress(Network.Main); Console.WriteLine(mainNetAddress.ScriptPubKey); Console.WriteLine(testNetAddress.ScriptPubKey); var paymentScript = publicKeyHash.ScriptPubKey; var sameMainNetAddress = paymentScript.GetDestinationAddress(Network.Main); Console.WriteLine(mainNetAddress == sameMainNetAddress); var samePublicKeyHash = (KeyId)paymentScript.GetDestination(); Console.WriteLine(publicKeyHash == samePublicKeyHash); // True var sameMainNetAddress2 = new BitcoinPubKeyAddress(samePublicKeyHash, Network.Main); Console.WriteLine(mainNetAddress == sameMainNetAddress2); // Key privateKey = new Key(); // generate a random private key // generate our Bitcoin secret(also known as Wallet Import Format or simply WIF) from our private key for the mainnet BitcoinSecret mainNetPrivateKey = privateKey.GetBitcoinSecret(Network.Main); BitcoinSecret testNetPrivateKey = privateKey.GetBitcoinSecret(Network.TestNet); // generate our Bitcoin secret(also known as Wallet Import Format or simply WIF) from our private key for the testnet Console.WriteLine(mainNetPrivateKey); Console.WriteLine(testNetPrivateKey); bool WifIsBitcoinSecret = mainNetPrivateKey == privateKey.GetWif(Network.Main); Console.WriteLine(WifIsBitcoinSecret); // True // Create a client QBitNinjaClient client = new QBitNinjaClient(Network.Main); // Parse transaction id to NBitcoin.uint256 so the client can eat it var transactionId = uint256.Parse("f13dc48fb035bbf0a6e989a26b3ecb57b84f85e0836e777d6edf60d87a4a2d94"); // Query the transaction GetTransactionResponse transactionResponse = client.GetTransaction(transactionId).Result; }
protected void btnStart_Click(object sender, EventArgs e) { Key privatekey = new Key(); //私钥 PubKey publickey = privatekey.PubKey; //与私钥相对应得公钥 KeyId publicKeyHash = publickey.Hash; //得到公钥的 hash var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); //测试网地址 var mainNetAddress = publicKeyHash.GetAddress(Network.Main); //主网地址 tbxMsg.Text = mainNetAddress.ToString(); }
// Generate ScriptPubKey private static void GenerateScriptPubKey() { Console.WriteLine("\nScriptPubKey"); var publicKeyHash = new KeyId("14836dbe7f38c5ac3d49e8d790af808a4ee9edcf"); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); var mainNetAddress = publicKeyHash.GetAddress(Network.Main); Console.WriteLine(mainNetAddress .ScriptPubKey); // OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG Console.WriteLine(testNetAddress .ScriptPubKey); // OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG }
static void Main(string[] args) { RandomUtils.Random = new UnsecureRandom(); // set the random number generator. Key privateKey = new Key(); // generate a random private key PubKey publicKey = privateKey.PubKey; Console.WriteLine(publicKey); Console.WriteLine(publicKey.GetAddress(Network.Main)); Console.WriteLine(publicKey.GetAddress(Network.TestNet)); var publicKeyHash = publicKey.Hash; Console.WriteLine("publicKeyHash: " + publicKeyHash); var mainNetAddress = publicKeyHash.GetAddress(Network.Main); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); Console.WriteLine("mainNetAddress: " + mainNetAddress); Console.WriteLine("testNetAddress: " + testNetAddress); var paymentScript = publicKeyHash.ScriptPubKey; var sameMainNetAddress = paymentScript.GetDestinationAddress(Network.Main); Console.WriteLine(mainNetAddress == sameMainNetAddress); var samePublicKeyHash = (KeyId)paymentScript.GetDestination(); Console.WriteLine(publicKeyHash == samePublicKeyHash); var sameMainNetAddress2 = new BitcoinPubKeyAddress(samePublicKeyHash, Network.Main); Console.WriteLine(mainNetAddress == sameMainNetAddress2); var bitcoinSecret = privateKey.GetWif(Network.Main); Console.WriteLine("Hello World! " + bitcoinSecret); Console.WriteLine("#######################################################################"); var publicKeyHash2 = new KeyId("14836dbe7f38c5ac3d49e8d790af808a4ee9edcf"); var testNetAddress2 = publicKeyHash2.GetAddress(Network.TestNet); var mainNetAddress2 = publicKeyHash2.GetAddress(Network.Main); // OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG Console.WriteLine(mainNetAddress2.ScriptPubKey); // OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG Console.WriteLine(testNetAddress2.ScriptPubKey); Console.ReadLine(); }
private void label4_Click(object sender, EventArgs e)//查询地址 { if (flagadr == 0) { string privateKeyStr = PrivateKey; BitcoinSecret privateKey = new BitcoinSecret(privateKeyStr); Network network = privateKey.Network; PubKey pubKey = privateKey.PubKey; string pubKeyStr = pubKey.ToHex(); KeyId pkhash = pubKey.Hash; string pkhashStr = pkhash.ToString(); BitcoinAddress addres = pkhash.GetAddress(network); string address = addres.ToString(); textBox1.Visible = true; textBox1.Text = address; flagadr = 2; } else if (flagadr == 1) { textBox1.Visible = true; flagadr = 2; } else { textBox1.Visible = false; flagadr = 1; } }
public void GetBalance() { ssbalance = 0; string privateKeyStr = PrivateKey; //string privateKeyStr = "cUvazeu9ucqD4trygt8xMEQKZfR3SZ5BdiAWb3eEwbQ48iPwYKSB"; BitcoinSecret privateKey = new BitcoinSecret(privateKeyStr); Network network = privateKey.Network; PubKey pubKey = privateKey.PubKey; string pubKeyStr = pubKey.ToHex(); KeyId pkhash = pubKey.Hash; string pkhashStr = pkhash.ToString(); BitcoinAddress addres = pkhash.GetAddress(network); string address = addres.ToString(); string networkStr = bsvConfiguration_class.testNetwork; string uri = bsvConfiguration_class.RestApiUri; //读取未消费的交易输出utxo Task <RestApiUtxo_class[]> utxo = Task <RestApiUtxo_class[]> .Run(() => { RestApiUtxo_class[] untxo = RestApi_class.getUtxosByAnAddress(uri, networkStr, address); return(untxo); }); utxo.Wait(); int n = utxo.Result.Length; for (int i = 0; i < n; i++) { ssbalance += utxo.Result[i].Value; } //Console.WriteLine(ssbalance); label3.Text = "账户余额:"; label3.Text += ssbalance; label3.Text += " sat"; }
// get public script key public Script GetScriptPubKey() { var publicKeyHash = new KeyId(GetPublicKey().ToString()); var testNetAddress = publicKeyHash.GetAddress(_networkType); return(testNetAddress.ScriptPubKey); }
private static void TestMethod() { Key privateKey = new Key(); PubKey publicKey = privateKey.PubKey; string mnemonicString = "quantum tobacco key they maid mean crime youth chief jungle mind design broken tilt bus shoulder leaf good forward erupt split divert bread kitten"; var mnemonic = new Mnemonic(mnemonicString); var seed = mnemonic.DeriveSeed(); var xprv = mnemonic.DeriveExtKey().Derive(new KeyPath("m/44\'/0\'/0\'/0")); var xpub = xprv.Neuter(); Console.WriteLine("xpub: " + xpub.ToString(Network.Main)); Console.WriteLine("Private Key Test net: " + privateKey.ToString(Network.TestNet)); Console.WriteLine("Secret Test net" + privateKey.GetBitcoinSecret(Network.TestNet).ToString()); //Console.WriteLine("address Main net: " + publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main)); Console.WriteLine("address Test net: " + publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.TestNet)); Console.WriteLine("Segwit address Test net: " + publicKey.GetAddress(ScriptPubKeyType.Segwit, Network.TestNet)); var publicKeyHash = publicKey.Hash; Console.WriteLine("public key hash:" + publicKeyHash); var mainNetAddress = publicKeyHash.GetAddress(Network.Main); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); Console.WriteLine("address Main net: " + mainNetAddress); Console.WriteLine("address Test net: " + testNetAddress); Console.WriteLine("----------------------"); var publicKeyHash2 = new KeyId("14836dbe7f38c5ac3d49e8d790af808a4ee9edcf"); var testNetAddress2 = publicKeyHash2.GetAddress(Network.TestNet); var mainNetAddress2 = publicKeyHash2.GetAddress(Network.Main); Console.WriteLine(mainNetAddress2.ScriptPubKey); // OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG Console.WriteLine(testNetAddress2.ScriptPubKey); // OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG Console.WriteLine("-----------------------"); var paymentScript = publicKeyHash.ScriptPubKey; var sameMainNetAddress = paymentScript.GetDestinationAddress(Network.Main); Console.WriteLine(mainNetAddress == sameMainNetAddress); // True }
static void Main(string[] args) { Console.WriteLine("Hello CryptoDosh!"); Key privateKey = new Key(); // generates a random private key PubKey publicKey = privateKey.PubKey; //one way cryptographic function generates a public key derived from the private key Console.WriteLine(publicKey); //TestNet is a Bitcoin network for development purposes. Bitcoins on this network worth nothing. //70f5804823b296b439c555a127e29b90a61e07294aba7db9815c519683b22894 - TX ID //gets your bitcoin address from your public key and the network on which this address should be used. Console.WriteLine(publicKey.GetAddress(Network.TestNet)); //a bitcoin address is made up of a version byte and your public key’s hash bytes. var publicKeyHash = publicKey.Hash; Console.WriteLine(publicKeyHash); //both of these bytes are concatenated and then encoded into a Base58Check. var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); Console.WriteLine(testNetAddress); //there is no such thing as a Bitcoin Address. //internally, the Bitcoin protocol identifies the recipient of Bitcoin by a ScriptPubKey. //a ScriptPubKey is a short script that explains what conditions must be met to claim ownership of bitcoins. //we generate the ScriptPubKey from the Bitcoin Address. //this is a step that all bitcoin clients do to translate the “human friendly” Bitcoin Address to the Blockchain readable address. publicKeyHash = new KeyId("14836dbe7f38c5ac3d49e8d790af808a4ee9edcf"); testNetAddress = publicKeyHash.GetAddress(Network.TestNet); Console.WriteLine(testNetAddress.ScriptPubKey); //ScriptPubKey appears to have nothing to do with the Bitcoin Address, but it does show the hash of the public key. //Bitcoin Addresses are composed of a version byte which identifies the network where to use the address and the hash of a public key. //We can go backwards and generate a bitcoin address from the ScriptPubKey and the network identifier. var paymentScript = publicKeyHash.ScriptPubKey; var sameTestNetAddress = paymentScript.GetDestinationAddress(Network.TestNet); Console.WriteLine(testNetAddress == sameTestNetAddress); //it is also possible to retrieve the hash from the ScriptPubKey and generate a Bitcoin Address from it: var samePublicKeyHash = (KeyId)paymentScript.GetDestination(); Console.WriteLine(publicKeyHash == samePublicKeyHash); // True var sameTestNetAddress2 = new BitcoinPubKeyAddress(samePublicKeyHash, Network.TestNet); Console.WriteLine(testNetAddress == sameTestNetAddress2); // True }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var address = string.Empty; if (value != null) { var key = new KeyId((byte[])value); address = key.GetAddress(_network).ToString(); } writer.WriteValue(address); }
//-------------------------------------------------------------------------------- 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"); }
private static void Main(string[] args) { // as the Blockchain is concerned, there is no such thing as a Bitcoin Address. // Internally, the Bitcoin protocol identifies the recipient of Bitcoin by a ScriptPubKey. // e.g., OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG // ScriptPubKey explains the conditions that must be met to claim ownership of bitcoin // generate the scriptpubkey from the bitcoin address var publicKeyHash = new KeyId("14836dbe7f38c5ac3d49e8d790af808a4ee9edcf"); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); var mainNetAddress = publicKeyHash.GetAddress(Network.Main); // scriptpubkeys will contain hash of public key Console.WriteLine(mainNetAddress.ScriptPubKey); Console.WriteLine(testNetAddress.ScriptPubKey); // Bitcoin Addresses are composed of a version byte which identifies the network where to use the address and the hash of a public key. // So we can go backwards and generate a bitcoin address from the ScriptPubKey and the network identifier. var paymentScript = publicKeyHash.ScriptPubKey; var sameMainNetAddress = paymentScript.GetDestinationAddress(Network.Main); Console.WriteLine(mainNetAddress == sameMainNetAddress); // also possible to retrieve the hash from the ScriptPubKey and generate a Bitcoin Address from it var samePublicKeyHash = paymentScript.GetDestination() as KeyId; Console.WriteLine(publicKeyHash == samePublicKeyHash); var sameMainNetAddress2 = new BitcoinPubKeyAddress(samePublicKeyHash, Network.Main); Console.WriteLine(mainNetAddress == sameMainNetAddress2); // Note: A ScriptPubKey does not necessarily contain the hashed public key(s) permitted to spend the bitcoin. Console.ReadLine(); }
/// <summary> /// Initializes an instance of the <see cref="ScriptPubKey"/> class. /// </summary> /// <param name="script">The script.</param> /// <param name="network">The network where the transaction was conducted.</param> public ScriptPubKey(NBitcoin.Script script, Network network) : base(script) { this.Type = this.GetScriptType(script.FindTemplate(network)); // To avoid modifying the very low-level GetDestination logic, check for a cold staking script first. // The decision to show the cold pubkey's address in the 'addresses' list is based on the following: // 1. It seems more intuitive from a user's perspective that their balance will appear against this address. // 2. A balance should never appear against a hot address from an exchange's perspective, as they have no guarantee they will be able to spend those funds. // It is also presumed that it is preferable to show an address rather than not, as a block explorer would then have to show only a relatively meaningless raw script as the output's destination. // Another considered alternative was to show both addresses, but with a modified version byte. The underlying pubkey hashes would be the same, but the resulting addresses would be visually distinct from regular P2PKH. // This may have caused user confusion, however, as the modified addresses would not look like those they used to configure the cold staking setup, making searching for them on a block explorer more difficult. if (script.IsScriptType(ScriptType.ColdStaking)) { var coldPubKeyHash = new KeyId(script.ToBytes(true).SafeSubarray(28, 20)); this.ReqSigs = 1; this.Addresses = new List <string> { coldPubKeyHash.GetAddress(network).ToString() }; return; } var destinations = new List <TxDestination> { script.GetDestination(network) }; if (destinations[0] == null) { destinations = script.GetDestinationPublicKeys(network).Select(p => p.Hash).ToList <TxDestination>(); } // TODO: We do not want to put the cold staking addresses into the 'addresses' element due to the high potential for confusion. Maybe introduce an additional element? if (destinations.Count == 1) { this.ReqSigs = 1; this.Addresses = new List <string> { destinations[0].GetAddress(network).ToString() }; } else if (destinations.Count > 1) { PayToMultiSigTemplateParameters multi = PayToMultiSigTemplate.Instance.ExtractScriptPubKeyParameters(script) ?? PayToFederationTemplate.Instance.ExtractScriptPubKeyParameters(script, network); if (multi != null) { this.ReqSigs = multi.SignatureCount; this.Addresses = multi.PubKeys.Select(m => m.GetAddress(network).ToString()).ToList(); } } }
void Start() { var publicKeyHash = new KeyId("14836dbe7f38c5ac3d49e8d790af808a4ee9edcf"); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); var mainNetAddress = publicKeyHash.GetAddress(Network.Main); UnityEngine.Debug.Log(mainNetAddress.ScriptPubKey); // OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG UnityEngine.Debug.Log(testNetAddress.ScriptPubKey); // OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG var paymentScript = publicKeyHash.ScriptPubKey; var sameMainNetAddress = paymentScript.GetDestinationAddress(Network.Main); UnityEngine.Debug.Log(mainNetAddress == sameMainNetAddress); // True var samePublicKeyHash = (KeyId)paymentScript.GetDestination(); UnityEngine.Debug.Log(publicKeyHash == samePublicKeyHash); // True var sameMainNetAddress2 = new BitcoinPubKeyAddress(samePublicKeyHash, Network.Main); UnityEngine.Debug.Log(mainNetAddress == sameMainNetAddress2); // True }
public ScriptPubKey() { var publicKeyHash = new KeyId("14836dbe7f38c5ac3d49e8d790af808a4ee9edcf"); var mainNetAddress = publicKeyHash.GetAddress(Network.Main); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); Console.WriteLine("ScriptPubKey (MainNet): " + mainNetAddress.ScriptPubKey); Console.WriteLine("ScriptPubKey (TestNet): " + testNetAddress.ScriptPubKey); Console.WriteLine(); var paymentScript = publicKeyHash.ScriptPubKey; var sameMainNetAddress = paymentScript.GetDestinationAddress(Network.Main); Console.WriteLine("Is it possible to get BitcoinAddress from ScriptPubKey?"); Console.WriteLine(mainNetAddress == sameMainNetAddress); Console.WriteLine(); var samePublicKeyHash = (KeyId)paymentScript.GetDestination(); Console.WriteLine("Is it possible to get PublicKeyHash from ScriptPubKey?"); Console.WriteLine(publicKeyHash == samePublicKeyHash); }
void Sample_02_ScriptPublicKey() { // case 1 KeyId publicKeyHash = new KeyId("14836dbe7f38c5ac3d49e8d790af808a4ee9edcf"); BitcoinAddress bitcoinAddress = publicKeyHash.GetAddress(NBitcoin.Network.TestNet); Debug.Log(bitcoinAddress.ScriptPubKey + " <-------------- Bitcoin Address from public Key Hash"); Script paymentScript = publicKeyHash.ScriptPubKey; BitcoinAddress destination = paymentScript.GetDestinationAddress(NBitcoin.Network.TestNet); Debug.Log(destination.ScriptPubKey + " <-------------- Bitcoin Address from public Key Hash"); // case 2 KeyId sameKeyHash = (KeyId)paymentScript.GetDestination(); BitcoinAddress sameAdress = new BitcoinPubKeyAddress(sameKeyHash, NBitcoin.Network.TestNet); Debug.Log(sameAdress); Debug.Log(publicKeyHash); Debug.Log(sameKeyHash); }
public async Task <object> Find(string data) { data = data.Trim(); var b58 = NoException(() => WhatIsBase58.GetFromBitcoinString(data)); if (b58 != null) { if (b58 is WhatIsAddress) { var address = (WhatIsAddress)b58; TryFetchRedeemOrPubKey(address); } return(b58); } if (data.Length == 0x40) { try { return(await Controller.JsonTransaction(uint256.Parse(data), false)); } catch { } } var b = NoException(() => Controller.JsonBlock(BlockFeature.Parse(data), true, false)); if (b != null) { return(b); } if (data.Length == 0x28) //Hash of pubkey or script { TxDestination dest = new KeyId(data); var address = new WhatIsAddress(dest.GetAddress(Network)); if (TryFetchRedeemOrPubKey(address)) { return(address); } dest = new ScriptId(data); address = new WhatIsAddress(dest.GetAddress(Network)); if (TryFetchRedeemOrPubKey(address)) { return(address); } } var script = NoException(() => GetScriptFromBytes(data)); if (script != null) { return(new WhatIsScript(script, Network)); } script = NoException(() => GetScriptFromText(data)); if (script != null) { return(new WhatIsScript(script, Network)); } var sig = NoException(() => new TransactionSignature(Encoders.Hex.DecodeData(data))); if (sig != null) { return(new WhatIsTransactionSignature(sig)); } var pubkeyBytes = NoException(() => Encoders.Hex.DecodeData(data)); if (pubkeyBytes != null && PubKey.Check(pubkeyBytes, true)) { var pubKey = NoException(() => new PubKey(data)); if (pubKey != null) { return(new WhatIsPublicKey(pubKey, Network)); } } if (data.Length == 80 * 2) { var blockHeader = NoException(() => { var h = new BlockHeader(); h.ReadWrite(Encoders.Hex.DecodeData(data)); return(h); }); if (blockHeader != null) { return(new WhatIsBlockHeader(blockHeader)); } } return(null); }
public static BitcoinAddress getAddressFromPubKeyHash(KeyId pubKeyHash, Network network) { return(pubKeyHash.GetAddress(network)); }
static void Main(string[] args) { Console.WriteLine("Hello Coin!"); Console.WriteLine("======================= privateKey"); Key privateKey = new Key(); // generate a random private key Console.WriteLine("privateKey : " + privateKey); Console.WriteLine(""); Console.WriteLine("======================= privateKey => publicKey"); PubKey publicKey = privateKey.PubKey; Console.WriteLine("publicKey : " + publicKey); // ง่ายมากที่จะรับ bitcoin address จาก publicKey Console.WriteLine(""); Console.WriteLine("======================= publicKey => GetAddress"); Console.WriteLine("addressMain : " + publicKey.GetAddress(Network.Main)); Console.WriteLine("addressTestNet : " + publicKey.GetAddress(Network.TestNet)); // hash คือการลดรูปของ publicKey // address ที่ได้จาก publish key หรือ publish key has จะเป็น address เดียวกันคือตัวเดียวกันนั่นแหละ Console.WriteLine(""); Console.WriteLine("======================= publicKey => publicKeyHash => GetAddress"); var publicKeyHash = publicKey.Hash; Console.WriteLine("publicKeyHash : " + publicKeyHash); var mainNetAddress = publicKeyHash.GetAddress(Network.Main); var testNetAddress = publicKeyHash.GetAddress(Network.TestNet); Console.WriteLine("mainNetAddress : " + mainNetAddress); Console.WriteLine("testNetAddress : " + testNetAddress); // เอาส่วนแรกก่อนละกัน ส่วนแรกที่เป็นการเขียนโน้ตทิ้งไว้ใน out ฝากไว้ให้ผู้รับ Script ส่วนนี้มีชื่อเรียกอย่างเป็นทางการว่า Public Key Script หรือ scriptPubKey ซึ่งก็คือ Script ฝั่งที่ใช้ Public Key ครับ Console.WriteLine(""); Console.WriteLine("======================= ScriptPubKey from Address"); var publicKeyHash1 = new KeyId("14836dbe7f38c5ac3d49e8d790af808a4ee9edcf"); Console.WriteLine("publicKeyHash1 : " + publicKeyHash1); var testNetAddress1 = publicKeyHash1.GetAddress(Network.TestNet); var mainNetAddress1 = publicKeyHash1.GetAddress(Network.Main); Console.WriteLine("mainNetAddress : " + mainNetAddress1); Console.WriteLine("testNetAddress : " + testNetAddress1); Console.WriteLine("mainNetAddress1ScriptPubKey : " + mainNetAddress1.ScriptPubKey); Console.WriteLine("testNetAddress1ScriptPubKey : " + testNetAddress1.ScriptPubKey); Console.WriteLine(""); Console.WriteLine("======================= Address ที่ได้จาก ScriptPubKey ที่มาจาก hash เดียวกัน จะเท่ากับ hash ที่ GetAddress (ScriptPubKey หน้าจะมีความสัมพันกับ publicKeyHash)"); var paymentScript = publicKeyHash1.ScriptPubKey; var sameMainNetAddress = paymentScript.GetDestinationAddress(Network.Main); Console.WriteLine(mainNetAddress1 == sameMainNetAddress); // True Console.WriteLine(mainNetAddress1.ScriptPubKey == publicKeyHash1.ScriptPubKey); // True // private key ที่เข้ารหัส Base58Check เรียกมันว่า Bitcoin Secret (หรือเรียกว่า Wallet Import Format หรือ simply WIF) คล้ายๆ Bitcoin Addresses. // เข้าใจว่า GetBitcoinSecret เป็นฟังชั่นเหมือนกันกับ GetWif Console.WriteLine(""); Console.WriteLine("======================= GetBitcoinSecret"); BitcoinSecret mainNetPrivateKey = privateKey.GetBitcoinSecret(Network.Main); // generate our Bitcoin secret(also known as Wallet Import Format or simply WIF) from our private key for the mainnet BitcoinSecret testNetPrivateKey = privateKey.GetBitcoinSecret(Network.TestNet); // generate our Bitcoin secret(also known as Wallet Import Format or simply WIF) from our private key for the testnet Console.WriteLine(mainNetPrivateKey); Console.WriteLine(testNetPrivateKey); bool WifIsBitcoinSecret = mainNetPrivateKey == privateKey.GetWif(Network.Main); Console.WriteLine(WifIsBitcoinSecret); // True Console.ReadLine(); }
private void button1_Click(object sender, EventArgs e) //读取 { textBox2.Text = ""; Console.WriteLine("start get"); string privateKeyStr = PrivateKey; //string privateKeyStr = "cUvazeu9ucqD4trygt8xMEQKZfR3SZ5BdiAWb3eEwbQ48iPwYKSB"; BitcoinSecret privateKey = new BitcoinSecret(privateKeyStr); Network network = privateKey.Network; PubKey pubKey = privateKey.PubKey; string pubKeyStr = pubKey.ToHex(); KeyId pkhash = pubKey.Hash; string pkhashStr = pkhash.ToString(); BitcoinAddress addres = pkhash.GetAddress(network); string address = addres.ToString(); string networkStr = bsvConfiguration_class.testNetwork; string uri = bsvConfiguration_class.RestApiUri; //获取链上的交易历史 Task <RestApiAddressHistoryTx[]> t = Task <RestApiAddressHistoryTx[]> .Run(() => { RestApiAddressHistoryTx[] addrHistory = RestApi_class.getAddressHistory(uri, networkStr, address); return(addrHistory); }); t.Wait(); int num = t.Result.Length; Console.WriteLine("链上交易数目:" + num); //读取链上信息 Task <RestApiTransaction[]> gettxs = null; if (num > 0) { string[] txHashs = new string[num]; for (int i = 0; i < num; i++) { txHashs[i] = t.Result[i].TxHash; } gettxs = Task <RestApiTransaction[]> .Run(() => { RestApiTransaction[] txs = RestApi_class.getTransactions(uri, networkStr, txHashs); return(txs); }); } for (int i = 0; i < num; i++) { RestApiTransaction tx = gettxs.Result[i]; string s = RestApi_class.getOpReturnData(tx, bsvConfiguration_class.encoding); if (s != null) { //解密 byte[] encryptedBytes; Base58Encoder base58Encoder = new Base58Encoder(); encryptedBytes = base58Encoder.DecodeData(s); string data = AES_class.AesDecrypt(encryptedBytes, privateKeyStr); textBox2.Text += data; textBox2.Text += "\r\n"; textBox2.Text += System.Environment.NewLine; textBox2.Text += "------------------------------------------------------------------------------------"; textBox2.Text += "\r\n"; textBox2.Text += System.Environment.NewLine; Console.WriteLine("链上内容:" + s); } } }
/// <summary> /// Try to interpret the given string in a few ways in order to detect what object it's supposed to represent. /// </summary> /// <returns>The object represented by the input string. This may be a Bitcoin address, a script, a signature, a public key, etc.</returns> public async Task <object> Find(string data) { data = data.Trim(); // Is it a Bitcoin address? var b58 = NoException(() => WhatIsBase58.GetFromBitcoinString(data)); if (b58 != null) { if (b58 is WhatIsAddress address) { await TryFetchRedeemOrPubKey(address); // Shouldn't the return value here be checked? } return(b58); } // Is it a transaction ID? if (data.Length == 0x40) { try { return(await Controller.JsonTransaction(uint256.Parse(data), false)); } catch { // Well, apparently it's not a transaction ID. } } // Is it a block feature? var b = NoException(() => Controller.JsonBlock(BlockFeature.Parse(data), true, false)); if (b != null) { return(b); } // Is it the hash of a public key (modeled as KeyId in NBitcoin), or is it the hash of a script ID? if (data.Length == 0x28) // Hash of pubkey or script { TxDestination dest = new KeyId(data); var address = new WhatIsAddress(dest.GetAddress(Network)); if (await TryFetchRedeemOrPubKey(address)) { return(address); } dest = new ScriptId(data); address = new WhatIsAddress(dest.GetAddress(Network)); if (await TryFetchRedeemOrPubKey(address)) { return(address); } } // Is it a script? var script = NoException(() => GetScriptFromBytes(data)); if (script != null) { return(new WhatIsScript(script, Network)); } script = NoException(() => GetScriptFromText(data)); if (script != null) { return(new WhatIsScript(script, Network)); } // Is it a transaction signature? var sig = NoException(() => new TransactionSignature(Encoders.Hex.DecodeData(data))); if (sig != null) { return(new WhatIsTransactionSignature(sig)); } // Is it a hexstring representing the bytes of a public key? var pubkeyBytes = NoException(() => Encoders.Hex.DecodeData(data)); if (pubkeyBytes != null && PubKey.Check(pubkeyBytes, true)) { var pubKey = NoException(() => new PubKey(data)); if (pubKey != null) { return(new WhatIsPublicKey(pubKey, Network)); } } // Is it a blockheader? if (data.Length == 80 * 2) { var blockHeader = NoException(() => { var h = ConsensusFactory.CreateBlockHeader(); h.ReadWrite(Encoders.Hex.DecodeData(data), ConsensusFactory); return(h); }); if (blockHeader != null) { return(new WhatIsBlockHeader(blockHeader)); } } // No idea what this is. return(null); }
//注册 private void button1_Click(object sender, EventArgs e) { label10.Text = mes; string user = textBox1.Text; string pwd = textBox2.Text; string pwd2 = textBox3.Text; string realname = textBox4.Text; string realnum = textBox5.Text; string lovename = textBox6.Text; byte[] pk = new byte[32]; if (user.Length != 0 && realname.Length != 0 && realnum.Length != 0) //用户名、真实姓名、证件号不能为空 { if (pwd == pwd2) //确认密码 { //产生私钥 string AESkey = user + pwd; while (AESkey.Length < 32) { AESkey += "1"; } string sStr = realnum; while (sStr.Length < 32) { sStr += "3"; } string s = realname + realnum + lovename; Console.WriteLine(AESkey); Console.WriteLine(s); byte[] p; p = AES_class.AesEncrypt(s, sStr); int n = p.Length; if (n < 32) { for (int i = 0; i < n; i++) { pk[i] = p[i]; } for (int i = n; i < 32; i++) { pk[i] = 1; } } else if (n > 31) { for (int i = 0; i < 32; i++) { pk[i] = p[i]; } } Key key = new Key(pk); BitcoinSecret privateKey = key.GetBitcoinSecret(Network.TestNet); //加密私钥后写入文件 //string path = user+".txt"; string path = user; string privateKeyStr = "[[["; privateKeyStr += privateKey.ToString(); privateKeyStr += "]]]"; Console.WriteLine("私钥:" + privateKeyStr); byte[] APkey; APkey = AES_class.AesEncrypt(privateKeyStr, AESkey); Base58Encoder base58Encoder = new Base58Encoder(); string base58Str = base58Encoder.EncodeData(APkey); Console.WriteLine("写入数据:" + base58Str); File.WriteAllText(path, base58Str); //获取地址 PubKey pubKey = privateKey.PubKey; KeyId pkhash = pubKey.Hash; BitcoinAddress addres = pkhash.GetAddress(Network.TestNet); string address = addres.ToString(); MessageBox.Show("注册成功!\n请使用地址:" + address + "前往https://faucet.bitcoincloud.net/网站申领比特币。"); //跳转页面 string pkey = privateKey.ToString(); this.Hide(); Form1 ff = new Form1(pkey); ff.ShowDialog(); } else { MessageBox.Show("密码输入错误!"); } } else { MessageBox.Show("请正确输入相关信息!"); } }