Skip to content

Commit

Permalink
fix: wallet builds with new scale-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
alecdwm committed May 6, 2024
1 parent 0411873 commit 9d0b7ed
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 116 deletions.
10 changes: 2 additions & 8 deletions packages/balances/src/modules/util/buildStorageCoders.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChainId, ChainList } from "@talismn/chaindata-provider"
import { V14, V15, getDynamicBuilder, metadata as scaleMetadata } from "@talismn/scale"
import { decodeMetadata, getDynamicBuilder } from "@talismn/scale"

import log from "../../log"
import { MiniMetadata } from "../../types"
Expand Down Expand Up @@ -30,14 +30,8 @@ export const buildStorageCoders = <
const [, miniMetadata] = findChainMeta<TBalanceModule>(miniMetadatas, moduleType, chain)
if (!miniMetadata) return []
if (!miniMetadata.data) return []
if (miniMetadata.version !== 15 && miniMetadata.version !== 14) return []

const [metadata, tag] = ((): [V15, "v15"] | [V14, "v14"] | [] => {
const decoded = scaleMetadata.dec(miniMetadata.data)
if (decoded.metadata.tag === "v15") return [decoded.metadata.value, decoded.metadata.tag]
if (decoded.metadata.tag === "v14") return [decoded.metadata.value, decoded.metadata.tag]
return []
})()
const { metadata, tag } = decodeMetadata(miniMetadata.data)
if (!metadata || !tag) return []

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { OnChainId, OnChainIds, ResolvedNames } from "@talismn/on-chain-id"
import { chainConnectors } from "../../rpcs/balance-modules"
import { getTypeRegistry } from "../../util/getTypeRegistry"

const chainIdPolkadot = "polkadot"
const chainIdAlephZero = "aleph-zero"
const aznsSupportedChainIdAlephZero = "alephzero"

Expand All @@ -14,17 +13,12 @@ export const lookupAddresses = async (addresses: string[]): Promise<OnChainIds>
(await getOnChainId()).lookupAddresses(addresses)

const getOnChainId = async () => {
const [{ registry: registryPolkadot }, { registry: registryAlephZero }] = await Promise.all([
getTypeRegistry(chainIdPolkadot),
getTypeRegistry(chainIdAlephZero),
])
const { registry: registryAlephZero } = await getTypeRegistry(chainIdAlephZero)

return new OnChainId({
registryPolkadot,
registryAlephZero,
chainConnectors,

chainIdPolkadot,
chainIdAlephZero,
aznsSupportedChainIdAlephZero,
})
Expand Down
10 changes: 1 addition & 9 deletions packages/on-chain-id/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
lookupAddresses,
lookupAznsAddresses,
lookupEnsAddresses,
lookupPolkadotAddresses,
} from "./util/addressesToNames"
import { lookupAddresses, lookupAznsAddresses, lookupEnsAddresses } from "./util/addressesToNames"
import { resolveAznsNames, resolveEnsNames, resolveNames } from "./util/namesToAddresses"
import { Config, DropFirst, OptionalConfig } from "./util/types"

Expand All @@ -22,7 +17,6 @@ export class OnChainId {
constructor(config: OptionalConfig) {
this.#config = {
...config,
chainIdPolkadot: config.chainIdPolkadot ?? "polkadot",
chainIdAlephZero: config.chainIdAlephZero ?? "aleph-zero",
aznsSupportedChainIdAlephZero: config.aznsSupportedChainIdAlephZero ?? "alephzero",
networkIdEthereum: config.networkIdEthereum ?? "1",
Expand All @@ -38,8 +32,6 @@ export class OnChainId {

lookupAddresses = (...args: DropFirst<Parameters<typeof lookupAddresses>>) =>
lookupAddresses(this.#config, ...args)
lookupPolkadotAddresses = (...args: DropFirst<Parameters<typeof lookupPolkadotAddresses>>) =>
lookupPolkadotAddresses(this.#config, ...args)
lookupAznsAddresses = (...args: DropFirst<Parameters<typeof lookupAznsAddresses>>) =>
lookupAznsAddresses(this.#config, ...args)
lookupEnsAddresses = (...args: DropFirst<Parameters<typeof lookupEnsAddresses>>) =>
Expand Down
91 changes: 3 additions & 88 deletions packages/on-chain-id/src/util/addressesToNames.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
import { resolveAddressToDomain } from "@azns/resolver-core"
import { ApiPromise } from "@polkadot/api"
import { RpcStateQuery, RpcStateQueryHelper, StorageHelper } from "@talismn/balances"
import { decodeAnyAddress, isEthereumAddress } from "@talismn/util"
import { isEthereumAddress } from "@talismn/util"

import log from "../log"
import { Config, OnChainIds } from "./types"

/**
* Looks up the on-chain identifiers for some addresses.
*
* Prefers ENS, then AZNS, then falls back to Polkadot identities.
*
* Requires a TypeRegistry which has been instantiated on the Polkadot relay chain.
* Talisman Wallet developers can build one by using `/apps/extension/src/core/util/getTypeRegistry.ts`.
* Supports ENS and AZNS.
*/
export const lookupAddresses = async (config: Config, addresses: string[]): Promise<OnChainIds> => {
const onChainIds: OnChainIds = new Map(addresses.map((address) => [address, null]))

const [/* polkadotIdentities, */ aznsDomains, ensDomains] = await Promise.all([
// lookupPolkadotAddresses(config, addresses),
const [aznsDomains, ensDomains] = await Promise.all([
lookupAznsAddresses(config, addresses),
lookupEnsAddresses(config, addresses),
])

// polkadotIdentities.forEach((polkadotIdentity, address) => {
// if (!polkadotIdentity) return
// onChainIds.set(address, polkadotIdentity)
// })

aznsDomains.forEach((domain, address) => {
if (!domain) return
onChainIds.set(address, domain)
Expand All @@ -41,80 +30,6 @@ export const lookupAddresses = async (config: Config, addresses: string[]): Prom
return onChainIds
}

/**
* Looks up the on-chain Polkadot identities for some addresses.
*
* Requires a TypeRegistry which has been instantiated on the Polkadot relay chain.
* Talisman Wallet developers can build one by using `/apps/extension/src/core/util/getTypeRegistry.ts`.
*/
export const lookupPolkadotAddresses = async (
config: Config,
addresses: string[]
): Promise<OnChainIds> => {
const onChainIds: OnChainIds = new Map(addresses.map((address) => [address, null]))

if (!config.chainConnectors.substrate) {
log.warn(`Could not find Substrate chainConnector in OnChainId::lookupPolkadotAddresses`)
return onChainIds
}

const queries = addresses.flatMap((address): RpcStateQuery<[string, string | null]> | [] => {
const storageHelper = new StorageHelper(
config.registryPolkadot,
"identity",
"identityOf",
decodeAnyAddress(address)
)

// filter out queries which we failed to encode (e.g. an invalid address was input)
const stateKey = storageHelper.stateKey
if (!stateKey) return []

const decodeResult = (change: string | null): [string, string | null] => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const decoded: any = storageHelper.decode(change)

// explicit null is required here to ensure the frontend knows that the address has been queried
const bytes = decoded?.value?.info?.display?.value
const bytesDecoded = new TextDecoder().decode(bytes)

const judgements: string[] =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
decoded?.value?.judgements?.flatMap?.((judgement: any) => {
if (judgement?.[1]?.isErroneous) return "Erroneous"
if (judgement?.[1]?.isFeePaid) return "FeePaid"
if (judgement?.[1]?.isKnownGood) return "KnownGood"
if (judgement?.[1]?.isLowQuality) return "LowQuality"
if (judgement?.[1]?.isOutOfDate) return "OutOfDate"
if (judgement?.[1]?.isReasonable) return "Reasonable"
if (judgement?.[1]?.isUnknown) return "Unknown"

log.warn(`Unknown judgement type ${judgement?.toJSON?.() ?? String(judgement)}`)
return []
}) ?? []
if (judgements.length < 1) judgements.push("NoJudgement")

const display = bytes ? `${bytesDecoded} (${judgements.join(", ")})` : null

return [address, display]
}

return { chainId: config.chainIdPolkadot, stateKey, decodeResult }
})

const identities = await new RpcStateQueryHelper(
config.chainConnectors.substrate,
queries
).fetch()

identities.forEach(([address, polkadotIdentity]) => {
if (!polkadotIdentity) return
onChainIds.set(address, polkadotIdentity)
})

return onChainIds
}

/**
* Looks up the on-chain AZNS domains for some addresses.
*/
Expand Down
4 changes: 0 additions & 4 deletions packages/on-chain-id/src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ export type NsLookupType = "ens" | "azns"
export type Config = {
// TODO: Create a package for `/apps/extension/src/core/util/getTypeRegistry.ts` which
// can be used from outside of the wallet.
registryPolkadot: TypeRegistry
registryAlephZero: TypeRegistry
chainConnectors: ChainConnectors

/** Used for polkadot identity lookups */
chainIdPolkadot: string
/** Used for azns lookups */
chainIdAlephZero: string
/** Used for azns lookups */
Expand All @@ -32,7 +29,6 @@ export type Config = {
}

export type OptionalConfigParams =
| "chainIdPolkadot"
| "chainIdAlephZero"
| "aznsSupportedChainIdAlephZero"
| "networkIdEthereum"
Expand Down

0 comments on commit 9d0b7ed

Please sign in to comment.