成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

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

RyanQ / 1419人閱讀

摘要:比原項(xiàng)目倉庫地址地址該部分主要針對用戶自己管理私鑰和地址,并通過來構(gòu)建和發(fā)送交易。其中創(chuàng)建單簽地址參考代碼進(jìn)行相應(yīng)改造為創(chuàng)建多簽地址參考代碼進(jìn)行相應(yīng)改造為找到可花費(fèi)的找到可花費(fèi)的,其實(shí)就是找到接收地址或接收是你自己的。

比原項(xiàng)目倉庫:

Github地址:https://github.com/Bytom/bytom

Gitee地址:https://gitee.com/BytomBlockc...

該部分主要針對用戶自己管理私鑰和地址,并通過utxo來構(gòu)建和發(fā)送交易。

1.創(chuàng)建私鑰和公鑰

2.根據(jù)公鑰創(chuàng)建接收對象

3.找到可花費(fèi)的utxo

4.通過utxo構(gòu)造交易

5.組合交易的inputoutput構(gòu)成交易模板

6.對構(gòu)造的交易進(jìn)行簽名

7.提交交易上鏈

注意事項(xiàng):

以下步驟以及功能改造僅供參考,具體代碼實(shí)現(xiàn)需要用戶根據(jù)實(shí)際情況進(jìn)行調(diào)試,具體可以參考單元測試案例代碼blockchain/txbuilder/txbuilder_test.go#L255

1.創(chuàng)建私鑰和公鑰

該部分功能可以參考代碼crypto/ed25519/chainkd/util.go#L11,可以通過 NewXKeys(nil) 創(chuàng)建主私鑰和主公鑰

func NewXKeys(r io.Reader) (xprv XPrv, xpub XPub, err error) {
    xprv, err = NewXPrv(r)
    if err != nil {
        return
    }
    return xprv, xprv.XPub(), nil
}
2.根據(jù)公鑰創(chuàng)建接收對象

接收對象包含兩種形式:address形式和program形式,兩者是一一對應(yīng)的,任選其一即可。其中創(chuàng)建單簽地址參考代碼account/accounts.go#L267進(jìn)行相應(yīng)改造為:

func (m *Manager) createP2PKH(xpub chainkd.XPub) (*CtrlProgram, error) {
    pubKey := xpub.PublicKey()
    pubHash := crypto.Ripemd160(pubKey)

    // TODO: pass different params due to config
    address, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.ActiveNetParams)
    if err != nil {
        return nil, err
    }

    control, err := vmutil.P2WPKHProgram([]byte(pubHash))
    if err != nil {
        return nil, err
    }

    return &CtrlProgram{
        Address:        address.EncodeAddress(),
        ControlProgram: control,
    }, nil
}

創(chuàng)建多簽地址參考代碼account/accounts.go#L294進(jìn)行相應(yīng)改造為:

func (m *Manager) createP2SH(xpubs []chainkd.XPub) (*CtrlProgram, error) {
    derivedPKs := chainkd.XPubKeys(xpubs)
    signScript, err := vmutil.P2SPMultiSigProgram(derivedPKs, len(derivedPKs))
    if err != nil {
        return nil, err
    }
    scriptHash := crypto.Sha256(signScript)

    // TODO: pass different params due to config
    address, err := common.NewAddressWitnessScriptHash(scriptHash, &consensus.ActiveNetParams)
    if err != nil {
        return nil, err
    }

    control, err := vmutil.P2WSHProgram(scriptHash)
    if err != nil {
        return nil, err
    }

    return &CtrlProgram{
        Address:        address.EncodeAddress(),
        ControlProgram: control,
    }, nil
}
3.找到可花費(fèi)的utxo

找到可花費(fèi)的utxo,其實(shí)就是找到接收地址或接收program是你自己的unspend_output。其中utxo的結(jié)構(gòu)為:(參考代碼account/reserve.go#L39)

// UTXO describes an individual account utxo.
type UTXO struct {
    OutputID bc.Hash
    SourceID bc.Hash

    // Avoiding AssetAmount here so that new(utxo) doesn"t produce an
    // AssetAmount with a nil AssetId.
    AssetID bc.AssetID
    Amount  uint64

    SourcePos      uint64
    ControlProgram []byte

    AccountID           string
    Address             string
    ControlProgramIndex uint64
    ValidHeight         uint64
    Change              bool
}

涉及utxo構(gòu)造交易的相關(guān)字段說明如下:

SourceID 前一筆關(guān)聯(lián)交易的mux_id, 根據(jù)該ID可以定位到前一筆交易的output

AssetID utxo的資產(chǎn)ID

Amount utxo的資產(chǎn)數(shù)目

SourcePos 該utxo在前一筆交易的output的位置

ControlProgram utxo的接收program

Address utxo的接收地址

上述這些utxo的字段信息可以從get-block接口返回結(jié)果的transaction中找到,其相關(guān)的結(jié)構(gòu)體如下:(參考代碼api/block_retrieve.go#L26)

// BlockTx is the tx struct for getBlock func
type BlockTx struct {
    ID         bc.Hash                  `json:"id"`
    Version    uint64                   `json:"version"`
    Size       uint64                   `json:"size"`
    TimeRange  uint64                   `json:"time_range"`
    Inputs     []*query.AnnotatedInput  `json:"inputs"`
    Outputs    []*query.AnnotatedOutput `json:"outputs"`
    StatusFail bool                     `json:"status_fail"`
    MuxID      bc.Hash                  `json:"mux_id"`
}

//AnnotatedOutput means an annotated transaction output.
type AnnotatedOutput struct {
    Type            string             `json:"type"`
    OutputID        bc.Hash            `json:"id"`
    TransactionID   *bc.Hash           `json:"transaction_id,omitempty"`
    Position        int                `json:"position"`
    AssetID         bc.AssetID         `json:"asset_id"`
    AssetAlias      string             `json:"asset_alias,omitempty"`
    AssetDefinition *json.RawMessage   `json:"asset_definition,omitempty"`
    Amount          uint64             `json:"amount"`
    AccountID       string             `json:"account_id,omitempty"`
    AccountAlias    string             `json:"account_alias,omitempty"`
    ControlProgram  chainjson.HexBytes `json:"control_program"`
    Address         string             `json:"address,omitempty"`
}

utxo跟get-block返回結(jié)果的字段對應(yīng)關(guān)系如下:

`SourceID`       - `json:"mux_id"`
`AssetID`        - `json:"asset_id"`
`Amount`         - `json:"amount"`
`SourcePos`      - `json:"position"`
`ControlProgram` - `json:"control_program"`
`Address`        - `json:"address,omitempty"`
4.通過utxo構(gòu)造交易

通過utxo構(gòu)造交易就是使用spend_account_unspent_output的方式來花費(fèi)指定的utxo。

第一步,通過utxo構(gòu)造交易輸入TxInput和簽名需要的數(shù)據(jù)信息SigningInstruction,該部分功能可以參考代碼account/builder.go#L169進(jìn)行相應(yīng)改造為:

// UtxoToInputs convert an utxo to the txinput
func UtxoToInputs(xpubs []chainkd.XPub, u *UTXO) (*types.TxInput, *txbuilder.SigningInstruction, error) {
    txInput := types.NewSpendInput(nil, u.SourceID, u.AssetID, u.Amount, u.SourcePos, u.ControlProgram)
    sigInst := &txbuilder.SigningInstruction{}

    if u.Address == "" {
        return txInput, sigInst, nil
    }

    address, err := common.DecodeAddress(u.Address, &consensus.ActiveNetParams)
    if err != nil {
        return nil, nil, err
    }

    switch address.(type) {
    case *common.AddressWitnessPubKeyHash:
        derivedPK := xpubs[0].PublicKey()
        sigInst.WitnessComponents = append(sigInst.WitnessComponents, txbuilder.DataWitness([]byte(derivedPK)))

    case *common.AddressWitnessScriptHash:
        derivedPKs := chainkd.XPubKeys(xpubs)
        script, err := vmutil.P2SPMultiSigProgram(derivedPKs, len(derivedPKs))
        if err != nil {
            return nil, nil, err
        }
        sigInst.WitnessComponents = append(sigInst.WitnessComponents, txbuilder.DataWitness(script))

    default:
        return nil, nil, errors.New("unsupport address type")
    }

    return txInput, sigInst, nil
}

第二步,通過utxo構(gòu)造交易輸出TxOutput
該部分功能可以參考代碼protocol/bc/types/txoutput.go#L20:

// NewTxOutput create a new output struct
func NewTxOutput(assetID bc.AssetID, amount uint64, controlProgram []byte) *TxOutput {
    return &TxOutput{
        AssetVersion: 1,
        OutputCommitment: OutputCommitment{
            AssetAmount: bc.AssetAmount{
                AssetId: &assetID,
                Amount:  amount,
            },
            VMVersion:      1,
            ControlProgram: controlProgram,
        },
    }
}
5.組合交易的input和output構(gòu)成交易模板

通過上面已經(jīng)生成的交易信息構(gòu)造交易txbuilder.Template,該部分功能可以參考blockchain/txbuilder/builder.go#L92進(jìn)行改造為:

type InputAndSigInst struct {
    input *types.TxInput
    sigInst *SigningInstruction
}

// Build build transactions with template
func BuildTx(inputs []InputAndSigInst, outputs []*types.TxOutput) (*Template, *types.TxData, error) {
    tpl := &Template{}
    tx := &types.TxData{}
    // Add all the built outputs.
    tx.Outputs = append(tx.Outputs, outputs...)

    // Add all the built inputs and their corresponding signing instructions.
    for _, in := range inputs {
        // Empty signature arrays should be serialized as empty arrays, not null.
        in.sigInst.Position = uint32(len(inputs))
        if in.sigInst.WitnessComponents == nil {
            in.sigInst.WitnessComponents = []witnessComponent{}
        }
        tpl.SigningInstructions = append(tpl.SigningInstructions, in.sigInst)
        tx.Inputs = append(tx.Inputs, in.input)
    }

    tpl.Transaction = types.NewTx(*tx)
    return tpl, tx, nil
}
6.對構(gòu)造的交易進(jìn)行簽名

賬戶模型是根據(jù)密碼找到對應(yīng)的私鑰對交易進(jìn)行簽名,這里用戶可以直接使用私鑰對交易進(jìn)行簽名,可以參考簽名代碼blockchain/txbuilder/txbuilder.go#L82進(jìn)行改造為:(以下改造僅支持單簽交易,多簽交易用戶可以參照該示例進(jìn)行改造)

// Sign will try to sign all the witness
func Sign(tpl *Template, xprv chainkd.XPrv) error {
    for i, sigInst := range tpl.SigningInstructions {
        h := tpl.Hash(uint32(i)).Byte32()
        sig := xprv.Sign(h[:])
        rawTxSig := &RawTxSigWitness{
            Quorum: 1,
            Sigs:   []json.HexBytes{sig},
        }
        sigInst.WitnessComponents = append([]witnessComponent(rawTxSig), sigInst.WitnessComponents...)
    }
    return materializeWitnesses(tpl)
}
7.提交交易上鏈

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

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/24255.html

相關(guān)文章

  • Bytom設(shè)計(jì)結(jié)構(gòu)解讀

    摘要:一引文設(shè)計(jì)數(shù)據(jù)結(jié)構(gòu),組合了許多技術(shù)點(diǎn),如,,,,,,等。采用樹,其中的數(shù)據(jù)可快速證明,可以快速證明每一份狀態(tài)機(jī)是否一致。四是在狀態(tài)機(jī)的轉(zhuǎn)化過程被啟動運(yùn)行,也就是這一步驟。是指發(fā)布該資產(chǎn)時需要執(zhí)行的程序。的邏輯結(jié)構(gòu)則是用二叉樹來管理。 一、引文 設(shè)計(jì)Bytom 數(shù)據(jù)結(jié)構(gòu),組合了許多技術(shù)點(diǎn),如 patricia tree,utxo, bvm, account model,protobuf,...

    xuexiangjys 評論0 收藏0
  • 比原鏈合約入門教程

    摘要:比原項(xiàng)目倉庫地址地址一合約簡述是的一種智能合約語言,是一門聲明性謂詞語言。詳細(xì)說明請參考官方合約相關(guān)介紹。編譯合約,返回結(jié)果便是可鎖定的合約。三解鎖合約流程合約交易被區(qū)塊打包成功之后,可以查看具體的合約交易內(nèi)容,找到對應(yīng)的。 比原項(xiàng)目倉庫: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBl...

    brianway 評論0 收藏0
  • 使用Java SDK實(shí)現(xiàn)離線簽名

    摘要:當(dāng)使用構(gòu)建完成一筆交易并簽名后,若沒有全節(jié)點(diǎn)的幫助,也需要自己實(shí)現(xiàn)網(wǎng)絡(luò)協(xié)議將交易廣播到其他節(jié)點(diǎn)。其中,第一個依賴是的封裝,可用于查詢可用的以及提交交易第二個依賴用于構(gòu)建交易以及對交易進(jìn)行離線簽名。 嚴(yán)格來說,tx-signer并不屬于SDK,它是bytomd中構(gòu)建交易、對交易簽名兩大模塊的java實(shí)現(xiàn)版。因此,若想用tx-signer對交易進(jìn)行離線簽名,需要由你在本地保管好自己的私鑰。...

    dreamGong 評論0 收藏0
  • 調(diào)用Bytom Chrome插件錢包開發(fā)Dapp

    摘要:流程總結(jié)就是下載安裝插件錢包,如果自己的不需要跳過這一步。然后將編譯后的合約參數(shù)配置在的配置文件,如下圖全紅部分是測試網(wǎng)合約配置參數(shù)調(diào)用插件錢包。開發(fā)出優(yōu)秀的應(yīng)用。 安裝使用插件錢包 1. 打開Google瀏覽器的應(yīng)用商店,搜索Bystore showImg(https://segmentfault.com/img/bVbq0Ol?w=2554&h=1312); 下載鏈接:http:/...

    Mike617 評論0 收藏0
  • Derek解讀Bytom源碼-持久化存儲LevelDB

    摘要:函數(shù)總共操作有兩步從緩存中查詢值,如果查到則返回如果為從緩存中查詢到則回調(diào)回調(diào)函數(shù)。回調(diào)函數(shù)會將從磁盤上獲得到塊信息存儲到緩存中并返回該塊的信息。回調(diào)函數(shù)實(shí)際上調(diào)取的是下的,它會從磁盤中獲取信息并返回。 作者:Derek 簡介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockc......

    Eminjannn 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<