go语言实现交易的基本构思

package main

import "bytes"

const subsidy=10//奖励,矿工挖掘给与的奖励
type TXInput struct {
    Txid []byte //Txid存储了交易的id
    Vout int //vout 则保存该交易的中一个output索引
    ScriptSig string  //ScriptSig 仅只是保存了一个任意用户定义的钱包
}
func(intput*TXInput)CanUnlockOutPutWith(un)
}


type TXOutput struct {
    Value int    //output保存了“币”(上面的value)
    ScriptPubkey string//   用脚本语言意味着比特币可以也作为智能合约平台
}
type Transaction struct {
    ID[]byte
    Vin[]TXInput
    Vout[]TXOutput
}
//检查交易失误是否为coinbase
func( tx *Transaction )IsCoinBase()bool{
    return len(tx.Vin)==1&&len(tx.Vin[0].Txid)==0 && tx.Vin[0].Vout==-1

}
//设置交易ID,从二进制数据中
func( tx *Transaction)Set ID(){
    var encoded bytes.Buffer//开辟内存
}   var hash[32]byte//哈希数组
    enc:=gob.NewEncoder(&enncoded)//解码对象
    err:=enc.Encode(tx)//解码
    if err!=nil{
        log.Panic(err)
    }
    hash=sha256.Sum256(encoded.Bytes())//计算哈希
    tx.ID=hash[:]//设置ID`这里写代码片`

猜你喜欢

转载自blog.csdn.net/weixin_43004357/article/details/81810946