@ocap/client 1.30.23 → 1.30.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/base.js CHANGED
@@ -4,7 +4,7 @@
4
4
  const isAbsoluteUrl = require('is-absolute-url');
5
5
  const { pick } = require('./helpers/lite');
6
6
  const Mcrypto = require('@ocap/mcrypto');
7
- const { fromBase64, toUint8Array } = require('@ocap/util');
7
+ const { BN, fromBase64, toUint8Array } = require('@ocap/util');
8
8
  const BaseClient = require('@arcblock/sdk-util');
9
9
  const jsonpack = require('jsonpack');
10
10
 
@@ -17,8 +17,8 @@ const SUBSCRIBE_NOT_INSTALLED = 'subscribe/unsubscribe are not installed in this
17
17
  // and callers never use `$tx` variables. A bounded regex match reproduces
18
18
  // the original contract — we degrade to `{}` (no gas-payer header) for any
19
19
  // non-matching query, matching the prior behavior for non-sendTx mutations.
20
- const SEND_TX_MUTATION_RE = /^\s*mutation\b[^{(]*(?:\([^)]*\))?\s*\{\s*sendTx\s*\(([^)]*)\)/;
21
- const SEND_TX_LITERAL_RE = /\btx\s*:\s*"((?:[^"\\]|\\.)*)"/;
20
+ const SEND_TX_MUTATION_RE = /^\s*mutation\b[^{(]*(?:\([^)]*\))?\s*\{\s*sendTx\s*\(((?:[^"\\)]|"(?:[^"\\]|\\.)*")*)\)/;
21
+ const SEND_TX_ARG_RE = /([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*(?:"((?:[^"\\]|\\.)*)"|[^,\s)]+)/g;
22
22
 
23
23
  /**
24
24
  * Provides a readonly client to forge
@@ -47,8 +47,9 @@ class GraphQLClientBase extends BaseClient {
47
47
  this._socketClient = null;
48
48
  }
49
49
 
50
- setGasPayer(wallet) {
50
+ setGasPayer(wallet, options = {}) {
51
51
  this._gasPayer = wallet;
52
+ this._gasPayerOptions = options || {};
52
53
  }
53
54
 
54
55
  subscribe() {
@@ -89,16 +90,105 @@ class GraphQLClientBase extends BaseClient {
89
90
  return {};
90
91
  }
91
92
 
92
- const txMatch = sendTxMatch[1].match(SEND_TX_LITERAL_RE);
93
- if (!txMatch) {
93
+ let txBase64 = null;
94
+ SEND_TX_ARG_RE.lastIndex = 0;
95
+ for (let match = SEND_TX_ARG_RE.exec(sendTxMatch[1]); match !== null; match = SEND_TX_ARG_RE.exec(sendTxMatch[1])) {
96
+ if (match[1] === 'tx' && typeof match[2] === 'string') {
97
+ txBase64 = match[2];
98
+ break;
99
+ }
100
+ }
101
+ if (!txBase64) {
94
102
  return {};
95
103
  }
96
104
 
97
- const txBuffer = toUint8Array(fromBase64(txMatch[1]));
105
+ const txBuffer = toUint8Array(fromBase64(txBase64));
98
106
  const txHash = Mcrypto.toTxHash(txBuffer);
107
+ const options = this._gasPayerOptions || {};
108
+
109
+ if (options.allowedTxTypes || options.maxTokenValue !== undefined || typeof options.filter === 'function') {
110
+ let decoded = null;
111
+ try {
112
+ const decodeFn = typeof this.decodeTx === 'function' ? this.decodeTx.bind(this) : require('@ocap/message/cbor').decodeTx;
113
+ decoded = decodeFn(txBuffer);
114
+ } catch (_err) {
115
+ return {};
116
+ }
117
+ const decodedTx = decoded || {};
118
+ if (Array.isArray(options.allowedTxTypes)) {
119
+ const normalizeTxType = t => String(t || '').replace(/^fg:t:/, '').replace(/Tx$/i, '').replace(/([a-z0-9])([A-Z])/g, '$1_$2').toLowerCase();
120
+ const itx = decodedTx.itx || {};
121
+ const txType = normalizeTxType(itx.type || itx.typeUrl);
122
+ const allowed = options.allowedTxTypes.map(normalizeTxType);
123
+ if (!txType || !allowed.includes(txType)) {
124
+ return {};
125
+ }
126
+ }
127
+ if (options.maxTokenValue !== undefined && options.maxTokenValue !== null) {
128
+ const toAmountBN = v => {
129
+ if (!v) return new BN(0);
130
+ if (typeof v === 'object' && v.value instanceof Uint8Array) {
131
+ return new BN(v.value);
132
+ }
133
+ return new BN(String(v));
134
+ };
135
+ let totalValue = new BN(0);
136
+ const rawItx = decodedTx.itx || {};
137
+ const itxVal = rawItx.value && typeof rawItx.value === 'object' && !(rawItx.value instanceof Uint8Array) && !rawItx.value.value ? rawItx.value : rawItx;
138
+ const sumContainerValue = rawContainer => {
139
+ let sum = new BN(0);
140
+ if (!rawContainer || typeof rawContainer !== 'object') return sum;
141
+ if (rawContainer.value) {
142
+ sum = sum.add(toAmountBN(rawContainer.value));
143
+ }
144
+ if (Array.isArray(rawContainer.tokens)) {
145
+ for (const rawToken of rawContainer.tokens) {
146
+ const token = rawToken || {};
147
+ if (token.value) {
148
+ sum = sum.add(toAmountBN(token.value));
149
+ }
150
+ }
151
+ }
152
+ return sum;
153
+ };
154
+ try {
155
+ totalValue = totalValue.add(sumContainerValue(itxVal));
156
+ totalValue = totalValue.add(sumContainerValue(itxVal.sender));
157
+ totalValue = totalValue.add(sumContainerValue(itxVal.receiver));
158
+ let inputsTotal = new BN(0);
159
+ if (Array.isArray(itxVal.inputs)) {
160
+ for (const input of itxVal.inputs) {
161
+ inputsTotal = inputsTotal.add(sumContainerValue(input));
162
+ }
163
+ }
164
+ let outputsTotal = new BN(0);
165
+ if (Array.isArray(itxVal.outputs)) {
166
+ for (const output of itxVal.outputs) {
167
+ outputsTotal = outputsTotal.add(sumContainerValue(output));
168
+ }
169
+ }
170
+ totalValue = totalValue.add(inputsTotal.gt(outputsTotal) ? inputsTotal : outputsTotal);
171
+ if (totalValue.gt(new BN(String(options.maxTokenValue)))) {
172
+ return {};
173
+ }
174
+ } catch (_err) {
175
+ return {};
176
+ }
177
+ }
178
+ if (typeof options.filter === 'function') {
179
+ const allowed = await options.filter(decoded, { txHash, txBuffer, query });
180
+ if (!allowed) {
181
+ return {};
182
+ }
183
+ }
184
+ }
185
+
186
+ const ctx = this.context || {};
187
+ const chainId = options.chainId || ctx.chainId;
188
+ const jwtPayload = chainId ? { txHash, chainId } : { txHash };
99
189
  return {
100
190
  'x-gas-payer-pk': this._gasPayer.publicKey,
101
- 'x-gas-payer-sig': await this._gasPayer.signJWT({ txHash })
191
+ 'x-gas-payer-sig': await this._gasPayer.signJWT(jwtPayload)
102
192
  };
103
193
  }
104
194
 
package/dist/base.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/base.js"],"names":["isAbsoluteUrl","require","pick","Mcrypto","fromBase64","toUint8Array","BaseClient","jsonpack","SUBSCRIBE_NOT_INSTALLED","SEND_TX_MUTATION_RE","SEND_TX_LITERAL_RE","GraphQLClientBase","constructor","endpoint","Error","dataSource","httpEndpoint","socketEndpoint","_getSocketEndpoint","enableQuery","enableSubscription","enableMutation","maxQueryDepth","_endpoint","_socketClient","setGasPayer","wallet","_gasPayer","subscribe","unsubscribe","_ensureSocketClient","replace","indexOf","_getAuthHeaders","query","sign","sendTxMatch","match","txMatch","txBuffer","txHash","toTxHash","publicKey","signJWT","_getSchema","unpack","packedSchema","_getIgnoreFields","pickGasPayerHeaders","headers","module","exports"],"mappings":";;;AAEA,MAAMA,gBAAgBC,QAAQ,iBAAR,CAAtB;AACA,MAAM,EAAEC,IAAF,KAAWD,QAAQ,gBAAR,CAAjB;AACA,MAAME,UAAUF,QAAQ,eAAR,CAAhB;AACA,MAAM,EAAEG,UAAF,EAAcC,YAAd,KAA+BJ,QAAQ,YAAR,CAArC;AACA,MAAMK,aAAaL,QAAQ,oBAAR,CAAnB;AACA,MAAMM,WAAWN,QAAQ,UAAR,CAAjB;;AAEA,MAAMO,0BACJ,gJADF;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,sBAAsB,gEAA5B;AACA,MAAMC,qBAAqB,gCAA3B;;AAEA;;;;;;;AAOA,MAAMC,iBAAN,SAAgCL,UAAhC,CAA2C;AACzCM,cAAYC,QAAZ,EAAsB;AACpB,QAAIb,cAAca,QAAd,MAA4B,KAAhC,EAAuC;AACrC,YAAM,IAAIC,KAAJ,CAAU,oCAAV,CAAN;AACD;;AAED,UAAM;AACJC,kBAAY,OADR;AAEJC,oBAAcH,QAFV;AAGJI,sBAAgB,MAAM,KAAKC,kBAAL,CAAwBL,QAAxB,CAHl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WkC,QAAQ,CAAR,CAAX,CAAb,CAAjB;AACA,UAAME,SAASrC,QAAQsC,QAAR,CAAiBF,QAAjB,CAAf;AACA,WAAO;AACL,wBAAkB,KAAKZ,SAAL,CAAee,SAD5B;AAEL,yBAAmB,MAAM,KAAKf,SAAL,CAAegB,OAAf,CAAuB,EAAEH,MAAF,EAAvB;AAFpB,KAAP;AAID;;AAEDI,eAAa;AACX,WAAOrC,SAASsC,MAAT,CAAgBC,YAAhB,CAAP;AACD;;AAEDC,qBAAmB;AACjB,WAAO,EAAP;AACD;;AAEDC,sBAAoB,EAAEC,OAAF,EAApB,EAAiC;AAC/B,WAAO/C,KAAK+C,OAAL,EAAc,CAAC,iBAAD,EAAoB,gBAApB,CAAd,CAAP;AACD;AArFwC;;AAwF3CC,OAAOC,OAAP,GAAiBxC,iBAAjB","file":"base.js","sourcesContent":["import packedSchema from './schema/graphql.txt';\n\nconst isAbsoluteUrl = require('is-absolute-url');\nconst { pick } = require('./helpers/lite');\nconst Mcrypto = require('@ocap/mcrypto');\nconst { fromBase64, toUint8Array } = require('@ocap/util');\nconst BaseClient = require('@arcblock/sdk-util');\nconst jsonpack = require('jsonpack');\n\nconst SUBSCRIBE_NOT_INSTALLED =\n 'subscribe/unsubscribe are not installed in this build. Import \"@ocap/client/subscribe\" and call install(client) to enable topic subscriptions.';\n\n// Gas-payer signing only applies to `mutation { sendTx(tx: \"...\") ... }`.\n// The legacy path ran a full GraphQL parse (graphql/language/parser ~42 KB)\n// just to pick out the literal `tx` arg. The mutation shape is tightly\n// constrained: `sdk-util`'s builder emits `mutation{sendTx(tx: \"...\", ...)}`\n// and callers never use `$tx` variables. A bounded regex match reproduces\n// the original contract — we degrade to `{}` (no gas-payer header) for any\n// non-matching query, matching the prior behavior for non-sendTx mutations.\nconst SEND_TX_MUTATION_RE = /^\\s*mutation\\b[^{(]*(?:\\([^)]*\\))?\\s*\\{\\s*sendTx\\s*\\(([^)]*)\\)/;\nconst SEND_TX_LITERAL_RE = /\\btx\\s*:\\s*\"((?:[^\"\\\\]|\\\\.)*)\"/;\n\n/**\n * Provides a readonly client to forge\n * Its recommended to use this version in browser to get smaller bundle size\n *\n * @class GraphQLClientBase\n * @extends {BaseClient}\n */\nclass GraphQLClientBase extends BaseClient {\n constructor(endpoint) {\n if (isAbsoluteUrl(endpoint) === false) {\n throw new Error('Chain endpoint must be a valid url');\n }\n\n super({\n dataSource: 'forge',\n httpEndpoint: endpoint,\n socketEndpoint: () => this._getSocketEndpoint(endpoint),\n enableQuery: true,\n enableSubscription: true,\n enableMutation: true,\n maxQueryDepth: 6,\n });\n\n this._endpoint = endpoint;\n this._socketClient = null;\n }\n\n setGasPayer(wallet) {\n this._gasPayer = wallet;\n }\n\n subscribe() {\n throw new Error(SUBSCRIBE_NOT_INSTALLED);\n }\n\n unsubscribe() {\n throw new Error(SUBSCRIBE_NOT_INSTALLED);\n }\n\n _ensureSocketClient() {\n throw new Error(SUBSCRIBE_NOT_INSTALLED);\n }\n\n _getSocketEndpoint(endpoint) {\n let socketEndpoint = endpoint.replace(/https?:\\/\\//, 'ws://');\n if (endpoint.indexOf('https://') === 0) {\n socketEndpoint = socketEndpoint.replace('ws://', 'wss://');\n }\n\n return socketEndpoint;\n }\n\n async _getAuthHeaders(query) {\n if (!this._gasPayer) {\n return {};\n }\n if (typeof this._gasPayer.sign !== 'function') {\n return {};\n }\n\n if (typeof query !== 'string') {\n return {};\n }\n\n const sendTxMatch = query.match(SEND_TX_MUTATION_RE);\n if (!sendTxMatch) {\n return {};\n }\n\n const txMatch = sendTxMatch[1].match(SEND_TX_LITERAL_RE);\n if (!txMatch) {\n return {};\n }\n\n const txBuffer = toUint8Array(fromBase64(txMatch[1]));\n const txHash = Mcrypto.toTxHash(txBuffer);\n return {\n 'x-gas-payer-pk': this._gasPayer.publicKey,\n 'x-gas-payer-sig': await this._gasPayer.signJWT({ txHash }),\n };\n }\n\n _getSchema() {\n return jsonpack.unpack(packedSchema);\n }\n\n _getIgnoreFields() {\n return [];\n }\n\n pickGasPayerHeaders({ headers }) {\n return pick(headers, ['x-gas-payer-sig', 'x-gas-payer-pk']);\n }\n}\n\nmodule.exports = GraphQLClientBase;\n"]}
1
+ {"version":3,"sources":["../src/base.js"],"names":["isAbsoluteUrl","require","pick","Mcrypto","BN","fromBase64","toUint8Array","BaseClient","jsonpack","SUBSCRIBE_NOT_INSTALLED","SEND_TX_MUTATION_RE","SEND_TX_ARG_RE","GraphQLClientBase","constructor","endpoint","Error","dataSource","httpEndpoint","socketEndpoint","_getSocketEndpoint","enableQuery","enableSubscription","enableMutation","maxQueryDepth","_endpoint","_socketClient","setGasPayer","wallet","options","_gasPayer","_gasPayerOptions","subscribe","unsubscribe","_ensureSocketClient","replace","indexOf","_getAuthHeaders","query","sign","sendTxMatch","match","txBase64","lastIndex","exec","txBuffer","txHash","toTxHash","allowedTxTypes","maxTokenValue","undefined","filter","decoded","decodeFn","decodeTx","bind","_err","decodedTx","Array","isArray","normalizeTxType","t","String","toLowerCase","itx","txType","type","typeUrl","allowed","map","includes","toAmountBN","v","value","Uint8Array","totalValue","rawItx","itxVal","sumContainerValue","rawContainer","sum","add","tokens","rawToken","token","sender","receiver","inputsTotal","inputs","input","outputsTotal","outputs","output","gt","ctx","context","chainId","jwtPayload","publicKey","signJWT","_getSchema","unpack","packedSchema","_getIgnoreFields","pickGasPayerHeaders","headers","module","exports"],"mappings":";;;AAEA,MAAMA,gBAAgBC,QAAQ,iBAAR,CAAtB;AACA,MAAM,EAAEC,IAAF,KAAWD,QAAQ,gBAAR,CAAjB;AACA,MAAME,UAAUF,QAAQ,eAAR,CAAhB;AACA,MAAM,EAAEG,EAAF,EAAMC,UAAN,EAAkBC,YAAlB,KAAmCL,QAAQ,YAAR,CAAzC;AACA,MAAMM,aAAaN,QAAQ,oBAAR,CAAnB;AACA,MAAMO,WAAWP,QAAQ,UAAR,CAAjB;;AAEA,MAAMQ,0BACJ,gJADF;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAA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8L3CC,OAAOC,OAAP,GAAiB9F,iBAAjB","file":"base.js","sourcesContent":["import packedSchema from './schema/graphql.txt';\n\nconst isAbsoluteUrl = require('is-absolute-url');\nconst { pick } = require('./helpers/lite');\nconst Mcrypto = require('@ocap/mcrypto');\nconst { BN, fromBase64, toUint8Array } = require('@ocap/util');\nconst BaseClient = require('@arcblock/sdk-util');\nconst jsonpack = require('jsonpack');\n\nconst SUBSCRIBE_NOT_INSTALLED =\n 'subscribe/unsubscribe are not installed in this build. Import \"@ocap/client/subscribe\" and call install(client) to enable topic subscriptions.';\n\n// Gas-payer signing only applies to `mutation { sendTx(tx: \"...\") ... }`.\n// The legacy path ran a full GraphQL parse (graphql/language/parser ~42 KB)\n// just to pick out the literal `tx` arg. The mutation shape is tightly\n// constrained: `sdk-util`'s builder emits `mutation{sendTx(tx: \"...\", ...)}`\n// and callers never use `$tx` variables. A bounded regex match reproduces\n// the original contract — we degrade to `{}` (no gas-payer header) for any\n// non-matching query, matching the prior behavior for non-sendTx mutations.\nconst SEND_TX_MUTATION_RE = /^\\s*mutation\\b[^{(]*(?:\\([^)]*\\))?\\s*\\{\\s*sendTx\\s*\\(((?:[^\"\\\\)]|\"(?:[^\"\\\\]|\\\\.)*\")*)\\)/;\nconst SEND_TX_ARG_RE = /([a-zA-Z_][a-zA-Z0-9_]*)\\s*:\\s*(?:\"((?:[^\"\\\\]|\\\\.)*)\"|[^,\\s)]+)/g;\n\n/**\n * Provides a readonly client to forge\n * Its recommended to use this version in browser to get smaller bundle size\n *\n * @class GraphQLClientBase\n * @extends {BaseClient}\n */\nclass GraphQLClientBase extends BaseClient {\n constructor(endpoint) {\n if (isAbsoluteUrl(endpoint) === false) {\n throw new Error('Chain endpoint must be a valid url');\n }\n\n super({\n dataSource: 'forge',\n httpEndpoint: endpoint,\n socketEndpoint: () => this._getSocketEndpoint(endpoint),\n enableQuery: true,\n enableSubscription: true,\n enableMutation: true,\n maxQueryDepth: 6,\n });\n\n this._endpoint = endpoint;\n this._socketClient = null;\n }\n\n setGasPayer(wallet, options = {}) {\n this._gasPayer = wallet;\n this._gasPayerOptions = options || {};\n }\n\n subscribe() {\n throw new Error(SUBSCRIBE_NOT_INSTALLED);\n }\n\n unsubscribe() {\n throw new Error(SUBSCRIBE_NOT_INSTALLED);\n }\n\n _ensureSocketClient() {\n throw new Error(SUBSCRIBE_NOT_INSTALLED);\n }\n\n _getSocketEndpoint(endpoint) {\n let socketEndpoint = endpoint.replace(/https?:\\/\\//, 'ws://');\n if (endpoint.indexOf('https://') === 0) {\n socketEndpoint = socketEndpoint.replace('ws://', 'wss://');\n }\n\n return socketEndpoint;\n }\n\n async _getAuthHeaders(query) {\n if (!this._gasPayer) {\n return {};\n }\n if (typeof this._gasPayer.sign !== 'function') {\n return {};\n }\n\n if (typeof query !== 'string') {\n return {};\n }\n\n const sendTxMatch = query.match(SEND_TX_MUTATION_RE);\n if (!sendTxMatch) {\n return {};\n }\n\n let txBase64 = null;\n SEND_TX_ARG_RE.lastIndex = 0;\n for (let match = SEND_TX_ARG_RE.exec(sendTxMatch[1]); match !== null; match = SEND_TX_ARG_RE.exec(sendTxMatch[1])) {\n if (match[1] === 'tx' && typeof match[2] === 'string') {\n txBase64 = match[2];\n break;\n }\n }\n if (!txBase64) {\n return {};\n }\n\n const txBuffer = toUint8Array(fromBase64(txBase64));\n const txHash = Mcrypto.toTxHash(txBuffer);\n const options = this._gasPayerOptions || {};\n\n if (options.allowedTxTypes || options.maxTokenValue !== undefined || typeof options.filter === 'function') {\n let decoded = null;\n try {\n const decodeFn =\n typeof this.decodeTx === 'function' ? this.decodeTx.bind(this) : require('@ocap/message/cbor').decodeTx;\n decoded = decodeFn(txBuffer);\n } catch (_err) {\n return {};\n }\n const decodedTx = decoded || {};\n if (Array.isArray(options.allowedTxTypes)) {\n const normalizeTxType = (t) =>\n String(t || '')\n .replace(/^fg:t:/, '')\n .replace(/Tx$/i, '')\n .replace(/([a-z0-9])([A-Z])/g, '$1_$2')\n .toLowerCase();\n const itx = decodedTx.itx || {};\n const txType = normalizeTxType(itx.type || itx.typeUrl);\n const allowed = options.allowedTxTypes.map(normalizeTxType);\n if (!txType || !allowed.includes(txType)) {\n return {};\n }\n }\n if (options.maxTokenValue !== undefined && options.maxTokenValue !== null) {\n const toAmountBN = (v) => {\n if (!v) return new BN(0);\n if (typeof v === 'object' && v.value instanceof Uint8Array) {\n return new BN(v.value);\n }\n return new BN(String(v));\n };\n let totalValue = new BN(0);\n const rawItx = decodedTx.itx || {};\n const itxVal =\n rawItx.value &&\n typeof rawItx.value === 'object' &&\n !(rawItx.value instanceof Uint8Array) &&\n !rawItx.value.value\n ? rawItx.value\n : rawItx;\n const sumContainerValue = (rawContainer) => {\n let sum = new BN(0);\n if (!rawContainer || typeof rawContainer !== 'object') return sum;\n if (rawContainer.value) {\n sum = sum.add(toAmountBN(rawContainer.value));\n }\n if (Array.isArray(rawContainer.tokens)) {\n for (const rawToken of rawContainer.tokens) {\n const token = rawToken || {};\n if (token.value) {\n sum = sum.add(toAmountBN(token.value));\n }\n }\n }\n return sum;\n };\n try {\n totalValue = totalValue.add(sumContainerValue(itxVal));\n totalValue = totalValue.add(sumContainerValue(itxVal.sender));\n totalValue = totalValue.add(sumContainerValue(itxVal.receiver));\n let inputsTotal = new BN(0);\n if (Array.isArray(itxVal.inputs)) {\n for (const input of itxVal.inputs) {\n inputsTotal = inputsTotal.add(sumContainerValue(input));\n }\n }\n let outputsTotal = new BN(0);\n if (Array.isArray(itxVal.outputs)) {\n for (const output of itxVal.outputs) {\n outputsTotal = outputsTotal.add(sumContainerValue(output));\n }\n }\n totalValue = totalValue.add(inputsTotal.gt(outputsTotal) ? inputsTotal : outputsTotal);\n if (totalValue.gt(new BN(String(options.maxTokenValue)))) {\n return {};\n }\n } catch (_err) {\n return {};\n }\n }\n if (typeof options.filter === 'function') {\n const allowed = await options.filter(decoded, { txHash, txBuffer, query });\n if (!allowed) {\n return {};\n }\n }\n }\n\n const ctx = this.context || {};\n const chainId = options.chainId || ctx.chainId;\n const jwtPayload = chainId ? { txHash, chainId } : { txHash };\n return {\n 'x-gas-payer-pk': this._gasPayer.publicKey,\n 'x-gas-payer-sig': await this._gasPayer.signJWT(jwtPayload),\n };\n }\n\n _getSchema() {\n return jsonpack.unpack(packedSchema);\n }\n\n _getIgnoreFields() {\n return [];\n }\n\n pickGasPayerHeaders({ headers }) {\n return pick(headers, ['x-gas-payer-sig', 'x-gas-payer-pk']);\n }\n}\n\nmodule.exports = GraphQLClientBase;\n"]}
package/dist/browser.d.ts CHANGED
@@ -552,6 +552,7 @@ enum KeyType {
552
552
  ED25519,
553
553
  SECP256K1,
554
554
  ETHEREUM,
555
+ PASSKEY,
555
556
  }
556
557
 
557
558
  enum RoleType {