@polkadot/types#Metadata TypeScript Examples

The following examples show how to use @polkadot/types#Metadata. 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: keyring.ts    From sdk with Apache License 2.0 6 votes vote down vote up
/**
 * sign tx with QR
 */
async function signAsync(chain: string, password: string) {
  return new Promise((resolve) => {
    const { unsignedData } = getSigner();
    const keyPair = keyring.getPair(unsignedData.data.account);
    try {
      if (!keyPair.isLocked) {
        keyPair.lock();
      }
      keyPair.decodePkcs8(password);

      let payload: any;
      if (!(<any>window).api) {
        const registry = new TypeRegistry();
        registry.setMetadata(new Metadata(registry, metaDataMap[chain]));
        payload = registry.createType("ExtrinsicPayload", unsignedData.data.data, {
          version: 4,
        });
      } else {
        payload = (<any>window).api.registry.createType("ExtrinsicPayload", unsignedData.data.data, {
          version: (<any>window).api.extrinsicVersion,
        });
      }

      const signed = payload.sign(keyPair);
      resolve(signed);
    } catch (err) {
      resolve({ error: err.message });
    }
  });
}
Example #2
Source File: keyring.ts    From sdk with Apache License 2.0 6 votes vote down vote up
/**
 * sign tx from dapp as extension
 */
async function signTxAsExtension(password: string, json: any) {
  return new Promise((resolve) => {
    const keyPair = keyring.getPair(json["address"]);
    try {
      if (!keyPair.isLocked) {
        keyPair.lock();
      }
      keyPair.decodePkcs8(password);

      let registry: any;
      if (!(<any>window).api) {
        registry = new TypeRegistry();
        registry.setMetadata(new Metadata(registry, metaDataMap["kusama"]));
      } else {
        registry = (<any>window).api.registry;
      }

      registry.setSignedExtensions(json["signedExtensions"]);
      const payload = registry.createType("ExtrinsicPayload", json, {
        version: json["version"],
      });
      const signed = payload.sign(keyPair);
      resolve(signed);
    } catch (err) {
      resolve({ error: err.message });
    }
  });
}
Example #3
Source File: helpers.ts    From gear-js with GNU General Public License v3.0 5 votes vote down vote up
calculateGas = async (
  method: string,
  api: any,
  values: UploadInitialValues['programValues'] | SendMessageInitialValues,
  alert: AlertContainerFactory,
  meta: any,
  code?: Uint8Array | null,
  addressId?: String | null,
  replyCodeError?: string
): Promise<number> => {
  const payload = getSubmitPayload(values.payload);

  try {
    if (isString(payload) && payload === '') {
      throw new Error("payload can't be empty");
    }

    if (isPlainObject(payload) && Object.keys(payload as object).length === 0) {
      throw new Error(`form can't be empty`);
    }

    const { value } = values;
    const metaOrTypeOfPayload: Metadata | string = meta || 'String';

    let estimatedGas;

    switch (method) {
      case 'init':
        estimatedGas = await api.program.gasSpent.init(
          localStorage.getItem(LOCAL_STORAGE.PUBLIC_KEY_RAW) as Hex,
          code,
          payload,
          value,
          metaOrTypeOfPayload
        );
        break;
      case 'handle':
        estimatedGas = await api.program.gasSpent.handle(
          localStorage.getItem(LOCAL_STORAGE.PUBLIC_KEY_RAW) as Hex,
          addressId,
          payload,
          value,
          metaOrTypeOfPayload
        );
        break;
      case 'reply':
        estimatedGas = await api.program.gasSpent.reply(
          localStorage.getItem(LOCAL_STORAGE.PUBLIC_KEY_RAW) as Hex,
          addressId,
          Number(replyCodeError),
          payload,
          value,
          metaOrTypeOfPayload
        );
        break;
    }

    alert.info(`Estimated gas ${estimatedGas.toHuman()}`);

    return estimatedGas.toNumber();
  } catch (error) {
    alert.error(`${error}`);

    return Promise.reject(error);
  }
}
Example #4
Source File: testHelpers.ts    From guardian with Apache License 2.0 5 votes vote down vote up
metadata = new Metadata(registry, metadataLatest)
Example #5
Source File: testHelpers.ts    From guardian with Apache License 2.0 5 votes vote down vote up
metadata = new Metadata(registry, metadataLatest)
Example #6
Source File: SubstrateService.ts    From squid with GNU General Public License v3.0 5 votes vote down vote up
async metadata(hash: Hash): Promise<MetadataLatest> {
    const metadata: Metadata = await this.apiCall((api) =>
      api.rpc.state.getMetadata(hash)
    )
    return metadata.asLatest
  }