BLC

package
v0.0.0-...-c7dd34c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2021 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base58Decode

func Base58Decode(input []byte) []byte

解码函数

Jh83

func Base58Encode

func Base58Encode(input []byte) []byte

编码函数

func CheckSum

func CheckSum(input []byte) []byte

生成校验和

func IntToHex

func IntToHex(data int64) []byte

实现int64转[]byte

func IsValidArgs

func IsValidArgs()

参数数量的检测函数

func IsValidForAddress

func IsValidForAddress(addressBytes []byte) bool

判断地址的有效性

func JSONToSlice

func JSONToSlice(jsonString string) []string

标准JSON格式转切片 windows下需要添加引号 bc.exe send -from "[\"troytan\"]" -to "[\"Alice\"]" -amount "[\"5\"]" bc.exe send -from "[\"troytan\",\"Alice\"]" -to "[\"Alice\",\"troytan\"]" -amount "[\"5\",\"2\"]" troytan ->Alice 5 -->Alice 5 troytan 5 Alice ->troytan 2 -->Alice 3 troytan 7

func PrintUsage

func PrintUsage()

用法展示

func Reverse

func Reverse(data []byte)

反转切片函数

func Ripemd160Hash

func Ripemd160Hash(pubKey []byte) []byte

实现双哈希

func StringToHash160

func StringToHash160(address string) []byte

string 转hash160

Types

type Block

type Block struct {
	TimeStamp    int64  //区块时间戳,代表区块时间
	Hash         []byte //当前区块哈希
	PreBlockHash []byte //前区块哈希
	Height       int64  //区块高度
	//Data      []byte  //交易数据
	Txs   []*Transaction //交易数据(交易列表)
	Nonce int64          //在运行pow时生成的哈希变化值,也代表pow运行时动态修改的数据
}

区块基本结构与功能管理文件

func CreateGenesisBlock

func CreateGenesisBlock(txs []*Transaction) *Block

生成创世区块

func DeserializeBlock

func DeserializeBlock(blockBytes []byte) *Block

区块数据反序列化

func NewBlock

func NewBlock(height int64, preBlockHash []byte, txs []*Transaction) *Block

新建区块

func (*Block) HashTransaction

func (block *Block) HashTransaction() []byte

把指定区块中所有交易结构都序列化(类Merkle的哈希计算方法)

func (*Block) Serialize

func (block *Block) Serialize() []byte

区块结构序列化

type BlockChain

type BlockChain struct {
	//Blocks []*Block //区块的切片
	DB *bolt.DB //数据库对象

	Tip []byte //保存最新区块的哈希值
}

区块链的基本结构

func BlockChainObject

func BlockChainObject() *BlockChain

获取一个blockchain对象

func CreateBlockChainWithGenesisBlock

func CreateBlockChainWithGenesisBlock(address string) *BlockChain

初始化区块链

func (*BlockChain) AddBlock

func (bc *BlockChain) AddBlock(txs []*Transaction)

添加区块到区块链中

func (*BlockChain) FindSpendableUTXO

func (blockchain *BlockChain) FindSpendableUTXO(from string, amount int, txs []*Transaction) (int, map[string][]int)

查找指定地址的可用UTXO,超过amount就中断查找 更新当前数据库中指定地址的UTXO数量 txs:缓存中的交易列表(用于多笔交易处理)

func (*BlockChain) FindTransaction

func (blockchain *BlockChain) FindTransaction(ID []byte) Transaction

通过指定的交易哈希查找交易

func (*BlockChain) Iterator

func (blc *BlockChain) Iterator() *BlockChainIterator

创建迭代器对象

func (*BlockChain) MineNewBlock

func (blockchain *BlockChain) MineNewBlock(from, to, amount []string)

实现挖矿功能 通过接收交易生成区块

func (*BlockChain) PrintChain

func (bc *BlockChain) PrintChain()

遍历数据库,输出所有区块信息

func (*BlockChain) SignTransaction

func (blockchain *BlockChain) SignTransaction(tx *Transaction, privKey ecdsa.PrivateKey)

交易签名

func (*BlockChain) SpentOutput

func (blockchain *BlockChain) SpentOutput(address string) map[string][]int

获取指定地址所有已花费输出

func (*BlockChain) UnUTXOS

func (blockchain *BlockChain) UnUTXOS(address string, txs []*Transaction) []*UTXO

func (*BlockChain) VerifyTransaction

func (bc *BlockChain) VerifyTransaction(tx *Transaction) bool

验证签名

type BlockChainIterator

type BlockChainIterator struct {
	DB          *bolt.DB //迭代目标
	CurrentHash []byte   //当前接待目标的哈希
}

迭代器基本结构

func (*BlockChainIterator) Next

func (bcit *BlockChainIterator) Next() *Block

实现迭代函数next,获取到每一个区块

type CLI

type CLI struct {
	BC *BlockChain //区块链对象
}

client对象

func (*CLI) CreateWallets

func (cli *CLI) CreateWallets()

创建钱包集合

func (*CLI) GetAccounts

func (cli *CLI) GetAccounts()

获取地址列表

func (*CLI) Run

func (cli *CLI) Run()

命令行运行函数

type ProofOfWork

type ProofOfWork struct {
	//需要共识验证的模块
	Block *Block
	// contains filtered or unexported fields
}

工作量证明的结构

func NewProofOfWork

func NewProofOfWork(block *Block) *ProofOfWork

创建一个POW对象

func (*ProofOfWork) Run

func (proofOfWork *ProofOfWork) Run() ([]byte, int)

执行pow,比较哈希值 返回哈希值,以及碰撞次数

type Transaction

type Transaction struct {
	//交易哈希(标识)
	TxHash []byte
	//输入列表
	Vins []*TxInput
	//输出列表
	Vouts []*TxOutput
}

定义一个交易基本结构

func NewCoinbaseTransaction

func NewCoinbaseTransaction(address string) *Transaction

实现coinbase交易

func NewSimpleTransaction

func NewSimpleTransaction(from string, to string, amount int, bc *BlockChain, txs []*Transaction) *Transaction

生成普通转账交易

func (*Transaction) Hash

func (tx *Transaction) Hash() []byte

设置用于签名的交易的哈希

func (*Transaction) HashTransaction

func (tx *Transaction) HashTransaction()

生成交易哈希(交易序列化)

func (*Transaction) IsCoinbaseTransaction

func (tx *Transaction) IsCoinbaseTransaction() bool

判断指定的交易是否是一个coinbase交易

func (*Transaction) Serialize

func (tx *Transaction) Serialize() []byte

交易序列化

func (*Transaction) Sign

func (tx *Transaction) Sign(privateKey ecdsa.PrivateKey, prevTxs map[string]Transaction)

交易签名 prevTxs : 代表当前交易的输入所引用的所有OUTPUT松鼠的交易

func (*Transaction) TrimmedCopy

func (tx *Transaction) TrimmedCopy() Transaction

交易拷贝,生成一个专门用于交易签名的脚本

func (*Transaction) Verify

func (tx *Transaction) Verify(prevTxs map[string]Transaction) bool

验证签名

type TxInput

type TxInput struct {
	//交易哈希(不是指当前交易的哈希)
	TxHash []byte
	//引用的上一笔交易的输出索引号
	Vout int
	//数字签名
	Signature []byte
	//公钥
	PublicKey []byte
}

输入结构

func (*TxInput) UnLockRipemd160Hash

func (in *TxInput) UnLockRipemd160Hash(ripemd160Hash []byte) bool

传递哈希160进行判断

type TxOutput

type TxOutput struct {
	//金额
	Value int

	//ScriptPubkey  string
	//用户名(UTXO)的所有者
	Ripemd160Hash []byte
}

输出结构

func NewTxOutput

func NewTxOutput(value int, address string) *TxOutput

新建output对象

func (*TxOutput) UnLockScriptPubkeyWithAddress

func (TxOutput *TxOutput) UnLockScriptPubkeyWithAddress(address string) bool

output身份验证

type UTXO

type UTXO struct {
	//UTXO对应的交易哈希
	TxHash []byte
	//UTXO在其所属交易的输出列表中的索引
	Index int
	//Output本身
	Output *TxOutput
}

UTXO结构管理

type Wallet

type Wallet struct {
	//1.私钥
	PrivateKey ecdsa.PrivateKey
	//2.公钥
	PublicKey []byte
}

钱包基本结构

func NewWallet

func NewWallet() *Wallet

创建一个钱包

func (*Wallet) GetAddress

func (w *Wallet) GetAddress() []byte

通过钱包(公钥)获取地址

type Wallets

type Wallets struct {
	//key : 地址
	//value : 钱包结构
	Wallets map[string]*Wallet
}

实现钱包集合的基本结构

func NewWallets

func NewWallets() *Wallets

初始化钱包集合

func (*Wallets) CreateWallet

func (wallets *Wallets) CreateWallet()

添加新的钱包到集合中

func (*Wallets) SaveWallets

func (w *Wallets) SaveWallets()

持久化钱包信息(存储到文件里)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL