Code Examples

JavaScript/TypeScript

Send Priority Message

import { ethers } from "ethers";

async function sendPriorityMessage(content, bidAmount) {
  const provider = new ethers.JsonRpcProvider(process.env.OBSIDIAN_RPC_URL ?? "http://localhost:8545");
  const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);

  const payload = "0x" + Buffer.from(content).toString("hex");
  const chainId = 421;
  const nonce = BigInt(Date.now());
  const targetBlock = 0;
  const bid = ethers.parseEther(bidAmount);

  // Create signature
  const bidHex = bid > 0n ? ethers.toBeHex(bid) : "0x";
  const signatureHash = ethers.keccak256(
    ethers.encodeRlp([
      ethers.toBeHex(chainId),
      ethers.toBeHex(targetBlock),
      ethers.toBeHex(nonce),
      wallet.address,
      payload,
      bidHex,
    ]),
  );
  const signature = await wallet.signMessage(ethers.getBytes(signatureHash));

  // Send with bid
  const result = await provider.send("eth_sendMessageBlob", [
    {
      from: wallet.address,
      data: payload,
      signature,
      targetBlock: ethers.toBeHex(targetBlock),
      nonce: ethers.toBeHex(nonce),
      chainId: ethers.toBeHex(chainId),
      bid: ethers.toBeHex(bid),
    },
  ]);

  return result;
}

// Usage
const hash = await sendPriorityMessage("Important update!", "0.01");

Send Free Message (with VDF)

Fetch Messages

Check Buffer Status

Python

cURL

Last updated