mirror of
https://github.com/pezkuwichain/pezkuwi-api.git
synced 2026-12-04 02:27:07 +00:00
* 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. * chore(papi): keep the upstream sources in upstream-src/ The pezkuwi-dev build treats any package with a src/ directory as its own: it requires our license header on every file and builds it. These are upstream files that must stay byte-identical to the source embedded in dist/, so they live in upstream-src/ instead, which neither the build nor the root tsconfig picks up.
107 lines
2.8 KiB
TypeScript
107 lines
2.8 KiB
TypeScript
import { fromHex, mergeUint8, toHex } from "@pezkuwi/papi-utils"
|
|
import type { Codec } from "scale-ts"
|
|
import {
|
|
Blake2128,
|
|
Blake2128Concat,
|
|
Blake2256,
|
|
Identity,
|
|
Twox128,
|
|
Twox256,
|
|
Twox64Concat,
|
|
} from "./hashes"
|
|
|
|
export type EncoderWithHash<T> = [Codec<T>, (input: Uint8Array) => Uint8Array]
|
|
|
|
const textEncoder = new TextEncoder()
|
|
|
|
// the value indicates:
|
|
// - when positive: the number of bytes to skip before reaching the transparent-encoded key
|
|
// - when negative: the number of bytes that the opaque hasher will generate
|
|
const hashers: Map<(input: Uint8Array) => Uint8Array, number> = new Map([
|
|
[Identity, 0],
|
|
[Twox64Concat, 8],
|
|
[Blake2128Concat, 16],
|
|
[Blake2128, -16],
|
|
[Blake2256, -32],
|
|
[Twox128, -16],
|
|
[Twox256, -32],
|
|
])
|
|
|
|
export type OpaqueKeyHash = string & { __opaqueKeyHash?: unknown }
|
|
|
|
export const Storage = (pallet: string) => {
|
|
const palledEncoded = Twox128(textEncoder.encode(pallet))
|
|
return <A extends Array<EncoderWithHash<any>>>(
|
|
name: string,
|
|
...encoders: [...A]
|
|
): {
|
|
enc: (
|
|
...args: {
|
|
[K in keyof A]: A[K] extends EncoderWithHash<infer V> ? V : unknown
|
|
}
|
|
) => string
|
|
dec: (value: string) => {
|
|
[K in keyof A]: A[K] extends EncoderWithHash<infer V> ? V : unknown
|
|
}
|
|
} => {
|
|
const palletItemEncoded = mergeUint8([
|
|
palledEncoded,
|
|
Twox128(textEncoder.encode(name)),
|
|
])
|
|
|
|
const palletItemEncodedHex = toHex(palletItemEncoded)
|
|
|
|
const dec = (
|
|
key: string,
|
|
): {
|
|
[K in keyof A]: A[K] extends EncoderWithHash<infer V> ? V : unknown
|
|
} => {
|
|
if (!key.startsWith(palletItemEncodedHex))
|
|
throw new Error(`key does not match this storage (${pallet}.${name})`)
|
|
|
|
if (encoders.length === 0) return [] as any
|
|
|
|
const argsKey = fromHex(key.slice(palletItemEncodedHex.length))
|
|
const result = new Array<any>(encoders.length)
|
|
for (let i = 0, cur = 0; i < encoders.length; i++) {
|
|
const [codec, hasher] = encoders[i]
|
|
const hBytes = hashers.get(hasher)
|
|
if (hBytes == null) throw new Error("Unknown hasher")
|
|
if (hBytes < 0) {
|
|
const opaqueBytes = hBytes * -1
|
|
result[i] = toHex(argsKey.slice(cur, cur + opaqueBytes))
|
|
cur += opaqueBytes
|
|
} else {
|
|
cur += hBytes
|
|
result[i] = codec.dec(argsKey.slice(cur))
|
|
cur += codec.enc(result[i]).length
|
|
}
|
|
}
|
|
return result as any
|
|
}
|
|
|
|
const fns = encoders.map(
|
|
([{ enc }, hash]) =>
|
|
(val: any) =>
|
|
hash(enc(val)),
|
|
)
|
|
|
|
const enc = (
|
|
...args: {
|
|
[K in keyof A]: A[K] extends EncoderWithHash<infer V> ? V : unknown
|
|
}
|
|
): string =>
|
|
toHex(
|
|
mergeUint8([
|
|
palletItemEncoded,
|
|
...args.map((val, idx) => fns[idx](val)),
|
|
]),
|
|
)
|
|
|
|
return {
|
|
enc,
|
|
dec,
|
|
}
|
|
}
|
|
}
|