LikeCoin
WebsiteDiscord GitHubSupport Us
English
English
  • Decentralize Publishing
  • Liker Land FAQ
  • 📕DePub
    • Decentralized Publishing
    • Liker Land Affiliation Program
      • Stripe Connect Payout Account Status
    • FAQ: Listing ebooks
    • List ebook
      • List PDF
    • Manage ebooks
      • Manage Book Collection
      • Latest Books
      • Bulk create QR codes and Short URLs
      • NFT Authz Grants
      • ebooks Replenishment
      • Modify ebook
      • Burn ebook
    • Purchase ebooks
      • Gift an ebook
      • Read ebooks
      • Import ebook to ereaders
        • Import ebook to Kobo
      • Other ebook Platforms
        • Terminus Press
        • Dung Foo Kei
        • BookForest
    • Register on Liker Land
      • Resetting password
      • Securing your Liker ID
      • Manage logged in devices
      • Edit Avatar and Name
      • Export Wallet Seed Words
      • What is Authcore?
    • Liker Land features
      • My Bookshelf
      • Notifications
      • Follow Creators
      • NFT Details
    • Transfer NFT
      • Batch send NFT to supporters
    • NFT Tools
    • Writing NFT FAQ
    • Publish Writing NFT
      • Email/Social
      • Keplr
      • Cosmostation
      • Cosmostation app
      • Matters
      • Liker.Social
      • illustBuy
      • Publish NFT with ISCN ID
    • Collect Writing NFT
      • Collect Writing NFT by LIKE on desktop
      • Collect Writing NFT by LIKE on mobile
      • One-Click Collect All Writing NFT
      • NFT Widget
      • Dynamic Pricing
      • NFT Marketplace
    • ISCN
      • What is the relationship between ISCN and IPFS?
      • What's the point of decentralization to the content ecosystem?
      • What type of content is supported by ISCN?
      • How to register an ISCN?
        • Email/Social
        • Keplr
        • Cosmostation
        • Cosmostation app
        • Matters
      • Register ISCN in batch
      • ISCN Browser
  • 🧭General Guides
    • On the News
      • Interviews and Features
      • Activities and Events
      • Mentions
      • Awards and Press Release
      • Recommendations
    • Community
      • #Community Call
      • #open-a-ticket
      • #faucet-testnet
      • #translate-wallet-prefix
      • LikeCoin Discord wallet
      • LIKE this message
    • Faucet
    • Wallet
      • LIKE pay
      • Keplr Browser Extension Wallet
        • Install Keplr and create a new wallet
          • Creating a new Keplr wallet via setting up a new recovery phrase
        • Keplr: Deposit and Send LikeCoin
        • Keplr + dao.like.co: Deposit, send, delegate, governance
        • Import wallet into Keplr
      • Keplr Mobile Wallet
        • How to install Keplr Mobile
        • Keplr Mobile: Deposit and Send LikeCoin
        • Import wallet into Keplr Mobile
      • Cosmostation Browser Extension Wallet
        • How to install Cosmostation Browser Extension
        • Cosmostation: Deposit and Send LikeCoin
        • Cosmostation + dao.like.co: Deposit, send, delegate, governance
        • Import wallet into Cosmostation
      • Cosmostation Mobile Wallet
        • How to install Cosmostation app
        • Cosmostation app: Deposit and Send LikeCoin
        • Import wallet into Cosmostation app
      • Leap Browser Extension Wallet
        • How to install Leap Browser Extension
        • Leap: Deposit and Send LikeCoin
        • Import wallet into Leap
      • Multisig
      • Wallet Address
      • Block Explorer
        • dao.like.co
        • Ping.pub
        • ATOMScan
      • Transaction Fee
      • New LikeCoin address prefix "like" - FAQ
      • Migrating from ERC-20
        • Upgrade LikeCoin ERC-20 to LikeCoin chain
        • LikeCoin Migration FAQ
    • Trade
      • Buy LikeCoin
      • Trade in Osmosis
      • Registering on DigiFinex
      • Trade in DigiFinex
      • Trade in MyEthShop
      • OTC
    • Liquidity
      • Osmosis
    • Stake
      • Delegation of LikeCoin
        • Delegate on desktop
        • Delegate on mobile
      • Undelegation of LikeCoin
        • Undelegate on desktop
        • Undelegate on mobile
      • Redelegation of LikeCoin
        • Redelegate on desktop
        • Redelegate on mobile
      • Where comes the LikeCoin rewards?
    • Governance
      • Community Call
      • LikeCoin chain
      • Liquid Democracy
      • Proposal Deposit
      • Direct Voting
        • Vote on desktop
        • Vote on mobile
      • Validator
        • Community Delegation
        • Validator Report
      • Proposals
    • DApp
      • LikeCoin app
        • Download LikeCoin app
        • Like - Reinventing the Like
        • Super Like - Tweeting to the Space
      • Liker ID
        • Register a Liker ID with Keplr
        • Connecting to my social account
      • Civic Liker
        • How to be a Civic Liker
        • Civic Liker Settings
        • FAQ
        • Creators Fund
        • Rationale and benefits
        • ​Civic Liker Web3 Report
      • Web3Press
      • LikeCoin button
        • Matters
        • Self-Host
          • ghost
          • Hexo
          • Hugo
          • Jekyll
          • mdBook
          • October CMS
          • Others
        • Blogging Platforms
          • inmediahk.net
          • illustBuy
          • Tipga
          • Blink
          • Blogspot
          • SteemCN
          • Wix / Weebly
          • Write.as
      • Content Creator Tools
        • Rewards
        • Monthly Achievement
        • Creators Fund Report
        • Reward Release Time
        • Cannot get reward?
      • Liker.Social
      • depub.space
        • Share tweet from From LikeCoin Discord to depub.space
        • Avatar for depub.space
      • Glossary
    • Branding
      • Design Materials
  • 🔬Developer Guide
    • Developer Introduction
    • LikeCoin chain API
      • Cosmos concepts
      • RPC/LCD API
        • ISCN API
        • NFT API
      • Setup local test node
      • Sample
        • Javascript
        • CosmJS (Recommended)
          • Keplr
          • ISCN
    • LikeCoin API
      • Calling API
      • Authetication
      • Reference
    • LikeCoin button
      • Widget & Iframe
      • Javascript CDN
      • Javascript SDK
    • LIKE pay
      • Web Widget
        • Reference
        • Webhook
    • ISCN
      • Specification
      • Chain API
      • JavaScript
        • iscn-js
        • iscn-message-types
      • Web Widget
        • ISCN Widget
          • ISCN Widget Reference
        • ISCN x Arweave Widget
          • ISCN-AR Widget Reference
      • Batch upload tool
      • ISCN Badge
      • ISCN IPFS plugin
    • LikeCoin NFT
      • LikeCoin NFT Module Spec
      • Metadata best practice
      • Writing NFT Spec
      • NFT Book Spec
      • NFT Portal
        • NFT Portal Reference
      • API Reference
    • likecoin-js
    • Ecosystem Statistics Badge
    • Raise Proposal
  • 👥Validator Guide
    • Technical Introduction
    • LikeCoin chain node
      • Setup a node (mainnet / public testnet)
        • Node Security
        • Setup on docker (Deprecated)
        • Setup on cloud platforms
        • Syncing from genesis
      • Node configuration
      • Command line interface
        • Useful commands
    • Become a validator
    • ChungKing++ Upgrade
    • Previous upgrades
      • ChungKing+ Upgrade
      • ChungKing Upgrade
      • StarFerry Upgrade
      • LaiChiKok Upgrade
      • FoTan Upgrade
        • Overview
        • Software Changes Overview
        • For other full node operators
        • Miscellaneous
    • Legacy endpoints
Powered by GitBook
On this page
  • Encode and Sign Raw Message
  • Commit a Transaction
  • Verify Transaction Signature
Export as PDF
  1. Developer Guide
  2. LikeCoin chain API
  3. Sample

Javascript

Using Javascript/node.js to sign a transaction

Encode and Sign Raw Message

The parameters should consist with your account information. The accountNumber and sequence could be found by querying LCD /cosmos/auth/v1beta1/accounts/{fromAddress} endpoint.

const secp256k1 = require('secp256k1');
const createHash = require('create-hash');
const Long = require('long');
const { MsgSend } = require("./build/codec/cosmos/bank/v1beta1/tx");
const { TxBody, AuthInfo, SignDoc, TxRaw } = require("./build/codec/cosmos/tx/v1beta1/tx");
const { PubKey } = require("./build/codec/cosmos/crypto/secp256k1/keys");

// define parameters
const chainId = "likechain-local-testnet";
const privateKey = "69b4e47d3aa61ad6184493529cd0feb0d2dfb55ea31aa9799af42607de3cd1a9";
const publicKey = "A4Fj1Y4k77Qaxuy496CHYB2rpfWXkM3LCnlyrU8eKbH7";
const accountNumber = 2;
const fromAddress = "cosmos1lsagfzrm4gz28he4wunt63sts5xzmczw8pkek3";
const toAddress = "cosmos1mnyn7x24xj6vraxeeq56dfkxa009tvhgknhm04";
const tokenAmount = 1000000;
const memo = "Enjoy your money";
const gasLimit = 100000;
const sequence = 17;

const messages = [{
  typeUrl: "/cosmos.bank.v1beta1.MsgSend",
  value: {
    fromAddress,
    toAddress,
    amount: [
      {
        denom: "nanolike",
        amount: tokenAmount.toString(),
      },
    ],
  }
}];

const wrappedMessages = messages.map(msg => {
  return {
    typeUrl: msg.typeUrl,
    value: MsgSend.encode(msg.value).finish(),
  }
})

const body = {
  typeUrl: "/cosmos.tx.v1beta1.TxBody",
  value: {
    memo,
    messages: wrappedMessages,
    timeoutHeight: Long.UZERO,
    extensionOptions: [],
    nonCriticalExtensionOptions: [],
  },
}
const bodyBytes = TxBody.encode(body.value).finish();

const pubkeyBytes = PubKey.encode({ key: publicKey }).finish();

const authInfo = {
  signerInfos: [
    {
      sequence: Long.fromNumber(sequence),
      publicKey: {
        typeUrl: "/cosmos.crypto.secp256k1.PubKey",
        value: pubkeyBytes,
      },
      modeInfo: {
        single: {
          mode: 1,
        },
      },
    },
  ],
  fee: {
    gasLimit: Long.fromNumber(gasLimit),
    payer: "",
    granter: "",
    amount: [
      {
        denom: "nanolike",
        amount: "0",
      },
    ],
  },
}

const authInfoBytes = AuthInfo.encode(authInfo).finish();
const signDoc = {
  bodyBytes,
  authInfoBytes,
  chainId,
  accountNumber: Long.fromNumber(accountNumber),
}
const signBytes = SignDoc.encode(signDoc).finish();

const privkeyBytes = Buffer.from(privateKey, 'hex')

const sign = (msg, privateKey) => {
  const msgSha256 = createHash('sha256');
  msgSha256.update(msg);
  const msgHash = msgSha256.digest();
  const { signature: signatureArr } = secp256k1.ecdsaSign(msgHash, privateKey);
  const signature = Buffer.from(signatureArr)
  return signature;
}
const signatureBytes = sign(signBytes, privkeyBytes);

const tx = {
  bodyBytes,
  authInfoBytes,
  signatures: [signatureBytes],
}
​
const txBytes = TxRaw.encode(tx).finish();
​
console.log("signature_bytes:", signatureBytes.toString('base64'));
console.log("sign_bytes:", signBytes.toString('base64'));
console.log("tx_bytes:", txBytes.toString('base64'));

// signature_bytes: zIfF132OINwGz0psHn+nxeYVQdHZXiqO/94qaXrowcRFEL2jA0qFarz22VBXYXQudOV6y0BL84/m475awNkiFA==
// sign_bytes: CqgBCpMBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEnMKLWNvc21vczFsc2FnZnpybTRnejI4aGU0d3VudDYzc3RzNXh6bWN6dzhwa2VrMxItY29zbW9zMW1ueW43eDI0eGo2dnJheGVlcTU2ZGZreGEwMDl0dmhna25obTA0GhMKCG5hbm9saWtlEgcxMDAwMDAwEhBFbmpveSB5b3VyIG1vbmV5EmcKUApGCh8vY29zbW9zLmNyeXB0by5zZWNwMjU2azEuUHViS2V5EiMKIQOBY9WOJO+0GsbsuPegh2Adq6X1l5DNywp5cq1PHimx+xIECgIIARgREhMKDQoIbmFub2xpa2USATAQoI0GGhdsaWtlY2hhaW4tbG9jYWwtdGVzdG5ldCAC
// tx_bytes: CqgBCpMBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEnMKLWNvc21vczFsc2FnZnpybTRnejI4aGU0d3VudDYzc3RzNXh6bWN6dzhwa2VrMxItY29zbW9zMW1ueW43eDI0eGo2dnJheGVlcTU2ZGZreGEwMDl0dmhna25obTA0GhMKCG5hbm9saWtlEgcxMDAwMDAwEhBFbmpveSB5b3VyIG1vbmV5EmcKUApGCh8vY29zbW9zLmNyeXB0by5zZWNwMjU2azEuUHViS2V5EiMKIQOBY9WOJO+0GsbsuPegh2Adq6X1l5DNywp5cq1PHimx+xIECgIIARgREhMKDQoIbmFub2xpa2USATAQoI0GGkDMh8XXfY4g3AbPSmwef6fF5hVB0dleKo7/3ippeujBxEUQvaMDSoVqvPbZUFdhdC505XrLQEvzj+bjvlrA2SIU

Commit a Transaction

Put the tx_bytes from above step in the JSON request body as below, and post the LCD/cosmos/tx/v1beta1/txs endpoint.

{
  "tx_bytes": "Your tx bytes here",
  "mode": "BROADCAST_MODE_SYNC"
}

Verify Transaction Signature

const secp256k1 = require('secp256k1');
const createHash = require('create-hash');

const publicKey = Buffer.from("A4Fj1Y4k77Qaxuy496CHYB2rpfWXkM3LCnlyrU8eKbH7", "base64");
const signature = Buffer.from("zIfF132OINwGz0psHn+nxeYVQdHZXiqO/94qaXrowcRFEL2jA0qFarz22VBXYXQudOV6y0BL84/m475awNkiFA==", "base64");
const signBytes = Buffer.from("CqgBCpMBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEnMKLWNvc21vczFsc2FnZnpybTRnejI4aGU0d3VudDYzc3RzNXh6bWN6dzhwa2VrMxItY29zbW9zMW1ueW43eDI0eGo2dnJheGVlcTU2ZGZreGEwMDl0dmhna25obTA0GhMKCG5hbm9saWtlEgcxMDAwMDAwEhBFbmpveSB5b3VyIG1vbmV5EmcKUApGCh8vY29zbW9zLmNyeXB0by5zZWNwMjU2azEuUHViS2V5EiMKIQOBY9WOJO+0GsbsuPegh2Adq6X1l5DNywp5cq1PHimx+xIECgIIARgREhMKDQoIbmFub2xpa2USATAQoI0GGhdsaWtlY2hhaW4tbG9jYWwtdGVzdG5ldCAC", "base64");
const msgSha256 = createHash('sha256');
msgSha256.update(signBytes);
const msgHash = msgSha256.digest();
console.log(secp256k1.ecdsaVerify(signature, msgHash, publicKey));

// true
PreviousSampleNextCosmJS (Recommended)

Last updated 3 years ago

For details of the and other chain RPC API, please refer to the .

🔬
LikeCoin chain RPC API section
POST /txs