Ch7. Advanced Transactions and Scrip Ting: Myongji University
Ch7. Advanced Transactions and Scrip Ting: Myongji University
Myongji University
Introduction
In this chapter we will look at more advanced scripting and how
we can use it to build transactions with complex conditions
Then, we will examine new script operators that add a time dime
nsion to bitcoin, through timelocks
2
multi-signature - introduction
multi-signature scripts set a condition where N public keys are re
corded in the script and at least M of those must provide signatu
res to unlock the funds.
3
multi-signature – Locking script
The general form of a locking script setting an M-of-N multi-sign
ature condition is:
M <Public Key 1> <Public Key 2> ... <Public Key N> N CHECKMULTISIG
where N is the total number of listed public keys and M is the thr
eshold of required signatures to spend the output.
4
multi-signature – Unlocking script
The preceding locking script can be satisfied with an unlocking sc
ript containing any combination of two signatures from the privat
e keys corresponding to the three listed public keys. :
The two scripts together would form the combined validation scri
pt:
5
multi-signature – Validation
When executed, if the unlocking script matches the conditions set
by the locking script, this combined script will evaluate to TRUE.
6
multi-signature – A bug in CHECKMULTISIG(1)
When CHECKMULTISIG executes, it should consume M+N+2 item
s on the stack as parameters.
7
multi-signature – A bug in CHECKMULTISIG(2)
First, CHECKMULTISIG pops the top item, which is N.
Then it pops N items, which are the public keys that can sign.
In this example, public keys A, B, and C.
Then, it pops one item, which is M(how many signatures are nee
ded).
8
multi-signature – A bug in CHECKMULTISIG(3)
However, a bug in the implementation causes CHECKMULTISIG to
pop one more item (M+1 total) than it should.
9
multi-signature – A bug in CHECKMULTISIG(5)
10
multi-signature – A bug in CHECKMULTISIG(4)
11
Pay-to-Script-Hash (P2SH) – Introduction(1)
12
Pay-to-Script-Hash (P2SH) – Introduction(2)
13
Pay-to-Script-Hash (P2SH) – Redeem Script (1)
14
Pay-to-Script-Hash (P2SH) – Redeem Script (2)
This shifts the burden in fees and complexity from the sender to t
he recipient (spender) of the transaction.
15
Pay-to-Script-Hash (P2SH) – Example(1)
16
Pay-to-Script-Hash (P2SH) – Example (2)
17
Pay-to-Script-Hash (P2SH) – Example (3)
18
P2SH Addresses – Introduction(1)
P2SH addresses use the version prefix “5,” which results in Base58
Check-encoded addresses that start with a “3.”
Base58Check Encoding
19
P2SH Addresses – Introduction(2)
20
P2SH Addresses - Benefits
21
P2SH Addresses - Loss
The redeem script is not presented to the network until you atte
mpt to spend a P2SH output.
The network will accept the P2SH locking script even if it corresp
onds to an invalid redeem script, because it is one of the standar
d types and the script hash gives no indication of the script it rep
resents.
22
Data Recording Output (RETURN) - Introduction
The use of bitcoin’s blockchain to store data unrelated to bitcoin pay
ments is a controversial subject.
Such transactions create UTXO that cannot be spent, using the destin
ation bitcoin address as a freeform 20-byte field.
These transactions that can never be spent are therefore never remov
ed from the UTXO set and cause the size of the UTXO database to for
ever increase, or “bloat.”
23
Data Recording Output (RETURN) – RETURN(1)
24
Data Recording Output (RETURN) – RETURN(2)
RETURN <data>
25
Data Recording Output (RETURN) – RETURN(3)
26
Timelocks
Timelocks are restrictions on transactions or outputs that only all
ow spending after a point in time.
27
Transaction Locktime(nLocktime)
Transaction locktime defines the earliest time that a transaction is
valid and can be relayed on the network or added to the blockch
ain.
0(Zero)
– in most transaction to indicate immediate propagation and execution
.
1 ~ 500 million
– It is interpreted as a block height.
28
Transaction Locktime Limitation
nLocktime has the limitation that while it makes it possible to spe
nd some outputs in the future.
But, it does not make it impossible to spend them until that time.
– If Alice signs a transaction spending one of her outputs to Bob’s add
ress, and sets the transaction nLocktime to 3 months in the future.
– Bob cannot transmit the transaction to redeem the funds until 3 mon
ths have elapsed.
– But, Alice can create another transaction. Thus, Alice can spend the s
ame UTXO before the 3 months have elapsed.
It means, the only guarantee is that Bob will not be able to redee
m it before 3 months have elapsed. There is no guarantee that B
ob will get the funds.
29
Check Lock Time Verify(CLTV)
CLTV is a per-output timelock, rather than a per-transaction timel
ock as is the case with nLocktime.
CLTV restricts specific UTXO such that they can only be spent in f
uture transaction with nLocktime set to a greater or equal value.
30
CLTV example
If Alice is paying Bob’s address, the output would contain P2PKH
script like below:
– DUP HASH160 <Bob's Public Key Hash> EQUALVERIFY CHECKSIG
31
CLTV example(2)
<expiry time> CHECKLOCKTIMEVERIFY DROP DUP HASH160 <Bob's Publ
ic Key Hash> EQUALVERIFY CHECKSIG
When Bob tries to spend this UTXO, he constructs a transaction that refe
rences the UTXO as an input with his signature and public key.
32
CLTV example(3)
33
CLTV example(4)
CHECKLOCKTIMEVERIFY fails and halts execution, marking the transactio
n invalid if
– the stack is empty; or
– the top item on the stack is less than 0; or
– the lock-time type (height versus timestamp) of the top stack item a
nd the nLocktime field are not the same; or
– the top stack item is greater than the transaction’s nLocktime field; o
r
– the nSequence field of the input is 0xffffffff.
Alice locked the UTXO itself, it is now impossible for either Bob a
nd Alice to spend it before the 3 month locktime has expired.
34
Relative Timelocks
nLocktime and CLTV are both absolute timelock in that they spe
cify an absolute point in time.
35
Relative Timelocks with nSequence
original meaning of nSequence
nSequence
is transaction-level relative timelock and can be set in
every transaction input.
36
nSequence as a consensus-enforced relative timelock
Since the activation of BIP-68, new consensus rules apply for any transactio
n containing an input whose nSequence value is less than (bit 1<<31 is n
ot set).
If the most significant (bit 1<<31) is not set, it is a flag that means “relativ
e locktime.”
– Transaction inputs with nSequence values less than are interpreted as having a
relative timelock.
– Such a transaction is only valid once the input has aged by the relative timelock
amount.
Otherwise (bit 1<<31 set), the nSequence value is reserved for other uses
such as enabling CHECKLOCKTIMEVERIFY, nLocktime, Opt-In-Replace-By-Fe
e, and other future developments.
A transaction can include both timelocked inputs (nSequence < ) and input
s without a relative timelock (nSequence >= ).
37
nSequence as a consensus-enforced relative timelock(2)
38
nSequence as a consensus-enforced relative timelock(3)
39
Relative Timelock with CSV
There is a script opcode for relative timelocks that leverages the
nSequence value in scripts.
Relative timelocks with CSV are especially useful when several (ch
ained) transactions are created and signed, but not propagated,
when they’re kept “off-chain.”
40
Median-Time-Past
In bitcoin there is a subtle, but very significant, difference between wall
time and consensus time.
– each participant has his or her own perspective of time.
41
Timelock Defense Against Fee Sniping
Fee-sniping is a theoretical attack scenario.
– where miners attempting to rewrite past blocks “snipe” higher-fee tran
sactions from future blocks to maximize their profitability.
42
Timelock Defense Against Fee Sniping(2)
Today, reward is much higher than total fees per block. But trans
action fees will be the majority of the reward (or even the entiret
y of the reward).
43
Scripts with Flow Control – Conditional Clauses(1)
Bitcoin implements flow control using the IF, ELSE, ENDIF, and N
OTIF opcodes. Additionally, conditional expressions can contain b
oolean operators such as BOOLAND, BOOLOR, and NOT.
44
Scripts with Flow Control – Conditional Clauses(2)
Because Bitcoin Script is a stack language, you may find the bitco
in’s flow control scripts confusing first.
45
Scripts with Flow Control – VERIFY
46
Scripts with Flow Control – Example(1)
47
Scripts with Flow Control – Example(2)
The script with IF does the same thing as using an opcode with a
VERIFY suffix
48
Scripts with Flow Control – Example(3)
If, however, we want to have more than one execution path (flow
control), then we need an IF...ELSE flow control clause.
49
Scripts with Flow Control – Script(1)
50
Scripts with Flow Control – Script(2)
The condition is not part of the redeem script. Instead, the condit
ion will be offered in the unlocking script, allowing Alice and Bob
to “choose” which execution path they want.
<Alice's Sig> 1
The 1 at the end serves as the condition (TRUE) that will make th
e IF clause execute the first redemption path for which Alice has
a signature.
51
Scripts with Flow Control – Script(3)
For Bob to redeem this, he would have to choose the second exe
cution path by giving a FALSE value to the IF clause:
<Bob's Sig> 0
52
Scripts with Flow Control – Complex Script Example (1)
The participants in the multisig scheme are Mohammed, his two partners Sae
ed and Zaira, and their company lawyer Abdul.
The three partners make decisions based on a majority rule, so two of the thr
ee must agree.
However, in the case of a problem with their keys, they want their lawyer to b
e able to recover the funds with one of the three partner signatures.
Finally, if all partners are unavailable or incapacitated for a while, they want th
e lawyer to be able to manage the account directly.
53
Scripts with Flow Control – Complex Script Example (2)
The participants in the multisig scheme are Mohammed, his two partners Sae
ed and Zaira, and their company lawyer Abdul.
The three partners make decisions based on a majority rule, so two of the thr
ee must agree.
However, in the case of a problem with their keys, they want their lawyer to b
e able to recover the funds with one of the three partner signatures.
Finally, if all partners are unavailable or incapacitated for a while, they want th
e lawyer to be able to manage the account directly.
54
Scripts with Flow Control – Complex Script Example (2)
55
Scripts with Flow Control – Complex Script Example (3)
This execution path consists of lines 3 and 9. Line 3 sets the quor
um of the multisig to 2 (2-of-3).
56
Scripts with Flow Control – Complex Script Example (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
0 <Mohammed's Sig> <Zaira's Sig> TRUE TRUE
57
Scripts with Flow Control – Complex Script Example (3)
The second execution path can only be used after 30 days have e
lapsed from the creation of the UTXO.
This is achieved by line 5 ~ 7, which sets the quorum for the mul
tisig to 1. To select this execution path, the unlocking script woul
d end in FALSE TRUE:
58
Scripts with Flow Control – Complex Script Example (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
0 <Saeed's Sig> <Abdul's Sig> FALSE TRUE
59
Scripts with Flow Control – Complex Script Example (3)
Finally, the third execution path allows Abdul the lawyer to spend
the funds alone, but only after 90 days.
60
Scripts with Flow Control – Complex Script Example (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
<Abdul's Sig> FALSE
61
END