Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

fix: add ChargeAssetTxPayment to pjs_sender #1105

Merged
merged 2 commits into from
Jun 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions patterns/compat/pjs_sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<C extends AddressPrefixChain, CU>(
chain: ChainRune<C, CU>,
Expand Down Expand Up @@ -43,7 +43,7 @@ export function pjsSender<C extends AddressPrefixChain, CU>(
payload.address = address
let sig
try {
sig = await pjsSigner.signPayload(payload)
sig = await pjsSigner.signPayload!(payload)
} catch (e) {
throw new SignerError(e)
}
Expand All @@ -55,14 +55,18 @@ export function pjsSender<C extends AddressPrefixChain, CU>(
}
}

const convertExtensionCodec = (keyMap: Record<string, string>) => {
type KeyHandler = (codec: $.Codec<any>) => $.Codec<any>

const simpleKeyHandler = (pjsKey: string): KeyHandler => (value) => $.field(pjsKey, $pjs(value))

const convertExtensionCodec = (keyMap: Record<string, KeyHandler>) => {
const visitor: $.CodecVisitor<$.Codec<any>> = new $.CodecVisitor<$.Codec<any>>()
.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
Expand All @@ -72,20 +76,30 @@ const keys: $.CodecVisitor<string[]> = new $.CodecVisitor<string[]>()
.add($.field, (_, key: string, _v) => [key])
.add($.object, (_, ...fields) => fields.flatMap((x) => keys.visit(x)))

const pjsExtraKeyMap: Record<string, string> = {
CheckEra: "era",
CheckMortality: "era",
ChargeTransactionPayment: "tip",
CheckNonce: "nonce",
// https://github.com/polkadot-js/api/tree/8282159/packages/types/src/extrinsic/signedExtensions

const pjsExtraKeyMap: Record<string, KeyHandler> = {
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<any>> = new $.CodecVisitor<$.Codec<any>>()
.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<string, string> = {
CheckEra: "blockHash",
CheckMortality: "blockHash",
CheckSpecVersion: "specVersion",
CheckTxVersion: "transactionVersion",
CheckVersion: "specVersion",
CheckGenesis: "genesisHash",
const pjsAdditionalKeyMap: Record<string, KeyHandler> = {
CheckEra: simpleKeyHandler("blockHash"),
CheckMortality: simpleKeyHandler("blockHash"),
CheckSpecVersion: simpleKeyHandler("specVersion"),
CheckTxVersion: simpleKeyHandler("transactionVersion"),
CheckVersion: simpleKeyHandler("specVersion"),
CheckGenesis: simpleKeyHandler("genesisHash"),
}

function $pjs<T>($inner: $.Codec<T>): $.Codec<(T & number) | string> {
Expand Down