Skip to content

Commit d5a5c79

Browse files
committed
Get first Bitcoin address ever exercise added
1 parent 827f29f commit d5a5c79

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

ProofOfOwnership/Program.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using NBitcoin;
34
// ReSharper disable All
45

@@ -11,6 +12,9 @@ static void Main()
1112
SignAsCraigWright();
1213
VerifySatoshi();
1314
VerifyDorier();
15+
16+
/* BONUS: Get the first Bitcoin Address */
17+
Console.WriteLine(GetFirstBitcoinAddressEver()); // 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
1418

1519
Console.ReadLine();
1620
}
@@ -40,5 +44,58 @@ static void VerifyDorier()
4044
var signature = "H1jiXPzun3rXi0N9v9R5fAWrfEae9WPmlL5DJBj1eTStSvpKdRR8Io6/uT9tGH/3OnzG6ym5yytuWoA9ahkC3dQ=";
4145
Console.WriteLine(address.VerifyMessage(message, signature));
4246
}
47+
48+
static string GetFirstBitcoinAddressEver()
49+
{
50+
/*
51+
* You probably know The Blockchain is a chain of blocks,
52+
* All the way back to the first block ever, called genesis.
53+
* Here is how you can get it:
54+
*/
55+
var genesisBlock = Network.Main.GetGenesis();
56+
57+
/*
58+
* You probably also know a block is made up of transactions.
59+
* Here is how you can get the first transaction ever:
60+
*/
61+
var firstTransactionEver = genesisBlock.Transactions.First();
62+
63+
/*
64+
* You might not know that a transaction can have multiple outputs (and inputs).
65+
* Here is how you can get the first output ever:
66+
*/
67+
var firstOutputEver = firstTransactionEver.Outputs.First();
68+
69+
/*
70+
* You usually see a destination of an output as a bitcoin address *
71+
* But the Bitcoin network doesn't know addresses, it knows ScriptPubKeys
72+
* A ScriptPubKey looks something like this:
73+
* OP_DUP OP_HASH160 62e907b15cbf27d5425399ebf6f0fb50ebb88f18 OP_EQUALVERIFY OP_CHECKSIG
74+
* Let's get the ScriptPubKey of the first output ever:
75+
*/
76+
var firstScriptPubKeyEver = firstOutputEver.ScriptPubKey;
77+
78+
/*
79+
* Actually your wallet software is what decodes addresses into ScriptPubKeys.
80+
* Or decodes ScriptPubKeys to addresses.
81+
* Here is how you do it:
82+
*/
83+
84+
/*
85+
* First it is important to know a ScriptPubKey may contain one or multiple public keys.
86+
* Public keys can be encoded into a bitcoin address.
87+
* When a ScriptPubKey contains multiple public keys, it is called multi-sig.
88+
* So in order to get the first address ever, we can get the first public key ever:
89+
*/
90+
91+
var firstPubKeyEver = firstScriptPubKeyEver.GetDestinationPublicKeys().First();
92+
93+
/*
94+
* You can get a bitcoin address from a public key with the network identifier:
95+
*/
96+
var firstBitcoinAddressEver = firstPubKeyEver.GetAddress(Network.Main);
97+
98+
return firstBitcoinAddressEver.ToString();
99+
}
43100
}
44101
}

0 commit comments

Comments
 (0)