web3-utils#toChecksumAddress TypeScript Examples

The following examples show how to use web3-utils#toChecksumAddress. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: eth-like-web3-public.ts    From rubic-app with GNU General Public License v3.0 5 votes vote down vote up
static toChecksumAddress(address: string): string {
    return toChecksumAddress(address);
  }
Example #2
Source File: web3-pure.ts    From rubic-sdk with GNU General Public License v3.0 5 votes vote down vote up
/**
     * @description convert address to checksum format
     * @param address address to convert
     */
    static toChecksumAddress(address: string): string {
        return toChecksumAddress(address);
    }
Example #3
Source File: torus.ts    From torus.js with MIT License 5 votes vote down vote up
generateAddressFromPrivKey(privateKey: BN): string {
    const key = this.ec.keyFromPrivate(privateKey.toString("hex", 64), "hex");
    const publicKey = key.getPublic().encode("hex", false).slice(2);
    log.info(publicKey, "public key");
    const ethAddressLower = `0x${keccak256(Buffer.from(publicKey, "hex")).slice(64 - 38)}`;
    return toChecksumAddress(ethAddressLower);
  }
Example #4
Source File: torus.ts    From torus.js with MIT License 5 votes vote down vote up
generateAddressFromPubKey(publicKeyX: BN, publicKeyY: BN): string {
    const key = this.ec.keyFromPublic({ x: publicKeyX.toString("hex", 64), y: publicKeyY.toString("hex", 64) });
    const publicKey = key.getPublic().encode("hex", false).slice(2);
    log.info(key.getPublic().encode("hex", false), "public key");
    const ethAddressLower = `0x${keccak256(Buffer.from(publicKey, "hex")).slice(64 - 38)}`;
    return toChecksumAddress(ethAddressLower);
  }
Example #5
Source File: index.ts    From commonwealth with GNU General Public License v3.0 4 votes vote down vote up
ProfilePage: m.Component<IProfilePageAttrs, IProfilePageState> = {
  oninit: (vnode) => {
    vnode.state.account = null;
    vnode.state.tabSelected = 0;
    vnode.state.initialized = false;
    vnode.state.loaded = false;
    vnode.state.loading = false;
    vnode.state.threads = [];
    vnode.state.comments = [];
    vnode.state.refreshProfile = false;
    const chain =
      m.route.param('base') || app.customDomainId() || m.route.param('scope');
    const { address } = vnode.attrs;
    const chainInfo = app.config.chains.getById(chain);
    const baseSuffix = m.route.param('base');

    if (chainInfo?.base === ChainBase.Substrate) {
      const decodedAddress = decodeAddress(address);
      const ss58Prefix = parseInt(chainInfo.ss58Prefix, 10);

      const [valid] = checkAddress(address, ss58Prefix);
      if (!valid) {
        try {
          const encoded = encodeAddress(decodedAddress, ss58Prefix);
          navigateToSubpage(
            `/account/${encoded}${baseSuffix ? `?base=${baseSuffix}` : ''}`
          );
        } catch (e) {
          // do nothing if can't encode address
        }
      }
    } else if (chainInfo?.base === ChainBase.Ethereum) {
      const valid = checkAddressChecksum(address);

      if (!valid) {
        try {
          const checksumAddress = toChecksumAddress(address);
          navigateToSubpage(
            `/account/${checksumAddress}${
              baseSuffix ? `?base=${baseSuffix}` : ''
            }`
          );
        } catch (e) {
          // do nothing if can't get checksumAddress
        }
      }
    }
  },
  oncreate: async (vnode) => {},
  view: (vnode) => {
    const { setIdentity } = vnode.attrs;
    const { account, loaded, loading, refreshProfile } = vnode.state;
    if (!loading && !loaded) {
      loadProfile(vnode.attrs, vnode.state);
    }
    if (account && account.address !== vnode.attrs.address) {
      vnode.state.loaded = false;
      loadProfile(vnode.attrs, vnode.state);
    }
    if (loading) return m(PageLoading, { showNewProposalButton: true });
    if (!account && !vnode.state.initialized) {
      return m(PageNotFound, { message: 'Invalid address provided' });
    } else if (!account) {
      return m(PageLoading, { showNewProposalButton: true });
    }

    if (!vnode.state.allContentCount) {
      vnode.state.allContentCount = 10;
    }

    if (!vnode.state.proposalsContentCount) {
      vnode.state.proposalsContentCount = 10;
    }

    if (!vnode.state.commentsContentCount) {
      vnode.state.commentsContentCount = 10;
    }

    const { onOwnProfile, onLinkedProfile, displayBanner, currentAddressInfo } =
      getProfileStatus(account);

    if (refreshProfile) {
      loadProfile(vnode.attrs, vnode.state);
      vnode.state.refreshProfile = false;
      if (onOwnProfile) {
        setActiveAccount(account).then(() => {
          m.redraw();
        });
      } else {
        m.redraw();
      }
    }

    const onscroll = _.debounce(() => {
      const tab = vnode.state.tabSelected;
      if (tab === 0) {
        if (!postsRemaining(allContent.length, vnode.state.allContentCount))
          return;
      } else if (tab === 1) {
        if (
          !postsRemaining(proposals.length, vnode.state.proposalsContentCount)
        )
          return;
      } else {
        if (!postsRemaining(comments.length, vnode.state.commentsContentCount))
          return;
      }
      const scrollHeight = $(document).height();
      const scrollPos = $(window).height() + $(window).scrollTop();
      if (scrollPos > scrollHeight - 400) {
        if (tab === 0) {
          vnode.state.allContentCount += 20;
          const thisUrl = m.route.get();
          if (m.route.get() === thisUrl)
            window.location.hash = vnode.state.allContentCount.toString();
        } else if (tab === 1) {
          vnode.state.proposalsContentCount += 20;
          const thisUrl = m.route.get();
          if (m.route.get() === thisUrl)
            window.location.hash = vnode.state.proposalsContentCount.toString();
        } else {
          vnode.state.commentsContentCount += 20;
          const thisUrl = m.route.get();
          if (m.route.get() === thisUrl)
            window.location.hash = vnode.state.commentsContentCount.toString();
        }
        m.redraw();
      }
    }, 400);

    // TODO: search for cosmos proposals, if ChainBase is Cosmos
    const comments = vnode.state.comments.sort(
      (a, b) => +b.createdAt - +a.createdAt
    );
    const proposals = vnode.state.threads.sort(
      (a, b) => +b.createdAt - +a.createdAt
    );
    const allContent = []
      .concat(proposals || [])
      .concat(comments || [])
      .sort((a, b) => +b.createdAt - +a.createdAt);

    const allTabTitle =
      proposals && comments
        ? ['All ', m('.count', proposals.length + comments.length)]
        : 'All';
    const threadsTabTitle = proposals
      ? ['Threads ', m('.count', proposals.length)]
      : 'Threads';
    const commentsTabTitle = comments
      ? ['Comments ', m('.count', comments.length)]
      : 'Comments';

    return m(
      Sublayout,
      {
        showNewProposalButton: true,
        onscroll,
      },
      [
        m('.ProfilePage', [
          displayBanner &&
            m(ProfileBanner, {
              account,
              addressInfo: currentAddressInfo,
            }),
          m('.row.row-narrow.forum-row', [
            m('.col-xs-12 .col-md-8', [
              m(ProfileHeader, {
                account,
                setIdentity,
                onOwnProfile,
                onLinkedProfile,
                refreshCallback: () => {
                  vnode.state.refreshProfile = true;
                },
              }),
              m(Tabs, [
                {
                  name: allTabTitle,
                  onclick: () => {
                    vnode.state.tabSelected = 0;
                  },
                  content: m(ProfileContent, {
                    account,
                    type: UserContent.All,
                    content: allContent,
                    count: vnode.state.allContentCount,
                    // eslint-disable-next-line max-len
                    localStorageScrollYKey: `profile-${
                      vnode.attrs.address
                    }-${m.route.param('base')}-${app.activeChainId()}-scrollY`,
                  }),
                },
                {
                  name: threadsTabTitle,
                  onclick: () => {
                    vnode.state.tabSelected = 1;
                  },
                  content: m(ProfileContent, {
                    account,
                    type: UserContent.Threads,
                    content: proposals,
                    count: vnode.state.proposalsContentCount,
                    // eslint-disable-next-line max-len
                    localStorageScrollYKey: `profile-${
                      vnode.attrs.address
                    }-${m.route.param('base')}-${app.activeChainId()}-scrollY`,
                  }),
                },
                {
                  name: commentsTabTitle,
                  onclick: () => {
                    vnode.state.tabSelected = 2;
                  },
                  content: m(ProfileContent, {
                    account,
                    type: UserContent.Comments,
                    content: comments,
                    count: vnode.state.commentsContentCount,
                    // eslint-disable-next-line max-len
                    localStorageScrollYKey: `profile-${
                      vnode.attrs.address
                    }-${m.route.param('base')}-${app.activeChainId()}-scrollY`,
                  }),
                },
              ]),
            ]),
            m('.xs-display-none .col-md-4', [
              m(ProfileBio, {
                account,
                setIdentity,
                onOwnProfile,
                onLinkedProfile,
                refreshCallback: () => {
                  vnode.state.refreshProfile = true;
                },
              }),
            ]),
          ]),
        ]),
      ]
    );
  },
}