diff --git a/patterns/compat/pjs_sender.ts b/patterns/compat/pjs_sender.ts index e57742fc7..790bb2970 100644 --- a/patterns/compat/pjs_sender.ts +++ b/patterns/compat/pjs_sender.ts @@ -4,7 +4,7 @@ import { AddressPrefixChain, Chain, ChainRune, ExtrinsicSender } from "../../flu import { SignerError } from "../../frame_metadata/mod.ts" import { Rune, RunicArgs } from "../../rune/mod.ts" -export type PjsSigner = { signPayload(payload: any): Promise<{ signature: string }> } +export type PjsSigner = { signPayload?(payload: any): Promise<{ signature: string }> } export function pjsSender( chain: ChainRune, @@ -43,7 +43,7 @@ export function pjsSender( payload.address = address let sig try { - sig = await pjsSigner.signPayload(payload) + sig = await pjsSigner.signPayload!(payload) } catch (e) { throw new SignerError(e) } @@ -55,14 +55,18 @@ export function pjsSender( } } -const convertExtensionCodec = (keyMap: Record) => { +type KeyHandler = (codec: $.Codec) => $.Codec + +const simpleKeyHandler = (pjsKey: string): KeyHandler => (value) => $.field(pjsKey, $pjs(value)) + +const convertExtensionCodec = (keyMap: Record) => { const visitor: $.CodecVisitor<$.Codec> = new $.CodecVisitor<$.Codec>() .add($.field, (_, key: string, value) => { - const pjsKey = keyMap[key] - if (pjsKey === undefined) { + const handler = keyMap[key] + if (handler === undefined) { throw new Error(`pjs signer: unknown extension ${key}`) } - return $.field(pjsKey, $pjs(value)) + return handler(value) }) .add($.object, (_, ...fields) => $.object(...fields.map((x) => visitor.visit(x)))) return visitor @@ -72,20 +76,30 @@ const keys: $.CodecVisitor = new $.CodecVisitor() .add($.field, (_, key: string, _v) => [key]) .add($.object, (_, ...fields) => fields.flatMap((x) => keys.visit(x))) -const pjsExtraKeyMap: Record = { - CheckEra: "era", - CheckMortality: "era", - ChargeTransactionPayment: "tip", - CheckNonce: "nonce", +// https://github.com/polkadot-js/api/tree/8282159/packages/types/src/extrinsic/signedExtensions + +const pjsExtraKeyMap: Record = { + CheckEra: simpleKeyHandler("era"), + CheckMortality: simpleKeyHandler("era"), + ChargeTransactionPayment: simpleKeyHandler("tip"), + CheckNonce: simpleKeyHandler("nonce"), + ChargeAssetTxPayment: (value) => { + // https://github.com/polkadot-js/api/blob/8282159/packages/types/src/extrinsic/signedExtensions/statemint.ts + // pjs essentially spreads this into the extrinsic + const visitor: $.CodecVisitor<$.Codec> = new $.CodecVisitor<$.Codec>() + .add($.field, (_, key, value) => $.field(key, $pjs(value))) + .add($.object, (_, ...fields) => $.object(...fields.map((x) => visitor.visit(x)))) + return visitor.visit(value) + }, } -const pjsAdditionalKeyMap: Record = { - CheckEra: "blockHash", - CheckMortality: "blockHash", - CheckSpecVersion: "specVersion", - CheckTxVersion: "transactionVersion", - CheckVersion: "specVersion", - CheckGenesis: "genesisHash", +const pjsAdditionalKeyMap: Record = { + CheckEra: simpleKeyHandler("blockHash"), + CheckMortality: simpleKeyHandler("blockHash"), + CheckSpecVersion: simpleKeyHandler("specVersion"), + CheckTxVersion: simpleKeyHandler("transactionVersion"), + CheckVersion: simpleKeyHandler("specVersion"), + CheckGenesis: simpleKeyHandler("genesisHash"), } function $pjs($inner: $.Codec): $.Codec<(T & number) | string> {