@xyo-network/xl1-rpc 1.10.0 → 1.10.1

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.
Files changed (40) hide show
  1. package/dist/neutral/index.mjs +64 -19
  2. package/dist/neutral/index.mjs.map +1 -1
  3. package/dist/neutral/provider/client/PermissionsStore/getPermissionsStoreFromTransport.d.ts +1 -1
  4. package/dist/neutral/provider/client/PermissionsStore/getPermissionsStoreFromTransport.d.ts.map +1 -1
  5. package/dist/neutral/provider/connection/XyoConnection.d.ts +2 -2
  6. package/dist/neutral/provider/connection/XyoConnection.d.ts.map +1 -1
  7. package/dist/neutral/transport/post-message/LegacyPostMessageRpcTransport.d.ts +15 -0
  8. package/dist/neutral/transport/post-message/LegacyPostMessageRpcTransport.d.ts.map +1 -0
  9. package/dist/neutral/transport/post-message/index.d.ts +1 -1
  10. package/dist/neutral/transport/post-message/index.d.ts.map +1 -1
  11. package/dist/neutral/transport/post-message/resolveData.d.ts +13 -0
  12. package/dist/neutral/transport/post-message/resolveData.d.ts.map +1 -0
  13. package/dist/node/index-node.mjs +64 -19
  14. package/dist/node/index-node.mjs.map +1 -1
  15. package/dist/node/provider/client/PermissionsStore/getPermissionsStoreFromTransport.d.ts +1 -1
  16. package/dist/node/provider/client/PermissionsStore/getPermissionsStoreFromTransport.d.ts.map +1 -1
  17. package/dist/node/provider/connection/XyoConnection.d.ts +2 -2
  18. package/dist/node/provider/connection/XyoConnection.d.ts.map +1 -1
  19. package/dist/node/transport/post-message/LegacyPostMessageRpcTransport.d.ts +15 -0
  20. package/dist/node/transport/post-message/LegacyPostMessageRpcTransport.d.ts.map +1 -0
  21. package/dist/node/transport/post-message/index.d.ts +1 -1
  22. package/dist/node/transport/post-message/index.d.ts.map +1 -1
  23. package/dist/node/transport/post-message/resolveData.d.ts +13 -0
  24. package/dist/node/transport/post-message/resolveData.d.ts.map +1 -0
  25. package/package.json +26 -26
  26. package/src/provider/client/PermissionsStore/getPermissionsStoreFromTransport.ts +1 -1
  27. package/src/provider/connection/XyoConnection.ts +5 -2
  28. package/src/provider/connection/spec/XyoConnection.ts +1 -0
  29. package/src/provider/signer/spec/RpcEngineXyoSigner.spec.ts +1 -0
  30. package/src/transport/post-message/LegacyPostMessageRpcTransport.ts +117 -0
  31. package/src/transport/post-message/SessionEnvelope.ts +1 -1
  32. package/src/transport/post-message/helpers.ts +1 -1
  33. package/src/transport/post-message/index.ts +1 -1
  34. package/src/transport/post-message/resolveData.ts +30 -0
  35. package/src/types/global.d.ts +1 -1
  36. package/dist/neutral/transport/post-message/PostMessageRpcTransport.d.ts +0 -12
  37. package/dist/neutral/transport/post-message/PostMessageRpcTransport.d.ts.map +0 -1
  38. package/dist/node/transport/post-message/PostMessageRpcTransport.d.ts +0 -12
  39. package/dist/node/transport/post-message/PostMessageRpcTransport.d.ts.map +0 -1
  40. package/src/transport/post-message/PostMessageRpcTransport.ts +0 -73
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolveData.d.ts","sourceRoot":"","sources":["../../../../src/transport/post-message/resolveData.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AAEH,KAAK,iBAAiB,GAAG;IACvB,SAAS,CAAC,EAAE,GAAG,CAAA;IACf,eAAe,CAAC,EAAE,GAAG,CAAA;CACtB,CAAA;AAcD,eAAO,MAAM,aAAa,GAAI,OAAO,YAAY,EAAE,UAAU,MAAM,KAAG,KAAK,GAAG,iBAG7E,CAAA"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "http://json.schemastore.org/package.json",
3
3
  "name": "@xyo-network/xl1-rpc",
4
- "version": "1.10.0",
4
+ "version": "1.10.1",
5
5
  "description": "XYO Layer One API",
6
6
  "homepage": "https://xylabs.com",
7
7
  "bugs": {
@@ -59,26 +59,26 @@
59
59
  "dependencies": {
60
60
  "@metamask/json-rpc-engine": "~10.0.3",
61
61
  "@metamask/utils": "~11.4.2",
62
- "@xylabs/assert": "~5.0.2",
63
- "@xylabs/axios": "~5.0.2",
64
- "@xylabs/exists": "~5.0.2",
65
- "@xylabs/hex": "~5.0.2",
66
- "@xylabs/object": "~5.0.2",
67
- "@xylabs/promise": "~5.0.2",
68
- "@xylabs/typeof": "~5.0.2",
69
- "@xyo-network/account-model": "~5.0.0",
70
- "@xyo-network/archivist-memory": "~5.0.0",
71
- "@xyo-network/archivist-model": "~5.0.0",
72
- "@xyo-network/boundwitness-model": "~5.0.0",
73
- "@xyo-network/hash": "~5.0.0",
74
- "@xyo-network/module-model": "~5.0.0",
75
- "@xyo-network/node-model": "~5.0.0",
76
- "@xyo-network/payload-builder": "~5.0.0",
77
- "@xyo-network/payload-model": "~5.0.0",
78
- "@xyo-network/payload-wrapper": "~5.0.0",
62
+ "@xylabs/assert": "~5.0.3",
63
+ "@xylabs/axios": "~5.0.3",
64
+ "@xylabs/exists": "~5.0.3",
65
+ "@xylabs/hex": "~5.0.3",
66
+ "@xylabs/object": "~5.0.3",
67
+ "@xylabs/promise": "~5.0.3",
68
+ "@xylabs/typeof": "~5.0.3",
69
+ "@xyo-network/account-model": "~5.0.1",
70
+ "@xyo-network/archivist-memory": "~5.0.1",
71
+ "@xyo-network/archivist-model": "~5.0.1",
72
+ "@xyo-network/boundwitness-model": "~5.0.1",
73
+ "@xyo-network/hash": "~5.0.1",
74
+ "@xyo-network/module-model": "~5.0.1",
75
+ "@xyo-network/node-model": "~5.0.1",
76
+ "@xyo-network/payload-builder": "~5.0.1",
77
+ "@xyo-network/payload-model": "~5.0.1",
78
+ "@xyo-network/payload-wrapper": "~5.0.1",
79
79
  "@xyo-network/xl1-protocol": "~1.9.3",
80
- "@xyo-network/xl1-protocol-sdk": "~1.10.0",
81
- "@xyo-network/xl1-wrappers": "~1.10.0",
80
+ "@xyo-network/xl1-protocol-sdk": "~1.10.1",
81
+ "@xyo-network/xl1-wrappers": "~1.10.1",
82
82
  "axios": "~1.11.0",
83
83
  "uuid": "~11.1.0",
84
84
  "zod": "~3.25.76"
@@ -86,12 +86,12 @@
86
86
  "devDependencies": {
87
87
  "@metamask/utils": "~11.4.2",
88
88
  "@types/node": "~24.2.0",
89
- "@xylabs/logger": "~5.0.2",
90
- "@xylabs/ts-scripts-yarn3": "~7.0.3",
91
- "@xylabs/tsconfig": "~7.0.3",
92
- "@xylabs/vitest-extended": "~5.0.2",
93
- "@xyo-network/account": "~5.0.0",
94
- "@xyo-network/wallet": "~5.0.0",
89
+ "@xylabs/logger": "~5.0.3",
90
+ "@xylabs/ts-scripts-yarn3": "~7.1.0",
91
+ "@xylabs/tsconfig": "~7.1.0",
92
+ "@xylabs/vitest-extended": "~5.0.3",
93
+ "@xyo-network/account": "~5.0.1",
94
+ "@xyo-network/wallet": "~5.0.1",
95
95
  "dotenv": "~17.2.1",
96
96
  "typescript": "~5.9.2",
97
97
  "vitest": "~3.2.4"
@@ -2,7 +2,7 @@ import { type RpcTransport } from '../../../transport/index.ts'
2
2
  import type { XyoGatewayRpcSchemas } from '../../../types/index.ts'
3
3
  import type { PermissionsStore } from './PermissionsStore.ts'
4
4
 
5
- export const getPermissionsStoreFromTransport = (transport: RpcTransport<typeof XyoGatewayRpcSchemas>): PermissionsStore => {
5
+ export const getPermissionsStoreFromTransport = (_transport: RpcTransport<typeof XyoGatewayRpcSchemas>): PermissionsStore => {
6
6
  throw new Error('Not implemented')
7
7
  // return {
8
8
  // getPermissions: () => transport.sendRequest('xyoGateway_getPermissions'),
@@ -7,7 +7,7 @@ import type {
7
7
  AllowedBlockPayload,
8
8
  HydratedTransaction,
9
9
  TransactionBoundWitness, TransactionFeesBigInt,
10
- TransactionSubmitterOptions, XyoConnectionProvider, XyoDataLakeProvider, XyoDataLakeViewer, XyoNetwork, XyoRunner, XyoSigner, XyoViewer,
10
+ TransactionOptions, XyoConnectionProvider, XyoDataLakeProvider, XyoDataLakeViewer, XyoNetwork, XyoRunner, XyoSigner, XyoViewer,
11
11
  } from '@xyo-network/xl1-protocol'
12
12
  import { defaultTransactionFees, isDataLakeProvider } from '@xyo-network/xl1-protocol'
13
13
  import { HydratedTransactionWrapper } from '@xyo-network/xl1-wrappers'
@@ -64,6 +64,7 @@ export class XyoConnection implements XyoConnectionProvider {
64
64
  from?: Address,
65
65
  fees?: TransactionFeesBigInt,
66
66
  ): Promise<Signed<TransactionBoundWitness>> {
67
+ // eslint-disable-next-line sonarjs/deprecation
67
68
  return (await this.submitTransaction(elevatedPayloads, additionalPayloads, {
68
69
  chain, nbf, exp, from, fees,
69
70
  }))[0]
@@ -75,9 +76,10 @@ export class XyoConnection implements XyoConnectionProvider {
75
76
  additionalPayloads: Payload[],
76
77
  {
77
78
  chain, exp, fees, from, nbf,
78
- }: TransactionSubmitterOptions = {},
79
+ }: TransactionOptions = {},
79
80
  ): Promise<HydratedTransaction> {
80
81
  const viewer = assertEx(this.viewer, () => 'No viewer available')
82
+ // eslint-disable-next-line sonarjs/deprecation
81
83
  const signer = assertEx(this.signer, () => 'No signer available')
82
84
  const runner = assertEx(this.runner, () => 'No runner available')
83
85
  const storage = this.storage
@@ -88,6 +90,7 @@ export class XyoConnection implements XyoConnectionProvider {
88
90
  const feesResolved = fees ?? defaultTransactionFees
89
91
  const fromResolved = from ?? await signer.address()
90
92
  if (fromResolved === undefined) throw new Error('Failed to determine from address')
93
+ // eslint-disable-next-line sonarjs/deprecation
91
94
  const signedTransaction = assertEx(await signer.createSignedTransaction(
92
95
  chainResolved,
93
96
  elevatedPayloads,
@@ -19,6 +19,7 @@ describe('RpcEngine - XyoProvider', () => {
19
19
  sut = new XyoConnection({ runner, signer })
20
20
  })
21
21
  it('should submit transaction', async () => {
22
+ // eslint-disable-next-line sonarjs/deprecation
22
23
  const result = await sut.send?.([], [], chain, 0, Number.MAX_VALUE)
23
24
  expect(result).toBeDefined()
24
25
  })
@@ -32,6 +32,7 @@ describe('RpcEngine - XyoSigner', () => {
32
32
  })
33
33
  describe('createSignedTransaction', () => {
34
34
  it('should return signed transaction', async () => {
35
+ // eslint-disable-next-line sonarjs/deprecation
35
36
  const signedTransaction = await sut.createSignedTransaction(
36
37
  chain,
37
38
  [],
@@ -0,0 +1,117 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import type { JsonRpcRequest } from '@metamask/utils'
3
+ import {
4
+ isJsonRpcError,
5
+ isJsonRpcSuccess,
6
+ } from '@metamask/utils'
7
+ import { isDefined } from '@xylabs/typeof'
8
+ import { v4 } from 'uuid'
9
+ import type { z } from 'zod'
10
+
11
+ import type { RpcSchemaMap } from '../../types/index.ts'
12
+ import { jsonrpc, XyoGatewayRpcSchemas } from '../../types/index.ts'
13
+ import type { RpcTransport } from '../RpcTransport.ts'
14
+ import { isRpcError } from './helpers.ts'
15
+ import { resolvedEvent } from './resolveData.ts'
16
+
17
+ const isValidBaseRpcEvent = (data: any) => {
18
+ return !!(data && typeof data === 'object'
19
+ && 'jsonrpc' in data && data.jsonrpc === '2.0'
20
+ && 'method' in data && typeof data.method === 'string'
21
+ && 'id' in data && typeof data.id === 'string')
22
+ }
23
+
24
+ const isRpcResponse = (data: any, targetId: string) => {
25
+ return isValidBaseRpcEvent(data) && (isDefined(targetId) ? data.id === targetId : true) && 'result' in data && Array.isArray(data.result)
26
+ }
27
+
28
+ /** @deprecated - now part of a client gateway */
29
+ export class LegacyPostMessageRpcTransport<T extends RpcSchemaMap = RpcSchemaMap> implements RpcTransport<T> {
30
+ protected readonly destination?: string
31
+ protected readonly schemas: T
32
+ protected readonly sessionId?: string
33
+
34
+ constructor(defaultDestination?: string, schemas: T = XyoGatewayRpcSchemas as T, sessionId?: string) {
35
+ this.schemas = schemas
36
+ this.destination = defaultDestination
37
+ this.sessionId = sessionId
38
+ }
39
+
40
+ async sendRequest<
41
+ TMethod extends keyof T & string,
42
+ >(
43
+ method: TMethod,
44
+ params?: z.infer<T[TMethod]['params']['to']>,
45
+ ): Promise<z.infer<T[TMethod]['result']['from']>> {
46
+ const id = v4()
47
+ return await this.callRpc(this.schemas, method, id, params)
48
+ }
49
+
50
+ private async callRpc<
51
+ TSchemas extends RpcSchemaMap,
52
+ TMethod extends keyof TSchemas,
53
+ >(
54
+ schemas: TSchemas,
55
+ method: TMethod,
56
+ id: string,
57
+ params?: z.infer<TSchemas[TMethod]['params']['to']>,
58
+ ): Promise<z.infer<TSchemas[TMethod]['result']['from']>> {
59
+ const body: JsonRpcRequest = {
60
+ jsonrpc,
61
+ id,
62
+ method: method as string,
63
+ }
64
+
65
+ body.params = (isDefined(params) && isDefined(schemas[method])) ? schemas[method].params.to.parse(params) : params
66
+
67
+ return await new Promise((resolve, reject) => {
68
+ const id = body.id
69
+
70
+ const handler = (event: MessageEvent) => {
71
+ if (event.origin !== globalThis.window.origin) return
72
+ if (!isRpcResponse(event.data, id as string)) return
73
+ const data = resolvedEvent(event, id as string)
74
+ if (data === false) return
75
+
76
+ globalThis.removeEventListener('message', handler)
77
+
78
+ if (data.legacyEventData) {
79
+ // Handle legacy event data
80
+ if (data.legacyEventData.result.some(isRpcError)) {
81
+ reject(data.legacyEventData.result[0])
82
+ } else {
83
+ resolve(data.legacyEventData.result[0])
84
+ }
85
+ return
86
+ }
87
+
88
+ if (data.eventData) {
89
+ // Handle new event data
90
+ if (isJsonRpcError(data.eventData)) {
91
+ reject(data.eventData)
92
+ return
93
+ }
94
+ if (isJsonRpcSuccess(data.eventData)) {
95
+ resolve(data.eventData)
96
+ return
97
+ }
98
+ reject(new Error('Unexpected response format'))
99
+ }
100
+ }
101
+
102
+ globalThis.addEventListener('message', handler)
103
+
104
+ globalThis.postMessage(
105
+ {
106
+ data: body,
107
+ destination: this.destination,
108
+ sessionId: this.sessionId,
109
+ },
110
+ globalThis.location.origin,
111
+ )
112
+ })
113
+ }
114
+ }
115
+
116
+ /** @deprecated - use LegacyPostMessageRpcTransport but rely on wallet release > 1.17.1 to handle the postMessage flow */
117
+ export const PostMessageRpcTransport = LegacyPostMessageRpcTransport
@@ -13,7 +13,7 @@ export const buildSessionMessageRequest = <T extends JsonRpcRequest>(data: T, de
13
13
  const request: SessionMessageEnvelope<T> = {
14
14
  data,
15
15
  destination,
16
- sessionId: globalThis.xyoWalletExtensionId ?? '',
16
+ sessionId: globalThis.xyo.walletExtensionId() ?? '',
17
17
  }
18
18
 
19
19
  return request
@@ -13,7 +13,7 @@ export interface RpcError extends JsonObject {
13
13
  }
14
14
 
15
15
  /** @deprecated - no longer part of the public rpc implementation */
16
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, sonarjs/deprecation
17
17
  export const isRpcError = (data: any): data is RpcError => {
18
18
  return (
19
19
  data
@@ -1,2 +1,2 @@
1
- export * from './PostMessageRpcTransport.ts'
1
+ export * from './LegacyPostMessageRpcTransport.ts'
2
2
  export * from './SessionEnvelope.ts'
@@ -0,0 +1,30 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+
3
+ /**
4
+ * NOTE: This file is to help resolve the data from a postMessage event in the transport
5
+ * layer. Older implementations require the id to be at the top level, while newer
6
+ * implementations nest the id inside a data object. Once the wallet extension is updated
7
+ * to always use the nested structure, this file can be removed.
8
+ */
9
+
10
+ type ResolvedEventData = {
11
+ eventData?: any
12
+ legacyEventData?: any
13
+ }
14
+
15
+ // If the id is present in the eventData and matches the targetId, return the eventData
16
+ // and mark it as legacyEventData since newer implementations nest the id inside a data object
17
+ const hasMatchingId = (eventData: any, targetId: string): false | ResolvedEventData => {
18
+ return eventData?.id === targetId ? { legacyEventData: eventData } : false
19
+ }
20
+
21
+ // If the id is present in the data object of the eventData and matches the targetId,
22
+ // return the eventData as eventData so its clear that this is a newer implementation
23
+ const hasMatchingNestedId = (eventData: any, targetId: string): false | ResolvedEventData => {
24
+ return eventData?.data?.id === targetId ? { eventData: eventData.data } : false
25
+ }
26
+
27
+ export const resolvedEvent = (event: MessageEvent, targetId: string): false | ResolvedEventData => {
28
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
29
+ return hasMatchingId(event.data, targetId) || hasMatchingNestedId(event.data, targetId)
30
+ }
@@ -1,4 +1,3 @@
1
- // TODO - add optional send method of type Transport
2
1
  export interface XyoGlobal {
3
2
  _debug: () => void
4
3
  provider?: XyoProvider
@@ -11,5 +10,6 @@ export declare global {
11
10
  }
12
11
 
13
12
  var xyo: XyoGlobal
13
+ /** @deprecated - use globalThis.xyo.walletExtensionId() instead */
14
14
  var xyoWalletExtensionId: string
15
15
  }
@@ -1,12 +0,0 @@
1
- import type { z } from 'zod';
2
- import type { RpcSchemaMap } from '../../types/index.ts';
3
- import type { RpcTransport } from '../RpcTransport.ts';
4
- /** @deprecated - now part of a client gateway */
5
- export declare class PostMessageRpcTransport<T extends RpcSchemaMap = RpcSchemaMap> implements RpcTransport<T> {
6
- protected readonly destination?: string;
7
- protected readonly schemas: T;
8
- constructor(defaultDestination?: string, schemas?: T);
9
- sendRequest<TMethod extends keyof T & string>(method: TMethod, params?: z.infer<T[TMethod]['params']['to']>): Promise<z.infer<T[TMethod]['result']['from']>>;
10
- private callRpc;
11
- }
12
- //# sourceMappingURL=PostMessageRpcTransport.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"PostMessageRpcTransport.d.ts","sourceRoot":"","sources":["../../../../src/transport/post-message/PostMessageRpcTransport.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAE5B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAExD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAKtD,iDAAiD;AACjD,qBAAa,uBAAuB,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,CAAE,YAAW,YAAY,CAAC,CAAC,CAAC;IACpG,SAAS,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAA;IACvC,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAA;gBAEjB,kBAAkB,CAAC,EAAE,MAAM,EAAE,OAAO,GAAE,CAA6B;IAKzE,WAAW,CACf,OAAO,SAAS,MAAM,CAAC,GAAG,MAAM,EAEhC,MAAM,EAAE,OAAO,EACf,MAAM,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,GAC3C,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;YAKnC,OAAO;CAwCtB"}
@@ -1,12 +0,0 @@
1
- import type { z } from 'zod';
2
- import type { RpcSchemaMap } from '../../types/index.ts';
3
- import type { RpcTransport } from '../RpcTransport.ts';
4
- /** @deprecated - now part of a client gateway */
5
- export declare class PostMessageRpcTransport<T extends RpcSchemaMap = RpcSchemaMap> implements RpcTransport<T> {
6
- protected readonly destination?: string;
7
- protected readonly schemas: T;
8
- constructor(defaultDestination?: string, schemas?: T);
9
- sendRequest<TMethod extends keyof T & string>(method: TMethod, params?: z.infer<T[TMethod]['params']['to']>): Promise<z.infer<T[TMethod]['result']['from']>>;
10
- private callRpc;
11
- }
12
- //# sourceMappingURL=PostMessageRpcTransport.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"PostMessageRpcTransport.d.ts","sourceRoot":"","sources":["../../../../src/transport/post-message/PostMessageRpcTransport.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAE5B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAExD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAKtD,iDAAiD;AACjD,qBAAa,uBAAuB,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,CAAE,YAAW,YAAY,CAAC,CAAC,CAAC;IACpG,SAAS,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAA;IACvC,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAA;gBAEjB,kBAAkB,CAAC,EAAE,MAAM,EAAE,OAAO,GAAE,CAA6B;IAKzE,WAAW,CACf,OAAO,SAAS,MAAM,CAAC,GAAG,MAAM,EAEhC,MAAM,EAAE,OAAO,EACf,MAAM,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,GAC3C,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;YAKnC,OAAO;CAwCtB"}
@@ -1,73 +0,0 @@
1
- import type { JsonRpcRequest } from '@metamask/utils'
2
- import { isDefined } from '@xylabs/typeof'
3
- import { v4 } from 'uuid'
4
- import type { z } from 'zod'
5
-
6
- import type { RpcSchemaMap } from '../../types/index.ts'
7
- import { jsonrpc, XyoGatewayRpcSchemas } from '../../types/index.ts'
8
- import type { RpcTransport } from '../RpcTransport.ts'
9
- import type { JsonRpcRequestWithId } from './helpers.ts'
10
- import { isRpcError } from './helpers.ts'
11
- import { buildSessionMessageRequest } from './SessionEnvelope.ts'
12
-
13
- /** @deprecated - now part of a client gateway */
14
- export class PostMessageRpcTransport<T extends RpcSchemaMap = RpcSchemaMap> implements RpcTransport<T> {
15
- protected readonly destination?: string
16
- protected readonly schemas: T
17
-
18
- constructor(defaultDestination?: string, schemas: T = XyoGatewayRpcSchemas as T) {
19
- this.schemas = schemas
20
- this.destination = defaultDestination
21
- }
22
-
23
- async sendRequest<
24
- TMethod extends keyof T & string,
25
- >(
26
- method: TMethod,
27
- params?: z.infer<T[TMethod]['params']['to']>,
28
- ): Promise<z.infer<T[TMethod]['result']['from']>> {
29
- const id = v4()
30
- return await this.callRpc(this.schemas, method, id, params)
31
- }
32
-
33
- private async callRpc<
34
- TSchemas extends RpcSchemaMap,
35
- TMethod extends keyof TSchemas,
36
- >(
37
- schemas: TSchemas,
38
- method: TMethod,
39
- id: string,
40
- params?: z.infer<TSchemas[TMethod]['params']['to']>,
41
- ): Promise<z.infer<TSchemas[TMethod]['result']['from']>> {
42
- const body: JsonRpcRequest = {
43
- jsonrpc,
44
- id,
45
- method: method as string,
46
- }
47
-
48
- body.params = (isDefined(params) && isDefined(schemas[method])) ? schemas[method].params.to.parse(params) : params
49
-
50
- return await new Promise((resolve, reject) => {
51
- const id = body.id
52
-
53
- const handler = (event: MessageEvent) => {
54
- if (event.origin !== globalThis.window.origin) return
55
- if ((event.data as JsonRpcRequestWithId).id !== id) return
56
-
57
- globalThis.removeEventListener('message', handler)
58
- if (event.data.result.some(isRpcError)) {
59
- reject(event.data.result[0])
60
- } else {
61
- resolve(event.data.result[0])
62
- }
63
- }
64
-
65
- globalThis.addEventListener('message', handler)
66
-
67
- globalThis.postMessage(
68
- buildSessionMessageRequest(body, this.destination),
69
- globalThis.location.origin,
70
- )
71
- })
72
- }
73
- }