Injective | TS & dApps Docs
DocumentationInjective TS
  • Overview
  • Getting Started
    • Technical Concepts
    • Application Concepts
      • Calculations
        • Min Price Tick Size
        • Min Quantity Tick Size
      • Networks
      • CosmJs Support
    • Assets
      • Creating Tokens
      • Denom Client (deprecated)
      • Injective Lists
    • Running examples
  • Wallets
    • Getting Started
    • Accounts
    • Wallet Connections
    • Wallet Strategy
    • Offchain (Arbitrary) Data
  • Querying
    • Getting Started
    • Chain
      • Auction
      • Auth
      • Bank
      • Distribution
      • Exchange
      • Governance
      • IBC
      • Mint
      • Insurance Funds
      • Oracle
      • Peggy
      • Permissions
      • Staking
      • Tendermint
      • Wasm
      • WasmX
      • Token Factory
    • Indexer
      • Account
      • Auction
      • Derivatives
      • Explorer
      • Insurance Funds
      • Markets
      • Leaderboard
      • Mito
      • Oracle
      • Portfolio
      • Spot
      • Web3Gw Transactions
      • Streaming
        • Account
        • Auction
        • Derivatives
        • Oracle
        • Portfolio
        • Spot
        • Explorer
    • Ethereum (GraphQL)
  • Transactions
    • Getting Started
    • Cosmos
      • Ledger through Keplr Wallet
    • Ethereum
      • Ethereum Ledger
    • MsgBroadcaster
    • Private Key
    • Web3 Gateway
  • Core Modules (& examples)
    • Getting Started
    • Auction
    • AuthZ
    • Bank
    • Distribution
    • Exchange
    • Feegrant
    • Governance
    • IBC
    • Insurance
    • Peggy
    • Permissions
    • Staking
    • Tokenfactory
    • Wasm
  • Smart Contracts
    • Cosmwasm
      • Injective Name Service
      • Neptune Service
      • CW20 to Bank & Market Order in One Transaction
  • Bridges
    • Getting Started
    • Ethereum
    • IBC
    • Wormhole
  • Building Dapps
    • Getting Started
    • Configuring Nuxt
    • Configuring React
    • dApps Examples
      • Smart Contract
      • DEX
      • Bridge
      • Simple HTML example with Webpack
Powered by GitBook
On this page
  1. Wallets

Offchain (Arbitrary) Data

PreviousWallet StrategyNextGetting Started

Last updated 1 year ago

On this page, we'll provide an example of how to sign and verify arbitrary data as per the specification on Cosmos.

You can use the generateArbitrarySignDoc function from @injectivelabs/sdk-ts to generate ADR-36 compatible signDoc. You can then use it to sign/verify using a browser wallet or in a CLI environment. Make sure you are using the latest package versions.

Sign and verify using a browser wallet like Keplr


(async () => {
  const message = "Offline Sign Message Example";
  const signer = 'inj1...'
  const chainId = 'injective-1'
  
  // Sign Arbitrary Data
  const signature = await window.keplr.signArbitrary(chainId, signer, message)
  
  // Verify Arbitrary Data
  const result = await window.keplr.verifyArbitrary(chainId, signer, message, signature)
  
  if (result) {
    console.log("Signature is valid");
  }
})();

Sign and verify using PrivateKey in a CLI environment

import { config } from "dotenv";
import { PrivateKey, generateArbitrarySignDoc } from "@injectivelabs/sdk-ts";

config();

(async () => {
  const { privateKey } = PrivateKey.generate();
  const injectiveAddress = privateKey.toBech32();
  const publicKey = privateKey.toPublicKey();
  
  const message = "Offline Sign Message Example";
  const { signDocBuff } = generateArbitrarySignDoc(message, injectiveAddress);

  const signature = await privateKey.sign(signDocBuff);
  const signatureInHex = Buffer.from(signature).toString("hex");

  if (
    PrivateKey.verifyArbitrarySignature({
      signature: signatureInHex,
      signDoc: signDocBuff,
      publicKey: publicKey.toHex(),
    })
  ) {
    console.log("Signature is valid");
  }
})();
ADR-036