Files
pezkuwi-api/packages/merkleize-metadata/src/get-lookup.ts
T
Satoshi Qazi Muhammed ea8cb96cea chore(papi): commit the sources behind the four polkadot-api packages
papi-utils, bizinikiwi-bindings, metadata-builders and merkleize-metadata
held only dist/ builds, and no repository held their source. Every
source map in dist/ embeds the TypeScript it was compiled from; that is
now committed as src/ (59 files), and it matches upstream polkadot-api
(MIT) exactly once @polkadot-api/* imports are renamed:

  papi-utils           @polkadot-api/utils 0.2.0
  bizinikiwi-bindings  @polkadot-api/substrate-bindings 0.16.6
  metadata-builders    @polkadot-api/metadata-builders 0.13.0-0.13.2
  merkleize-metadata   @polkadot-api/merkleize-metadata 1.1.21-1.1.23

scripts/check-papi-sources.mjs, run in the lint job, fails unless the
source embedded in dist/ is exactly the committed src/. tsconfig.json
excludes these src/ trees; ESLint already skipped the packages. Each
README now states the upstream package and version.
2026-10-19 21:08:01 +04:00

120 lines
3.0 KiB
TypeScript

import { Lookup, LookupValue, ScalePrimitive, TypeDef, TypeRef } from "./codecs"
const bitSequenceBytes = {
u8: 1,
u16: 2,
u32: 4,
u64: 8,
}
const constructTypeDef = (
definitions: Map<number, LookupValue>,
getTypeRef: (frameId: number) => TypeRef,
getPrimitive: (frameId: number) => ScalePrimitive["tag"] | null,
frameId: number,
): TypeDef[] => {
const {
def: { tag, value },
} = definitions.get(frameId)!
switch (tag) {
case "composite":
return [
{
tag,
value: value.map((f) => ({
name: f.name,
typeName: f.typeName,
ty: getTypeRef(f.type),
})),
},
]
case "variant": {
return value.map((v) => ({
tag: "enumeration",
value: {
name: v.name,
index: v.index,
fields: v.fields.map((f) => ({
name: f.name,
typeName: f.typeName,
ty: getTypeRef(f.type),
})),
},
}))
}
case "sequence":
return [
{
tag,
value: getTypeRef(value),
},
]
case "array":
return [
{
tag,
value: {
len: value.len,
typeParam: getTypeRef(value.type),
},
},
]
case "tuple":
return [
{
tag,
value: value.map(getTypeRef),
},
]
case "bitSequence": {
const primitive = getPrimitive(value.bitStoreType)
const numBytes = bitSequenceBytes[primitive as "u8"]
if (!numBytes) throw new Error("Invalid primitive for BitSequence")
const storeOrderPath = definitions.get(value.bitOrderType)!.path
const leastSignificantBitFirst = storeOrderPath.includes("Lsb0")
if (!leastSignificantBitFirst && !storeOrderPath.includes("Msb0"))
throw new Error("BitOrderType not recognized")
return [
{
tag: "bitSequence",
value: { numBytes, leastSignificantBitFirst },
},
]
}
}
throw new Error(`FrameId(${frameId}) should have been filtered out`)
}
export const getLookup = (
definitions: Map<number, LookupValue>,
accessibleTypes: Map<number, number>,
getTypeRef: (frameId: number) => TypeRef,
getPrimitive: (frameId: number) => ScalePrimitive["tag"] | null,
) => {
const typeTree: Lookup = []
;[...accessibleTypes.entries()].forEach(([frameId, typeId]) => {
const { path } = definitions.get(frameId)!
constructTypeDef(definitions, getTypeRef, getPrimitive, frameId).forEach(
(typeDef) => {
typeTree.push({
path,
typeId,
typeDef,
})
},
)
})
typeTree.sort((a, b) => {
if (a.typeId !== b.typeId) return a.typeId - b.typeId // in general
// should only happen for variants
if (a.typeDef.tag !== "enumeration" || b.typeDef.tag !== "enumeration")
throw new Error("Found two types with same id")
return a.typeDef.value.index - b.typeDef.value.index
})
return typeTree
}