Bytom交易說明(UTXO使用者自己管理模式)

2021-08-26 08:40:40 字數 4102 閱讀 5442

比原專案倉庫:

該部分主要針對使用者自己管理私鑰和位址,並通過utxo來構建和傳送交易。

注意事項:

以下步驟以及功能改造僅供參考,具體**實現需要使用者根據實際情況進行除錯,具體可以參考單元測試案例**blockchain/txbuilder/txbuilder_test.go#l255

該部分功能可以參考**crypto/ed25519/chainkd/util.go#l11,可以通過newxkeys(nil)建立主私鑰和主公鑰

func newxkeys(r io.reader) (xprv xprv, xpub xpub, err error) 

return xprv, xprv.xpub(), nil

}

接收物件包含兩種形式:address形式和program形式,兩者是一一對應的,任選其一即可。其中建立單簽位址參考**account/accounts.go#l267進行相應改造為:

func (m *manager) createp2pkh(xpub chainkd.xpub) (*ctrlprogram, error) 

control, err := vmutil.p2wpkhprogram(byte(pubhash))

if err != nil

return &ctrlprogram, nil

}

建立多簽位址參考**account/accounts.go#l294進行相應改造為:

func (m *manager) createp2sh(xpubs chainkd.xpub) (*ctrlprogram, error) 

scripthash := crypto.sha256(signscript)

// todo: pass different params due to config

address, err := common.newaddresswitnessscripthash(scripthash, &consensus.activenetparams)

if err != nil

control, err := vmutil.p2wshprogram(scripthash)

if err != nil

return &ctrlprogram, nil

}

找到可花費的utxo,其實就是找到接收位址或接收program是你自己的unspend_output。其中utxo的結構為:(參考**account/reserve.go#l39)

// utxo describes an individual account utxo.

type utxo struct

上述這些utxo的字段資訊可以從get-block介面返回結果的transaction中找到,其相關的結構體如下:(參考**api/block_retrieve.go#l26)

// blocktx is the tx struct for getblock func

type blocktx struct

//annotatedoutput means an annotated transaction output.

type annotatedoutput struct

utxo跟get-block返回結果的字段對應關係如下:

`sourceid`       - `json:"mux_id"`

`assetid` - `json:"asset_id"`

`amount` - `json:"amount"`

`sourcepos` - `json:"position"`

`controlprogram` - `json:"control_program"`

`address` - `json:"address,omitempty"`

通過utxo構造交易就是使用spendaccountunspent_output的方式來花費指定的utxo。

第一步,通過utxo構造交易輸入txinput和簽名需要的資料資訊signinginstruction,該部分功能可以參考**account/builder.go#l169進行相應改造為:

// utxotoinputs convert an utxo to the txinput

func utxotoinputs(xpubs chainkd.xpub, u *utxo) (*types.txinput, *txbuilder.signinginstruction, error)

if u.address == ""

address, err := common.decodeaddress(u.address, &consensus.activenetparams)

if err != nil

switch address.(type)

default:

return

nil, nil, errors.new("unsupport address type")

}return txinput, siginst, nil

}

第二步,通過utxo構造交易輸出txoutput該部分功能可以參考**protocol/bc/types/txoutput.go#l20:

// newtxoutput create a new output struct

func newtxoutput(assetid bc.assetid, amount uint64, controlprogram byte) *txoutput ,

vmversion: 1,

controlprogram: controlprogram,},}

}

通過上面已經生成的交易資訊構造交易txbuilder.template,該部分功能可以參考blockchain/txbuilder/builder.go#l92進行改造為:

type inputandsiginst struct 

// build build transactions with template

func buildtx(inputs inputandsiginst, outputs *types.txoutput) (*template, *types.txdata, error)

tx := &types.txdata{}

// add all the built outputs.

// add all the built inputs and their corresponding signing instructions.

for _, in := range inputs }}

tpl.transaction = types.newtx(*tx)

return tpl, tx, nil

}

賬戶模型是根據密碼找到對應的私鑰對交易進行簽名,這裡使用者可以直接使用私鑰對交易進行簽名,可以參考簽名**blockchain/txbuilder/txbuilder.go#l82進行改造為:(以下改造僅支援單簽交易,多簽交易使用者可以參照該示例進行改造)

// sign will try to sign all the witness

func sign(tpl *template, xprv chainkd.xprv) error ,}}

return materializewitnesses(tpl)

}

該步驟無需更改任何內容,直接參照wiki中提交交易的apisubmit-transaction的功能即可

位元幣UTXO交易模型

因為位元幣網路有個天然的問題 分叉問題,總是可能出現多個位元幣礦工挖出合法區塊,然後在鏈上產生分叉。在這個時候位元幣有一條規則 多條分叉鏈出現時,最長鏈 積累工作量最多的鏈 是有效的。如果你挖出一條比現有的誠實鏈更長的攻擊鏈,那大家就會承認你的攻擊鏈是有效的,誠實鏈會被作廢。這個時候就會產生雙花問題...

Bytom的鏈式交易和花費未確認的交易

當我們基於比原做應用的時候,在構建交易過程中會遇到以下兩種情況。多個位址向乙個位址轉賬,還有一種就是從乙個位址分批次向多個位址轉賬。那我們今天就來介紹一下這兩種交易構建的具體流程,以及貼出具體實現的 當我們從多個錢包位址一次性轉到乙個位址的時候,為了提高使用者體驗。我們可以選擇鏈式交易,把多筆交易一...

Bit Z 關於交易隱藏及下線說明

bit z 致力於為全球使用者提供最優質的綜合性數字資產交易服務。支援所有主流的數字資產發展路線,並將選擇權交給市場和社群使用者。bit z專案評審中心將對所有上線幣種從專案進展 社群熱度 持幣使用者 漲跌幅 交易量等多方面評估,對活躍度不足的幣種交易對進行隱藏 隱藏的交易對需在幣幣交易中主動搜尋才...