<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://en.bitcoin.it/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Jayemar</id>
	<title>Bitcoin Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://en.bitcoin.it/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Jayemar"/>
	<link rel="alternate" type="text/html" href="https://en.bitcoin.it/wiki/Special:Contributions/Jayemar"/>
	<updated>2026-04-09T04:11:43Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Transaction_malleability&amp;diff=57918</id>
		<title>Transaction malleability</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Transaction_malleability&amp;diff=57918"/>
		<updated>2015-07-26T02:46:15Z</updated>

		<summary type="html">&lt;p&gt;Jayemar: Cleaned up the grammar a bit in the opening paragraph&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;While [[transaction]]s are signed, the signature does not currently cover all the data in a transaction that is hashed to create the transaction hash. Thus, while uncommon, it is possible for a node on the network to change a transaction you send in such a way that the hash is invalidated. Note that this just changes the hash; the output of the transaction remains the same and the bitcoins will go to their intended recipient. However this does mean that, for instance, it is not safe to accept a chain of unconfirmed transactions under any circumstance because the later transactions will depend on the hashes of the previous transactions, and those hashes can be changed until they are confirmed in a block (and potentially even after a confirmation if the block chain is reorganized). In addition, clients must always actively scan for transactions to them; assuming a txout exists because the client created it previously is unsafe.&lt;br /&gt;
&lt;br /&gt;
== Signature Malleability ==&lt;br /&gt;
&lt;br /&gt;
The first form of malleability is in the signatures themselves. Each signature has exactly one DER-encoded ASN.1 octet representation, but OpenSSL does not enforce this, and as long as a signature isn&#039;t horribly malformed, it will be accepted.&amp;lt;ref&amp;gt;https://bitcointalk.org/index.php?topic=8392.msg122410#msg122410&amp;lt;/ref&amp;gt; In addition for every ECDSA signature (r,s), the signature (r, -s (mod N)) is a valid signature of the same message.&amp;lt;ref&amp;gt;https://bitcointalk.org/index.php?topic=8392.msg1245898#msg1245898&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Efforts are underway to first make Bitcoin nodes not relay non-standard signatures, and eventually disallow them from being included in new blocks entirely.&lt;br /&gt;
&lt;br /&gt;
== scriptSig Malleability ==&lt;br /&gt;
&lt;br /&gt;
The [[OP_CHECKSIG|signature algorithm]] used in Bitcoin does not sign any of the scriptSig to create the signature. While signing the whole scriptSig would be impossible - the signature would be signing itself - this does mean that additional data can be added such that it will be pushed on the stack prior to the required signatures and public keys. Similarly OP_DROP can be added to leave the stack exactly as before prior to scriptPubKey execution.&lt;br /&gt;
&lt;br /&gt;
Preventing scriptSig malleability is being considered as well. Currently transactions with anything other than data push operations in their scriptSig are considered non-standard and are not relayed, and eventually this rule may extend to enforcing that the stack have exactly one item after execution. However doing that may interfere with later extensions to Bitcoin.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;br /&gt;
[[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Jayemar</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Data_directory&amp;diff=57451</id>
		<title>Data directory</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Data_directory&amp;diff=57451"/>
		<updated>2015-07-11T14:57:32Z</updated>

		<summary type="html">&lt;p&gt;Jayemar: Made the opening statement into a complete sentence.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The data directory is the location where Bitcoin&#039;s data files are stored, including the [[Wallet|wallet]] data file.&lt;br /&gt;
&lt;br /&gt;
==Default Location==&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
Go to Start -&amp;gt; Run (or press WinKey+R) and run this:&lt;br /&gt;
&lt;br /&gt;
 %APPDATA%\Bitcoin&lt;br /&gt;
&lt;br /&gt;
Bitcoin&#039;s data folder will open. For most users, this is the following locations:&lt;br /&gt;
&lt;br /&gt;
 C:\Documents and Settings\YourUserName\Application data\Bitcoin (XP)&lt;br /&gt;
 &lt;br /&gt;
 C:\Users\YourUserName\Appdata\Roaming\Bitcoin (Vista and 7)&lt;br /&gt;
&lt;br /&gt;
&amp;quot;AppData&amp;quot; and &amp;quot;Application data&amp;quot; are hidden by default.&lt;br /&gt;
&lt;br /&gt;
You can also store Bitcoin data files in any other drive or folder. &lt;br /&gt;
&lt;br /&gt;
If you have already downloaded the data then you will have to move the data to the new folder.&lt;br /&gt;
If you want to store them in D:\BitcoinData then click on &amp;quot;Properties&amp;quot; of a shortcut to bitcoin-qt.exe and&lt;br /&gt;
add -datadir=D:\BitCoinData at the end as an example:&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;C:\Program Files (x86)\Bitcoin\bitcoin-qt.exe&amp;quot; -datadir=d:\BitCoinData&lt;br /&gt;
&lt;br /&gt;
Start Bitcoin, now you will see all the files are created in the new data directory.&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
By default Bitcoin will put its data here:&lt;br /&gt;
&lt;br /&gt;
 ~/.bitcoin/&lt;br /&gt;
&lt;br /&gt;
You need to do a &amp;quot;ls -a&amp;quot; to see directories that start with a dot.&lt;br /&gt;
&lt;br /&gt;
If that&#039;s not it, you can do a search like this:&lt;br /&gt;
&lt;br /&gt;
 find / -name wallet.dat -print 2&amp;gt;/dev/null&lt;br /&gt;
&lt;br /&gt;
=== Mac ===&lt;br /&gt;
&lt;br /&gt;
By default Bitcoin will put its data here:&lt;br /&gt;
&lt;br /&gt;
 ~/Library/Application Support/Bitcoin/&lt;br /&gt;
&lt;br /&gt;
==Directory Contents==&lt;br /&gt;
&lt;br /&gt;
===Files===&lt;br /&gt;
* .lock&lt;br /&gt;
**BDB lock file&lt;br /&gt;
* bitcoin.conf [optional]&lt;br /&gt;
**Contains [[Running_Bitcoin#Bitcoin.conf_Configuration_File|configuration options]].  &lt;br /&gt;
* blk&#039;&#039;xxxx&#039;&#039;.dat [Versions prior to v0.8.0]&lt;br /&gt;
**Contains concatenated raw blocks.  Stored are actual Bitcoin blocks, in network format, dumped to disk raw.&lt;br /&gt;
* blkindex.dat [Versions prior to v0.8.0]&lt;br /&gt;
**Indexing information used with blk&#039;&#039;xxxx&#039;&#039;.dat&lt;br /&gt;
* __db.&#039;&#039;xxx&#039;&#039;&lt;br /&gt;
**Used by BDB&lt;br /&gt;
* db.log&lt;br /&gt;
* debug.log&lt;br /&gt;
**Bitcoin&#039;s verbose log file. Automatically trimmed from time to time.&lt;br /&gt;
* wallet.dat&lt;br /&gt;
**Storage for keys, transactions, metadata, and options. &amp;lt;span style=&amp;quot;color:red&amp;quot;&amp;gt;Please be sure to make backups of this file.  It contains the keys necessary for spending your bitcoins.&amp;lt;/span&amp;gt;&lt;br /&gt;
* addr.dat [Versions prior to v0.7.0]&lt;br /&gt;
** Storage for ip addresses to make a reconnect easier&lt;br /&gt;
* peers.dat [Versions v0.7.0 and later]&lt;br /&gt;
** Storage for peer information to make a reconnect easier.  This file uses a bitcoin-specific file format, unrelated to any database system&amp;lt;ref&amp;gt;[http://bitcointalk.org/index.php?topic=119525.msg1287284#msg1287284 Ultraprune merged in mainline]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
The data, index and log files are used by Oracle [http://en.wikipedia.org/wiki/Berkeley_DB Berkeley DB], the embedded key/value data store that Bitcoin uses.&lt;br /&gt;
&lt;br /&gt;
===database subdirectory===&lt;br /&gt;
Contains BDB journaling files&lt;br /&gt;
&lt;br /&gt;
===testnet3 subdirectory===&lt;br /&gt;
Contains testnet versions of these files (if running with -testnet)&lt;br /&gt;
&lt;br /&gt;
===blocks subdirectory===&lt;br /&gt;
[v0.8 and above] Contains blockchain data.  &lt;br /&gt;
&lt;br /&gt;
* blk*.dat &lt;br /&gt;
** Stored are actual Bitcoin blocks, in network format, dumped to disk raw.  They are only needed for re-scanning missing transactions in a wallet, reorganizing to a different part of the chain, and serving the block data to other nodes that are synchronizing.&lt;br /&gt;
&lt;br /&gt;
* blocks/index subdirectory&lt;br /&gt;
** [v0.8 and above] A LevelDB database that contains metadata about all known blocks, and where to find them on disk. Without this, finding a block would be very slow.&lt;br /&gt;
&lt;br /&gt;
===chainstate subdirectory===&lt;br /&gt;
[v0.8 and above] A LevelDB database with a compact representation of all currently unspent transaction outputs and some metadata about the transactions they are from. The data here is necessary for validating new incoming blocks and transactions. It can theoretically be rebuilt from the block data (see the -reindex command line option), but this takes a rather long time. Without it, you could still theoretically do validation indeed, but it would mean a full scan through the blocks (7 GB as of may 2013) for every output being spent.&lt;br /&gt;
&lt;br /&gt;
===locks subdirectory===&lt;br /&gt;
[v0.8 and above] Contains &amp;quot;undo&amp;quot; data. &lt;br /&gt;
&lt;br /&gt;
* rev*.dat&lt;br /&gt;
You can see blocks as &#039;patches&#039; to the chain state (they consume some unspent outputs, and produce new ones), and see the undo data as reverse patches. They are necessary for rolling back the chainstate, which is necessary in case of reorganizations.&lt;br /&gt;
&lt;br /&gt;
===Bootstrapping the blockchain from a snapshot distributed through BitTorrent===&lt;br /&gt;
There is a [https://bitcoin.org/bin/block-chain/ torrent file that gets updated] every few months that enables a much faster download of the blockchain. Once downloaded, the bootstrap.dat file can be placed in the root of the data directory, and Bitcoin Core 0.7.1 and above will automatically import it. &#039;&#039;&#039;NOTE:&#039;&#039;&#039; As of Bitcoin Core version 0.10.0 and later, the blockchain bootstrap&lt;br /&gt;
torrent is &#039;&#039;slower&#039;&#039; than a direct download using the bitcoin P2P protocol &amp;amp; client.&amp;lt;ref&amp;gt;[https://bitcoin.org/bin/block-chain/README.txt README.txt for bootstrap.dat.torrent]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Personally identifiable data [v0.8 and above]===&lt;br /&gt;
This section may be of use to you if you wish to send a friend the blockchain, avoiding them a hefty download.&lt;br /&gt;
&lt;br /&gt;
*wallet.dat&lt;br /&gt;
**Contains addresses and transactions linked to them. &amp;lt;span style=&amp;quot;color:red&amp;quot;&amp;gt;Please be sure to make backups of this file.  It contains the keys necessary for spending your bitcoins.&amp;lt;/span&amp;gt; You should not transfer this file to any third party or they may be able to access your bitcoins.&lt;br /&gt;
*db.log&lt;br /&gt;
**May contain information pertaining to your wallet. It may be safely deleted.&lt;br /&gt;
*debug.log&lt;br /&gt;
**May contain IP addresses and transaction ID&#039;s. It may be safely deleted.&lt;br /&gt;
*database/ folder&lt;br /&gt;
**This should only exist when bitcoin-qt is currently running. It contains information (BDB state) relating to your wallet.&lt;br /&gt;
*peers.dat&lt;br /&gt;
**Unknown whether this contains personally identifiable data. It may be safely deleted.&lt;br /&gt;
&lt;br /&gt;
Other files and folders (blocks, blocks/index, chainstate) may be safely transferred/archived as they contain information pertaining only to the public blockchain.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[Running Bitcoin]]&lt;br /&gt;
* [[Securing your wallet]]&lt;br /&gt;
* [http://bitcoin.stackexchange.com/a/11108/153 What is the database for?] Question on Bitcoin Stack Exchange&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;br /&gt;
&lt;br /&gt;
[[es:Directorio de datos]]&lt;/div&gt;</summary>
		<author><name>Jayemar</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Smart_Property&amp;diff=57450</id>
		<title>Smart Property</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Smart_Property&amp;diff=57450"/>
		<updated>2015-07-11T13:39:29Z</updated>

		<summary type="html">&lt;p&gt;Jayemar: Minor grammar fix breaking a phrase into two sentences&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Smart property&#039;&#039;&#039; is property whose ownership is controlled via the Bitcoin block chain, using [[Contracts|contracts]]. Examples could include physical property such as cars, phones or houses. Smart property also includes non-physical property like shares in a company or access rights to a remote computer. Making property smart allows it to be traded with radically less trust. This reduces fraud, mediation fees and allows trades to take place that otherwise would never have happened. For example, it allows strangers to loan you money over the internet taking your smart property as collateral, which should make lending more competitive and thus credit cheaper.&lt;br /&gt;
&lt;br /&gt;
Smart property was first proposed by Nick Szabo in his 1997 paper, [http://szabo.best.vwh.net/idea.html &amp;quot;The idea of smart contracts&amp;quot;]. There are currently no implementations of this idea. This page was written by [mailto:mike@plan99.net Mike Hearn].  Contact him with any questions or ask on the forums.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
Primitive forms of smart property are already common - if you own a car, it probably comes with an immobilizer. Immobilizers augment the physical key with a protocol exchange ensuring only the holders of the correct cryptographic token can activate the engine. They have dramatically reduced car theft, for example, immobilisers are fitted to around 45% of all cars in Australia, but account for only 7% of the cars that are stolen.&lt;br /&gt;
&lt;br /&gt;
Many other forms of modern property are protected against theft using cryptography, for example, some smartphones will refuse to release certain keys if the correct PIN unlock isn&#039;t entered, and cryptography not only renders a stolen device fairly useless but makes it impossible to steal someone&#039;s phone number as well.&lt;br /&gt;
&lt;br /&gt;
Although these are victories for cryptography, the potential of cryptographically activated property has not been fully explored. The private key is usually itself held in a physical container (like a key or SIM card) and can&#039;t be easily transferred or manipulated. Smart property changes this, allowing ownership to be intermediated by Bitcoin miners.&lt;br /&gt;
&lt;br /&gt;
==Theory==&lt;br /&gt;
&lt;br /&gt;
This section assumes you are familiar with the Bitcoin protocol, and have a good understanding of [[contracts]].&lt;br /&gt;
&lt;br /&gt;
Let&#039;s start with the example of a car. The cars computer requires authentication using an &#039;&#039;ownership key&#039;&#039;. The ownership key is a regular Bitcoin ECDSA-256 key. The car starts its life at the factory gate with the public part of a newly created ownership key. A small token amount of Bitcoins are deposited on that key, call the amount T (it could be 0.0001 BTC for example). Additionally the car has a digital certificate from its manufacturer, and an &#039;&#039;identification key&#039;&#039; which has the public part in the certificate. This allows the car to prove things like its existence, age or mileage to third parties.&lt;br /&gt;
&lt;br /&gt;
When the car is sold, the following protocol is used:&lt;br /&gt;
&lt;br /&gt;
# The buyer generates a nonce (random number) and asks the seller to send them the car data.&lt;br /&gt;
# The seller gives the car that nonce, and the car returns a data structure signed with its identification key. The data contains the nonce, the cars public cert, data about the car, the public key of the current owner, and the transaction+merkle branch which transferred ownership last time. This ensures the buyer knows what they are getting and that it came from the real seller (it&#039;s not a replay).&lt;br /&gt;
# The seller selects a key to receive the payment, k1, and names their price P.&lt;br /&gt;
# The buyer generates a new ownership key, k2.&lt;br /&gt;
# The buyer creates a transaction with two inputs and two outputs. The first input signs for P coins. The second input is connected to the output holding T coins for the ownership address. The first output sends P coins to k1 and the second output sends T coins to k2. This transaction is not valid because only the first input can be signed. The buyer passes this partially complete transaction to the seller, who then signs the second input with the car&#039;s current ownership key and broadcasts the transaction.&lt;br /&gt;
# They wait for some confirmations.&lt;br /&gt;
# The buyer presents the car with the Bitcoin transaction, a merkle branch linking it to the block header and then enough block headers to fill in the gap from the cars current ownership transaction. The car sees that the new transaction re-assigns ownership and is further along in the chain than its current one, plus it has enough work piled on top to be sure the tx won&#039;t be reversed. It then updates its ownership information. The car does not need to keep a full record of the chain nor all headers, but rather just enough data to be able to connect future block headers to the one it was previously presented with.&lt;br /&gt;
&lt;br /&gt;
In practice this process would likely be handled using smartphones with NFC hardware -- the act of touching the phone containing the ownership key to the dashboard would start your wallet app in a special mode that knows how to do smart property trades, after inputting the price the buyer and seller would then touch their phones together to finalize the deal. Although the cryptography is complex they would never need to know anything about it. The phone could double as a way to start the car as well.&lt;br /&gt;
&lt;br /&gt;
==Loans and collateral==&lt;br /&gt;
&lt;br /&gt;
Being able to trade physical property without fraud risk is useful, but we can add an extra layer to allow for secured low-trust loans. Consider a loan with which to start a small business. Rather than deal with a bank, you decide to allow people from around the world bid on your debt so you can get the best rates. For this to work, the strangers need some assurance that if the loan is not repaid, they get to keep the collateral - yet you still need to be able to use the car to set up the business.&lt;br /&gt;
&lt;br /&gt;
We can do this by adding &#039;&#039;access keys&#039;&#039; to the ownership key. By signing a message with the ownership key, access keys can be added or removed. Access keys can be temporary in nature. This means that for the duration of the loan, you can re-assign ownership of the vehicle to the creditor whilst keeping an access key for yourself.&lt;br /&gt;
&lt;br /&gt;
It would be best if the debtor had an assurance that, on repaying his debt, the cars ownership would indeed revert to his control. We can implement this as follows:&lt;br /&gt;
&lt;br /&gt;
# The creditor generates k1, which is used to receive the loan repayments. The loan size is L.&lt;br /&gt;
# The creditor signs Tx1 that has an input/output re-assigning ownership of the car back to the debtor which is signed with SIGHASH_ALL | SIGHASH_ANYONECANPAY, and an output for L coins to k1. This transaction is not valid because the loan has not yet been repaid, so the output sums to more value than the inputs. The creditor sends this transaction to the debtor who keeps it.&lt;br /&gt;
# As the debtor re-earns the money they spent, they add inputs to Tx1 to increase its value. This doesn&#039;t break the signature on the ownership key input/output pair because it was signed with SIGHASH_ANYONECANPAY so is independent of other inputs. They can&#039;t adjust the outputs or anything else about the transaction because that would invalidate the ownership input/output (SIGHASH_ALL).&lt;br /&gt;
# Once the transaction has enough inputs to sum to L, the debtor broadcasts the transaction, thus repaying their debt and simultaneously retaking ownership of the vehicle.&lt;br /&gt;
&lt;br /&gt;
Because access keys can be given time limits, if the debtor does not repay the loan by its maturity period his access key expires and the car will no longer start for him. The new owner can now either come and pick it up himself, or if he doesn&#039;t want to (eg he is in another country), he can sell it using the low-trust sales protocol described above and collect the money that way.&lt;br /&gt;
&lt;br /&gt;
Most loans are repaid in multiple installments. The same protocol as above can work in this case by embedding some control data in the extra input/output pair, the ownership key would not change but the signature would cover a command that extends the lifetime of the access key for another month. The vehicle would know how to parse the data out of the transaction.&lt;br /&gt;
&lt;br /&gt;
==Implementation details==&lt;br /&gt;
&lt;br /&gt;
For expiring access keys, the device must have a trustable source of time. Some devices like cars and phones keep time by themselves. In other cases where that&#039;s not practical for some reason, a secure timestamping service can be used. This is a service that signs a message containing the current time and a nonce. The device generates a nonce, and as part of the activation/switch-on protocol, a network connected device like a smartphone sends the nonce to the timestamping service then hands back the signed message. The block chain itself cannot be used as a source of time because there&#039;s no challenge/response aspect - the device has no way of knowing if you&#039;ve handed it the latest blocks or not. Signing the time with a nonce solves this.&lt;br /&gt;
&lt;br /&gt;
Smart phones play a key role in smart property because they have the ability to bridge devices without network access to the network, using Bluetooth or NFC radio. For instance, requiring internet access for a smart lock on a house door is too expensive and impractical. However, a lock with an NFC touchpoint that understands how to check block header progression is quite feasible. The only operations needed to implement Bitcoin-linked smart property is hashing, ECDSA and a small amount of storage. Smartcard chips that implement everything required are common and cheap.&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>Jayemar</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Distributed_markets&amp;diff=57449</id>
		<title>Distributed markets</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Distributed_markets&amp;diff=57449"/>
		<updated>2015-07-11T13:35:31Z</updated>

		<summary type="html">&lt;p&gt;Jayemar: /* Detour: a financial distributed hashmap */ Added a link for Kademlia&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bitcoin allows us to create &#039;&#039;&#039;distributed markets&#039;&#039;&#039; for the trading of securities, like stocks and bonds. Such markets have no central clearing house and can support securities of a size too small to be practical with today&#039;s techniques. It&#039;s also possible to build a P2P replacement for &#039;&#039;investment funds&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
This page was written by [mailto:mike@plan99.net Mike Hearn]. Contact him if you have any questions. Jeff Garzik has started implementing some of these ideas in his [https://github.com/jgarzik/smartcoin smartcoin] project. &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Let&#039;s start by defining some terms.&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;bond&#039;&#039; is a securitized loan. That means somebody who wants a loan can sell some bonds and whoever owns that bond receives the re-payments and any interest that accrues. The bond, being an asset like any other, is transferable - if you get tired of receiving the repayments you can sell the bond to somebody else. Once the full amount of the bond has been repaid, we say it has reached maturity. Some bonds never mature and yield interest payments in perpetuity.&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;stock&#039;&#039; is somewhat similar to a bond, except it never matures, yields irregular dividend payments instead of regular interest payments and confers voting rights upon the owner. In this document we will talk mostly about bonds, but the same ideas can be applied to build a stock market too.&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;ratings agency&#039;&#039; evaluates the creditworthiness of bond sellers and assigns bonds a consistent score (AAA, AA, junk, etc).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Sahai-Waters CP-ABE&#039;&#039; is a recent advance based on pairing-based encryption. Standard Bitcoin payments are point-to-point: I must know the target of my payment by knowing their key/address, and then I can send value to that target alone. Bitcoin script allows some flexibility, eg, by allowing threshold signatures or password based coins. But we lack the ability to pay to anyone who satisfies an arbitrary policy. Ciphertext-policy attribute based encryption (CP-ABE) allows you to encrypt some data such that anyone in possession of a key with attributes that match a formula can decrypt it.&lt;br /&gt;
&lt;br /&gt;
== Detour: a financial distributed hashmap ==&lt;br /&gt;
&lt;br /&gt;
A common problem encountered when designing advanced financial markets on top of Bitcoin is the need for a place to temporarily store small pieces of data, keyed by hash. This is because the block chain is not always the right place to put non-trivial amounts of data. The [https://en.wikipedia.org/wiki/Kademlia Kademlia] protocol and similar designs have solved this problem, albeit in a way that is vulnerable to a variety of DoS and Sybil attacks. But there is no network designed specifically for use in Bitcoin related systems. Today, their most common usage is in file sharing networks that are typically used for copyright infringement.&lt;br /&gt;
&lt;br /&gt;
A separate Kademlia network can be built that is optimized solely for financial usage. Nodes would restrict the size of the stored data and artificially throttle the serving of it, thus discouraging storage of movies, games, MP3s and other such things. Initial insertion or value longevity could be made subject to proof of a Bitcoin stake (aged balances or posted bond), deterring garbage. This means users who are interested in the financial applications but don&#039;t wish to assist file sharing can donate their resources to the network without issue.&lt;br /&gt;
&lt;br /&gt;
== A P2P bond network ==&lt;br /&gt;
&lt;br /&gt;
We start with the familiar structure of an independent P2P broadcast network that connects to the financial hashmap. There is no need for an alternative block chain. Bonds can be modeled as if they were [[Smart Property]].&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
message OutPoint {&lt;br /&gt;
  required bytes tx_hash = 1;&lt;br /&gt;
  required int index = 2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
message Issuer {&lt;br /&gt;
  enum AuthType {&lt;br /&gt;
    PUBLIC_KEY,&lt;br /&gt;
    EMAIL_ADDRESS,&lt;br /&gt;
    // Could add more in future, like EV SSL.&lt;br /&gt;
  }&lt;br /&gt;
  required AuthType auth_type = 1;&lt;br /&gt;
&lt;br /&gt;
  // Only one of the following should be set, according to authtype.&lt;br /&gt;
  optional bytes pubkey = 2;&lt;br /&gt;
  optional string email = 3;&lt;br /&gt;
&lt;br /&gt;
  // Name of the issuer as it will appear in the software, eg, a real name or company name.&lt;br /&gt;
  required string display_name = 4;&lt;br /&gt;
  // The script the issuer wants to receive the payments on.&lt;br /&gt;
  required bytes pay_to_script = 5;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
message Bond {&lt;br /&gt;
  required OutPoint start_point = 1;&lt;br /&gt;
  required Issuer issuer = 3;&lt;br /&gt;
  required int value = 2;&lt;br /&gt;
  required int coupon_value = 3;&lt;br /&gt;
&lt;br /&gt;
  // You could model more complex repayment schedules in future.&lt;br /&gt;
  required int repayment_value = 4;&lt;br /&gt;
&lt;br /&gt;
  // UNIX time of when the bond was issued.&lt;br /&gt;
  required int timestamp = 5;&lt;br /&gt;
&lt;br /&gt;
  // URL of the peer issuing the bond. This can be http://&amp;lt;home ip&amp;gt;:12345/ or &lt;br /&gt;
  // perhaps a Tor onion address.&lt;br /&gt;
  required string peer_url = 6;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The issuer, timestamp and value fields should be self explanatory. The bond message would be inserted into the hashmap so others who know its hash can find it.&lt;br /&gt;
&lt;br /&gt;
The start point identifies an output on the Bitcoin network that is created by the bond issuer. That output has a special form: it is of zero value and contains a script like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;BOND&amp;quot; &amp;lt;hash of bond message&amp;gt; 2DROP &amp;lt;issuer pubkey&amp;gt; CHECKSIG&lt;br /&gt;
&lt;br /&gt;
This output tracks the current owner of the bond. The owner receives the repayments and any accrued interest. Of course a newly issued bond is special, it starts out being owned by the issuer.&lt;br /&gt;
&lt;br /&gt;
The bond market client app that runs on your desktop follows the block chain, looking for Bitcoin transactions of that form. When it finds one, it downloads the bond message from the hashmap and shows the details in the UI.&lt;br /&gt;
&lt;br /&gt;
If the bond owner wishes to sell the bond, a sale message is broadcast on the exchange p2p network:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
message ProposedBondSale {&lt;br /&gt;
  required bytes bond_hash = 1;&lt;br /&gt;
  required int requested_value = 2;&lt;br /&gt;
  required bytes pay_to_script = 3;&lt;br /&gt;
  required string peer_url = 4;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If somebody wishes to buy that bond and sees the broadcast, they construct a transaction that spends requested_value of their own money to the pay_to_script, and add an input that connects to the zero-valued output of the current bondholder, and adds an output of the same BOND form to their own key. They pass it to the selling peer who checks that it looks valid and if so, signs for it then broadcasts thus atomically transferring the bond and the money.&lt;br /&gt;
&lt;br /&gt;
By scanning the block chain for payments to the current owner of the bond, and checking them against the repayment schedule, the software can automatically calculate which bonds are delinquent and which are mature. Issuers of delinquent bonds would show up in the UI as in debt until sufficient sums were paid to the owners pubkey.&lt;br /&gt;
&lt;br /&gt;
== Pay to policy outputs ==&lt;br /&gt;
&lt;br /&gt;
Creating a decentralized bond market is a good start, but real financial markets are more complex. Often there is a layer of abstraction between buyer and seller: an investment fund. The funds encapsulate general instructions from the clients, such as &amp;quot;buy low risk municipal bonds&amp;quot; or &amp;quot;invest in energy stocks&amp;quot; and translates that into a stream of purchases and sales of specific assets. Funds compete on how they choose the specific assets and therefore what return they get.&lt;br /&gt;
&lt;br /&gt;
Bitcoin payments today must be to a specific, concrete owner. By combining Bitcoin with CP-ABE we can instead make payments to a &#039;&#039;&#039;policy&#039;&#039;&#039;. A policy is a formula over a set of attributes. Attributes are arbitrary strings or string=integer pairs. Example policies include:&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;ceo or (accounts_department AND manager)&#039;&#039; - a policy that allows the CEO of the company access, or a manager in the accounts department, but not any other kind of manager or member of accounting.&lt;br /&gt;
# &#039;&#039;master_key OR (3 OF (a, b, c, d, e))&#039;&#039; - a threshold policy that is overridable by a master key. Your key must have at least 3 of the 5 lettered attributes to be usable.&lt;br /&gt;
# &#039;&#039;verification_class=MtGoxBasicKYC AND common_name=MikeHearn AND access_level &amp;gt; 5&#039;&#039; - a policy that requires a key with a basic identity assertion of a specific name, verified according to the MtGox basic class, and which has an &amp;quot;access level&amp;quot; above a certain number.&lt;br /&gt;
# &#039;&#039;debt_rating(bobs_rating_agency) &amp;gt; 3 AND mining_bond AND interest_rate &amp;gt; 2300 AND interest_rate &amp;lt; 5000 AND repayment_months &amp;lt; 6&#039;&#039; - a policy stating that it can be unlocked by anyone in possession of a key that asserts it was issued for a mining bond with better than a rating of &amp;quot;3&amp;quot; (imagine an AAA like rating system), that will repay fully within 6 months and with an interest rate between 2.3 and 5%.&lt;br /&gt;
&lt;br /&gt;
The policy is encoded into the ciphertext at encryption time, and the result can only be unlocked by a key that matches the policy. Keys attributes are assigned by an issuing authority.&lt;br /&gt;
&lt;br /&gt;
It may at first appear that some of the policies expressed above are also expressible with script. For instance, if each attribute is considered to be a separate private key, you could try expressing the first example as a script that uses some CHECKSIGs along with boolean operators. But there are some problems with that approach.&lt;br /&gt;
&lt;br /&gt;
The first problem is that people can collude to give themselves &amp;quot;superkeys&amp;quot;. The Sahai-Waters scheme is collusion-resistant. In the first example, a manager who is not in accounting could team up with somebody who&#039;s not a manager but in the right department, and combine their keys to be able to spend the companies money. With CP-ABE the keys cannot be combined to merge attributes like that.&lt;br /&gt;
&lt;br /&gt;
The second problem is that script cannot contain signatures over anything other than transactions, and only limited forms at that. So you cannot have numeric assertions in outputs because you have no way to verify the inputs are legitimate.&lt;br /&gt;
&lt;br /&gt;
The third problem is that script rapidly becomes inefficient if you want complex policies over hundreds or thousands of attributes. CP-ABE policies can be quite large whilst remaining feasible.&lt;br /&gt;
&lt;br /&gt;
There is a final reason to explore CP-ABE: whilst keys cannot be merged together to elevate privilege, you &#039;&#039;can&#039;&#039; remove attributes and thus delegated weakened power to others. In the first example, the manager of the accounts department does not need to get a new key issued from the key authority when a new employee joins: he can just remove the &amp;quot;manager&amp;quot; attribute from his key and generate a new key for the employee himself.&lt;br /&gt;
&lt;br /&gt;
In the straightforward CP-ABE scheme, there must be exactly one key issuing authority that verifies the client deserves the attributes they&#039;re about to be given and then mints the keys. It is possible to decentralize the scheme to become [http://eprint.iacr.org/2010/351.pdf multi-authority CP-ABE] using the Lewko-Waters design.&lt;br /&gt;
&lt;br /&gt;
Whilst some ABE schemes allow for hiding of the policy, the most expressive types make the policy public as part of the ciphertext. Fortunately this constraint is not a problem for us, indeed, it is an advantage.&lt;br /&gt;
&lt;br /&gt;
An implementation of single-authority CP-ABE (as well as KP-ABE where keys contain policies and ciphertexts contain attributes) is available in [http://code.google.com/p/libfenc/ libfenc]. Multi-authority CP-ABE is available in a Python library called Charm. There are obviously many applications of this technique. Here we will explore only one.&lt;br /&gt;
&lt;br /&gt;
== Investment funds ==&lt;br /&gt;
&lt;br /&gt;
Rather than wait for specific bonds to become available and then manually evaluate each one by hand, we can construct a kind of distributed investment fund by sending money to a Bitcoin private key that is then encrypted under a policy and uploaded into the financial hashmap:&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;POLICY&amp;quot; &amp;lt;hash of InvestmentFundAdvertisement message&amp;gt; 2DROP &amp;lt;pubkey&amp;gt; CHECKSIG&lt;br /&gt;
&lt;br /&gt;
Policy-locked ciphertexts are potentially quite large, depending on the size of the policy (a comparison against a large number like a date can use over a kilobyte), so it makes sense to use the same hash disassociation used in the bond protocol above in order to minimize block chain size.&lt;br /&gt;
&lt;br /&gt;
The hash would actually identify a data structure containing the ciphered private key:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
message InvestmentFundAdvertisement {&lt;br /&gt;
  required bytes owner_pubkey = 1;  // The key that will control the bond once sold.&lt;br /&gt;
  required bytes ciphertext = 2;    // Policy is embedded within this.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the bond market client crawls the block chain, it may encounter policy locked coins. By downloading the ciphertext from the hashmap, it can identify what conditions would unlock the coins and advertise that in the client. Somebody who is looking for business opportunities may see a message like this in their GUI:&lt;br /&gt;
&lt;br /&gt;
There are &#039;&#039;200 BTC&#039;&#039; available to any bond issuer meeting the following criteria:&lt;br /&gt;
# Must be a mining bond.&lt;br /&gt;
# Must be repaid entirely within 6 months.&lt;br /&gt;
# Must yield an interest rate between 2.3% and 5%&lt;br /&gt;
# Debt must be rated as AA or better by one of {MtGox Ratings, Moodys, Standard&amp;amp;Poors}.&lt;br /&gt;
# These assertions must have been issued within the last month&lt;br /&gt;
&lt;br /&gt;
Therefore demand is communicated to suppliers. By submitting such policy-locked coins, investors can send a message to the markets indicating that more mining capacity should be built.&lt;br /&gt;
&lt;br /&gt;
To be able to find the private key and take the coins, you must obtain a key that matches these attributes by talking to the various issuing authorities. Once you have such a key, any coins sent to such a policy can be taken by yourself. By requiring attributes such as &amp;quot;issued_in=2012/06&amp;quot; the policies can be expired when needed (at the cost of an additional block chain transaction to update the hash).&lt;/div&gt;</summary>
		<author><name>Jayemar</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Base58Check_encoding&amp;diff=57145</id>
		<title>Base58Check encoding</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Base58Check_encoding&amp;diff=57145"/>
		<updated>2015-07-03T13:59:18Z</updated>

		<summary type="html">&lt;p&gt;Jayemar: /* Creating a Base58Check string */  Added a link to the technical background of the bitcoin address&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A modified Base 58 [http://en.wikipedia.org/wiki/Binary-to-text_encoding binary-to-text encoding] known as &#039;&#039;&#039;Base58Check&#039;&#039;&#039; is used for encoding [[Bitcoin address|Bitcoin addresses]].&lt;br /&gt;
&lt;br /&gt;
More generically, Base58Check encoding is used for encoding byte arrays in Bitcoin into human-typable strings.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
The original Bitcoin client source code explains the reasoning behind base58 encoding:&lt;br /&gt;
&lt;br /&gt;
base58.h:&lt;br /&gt;
 // Why base-58 instead of standard base-64 encoding?&lt;br /&gt;
 // - Don&#039;t want 0OIl characters that look the same in some fonts and&lt;br /&gt;
 //      could be used to create visually identical looking account numbers.&lt;br /&gt;
 // - A string with non-alphanumeric characters is not as easily accepted as an account number.&lt;br /&gt;
 // - E-mail usually won&#039;t line-break if there&#039;s no punctuation to break at.&lt;br /&gt;
 // - Doubleclicking selects the whole number as one word if it&#039;s all alphanumeric.&lt;br /&gt;
&lt;br /&gt;
==Features of Base58Check==&lt;br /&gt;
Base58Check has the following features:&lt;br /&gt;
* An arbitrarily sized payload.&lt;br /&gt;
* A set of 58 alphanumeric symbols consisting of easily distinguished uppercase and lowercase letters (0OIl are not used) &lt;br /&gt;
* One byte of version/application information.  Bitcoin addresses use 0x00 for this byte (future ones may use 0x05).&lt;br /&gt;
* Four bytes (32 bits) of SHA256-based error checking code.  This code can be used to automatically detect and possibly correct typographical errors.&lt;br /&gt;
* An extra step for preservation of leading zeroes in the data.&lt;br /&gt;
&lt;br /&gt;
==Creating a Base58Check string==&lt;br /&gt;
A Base58Check string is created from a version/application byte and payload as follows.&lt;br /&gt;
# Take the version byte and payload bytes, and concatenate them together (bytewise).&lt;br /&gt;
# Take the first four bytes of SHA256(SHA256(results of step 1))&lt;br /&gt;
# Concatenate the results of step 1 and the results of step 2 together (bytewise).&lt;br /&gt;
# Treating the results of step 3 - a series of bytes - as a single big-endian bignumber, convert to base-58 using normal mathematical steps (bignumber division) and the base-58 alphabet described below.  The result should be normalized to not have any leading base-58 zeroes (character &#039;1&#039;).&lt;br /&gt;
# The leading character &#039;1&#039;, which has a value of zero in base58, is reserved for representing an entire leading zero &#039;&#039;&#039;byte&#039;&#039;&#039;, as when it is in a leading position, has no value as a base-58 symbol.  There can be one or more leading &#039;1&#039;s when necessary to represent one or more leading zero bytes.  Count the number of leading zero bytes that were the result of step 3 (for old Bitcoin addresses, there will always be at least one for the version/application byte; for new addresses, there will never be any).  Each leading zero byte shall be represented by its own character &#039;1&#039; in the final result.&lt;br /&gt;
# Concatenate the 1&#039;s from step 5 with the results of step 4.  &#039;&#039;&#039;This is the Base58Check result.&#039;&#039;&#039;&lt;br /&gt;
A more detailed example is provided on the page describing the [[Technical_background_of_version_1_Bitcoin_addresses#How_to_create_Bitcoin_Address | technical background]] of the bitcoin address.&lt;br /&gt;
&lt;br /&gt;
==Encoding a Bitcoin address==&lt;br /&gt;
Bitcoin addresses are implemented using the Base58Check encoding of the hash of either:&lt;br /&gt;
* Pay-to-script-hash (p2sh): payload is [[RIPEMD160]]([[SHA256]](redeemScript the wallet knows how to spend)); version 0x05 (these addresses begin with the digit &#039;3&#039;)&lt;br /&gt;
* Pay-to-pubkey-hash (p2pkh): payload is [[RIPEMD160]]([[SHA256]](ECDSA public key the wallet knows the private key for)); version 0x00 (these addresses begin with the digit &#039;1&#039;)&lt;br /&gt;
&lt;br /&gt;
The resulting hash in both of these cases is always exactly 20 bytes.&lt;br /&gt;
These are big-endian (most significant byte first).  (Beware of [[bignumber]] implementations that clip leading 0x00 bytes, or prepend extra 0x00 bytes to indicate sign - your code must handle these cases properly or else you may generate valid-looking addresses which can be sent to, but cannot be spent from - which would lead to the permanent loss of coins.)&lt;br /&gt;
&lt;br /&gt;
==Encoding a private key==&lt;br /&gt;
Base58Check encoding is also used for encoding [[private key|ECDSA private keys]] in the [[wallet import format]].&lt;br /&gt;
This is formed exactly the same as a Bitcoin address, except that 0x80 is used for the version/application byte, and the payload is 32 bytes instead of 20 (a private key in Bitcoin is a single 32-byte unsigned big-endian integer).&lt;br /&gt;
For private keys associated with an uncompressed public key, such encodings will always yield a 51-character string that starts with &#039;5&#039;, or more specifically, either &#039;5H&#039;, &#039;5J&#039;, or &#039;5K&#039;.&lt;br /&gt;
&lt;br /&gt;
==Base58 symbol chart==&lt;br /&gt;
The Base58 symbol chart used in Bitcoin is specific to the Bitcoin project and is not intended to be the same as any other Base58 implementation used outside the context of Bitcoin.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
!Value&lt;br /&gt;
!Character&lt;br /&gt;
!Value&lt;br /&gt;
!Character&lt;br /&gt;
!Value&lt;br /&gt;
!Character&lt;br /&gt;
!Value&lt;br /&gt;
!Character&lt;br /&gt;
|-&lt;br /&gt;
|0&lt;br /&gt;
|1&lt;br /&gt;
|1&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|3&lt;br /&gt;
|3&lt;br /&gt;
|4&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|5&lt;br /&gt;
|5&lt;br /&gt;
|6&lt;br /&gt;
|6&lt;br /&gt;
|7&lt;br /&gt;
|7&lt;br /&gt;
|8&lt;br /&gt;
|-&lt;br /&gt;
|8&lt;br /&gt;
|9&lt;br /&gt;
|9&lt;br /&gt;
|A&lt;br /&gt;
|10&lt;br /&gt;
|B&lt;br /&gt;
|11&lt;br /&gt;
|C&lt;br /&gt;
|-&lt;br /&gt;
|12&lt;br /&gt;
|D&lt;br /&gt;
|13&lt;br /&gt;
|E&lt;br /&gt;
|14&lt;br /&gt;
|F&lt;br /&gt;
|15&lt;br /&gt;
|G&lt;br /&gt;
|-&lt;br /&gt;
|16&lt;br /&gt;
|H&lt;br /&gt;
|17&lt;br /&gt;
|J&lt;br /&gt;
|18&lt;br /&gt;
|K&lt;br /&gt;
|19&lt;br /&gt;
|L&lt;br /&gt;
|-&lt;br /&gt;
|20&lt;br /&gt;
|M&lt;br /&gt;
|21&lt;br /&gt;
|N&lt;br /&gt;
|22&lt;br /&gt;
|P&lt;br /&gt;
|23&lt;br /&gt;
|Q&lt;br /&gt;
|-&lt;br /&gt;
|24&lt;br /&gt;
|R&lt;br /&gt;
|25&lt;br /&gt;
|S&lt;br /&gt;
|26&lt;br /&gt;
|T&lt;br /&gt;
|27&lt;br /&gt;
|U&lt;br /&gt;
|-&lt;br /&gt;
|28&lt;br /&gt;
|V&lt;br /&gt;
|29&lt;br /&gt;
|W&lt;br /&gt;
|30&lt;br /&gt;
|X&lt;br /&gt;
|31&lt;br /&gt;
|Y&lt;br /&gt;
|-&lt;br /&gt;
|32&lt;br /&gt;
|Z&lt;br /&gt;
|33&lt;br /&gt;
|a&lt;br /&gt;
|34&lt;br /&gt;
|b&lt;br /&gt;
|35&lt;br /&gt;
|c&lt;br /&gt;
|-&lt;br /&gt;
|36&lt;br /&gt;
|d&lt;br /&gt;
|37&lt;br /&gt;
|e&lt;br /&gt;
|38&lt;br /&gt;
|f&lt;br /&gt;
|39&lt;br /&gt;
|g&lt;br /&gt;
|-&lt;br /&gt;
|40&lt;br /&gt;
|h&lt;br /&gt;
|41&lt;br /&gt;
|i&lt;br /&gt;
|42&lt;br /&gt;
|j&lt;br /&gt;
|43&lt;br /&gt;
|k&lt;br /&gt;
|-&lt;br /&gt;
|44&lt;br /&gt;
|m&lt;br /&gt;
|45&lt;br /&gt;
|n&lt;br /&gt;
|46&lt;br /&gt;
|o&lt;br /&gt;
|47&lt;br /&gt;
|p&lt;br /&gt;
|-&lt;br /&gt;
|48&lt;br /&gt;
|q&lt;br /&gt;
|49&lt;br /&gt;
|r&lt;br /&gt;
|50&lt;br /&gt;
|s&lt;br /&gt;
|51&lt;br /&gt;
|t&lt;br /&gt;
|-&lt;br /&gt;
|52&lt;br /&gt;
|u&lt;br /&gt;
|53&lt;br /&gt;
|v&lt;br /&gt;
|54&lt;br /&gt;
|w&lt;br /&gt;
|55&lt;br /&gt;
|x&lt;br /&gt;
|-&lt;br /&gt;
|56&lt;br /&gt;
|y&lt;br /&gt;
|57&lt;br /&gt;
|z&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The algorithm for encoding address_byte_string (consisting of 1-byte_version + hash_or_other_data + 4-byte_check_code) is&lt;br /&gt;
&lt;br /&gt;
    code_string = &amp;quot;123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz&amp;quot;&lt;br /&gt;
    x = convert_bytes_to_big_integer(hash_result)&lt;br /&gt;
    &lt;br /&gt;
    output_string = &amp;quot;&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
    while(x &amp;gt; 0) &lt;br /&gt;
        {&lt;br /&gt;
            (x, remainder) = divide(x, 58)&lt;br /&gt;
            output_string.append(code_string[remainder])&lt;br /&gt;
        }&lt;br /&gt;
    &lt;br /&gt;
    repeat(number_of_leading_zero_bytes_in_hash)&lt;br /&gt;
        {&lt;br /&gt;
        output_string.append(code_string[0]);&lt;br /&gt;
        }&lt;br /&gt;
    &lt;br /&gt;
    output_string.reverse();&lt;br /&gt;
&lt;br /&gt;
==Version bytes==&lt;br /&gt;
Here are some common version bytes:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
!Decimal version&lt;br /&gt;
!Leading symbol&lt;br /&gt;
!Use&lt;br /&gt;
|-&lt;br /&gt;
|0&lt;br /&gt;
|1&lt;br /&gt;
|Bitcoin pubkey hash&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|3&lt;br /&gt;
|Bitcoin script hash&lt;br /&gt;
|-&lt;br /&gt;
|21&lt;br /&gt;
|4&lt;br /&gt;
|Bitcoin (compact) public key (proposed)&lt;br /&gt;
|-&lt;br /&gt;
|52&lt;br /&gt;
|M or N&lt;br /&gt;
|Namecoin pubkey hash&lt;br /&gt;
|-&lt;br /&gt;
|128&lt;br /&gt;
|5&lt;br /&gt;
|Private key&lt;br /&gt;
|-&lt;br /&gt;
|111&lt;br /&gt;
|m or n&lt;br /&gt;
|Bitcoin testnet pubkey hash&lt;br /&gt;
|-&lt;br /&gt;
|196&lt;br /&gt;
|2&lt;br /&gt;
|Bitcoin testnet script hash&lt;br /&gt;
|}&lt;br /&gt;
[[List of address prefixes]] is a more complete list.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [http://lenschulwitz.com/base58 Online Base58 Decoder, Encoder, and Validator]&lt;br /&gt;
&lt;br /&gt;
== Source code ==&lt;br /&gt;
* [https://github.com/bitcoin/bitcoin/blob/master/src/base58.cpp &amp;quot;Satoshi&amp;quot; C++ codebase (decode and encode, no external libraries needed)]&lt;br /&gt;
* [https://github.com/luke-jr/libbase58 libbase58 C code (decode and encode, no external libraries needed)]&lt;br /&gt;
* [http://lenschulwitz.com/b58/base58perl.txt Base58 Decode, Encode, and Validate in Perl]&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;br /&gt;
&lt;br /&gt;
[[es:Codificación Base58Check]]&lt;/div&gt;</summary>
		<author><name>Jayemar</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=OP_CHECKSIG&amp;diff=57140</id>
		<title>OP CHECKSIG</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=OP_CHECKSIG&amp;diff=57140"/>
		<updated>2015-07-02T23:54:02Z</updated>

		<summary type="html">&lt;p&gt;Jayemar: Clarified some of the grammar use in the explanation&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;OP_CHECKSIG&#039;&#039;&#039; is [[Script|script]] opcode used to verify that the signature for a tx input is valid. OP_CHECKSIG expects two values to be on the stack.  These are, in order of stack depth, the public key and the signature of the script. These two values are normally obtained by running the scriptSig script of the transaction input we are attempting to validate. The scriptSig script is deleted after it is run, but the stack is left as is.  Then, the scriptPubKey script from the previous transaction output that is now being spent is run, generally concluding in an OP_CHECKSIG. &lt;br /&gt;
&lt;br /&gt;
The standard scriptPubKey checks that the public key (actually a hash of) is a particular value, and that OP_CHECKSIG passes.&lt;br /&gt;
&lt;br /&gt;
For normal transaction inputs, if the creator of the current transaction can successfully create a ScriptSig signature that uses the right public key for the ScriptPubKey of the transaction output they are attempting to spend, that transaction input is considered valid. &lt;br /&gt;
&lt;br /&gt;
== Parameters ==&lt;br /&gt;
&lt;br /&gt;
In addition to the stack parameters and the script code itself, in order to operate correctly OP_CHECKSIG needs to know the current transaction and the index of current transaction input.&lt;br /&gt;
&lt;br /&gt;
== How it works ==&lt;br /&gt;
Firstly always this (the default) procedure is applied:&lt;br /&gt;
[[File:Bitcoin_OpCheckSig_InDetail.png|thumb|right|Signature verification process of the default procedure]]&lt;br /&gt;
# the public key and the signature are popped from the stack, in that order. If the hash-type value is 0, then it is replaced by the last_byte of the signature. Then the last byte of the signature is always deleted.&lt;br /&gt;
# A new subscript is created from the instruction from the most recently parsed OP_CODESEPARATOR (last one in script) to the end of the script. If there is no OP_CODESEPARATOR the entire script becomes the subscript (hereby referred to as subScript)&lt;br /&gt;
# The sig is deleted from subScript.&lt;br /&gt;
# All OP_CODESEPARATORS are removed from subScript&lt;br /&gt;
# The hashtype is removed from the last byte of the sig and stored&lt;br /&gt;
# A copy is made of the current transaction (hereby referred to txCopy)&lt;br /&gt;
# The scripts for all transaction inputs in txCopy are set to empty scripts (exactly 1 byte 0x00)&lt;br /&gt;
# The script for the current transaction input in txCopy is set to subScript (lead in by its length as a var-integer encoded!)&lt;br /&gt;
&lt;br /&gt;
Now depending on the hashtype various things can happen to txCopy, these will be discussed individually.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Hashtype Values (from script.h):&#039;&#039;&#039;&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Name !! Value&lt;br /&gt;
|-&lt;br /&gt;
| SIGHASH_ALL || 0x00000001&lt;br /&gt;
|-&lt;br /&gt;
| SIGHASH_NONE || 0x00000002&lt;br /&gt;
|-&lt;br /&gt;
| SIGHASH_SINGLE || 0x00000003&lt;br /&gt;
|-&lt;br /&gt;
| SIGHASH_ANYONECANPAY || 0x00000080&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; If (hashtype&amp;amp;31) = SIGHASH_NONE then apply the SIGHASH_NONE-procedure&lt;br /&gt;
&amp;lt;li&amp;gt; If (hashtype&amp;amp;31) = SIGHASH_SINGLE then apply the SIGHASH_SINGLE-procedure&lt;br /&gt;
&amp;lt;li&amp;gt; If hashtype &amp;amp; SIGHASH_ANYONECANPAY then apply the SIGHASH_ANYONECANPAY-procedure&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
Hence, hashtype SIGHASH_ANYONECANPAY may be applied also after any other hashtype-procedure&amp;lt;ref&amp;gt;[http://sourceforge.net/projects/bitcoin/files/Bitcoin/bitcoin-0.7.1/bitcoin-0.7.1-linux.tar.gz file src/src/script.cpp in bitcoin-0.7.1]&amp;lt;/ref&amp;gt;. Besides the four listed hashtypes only a hashtype of value 0 appears a few times in the (main) block chain (and is handled like SIGHASH_ALL).&lt;br /&gt;
&lt;br /&gt;
=== Hashtype SIGHASH_ALL (default) ===&lt;br /&gt;
&lt;br /&gt;
No special further handling occurs in the default case.  Think of this as &amp;quot;sign &#039;&#039;&#039;all&#039;&#039;&#039; of the outputs.&amp;quot; Which is already done by the default procedure.&lt;br /&gt;
&lt;br /&gt;
=== Procedure for Hashtype SIGHASH_NONE ===&lt;br /&gt;
&lt;br /&gt;
# The output of txCopy is set to a vector of zero size.&lt;br /&gt;
# All other inputs aside from the current input in txCopy have their nSequence index set to zero&lt;br /&gt;
&lt;br /&gt;
Think of this as &amp;quot;sign &#039;&#039;&#039;none&#039;&#039;&#039; of the outputs-- I don&#039;t care where the bitcoins go.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Procedure for Hashtype SIGHASH_SINGLE ===&lt;br /&gt;
&lt;br /&gt;
# The output of txCopy is resized to the size of the current input index+1.&lt;br /&gt;
# All other txCopy outputs aside from the output that is the same as the current input index are set to a blank script and a value of (long) -1.&lt;br /&gt;
# All other txCopy inputs aside from the current input are set to have an nSequence index of zero.&lt;br /&gt;
&lt;br /&gt;
Think of this as &amp;quot;sign &#039;&#039;&#039;one&#039;&#039;&#039; of the outputs-- I don&#039;t care where the other outputs go&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Note: The transaction that uses SIGHASH_SINGLE type of signature should not have more inputs than outputs.&lt;br /&gt;
However if it does (because of the pre-existing implementation), it shall not be rejected, but instead for every &amp;quot;illegal&amp;quot; input (meaning: an input that has an index bigger than the maximum output index) the node should still verify it, though assuming the hash of 0000000000000000000000000000000000000000000000000000000000000001 [https://bitcointalk.org/index.php?topic=260595.0]&lt;br /&gt;
&lt;br /&gt;
=== Procedure for Hashtype SIGHASH_ANYONECANPAY ===&lt;br /&gt;
&lt;br /&gt;
# The txCopy input vector is resized to a length of one.&lt;br /&gt;
# The subScript (lead in by its length as a var-integer encoded!) is set as the first and only member of this vector.&lt;br /&gt;
&lt;br /&gt;
Think of this as &amp;quot;Let other people add inputs to this transaction, I don&#039;t care where the rest of the bitcoins come from.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
===Final signature check===&lt;br /&gt;
&lt;br /&gt;
An array of bytes is constructed from the serialized txCopy appended by four bytes for the hash type. This array is sha256 hashed twice, then the public key is used to check the supplied signature against the hash.&lt;br /&gt;
The secp256k1 elliptic curve is used for the verification with the given public key.&lt;br /&gt;
&lt;br /&gt;
== Return values ==&lt;br /&gt;
&lt;br /&gt;
OP_CHECKSIG will push true to the stack if the check passed, false otherwise.&lt;br /&gt;
OP_CHECKSIG_VERIFY leaves nothing on the stack but will cause the script eval to fail immediately if the check does not pass.&lt;br /&gt;
&lt;br /&gt;
== Code samples and raw dumps ==&lt;br /&gt;
&lt;br /&gt;
Taking the first transaction in Bitcoin which is in block number 170, we would get after serialising the transaction but before we hash+sign (or verify) it:&lt;br /&gt;
&lt;br /&gt;
* http://blockexplorer.com/block/00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee&lt;br /&gt;
* http://blockexplorer.com/tx/f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16&lt;br /&gt;
&lt;br /&gt;
See also [https://gitorious.org/libbitcoin/libbitcoin libbitcoin] for code samples.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
01 00 00 00              version&lt;br /&gt;
01                       number of inputs (var_uint)&lt;br /&gt;
&lt;br /&gt;
input 0:&lt;br /&gt;
c9 97 a5 e5 6e 10 41 02  input address hash&lt;br /&gt;
fa 20 9c 6a 85 2d d9 06 &lt;br /&gt;
60 a2 0b 2d 9c 35 24 23 &lt;br /&gt;
ed ce 25 85 7f cd 37 04&lt;br /&gt;
00 00 00 00              input index&lt;br /&gt;
&lt;br /&gt;
48                       size of script (var_uint)&lt;br /&gt;
47                       push 71 bytes to stack&lt;br /&gt;
30 44 02 20 4e 45 e1 69&lt;br /&gt;
32 b8 af 51 49 61 a1 d3&lt;br /&gt;
a1 a2 5f df 3f 4f 77 32&lt;br /&gt;
e9 d6 24 c6 c6 15 48 ab&lt;br /&gt;
5f b8 cd 41 02 20 18 15&lt;br /&gt;
22 ec 8e ca 07 de 48 60&lt;br /&gt;
a4 ac dd 12 90 9d 83 1c&lt;br /&gt;
c5 6c bb ac 46 22 08 22&lt;br /&gt;
21 a8 76 8d 1d 09 01&lt;br /&gt;
ff ff ff ff              sequence&lt;br /&gt;
&lt;br /&gt;
02                       number of outputs (var_uint)&lt;br /&gt;
&lt;br /&gt;
output 0:&lt;br /&gt;
00 ca 9a 3b 00 00 00 00  amount = 10.00000000&lt;br /&gt;
43                       size of script (var_uint)&lt;br /&gt;
script for output 0:&lt;br /&gt;
41                       push 65 bytes to stack&lt;br /&gt;
04 ae 1a 62 fe 09 c5 f5 &lt;br /&gt;
1b 13 90 5f 07 f0 6b 99 &lt;br /&gt;
a2 f7 15 9b 22 25 f3 74 &lt;br /&gt;
cd 37 8d 71 30 2f a2 84 &lt;br /&gt;
14 e7 aa b3 73 97 f5 54 &lt;br /&gt;
a7 df 5f 14 2c 21 c1 b7 &lt;br /&gt;
30 3b 8a 06 26 f1 ba de &lt;br /&gt;
d5 c7 2a 70 4f 7e 6c d8 &lt;br /&gt;
4c &lt;br /&gt;
ac                       OP_CHECKSIG&lt;br /&gt;
&lt;br /&gt;
output 1:&lt;br /&gt;
00 28 6b ee 00 00 00 00  amount = 40.00000000&lt;br /&gt;
43                       size of script (var_uint)&lt;br /&gt;
script for output 1:&lt;br /&gt;
41                       push 65 bytes to stack&lt;br /&gt;
04 11 db 93 e1 dc db 8a  &lt;br /&gt;
01 6b 49 84 0f 8c 53 bc &lt;br /&gt;
1e b6 8a 38 2e 97 b1 48 &lt;br /&gt;
2e ca d7 b1 48 a6 90 9a&lt;br /&gt;
5c b2 e0 ea dd fb 84 cc &lt;br /&gt;
f9 74 44 64 f8 2e 16 0b &lt;br /&gt;
fa 9b 8b 64 f9 d4 c0 3f &lt;br /&gt;
99 9b 86 43 f6 56 b4 12 &lt;br /&gt;
a3                       &lt;br /&gt;
ac                       OP_CHECKSIG&lt;br /&gt;
&lt;br /&gt;
00 00 00 00              locktime&lt;br /&gt;
01 00 00 00              hash_code_type (added on)&lt;br /&gt;
&lt;br /&gt;
result =&lt;br /&gt;
01 00 00 00 01 c9 97 a5 e5 6e 10 41 02 fa 20 9c 6a 85 2d d9 06 60 a2 0b 2d 9c 35 &lt;br /&gt;
24 23 ed ce 25 85 7f cd 37 04 00 00 00 00 43 41 04 11 db 93 e1 dc db 8a 01 6b 49 &lt;br /&gt;
84 0f 8c 53 bc 1e b6 8a 38 2e 97 b1 48 2e ca d7 b1 48 a6 90 9a 5c b2 e0 ea dd fb &lt;br /&gt;
84 cc f9 74 44 64 f8 2e 16 0b fa 9b 8b 64 f9 d4 c0 3f 99 9b 86 43 f6 56 b4 12 a3 &lt;br /&gt;
ac ff ff ff ff 02 00 ca 9a 3b 00 00 00 00 43 41 04 ae 1a 62 fe 09 c5 f5 1b 13 90 &lt;br /&gt;
5f 07 f0 6b 99 a2 f7 15 9b 22 25 f3 74 cd 37 8d 71 30 2f a2 84 14 e7 aa b3 73 97 &lt;br /&gt;
f5 54 a7 df 5f 14 2c 21 c1 b7 30 3b 8a 06 26 f1 ba de d5 c7 2a 70 4f 7e 6c d8 4c &lt;br /&gt;
ac 00 28 6b ee 00 00 00 00 43 41 04 11 db 93 e1 dc db 8a 01 6b 49 84 0f 8c 53 bc &lt;br /&gt;
1e b6 8a 38 2e 97 b1 48 2e ca d7 b1 48 a6 90 9a 5c b2 e0 ea dd fb 84 cc f9 74 44 &lt;br /&gt;
64 f8 2e 16 0b fa 9b 8b 64 f9 d4 c0 3f 99 9b 86 43 f6 56 b4 12 a3 ac 00 00 00 00 &lt;br /&gt;
01 00 00 00 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To understand where that raw dump has come from, it may be useful to examine tests/ec-key.cpp in [https://gitorious.org/libbitcoin/libbitcoin libbitcoin],&lt;br /&gt;
&lt;br /&gt;
[https://gitorious.org/libbitcoin/libbitcoin libbitcoin] has a unit test under tests/ec-key.cpp (make ec-key &amp;amp;&amp;amp; ./bin/tests/ec-key). There is also a working OP_CHECKSIG implementation in src/script.cpp under script::op_checksig(). See also the unit test: tests/script-test.cpp&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;iomanip&amp;gt;&lt;br /&gt;
#include &amp;lt;bitcoin/util/serializer.hpp&amp;gt;&lt;br /&gt;
#include &amp;lt;bitcoin/util/elliptic_curve_key.hpp&amp;gt;&lt;br /&gt;
#include &amp;lt;bitcoin/util/sha256.hpp&amp;gt;&lt;br /&gt;
#include &amp;lt;bitcoin/util/assert.hpp&amp;gt;&lt;br /&gt;
#include &amp;lt;bitcoin/util/logger.hpp&amp;gt;&lt;br /&gt;
#include &amp;lt;bitcoin/types.hpp&amp;gt;&lt;br /&gt;
#include &amp;lt;openssl/ecdsa.h&amp;gt;&lt;br /&gt;
#include &amp;lt;openssl/obj_mac.h&amp;gt;&lt;br /&gt;
using libbitcoin::elliptic_curve_key;&lt;br /&gt;
using libbitcoin::serializer;&lt;br /&gt;
using libbitcoin::hash_digest;&lt;br /&gt;
using libbitcoin::data_chunk;&lt;br /&gt;
using libbitcoin::log_info;&lt;br /&gt;
using libbitcoin::log_fatal;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    serializer ss;&lt;br /&gt;
    // blk number 170, tx 1, input 0&lt;br /&gt;
    // version = 1&lt;br /&gt;
    ss.write_4_bytes(1);&lt;br /&gt;
    // 1 inputs&lt;br /&gt;
    ss.write_var_uint(1);&lt;br /&gt;
&lt;br /&gt;
    // input 0&lt;br /&gt;
    // prevout hash&lt;br /&gt;
    ss.write_hash(hash_digest{0x04, 0x37, 0xcd, 0x7f, 0x85, 0x25, 0xce, 0xed, 0x23, 0x24, 0x35, 0x9c, 0x2d, 0x0b, 0xa2, 0x60, 0x06, 0xd9, 0x2d, 0x85, 0x6a, 0x9c, 0x20, 0xfa, 0x02, 0x41, 0x10, 0x6e, 0xe5, 0xa5, 0x97, 0xc9});&lt;br /&gt;
    // prevout index &lt;br /&gt;
    ss.write_4_bytes(0);&lt;br /&gt;
&lt;br /&gt;
    // input script after running OP_CHECKSIG for this tx is a single&lt;br /&gt;
    // OP_CHECKSIG opcode&lt;br /&gt;
    data_chunk raw_data;&lt;br /&gt;
    raw_data = {0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a, 0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e, 0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca, 0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0, 0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64, 0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9, 0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56, 0xb4, 0x12, 0xa3};&lt;br /&gt;
    data_chunk raw_script;&lt;br /&gt;
    raw_script = data_chunk();&lt;br /&gt;
    raw_script.push_back(raw_data.size());&lt;br /&gt;
    libbitcoin::extend_data(raw_script, raw_data);&lt;br /&gt;
    raw_script.push_back(172);&lt;br /&gt;
    ss.write_var_uint(raw_script.size());&lt;br /&gt;
    ss.write_data(raw_script);&lt;br /&gt;
    // sequence&lt;br /&gt;
    ss.write_4_bytes(0xffffffff);&lt;br /&gt;
&lt;br /&gt;
    // 2 outputs for this tx&lt;br /&gt;
    ss.write_var_uint(2);&lt;br /&gt;
&lt;br /&gt;
    // output 0&lt;br /&gt;
    ss.write_8_bytes(1000000000);&lt;br /&gt;
    // script for output 0&lt;br /&gt;
    raw_data = {0x04, 0xae, 0x1a, 0x62, 0xfe, 0x09, 0xc5, 0xf5, 0x1b, 0x13, 0x90, 0x5f, 0x07, 0xf0, 0x6b, 0x99, 0xa2, 0xf7, 0x15, 0x9b, 0x22, 0x25, 0xf3, 0x74, 0xcd, 0x37, 0x8d, 0x71, 0x30, 0x2f, 0xa2, 0x84, 0x14, 0xe7, 0xaa, 0xb3, 0x73, 0x97, 0xf5, 0x54, 0xa7, 0xdf, 0x5f, 0x14, 0x2c, 0x21, 0xc1, 0xb7, 0x30, 0x3b, 0x8a, 0x06, 0x26, 0xf1, 0xba, 0xde, 0xd5, 0xc7, 0x2a, 0x70, 0x4f, 0x7e, 0x6c, 0xd8, 0x4c};&lt;br /&gt;
    // when data &amp;lt; 75, we can just write it&#039;s length as a single byte (&#039;special&#039;&lt;br /&gt;
    // opcodes)&lt;br /&gt;
    raw_script = data_chunk();&lt;br /&gt;
    raw_script.push_back(raw_data.size());&lt;br /&gt;
    libbitcoin::extend_data(raw_script, raw_data);&lt;br /&gt;
    // OP_CHECKSIG&lt;br /&gt;
    raw_script.push_back(172);&lt;br /&gt;
    // now actually write the script&lt;br /&gt;
    ss.write_var_uint(raw_script.size());&lt;br /&gt;
    ss.write_data(raw_script);&lt;br /&gt;
&lt;br /&gt;
    // output 1&lt;br /&gt;
    ss.write_8_bytes(4000000000);&lt;br /&gt;
    // script for output 0&lt;br /&gt;
    raw_data = {0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a, 0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e, 0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca, 0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0, 0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64, 0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9, 0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56, 0xb4, 0x12, 0xa3};&lt;br /&gt;
    // when data &amp;lt; 75, we can just write it&#039;s length as a single byte (&#039;special&#039;&lt;br /&gt;
    raw_script.push_back(raw_data.size());&lt;br /&gt;
    libbitcoin::extend_data(raw_script, raw_data);&lt;br /&gt;
    // OP_CHECKSIG&lt;br /&gt;
    raw_script.push_back(172);&lt;br /&gt;
    // now actually write the script&lt;br /&gt;
    ss.write_var_uint(raw_script.size());&lt;br /&gt;
    ss.write_data(raw_script);&lt;br /&gt;
&lt;br /&gt;
    // End of 2 outputs&lt;br /&gt;
&lt;br /&gt;
    // locktime&lt;br /&gt;
    ss.write_4_bytes(0);&lt;br /&gt;
&lt;br /&gt;
    // write hash_type_code&lt;br /&gt;
    ss.write_4_bytes(1);&lt;br /&gt;
&lt;br /&gt;
    // Dump hex to screen&lt;br /&gt;
    log_info() &amp;lt;&amp;lt; &amp;quot;hashing:&amp;quot;;&lt;br /&gt;
    {&lt;br /&gt;
        auto log_obj = log_info();&lt;br /&gt;
        log_obj &amp;lt;&amp;lt; std::hex;&lt;br /&gt;
        for (int val: ss.get_data())&lt;br /&gt;
            log_obj &amp;lt;&amp;lt; std::setfill(&#039;0&#039;) &amp;lt;&amp;lt; std::setw(2) &amp;lt;&amp;lt; val &amp;lt;&amp;lt; &#039; &#039;;&lt;br /&gt;
    }&lt;br /&gt;
    log_info();&lt;br /&gt;
&lt;br /&gt;
    data_chunk raw_tx = {0x01, 0x00, 0x00, 0x00, 0x01, 0xc9, 0x97, 0xa5, 0xe5, 0x6e, 0x10, 0x41, 0x02, 0xfa, 0x20, 0x9c, 0x6a, 0x85, 0x2d, 0xd9, 0x06, 0x60, 0xa2, 0x0b, 0x2d, 0x9c, 0x35, 0x24, 0x23, 0xed, 0xce, 0x25, 0x85, 0x7f, 0xcd, 0x37, 0x04, 0x00, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a, 0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e, 0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca, 0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0, 0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64, 0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9, 0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56, 0xb4, 0x12, 0xa3, 0xac, 0xff, 0xff, 0xff, 0xff, 0x02, 0x00, 0xca, 0x9a, 0x3b, 0x00, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, 0xae, 0x1a, 0x62, 0xfe, 0x09, 0xc5, 0xf5, 0x1b, 0x13, 0x90, 0x5f, 0x07, 0xf0, 0x6b, 0x99, 0xa2, 0xf7, 0x15, 0x9b, 0x22, 0x25, 0xf3, 0x74, 0xcd, 0x37, 0x8d, 0x71, 0x30, 0x2f, 0xa2, 0x84, 0x14, 0xe7, 0xaa, 0xb3, 0x73, 0x97, 0xf5, 0x54, 0xa7, 0xdf, 0x5f, 0x14, 0x2c, 0x21, 0xc1, 0xb7, 0x30, 0x3b, 0x8a, 0x06, 0x26, 0xf1, 0xba, 0xde, 0xd5, 0xc7, 0x2a, 0x70, 0x4f, 0x7e, 0x6c, 0xd8, 0x4c, 0xac, 0x00, 0x28, 0x6b, 0xee, 0x00, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a, 0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e, 0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca, 0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0, 0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64, 0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9, 0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56, 0xb4, 0x12, 0xa3, 0xac, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00};&lt;br /&gt;
    BITCOIN_ASSERT(raw_tx == ss.get_data());&lt;br /&gt;
&lt;br /&gt;
    hash_digest tx_hash = libbitcoin::generate_sha256_hash(ss.get_data());&lt;br /&gt;
&lt;br /&gt;
    data_chunk pubkey{0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a, 0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e, 0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca, 0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0, 0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64, 0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9, 0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56, 0xb4, 0x12, 0xa3};&lt;br /&gt;
    // Leave out last byte since that&#039;s the hash_type_code (SIGHASH_ALL in this&lt;br /&gt;
    // case)&lt;br /&gt;
    data_chunk signature{0x30, 0x44, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69, 0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1, 0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6, 0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd, 0x41, 0x02, 0x20, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca, 0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90, 0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22, 0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09};&lt;br /&gt;
    BITCOIN_ASSERT(signature.size() == 70);&lt;br /&gt;
&lt;br /&gt;
    elliptic_curve_key key;&lt;br /&gt;
    if (!key.set_public_key(pubkey))&lt;br /&gt;
    {&lt;br /&gt;
        log_fatal() &amp;lt;&amp;lt; &amp;quot;unable to set EC public key&amp;quot;;&lt;br /&gt;
        return -1;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    log_info() &amp;lt;&amp;lt; &amp;quot;checksig returns: &amp;quot; &amp;lt;&amp;lt; (key.verify(tx_hash, signature) ? &amp;quot;true&amp;quot; : &amp;quot;false&amp;quot;);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
{{DISPLAYTITLE:OP_CHECKSIG}}&lt;br /&gt;
{{good}}&lt;/div&gt;</summary>
		<author><name>Jayemar</name></author>
	</entry>
</feed>