forked from polkadot-js/extension
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3937 from Koniverse/koni/dev/issue-3306-v2
[Issue 3306] Update signing flow with metadata
- Loading branch information
Showing
10 changed files
with
275 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2019-2022 @subwallet/extension-koni authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { _ChainInfo } from '@subwallet/chain-list/types'; | ||
import { MetadataItem } from '@subwallet/extension-base/background/KoniTypes'; | ||
import { metadataExpand } from '@subwallet/extension-chains/bundle'; | ||
import { MetadataDef } from '@subwallet/extension-inject/types'; | ||
|
||
import { Metadata, TypeRegistry } from '@polkadot/types'; | ||
import { ChainProperties } from '@polkadot/types/interfaces'; | ||
import { Registry, SignerPayloadJSON } from '@polkadot/types/types'; | ||
|
||
import KoniState from './handlers/State'; | ||
|
||
export interface RegistrySource{ | ||
registry: Registry, | ||
specVersion: string | number, | ||
} | ||
|
||
export function getSuitableRegistry (registries: RegistrySource[], payload: SignerPayloadJSON) { | ||
const payloadSpecVersion = parseInt(payload.specVersion); | ||
const sortedRegistries = registries | ||
.filter((registrySource): registrySource is RegistrySource => registrySource.registry !== undefined) | ||
.map((registrySource) => { | ||
const specVersion = Number(registrySource.specVersion); | ||
const distance = Math.abs(specVersion - payloadSpecVersion); | ||
const isHigher = specVersion >= payloadSpecVersion; | ||
|
||
return { | ||
registry: registrySource.registry, | ||
specVersion, | ||
distance, | ||
isHigher | ||
}; | ||
}) | ||
.sort((a, b) => { | ||
if (a.distance !== b.distance) { | ||
return a.distance - b.distance; | ||
} | ||
|
||
return b.specVersion - a.specVersion; | ||
}); | ||
|
||
return sortedRegistries[0].registry; | ||
} | ||
|
||
export function setupApiRegistry (chainInfo: _ChainInfo | undefined, koniState: KoniState): RegistrySource | null { | ||
if (!chainInfo) { | ||
return null; | ||
} | ||
|
||
const api = koniState.getSubstrateApi(chainInfo.slug).api; | ||
const apiSpecVersion = api?.runtimeVersion.specVersion.toString(); | ||
const registry = api?.registry as unknown as TypeRegistry; | ||
|
||
return { | ||
registry, | ||
specVersion: apiSpecVersion | ||
}; | ||
} | ||
|
||
export function setupDatabaseRegistry (metadata: MetadataItem, chainInfo: _ChainInfo | undefined, payload: SignerPayloadJSON): RegistrySource | null { | ||
if (!metadata || !metadata.genesisHash || !chainInfo) { | ||
return null; | ||
} | ||
|
||
const registry = new TypeRegistry(); | ||
const _metadata = new Metadata(registry, metadata.hexValue); | ||
|
||
registry.register(metadata.types); | ||
registry.setChainProperties(registry.createType('ChainProperties', metadata.tokenInfo) as unknown as ChainProperties); | ||
registry.setMetadata(_metadata, payload.signedExtensions, metadata.userExtensions); | ||
|
||
return { | ||
registry, | ||
specVersion: metadata.specVersion | ||
}; | ||
} | ||
|
||
export function setupDappRegistry (metadata: MetadataDef, payload: SignerPayloadJSON): RegistrySource | null { | ||
if (!metadata || !metadata.genesisHash) { | ||
return null; | ||
} | ||
|
||
const expanded = metadataExpand(metadata, false); | ||
const registry = expanded.registry; | ||
|
||
registry.setSignedExtensions(payload.signedExtensions, expanded.definition.userExtensions); | ||
|
||
return { | ||
registry, | ||
specVersion: metadata.specVersion | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
42a5e2d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Deployed on https://subwallet-dev--sw-web-runner.netlify.app