区块链学习笔记3

交易 - Transaction

一点点手写笔记

image
image

一个真实的交易

示例链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{
"ver":1,
// List of inputs 数组
"inputs":[
{
"sequence":4294967295,
"witness":"",
"prev_out":{
"spent":true,
"tx_index":283891696,
"type":0,
// Sender 发送地址
"addr":"1GEpdQKniomEWNBT57i2Gakx3LbocdPbBu",
// Amount 83252 Satoshis
"value":83252,
"n":0,
"script":"76a914a7270d42995925700c222f8490d04beca9b8136388ac"
},
"script":"473044022050d8b9012569a81937e95922d0d731d9962da37ff68b3a91bad459c11a92e263022031981eec3e698cb078a69a09997c9384702524b251d574c413ff1847d6c498b60121039840b9ff142da071cd3fdb94fbf3460b0b6b8b5a7dad836a5586ee192ffbe520"
}
],
"weight":756,
"block_height":485786,
"relayed_by":"0.0.0.0",
// List of outputs 数组
"out":[
{
"spent":true,
"tx_index":283891999,
"type":0,
// Receiver 接受地址
"addr":"3BX3ev7igW8pzFV4GrkkAvaNv6wv5de4ns",
"value":49992,
// input index
"n":0,
//This scriptsig is a combination of the sender’s (后面还会说到multisig) signature and their public key
"script":"a9146bce8af68361b1fbf6bd6cc2c08acf1705a10e0887"
}
],
// Lock Time 之后我们要介绍
"lock_time":0,
"size":189,
"double_spend":false,
// Receive Time
"time":1505676139,
"tx_index":283891999,
// In counter, input的数量
"vin_sz":1,
// Transaction ID
"hash":"e646284afca18a67980ba96cd3b891476fe182ef9235878296ac18091659440e",
// Out counter, output的数量
"vout_sz":1
}
Lock Time

The locktime indicates the earliest time a transaction can be added to the blockchain.

想象一个实际生活中的场景: Alice给Bob一个支票,支票上写Bob只有到某个日期才能使用支票提款。在比特币中,我们则用Lock Time来实现这个Post Date

除此之外,Locktime给支付者了一定的反悔时间。

  • 在区块链中,交易一旦发出就不可收回。
  • Alice发出了一个3天后的交易,给Bob 1个比特币。
  • 现在Alice反悔了,但是之前的消息不可撤回。
  • Alice这时候发出一个没有Locktime的交易,把这个相同的比特币支付给Alice’。
  • 这样当三天后这个给Bob的交易执行的时候会被认为是一个double spend因此会被拒绝
  • Alice成功反悔了她之前的操作。

关于Locktime的使用:

  • 如果locktime < 500 million. locktime被认为是block height (The number of blocks preceding a particular block on a block chain 就是说之后的第几个区块)。Transaction can be added to any block which has this height or higher.
  • 如果locktime >= 500 million, 那locktime被认为是一个unix format的时间 (the number of seconds elapsed since 1970-01-01T00:00 UTC—currently over 1.395 billion). The transaction can be added to any block whose block time is greater than the locktime.
Multisig

The multisig can be implemented as a m of n scheme.

简单的应用如下: Alice, Bob, Tom把他们的bitcoin都放在一起,如果要使用必须要拿到至少两个人的签名。(就是个例子,不要考虑两人狼狈为奸坑掉第三个人…hhh)

Writing messages in a transaction

应用场景: 表白(偷笑), 因为区块链是永存的…也不能删除。所以把表白信息放在区块链上后就写入历史了!看,表白墙。当然也可以用于存在性证明,比如说我拍了个照片或者其他数字信息放到区块链中去就表示自己拥有它了(就像你拥有钱币一样样的意思)。

一个方法是用OP_RETURN

  • This script allows 80 bytes of data to be added to an output
  • This creates a provably unspendable output that the network recognizes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"vout" : [
{
"value" : 0.00000000,
"n" : 0,
"scriptPubKey" : {
"asm" : "OP_RETURN 636861726c6579206c6f766573206865696469",
"hex" : "6a13636861726c6579206c6f766573206865696469",
"type" : "nulldata"
}
},
{
"value" : 0.00200000,
"n" : 1,
"scriptPubKey" : {
"asm" : "OP_DUP OP_HASH160 b8268ce4d481413c4e848ff353cd16104291c45b OP_EQUALVERIFY OP_CHECKSIG",
"hex" : "76a914b8268ce4d481413c4e848ff353cd16104291c45b88ac",
"reqSigs" : 1,
"type" : "pubkeyhash",
"addresses" : [
"1HnhWpkMHMjgt167kvgcPyurMmsCQ2WPgg"
]
}
}
],

上面的”636861726c6579206c6f766573206865696469”就是“charley loves heidi”

相关资料: