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

Commit

Permalink
fix: add ChargeAssetTxPayment to pjs_sender (#1105)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfvi authored Jun 23, 2023
1 parent 34002ce commit 20f36c8
Showing 1 changed file with 32 additions and 18 deletions.
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

1 comment on commit 20f36c8

@deno-deploy
Copy link

@deno-deploy deno-deploy bot commented on 20f36c8 Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to deploy:

HTTP status server error (502 Bad Gateway) for url (https://deno.land/x/[email protected]/sr25519/keccak.wasm.ts)

Please sign in to comment.