In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to create the UTXO user management mode of Bytom". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to create the UTXO user management mode of Bytom".
1. Create private and public keys
For this part of the function, please refer to the code crypto/ed25519/chainkd/util.go#L11, and you can create a master private key and a master public key through NewXKeys (nil).
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. Create a receiving object based on the public key
The receiving object consists of two forms: the address form and the program form, which correspond one to one, and you can choose either of them. Create a single signature address reference code account/accounts.go#L267 and modify it as follows:
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}
Create the multi-signature address reference code account/accounts.go#L294 and modify it as follows:
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. Find the utxo that can be spent
To find the affordable utxo is to find the receiving address or the receiving program is your own unspend_output. The structure of utxo is: (reference code 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}
The relevant fields related to the utxo construction deal are described as follows:
The mux_id of the previous related transaction of SourceID. According to this ID, you can locate the output of the previous transaction.
AssetID utxo's asset ID
Number of assets of Amount utxo
SourcePos the location of the utxo in the output of the previous transaction
Receiving program of ControlProgram utxo
Receive address of Address utxo
The field information of the above utxo can be found in the transaction of the result returned by the get-block API, and its related structure is as follows: (see code api/block_retrieve.go#L26)
/ / BlockTx is the tx struct for getBlock functype 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 "`}
The corresponding relationship between the fields returned by utxo and get-block is as follows:
`SourceID`-`json: "mux_id" ``AssetID` -` json: "asset_id" ``Amount`-`json: "amount" ``SourcePos`-`SourcePos`: "position" ``ControlProgram`-`json: "control_program" `Address`-`json: "address,omitempty" `4. Construct a deal through utxo
To construct a transaction through utxo is to use spend_account_unspent_output to spend the specified utxo.
The first step is to construct the transaction input TxInput and signature data information SigningInstruction through utxo. This part of the function can be modified by referring to the code account/builder.go#L169:
/ UtxoToInputs convert an utxo to the txinputfunc 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}
The second step is to construct the transaction output TxOutput through utxo. For this part of the function, please refer to the code protocol/bc/types/txoutput.go#L20:
/ / NewTxOutput create a new output structfunc 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. The input and output of the combined transaction constitute the transaction template
The transaction txbuilder.Template is constructed from the transaction information generated above. This part of the function can be modified by referring to blockchain/txbuilder/builder.go#L92 as follows:
Type InputAndSigInst struct {input * types.TxInput sigInst * SigningInstruction} / Build build transactions with templatefunc 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. Sign the constructed transaction
In the account model, the corresponding private key is found according to the password to sign the transaction. Here, users can directly use the private key to sign the transaction. You can refer to the signature code blockchain/txbuilder/txbuilder.go#L82 to modify it to: (the following modification only supports single-signature transaction, and users of multi-signature transaction can refer to this example)
/ / Sign will try to sign all the witnessfunc 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. Submit the deal online
This step does not need to change anything, but can directly refer to the function of the APIsubmit-transaction that submitted the deal in wiki.
Thank you for reading, the above is the content of "how to create Bytom UTXO user management mode". After the study of this article, I believe you have a deeper understanding of how to create Bytom UTXO user management mode, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.