@polkadot/util#hexToString TypeScript Examples

The following examples show how to use @polkadot/util#hexToString. 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: util.ts    From crust-apps with Apache License 2.0 7 votes vote down vote up
// recover an address from a signature JSON (as supplied by e.g. MyCrypto)
export function recoverFromJSON (signatureJson: string | null): RecoveredSignature {
  try {
    const { msg, sig } = JSON.parse(signatureJson || '{}') as Record<string, string>;

    if (!msg || !sig) {
      throw new Error('Invalid signature object');
    }

    const parts = sigToParts(sig);

    return {
      error: null,
      ethereumAddress: registry.createType('EthereumAddress', recoverAddress(hexHasPrefix(msg) ? hexToString(msg) : msg, parts)),
      signature: registry.createType('EcdsaSignature', u8aConcat(parts.signature, new Uint8Array([parts.recovery])))
    };
  } catch (error) {
    console.error(error);

    return {
      error: error as Error,
      ethereumAddress: null,
      signature: null
    };
  }
}
Example #2
Source File: utils.ts    From evm-provider.js with Apache License 2.0 7 votes vote down vote up
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
export function decodeMessage(reason: any, code: string): string {
  const reasonString = JSON.stringify(reason).toLowerCase();
  let codeString = `0x${code.substr(138)}`.replace(/0+$/, '');

  // If the codeString is an odd number of characters, add a trailing 0
  if (codeString.length % 2 === 1) {
    codeString += '0';
  }

  return `${reasonString} ${hexToString(codeString)}`;
}
Example #3
Source File: handleTxResponse.ts    From bodhi.js with Apache License 2.0 6 votes vote down vote up
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
export function decodeMessage(reason: any, code: string): string {
  const reasonString = JSON.stringify(reason).toLowerCase();
  let codeString = `0x${code.substr(138)}`.replace(/0+$/, '');

  // If the codeString is an odd number of characters, add a trailing 0
  if (codeString.length % 2 === 1) {
    codeString += '0';
  }

  return `${reasonString} ${hexToString(codeString)}`;
}
Example #4
Source File: AddressInfo.tsx    From crust-apps with Apache License 2.0 6 votes vote down vote up
function lookupLock (lookup: Record<string, string>, lockId: LockIdentifier): string {
  const lockHex = lockId.toHex();

  try {
    const lockName = hexToString(lockHex);

    return lookup[lockName] || lockName;
  } catch (error) {
    return lockHex;
  }
}
Example #5
Source File: account.ts    From sdk with Apache License 2.0 6 votes vote down vote up
/**
 * get network native token balance of an address
 */
async function getBalance(api: ApiPromise, address: string, msgChannel: string) {
  const transfrom = (res: any) => {
    const lockedBreakdown = res.lockedBreakdown.map((i: any) => {
      return {
        ...i,
        use: hexToString(i.id.toHex()),
      };
    });
    return {
      ...res,
      lockedBreakdown,
    };
  };
  if (msgChannel) {
    subscribeMessage(api.derive.balances.all, [address], msgChannel, transfrom);
    return;
  }

  const res = await api.derive.balances.all(address);
  return transfrom(res);
}
Example #6
Source File: gov.ts    From sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Query tips of treasury.
 */
async function getTreasuryTips(api: ApiPromise) {
  const tipKeys = await (api.query.tips || api.query.treasury).tips.keys();
  const tipHashes = tipKeys.map((key) => key.args[0].toHex());
  const optTips = (await (api.query.tips || api.query.treasury).tips.multi(tipHashes)) as Option<OpenTip>[];
  const tips = optTips
    .map((opt, index) => [tipHashes[index], opt.unwrapOr(null)])
    .filter((val) => !!val[1])
    .sort((a: any[], b: any[]) => a[1].closes.unwrapOr(BN_ZERO).cmp(b[1].closes.unwrapOr(BN_ZERO)));
  return Promise.all(
    tips.map(async (tip: any[]) => {
      const detail = tip[1].toJSON();
      const reason = (await (api.query.tips || api.query.treasury).reasons(detail.reason)) as Option<Bytes>;
      const tips = detail.tips.map((e: any) => ({
        address: e[0],
        value: e[1],
      }));
      return {
        hash: tip[0],
        ...detail,
        reason: reason.isSome ? hexToString(reason.unwrap().toHex()) : null,
        tips,
      };
    })
  );
}
Example #7
Source File: AddressInfo.tsx    From subscan-multisig-react with Apache License 2.0 6 votes vote down vote up
function lookupLock(lookup: Record<string, string>, lockId: LockIdentifier): string {
  const lockHex = lockId.toHex();

  try {
    const lockName = hexToString(lockHex);

    return lookup[lockName] || lockName;
  } catch (error) {
    return lockHex;
  }
}
Example #8
Source File: TipReason.tsx    From crust-apps with Apache License 2.0 5 votes vote down vote up
transformTip = {
  transform: (optBytes: Option<Bytes>) =>
    optBytes.isSome
      ? hexToString(optBytes.unwrap().toHex())
      : null
}