diff --git a/package.json b/package.json index 19d7945d78..8b12e2dabe 100644 --- a/package.json +++ b/package.json @@ -89,12 +89,14 @@ "@polkadot/api-contract": "^10.12.4", "@polkadot/networks": "^12.6.2", "@polkadot/types": "^10.12.4", - "@polkadot/types-codec": "10.10.1", + "@polkadot/types-codec": "^10.12.4", + "@polkadot/types-known": "^10.12.4", "@polkadot/util": "^12.6.2", "@polkadot/util-crypto": "^12.6.2", "@subwallet/chain-list": "0.2.60", "@subwallet/keyring": "^0.1.5", "@subwallet/ui-keyring": "^0.1.5", + "@zondax/ledger-substrate": "^0.42.1", "babel-core": "^7.0.0-bridge.0", "babel-jest": "^29.3.1", "browserify-sign": "^4.2.2", diff --git a/packages/extension-base/package.json b/packages/extension-base/package.json index 04dc2bcdb6..2835f743ad 100644 --- a/packages/extension-base/package.json +++ b/packages/extension-base/package.json @@ -44,6 +44,7 @@ "@polkadot/rpc-provider": "^10.12.4", "@polkadot/types": "^10.12.4", "@polkadot/types-augment": "^10.12.4", + "@polkadot/types-known": "^10.12.4", "@polkadot/ui-settings": "^3.6.5", "@polkadot/util": "^12.6.2", "@polkadot/util-crypto": "^12.6.2", diff --git a/packages/extension-base/src/background/KoniTypes.ts b/packages/extension-base/src/background/KoniTypes.ts index 944152bcea..c177f7bbf0 100644 --- a/packages/extension-base/src/background/KoniTypes.ts +++ b/packages/extension-base/src/background/KoniTypes.ts @@ -22,6 +22,7 @@ import Web3 from 'web3'; import { RequestArguments, TransactionConfig } from 'web3-core'; import { JsonRpcPayload, JsonRpcResponse } from 'web3-core-helpers'; +import { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types'; import { SignerResult } from '@polkadot/types/types/extrinsic'; import { HexString } from '@polkadot/util/types'; import { KeypairType } from '@polkadot/util-crypto/types'; @@ -283,6 +284,8 @@ export interface MetadataItem { genesisHash: string; specVersion: string; hexValue: HexString; + types: Record | string>; + userExtensions?: ExtDef; } export interface CrowdloanItem { @@ -2133,6 +2136,8 @@ export interface RequestFindRawMetadata { export interface ResponseFindRawMetadata { rawMetadata: string; specVersion: number; + types: Record | string>; + userExtensions?: ExtDef; } export interface ResolveDomainRequest { diff --git a/packages/extension-base/src/koni/background/handlers/Extension.ts b/packages/extension-base/src/koni/background/handlers/Extension.ts index cd0d382ba2..534779faef 100644 --- a/packages/extension-base/src/koni/background/handlers/Extension.ts +++ b/packages/extension-base/src/koni/background/handlers/Extension.ts @@ -42,6 +42,7 @@ import { SwapPair, SwapQuoteResponse, SwapRequest, SwapRequestResult, SwapSubmit import { BN_ZERO, convertSubjectInfoToAddresses, createTransactionFromRLP, isSameAddress, reformatAddress, signatureToHex, Transaction as QrTransaction, uniqueStringArray } from '@subwallet/extension-base/utils'; import { parseContractInput, parseEvmRlp } from '@subwallet/extension-base/utils/eth/parseTransaction'; import { balanceFormatter, formatNumber } from '@subwallet/extension-base/utils/number'; +import { metadataExpand } from '@subwallet/extension-chains'; import { MetadataDef } from '@subwallet/extension-inject/types'; import { createPair } from '@subwallet/keyring'; import { KeyringPair, KeyringPair$Json, KeyringPair$Meta } from '@subwallet/keyring/types'; @@ -57,6 +58,7 @@ import { TransactionConfig } from 'web3-core'; import { SubmittableExtrinsic } from '@polkadot/api/types'; import { TypeRegistry } from '@polkadot/types'; +import { Registry } from '@polkadot/types/types'; import { assert, BN, hexStripPrefix, hexToU8a, isAscii, isHex, u8aToHex, u8aToString } from '@polkadot/util'; import { addressToEvm, base64Decode, decodeAddress, isAddress, isEthereumAddress, jsonDecrypt, keyExtractSuri, mnemonicGenerate, mnemonicValidate } from '@polkadot/util-crypto'; import { EncryptedJson, KeypairType, Prefix } from '@polkadot/util-crypto/types'; @@ -3149,7 +3151,7 @@ export default class KoniExtension { const { payload } = request; - let registry = new TypeRegistry(); + let registry: Registry; let isEvm = false; @@ -3158,14 +3160,19 @@ export default class KoniExtension { * Get the metadata for the genesisHash * @todo: need to handle case metadata store in db */ - const currentMetadata = this.#koniState.knownMetadata.find((meta: MetadataDef) => + const metadata = this.#koniState.knownMetadata.find((meta: MetadataDef) => meta.genesisHash === payload.genesisHash); - // set the registry before calling the sign function - registry.setSignedExtensions(payload.signedExtensions, currentMetadata?.userExtensions); + if (metadata) { + // we have metadata, expand it and extract the info/registry + const expanded = metadataExpand(metadata, false); - if (currentMetadata) { - registry.register(currentMetadata?.types); + registry = expanded.registry; + registry.setSignedExtensions(payload.signedExtensions, expanded.definition.userExtensions); + } else { + // we have no metadata, create a new registry + registry = new TypeRegistry(); + registry.setSignedExtensions(payload.signedExtensions); } const [, chainInfo] = this.#koniState.findNetworkKeyByGenesisHash(payload.genesisHash); @@ -3201,6 +3208,9 @@ export default class KoniExtension { if (chainInfo) { isEvm = _isChainEvmCompatible(chainInfo); } + } else { + // for non-payload, just create a registry to use + registry = new TypeRegistry(); } const result = request.sign(registry as unknown as TypeRegistry, pair); @@ -3949,11 +3959,13 @@ export default class KoniExtension { /// Metadata private async findRawMetadata ({ genesisHash }: RequestFindRawMetadata): Promise { - const { metadata, specVersion } = await this.#koniState.findMetadata(genesisHash); + const { metadata, specVersion, types, userExtensions } = await this.#koniState.findMetadata(genesisHash); return { rawMetadata: metadata, - specVersion + specVersion, + types, + userExtensions }; } diff --git a/packages/extension-base/src/koni/background/handlers/State.ts b/packages/extension-base/src/koni/background/handlers/State.ts index 8e2b716f32..8f6201a39a 100644 --- a/packages/extension-base/src/koni/background/handlers/State.ts +++ b/packages/extension-base/src/koni/background/handlers/State.ts @@ -2067,7 +2067,9 @@ export default class KoniState { return { metadata: metadata?.hexValue || '', - specVersion: parseInt(metadata?.specVersion || '0') + specVersion: parseInt(metadata?.specVersion || '0'), + types: metadata?.types || {}, + userExtensions: metadata?.userExtensions }; } diff --git a/packages/extension-base/src/services/chain-service/handler/SubstrateApi.ts b/packages/extension-base/src/services/chain-service/handler/SubstrateApi.ts index 282c969aa8..0a180ad915 100644 --- a/packages/extension-base/src/services/chain-service/handler/SubstrateApi.ts +++ b/packages/extension-base/src/services/chain-service/handler/SubstrateApi.ts @@ -18,13 +18,24 @@ import { BehaviorSubject } from 'rxjs'; import { ApiPromise, WsProvider } from '@polkadot/api'; import { SubmittableExtrinsicFunction } from '@polkadot/api/promise/types'; import { ApiOptions } from '@polkadot/api/types'; -import { typesBundle } from '@polkadot/apps-config/api'; +import { typesBundle as _typesBundle } from '@polkadot/apps-config/api'; import { ProviderInterface } from '@polkadot/rpc-provider/types'; import { TypeRegistry } from '@polkadot/types/create'; -import { Registry } from '@polkadot/types/types'; +import { OverrideBundleDefinition, Registry } from '@polkadot/types/types'; import { BN, formatBalance } from '@polkadot/util'; import { defaults as addressDefaults } from '@polkadot/util-crypto/address/defaults'; +const typesBundle = { ..._typesBundle }; + +// Override avail spec for signedExtensions +const _availSpec: OverrideBundleDefinition = { + signedExtensions: availSpec.signedExtensions +}; + +if (typesBundle.spec) { + typesBundle.spec.avail = _availSpec; +} + export class SubstrateApi implements _SubstrateApi { chainSlug: string; api: ApiPromise; @@ -89,7 +100,7 @@ export class SubstrateApi implements _SubstrateApi { const apiOption: ApiOptions = { provider, typesBundle, - registry: this.registry, + registry: this.registry, // This line makes this object registry to be the same as the api registry noInitWarn: true }; @@ -108,29 +119,26 @@ export class SubstrateApi implements _SubstrateApi { if (externalApiPromise) { api = externalApiPromise; } else if (_API_OPTIONS_CHAIN_GROUP.acala.includes(this.chainSlug)) { - api = new ApiPromise(acalaOptions({ provider, noInitWarn: true })); + api = new ApiPromise(acalaOptions(apiOption)); } else if (_API_OPTIONS_CHAIN_GROUP.turing.includes(this.chainSlug)) { api = new ApiPromise({ - provider, + ...apiOption, rpc: oakRpc, - types: oakTypes, - noInitWarn: true + types: oakTypes }); } else if (_API_OPTIONS_CHAIN_GROUP.avail.includes(this.chainSlug)) { api = new ApiPromise({ - provider, + ...apiOption, rpc: availSpec.rpc, types: availSpec.types, - signedExtensions: availSpec.signedExtensions, - noInitWarn: true + signedExtensions: availSpec.signedExtensions }); } else if (_API_OPTIONS_CHAIN_GROUP.goldberg.includes(this.chainSlug)) { api = new ApiPromise({ - provider, + ...apiOption, rpc: goldbergRpc, types: goldbergTypes, - signedExtensions: availSpec.signedExtensions, - noInitWarn: true + signedExtensions: availSpec.signedExtensions }); } else { api = new ApiPromise(apiOption); @@ -272,7 +280,11 @@ export class SubstrateApi implements _SubstrateApi { this.systemName = systemName.toString(); this.systemVersion = systemVersion.toString(); - const properties = registry.createType('ChainProperties', { ss58Format: api.registry.chainSS58, tokenDecimals: api.registry.chainDecimals, tokenSymbol: api.registry.chainTokens }); + const properties = registry.createType('ChainProperties', { + ss58Format: api.registry.chainSS58, + tokenDecimals: api.registry.chainDecimals, + tokenSymbol: api.registry.chainTokens + }); const ss58Format = properties.ss58Format.unwrapOr(DEFAULT_SS58).toNumber(); const tokenSymbol = properties.tokenSymbol.unwrapOr([formatBalance.getDefaults().unit, ...DEFAULT_AUX]); const tokenDecimals = properties.tokenDecimals.unwrapOr([DEFAULT_DECIMALS]); diff --git a/packages/extension-base/src/services/chain-service/handler/SubstrateChainHandler.ts b/packages/extension-base/src/services/chain-service/handler/SubstrateChainHandler.ts index 5d7b869faa..2decb3017f 100644 --- a/packages/extension-base/src/services/chain-service/handler/SubstrateChainHandler.ts +++ b/packages/extension-base/src/services/chain-service/handler/SubstrateChainHandler.ts @@ -10,6 +10,7 @@ import { _ApiOptions, _SubstrateChainSpec } from '@subwallet/extension-base/serv import { _SmartContractTokenInfo, _SubstrateApi } from '@subwallet/extension-base/services/chain-service/types'; import { ContractPromise } from '@polkadot/api-contract'; +import { getSpecExtensions, getSpecTypes } from '@polkadot/types-known'; import { BN } from '@polkadot/util'; import { logger as createLogger } from '@polkadot/util/logger'; import { Logger } from '@polkadot/util/types'; @@ -203,6 +204,31 @@ export class SubstrateChainHandler extends AbstractChainHandler { public async initApi (chainSlug: string, apiUrl: string, { externalApiPromise, onUpdateStatus, providerName }: Omit<_ApiOptions, 'metadata'> = {}): Promise { const existed = this.substrateApiMap[chainSlug]; + const metadata = await this.parent?.getMetadata(chainSlug); + + const updateMetadata = (substrateApi: SubstrateApi) => { + // Update metadata to database with async methods + substrateApi.api.isReady.then(async (api) => { + const currentSpecVersion = api.runtimeVersion.specVersion.toString(); + const genesisHash = api.genesisHash.toHex(); + + // Avoid date existed metadata + if (metadata && metadata.specVersion === currentSpecVersion && metadata.genesisHash === genesisHash) { + return; + } + + const systemChain = await api.rpc.system.chain(); + + this.parent?.upsertMetadata(chainSlug, { + chain: chainSlug, + genesisHash: genesisHash, + specVersion: currentSpecVersion, + hexValue: api.runtimeMetadata.asCallsOnly.toHex(), // Shorten metadata to save space + types: getSpecTypes(api.registry, systemChain, api.runtimeVersion.specName, api.runtimeVersion.specVersion) as unknown as Record, + userExtensions: getSpecExtensions(api.registry, systemChain, api.runtimeVersion.specName) + }).catch(console.error); + }).catch(console.error); + }; // Return existed to avoid re-init metadata if (existed) { @@ -212,29 +238,18 @@ export class SubstrateChainHandler extends AbstractChainHandler { await existed.updateApiUrl(apiUrl); } + // Update data in case of existed api (if needed - old provider cannot connect) + updateMetadata(existed); + return existed; } - const metadata = await this.parent?.getMetadata(chainSlug); const apiObject = new SubstrateApi(chainSlug, apiUrl, { providerName, metadata, externalApiPromise }); apiObject.connectionStatusSubject.subscribe(this.handleConnection.bind(this, chainSlug)); onUpdateStatus && apiObject.connectionStatusSubject.subscribe(onUpdateStatus); - // Update metadata to database with async methods - apiObject.isReady.then((api) => { - // Avoid date existed metadata - if (metadata && metadata.specVersion === api.specVersion && metadata.genesisHash === api.api.genesisHash.toHex()) { - return; - } - - this.parent?.upsertMetadata(chainSlug, { - chain: chainSlug, - genesisHash: api.api.genesisHash.toHex(), - specVersion: api.specVersion, - hexValue: api.api.runtimeMetadata.toHex() - }).catch(console.error); - }).catch(console.error); + updateMetadata(apiObject); return apiObject; } diff --git a/packages/extension-base/src/services/migration-service/scripts/ClearMetadataDatabase.ts b/packages/extension-base/src/services/migration-service/scripts/ClearMetadataDatabase.ts new file mode 100644 index 0000000000..10228910bd --- /dev/null +++ b/packages/extension-base/src/services/migration-service/scripts/ClearMetadataDatabase.ts @@ -0,0 +1,13 @@ +// Copyright 2019-2022 @subwallet/extension-koni authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import BaseMigrationJob from '@subwallet/extension-base/services/migration-service/Base'; + +export default class ClearMetadataDatabase extends BaseMigrationJob { + public override async run (): Promise { + // Clear all old metadata data + await this.state.dbService.stores.metadata.clear(); + + return Promise.resolve(); + } +} diff --git a/packages/extension-base/src/services/migration-service/scripts/index.ts b/packages/extension-base/src/services/migration-service/scripts/index.ts index c01c81ac99..eca99081a9 100644 --- a/packages/extension-base/src/services/migration-service/scripts/index.ts +++ b/packages/extension-base/src/services/migration-service/scripts/index.ts @@ -6,6 +6,7 @@ import MigrateTransactionHistoryBySymbol from '@subwallet/extension-base/service import BaseMigrationJob from '../Base'; import MigrateAssetSetting from './databases/MigrateAssetSetting'; +import ClearMetadataDatabase from './ClearMetadataDatabase'; export const EVERYTIME = '__everytime__'; @@ -35,5 +36,6 @@ export default >{ // [`${EVERYTIME}-1`]: AutoEnableChainsTokens '1.1.58-0___AVAIL': EnableAvailTuringChain, '1.1.62-01': MigrateAssetSetting, - '1.1.62-02': MigrateTransactionHistoryBySymbol + '1.1.53-02': MigrateTransactionHistoryBySymbol, + '1.1.61-01___AVAIL': ClearMetadataDatabase }; diff --git a/packages/extension-base/src/utils/registry.ts b/packages/extension-base/src/utils/registry.ts index e05426e605..89c353e487 100644 --- a/packages/extension-base/src/utils/registry.ts +++ b/packages/extension-base/src/utils/registry.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { _ChainInfo } from '@subwallet/chain-list/types'; +import { ResponseFindRawMetadata } from '@subwallet/extension-base/background/KoniTypes'; import { _getChainNativeTokenBasicInfo } from '@subwallet/extension-base/services/chain-service/utils'; import { Metadata, TypeRegistry } from '@polkadot/types'; @@ -9,14 +10,13 @@ import { ChainProperties } from '@polkadot/types/interfaces'; import { Registry } from '@polkadot/types/types'; import { HexString } from '@polkadot/util/types'; -export const createRegistry = (chain: _ChainInfo, rawMetadata: HexString): Registry => { +export const createRegistry = (chain: _ChainInfo, data: ResponseFindRawMetadata): Registry => { const registry: Registry = new TypeRegistry(); - const metadata = new Metadata(registry, rawMetadata); - - registry.setMetadata(metadata); - + const metadata = new Metadata(registry, data.rawMetadata as HexString); const tokenInfo = _getChainNativeTokenBasicInfo(chain); + registry.register(data.types); + registry.setMetadata(metadata, undefined, data.userExtensions); registry.setChainProperties(registry.createType('ChainProperties', { ss58Format: chain.substrateInfo?.addressPrefix || 42, tokenDecimals: tokenInfo.decimals, diff --git a/packages/extension-koni-ui/src/constants/ledger.ts b/packages/extension-koni-ui/src/constants/ledger.ts index aa0e97ed6f..51d175da52 100644 --- a/packages/extension-koni-ui/src/constants/ledger.ts +++ b/packages/extension-koni-ui/src/constants/ledger.ts @@ -5,6 +5,17 @@ import { ChainInfoMap } from '@subwallet/chain-list'; import { ExtrinsicType, LedgerNetwork } from '@subwallet/extension-base/background/KoniTypes'; export const PredefinedLedgerNetwork: LedgerNetwork[] = [ + { + accountName: 'Avail', + appName: 'Avail', + networkName: 'Avail network', + genesisHash: ChainInfoMap.availTuringTest.substrateInfo?.genesisHash || '0xd3d2f3a3495dc597434a99d7d449ebad6616db45e4e4f178f31cc6fa14378b70', + icon: 'substrate', + network: 'avail', + slug: ChainInfoMap.availTuringTest.slug, + isDevMode: true, + isEthereum: false + }, { accountName: 'Polkadot', appName: 'Polkadot', diff --git a/packages/extension-koni-ui/src/hooks/transaction/confirmation/useParseSubstrateRequestPayload.ts b/packages/extension-koni-ui/src/hooks/transaction/confirmation/useParseSubstrateRequestPayload.ts index bbb1ac041a..412d0c8f76 100644 --- a/packages/extension-koni-ui/src/hooks/transaction/confirmation/useParseSubstrateRequestPayload.ts +++ b/packages/extension-koni-ui/src/hooks/transaction/confirmation/useParseSubstrateRequestPayload.ts @@ -2,15 +2,19 @@ // SPDX-License-Identifier: Apache-2.0 import { RequestSign } from '@subwallet/extension-base/background/types'; +import { useMetadata } from '@subwallet/extension-koni-ui/hooks'; import { isRawPayload } from '@subwallet/extension-koni-ui/utils/confirmation/request/substrate'; import { useMemo } from 'react'; import { TypeRegistry } from '@polkadot/types'; import { ExtrinsicPayload } from '@polkadot/types/interfaces'; +import { SignerPayloadJSON } from '@polkadot/types/types'; const registry = new TypeRegistry(); const useParseSubstrateRequestPayload = (request?: RequestSign): ExtrinsicPayload | string => { + const chain = useMetadata((request?.payload as SignerPayloadJSON).genesisHash); + return useMemo(() => { if (!request) { return ''; @@ -21,11 +25,13 @@ const useParseSubstrateRequestPayload = (request?: RequestSign): ExtrinsicPayloa if (isRawPayload(payload)) { return payload.data; } else { - registry.setSignedExtensions(payload.signedExtensions); // Important + const _registry = chain?.registry || registry; + + _registry.setSignedExtensions(payload.signedExtensions, chain?.definition.userExtensions); // Important - return registry.createType('ExtrinsicPayload', payload, { version: payload.version }); + return _registry.createType('ExtrinsicPayload', payload, { version: payload.version }); } - }, [request]); + }, [chain, request]); }; export default useParseSubstrateRequestPayload; diff --git a/packages/extension-koni-ui/src/messaging/metadata/index.ts b/packages/extension-koni-ui/src/messaging/metadata/index.ts index f21cf0bd12..26fc9d4c5b 100644 --- a/packages/extension-koni-ui/src/messaging/metadata/index.ts +++ b/packages/extension-koni-ui/src/messaging/metadata/index.ts @@ -10,8 +10,6 @@ import { MetadataDef } from '@subwallet/extension-inject/types'; import { sendMessage } from '@subwallet/extension-koni-ui/messaging/base'; import { _getKnownHashes, _getKnownNetworks, findChainInfoByGenesisHash } from '@subwallet/extension-koni-ui/utils'; -import { HexString } from '@polkadot/util/types'; - import { getSavedMeta, setSavedMeta } from './MetadataCache'; export async function getAllMetadata (): Promise { @@ -59,7 +57,9 @@ export async function getMetadataRaw (chainInfoMap: Record, return null; } - const { rawMetadata, specVersion } = await sendMessage('pri(metadata.find)', { genesisHash }); + const data = await sendMessage('pri(metadata.find)', { genesisHash }); + + const { rawMetadata, specVersion } = data; if (!rawMetadata) { return null; @@ -71,7 +71,7 @@ export async function getMetadataRaw (chainInfoMap: Record, return null; } - const registry = createRegistry(chainInfo, rawMetadata as HexString); + const registry = createRegistry(chainInfo, data); const tokenInfo = _getChainNativeTokenBasicInfo(chainInfo); @@ -80,7 +80,10 @@ export async function getMetadataRaw (chainInfoMap: Record, genesisHash, name: chainInfo.name, hasMetadata: true, - definition: {} as MetadataDef, + definition: { + types: data.types, + userExtensions: data.userExtensions + } as MetadataDef, icon: chainInfo.icon, registry: registry, isUnknown: false, diff --git a/packages/extension-web-ui/src/constants/ledger.ts b/packages/extension-web-ui/src/constants/ledger.ts index 490008c107..95a9512396 100644 --- a/packages/extension-web-ui/src/constants/ledger.ts +++ b/packages/extension-web-ui/src/constants/ledger.ts @@ -5,6 +5,17 @@ import { ChainInfoMap } from '@subwallet/chain-list'; import { ExtrinsicType, LedgerNetwork } from '@subwallet/extension-base/background/KoniTypes'; export const PredefinedLedgerNetwork: LedgerNetwork[] = [ + { + accountName: 'Avail', + appName: 'Avail', + networkName: 'Avail network', + genesisHash: ChainInfoMap.availTuringTest.substrateInfo?.genesisHash || '0xd3d2f3a3495dc597434a99d7d449ebad6616db45e4e4f178f31cc6fa14378b70', + icon: 'substrate', + network: 'avail', + slug: ChainInfoMap.availTuringTest.slug, + isDevMode: true, + isEthereum: false + }, { accountName: 'Polkadot', appName: 'Polkadot', diff --git a/packages/extension-web-ui/src/hooks/transaction/confirmation/useParseSubstrateRequestPayload.ts b/packages/extension-web-ui/src/hooks/transaction/confirmation/useParseSubstrateRequestPayload.ts index 6bb39aa78e..2b90848476 100644 --- a/packages/extension-web-ui/src/hooks/transaction/confirmation/useParseSubstrateRequestPayload.ts +++ b/packages/extension-web-ui/src/hooks/transaction/confirmation/useParseSubstrateRequestPayload.ts @@ -2,15 +2,19 @@ // SPDX-License-Identifier: Apache-2.0 import { RequestSign } from '@subwallet/extension-base/background/types'; +import { useMetadata } from '@subwallet/extension-web-ui/hooks'; import { isRawPayload } from '@subwallet/extension-web-ui/utils/confirmation/request/substrate'; import { useMemo } from 'react'; import { TypeRegistry } from '@polkadot/types'; import { ExtrinsicPayload } from '@polkadot/types/interfaces'; +import { SignerPayloadJSON } from '@polkadot/types/types'; const registry = new TypeRegistry(); const useParseSubstrateRequestPayload = (request?: RequestSign): ExtrinsicPayload | string => { + const chain = useMetadata((request?.payload as SignerPayloadJSON).genesisHash); + return useMemo(() => { if (!request) { return ''; @@ -21,11 +25,13 @@ const useParseSubstrateRequestPayload = (request?: RequestSign): ExtrinsicPayloa if (isRawPayload(payload)) { return payload.data; } else { - registry.setSignedExtensions(payload.signedExtensions); // Important + const _registry = chain?.registry || registry; + + _registry.setSignedExtensions(payload.signedExtensions, chain?.definition.userExtensions); // Important - return registry.createType('ExtrinsicPayload', payload, { version: payload.version }); + return _registry.createType('ExtrinsicPayload', payload, { version: payload.version }); } - }, [request]); + }, [chain, request]); }; export default useParseSubstrateRequestPayload; diff --git a/packages/extension-web-ui/src/messaging/metadata/index.ts b/packages/extension-web-ui/src/messaging/metadata/index.ts index 089ef07d6c..2e90694c39 100644 --- a/packages/extension-web-ui/src/messaging/metadata/index.ts +++ b/packages/extension-web-ui/src/messaging/metadata/index.ts @@ -10,8 +10,6 @@ import { MetadataDef } from '@subwallet/extension-inject/types'; import { sendMessage } from '@subwallet/extension-web-ui/messaging/base'; import { _getKnownHashes, _getKnownNetworks, findChainInfoByGenesisHash } from '@subwallet/extension-web-ui/utils'; -import { HexString } from '@polkadot/util/types'; - import { getSavedMeta, setSavedMeta } from './MetadataCache'; export async function getAllMetadata (): Promise { @@ -59,7 +57,9 @@ export async function getMetadataRaw (chainInfoMap: Record, return null; } - const { rawMetadata, specVersion } = await sendMessage('pri(metadata.find)', { genesisHash }); + const data = await sendMessage('pri(metadata.find)', { genesisHash }); + + const { rawMetadata, specVersion } = data; if (!rawMetadata) { return null; @@ -71,7 +71,7 @@ export async function getMetadataRaw (chainInfoMap: Record, return null; } - const registry = createRegistry(chainInfo, rawMetadata as HexString); + const registry = createRegistry(chainInfo, data); const tokenInfo = _getChainNativeTokenBasicInfo(chainInfo); @@ -80,7 +80,10 @@ export async function getMetadataRaw (chainInfoMap: Record, genesisHash, name: chainInfo.name, hasMetadata: true, - definition: {} as MetadataDef, + definition: { + types: data.types, + userExtensions: data.userExtensions + } as MetadataDef, icon: chainInfo.icon, registry: registry, isUnknown: false, diff --git a/packages/webapp/package.json b/packages/webapp/package.json index cfa8745047..5b26730169 100644 --- a/packages/webapp/package.json +++ b/packages/webapp/package.json @@ -49,4 +49,4 @@ "webpack-cli": "^5.1.4", "webpack-dev-server": "^5.0.4" } -} \ No newline at end of file +} diff --git a/patches/@polkadot+hw-ledger+12.6.2.patch b/patches/@polkadot+hw-ledger+12.6.2.patch new file mode 100644 index 0000000000..9dbdf99e5c --- /dev/null +++ b/patches/@polkadot+hw-ledger+12.6.2.patch @@ -0,0 +1,12 @@ +diff --git a/node_modules/@polkadot/hw-ledger/defaults.js b/node_modules/@polkadot/hw-ledger/defaults.js +index 9f06432..97efba8 100644 +--- a/node_modules/@polkadot/hw-ledger/defaults.js ++++ b/node_modules/@polkadot/hw-ledger/defaults.js +@@ -37,5 +37,6 @@ export const ledgerApps = { + unique: 'Unique', + vtb: 'VTB', + xxnetwork: 'XXNetwork', +- zeitgeist: 'Zeitgeist' ++ zeitgeist: 'Zeitgeist', ++ avail: 'Avail' + }; diff --git a/yarn.lock b/yarn.lock index 0f2bbe56f0..68580bd9ed 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3481,18 +3481,6 @@ __metadata: languageName: node linkType: hard -"@ledgerhq/devices@npm:^8.0.2": - version: 8.0.2 - resolution: "@ledgerhq/devices@npm:8.0.2" - dependencies: - "@ledgerhq/errors": ^6.12.5 - "@ledgerhq/logs": ^6.10.1 - rxjs: 6 - semver: ^7.3.5 - checksum: e6be750212c55f612f8802ee66b43b863358412da6872f11933a0d3a1d0056f5dabf34ad31561d1b3a6ce95c60c345211fb9079b0a59d39b86cd4495f6602076 - languageName: node - linkType: hard - "@ledgerhq/devices@npm:^8.2.0": version: 8.2.0 resolution: "@ledgerhq/devices@npm:8.2.0" @@ -3520,13 +3508,6 @@ __metadata: languageName: node linkType: hard -"@ledgerhq/errors@npm:^6.12.5": - version: 6.12.5 - resolution: "@ledgerhq/errors@npm:6.12.5" - checksum: 7758d10ba80f4f0e14b58fc74b512210c6e6472a381b2395b1264090e57d96272967d4b43be1217bde1af69454772dd3cbcd8c283eabbcad6e3c3a86a0e0e089 - languageName: node - linkType: hard - "@ledgerhq/errors@npm:^6.12.6, @ledgerhq/errors@npm:^6.16.1": version: 6.16.1 resolution: "@ledgerhq/errors@npm:6.16.1" @@ -3616,17 +3597,6 @@ __metadata: languageName: node linkType: hard -"@ledgerhq/hw-transport@npm:^6.27.1": - version: 6.28.3 - resolution: "@ledgerhq/hw-transport@npm:6.28.3" - dependencies: - "@ledgerhq/devices": ^8.0.2 - "@ledgerhq/errors": ^6.12.5 - events: ^3.3.0 - checksum: 0d69fba365296fb3647afe3ed766fc175d2145a8ec3d8fa88c040d7ab3ac5e8e79b2dfa18ca0d5fef265815cc139f2461342333c069568260e9815dd7c800333 - languageName: node - linkType: hard - "@ledgerhq/hw-transport@npm:^6.28.4, @ledgerhq/hw-transport@npm:^6.30.1": version: 6.30.1 resolution: "@ledgerhq/hw-transport@npm:6.30.1" @@ -4908,14 +4878,14 @@ __metadata: languageName: node linkType: hard -"@polkadot/types-codec@npm:10.10.1": - version: 10.10.1 - resolution: "@polkadot/types-codec@npm:10.10.1" +"@polkadot/types-codec@npm:^10.12.4": + version: 10.13.1 + resolution: "@polkadot/types-codec@npm:10.13.1" dependencies: - "@polkadot/util": ^12.5.1 - "@polkadot/x-bigint": ^12.5.1 + "@polkadot/util": ^12.6.2 + "@polkadot/x-bigint": ^12.6.2 tslib: ^2.6.2 - checksum: 17ceb561e6a82784febd5c8b0219050a9b8aeeb766ffbae8255ab586120063ca9fea1c89df776047e861aba87e43048a8060d5c469bcd42c169830a69416d62f + checksum: 5f5dadd0cde5686c19aab5042180e54bd9496505063bd873014773c6304c57b80903876162a3e87183487570a6a3e69c707b1ca99f4e6272f7c2c1c9588b9b66 languageName: node linkType: hard @@ -4930,7 +4900,7 @@ __metadata: languageName: node linkType: hard -"@polkadot/types-known@npm:10.13.1": +"@polkadot/types-known@npm:^10.12.4": version: 10.13.1 resolution: "@polkadot/types-known@npm:10.13.1" dependencies: @@ -5179,7 +5149,7 @@ __metadata: languageName: node linkType: hard -"@polkadot/x-bigint@npm:12.6.2, @polkadot/x-bigint@npm:^12.5.1": +"@polkadot/x-bigint@npm:12.6.2, @polkadot/x-bigint@npm:^12.6.2": version: 12.6.2 resolution: "@polkadot/x-bigint@npm:12.6.2" dependencies: @@ -6195,6 +6165,7 @@ __metadata: "@polkadot/rpc-provider": ^10.12.4 "@polkadot/types": ^10.12.4 "@polkadot/types-augment": ^10.12.4 + "@polkadot/types-known": ^10.12.4 "@polkadot/ui-settings": ^3.6.5 "@polkadot/util": ^12.6.2 "@polkadot/util-crypto": ^12.6.2 @@ -8875,20 +8846,12 @@ __metadata: languageName: node linkType: hard -"@zondax/ledger-substrate@npm:^0.41.3": - version: 0.41.3 - resolution: "@zondax/ledger-substrate@npm:0.41.3" +"@zondax/ledger-substrate@npm:^0.42.1": + version: 0.42.1 + resolution: "@zondax/ledger-substrate@npm:0.42.1" dependencies: - "@ledgerhq/hw-transport": ^6.27.1 - bip32: ^4.0.0 - bip32-ed25519: "git+https://github.com/Zondax/bip32-ed25519.git" - bip39: ^3.0.4 - blakejs: ^1.2.1 - bs58: ^5.0.0 - hash.js: ^1.1.7 - bin: - ledger-substrate: dist/cmd/cli.js - checksum: 2c25cb9b28956a47d7152ef0494c8c5bf4c23a9b3afe1321c6fd0e1c1e531ea5d3325acc7666453b53da22aa7fd269d543d6a382848f9ad91cc554074c3d8eea + axios: ^1.6.8 + checksum: c55549f956b976b90753174a71196d6d0ab9d9c96f1534f22196cc5539e0e4dc29c3a79d1ce553bd7b0c76831e71c2fc080cb3c7475053a2054719eeaaea72c4 languageName: node linkType: hard @@ -10511,13 +10474,6 @@ __metadata: languageName: node linkType: hard -"base-x@npm:^4.0.0": - version: 4.0.0 - resolution: "base-x@npm:4.0.0" - checksum: b25db9e07eb1998472a20557c7f00c797dc0595f79df95155ab74274e7fa98b9f2659b3ee547ac8773666b7f69540656793aeb97ad2b1ceccdb6fa5faaf69ac0 - languageName: node - linkType: hard - "base64-js@npm:^1.3.1": version: 1.5.1 resolution: "base64-js@npm:1.5.1" @@ -10599,30 +10555,7 @@ __metadata: languageName: node linkType: hard -"bip32-ed25519@git+https://github.com/Zondax/bip32-ed25519.git": - version: 0.0.4 - resolution: "bip32-ed25519@https://github.com/Zondax/bip32-ed25519.git#commit=0949df01b5c93885339bc28116690292088f6134" - dependencies: - bn.js: ^5.1.1 - elliptic: ^6.4.1 - hash.js: ^1.1.7 - checksum: c0631f4c177b6a459968fb5653956749da1e692634b359155f1c41275eae7f42175629970aa23c1ba39f885b826c76319d03cc2135ae28735777e40e6fde2d7a - languageName: node - linkType: hard - -"bip32@npm:^4.0.0": - version: 4.0.0 - resolution: "bip32@npm:4.0.0" - dependencies: - "@noble/hashes": ^1.2.0 - "@scure/base": ^1.1.1 - typeforce: ^1.11.5 - wif: ^2.0.6 - checksum: a36253ac164db50a25e88effce84aec1eaba2ec2fbd7a19bc1f836f9b1c934fa148c3a01ddece7f136596330074d98c6b2a441e51914c302d130f56f0f893d0d - languageName: node - linkType: hard - -"bip39@npm:^3.0.4, bip39@npm:^3.1.0": +"bip39@npm:^3.1.0": version: 3.1.0 resolution: "bip39@npm:3.1.0" dependencies: @@ -10642,7 +10575,7 @@ __metadata: languageName: node linkType: hard -"blakejs@npm:^1.1.0, blakejs@npm:^1.2.1": +"blakejs@npm:^1.1.0": version: 1.2.1 resolution: "blakejs@npm:1.2.1" checksum: d699ba116cfa21d0b01d12014a03e484dd76d483133e6dc9eb415aa70a119f08beb3bcefb8c71840106a00b542cba77383f8be60cd1f0d4589cb8afb922eefbe @@ -10670,7 +10603,7 @@ __metadata: languageName: node linkType: hard -"bn.js@npm:^5.0.0, bn.js@npm:^5.1.1, bn.js@npm:^5.1.2, bn.js@npm:^5.2.0, bn.js@npm:^5.2.1": +"bn.js@npm:^5.0.0, bn.js@npm:^5.1.2, bn.js@npm:^5.2.0, bn.js@npm:^5.2.1": version: 5.2.1 resolution: "bn.js@npm:5.2.1" checksum: 3dd8c8d38055fedfa95c1d5fc3c99f8dd547b36287b37768db0abab3c239711f88ff58d18d155dd8ad902b0b0cee973747b7ae20ea12a09473272b0201c9edd3 @@ -10936,16 +10869,7 @@ __metadata: languageName: node linkType: hard -"bs58@npm:^5.0.0": - version: 5.0.0 - resolution: "bs58@npm:5.0.0" - dependencies: - base-x: ^4.0.0 - checksum: 2475cb0684e07077521aac718e604a13e0f891d58cff923d437a2f7e9e28703ab39fce9f84c7c703ab369815a675f11e3bd394d38643bfe8969fbe42e6833d45 - languageName: node - linkType: hard - -"bs58check@npm:<3.0.0, bs58check@npm:^2.1.2": +"bs58check@npm:^2.1.2": version: 2.1.2 resolution: "bs58check@npm:2.1.2" dependencies: @@ -13025,7 +12949,7 @@ __metadata: languageName: node linkType: hard -"elliptic@npm:6.5.4, elliptic@npm:^6.4.0, elliptic@npm:^6.4.1, elliptic@npm:^6.5.2, elliptic@npm:^6.5.3, elliptic@npm:^6.5.4": +"elliptic@npm:6.5.4, elliptic@npm:^6.4.0, elliptic@npm:^6.5.2, elliptic@npm:^6.5.3, elliptic@npm:^6.5.4": version: 6.5.4 resolution: "elliptic@npm:6.5.4" dependencies: @@ -26112,13 +26036,6 @@ __metadata: languageName: node linkType: hard -"typeforce@npm:^1.11.5": - version: 1.18.0 - resolution: "typeforce@npm:1.18.0" - checksum: e3b21e27e76cb05f32285bef7c30a29760e79c622cfe4aa3c179ce49d1c7895b7154c8deedb9fe4599b1fd0428d35860d43e0776da1c04861168f3ad7ed99c70 - languageName: node - linkType: hard - "typescript@npm:^4.8.4": version: 4.8.4 resolution: "typescript@npm:4.8.4" @@ -27617,15 +27534,6 @@ __metadata: languageName: node linkType: hard -"wif@npm:^2.0.6": - version: 2.0.6 - resolution: "wif@npm:2.0.6" - dependencies: - bs58check: <3.0.0 - checksum: 8c3147ef98d56f394d66f0477f699fba7fc18dd0d1c2c5d0f8408be41acffed589fa82447d80eae5afc9a3cbd943bc3eebb337b9f114955adeaad02a244f4f9a - languageName: node - linkType: hard - "wildcard@npm:^2.0.0": version: 2.0.0 resolution: "wildcard@npm:2.0.0"