分享
golang[43]-blockchain-serialize
jonson_jackson · · 1113 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
真实比特币序列化
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"bytes"
"encoding/binary"
"log"
"fmt"
"encoding/hex"
"crypto/sha256"
)
//将类型转化为了字节数组
func IntToHex(num int32) []byte{
buff := new(bytes.Buffer)
//binary.LittleEndian 小端模式
err:= binary.Write(buff,binary.LittleEndian,num)
if err!=nil{
log.Panic(err)
}
return buff.Bytes()
}
//字节反转
func ReverseBytes4(data []byte){
for i,j :=0,len(data) - 1;i<j;i,j = i+1,j - 1{
data[i],data[j] = data[j],data[i]
}
}
func main(){
//版本号
var version int32 = 2
fmt.Printf("%x\n",IntToHex(version))
//前一个区块的hash
prev,_ := hex.DecodeString("000000000000000016145aa12fa7e81a304c38aec3d7c5208f1d33b587f966a6")
ReverseBytes4(prev)
fmt.Printf("%x\n",prev)
//默克尔根
merkleroot,_ := hex.DecodeString("3a4f410269fcc4c7885770bc8841ce6781f15dd304ae5d2770fc93a21dbd70d7")
ReverseBytes4(merkleroot)
fmt.Printf("%x\n",merkleroot)
//时间
var time int32 = 1418755780
fmt.Printf("%x\n",IntToHex(time))
//难度
var bits int32 = 404454260
fmt.Printf("%x\n",IntToHex(bits))
//随机数
var nonce int32 = 1865996595
fmt.Printf("%x\n",IntToHex(nonce))
//拼接
result := bytes.Join([][]byte{IntToHex(version),prev,merkleroot,IntToHex(time),IntToHex(bits),IntToHex(nonce)},[]byte{})
fmt.Printf("%x\n",result)
//double hash256
firsthash := sha256.Sum256(result)
resulthash:= sha256.Sum256(firsthash[:])
ReverseBytes4(resulthash[:])
fmt.Printf("%x",resulthash)
}
本文链接: https://dreamerjonson.com/2018/12/12/golang-43-blockchain-serialize/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信1113 次点击
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
真实比特币序列化
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"bytes"
"encoding/binary"
"log"
"fmt"
"encoding/hex"
"crypto/sha256"
)
//将类型转化为了字节数组
func IntToHex(num int32) []byte{
buff := new(bytes.Buffer)
//binary.LittleEndian 小端模式
err:= binary.Write(buff,binary.LittleEndian,num)
if err!=nil{
log.Panic(err)
}
return buff.Bytes()
}
//字节反转
func ReverseBytes4(data []byte){
for i,j :=0,len(data) - 1;i<j;i,j = i+1,j - 1{
data[i],data[j] = data[j],data[i]
}
}
func main(){
//版本号
var version int32 = 2
fmt.Printf("%x\n",IntToHex(version))
//前一个区块的hash
prev,_ := hex.DecodeString("000000000000000016145aa12fa7e81a304c38aec3d7c5208f1d33b587f966a6")
ReverseBytes4(prev)
fmt.Printf("%x\n",prev)
//默克尔根
merkleroot,_ := hex.DecodeString("3a4f410269fcc4c7885770bc8841ce6781f15dd304ae5d2770fc93a21dbd70d7")
ReverseBytes4(merkleroot)
fmt.Printf("%x\n",merkleroot)
//时间
var time int32 = 1418755780
fmt.Printf("%x\n",IntToHex(time))
//难度
var bits int32 = 404454260
fmt.Printf("%x\n",IntToHex(bits))
//随机数
var nonce int32 = 1865996595
fmt.Printf("%x\n",IntToHex(nonce))
//拼接
result := bytes.Join([][]byte{IntToHex(version),prev,merkleroot,IntToHex(time),IntToHex(bits),IntToHex(nonce)},[]byte{})
fmt.Printf("%x\n",result)
//double hash256
firsthash := sha256.Sum256(result)
resulthash:= sha256.Sum256(firsthash[:])
ReverseBytes4(resulthash[:])
fmt.Printf("%x",resulthash)
}
本文链接: https://dreamerjonson.com/2018/12/12/golang-43-blockchain-serialize/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!