mirror of
https://github.com/ChristopherA/Learning-Bitcoin-from-the-Command-Line.git
synced 2025-06-10 01:16:17 +00:00
Update 7_1_Understanding_the_Foundation_of_Transactions.md
This commit is contained in:
parent
06cc7cbe5a
commit
e5a36fd3cd
@ -6,14 +6,14 @@ The foundation of Bitcoin is the ability to protect the transactions, something
|
|||||||
|
|
||||||
## Know the Parts of the Cryptographic Puzzle
|
## Know the Parts of the Cryptographic Puzzle
|
||||||
|
|
||||||
As described in [Chapter 1](1_0_Introducing_Bitcoin.md), the funds in each Bitcoin transaction are locked with a cryptographic puzzle. To be precise, we said that Bitcoin is made up of "a sequence of atomic transactions: each of which is enabled by the sender with the solution to a cryptographic puzzle that is stored as a script; each of which is locked for the recipient with a new cryptographic puzzle that is stored as a script". Those scripts, which lock and unlock transactions, are written in Bitcoin Script.
|
As described in [Chapter 1](1_0_Introducing_Bitcoin.md), the funds in each Bitcoin transaction are locked with a cryptographic puzzle. To be precise, we said that Bitcoin is made up of "a sequence of atomic transactions: each of which is enabled by the sender with the solution to a previous cryptographic puzzle that was stored as a script; each of which is locked for the recipient with a new cryptographic puzzle that is stored as a script". Those scripts, which lock and unlock transactions, are written in Bitcoin Script.
|
||||||
|
|
||||||
_What is Bitcoin Script?_ Bitcoin Script is a stack-based Forth-like language that purposefully avoids loops and is not Turing-complete. It's made up of individual opcodes. Every single transaction in Bitcoin is locked with a Bitcoin Script; when the locking transaction for a UTXO is run with the correct inputs, that UTXO can then be spent.
|
_What is Bitcoin Script?_ Bitcoin Script is a stack-based Forth-like language that purposefully avoids loops and so is not Turing-complete. It's made up of individual opcodes. Every single transaction in Bitcoin is locked with a Bitcoin Script; when the locking transaction for a UTXO is run with the correct inputs, that UTXO can then be spent.
|
||||||
|
|
||||||
The fact that transactions are locked with scripts means that they can be unlocked in a variety of different ways. In fact, we've met a number of different unlocking mechanisms to date, each of which incorporates different opcodes:
|
The fact that transactions are locked with scripts means that they can be unlocked in a variety of different ways. In fact, we've met a number of different unlocking mechanisms to date, each of which incorporates different opcodes:
|
||||||
|
|
||||||
* OP_CHECKSIG, which checks a public key against a signature is the basis of a P2PKH address, as will be fully detailed in [7.3: Scripting a P2PKH](7_3_Scripting_a_P2PKH.md).
|
* OP_CHECKSIG, which checks a public key against a signature is the basis of a P2PKH address, as will be fully detailed in [§7.3: Scripting a P2PKH](7_3_Scripting_a_P2PKH.md).
|
||||||
* OP_CHECKMULTISIG similarly checks multisigs, as will be fully detailed in [8.2: Scripting a Multisig](8_2_Creating_Multisig_Scripts.md).
|
* OP_CHECKMULTISIG similarly checks multisigs, as will be fully detailed in [§8.2: Scripting a Multisig](8_2_Creating_Multisig_Scripts.md).
|
||||||
* OP_CHECKLOCKTIMEVERIFY and OP_SEQUENCEVERIFY form the basis of more complex Timelocks, as will be fully detailed in Chapter 9.
|
* OP_CHECKLOCKTIMEVERIFY and OP_SEQUENCEVERIFY form the basis of more complex Timelocks, as will be fully detailed in Chapter 9.
|
||||||
* OP_RETURN is the mark of an unspendable transaction, which is why it's used to carry data.
|
* OP_RETURN is the mark of an unspendable transaction, which is why it's used to carry data.
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ You may not realize it, but you've already seen these locking and unlocking scri
|
|||||||
|
|
||||||
### Create a Test Transaction
|
### Create a Test Transaction
|
||||||
|
|
||||||
This quick raw transaction grabs the first unspent transaction sitting around, and resends it to a change address, minus a transaction fee:
|
To examine real unlocking and locking scripts, create a quick raw transaction by grabbing the first unspent transaction sitting around, and resending it to a change address, minus a transaction fee:
|
||||||
```
|
```
|
||||||
$ utxo_txid=$(bitcoin-cli listunspent | jq -r '.[0] | .txid')
|
$ utxo_txid=$(bitcoin-cli listunspent | jq -r '.[0] | .txid')
|
||||||
$ utxo_vout=$(bitcoin-cli listunspent | jq -r '.[0] | .vout')
|
$ utxo_vout=$(bitcoin-cli listunspent | jq -r '.[0] | .vout')
|
||||||
@ -31,11 +31,11 @@ $ recipient=$(bitcoin-cli getrawchangeaddress)
|
|||||||
$ rawtxhex=$(bitcoin-cli -named createrawtransaction inputs='''[ { "txid": "'$utxo_txid'", "vout": '$utxo_vout' } ]''' outputs='''{ "'$recipient'": 1.2985 }''')
|
$ rawtxhex=$(bitcoin-cli -named createrawtransaction inputs='''[ { "txid": "'$utxo_txid'", "vout": '$utxo_vout' } ]''' outputs='''{ "'$recipient'": 1.2985 }''')
|
||||||
$ signedtx=$(bitcoin-cli -named signrawtransaction hexstring=$rawtxhex | jq -r '.hex')
|
$ signedtx=$(bitcoin-cli -named signrawtransaction hexstring=$rawtxhex | jq -r '.hex')
|
||||||
```
|
```
|
||||||
We're creating it and signing it but not sending it: the goal is simply produce a complete transaction that we can examine.
|
You don't actually need to send it: the goal is simply to produce a complete transaction that you can examine.
|
||||||
|
|
||||||
### Examing Your Test Transaction
|
### Examine Your Test Transaction
|
||||||
|
|
||||||
You can now examine your transaction in depth by examining the `$signedtx`:
|
You can now examine your transaction in depth by using `decoderawtransaction` on the `$signedtx`:
|
||||||
```
|
```
|
||||||
$ bitcoin-cli -named decoderawtransaction hexstring=$signedtx
|
$ bitcoin-cli -named decoderawtransaction hexstring=$signedtx
|
||||||
{
|
{
|
||||||
@ -75,37 +75,33 @@ $ bitcoin-cli -named decoderawtransaction hexstring=$signedtx
|
|||||||
```
|
```
|
||||||
The two scripts are found in the two different parts of the transaction.
|
The two scripts are found in the two different parts of the transaction.
|
||||||
|
|
||||||
The `scriptSig` is located in the `vin`. This is the _unlocking_ script. It's what's run to unlock the UTXO being used to fund this transaction. There will be one `scriptSig` per UTXO in a transaction.
|
The `scriptSig` is located in the `vin`. This is the _unlocking_ script. It's what's run to access the UTXO being used to fund this transaction. There will be one `scriptSig` per UTXO in a transaction.
|
||||||
|
|
||||||
The `scriptPubKey` is located in the `vout`. This is the _locking_ script. It's what locks the new output from the transaction. There will be one `scriptPubKey` per output in a transaction.
|
The `scriptPubKey` is located in the `vout`. This is the _locking_ script. It's what locks the new output from the transaction. There will be one `scriptPubKey` per output in a transaction.
|
||||||
|
|
||||||
To be precise: the `scriptSig` of this transaction will unlock the previous UTXO; this new transaction's output will then be locked with the `scriptPubKey`, which can in turn be unlocked by the `scriptSig` of the transaction that reuses that UTXO.
|
_How do the scriptSig and scriptPubKey interact?_ The `scriptSig` of a transaction unlocks the previous UTXO; this new transaction's output will then be locked with a `scriptPubKey`, which can in turn be unlocked by the `scriptSig` of the transaction that reuses that UTXO.
|
||||||
|
|
||||||
### Read The Scripts in Your Transaction
|
### Read The Scripts in Your Transaction
|
||||||
|
|
||||||
Note that each of these scripts includes two different representations: the `hex` is what actually gets stored, but the more readable assembly language (`asm`) can sort of show you what's going on.
|
Look at the two scripts and you'll see that each includes two different representations: the `hex` is what actually gets stored, but the more readable assembly language (`asm`) can sort of show you what's going on.
|
||||||
|
|
||||||
So, here's your first look at what Bitcoin Scripting looks like.
|
Take a look at the `asm` of the unlocking script and you'll get your first look at what Bitcoin Scripting looks like:
|
||||||
|
|
||||||
The unlocking script is:
|
|
||||||
```
|
```
|
||||||
"3045022100c4ef5b531061a184404e84ab46beee94e51e8ae15ce98d2f3e10ae7774772ffd02203c546c399c4dc1d6eea692f73bb3fff490ea2e98fe300ac6a11840c7d52b6166[ALL] 0319cd3f2485e3d47552617b03c693b7f92916ac374644e22b07420c8812501cfb"
|
"3045022100c4ef5b531061a184404e84ab46beee94e51e8ae15ce98d2f3e10ae7774772ffd02203c546c399c4dc1d6eea692f73bb3fff490ea2e98fe300ac6a11840c7d52b6166[ALL] 0319cd3f2485e3d47552617b03c693b7f92916ac374644e22b07420c8812501cfb"
|
||||||
```
|
```
|
||||||
As it happens, that mess of numbers is a private-key signature followed by the associated public key. Or at least hopefully that's what it is, because that's what's required to unlock the P2PKH UTXO that this transaction is using.
|
As it happens, that mess of numbers is a private-key signature followed by the associated public key. Or at least hopefully that's what it is, because that's what's required to unlock the P2PKH UTXO that this transaction is using.
|
||||||
|
|
||||||
The locking script is:
|
Read the locking script and you'll see it's a lot more obvious:
|
||||||
```
|
```
|
||||||
OP_DUP OP_HASH160 371c20fb2e9899338ce5e99908e64fd30b789313 OP_EQUALVERIFY OP_CHECKSIG
|
OP_DUP OP_HASH160 371c20fb2e9899338ce5e99908e64fd30b789313 OP_EQUALVERIFY OP_CHECKSIG
|
||||||
```
|
```
|
||||||
That in turn is the standard method in Bitcoin Script for locking a P2PKH transaction.
|
That is the standard method in Bitcoin Script for locking a P2PKH transaction.
|
||||||
|
|
||||||
[7.3: Scripting a Pay to Public Key Hash](7_3_Scripting_a_Pay_to_Public_Key_Hash.md) will explain how these two scripts go together, but first you will need to know how Bitcoin Scripts are evaluated.
|
[§7.3](7_3_Scripting_a_P2PKH.md) will explain how these two scripts go together, but first you will need to know how Bitcoin Scripts are evaluated.
|
||||||
|
|
||||||
## Examine a Different Sort of Transaction
|
## Examine a Different Sort of Transaction
|
||||||
|
|
||||||
Before we leave this foundation behind, however, we're going to look at a different type of locking script.
|
Before we leave this foundation behind, however, we're going to look at a different type of locking script. Here's the `scriptPubKey` from the multisig transaction that you created in [§6.1: Sending a Transaction with a Multisig](6_1_Sending_a_Transaction_to_a_Multisig.md).
|
||||||
|
|
||||||
Here's the `scriptPubKey` from the multisig transaction that you created in §6.2:
|
|
||||||
```
|
```
|
||||||
"scriptPubKey": {
|
"scriptPubKey": {
|
||||||
"asm": "OP_HASH160 babf9063cee8ab6e9334f95f6d4e9148d0e551c2 OP_EQUAL",
|
"asm": "OP_HASH160 babf9063cee8ab6e9334f95f6d4e9148d0e551c2 OP_EQUAL",
|
||||||
@ -137,4 +133,4 @@ These two transactions are _definitely_ locked in different ways. Bitcoin recogn
|
|||||||
|
|
||||||
Every Bitcoin transaction includes at least one unlocking script (`scriptSig`), which solves a previous cryptographic puzzle, and at least one locking script (`scriptPubKey`), which creates a new cryptographic puzzle. There's one `scriptSig` per input and one `scriptPubKey` per output. Each of these scripts is written in Bitcoin Script, a Forth-like language that further empowers Bitcoin.
|
Every Bitcoin transaction includes at least one unlocking script (`scriptSig`), which solves a previous cryptographic puzzle, and at least one locking script (`scriptPubKey`), which creates a new cryptographic puzzle. There's one `scriptSig` per input and one `scriptPubKey` per output. Each of these scripts is written in Bitcoin Script, a Forth-like language that further empowers Bitcoin.
|
||||||
|
|
||||||
_What is the power of scripts?_ Scripts unlock the full power of Smart Contracts. With the appropriate opcodes, you can make very precise decisions about who can redeem funds, when they can redeem funds, and how they can redeem funds. More intricate rules for corporate spending, partnership spending, proxy spending, and other methodologies can all be encoded within a Script, and that Script can be used as the redemption condition for specific funds.
|
_What is the power of scripts?_ Scripts unlock the full power of Smart Contracts. With the appropriate opcodes, you can make very precise decisions about who can redeem funds, when they can redeem funds, and how they can redeem funds. More intricate rules for corporate spending, partnership spending, proxy spending, and other methodologies can also be encoded within a Script. It even empowers more complex Bitcoin services such as Lightning and sidechains.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user