波卡的交易構(gòu)建與簽名操作指南


交易格式
Address:發(fā)送賬戶的SS58編碼地址。 Block Hash:檢查點(diǎn)區(qū)塊的哈希。 Block Number:檢查點(diǎn)塊的編號(hào)。 Genesis Hash:鏈的創(chuàng)世哈希。 Metadata:提交時(shí)Runtime的SCALE編碼元數(shù)據(jù)。 Nonce:本次交易的隨機(jī)數(shù)。 Spec Version:運(yùn)行時(shí)的當(dāng)前規(guī)范版本。 Transaction Version:交易格式的當(dāng)前版本。 Tip:可選,增加交易優(yōu)先級(jí)的提示。 Era Period:可選,交易有效的檢查點(diǎn)之后的區(qū)塊數(shù)。如果為零,則交易是永久的(immortal)。
dest: 目的地地址 #[compact] value:通證數(shù)(緊湊編碼compact encoding)
1
序列化交易格式
緊隨其后的SCALE編碼字節(jié)的緊湊編碼數(shù)。 1 位:如果沒有簽名則為0,如果有則為1。 7 位:外部版本,它等于十進(jìn)制的4。 4 個(gè)字節(jié):Runtime的規(guī)范版本。 4 個(gè)字節(jié):Runtime的交易版本。 32 字節(jié):鏈的創(chuàng)世哈希。 32 字節(jié):作為era參考的區(qū)塊哈希。如果交易是永久的,那么這將是創(chuàng)世哈希。
一個(gè)用sp_runtime::MultiAddress::Id指示交易的簽名者的編碼的SCALE。 一個(gè)用sp_runtime::MultiSignature::{SigningScheme}簽名(波卡支持sr25519、ed25519和ECDSA作為簽名方案)的編碼的SCALE。
如果交易是永久的,那么Era就是0。 否則,它將是一個(gè)Vec[u64, u64]包括時(shí)期和階段的時(shí)期。 用隨機(jī)數(shù)緊湊編碼的u32。 用支付給區(qū)塊生產(chǎn)者的小費(fèi)(tip)進(jìn)行緊湊編碼的u128。 一個(gè)用sp_runtime::traits::SignedExtension>與此交易相關(guān)的附加數(shù)據(jù)和邏輯編碼的SCALE 。
1 個(gè)字節(jié):交易調(diào)用的pallet索引。 1 個(gè)字節(jié):交易正在調(diào)用的pallet中的函數(shù)。 變量:被調(diào)用函數(shù)所需的SCALE編碼參數(shù)。
2
總結(jié)
構(gòu)造一個(gè)未簽名的交易。 創(chuàng)建簽名的有效載荷(payload)。 簽署有效載荷。 將簽名的有效載荷序列化為交易。 提交序列化交易。 Parity提供以下工具來幫助執(zhí)行這些步驟。
Polkadot-JS Tools
yarn run:signer <submit|sendOffline> --account <from-account-ss58> --ws <endpoint> <module.method> [param1] [...] [paramX]yarn run:signer sign --account <from-account-ss58> --seed <seed> --type <sr25519|ed25519> <payload>yarn run:signer sign --account 121X5bEgTZcGQx5NZjwuTjqqKoiG8B2wEAvrUFjuw24ZGZf2 --seed "pulp gaze fuel ... mercy inherit equal" --type sr25519 0x040300ff4a83f1...a8239139ff3ff7c3f6yarn run:signer sign --account 121X5bEgTZcGQx5NZjwuTjqqKoiG8B2wEAvrUFjuw24ZGZf2 --seed "pulp gaze fuel ... mercy inherit equal" --type sr25519 0x040300ff4a83f1...a8239139ff3ff7c3f6Tx Wrappers
1
導(dǎo)入私鑰
import { importPrivateKey } from '@substrate/txwrapper-polkadot';const keypair = importPrivateKey(“pulp gaze fuel ... mercy inherit equal”);
2
從公鑰導(dǎo)出地址
import { deriveAddress } from '@substrate/txwrapper-polkadot';// Public key, can be either hex string, or Uint8Arrayconst publicKey = “0x2ca17d26ca376087dc30ed52deb74bf0f64aca96fe78b05ec3e720a72adb1235”;const address = deriveAddress(publicKey);
3
離線構(gòu)建一筆交易
歸檔節(jié)點(diǎn)不會(huì)刪減任何區(qū)塊或狀態(tài)數(shù)據(jù)。使用--pruning archive標(biāo)注。某些類型的節(jié)點(diǎn)(如驗(yàn)證者)必須以歸檔模式運(yùn)行。
import { methods } from "@substrate/txwrapper-polkadot";const unsigned = methods.balances.transferKeepAlive({dest: "15vrtLsCQFG3qRYUcaEeeEih4JwepocNJHkpsrqojqnZPc2y",value: 5000000000,},{address: "121X5bEgTZcGQx5NZjwuTjqqKoiG8B2wEAvrUFjuw24ZGZf2",blockHash: "0x1fc7493f3c1e9ac758a183839906475f8363aafb1b1d3e910fe16fab4ae1b582",blockNumber: 4302222,genesisHash: "0xe3777fa922cafbff200cadeaea1a76bd7898ad5b89f7848999058b50e715f636",// must import from client RPC call state_getMetadatanonce: 2,specVersion: 1019,tip: 0,eraPeriod: 64, // number of blocks from checkpoint that transaction is validtransactionVersion: 1,},{metadataRpc,// Type registry});
4
構(gòu)建簽名有效載荷
import { methods, createSigningPayload } from '@substrate/txwrapper-polkadot';// See "Construct a transaction offline" for "{...}"const unsigned = methods.balances.transferKeepAlive({...}, {...}, {...});const signingPayload = createSigningPayload(unsigned, { registry });
5
序列化已簽名的交易
Parity Polkadot客戶端實(shí)現(xiàn)了一個(gè)Polkadot主機(jī)和一個(gè)本地Runtime。Runtime必須編譯為WebAssembly并存儲(chǔ)在鏈上。如果客戶端的Runtime規(guī)范與存儲(chǔ)在鏈上的Runtime規(guī)范相同,則客戶端將使用客戶端二進(jìn)制文件執(zhí)行區(qū)塊。
import { createSignedTx } from "@substrate/txwrapper-polkadot";// Example code, replace `signWithAlice` with actual remote signer.// An example is given here:// https://github.com/paritytech/txwrapper-core/blob/b213cabf50f18f0fe710817072a81596e1a53cae/packages/txwrapper-core/src/test-helpers/signWithAlice.tsconst signature = await signWithAlice(signingPayload);const signedTx = createSignedTx(unsigned, signature, { metadataRpc, registry });
6
解碼有效載荷類型
您可能希望在提交之前解碼有效載荷(payload)以驗(yàn)證其內(nèi)容。
import { decode } from "@substrate/txwrapper-polkadot";// Decode an unsigned txconst txInfo = decode(unsigned, { metadataRpc, registry });// Decode a signing payloadconst txInfo = decode(signingPayload, { metadataRpc, registry });// Decode a signed txconst txInfo = decode(signedTx, { metadataRpc, registry });
7
檢查交易的哈希
import { getTxHash } from ‘/txwrapper-polkadot’;const txHash = getTxHash(signedTx);
提交已簽名的有效荷載
簽名者CLI(yarn run:signer submit --tx --ws ) Substrate API Sidecar 帶有author_submitExtrinsic或author_submitAndWatchExtrinsic的RPC,后者將使您訂閱事件event,以便在交易得到驗(yàn)證并包含在鏈中時(shí)得到通知。
備注
subkey --network polkadot generateSecret phrase `pulp gaze fuel ... mercy inherit equal` is account:Secret seed: 0x57450b3e09ba4598 ... ... ... ... ... ... ... .. 219756eeba80bb16Public key (hex): 0x2ca17d26ca376087dc30ed52deb74bf0f64aca96fe78b05ec3e720a72adb1235Account ID: 0x2ca17d26ca376087dc30ed52deb74bf0f64aca96fe78b05ec3e720a72adb1235SS58 Address: 121X5bEgTZcGQx5NZjwuTjqqKoiG8B2wEAvrUFjuw24ZGZf2subkey --network polkadot generateSecret phrase `exercise auction soft ... obey control easily` is account:Secret seed: 0x5f4bbb9fbb69261a ... ... ... ... ... ... ... .. 4691ed7d1130fbbdPublic key (hex): 0xda04de6cd781c98acf0693dfb97c11011938ad22fcc476ed0089ac5aec3fe243Account ID: 0xda04de6cd781c98acf0693dfb97c11011938ad22fcc476ed0089ac5aec3fe243SS58 Address: 15vrtLsCQFG3qRYUcaEeeEih4JwepocNJHkpsrqojqnZPc2y
如果需要更好的查看代碼,請(qǐng)參見原文,相關(guān)地址:
https://wiki.polkadot.network/docs/build-transaction-construction#tx-wrapper
波卡網(wǎng)絡(luò)蓬勃發(fā)展,生態(tài)項(xiàng)目層出不窮,波卡生態(tài)研究院聚焦波卡生態(tài)動(dòng)向,把握當(dāng)前趨勢(shì)?;貜?fù)【日?qǐng)?bào)】,獲取波卡生態(tài)每日最新消息匯總。
歡迎大家加入我們Polkadot生態(tài)研究院的電報(bào):
https://t.me/polkadot_eri
Staking | Gavin Wood | 跨鏈 | 平行鏈 | 國庫 | Parity
KSM | Web3.0 | Kusama | Grant指南
Statemint | 區(qū)塊鏈 | SBP計(jì)劃 | NFT
XCVM | BEEFY | XCM
Wasm
評(píng)論
圖片
表情
