該部分主要針對用戶使用bytom自帶的帳戶模式發送交易git
API接口 build-transaction,代碼api/transact.go#L120github
以標準的非BTM資產轉帳交易爲例,資產ID爲全F表示BTM資產,在該示例中BTM資產僅做爲手續費,該交易表示花費99個特定的資產到指定地址中。其中構建交易的輸入請求json格式以下:npm
{ "base_transaction": null, "actions": [ { "account_id": "0ER7MEFGG0A02", "amount": 20000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "type": "spend_account" }, { "account_id": "0ER7MEFGG0A02", "amount": 99, "asset_id": "42275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f", "type": "spend_account" }, { "amount": 99, "asset_id": "42275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f", "address": "sm1qxe4jwhkekgnxkezu7xutu5gqnnpmyc8ppq98me", "type": "control_address" } ], "ttl": 0, "time_range": 0 }
對應源代碼的請求對象以下:json
// BuildRequest is main struct when building transactions type BuildRequest struct { Tx *types.TxData `json:"base_transaction"` Actions []map[string]interface{} `json:"actions"` TTL json.Duration `json:"ttl"` TimeRange uint64 `json:"time_range"` }
結構字段說明以下:api
Tx
交易的TxData
部分,該字段爲預留字段,爲空便可TTL
構建交易的生存時間(單位爲毫秒),意味着在該時間範圍內,已經緩存的utxo不能用於再一次build交易,除非剩餘的utxo足以構建一筆新的交易,不然會報錯。當ttl
爲0時會被默認設置爲600s,即5分鐘TimeRange
時間戳,意味着該交易將在該時間戳(區塊高度)以後不會被提交上鍊,爲了防止交易在網絡中傳輸延遲而等待過久時間,若是交易沒有在特定的時間範圍內被打包,該交易便會自動失效Actions
交易的actions
結構,全部的交易都是由action構成的,map
類型的interface{}
保證了action類型的可擴展性。其中action中必須包含type字段,用於區分不一樣的action類型,action
主要包含input
和output
兩種類型,其詳細介紹以下:數組
input action
類型:緩存
output action
類型:服務器
注意事項:網絡
下面對構建交易時用到的各類action
類型進行詳細說明:ui
issueAction
結構體源代碼以下:
type issueAction struct { assets *Registry bc.AssetAmount } type AssetAmount struct { AssetId *AssetID `protobuf:"bytes,1,opt,name=asset_id,json=assetId" json:"asset_id,omitempty"` Amount uint64 `protobuf:"varint,2,opt,name=amount" json:"amount,omitempty"` }
結構字段說明以下:
assets
主要用於資產的管理,無需用戶設置參數AssetAmount
表示用戶須要發行的資產ID和對應的資產數目,這裏的AssetID
須要經過create-asset
建立,而且這裏不能使用BTM
的資產IDissueAction
的json
格式爲:
{ "amount": 100000000, "asset_id": "3152a15da72be51b330e1c0f8e1c0db669269809da4f16443ff266e07cc43680", "type": "issue" }
例如發行一筆資產的交易示例以下:
(該交易表示發行數量爲900000000
個assetID
的42275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f
的資產到接收地址sm1qxe4jwhkekgnxkezu7xutu5gqnnpmyc8ppq98me
中, 其中手續費爲20000000
neu的BTM資產)
{ "base_transaction": null, "actions": [ { "account_id": "0ER7MEFGG0A02", "amount": 20000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "type": "spend_account" }, { "amount": 900000000, "asset_id": "42275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f", "type": "issue" }, { "amount": 900000000, "asset_id": "42275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f", "address": "sm1qxe4jwhkekgnxkezu7xutu5gqnnpmyc8ppq98me", "type": "control_address" } ], "ttl": 0, "time_range": 0 }
spendAction
結構體源代碼以下:
type spendAction struct { accounts *Manager bc.AssetAmount AccountID string `json:"account_id"` ClientToken *string `json:"client_token"` } type AssetAmount struct { AssetId *AssetID `protobuf:"bytes,1,opt,name=asset_id,json=assetId" json:"asset_id,omitempty"` Amount uint64 `protobuf:"varint,2,opt,name=amount" json:"amount,omitempty"` }
結構字段說明以下:
accounts
主要用於帳戶的管理,無需用戶設置參數AccountID
表示須要花費資產的帳戶IDAssetAmount
表示花費的資產ID和對應的資產數目ClientToken
表示Reserve用戶UTXO的限制條件,目前不填或爲空便可spendAction
的json
格式爲:
{ "account_id": "0BF63M2U00A04", "amount": 2000000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "type": "spend_account" }
例如轉帳一筆資產的交易示例以下:
(該交易表示經過帳戶的方式轉帳100000000
neu的BTM資產到地址sm1qxe4jwhkekgnxkezu7xutu5gqnnpmyc8ppq98me
中, 其中手續費20000000
neu = 輸入BTM資產數量 - 輸出BTM資產數量)
{ "base_transaction": null, "actions": [ { "account_id": "0ER7MEFGG0A02", "amount": 120000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "type": "spend_account" }, { "amount": 100000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "address": "sm1qxe4jwhkekgnxkezu7xutu5gqnnpmyc8ppq98me", "type": "control_address" } ], "ttl": 0, "time_range": 0 }
spendUTXOAction
結構體源代碼以下:
type spendUTXOAction struct { accounts *Manager OutputID *bc.Hash `json:"output_id"` ClientToken *string `json:"client_token"` }
結構字段說明以下:
accounts
主要用於帳戶的管理,無需用戶設置參數OutputID
表示須要花費的UTXO的ID,能夠根據list-unspent-outputs
查詢可用的UTXO,其中OutputID
對應該API返回結果的id
字段ClientToken
表示Reserve用戶UTXO的限制條件,目前不填或爲空便可spendUTXOAction
的json
格式爲:
{ "type": "spend_account_unspent_output", "output_id": "58f29f0f85f7bd2a91088bcbe536dee41cd0642dfb1480d3a88589bdbfd642d9" }
例如經過花費UTXO的方式轉帳一筆資產的交易示例以下:
(該交易表示經過直接花費UTXO的方式轉帳100000000
neu的BTM資產到地址sm1qxe4jwhkekgnxkezu7xutu5gqnnpmyc8ppq98me
中, 其中手續費 = 輸入BTM資產的UTXO值 - 輸出BTM資產數量)
{ "base_transaction": null, "actions": [ { "output_id": "58f29f0f85f7bd2a91088bcbe536dee41cd0642dfb1480d3a88589bdbfd642d9", "type": "spend_account_unspent_output" }, { "amount": 100000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "address": "sm1qxe4jwhkekgnxkezu7xutu5gqnnpmyc8ppq98me", "type": "control_address" } ], "ttl": 0, "time_range": 0 }
controlAddressAction
結構體源代碼以下:
type controlAddressAction struct { bc.AssetAmount Address string `json:"address"` } type AssetAmount struct { AssetId *AssetID `protobuf:"bytes,1,opt,name=asset_id,json=assetId" json:"asset_id,omitempty"` Amount uint64 `protobuf:"varint,2,opt,name=amount" json:"amount,omitempty"` }
結構字段說明以下:
Address
表示接收資產的地址,能夠根據 create-account-receiver
API接口建立地址AssetAmount
表示接收的資產ID和對應的資產數目controlAddressAction
的json
格式爲:
{ "amount": 100000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "address": "bm1q50u3z8empm5ke0g3ngl2t3sqtr6sd7cepd3z68", "type": "control_address" }
例如轉帳一筆資產的交易示例以下:
(該交易表示經過帳戶的方式轉帳100000000
neu的BTM資產到地址sm1qxe4jwhkekgnxkezu7xutu5gqnnpmyc8ppq98me
中, 其中control_address
類型表示以地址做爲接收方式)
{ "base_transaction": null, "actions": [ { "account_id": "0ER7MEFGG0A02", "amount": 120000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "type": "spend_account" }, { "amount": 100000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "address": "sm1qxe4jwhkekgnxkezu7xutu5gqnnpmyc8ppq98me", "type": "control_address" } ], "ttl": 0, "time_range": 0 }
controlProgramAction
結構體源代碼以下:
type controlProgramAction struct { bc.AssetAmount Program json.HexBytes `json:"control_program"` } type AssetAmount struct { AssetId *AssetID `protobuf:"bytes,1,opt,name=asset_id,json=assetId" json:"asset_id,omitempty"` Amount uint64 `protobuf:"varint,2,opt,name=amount" json:"amount,omitempty"` }
結構字段說明以下:
Program
表示接收資產的合約腳本,能夠根據 create-account-receiver
API接口建立接收program
(返回結果的 program
和 address
是一一對應的)AssetAmount
表示接收的資產ID和對應的資產數目controlProgramAction
的json
格式爲:
{ "amount": 100000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "control_program":"0014a3f9111f3b0ee96cbd119a3ea5c60058f506fb19", "type": "control_program" }
例如轉帳一筆資產的交易示例以下:
(該交易表示經過帳戶的方式轉帳100000000
neu的BTM資產到接收program
(跟address
是一一對應的)0014a3f9111f3b0ee96cbd119a3ea5c60058f506fb19
中, 其中control_program
類型表示以program
做爲接收方式)
{ "base_transaction": null, "actions": [ { "account_id": "0ER7MEFGG0A02", "amount": 120000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "type": "spend_account" }, { "amount": 100000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "control_program": "0014a3f9111f3b0ee96cbd119a3ea5c60058f506fb19", "type": "control_program" } ], "ttl": 0, "time_range": 0 }
retireAction
結構體源代碼以下:
type retireAction struct { bc.AssetAmount } type AssetAmount struct { AssetId *AssetID `protobuf:"bytes,1,opt,name=asset_id,json=assetId" json:"asset_id,omitempty"` Amount uint64 `protobuf:"varint,2,opt,name=amount" json:"amount,omitempty"` }
結構字段說明以下:
AssetAmount
表示銷燬的資產ID和對應的資產數目retireAction
的json
格式爲:
{ "amount": 900000000, "asset_id": "3152a15da72be51b330e1c0f8e1c0db669269809da4f16443ff266e07cc43680", "type": "retire" }
例如銷燬一筆資產的交易示例以下:
(該交易表示經過帳戶的方式將100000000
neu的BTM資產銷燬, retire
表示銷燬指定數量的資產)
{ "base_transaction": null, "actions": [ { "account_id": "0ER7MEFGG0A02", "amount": 120000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "type": "spend_account" }, { "amount": 100000000, "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "type": "retire" } ], "ttl": 0, "time_range": 0 }
build-transaction
的輸入構造完成以後,即可以經過http的調用方式進行發送交易,構建交易請求成功以後返回的json結果以下:
{ "allow_additional_actions": false, "raw_transaction": "070100020161015f1190c60818b4aff485c865113c802942f29ce09088cae1b117fc4c8db2292212ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8099c4d599010001160014a86c83ee12e6d790fb388345cc2e2b87056a077301000161015fb018097c4040c8dd86d95611a13c24f90d4c9d9d06b25f5c9ed0556ac8abd73442275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f80a094a58d1d0101160014068840e56af74038571f223b1c99f1b60caaf456010003013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80bfffcb9901011600140b946646626c55a52a325c8bb48de792284d9b7200013e42275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f9d9f94a58d1d01160014c8b4391bab4923a83b955170d24ee4ca5b6ec3fb00013942275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f6301160014366b275ed9b2266b645cf1b8be51009cc3b260e100", "signing_instructions": [ { "position": 0, "witness_components": [ { "keys": [ { "derivation_path": [ "010100000000000000", "0100000000000000" ], "xpub": "de0db655c091b2838ccb6cddb675779b0a9a4204b122e61699b339867dd10eb0dbdc926882ff6dd75c099c181c60d63eab0033a4b0a4d0a8c78079e39d7ad1d8" } ], "quorum": 1, "signatures": null, "type": "raw_tx_signature" }, { "type": "data", "value": "d174db6506e35f2decb5be148c2984bfd0f6c67f043365bf642d1af387c04fd5" } ] }, { "position": 1, "witness_components": [ { "keys": [ { "derivation_path": [ "010100000000000000", "0800000000000000" ], "xpub": "de0db655c091b2838ccb6cddb675779b0a9a4204b122e61699b339867dd10eb0dbdc926882ff6dd75c099c181c60d63eab0033a4b0a4d0a8c78079e39d7ad1d8" } ], "quorum": 1, "signatures": null, "type": "raw_tx_signature" }, { "type": "data", "value": "05cdbcc705f07ad87521835bbba226ad7b430cc24e5e3f008edbe61540535419" } ] } ] }
對應響應對象的源代碼以下:
// Template represents a partially- or fully-signed transaction. type Template struct { Transaction *types.Tx `json:"raw_transaction"` SigningInstructions []*SigningInstruction `json:"signing_instructions"` // AllowAdditional affects whether Sign commits to the tx sighash or // to individual details of the tx so far. When true, signatures // commit to tx details, and new details may be added but existing // ones cannot be changed. When false, signatures commit to the tx // as a whole, and any change to the tx invalidates the signature. AllowAdditional bool `json:"allow_additional_actions"` }
結構字段說明以下:
Transaction
交易相關信息,該字段包含TxData
和bc.Tx
兩個部分:
TxData
表示給用戶展現的交易數據部分,該部分對用戶可見
Version
交易版本SerializedSize
交易序列化以後的sizeTimeRange
交易提交上鍊的最大時間戳(區塊高度)(主鏈區塊高度到達該時間戳(區塊高度)以後,若是交易沒有被提交上鍊,該交易便會失效)Inputs
交易輸入Outputs
交易輸出bc.Tx
表示系統中處理交易用到的轉換結構,該部分對用戶不可見,故不作詳細描述SigningInstructions
交易的簽名信息
Position
對input action
簽名的位置WitnessComponents
對input action
簽名須要的數據信息,其中build交易的signatures
爲null
,表示沒有簽名; 若是交易簽名成功,則該字段會存在簽名信息。該字段是一個interface接口,主要包含3種不一樣的類型:
SignatureWitness
對交易模板Template
中交易input action
位置的合約program進行哈希,而後對hash值進行簽名
signatures
(數組類型)交易的簽名,sign-transaction
執行完成以後纔會有值存在keys
(數組類型)包含主公鑰xpub
和派生路徑derivation_path
,經過它們能夠在簽名階段找到對應的派生私鑰child_xprv
,而後使用派生私鑰進行簽名quorum
帳戶key
的個數,必須和上面的keys
的長度相等。若是quorum
等於1,則表示單簽帳戶,不然爲多簽帳戶program
簽名的數據部分,program
的hash值做爲簽名數據。若是program
爲空,則會根據當前交易ID和對應action位置的InputID兩部分生成一個hash,而後把它們做爲指令數據自動構造一個program
RawTxSigWitness
對交易模板Template
的交易ID和對應input action
位置的InputID(該字段位於bc.Tx中)進行哈希,而後對hash值進行簽名
signatures
(數組類型)交易的簽名,sign-transaction
執行完成以後纔會有值存在keys
(數組類型)包含主公鑰xpub
和派生路徑derivation_path
,經過它們能夠在簽名階段找到對應的派生私鑰child_xprv
,而後使用派生私鑰進行簽名quorum
帳戶key
的個數,必須和上面的keys
的長度相等。若是quorum
等於1,則表示單簽帳戶,不然爲多簽帳戶DataWitness
該類型無需簽名,驗證合約program的附加數據AllowAdditional
是否容許交易的附加數據,若是爲true
,則交易的附加數據會添加到交易中,可是不會影響交易的執行的program
腳本,對簽名結果不會形成影響; 若是爲false
,則整個交易做爲一個總體進行簽名,任何數據的改變將影響整個交易的簽名估算手續費接口estimate-transaction-gas
是對build-transaction
的結果進行手續費的預估,估算的結果須要從新加到build-transaction
的結果中,而後對交易進行簽名和提交。其主要流程以下:
build - estimate - build - sign - submit
估算手續費的輸入請求json格式以下:
{ "transaction_template": { "allow_additional_actions": false, "raw_transaction": "070100020161015f1190c60818b4aff485c865113c802942f29ce09088cae1b117fc4c8db2292212ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8099c4d599010001160014a86c83ee12e6d790fb388345cc2e2b87056a077301000161015fb018097c4040c8dd86d95611a13c24f90d4c9d9d06b25f5c9ed0556ac8abd73442275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f80a094a58d1d0101160014068840e56af74038571f223b1c99f1b60caaf456010003013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80bfffcb9901011600140b946646626c55a52a325c8bb48de792284d9b7200013e42275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f9d9f94a58d1d01160014c8b4391bab4923a83b955170d24ee4ca5b6ec3fb00013942275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f6301160014366b275ed9b2266b645cf1b8be51009cc3b260e100", "signing_instructions": [ { "position": 0, "witness_components": [ { "keys": [ { "derivation_path": [ "010100000000000000", "0100000000000000" ], "xpub": "de0db655c091b2838ccb6cddb675779b0a9a4204b122e61699b339867dd10eb0dbdc926882ff6dd75c099c181c60d63eab0033a4b0a4d0a8c78079e39d7ad1d8" } ], "quorum": 1, "signatures": null, "type": "raw_tx_signature" }, { "type": "data", "value": "d174db6506e35f2decb5be148c2984bfd0f6c67f043365bf642d1af387c04fd5" } ] }, { "position": 1, "witness_components": [ { "keys": [ { "derivation_path": [ "010100000000000000", "0800000000000000" ], "xpub": "de0db655c091b2838ccb6cddb675779b0a9a4204b122e61699b339867dd10eb0dbdc926882ff6dd75c099c181c60d63eab0033a4b0a4d0a8c78079e39d7ad1d8" } ], "quorum": 1, "signatures": null, "type": "raw_tx_signature" }, { "type": "data", "value": "05cdbcc705f07ad87521835bbba226ad7b430cc24e5e3f008edbe61540535419" } ] } ] } }
對應響應對象的源代碼以下:
type request struct{ TxTemplate txbuilder.Template `json:"transaction_template"` } // Template represents a partially- or fully-signed transaction. type Template struct { Transaction *types.Tx `json:"raw_transaction"` SigningInstructions []*SigningInstruction `json:"signing_instructions"` // AllowAdditional affects whether Sign commits to the tx sighash or // to individual details of the tx so far. When true, signatures // commit to tx details, and new details may be added but existing // ones cannot be changed. When false, signatures commit to the tx // as a whole, and any change to the tx invalidates the signature. AllowAdditional bool `json:"allow_additional_actions"` }
其中TxTemplate
相關字段的說明見build-transaction的結果描述
調用estimate-transaction-gas
接口成功以後返回的json結果以下:
{ "total_neu": 5000000, "storage_neu": 3840000, "vm_neu": 1419000 }
對應響應對象的源代碼以下:
// EstimateTxGasResp estimate transaction consumed gas type EstimateTxGasResp struct { TotalNeu int64 `json:"total_neu"` StorageNeu int64 `json:"storage_neu"` VMNeu int64 `json:"vm_neu"` }
結構字段說明以下:
TotalNeu
預估的總手續費(單位爲neu),該值直接加到build-transaction的BTM資產輸入action中便可StorageNeu
存儲交易的手續費VMNeu
運行虛擬機的手續費API接口 sign-transaction,代碼api/hsm.go#L53
簽名交易的輸入請求json格式以下:
{ "password": "123456", "transaction": { "allow_additional_actions": false, "raw_transaction": "070100020161015f1190c60818b4aff485c865113c802942f29ce09088cae1b117fc4c8db2292212ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8099c4d599010001160014a86c83ee12e6d790fb388345cc2e2b87056a077301000161015fb018097c4040c8dd86d95611a13c24f90d4c9d9d06b25f5c9ed0556ac8abd73442275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f80a094a58d1d0101160014068840e56af74038571f223b1c99f1b60caaf456010003013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80bfffcb9901011600140b946646626c55a52a325c8bb48de792284d9b7200013e42275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f9d9f94a58d1d01160014c8b4391bab4923a83b955170d24ee4ca5b6ec3fb00013942275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f6301160014366b275ed9b2266b645cf1b8be51009cc3b260e100", "signing_instructions": [ { "position": 0, "witness_components": [ { "keys": [ { "derivation_path": [ "010100000000000000", "0100000000000000" ], "xpub": "de0db655c091b2838ccb6cddb675779b0a9a4204b122e61699b339867dd10eb0dbdc926882ff6dd75c099c181c60d63eab0033a4b0a4d0a8c78079e39d7ad1d8" } ], "quorum": 1, "signatures": null, "type": "raw_tx_signature" }, { "type": "data", "value": "d174db6506e35f2decb5be148c2984bfd0f6c67f043365bf642d1af387c04fd5" } ] }, { "position": 1, "witness_components": [ { "keys": [ { "derivation_path": [ "010100000000000000", "0800000000000000" ], "xpub": "de0db655c091b2838ccb6cddb675779b0a9a4204b122e61699b339867dd10eb0dbdc926882ff6dd75c099c181c60d63eab0033a4b0a4d0a8c78079e39d7ad1d8" } ], "quorum": 1, "signatures": null, "type": "raw_tx_signature" }, { "type": "data", "value": "05cdbcc705f07ad87521835bbba226ad7b430cc24e5e3f008edbe61540535419" } ] } ] } }
對應請求對象的源代碼以下:
type SignRequest struct { //function pseudohsmSignTemplates request Password string `json:"password"` Txs txbuilder.Template `json:"transaction"` }
結構字段說明以下:
Password
簽名的密碼,根據密碼能夠從節點服務器上解析出用戶的私鑰,而後用私鑰對交易進行簽名Txs
交易模板,build-transaction的返回結果,結構類型爲 txbuilder.Template
,相關字段的說明見build-transaction的結果描述簽名交易sign-transaction
請求成功以後返回的json結果以下:
{ "sign_complete": true, "transaction": { "allow_additional_actions": false, "raw_transaction": "070100020161015f1190c60818b4aff485c865113c802942f29ce09088cae1b117fc4c8db2292212ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8099c4d599010001160014a86c83ee12e6d790fb388345cc2e2b87056a0773630240273d5fc4fb06909fbc2968ea91c411fd20f690c88e74284ce2732052400129948538562fe432afd6cf17e590e8645b80edf80b9d9581d0a980d5f9f859e3880620d174db6506e35f2decb5be148c2984bfd0f6c67f043365bf642d1af387c04fd50161015fb018097c4040c8dd86d95611a13c24f90d4c9d9d06b25f5c9ed0556ac8abd73442275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f80a094a58d1d0101160014068840e56af74038571f223b1c99f1b60caaf4566302400cf0beefceaf9fbf1efadedeff7aee5b38ee7a25a20d78b630b01613bc2f8c9230555a6e09aaa11a82ba68c0fc9e98a47c852dfe3de851d93f9b2b7ce256f90d2005cdbcc705f07ad87521835bbba226ad7b430cc24e5e3f008edbe6154053541903013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80bfffcb9901011600140b946646626c55a52a325c8bb48de792284d9b7200013e42275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f9d9f94a58d1d01160014c8b4391bab4923a83b955170d24ee4ca5b6ec3fb00013942275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f6301160014366b275ed9b2266b645cf1b8be51009cc3b260e100", "signing_instructions": [ { "position": 0, "witness_components": [ { "keys": [ { "derivation_path": [ "010100000000000000", "0100000000000000" ], "xpub": "de0db655c091b2838ccb6cddb675779b0a9a4204b122e61699b339867dd10eb0dbdc926882ff6dd75c099c181c60d63eab0033a4b0a4d0a8c78079e39d7ad1d8" } ], "quorum": 1, "signatures": [ "273d5fc4fb06909fbc2968ea91c411fd20f690c88e74284ce2732052400129948538562fe432afd6cf17e590e8645b80edf80b9d9581d0a980d5f9f859e38806" ], "type": "raw_tx_signature" }, { "type": "data", "value": "d174db6506e35f2decb5be148c2984bfd0f6c67f043365bf642d1af387c04fd5" } ] }, { "position": 1, "witness_components": [ { "keys": [ { "derivation_path": [ "010100000000000000", "0800000000000000" ], "xpub": "de0db655c091b2838ccb6cddb675779b0a9a4204b122e61699b339867dd10eb0dbdc926882ff6dd75c099c181c60d63eab0033a4b0a4d0a8c78079e39d7ad1d8" } ], "quorum": 1, "signatures": [ "0cf0beefceaf9fbf1efadedeff7aee5b38ee7a25a20d78b630b01613bc2f8c9230555a6e09aaa11a82ba68c0fc9e98a47c852dfe3de851d93f9b2b7ce256f90d" ], "type": "raw_tx_signature" }, { "type": "data", "value": "05cdbcc705f07ad87521835bbba226ad7b430cc24e5e3f008edbe61540535419" } ] } ] } }
對應響應對象的源代碼以下:
type signResp struct { Tx *txbuilder.Template `json:"transaction"` SignComplete bool `json:"sign_complete"` }
結構字段說明以下:
Tx
簽名以後的交易模板txbuilder.Template
,若是簽名成功則signatures
會由null變成簽名的值,而raw_transaction
的長度會變長,是由於bc.Tx
部分添加了驗證簽名的參數信息SignComplete
簽名是否完成標誌,若是爲true
表示簽名完成,不然爲false
表示簽名未完成,單籤的話通常可能爲簽名密碼錯誤; 而多籤的話通常爲還須要其餘簽名。簽名失敗只需將簽名的交易數據用正確的密碼從新簽名便可,無需再次build-transaction
構建交易API接口 submit-transaction,代碼api/transact.go#L135
提交交易的輸入請求json格式以下:
{ "raw_transaction": "070100020161015f1190c60818b4aff485c865113c802942f29ce09088cae1b117fc4c8db2292212ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8099c4d599010001160014a86c83ee12e6d790fb388345cc2e2b87056a0773630240273d5fc4fb06909fbc2968ea91c411fd20f690c88e74284ce2732052400129948538562fe432afd6cf17e590e8645b80edf80b9d9581d0a980d5f9f859e3880620d174db6506e35f2decb5be148c2984bfd0f6c67f043365bf642d1af387c04fd50161015fb018097c4040c8dd86d95611a13c24f90d4c9d9d06b25f5c9ed0556ac8abd73442275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f80a094a58d1d0101160014068840e56af74038571f223b1c99f1b60caaf4566302400cf0beefceaf9fbf1efadedeff7aee5b38ee7a25a20d78b630b01613bc2f8c9230555a6e09aaa11a82ba68c0fc9e98a47c852dfe3de851d93f9b2b7ce256f90d2005cdbcc705f07ad87521835bbba226ad7b430cc24e5e3f008edbe6154053541903013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80bfffcb9901011600140b946646626c55a52a325c8bb48de792284d9b7200013e42275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f9d9f94a58d1d01160014c8b4391bab4923a83b955170d24ee4ca5b6ec3fb00013942275aacbeda1522cd41580f875c3c452daf5174b17ba062bf0ab71a568c123f6301160014366b275ed9b2266b645cf1b8be51009cc3b260e100" }
對應源代碼的請求對象以下:
type SubmitRequest struct { //function submit request Tx types.Tx `json:"raw_transaction"` }
結構字段說明以下:
Tx
簽名完成以後的交易信息。這裏須要注意該字段中的raw_transaction
不是簽名交易sign-transaction
的所有返回結果,而是簽名交易返回結果中transaction
中的raw_transaction
字段。submit-transaction
請求成功以後返回的json結果以下:
{ "tx_id": "2c0624a7d251c29d4d1ad14297c69919214e78d995affd57e73fbf84ece361cd" }
對應源代碼的響應對象以下:
type submitTxResp struct { TxID *bc.Hash `json:"tx_id"` }
結構字段說明以下:
TxID
交易ID,當交易被提交到交易池以後會顯示該信息,不然表示交易失敗