graphprotocol / support

Community support for the Hosted Service of The Graph
https://thegraph.com/
10 stars 3 forks source link

Feature request: function to checksum addresses #40

Open PaulRBerg opened 5 years ago

PaulRBerg commented 5 years ago

Do you want to request a feature or report a bug?

Request a feature.

What is the current behavior?

All addresses found in event parameters are handled as lowercase strings, contrasting the latest community interest in pushing towards the checksummed version, which provides better security.

What is the expected behavior?

Addresses are checksummed by default or there is a new function that developers can call to checksum them before saving the mutation.

habdelra commented 3 years ago

as a work around here is a function written in assembly script that can create checksummed addresses:

import { crypto, Address, ByteArray } from '@graphprotocol/graph-ts';

function toChecksumAddress(address: Address): string {
  let lowerCaseAddress = address.toHex().slice(2);
  // note that this is actually a hash of the string representation of the hex without the "0x"
  let hash = crypto
    .keccak256(ByteArray.fromUTF8(address.toHex().slice(2)))
    .toHex()
    .slice(2);
  let result = '0x';

  for (let i = 0; i < lowerCaseAddress.length; i++) {
    if (parseInt(hash.charAt(i), 16) >= 8) {
      result += toUpper(lowerCaseAddress.charAt(i));
    } else {
      result += lowerCaseAddress.charAt(i);
    }
  }

  return result;
}

// because there is no String.toUpper() in assembly script
function toUpper(str: string): string {
  let result = '';
  for (let i = 0; i < str.length; i++) {
    let charCode = str.charCodeAt(i);
    // only operate on lowercase 'a' thru lower case 'z'
    if (charCode >= 97 && charCode <= 122) {
      result += String.fromCharCode(charCode - 32);
    } else {
      result += str.charAt(i);
    }
  }
  return result;
}

(derived from the web3 code here https://github.com/ChainSafe/web3.js/blob/5d027191c5cb7ffbcd44083528bdab19b4e14744/packages/web3-utils/src/index.js#L296-L317)

sebastiantf commented 2 years ago

Is this being worked on? Would be really nice to have this built-in