Offline na Transaksyon

Mag-sign Transaksyon

Upang lumikha ng isang offline na transaksyon, kailangan mong lagdaan ang transaksyon at pagkatapos kahit sino ay maaaring i-broadcast ito sa network.

Press </> button to view full source
// there are two ways you can recover the tx
// 3.a Recover Tranasction (use populate then addSignauture)
{
  let recoverTx = Transaction.populate(Message.from(realDataNeedToSign));
  recoverTx.addSignature(feePayer.publicKey, Buffer.from(feePayerSignature));
  recoverTx.addSignature(alice.publicKey, Buffer.from(aliceSignature));

  // 4. Send transaction
  console.log(
    `txhash: ${await connection.sendRawTransaction(recoverTx.serialize())}`
  );
}

// or

// 3.b. Recover Tranasction (use populate with signature)
{
  let recoverTx = Transaction.populate(Message.from(realDataNeedToSign), [
    bs58.encode(feePayerSignature),
    bs58.encode(aliceSignature),
  ]);

  // 4. Send transaction
  console.log(
    `txhash: ${await connection.sendRawTransaction(recoverTx.serialize())}`
  );
}

Bahagyang Sign Transaksyon

Kapag ang isang transaksyon ay nangangailangan ng maraming lagda, maaari mo itong bahagyang lagdaan. Ang iba pang mga pumirma ay maaaring pumirma at mai-broadcast ito sa network.

Ilang halimbawa kung kailan ito kapaki-pakinabang:

  • Magpadala ng token ng SPL bilang kapalit ng pagbabayad
  • Pumirma ng isang transaksyon upang ma-verify mo ang pagiging tunay nito
  • Tumawag sa mga pasadyang programa sa isang transaksyon na nangangailangan ng iyong lagda

Sa halimbawang ito, pinadalhan ni Bob si Alice ng isang token ng SPL bilang kapalit sa kanyang pagbabayad:

Press </> button to view full source
// 1. Add an instruction to send the token from Bob to Alice
transaction.add(
  createTransferCheckedInstruction(
    bobTokenAddress, // source
    tokenAddress, // mint
    aliceTokenAccount.address, // destination
    bobKeypair.publicKey, // owner of source account
    1 * 10 ** tokenMint.decimals, // amount to transfer
    tokenMint.decimals // decimals of token
  )
);

// 2. Bob partially signs the transaction
transaction.partialSign(bobKeypair);

// 3. Serialize the transaction without requiring all signatures
const serializedTransaction = transaction.serialize({
  requireAllSignatures: false,
});

// 4. Alice can deserialize the transaction
const recoveredTransaction = Transaction.from(
  Buffer.from(transactionBase64, "base64")
);

Matibay Nonce

Ang RecentBlockhash ay isang mahalagang halaga para sa isang transaksyon. Tatanggihan ang iyong transaksyon kung gumamit ka ng nag-expire kamakailang blockhash (pagkatapos ng 150 block). Maaari mong gamitin ang durable nonce para makakuha ng hindi nag-expire kamakailang blockhash. Upang ma-trigger ang mekanismong ito, dapat ang iyong transaksyon

  1. gumamit ng nonce na naka-store sa nonce account bilang kamakailang blockhash
  2. ilagay ang nonce advance na operasyon sa unang pagtuturo

Lumikha ng Nonce Account

Press </> button to view full source
let tx = new Transaction().add(
  // create nonce account
  SystemProgram.createAccount({
    fromPubkey: feePayer.publicKey,
    newAccountPubkey: nonceAccount.publicKey,
    lamports: await connection.getMinimumBalanceForRentExemption(
      NONCE_ACCOUNT_LENGTH
    ),
    space: NONCE_ACCOUNT_LENGTH,
    programId: SystemProgram.programId,
  }),
  // init nonce account
  SystemProgram.nonceInitialize({
    noncePubkey: nonceAccount.publicKey, // nonce account pubkey
    authorizedPubkey: nonceAccountAuth.publicKey, // nonce account authority (for advance and close)
  })
);

console.log(
  `txhash: ${await connection.sendTransaction(tx, [feePayer, nonceAccount])}`
);

Kumuha ng Nonce Account

Press </> button to view full source
let accountInfo = await connection.getAccountInfo(nonceAccountPubkey);
let nonceAccount = NonceAccount.fromAccountData(accountInfo.data);

Gumamit ng Nonce Account

Press </> button to view full source
let tx = new Transaction().add(
  // nonce advance must be the first instruction
  SystemProgram.nonceAdvance({
    noncePubkey: nonceAccountPubkey,
    authorizedPubkey: nonceAccountAuth.publicKey,
  }),
  // after that, you do what you really want to do, here we append a transfer instruction as an example.
  SystemProgram.transfer({
    fromPubkey: feePayer.publicKey,
    toPubkey: nonceAccountAuth.publicKey,
    lamports: 1,
  })
);
// assign `nonce` as recentBlockhash
tx.recentBlockhash = nonceAccount.nonce;
tx.feePayer = feePayer.publicKey;
tx.sign(
  feePayer,
  nonceAccountAuth
); /* fee payer + nonce account authority + ... */

console.log(`txhash: ${await connection.sendRawTransaction(tx.serialize())}`);
Last Updated: 6/21/2023, 2:33:55 AM
Contributors: mh