Hedera hashgraph checksum calculation in coffee script.
hbarcalcchecksum = (accoundid, ledgerid)->
  checksum = ""
  
  # 0 = mainnet
  # 1 = stable testnet
  # 2 = previewnet
  # ledgerid = 1
  
  a = "0.0." + accoundid.toString()
  d = a.split("").map (i)-> if i == "." then 10 else Number i
  
  h = [ledgerid, 0,0,0,0,0,0]
  
  p3 = 26 * 26 * 26
  p5 = 26 * 26 * 26 * 26 * 26
  
  sd0 = d.filter((i,idx)-> (idx % 2) == 0).reduce((a,b)-> a+b) %11
  sd1 = d.filter((i,idx)-> (idx % 2) == 1).reduce((a,b)-> a+b) %11
  
  sd = d.reduce ((acc, curr) -> (acc * 31 + curr) % p3), 0
  sh = h.reduce ((acc, curr) -> (acc * 31 + curr) % p5), 0
  
  c = ((((d.length % 5) * 11 + sd0) * 11 + sd1) * p3 + sd + sh) % p5
  cp = (c * 1000003) % p5
  
  
  while cp > 0
    checksum = String.fromCharCode((cp % 26) + 97) + checksum
    cp = Math.floor(cp / 26)
  
  # Ensure checksum is 5 characters long
  checksum = 'a'.repeat(5 - checksum.length) + checksum
  checksum

In this way, the checksum can be calculated from the hbar account ID. The checksum is essential to determine shard or realm, test or production environment. It will be a requirement in the future.

Subscribe to djhugu
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.