post_message.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {
  2. deriveFeeCollectorKey,
  3. deriveWormholeBridgeDataKey,
  4. } from "@certusone/wormhole-sdk/lib/cjs/solana/wormhole";
  5. import { AnchorProvider, Wallet, web3 } from "@coral-xyz/anchor";
  6. import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
  7. // Usage:
  8. // RPC_URL="https://api.devnet.solana.com" CORE_BRIDGE_PROGRAM_ID=3u8hJUVTA4jH1wYAyUur7FFZVQ8H635K3tSHHF4ssjQ5 SOLANA_KEY="<full_path>.json" npx tsx post_message.ts
  9. (async () => {
  10. const RPC_URL = process.env.RPC_URL;
  11. if (!RPC_URL) {
  12. throw new Error("RPC_URL is required");
  13. }
  14. const CORE_BRIDGE_PROGRAM_ID = process.env.CORE_BRIDGE_PROGRAM_ID;
  15. if (!CORE_BRIDGE_PROGRAM_ID) {
  16. throw new Error("CORE_BRIDGE_PROGRAM_ID is required");
  17. }
  18. const coreBridgeAddress = new web3.PublicKey(CORE_BRIDGE_PROGRAM_ID);
  19. const connection = new web3.Connection(RPC_URL, "confirmed");
  20. const key = process.env.SOLANA_KEY;
  21. if (!key) {
  22. throw new Error("SOLANA_KEY is required");
  23. }
  24. const payer = web3.Keypair.fromSecretKey(
  25. key.endsWith(".json") ? new Uint8Array(require(key)) : bs58.decode(key)
  26. );
  27. const provider = new AnchorProvider(connection, new Wallet(payer));
  28. const acct = new web3.Keypair();
  29. const data = Buffer.from("01000000000b00000068656c6c6f20776f726c6400", "hex");
  30. const transaction = new web3.Transaction();
  31. transaction.add(
  32. web3.SystemProgram.transfer({
  33. fromPubkey: provider.publicKey,
  34. toPubkey: deriveFeeCollectorKey(coreBridgeAddress), // fee collector
  35. lamports: 100, // hardcoded for tilt in devnet_setup.sh
  36. })
  37. );
  38. transaction.add(
  39. new web3.TransactionInstruction({
  40. keys: [
  41. {
  42. // config
  43. isSigner: false,
  44. isWritable: true,
  45. pubkey: deriveWormholeBridgeDataKey(coreBridgeAddress),
  46. },
  47. {
  48. // message
  49. isSigner: true,
  50. isWritable: true,
  51. pubkey: acct.publicKey,
  52. },
  53. {
  54. // emitter
  55. isSigner: true,
  56. isWritable: false,
  57. pubkey: provider.publicKey,
  58. },
  59. {
  60. // sequence
  61. isSigner: false,
  62. isWritable: true,
  63. pubkey: web3.PublicKey.findProgramAddressSync(
  64. [Buffer.from("Sequence"), provider.publicKey.toBuffer()],
  65. coreBridgeAddress
  66. )[0],
  67. },
  68. {
  69. // payer
  70. isSigner: true,
  71. isWritable: true,
  72. pubkey: provider.publicKey,
  73. },
  74. {
  75. // fee collector
  76. isSigner: false,
  77. isWritable: true,
  78. pubkey: deriveFeeCollectorKey(coreBridgeAddress),
  79. },
  80. {
  81. // clock
  82. isSigner: false,
  83. isWritable: false,
  84. pubkey: new web3.PublicKey(
  85. "SysvarC1ock11111111111111111111111111111111"
  86. ),
  87. },
  88. {
  89. // system program
  90. isSigner: false,
  91. isWritable: false,
  92. pubkey: new web3.PublicKey("11111111111111111111111111111111"),
  93. },
  94. ],
  95. programId: coreBridgeAddress,
  96. data,
  97. })
  98. );
  99. const tx = await provider.sendAndConfirm(transaction, [acct]);
  100. console.log(tx);
  101. })();